primary sku photo working with janky toggle workaround but w/e lol

This commit is contained in:
2025-11-01 16:10:48 -07:00
parent 41c5f7169e
commit b145e901ef
9 changed files with 226 additions and 10 deletions

View File

@@ -148,7 +148,7 @@ class ProductPhotosControllerTest extends BaseControllerTest
*
* @uses \CakeProducts\Controller\ProductPhotosController::add
*/
public function testAddPostLoggedInSuccessDefaultProductPhoto(): void
public function testAddPostLoggedInSuccessDefaultPhoto(): void
{
$cntBefore = $this->ProductPhotos->find()->count();
@@ -186,13 +186,14 @@ class ProductPhotosControllerTest extends BaseControllerTest
]);
$data = [
'product_id' => $productId,
'product_sku_id' => '',
'primary_photo' => 1,
'primary_category_photo' => 1,
'primary_category_photo' => 0,
'primary_sku_photo' => 0,
'photo' => $image,
'enabled' => 1,
];
$this->post($url, $data);
// dd($this->_response);
$this->assertResponseCode(302);
$this->assertRedirectContains('product-photos');
@@ -202,13 +203,16 @@ class ProductPhotosControllerTest extends BaseControllerTest
$new = $this->ProductPhotos->find()->where(['product_id' => $productId])->orderBy(['created' => 'DESC'])->first();
$this->assertTrue($new->primary_photo);
$this->assertTrue($new->primary_category_photo);
$this->assertFalse($new->primary_category_photo);
$this->assertFalse($new->primary_sku_photo);
$this->assertEquals($productId, $new->product_id);
$primaryPhotosCountAfter = $this->ProductPhotos->find()->where(['product_id' => $productId, 'primary_photo' => true])->count();
$primaryPhotoAfter = $this->ProductPhotos->find()->where(['product_id' => $productId, 'primary_photo' => true])->first();
$this->assertEquals(1, $primaryPhotosCountAfter);
$this->assertNotEquals($primaryPhotoBefore->id, $primaryPhotoAfter->id);
$this->assertEquals($new->id, $primaryPhotoAfter->id);
}
/**
@@ -266,6 +270,7 @@ class ProductPhotosControllerTest extends BaseControllerTest
'product_sku_id' => '',
'primary_photo' => 0,
'primary_category_photo' => 1,
'primary_sku_photo' => 0,
'photo' => $image,
'enabled' => 1,
];
@@ -290,6 +295,89 @@ class ProductPhotosControllerTest extends BaseControllerTest
}
/**
* Test add method
*
* Tests a POST request to the add action with a logged in user
*
* @return void
* @throws Exception
*
* @TODO handle toggle behavior when scope field is nullable
*
* @uses \CakeProducts\Controller\ProductPhotosController::add
*/
public function testAddPostLoggedInSuccessDefaultSkuPhoto(): 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');
}
$productId = 'cfc98a9a-29b2-44c8-b587-8156adc05317';
$skuId = '3a477e3e-7977-4813-81f6-f85949613979';
$primarySkuPhotosCountBefore = $this->ProductPhotos->find()->where(['product_sku_id' => $skuId, 'primary_sku_photo' => true])->count();
$primarySkuPhotoBefore = $this->ProductPhotos->find()->where(['product_sku_id' => $skuId, 'primary_sku_photo' => true])->first();
$this->assertEquals(1, $primarySkuPhotosCountBefore);
$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' => $productId,
'product_sku_id' => $skuId,
'primary_photo' => 0,
'primary_category_photo' => 0,
'primary_sku_photo' => 1,
'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);
$new = $this->ProductPhotos->find()->where(['product_sku_id' => $skuId])->orderBy(['created' => 'DESC'])->first();
$this->assertFalse($new->primary_photo);
$this->assertFalse($new->primary_category_photo);
$this->assertTrue($new->primary_sku_photo);
$primarySkuPhotosCountAfter = $this->ProductPhotos->find()->where(['product_sku_id' => $skuId, 'primary_sku_photo' => true])->count();
$primarySkuPhotoAfter = $this->ProductPhotos->find()->where(['product_sku_id' => $skuId, 'primary_sku_photo' => true])->first();
$this->assertEquals(1, $primarySkuPhotosCountAfter);
$this->assertNotEquals($primarySkuPhotoBefore->id, $primarySkuPhotoAfter->id);
$this->assertEquals($new->id, $primarySkuPhotoAfter->id);
}
/**
* Test add method
*