From 0181c4890726d219349b9c32280dc49907bdafec Mon Sep 17 00:00:00 2001 From: Brandon Shipley Date: Mon, 11 Aug 2025 23:35:46 -0700 Subject: [PATCH] product photos controller test fixes, test out --- src/Controller/ProductPhotosController.php | 10 ++-- src/Model/Table/ProductPhotosTable.php | 58 +++++++++++-------- .../ProductPhotosControllerTest.php | 14 ++++- 3 files changed, 50 insertions(+), 32 deletions(-) diff --git a/src/Controller/ProductPhotosController.php b/src/Controller/ProductPhotosController.php index efdb264..c6cb2f7 100644 --- a/src/Controller/ProductPhotosController.php +++ b/src/Controller/ProductPhotosController.php @@ -80,7 +80,6 @@ class ProductPhotosController extends AppController if (!file_exists($path)) { if (!mkdir($path, 0777, true)) { - dd('Failed to create the required folders. Please check the folder permissions and try again. PATH: ' . $path); throw new ForbiddenException('Failed to create the required folders. Please check the folder permissions and try again.'); } } @@ -89,7 +88,6 @@ class ProductPhotosController extends AppController // Existing files with the same name will be replaced. $photoObject->moveTo($destination); if (!file_exists($destination)) { - dd('Failed to move the uploaded image to the appropriate folder. Please try again.'); throw new ForbiddenException('Failed to move the uploaded image to the appropriate folder. Please try again.'); } $postData['photo_dir'] = $path; @@ -100,14 +98,13 @@ class ProductPhotosController extends AppController return $this->redirect(['action' => 'index']); } - dd(print_r($productPhoto->getErrors(), true)); $this->Flash->error(__('The product photo could not be saved. Please, try again.')); } $products = $productPhotosTable->Products->find('list', limit: 200)->all(); $this->set(compact('productPhoto', 'products')); } - /** +d /** * Edit method * * @param string|null $id Product Photo id. @@ -119,13 +116,14 @@ class ProductPhotosController extends AppController $productPhotosTable = $this->getTable(); $productPhoto = $productPhotosTable->get($id, contain: []); if ($this->request->is(['patch', 'post', 'put'])) { - $productPhoto = $productPhotosTable->patchEntity($productPhoto, $this->request->getData()); + $postData = $this->request->getData(); + + $productPhoto = $productPhotosTable->patchEntity($productPhoto, $postData); if ($productPhotosTable->save($productPhoto)) { $this->Flash->success(__('The product photo has been saved.')); return $this->redirect(['action' => 'index']); } - dd(print_r($productPhoto->getErrors(), true)); $this->Flash->error(__('The product photo could not be saved. Please, try again.')); } $products = $productPhotosTable->Products->find('list', limit: 200)->all(); diff --git a/src/Model/Table/ProductPhotosTable.php b/src/Model/Table/ProductPhotosTable.php index 1cd436c..254e655 100644 --- a/src/Model/Table/ProductPhotosTable.php +++ b/src/Model/Table/ProductPhotosTable.php @@ -3,31 +3,38 @@ declare(strict_types=1); namespace CakeProducts\Model\Table; +use Cake\Datasource\EntityInterface; +use Cake\Datasource\ResultSetInterface; +use Cake\ORM\Association\BelongsTo; +use Cake\ORM\Behavior\TimestampBehavior; use Cake\ORM\Query\SelectQuery; use Cake\ORM\RulesChecker; use Cake\ORM\Table; use Cake\Validation\Validator; +use CakeProducts\Model\Entity\ProductPhoto; +use Closure; +use Psr\SimpleCache\CacheInterface; /** * ProductPhotos Model * - * @property \CakeProducts\Model\Table\ProductsTable&\Cake\ORM\Association\BelongsTo $Products + * @property ProductsTable&BelongsTo $Products * - * @method \CakeProducts\Model\Entity\ProductPhoto newEmptyEntity() - * @method \CakeProducts\Model\Entity\ProductPhoto newEntity(array $data, array $options = []) - * @method array<\CakeProducts\Model\Entity\ProductPhoto> newEntities(array $data, array $options = []) - * @method \CakeProducts\Model\Entity\ProductPhoto get(mixed $primaryKey, array|string $finder = 'all', \Psr\SimpleCache\CacheInterface|string|null $cache = null, \Closure|string|null $cacheKey = null, mixed ...$args) - * @method \CakeProducts\Model\Entity\ProductPhoto findOrCreate($search, ?callable $callback = null, array $options = []) - * @method \CakeProducts\Model\Entity\ProductPhoto patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = []) - * @method array<\CakeProducts\Model\Entity\ProductPhoto> patchEntities(iterable $entities, array $data, array $options = []) - * @method \CakeProducts\Model\Entity\ProductPhoto|false save(\Cake\Datasource\EntityInterface $entity, array $options = []) - * @method \CakeProducts\Model\Entity\ProductPhoto saveOrFail(\Cake\Datasource\EntityInterface $entity, array $options = []) - * @method iterable<\CakeProducts\Model\Entity\ProductPhoto>|\Cake\Datasource\ResultSetInterface<\CakeProducts\Model\Entity\ProductPhoto>|false saveMany(iterable $entities, array $options = []) - * @method iterable<\CakeProducts\Model\Entity\ProductPhoto>|\Cake\Datasource\ResultSetInterface<\CakeProducts\Model\Entity\ProductPhoto> saveManyOrFail(iterable $entities, array $options = []) - * @method iterable<\CakeProducts\Model\Entity\ProductPhoto>|\Cake\Datasource\ResultSetInterface<\CakeProducts\Model\Entity\ProductPhoto>|false deleteMany(iterable $entities, array $options = []) - * @method iterable<\CakeProducts\Model\Entity\ProductPhoto>|\Cake\Datasource\ResultSetInterface<\CakeProducts\Model\Entity\ProductPhoto> deleteManyOrFail(iterable $entities, array $options = []) + * @method ProductPhoto newEmptyEntity() + * @method ProductPhoto newEntity(array $data, array $options = []) + * @method array newEntities(array $data, array $options = []) + * @method ProductPhoto get(mixed $primaryKey, array|string $finder = 'all', CacheInterface|string|null $cache = null, Closure|string|null $cacheKey = null, mixed ...$args) + * @method ProductPhoto findOrCreate($search, ?callable $callback = null, array $options = []) + * @method ProductPhoto patchEntity(EntityInterface $entity, array $data, array $options = []) + * @method array patchEntities(iterable $entities, array $data, array $options = []) + * @method ProductPhoto|false save(EntityInterface $entity, array $options = []) + * @method ProductPhoto saveOrFail(EntityInterface $entity, array $options = []) + * @method iterable|ResultSetInterface|false saveMany(iterable $entities, array $options = []) + * @method iterable|ResultSetInterface saveManyOrFail(iterable $entities, array $options = []) + * @method iterable|ResultSetInterface|false deleteMany(iterable $entities, array $options = []) + * @method iterable|ResultSetInterface deleteManyOrFail(iterable $entities, array $options = []) * - * @mixin \Cake\ORM\Behavior\TimestampBehavior + * @mixin TimestampBehavior */ class ProductPhotosTable extends Table { @@ -52,13 +59,19 @@ class ProductPhotosTable extends Table 'joinType' => 'INNER', 'className' => 'CakeProducts.Products', ]); + + $this->belongsTo('ProductSkus', [ + 'foreignKey' => 'product_sku_id', + 'joinType' => 'LEFT', + 'className' => 'CakeProducts.ProductSkus', + ]); } /** * Default validation rules. * - * @param \Cake\Validation\Validator $validator Validator instance. - * @return \Cake\Validation\Validator + * @param Validator $validator Validator instance. + * @return Validator */ public function validationDefault(Validator $validator): Validator { @@ -68,8 +81,7 @@ class ProductPhotosTable extends Table $validator ->uuid('product_sku_id') - ->allowEmptyString('product_sku_id') - ->add('product_sku_id', 'unique', ['rule' => 'validateUnique', 'provider' => 'table']); + ->allowEmptyString('product_sku_id'); $validator ->scalar('photo_dir') @@ -106,13 +118,13 @@ class ProductPhotosTable extends Table * Returns a rules checker object that will be used for validating * application integrity. * - * @param \Cake\ORM\RulesChecker $rules The rules object to be modified. - * @return \Cake\ORM\RulesChecker + * @param RulesChecker $rules The rules object to be modified. + * @return RulesChecker */ public function buildRules(RulesChecker $rules): RulesChecker { - $rules->add($rules->isUnique(['product_sku_id'], ['allowMultipleNulls' => true]), ['errorField' => '0']); - $rules->add($rules->existsIn(['product_id'], 'Products'), ['errorField' => '1']); + $rules->add($rules->existsIn(['product_id'], 'Products'), ['errorField' => 'product_id']); + $rules->add($rules->existsIn(['product_sku_id'], 'ProductSkus'), ['errorField' => 'product_sku_id']); return $rules; } diff --git a/tests/TestCase/Controller/ProductPhotosControllerTest.php b/tests/TestCase/Controller/ProductPhotosControllerTest.php index 8eb2329..1849bed 100644 --- a/tests/TestCase/Controller/ProductPhotosControllerTest.php +++ b/tests/TestCase/Controller/ProductPhotosControllerTest.php @@ -273,17 +273,19 @@ class ProductPhotosControllerTest extends BaseControllerTest $id, ]; $data = [ - // test new data here 'enabled' => 1, 'primary_photo' => 1, 'photo_position' => 999, ]; -// $this->put($url, $data); + $this->put($url, $data); $this->assertResponseCode(302); $this->assertRedirectContains('product-photos'); $after = $this->ProductPhotos->get($id); + $this->assertEquals($data['photo_position'], $after->photo_position); + $this->assertTrue($after->primary_photo); + $this->assertTrue($after->enabled); // assert saved properly below } @@ -308,10 +310,16 @@ class ProductPhotosControllerTest extends BaseControllerTest 'action' => 'edit', $id, ]; - $data = []; + $data = [ + 'enabled' => 1, + 'primary_photo' => 1, + 'photo_position' => 'not-valid', + ]; $this->put($url, $data); $this->assertResponseCode(200); $after = $this->ProductPhotos->get($id); + $this->assertEquals($before->modified, $after->modified); + $this->assertEquals($before->photo_position, $after->photo_position); // assert save failed below }