From 59a38758e91e24117014036ff86f96df144d39ee Mon Sep 17 00:00:00 2001 From: Brandon Shipley Date: Sat, 5 Jul 2025 20:48:40 -0700 Subject: [PATCH] variant options coming along - add test passing --- ...29054627_CreateProductCategoryVariants.php | 16 +++++------ ...35_CreateProductCategoryVariantOptions.php | 3 +- .../ProductCategoryVariantsController.php | 14 +++++++++- .../Table/ProductCategoryVariantsTable.php | 4 ++- .../ProductCategoryVariantOptions/add.php | 20 +++++++++++++ templates/ProductCategoryVariants/view.php | 28 +++++++++++++++++++ .../product_category_variant_option_form.php | 6 ++-- .../ProductCategoryVariantOptionsFixture.php | 8 +++--- .../ProductCategoryVariantsFixture.php | 5 ++-- .../ProductCategoryVariantsControllerTest.php | 26 +++++++++++++++++ ...ProductCategoryVariantOptionsTableTest.php | 4 +-- 11 files changed, 110 insertions(+), 24 deletions(-) create mode 100644 templates/ProductCategoryVariantOptions/add.php diff --git a/config/Migrations/20250629054627_CreateProductCategoryVariants.php b/config/Migrations/20250629054627_CreateProductCategoryVariants.php index 7bf161f..8e35324 100644 --- a/config/Migrations/20250629054627_CreateProductCategoryVariants.php +++ b/config/Migrations/20250629054627_CreateProductCategoryVariants.php @@ -48,14 +48,14 @@ class CreateProductCategoryVariants extends AbstractMigration 'name' => 'VARIANTS_BY_PRODUCT_ID', 'unique' => false, ]); - $table->addIndex([ - 'name', - 'product_category_id', - 'product_id', - ], [ - 'name' => 'VARIANTS_BY_NAME_AND_PRODUCT_CATEGORY_ID_AND_PRODUCT_ID_UNIQUE', - 'unique' => true, - ]); +// $table->addIndex([ +// 'name', +// 'product_category_id', +// 'product_id', +// ], [ +// 'name' => 'VARIANTS_BY_NAME_AND_PRODUCT_CATEGORY_ID_AND_PRODUCT_ID_UNIQUE', +// 'unique' => true, +// ]); $table->create(); } } diff --git a/config/Migrations/20250706011935_CreateProductCategoryVariantOptions.php b/config/Migrations/20250706011935_CreateProductCategoryVariantOptions.php index 3491a85..2628b15 100644 --- a/config/Migrations/20250706011935_CreateProductCategoryVariantOptions.php +++ b/config/Migrations/20250706011935_CreateProductCategoryVariantOptions.php @@ -49,8 +49,7 @@ class CreateProductCategoryVariantOptions extends BaseMigration 'default' => true, 'null' => false, ]); - - $table->addForeignKey('product_category_variant_id', 'product_category_variants'); +// $table->addForeignKey('product_category_variant_id', 'product_category_variants'); // @TODO why cant this be included??? breaks tests on tearDown $table->create(); } diff --git a/src/Controller/ProductCategoryVariantsController.php b/src/Controller/ProductCategoryVariantsController.php index 9420db4..6de1897 100644 --- a/src/Controller/ProductCategoryVariantsController.php +++ b/src/Controller/ProductCategoryVariantsController.php @@ -5,6 +5,7 @@ namespace CakeProducts\Controller; use App\Controller\AppController; use Cake\Core\Configure; +use Cake\Log\Log; use Cake\ORM\Table; use Cake\ORM\TableRegistry; use CheeseCake\Controller\Traits\OverrideTableTrait; @@ -66,12 +67,23 @@ class ProductCategoryVariantsController extends AppController $productCategoryVariant = $productCategoryVariantsTable->newEmptyEntity(); if ($this->request->is('post')) { - $productCategoryVariant = $productCategoryVariantsTable->patchEntity($productCategoryVariant, $this->request->getData()); + $postData = $this->request->getData(); + if ($this->request->getSession()->read('Auth.User.id')) { + $postData['created_by'] = $this->request->getSession()->read('Auth.User.id'); + } + $saveOptions = [ + 'associated' => [ + 'ProductCategoryVariantOptions' + ], + ]; + $productCategoryVariant = $productCategoryVariantsTable->patchEntity($productCategoryVariant, $postData, $saveOptions); if ($productCategoryVariantsTable->save($productCategoryVariant)) { $this->Flash->success(__('The product category variant has been saved.')); return $this->redirect(['action' => 'index']); } + Log::debug('print_r($productCategoryVariant->getErrors(), true) failed to save in product category variants add'); + Log::debug(print_r($productCategoryVariant->getErrors(), true)); $this->Flash->error(__('The product category variant could not be saved. Please, try again.')); } $productCategories = $productCategoryVariantsTable->ProductCategories->find('list', keyField: 'internal_id', valueField: 'name')->all(); diff --git a/src/Model/Table/ProductCategoryVariantsTable.php b/src/Model/Table/ProductCategoryVariantsTable.php index 6cca32b..d478e85 100644 --- a/src/Model/Table/ProductCategoryVariantsTable.php +++ b/src/Model/Table/ProductCategoryVariantsTable.php @@ -57,6 +57,8 @@ class ProductCategoryVariantsTable extends Table $this->hasMany('ProductCategoryVariantOptions', [ 'foreignKey' => 'product_category_variant_id', 'className' => 'CakeProducts.ProductCategoryVariantOptions', + 'dependent' => true, + 'cascadeCallbacks' => true, ]); } @@ -99,7 +101,7 @@ class ProductCategoryVariantsTable extends Table */ public function buildRules(RulesChecker $rules): RulesChecker { - $rules->add($rules->isUnique(['name', 'product_category_id', 'product_id'], ['allowMultipleNulls' => true]), ['errorField' => 'product_category_id']); +// $rules->add($rules->isUnique(['name', 'product_category_id', 'product_id'], ['allowMultipleNulls' => true]), ['errorField' => 'product_category_id']); $rules->add($rules->existsIn(['product_category_id'], 'ProductCategories'), ['errorField' => 'product_category_id']); $rules->add($rules->existsIn(['product_id'], 'Products'), ['errorField' => 'product_id']); diff --git a/templates/ProductCategoryVariantOptions/add.php b/templates/ProductCategoryVariantOptions/add.php new file mode 100644 index 0000000..205412a --- /dev/null +++ b/templates/ProductCategoryVariantOptions/add.php @@ -0,0 +1,20 @@ +setLayout('ajax'); +$prefix = $prefix ?? ''; +if ($this->request->getQuery('prefix') !== null) { + $prefix = 'product_category_variant_options.' . $this->request->getQuery('prefix') . '.'; +} +echo '
'; +echo $this->element('ProductCategoryVariants/product_category_variant_option_form', [ + 'prefix' => $prefix +]); +?> diff --git a/templates/ProductCategoryVariants/view.php b/templates/ProductCategoryVariants/view.php index 533a8ae..004dcee 100644 --- a/templates/ProductCategoryVariants/view.php +++ b/templates/ProductCategoryVariants/view.php @@ -43,6 +43,34 @@ enabled ? __('Yes') : __('No'); ?> + + diff --git a/templates/element/ProductCategoryVariants/product_category_variant_option_form.php b/templates/element/ProductCategoryVariants/product_category_variant_option_form.php index ec79239..e836bd0 100644 --- a/templates/element/ProductCategoryVariants/product_category_variant_option_form.php +++ b/templates/element/ProductCategoryVariants/product_category_variant_option_form.php @@ -8,15 +8,15 @@ $prefix = $prefix ?? ''; \Cake\Log\Log::debug('$prefix'); \Cake\Log\Log::debug($prefix); ?> -
+
- Form->control($prefix . 'attribute_value', [ + Form->control($prefix . 'variant_value', [ 'label' => 'Value', ]); ?>
- Form->control($prefix . 'attribute_label', [ + Form->control($prefix . 'variant_label', [ 'label' => 'Label', ]); ?>
diff --git a/tests/Fixture/ProductCategoryVariantOptionsFixture.php b/tests/Fixture/ProductCategoryVariantOptionsFixture.php index b1d66ee..a2f5b6d 100644 --- a/tests/Fixture/ProductCategoryVariantOptionsFixture.php +++ b/tests/Fixture/ProductCategoryVariantOptionsFixture.php @@ -19,8 +19,8 @@ class ProductCategoryVariantOptionsFixture extends TestFixture { $this->records = [ [ - 'id' => '5a386e9f-6e7a-4ae7-9360-c8e529f78d93', - 'variant_value' => 'Color', + 'id' => '5a386e9f-6e7a-4ae7-9360-c8e529f78d23', + 'variant_value' => 'Blue', 'variant_label' => null, 'product_category_variant_id' => '5a386e9f-6e7a-4ae7-9360-c8e529f78d93', 'created' => '2025-07-04 12:00:00', @@ -28,8 +28,8 @@ class ProductCategoryVariantOptionsFixture extends TestFixture 'enabled' => 1, ], [ - 'id' => '5a386e9f-6e7a-4ae7-9360-c8e529f78d94', - 'variant_value' => 'Size', + 'id' => '5a386e9f-6e7a-4ae7-9360-c8e529f78d24', + 'variant_value' => 'Red', 'variant_label' => null, 'product_category_variant_id' => '5a386e9f-6e7a-4ae7-9360-c8e529f78d93', 'created' => '2025-07-04 12:00:00', diff --git a/tests/Fixture/ProductCategoryVariantsFixture.php b/tests/Fixture/ProductCategoryVariantsFixture.php index 4549766..f7a8d6a 100644 --- a/tests/Fixture/ProductCategoryVariantsFixture.php +++ b/tests/Fixture/ProductCategoryVariantsFixture.php @@ -21,18 +21,17 @@ class ProductCategoryVariantsFixture extends TestFixture [ 'id' => '5a386e9f-6e7a-4ae7-9360-c8e529f78d93', 'name' => 'Color', - 'product_category_id' => '6d223283-361b-4f9f-a7f1-c97aa0ca4c23', + 'product_category_id' => 'db4b4273-eddc-46d4-93c8-45cf7c6e058e', 'product_id' => null, 'enabled' => 1, ], [ 'id' => '5a386e9f-6e7a-4ae7-9360-c8e529f78d94', 'name' => 'Color', - 'product_category_id' => '6d223283-361b-4f9f-a7f1-c97aa0ca4c23', + 'product_category_id' => 'db4b4273-eddc-46d4-93c8-45cf7c6e058e', 'product_id' => 'cfc98a9a-29b2-44c8-b587-8156adc05317', 'enabled' => 1, ], - ]; parent::init(); } diff --git a/tests/TestCase/Controller/ProductCategoryVariantsControllerTest.php b/tests/TestCase/Controller/ProductCategoryVariantsControllerTest.php index 20a0930..a192828 100644 --- a/tests/TestCase/Controller/ProductCategoryVariantsControllerTest.php +++ b/tests/TestCase/Controller/ProductCategoryVariantsControllerTest.php @@ -6,6 +6,7 @@ namespace CakeProducts\Test\TestCase\Controller; use CakeProducts\Controller\ProductCategoryVariantsController; use Cake\TestSuite\IntegrationTestTrait; use Cake\TestSuite\TestCase; +use CakeProducts\Model\Table\ProductCategoryVariantOptionsTable; use CakeProducts\Model\Table\ProductCategoryVariantsTable; use PHPUnit\Exception; @@ -22,6 +23,13 @@ class ProductCategoryVariantsControllerTest extends BaseControllerTest * @var ProductCategoryVariantsTable|Table */ protected $ProductCategoryVariants; + + /** + * Test subject + * + * @var ProductCategoryVariantOptionsTable|Table + */ + protected $ProductCategoryVariantOptions; /** * Fixtures * @@ -42,6 +50,7 @@ class ProductCategoryVariantsControllerTest extends BaseControllerTest { parent::setUp(); $this->ProductCategoryVariants = $this->getTableLocator()->get('ProductCategoryVariants'); + $this->ProductCategoryVariantOptions = $this->getTableLocator()->get('ProductCategoryVariantOptions'); } /** @@ -52,6 +61,7 @@ class ProductCategoryVariantsControllerTest extends BaseControllerTest protected function tearDown(): void { unset($this->ProductCategoryVariants); + unset($this->ProductCategoryVariantOptions); parent::tearDown(); } @@ -142,6 +152,7 @@ class ProductCategoryVariantsControllerTest extends BaseControllerTest public function testAddPostLoggedInSuccess(): void { $cntBefore = $this->ProductCategoryVariants->find()->count(); + $cntBeforeOptions = $this->ProductCategoryVariantOptions->find()->count(); $this->loginUserByRole('admin'); $url = [ @@ -152,14 +163,29 @@ class ProductCategoryVariantsControllerTest extends BaseControllerTest $data = [ 'name' => 'Size', 'product_category_id' => 'db4b4273-eddc-46d4-93c8-45cf7c6e058e', + 'product_id' => '', 'enabled' => true, + 'product_category_variant_options' => [ + [ + 'variant_value' => 'XL', + 'variant_label' => 'XL', + 'enabled' => true, + ], + [ + 'variant_value' => 'XXL', + 'variant_label' => 'XXL', + 'enabled' => true, + ], + ] ]; $this->post($url, $data); $this->assertResponseCode(302); $this->assertRedirectContains('product-category-variants'); $cntAfter = $this->ProductCategoryVariants->find()->count(); + $cntAfterOptions = $this->ProductCategoryVariantOptions->find()->count(); $this->assertEquals($cntBefore + 1, $cntAfter); + $this->assertEquals($cntBeforeOptions + 2, $cntAfterOptions); } /** diff --git a/tests/TestCase/Model/Table/ProductCategoryVariantOptionsTableTest.php b/tests/TestCase/Model/Table/ProductCategoryVariantOptionsTableTest.php index 9c8994e..7b7a103 100644 --- a/tests/TestCase/Model/Table/ProductCategoryVariantOptionsTableTest.php +++ b/tests/TestCase/Model/Table/ProductCategoryVariantOptionsTableTest.php @@ -24,8 +24,8 @@ class ProductCategoryVariantOptionsTableTest extends TestCase * @var array */ protected array $fixtures = [ -// 'ProductCategoryVariantOptions', -// 'ProductCategoryVariants', + 'plugin.CakeProducts.ProductCategoryVariants', + 'plugin.CakeProducts.ProductCategoryVariantOptions', ]; /**