variant options coming along - add test passing
CI / testsuite (mysql, 8.1, ) (push) Failing after 0s Details
CI / testsuite (mysql, 8.4, ) (push) Failing after 0s Details
CI / testsuite (pgsql, 8.1, ) (push) Failing after 0s Details
CI / testsuite (pgsql, 8.4, ) (push) Failing after 0s Details
CI / testsuite (sqlite, 8.1, ) (push) Failing after 0s Details
CI / testsuite (sqlite, 8.1, prefer-lowest) (push) Failing after 0s Details
CI / testsuite (sqlite, 8.4, ) (push) Failing after 0s Details
CI / Coding Standard & Static Analysis (push) Failing after 0s Details

This commit is contained in:
Brandon Shipley 2025-07-05 20:48:40 -07:00
parent 5adc791c20
commit 59a38758e9
Signed by: bmfs
GPG Key ID: 14E38571D8BB0DE4
11 changed files with 110 additions and 24 deletions

View File

@ -48,14 +48,14 @@ class CreateProductCategoryVariants extends AbstractMigration
'name' => 'VARIANTS_BY_PRODUCT_ID', 'name' => 'VARIANTS_BY_PRODUCT_ID',
'unique' => false, 'unique' => false,
]); ]);
$table->addIndex([ // $table->addIndex([
'name', // 'name',
'product_category_id', // 'product_category_id',
'product_id', // 'product_id',
], [ // ], [
'name' => 'VARIANTS_BY_NAME_AND_PRODUCT_CATEGORY_ID_AND_PRODUCT_ID_UNIQUE', // 'name' => 'VARIANTS_BY_NAME_AND_PRODUCT_CATEGORY_ID_AND_PRODUCT_ID_UNIQUE',
'unique' => true, // 'unique' => true,
]); // ]);
$table->create(); $table->create();
} }
} }

View File

@ -49,8 +49,7 @@ class CreateProductCategoryVariantOptions extends BaseMigration
'default' => true, 'default' => true,
'null' => false, 'null' => false,
]); ]);
// $table->addForeignKey('product_category_variant_id', 'product_category_variants'); // @TODO why cant this be included??? breaks tests on tearDown
$table->addForeignKey('product_category_variant_id', 'product_category_variants');
$table->create(); $table->create();
} }

View File

@ -5,6 +5,7 @@ namespace CakeProducts\Controller;
use App\Controller\AppController; use App\Controller\AppController;
use Cake\Core\Configure; use Cake\Core\Configure;
use Cake\Log\Log;
use Cake\ORM\Table; use Cake\ORM\Table;
use Cake\ORM\TableRegistry; use Cake\ORM\TableRegistry;
use CheeseCake\Controller\Traits\OverrideTableTrait; use CheeseCake\Controller\Traits\OverrideTableTrait;
@ -66,12 +67,23 @@ class ProductCategoryVariantsController extends AppController
$productCategoryVariant = $productCategoryVariantsTable->newEmptyEntity(); $productCategoryVariant = $productCategoryVariantsTable->newEmptyEntity();
if ($this->request->is('post')) { 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)) { if ($productCategoryVariantsTable->save($productCategoryVariant)) {
$this->Flash->success(__('The product category variant has been saved.')); $this->Flash->success(__('The product category variant has been saved.'));
return $this->redirect(['action' => 'index']); 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.')); $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(); $productCategories = $productCategoryVariantsTable->ProductCategories->find('list', keyField: 'internal_id', valueField: 'name')->all();

View File

@ -57,6 +57,8 @@ class ProductCategoryVariantsTable extends Table
$this->hasMany('ProductCategoryVariantOptions', [ $this->hasMany('ProductCategoryVariantOptions', [
'foreignKey' => 'product_category_variant_id', 'foreignKey' => 'product_category_variant_id',
'className' => 'CakeProducts.ProductCategoryVariantOptions', 'className' => 'CakeProducts.ProductCategoryVariantOptions',
'dependent' => true,
'cascadeCallbacks' => true,
]); ]);
} }
@ -99,7 +101,7 @@ class ProductCategoryVariantsTable extends Table
*/ */
public function buildRules(RulesChecker $rules): RulesChecker 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_category_id'], 'ProductCategories'), ['errorField' => 'product_category_id']);
$rules->add($rules->existsIn(['product_id'], 'Products'), ['errorField' => 'product_id']); $rules->add($rules->existsIn(['product_id'], 'Products'), ['errorField' => 'product_id']);

View File

@ -0,0 +1,20 @@
<?php
use App\View\AppView;
use Cake\Datasource\EntityInterface;
/**
* @var AppView $this
* @var EntityInterface $productCategoryAttributeOption
*/
$this->setLayout('ajax');
$prefix = $prefix ?? '';
if ($this->request->getQuery('prefix') !== null) {
$prefix = 'product_category_variant_options.' . $this->request->getQuery('prefix') . '.';
}
echo '<hr class="my-2">';
echo $this->element('ProductCategoryVariants/product_category_variant_option_form', [
'prefix' => $prefix
]);
?>

View File

@ -43,6 +43,34 @@
<td><?= $productCategoryVariant->enabled ? __('Yes') : __('No'); ?></td> <td><?= $productCategoryVariant->enabled ? __('Yes') : __('No'); ?></td>
</tr> </tr>
</table> </table>
<div class="related">
<h4><?= __('Related Product Category Variant Options') ?></h4>
<?php if (!empty($productCategoryVariant->product_category_variant_options)) : ?>
<div class="table-responsive">
<table>
<tr>
<th><?= __('Variant Value') ?></th>
<th><?= __('Variant Label') ?></th>
<th><?= __('Enabled') ?></th>
<th class="actions"><?= __('Actions') ?></th>
</tr>
<?php foreach ($productCategoryVariant->product_category_variant_options as $productCategoryVariantOption) : ?>
<tr>
<td><?= h($productCategoryVariantOption->variant_value) ?></td>
<td><?= h($productCategoryVariantOption->variant_label) ?></td>
<td><?= h($productCategoryVariantOption->enabled) ?></td>
<td class="actions">
<?= $this->Html->link(__('View'), ['controller' => 'ProductCategoryVariantOptions', 'action' => 'view', $productCategoryVariantOption->id]) ?>
<?= $this->Html->link(__('Edit'), ['controller' => 'ProductCategoryVariantOptions', 'action' => 'edit', $productCategoryVariantOption->id]) ?>
<?= $this->Form->postLink(__('Delete'), ['controller' => 'ProductCategoryVariantOptions', 'action' => 'delete', $productCategoryVariantOption->id], ['confirm' => __('Are you sure you want to delete # {0}?', $productCategoryVariantOption->id)]) ?>
</td>
</tr>
<?php endforeach; ?>
</table>
</div>
<?php endif; ?>
</div>
</div> </div>
</div> </div>
</div> </div>

View File

@ -8,15 +8,15 @@ $prefix = $prefix ?? '';
\Cake\Log\Log::debug('$prefix'); \Cake\Log\Log::debug('$prefix');
\Cake\Log\Log::debug($prefix); \Cake\Log\Log::debug($prefix);
?> ?>
<div class="container product-category-attribute-options-container" data-test="1" data-prefix="<?= $prefix; ?>"> <div class="container product-category-variant-options-container" data-test="1" data-prefix="<?= $prefix; ?>">
<div class="row"> <div class="row">
<div class="col"> <div class="col">
<?= $this->Form->control($prefix . 'attribute_value', [ <?= $this->Form->control($prefix . 'variant_value', [
'label' => 'Value', 'label' => 'Value',
]); ?> ]); ?>
</div> </div>
<div class="col"> <div class="col">
<?= $this->Form->control($prefix . 'attribute_label', [ <?= $this->Form->control($prefix . 'variant_label', [
'label' => 'Label', 'label' => 'Label',
]); ?> ]); ?>
</div> </div>

View File

@ -19,8 +19,8 @@ class ProductCategoryVariantOptionsFixture extends TestFixture
{ {
$this->records = [ $this->records = [
[ [
'id' => '5a386e9f-6e7a-4ae7-9360-c8e529f78d93', 'id' => '5a386e9f-6e7a-4ae7-9360-c8e529f78d23',
'variant_value' => 'Color', 'variant_value' => 'Blue',
'variant_label' => null, 'variant_label' => null,
'product_category_variant_id' => '5a386e9f-6e7a-4ae7-9360-c8e529f78d93', 'product_category_variant_id' => '5a386e9f-6e7a-4ae7-9360-c8e529f78d93',
'created' => '2025-07-04 12:00:00', 'created' => '2025-07-04 12:00:00',
@ -28,8 +28,8 @@ class ProductCategoryVariantOptionsFixture extends TestFixture
'enabled' => 1, 'enabled' => 1,
], ],
[ [
'id' => '5a386e9f-6e7a-4ae7-9360-c8e529f78d94', 'id' => '5a386e9f-6e7a-4ae7-9360-c8e529f78d24',
'variant_value' => 'Size', 'variant_value' => 'Red',
'variant_label' => null, 'variant_label' => null,
'product_category_variant_id' => '5a386e9f-6e7a-4ae7-9360-c8e529f78d93', 'product_category_variant_id' => '5a386e9f-6e7a-4ae7-9360-c8e529f78d93',
'created' => '2025-07-04 12:00:00', 'created' => '2025-07-04 12:00:00',

View File

@ -21,18 +21,17 @@ class ProductCategoryVariantsFixture extends TestFixture
[ [
'id' => '5a386e9f-6e7a-4ae7-9360-c8e529f78d93', 'id' => '5a386e9f-6e7a-4ae7-9360-c8e529f78d93',
'name' => 'Color', 'name' => 'Color',
'product_category_id' => '6d223283-361b-4f9f-a7f1-c97aa0ca4c23', 'product_category_id' => 'db4b4273-eddc-46d4-93c8-45cf7c6e058e',
'product_id' => null, 'product_id' => null,
'enabled' => 1, 'enabled' => 1,
], ],
[ [
'id' => '5a386e9f-6e7a-4ae7-9360-c8e529f78d94', 'id' => '5a386e9f-6e7a-4ae7-9360-c8e529f78d94',
'name' => 'Color', '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', 'product_id' => 'cfc98a9a-29b2-44c8-b587-8156adc05317',
'enabled' => 1, 'enabled' => 1,
], ],
]; ];
parent::init(); parent::init();
} }

View File

@ -6,6 +6,7 @@ namespace CakeProducts\Test\TestCase\Controller;
use CakeProducts\Controller\ProductCategoryVariantsController; use CakeProducts\Controller\ProductCategoryVariantsController;
use Cake\TestSuite\IntegrationTestTrait; use Cake\TestSuite\IntegrationTestTrait;
use Cake\TestSuite\TestCase; use Cake\TestSuite\TestCase;
use CakeProducts\Model\Table\ProductCategoryVariantOptionsTable;
use CakeProducts\Model\Table\ProductCategoryVariantsTable; use CakeProducts\Model\Table\ProductCategoryVariantsTable;
use PHPUnit\Exception; use PHPUnit\Exception;
@ -22,6 +23,13 @@ class ProductCategoryVariantsControllerTest extends BaseControllerTest
* @var ProductCategoryVariantsTable|Table * @var ProductCategoryVariantsTable|Table
*/ */
protected $ProductCategoryVariants; protected $ProductCategoryVariants;
/**
* Test subject
*
* @var ProductCategoryVariantOptionsTable|Table
*/
protected $ProductCategoryVariantOptions;
/** /**
* Fixtures * Fixtures
* *
@ -42,6 +50,7 @@ class ProductCategoryVariantsControllerTest extends BaseControllerTest
{ {
parent::setUp(); parent::setUp();
$this->ProductCategoryVariants = $this->getTableLocator()->get('ProductCategoryVariants'); $this->ProductCategoryVariants = $this->getTableLocator()->get('ProductCategoryVariants');
$this->ProductCategoryVariantOptions = $this->getTableLocator()->get('ProductCategoryVariantOptions');
} }
/** /**
@ -52,6 +61,7 @@ class ProductCategoryVariantsControllerTest extends BaseControllerTest
protected function tearDown(): void protected function tearDown(): void
{ {
unset($this->ProductCategoryVariants); unset($this->ProductCategoryVariants);
unset($this->ProductCategoryVariantOptions);
parent::tearDown(); parent::tearDown();
} }
@ -142,6 +152,7 @@ class ProductCategoryVariantsControllerTest extends BaseControllerTest
public function testAddPostLoggedInSuccess(): void public function testAddPostLoggedInSuccess(): void
{ {
$cntBefore = $this->ProductCategoryVariants->find()->count(); $cntBefore = $this->ProductCategoryVariants->find()->count();
$cntBeforeOptions = $this->ProductCategoryVariantOptions->find()->count();
$this->loginUserByRole('admin'); $this->loginUserByRole('admin');
$url = [ $url = [
@ -152,14 +163,29 @@ class ProductCategoryVariantsControllerTest extends BaseControllerTest
$data = [ $data = [
'name' => 'Size', 'name' => 'Size',
'product_category_id' => 'db4b4273-eddc-46d4-93c8-45cf7c6e058e', 'product_category_id' => 'db4b4273-eddc-46d4-93c8-45cf7c6e058e',
'product_id' => '',
'enabled' => true, '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->post($url, $data);
$this->assertResponseCode(302); $this->assertResponseCode(302);
$this->assertRedirectContains('product-category-variants'); $this->assertRedirectContains('product-category-variants');
$cntAfter = $this->ProductCategoryVariants->find()->count(); $cntAfter = $this->ProductCategoryVariants->find()->count();
$cntAfterOptions = $this->ProductCategoryVariantOptions->find()->count();
$this->assertEquals($cntBefore + 1, $cntAfter); $this->assertEquals($cntBefore + 1, $cntAfter);
$this->assertEquals($cntBeforeOptions + 2, $cntAfterOptions);
} }
/** /**

View File

@ -24,8 +24,8 @@ class ProductCategoryVariantOptionsTableTest extends TestCase
* @var array<string> * @var array<string>
*/ */
protected array $fixtures = [ protected array $fixtures = [
// 'ProductCategoryVariantOptions', 'plugin.CakeProducts.ProductCategoryVariants',
// 'ProductCategoryVariants', 'plugin.CakeProducts.ProductCategoryVariantOptions',
]; ];
/** /**