From 2041fc78d3dffbd26cdbc50c4d74cacb9be9e249 Mon Sep 17 00:00:00 2001 From: Brandon Shipley Date: Sat, 1 Nov 2025 17:00:15 -0700 Subject: [PATCH] more thorogh testing of toggle behavior on product photos --- .../ProductPhotosControllerTest.php | 94 ++++++++++++++++++- 1 file changed, 93 insertions(+), 1 deletion(-) diff --git a/tests/TestCase/Controller/ProductPhotosControllerTest.php b/tests/TestCase/Controller/ProductPhotosControllerTest.php index 682a9ad..71a41b3 100644 --- a/tests/TestCase/Controller/ProductPhotosControllerTest.php +++ b/tests/TestCase/Controller/ProductPhotosControllerTest.php @@ -515,10 +515,15 @@ class ProductPhotosControllerTest extends BaseControllerTest * * @uses \CakeProducts\Controller\ProductPhotosController::delete */ - public function testDeleteLoggedIn(): void + public function testDeleteLoggedInDefaultProductPhoto(): void { $cntBefore = $this->ProductPhotos->find()->count(); $id = '2c386086-f4c5-4093-bea5-ee9c29479f58'; + $productId = 'cfc98a9a-29b2-44c8-b587-8156adc05317'; + $primaryPhotoBefore = $this->ProductPhotos->find()->where(['primary_photo' => true, 'product_id' => $productId])->first(); + $primaryPhotosBeforeCnt = $this->ProductPhotos->find()->where(['primary_photo' => true, 'product_id' => $productId])->count(); + $this->assertEquals(1, $primaryPhotosBeforeCnt); + // $this->loginUserByRole('admin'); $url = [ 'plugin' => 'CakeProducts', @@ -532,6 +537,93 @@ class ProductPhotosControllerTest extends BaseControllerTest $cntAfter = $this->ProductPhotos->find()->count(); $this->assertEquals($cntBefore - 1, $cntAfter); + + $primaryPhotoAfter = $this->ProductPhotos->find()->where(['primary_photo' => true, 'product_id' => $productId])->first(); + $primaryPhotosAfterCnt = $this->ProductPhotos->find()->where(['primary_photo' => true, 'product_id' => $productId])->count(); + $this->assertEquals(1, $primaryPhotosAfterCnt); + $this->assertNotEquals($primaryPhotoBefore->id, $primaryPhotoAfter->id); + } + + /** + * Test delete method + * + * Tests the delete action with a logged in user + * + * @return void + * @throws Exception + * + * @uses \CakeProducts\Controller\ProductPhotosController::delete + */ + public function testDeleteLoggedInDefaultCategoryPhoto(): void + { + $cntBefore = $this->ProductPhotos->find()->count(); + $id = '2c386086-f4c5-4093-bea5-ee9c29479f51'; + $productId = 'cfc98a9a-29b2-44c8-b587-8156adc05317'; + $categoryId = '6d223283-361b-4f9f-a7f1-c97aa0ca4c23'; + + $primaryPhotoBefore = $this->ProductPhotos->find()->where(['primary_category_photo' => true, 'product_category_id' => $categoryId])->first(); + $primaryPhotosBeforeCnt = $this->ProductPhotos->find()->where(['primary_category_photo' => true, 'product_category_id' => $categoryId])->count(); + $this->assertEquals(1, $primaryPhotosBeforeCnt); + + // $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); + + $primaryPhotoAfter = $this->ProductPhotos->find()->where(['primary_category_photo' => true, 'product_category_id' => $categoryId])->first(); + $primaryPhotosAfterCnt = $this->ProductPhotos->find()->where(['primary_category_photo' => true, 'product_category_id' => $categoryId])->count(); + $this->assertEquals(1, $primaryPhotosAfterCnt); + $this->assertNotEquals($primaryPhotoBefore->id, $primaryPhotoAfter->id); + } + + /** + * Test delete method + * + * Tests the delete action with a logged in user + * + * @return void + * @throws Exception + * + * @uses \CakeProducts\Controller\ProductPhotosController::delete + */ + public function testDeleteLoggedInDefaultSkuPhoto(): void + { + $cntBefore = $this->ProductPhotos->find()->count(); + $id = '2c386086-f4c5-4093-bea5-ee9c29479f11'; + $productId = 'cfc98a9a-29b2-44c8-b587-8156adc05317'; + $skuId = '3a477e3e-7977-4813-81f6-f85949613979'; + + $primaryPhotoBefore = $this->ProductPhotos->find()->where(['primary_sku_photo' => true, 'product_sku_id' => $skuId])->first(); + $primaryPhotosBeforeCnt = $this->ProductPhotos->find()->where(['primary_sku_photo' => true, 'product_sku_id' => $skuId])->count(); + $this->assertEquals(1, $primaryPhotosBeforeCnt); + + // $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); + + $primaryPhotoAfter = $this->ProductPhotos->find()->where(['primary_sku_photo' => true, 'product_sku_id' => $skuId])->first(); + $primaryPhotosAfterCnt = $this->ProductPhotos->find()->where(['primary_sku_photo' => true, 'product_sku_id' => $skuId])->count(); + $this->assertEquals(1, $primaryPhotosAfterCnt); + $this->assertNotEquals($primaryPhotoBefore->id, $primaryPhotoAfter->id); } /**