product photos controller test fixes, test out
CI / testsuite (mysql, 8.4, ) (push) Has been cancelled Details
CI / testsuite (pgsql, 8.1, ) (push) Has been cancelled Details
CI / testsuite (pgsql, 8.4, ) (push) Has been cancelled Details
CI / testsuite (sqlite, 8.1, ) (push) Has been cancelled Details
CI / testsuite (sqlite, 8.1, prefer-lowest) (push) Has been cancelled Details
CI / testsuite (sqlite, 8.4, ) (push) Has been cancelled Details
CI / Coding Standard & Static Analysis (push) Has been cancelled Details
CI / testsuite (mysql, 8.1, ) (push) Has been cancelled Details

This commit is contained in:
Brandon Shipley 2025-08-11 23:35:46 -07:00
parent 3a47f09e8c
commit 0181c48907
Signed by: bmfs
GPG Key ID: 14E38571D8BB0DE4
3 changed files with 50 additions and 32 deletions

View File

@ -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();

View File

@ -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<ProductPhoto> 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<ProductPhoto> patchEntities(iterable $entities, array $data, array $options = [])
* @method ProductPhoto|false save(EntityInterface $entity, array $options = [])
* @method ProductPhoto saveOrFail(EntityInterface $entity, array $options = [])
* @method iterable<ProductPhoto>|ResultSetInterface<ProductPhoto>|false saveMany(iterable $entities, array $options = [])
* @method iterable<ProductPhoto>|ResultSetInterface<ProductPhoto> saveManyOrFail(iterable $entities, array $options = [])
* @method iterable<ProductPhoto>|ResultSetInterface<ProductPhoto>|false deleteMany(iterable $entities, array $options = [])
* @method iterable<ProductPhoto>|ResultSetInterface<ProductPhoto> 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;
}

View File

@ -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
}