From 3a47f09e8c76bd3a1953bd3c69d481e30f54ae1e Mon Sep 17 00:00:00 2001 From: Brandon Shipley Date: Sun, 10 Aug 2025 02:50:22 -0700 Subject: [PATCH] upload test passing + file not getting deleted every time - well it is but we now use a copy lol --- src/Controller/ProductPhotosController.php | 7 +++++++ .../Controller/ProductPhotosControllerTest.php | 13 +++++++++++-- tests/bootstrap.php | 1 + .../test_app/webroot}/images/cake_icon.png | Bin 4 files changed, 19 insertions(+), 2 deletions(-) rename {webroot => tests/test_app/webroot}/images/cake_icon.png (100%) diff --git a/src/Controller/ProductPhotosController.php b/src/Controller/ProductPhotosController.php index e2f43fc..efdb264 100644 --- a/src/Controller/ProductPhotosController.php +++ b/src/Controller/ProductPhotosController.php @@ -56,6 +56,13 @@ class ProductPhotosController extends AppController $productPhotosTable = $this->getTable(); $productPhoto = $productPhotosTable->newEmptyEntity(); if ($this->request->is('post')) { + if (!$this->request->getData('photo')) { + $this->Flash->error('Photo is required. Nothing was uploaded. Please try again.'); + $products = $productPhotosTable->Products->find('list', limit: 200)->all(); + $this->set(compact('productPhoto', 'products')); + + return; + } $uuid = Text::uuid(); $postData = $this->request->getData(); $postData['id'] = $uuid; diff --git a/tests/TestCase/Controller/ProductPhotosControllerTest.php b/tests/TestCase/Controller/ProductPhotosControllerTest.php index d8f4b3a..8eb2329 100644 --- a/tests/TestCase/Controller/ProductPhotosControllerTest.php +++ b/tests/TestCase/Controller/ProductPhotosControllerTest.php @@ -162,8 +162,16 @@ class ProductPhotosControllerTest extends BaseControllerTest 'controller' => 'ProductPhotos', 'action' => 'add', ]; + $file = Configure::readOrFail('App.paths.testWebroot') . 'images' . DS . 'cake_icon.png'; + $toUseFile = Configure::readOrFail('App.paths.testWebroot') . 'images' . DS . 'cake_icon_copy.png'; + if (!file_exists($file)) { + $this->fail('Test image did not exist'); + } + if (!copy($file, $toUseFile)) { + $this->fail('Failed to copy test image'); + } $image = new UploadedFile( - Configure::readOrFail('App.paths.webroot') . 'images' . DS . 'cake_icon.png', // stream or path to file representing the temp file + $toUseFile, // stream or path to file representing the temp file 12345, // the filesize in bytes UPLOAD_ERR_OK, // the upload/error status 'cake_icon.png', // the filename as sent by the client @@ -209,7 +217,8 @@ class ProductPhotosControllerTest extends BaseControllerTest 'controller' => 'ProductPhotos', 'action' => 'add', ]; - $data = []; + $data = [ + ]; $this->post($url, $data); $this->assertResponseCode(200); diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 662ce6b..ec76d1f 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -48,6 +48,7 @@ Configure::write('App', [ 'namespace' => 'TestApp', 'encoding' => 'UTF-8', 'paths' => [ + 'testWebroot' => PLUGIN_ROOT . DS . 'tests' . DS . 'test_app' . DS . 'webroot' . DS, 'webroot' => PLUGIN_ROOT . DS . 'webroot' . DS, 'templates' => [ PLUGIN_ROOT . DS . 'tests' . DS . 'test_app' . DS . 'templates' . DS, diff --git a/webroot/images/cake_icon.png b/tests/test_app/webroot/images/cake_icon.png similarity index 100% rename from webroot/images/cake_icon.png rename to tests/test_app/webroot/images/cake_icon.png