*/ protected array $fixtures = [ 'plugin.CakeProducts.Products', 'plugin.CakeProducts.ProductSkus', 'plugin.CakeProducts.ProductPhotos', ]; /** * setUp method * * @return void */ protected function setUp(): void { parent::setUp(); $this->ProductPhotos = $this->getTableLocator()->get('ProductPhotos'); } /** * tearDown method * * @return void */ protected function tearDown(): void { unset($this->ProductPhotos); $path = Configure::readOrFail('CakeProducts.photos.directory'); if (file_exists($path)) { $di = new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS); $ri = new RecursiveIteratorIterator($di, RecursiveIteratorIterator::CHILD_FIRST); foreach ( $ri as $file ) { $file->isDir() ? rmdir($file->getRealPath()) : unlink($file->getRealPath()); } } parent::tearDown(); } /** * Test index method * * Tests the index action with a logged in user * * @return void * @throws Exception * * @uses ProductPhotosController::index */ public function testIndexGetLoggedIn(): void { // $this->loginUserByRole('admin'); $url = [ 'plugin' => 'CakeProducts', 'controller' => 'ProductPhotos', 'action' => 'index', ]; $this->get($url); $this->assertResponseCode(200); } /** * Test view method * * Tests the view action with a logged in user * * @return void * @throws Exception * * @uses ProductPhotosController::view */ public function testViewGetLoggedIn(): void { $id = '2c386086-f4c5-4093-bea5-ee9c29479f58'; // $this->loginUserByRole('admin'); $url = [ 'plugin' => 'CakeProducts', 'controller' => 'ProductPhotos', 'action' => 'view', $id, ]; $this->get($url); $this->assertResponseCode(200); } /** * Test add method * * Tests the add action with a logged in user * * @return void * @throws Exception * * @uses ProductPhotosController::add */ public function testAddGetLoggedIn(): void { $cntBefore = $this->ProductPhotos->find()->count(); // $this->loginUserByRole('admin'); $url = [ 'plugin' => 'CakeProducts', 'controller' => 'ProductPhotos', 'action' => 'add', ]; $this->get($url); $this->assertResponseCode(200); $cntAfter = $this->ProductPhotos->find()->count(); $this->assertEquals($cntBefore, $cntAfter); } /** * Test add method * * Tests a POST request to the add action with a logged in user * * @return void * @throws Exception * * @uses ProductPhotosController::add */ public function testAddPostLoggedInSuccess(): void { $cntBefore = $this->ProductPhotos->find()->count(); // $this->loginUserByRole('admin'); $url = [ 'plugin' => 'CakeProducts', '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( $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 'image/png' // the mimetype as sent by the client ); $this->configRequest([ 'files' => [ 'photo' => $image, ], ]); $data = [ 'product_id' => 'cfc98a9a-29b2-44c8-b587-8156adc05317', 'product_sku_id' => '', 'primary_photo' => 0, 'photo' => $image, 'enabled' => 1, ]; $this->post($url, $data); $this->assertResponseCode(302); $this->assertRedirectContains('product-photos'); $cntAfter = $this->ProductPhotos->find()->count(); $this->assertEquals($cntBefore + 1, $cntAfter); } /** * Test add method * * Tests a POST request to the add action with a logged in user * * @return void * @throws Exception * * @uses ProductPhotosController::add */ public function testAddPostLoggedInFailure(): void { $cntBefore = $this->ProductPhotos->find()->count(); // $this->loginUserByRole('admin'); $url = [ 'plugin' => 'CakeProducts', 'controller' => 'ProductPhotos', 'action' => 'add', ]; $data = [ ]; $this->post($url, $data); $this->assertResponseCode(200); $cntAfter = $this->ProductPhotos->find()->count(); $this->assertEquals($cntBefore, $cntAfter); } /** * Test edit method * * Tests the edit action with a logged in user * * @return void * @throws Exception * * @uses ProductPhotosController::edit */ public function testEditGetLoggedIn(): void { // $this->loginUserByRole('admin'); $id = '2c386086-f4c5-4093-bea5-ee9c29479f58'; $url = [ 'plugin' => 'CakeProducts', 'controller' => 'ProductPhotos', 'action' => 'edit', $id, ]; $this->get($url); $this->assertResponseCode(200); } /** * Test edit method * * Tests a PUT request to the edit action with a logged in user * * @return void * @throws Exception * * @uses ProductPhotosController::edit */ public function testEditPutLoggedInSuccess(): void { // $this->loginUserByRole('admin'); $id = '2c386086-f4c5-4093-bea5-ee9c29479f58'; $before = $this->ProductPhotos->get($id); $url = [ 'plugin' => 'CakeProducts', 'controller' => 'ProductPhotos', 'action' => 'edit', $id, ]; $data = [ // test new data here 'enabled' => 1, 'primary_photo' => 1, 'photo_position' => 999, ]; // $this->put($url, $data); $this->assertResponseCode(302); $this->assertRedirectContains('product-photos'); $after = $this->ProductPhotos->get($id); // assert saved properly below } /** * Test edit method * * Tests a PUT request to the edit action with a logged in user * * @return void * @throws Exception * * @uses ProductPhotosController::edit */ public function testEditPutLoggedInFailure(): void { // $this->loginUserByRole('admin'); $id = '2c386086-f4c5-4093-bea5-ee9c29479f58'; $before = $this->ProductPhotos->get($id); $url = [ 'plugin' => 'CakeProducts', 'controller' => 'ProductPhotos', 'action' => 'edit', $id, ]; $data = []; $this->put($url, $data); $this->assertResponseCode(200); $after = $this->ProductPhotos->get($id); // assert save failed below } /** * Test delete method * * Tests the delete action with a logged in user * * @return void * @throws Exception * * @uses ProductPhotosController::delete */ public function testDeleteLoggedIn(): void { $cntBefore = $this->ProductPhotos->find()->count(); $id = '2c386086-f4c5-4093-bea5-ee9c29479f58'; // $this->loginUserByRole('admin'); $url = [ 'plugin' => 'CakeProducts', 'controller' => 'ProductPhotos', 'action' => 'delete', $id, ]; $this->delete($url); $this->assertResponseCode(302); $this->assertRedirectContains('product-photos'); $cntAfter = $this->ProductPhotos->find()->count(); $this->assertEquals($cntBefore - 1, $cntAfter); } }