From f3a6384c55347701d94e0ab0f153aa9717b8395b Mon Sep 17 00:00:00 2001 From: Brandon Shipley Date: Sat, 6 Sep 2025 01:11:51 -0700 Subject: [PATCH] test fixes for product skus add --- src/Controller/AppController.php | 1 - src/Controller/ProductSkusController.php | 56 ++++++++++++------- src/Model/Table/ProductSkusTable.php | 2 + templates/ProductSkus/add.php | 26 +++++---- .../ProductCategoryVariantOptionsFixture.php | 18 ++++++ .../Controller/BaseControllerTest.php | 2 +- .../Controller/ProductSkusControllerTest.php | 28 ++++++---- tests/bootstrap.php | 2 +- 8 files changed, 92 insertions(+), 43 deletions(-) diff --git a/src/Controller/AppController.php b/src/Controller/AppController.php index 4a64f8c..c63822f 100644 --- a/src/Controller/AppController.php +++ b/src/Controller/AppController.php @@ -4,7 +4,6 @@ declare(strict_types=1); namespace CakeProducts\Controller; use App\Controller\AppController as BaseController; -use Cake\Log\Log; class AppController extends BaseController { diff --git a/src/Controller/ProductSkusController.php b/src/Controller/ProductSkusController.php index 4d6df0d..f57a611 100644 --- a/src/Controller/ProductSkusController.php +++ b/src/Controller/ProductSkusController.php @@ -5,7 +5,6 @@ namespace CakeProducts\Controller; use Cake\Log\Log; use Cake\Utility\Hash; -use CakeProducts\Controller\AppController; use CheeseCake\Controller\Traits\OverrideTableTrait; use function BenTools\CartesianProduct\combinations; @@ -80,24 +79,6 @@ class ProductSkusController extends AppController for ($i = 0; $i < $numSkusToAdd; $i++) { $productSkus[$i] = $this->getTable()->newEmptyEntity(); } - if ($this->request->is('post')) { - $postData = $this->request->getData('skus', []); - $saveOptions = [ - 'associated' => [], - ]; - $postData = Hash::insert($postData, '{n}.product_id', $productId); - // Log::debug(print_r('$postData', true)); -// Log::debug(print_r($postData, true)); -// Log::debug(print_r('$saveOptions', true)); -// Log::debug(print_r($saveOptions, true)); - $productSkus = $table->patchEntities($productSkus, $postData, $saveOptions); - if ($table->saveManyOrFail($productSkus)) { - $this->Flash->success(__('The product SKU(s) have been saved.')); - - return $this->redirect(['action' => 'index']); - } - $this->Flash->error(__('The product SKU(s) could not be saved. Please, try again.')); - } $this->set(compact( 'productSkus', 'productCategoryVariants', @@ -106,6 +87,43 @@ class ProductSkusController extends AppController 'variantNameMapping', 'numSkusToAdd' )); + + if ($this->request->is('post')) { + $postedSkus = $this->request->getData(); + $saveOptions = [ + 'associated' => [], + ]; + + $postedSkus = Hash::insert($postedSkus, '{n}.product_id', $productId); + + foreach ($postedSkus as $postedSkuCnt => $postedSku) { + if ($postedSku['add'] ?? false) { + continue; + } + unset($postedSkus[$postedSkuCnt]); + if ($productSkus[$postedSkuCnt] ?? false) { + unset($productSkus[$postedSkuCnt]); + } + } + + if (!$productSkus || !$postedSkus) { + $this->Flash->error('Nothing to save! Add at least one SKU next time.'); + + return; + } + + $productSkus = $table->patchEntities($productSkus, $postedSkus, $saveOptions); + if ($table->saveManyOrFail($productSkus, $saveOptions)) { + $this->Flash->success(__(count($productSkus) . ' New SKUs have been saved.')); + + return $this->redirect(['action' => 'index']); + } + + $this->Flash->error(__('The product SKU(s) could not be saved. Please, try again.')); + } + $this->set(compact( + 'productSkus' + )); } /** diff --git a/src/Model/Table/ProductSkusTable.php b/src/Model/Table/ProductSkusTable.php index 7e22c32..cbb349b 100644 --- a/src/Model/Table/ProductSkusTable.php +++ b/src/Model/Table/ProductSkusTable.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace CakeProducts\Model\Table; use Cake\Core\Configure; +use Cake\ORM\Query\SelectQuery; use CakeProducts\Model\Table\ProductsTable; use Cake\Datasource\EntityInterface; use Cake\Datasource\ResultSetInterface; @@ -60,6 +61,7 @@ class ProductSkusTable extends Table $this->addBehavior('Timestamp'); $this->belongsTo('Products', [ + 'className' => 'CakeProducts.Products', 'foreignKey' => 'product_id', 'joinType' => 'INNER', ]); diff --git a/templates/ProductSkus/add.php b/templates/ProductSkus/add.php index 24c06c7..9a30aed 100644 --- a/templates/ProductSkus/add.php +++ b/templates/ProductSkus/add.php @@ -4,8 +4,7 @@ use function BenTools\CartesianProduct\combinations; /** * @var \App\View\AppView $this - * @var \App\Model\Entity\ProductSku $productSku - * @var \Cake\Collection\CollectionInterface|string[] $products + * @var \App\Model\Entity\ProductSku[] $productSkus */ @@ -26,6 +25,7 @@ use function BenTools\CartesianProduct\combinations; + @@ -35,7 +35,6 @@ use function BenTools\CartesianProduct\combinations; foreach ($variantNameMapping as $singleVariantName) : ?> - @@ -44,17 +43,22 @@ use function BenTools\CartesianProduct\combinations; $labelFalse = ['label' => false]; foreach (combinations($toGetCartesianProductsFrom) as $c => $combination) : ?> - - - - - $singleVariantName) : ?> + + + + + + $singleVariantName) : ?> - - + '2025-07-04 12:00:00', 'enabled' => 1, ], + [ + 'id' => '5a386e9f-6e7a-4ae7-9360-c8e529f78d25', + 'variant_value' => 'Blue', + 'variant_label' => null, + 'product_category_variant_id' => '5a386e9f-6e7a-4ae7-9360-c8e529f78d94', + 'created' => '2025-07-04 12:00:00', + 'modified' => '2025-07-04 12:00:00', + 'enabled' => 1, + ], + [ + 'id' => '5a386e9f-6e7a-4ae7-9360-c8e529f78d26', + 'variant_value' => 'Red', + 'variant_label' => null, + 'product_category_variant_id' => '5a386e9f-6e7a-4ae7-9360-c8e529f78d94', + 'created' => '2025-07-04 12:00:00', + 'modified' => '2025-07-04 12:00:00', + 'enabled' => 1, + ], ]; parent::init(); } diff --git a/tests/TestCase/Controller/BaseControllerTest.php b/tests/TestCase/Controller/BaseControllerTest.php index 9cc2b21..0426288 100644 --- a/tests/TestCase/Controller/BaseControllerTest.php +++ b/tests/TestCase/Controller/BaseControllerTest.php @@ -36,7 +36,7 @@ class BaseControllerTest extends TestCase { parent::setUp(); $toCopy = PLUGIN_ROOT . DS . 'tests' . DS . 'test_app' . DS . 'webroot' . DS . 'images' . DS . '2c386086-f4c5-4093-bea5-ee9c29479f58.png'; - $productsFolder = PLUGIN_ROOT . DS . 'tests' . DS . 'test_app' . DS . 'webroot' . DS . + $productsFolder = PLUGIN_ROOT . DS . 'tests' . DS . 'test_app' . DS . 'webroot' . DS . 'uploads' . DS . 'images' . DS . 'products' . DS . 'cfc98a9a-29b2-44c8-b587-8156adc05317'; $newName = $productsFolder . DS . '2c386086-f4c5-4093-bea5-ee9c29479f58.png'; if (file_exists($toCopy)) { diff --git a/tests/TestCase/Controller/ProductSkusControllerTest.php b/tests/TestCase/Controller/ProductSkusControllerTest.php index 3063b71..9c15157 100644 --- a/tests/TestCase/Controller/ProductSkusControllerTest.php +++ b/tests/TestCase/Controller/ProductSkusControllerTest.php @@ -46,6 +46,8 @@ class ProductSkusControllerTest extends BaseControllerTest parent::setUp(); // $this->enableCsrfToken(); // $this->enableSecurityToken(); + $this->disableErrorHandlerMiddleware(); + $config = $this->getTableLocator()->exists('ProductSkus') ? [] : ['className' => ProductSkusTable::class]; $this->ProductSkus = $this->getTableLocator()->get('ProductSkus', $config); } @@ -127,6 +129,7 @@ class ProductSkusControllerTest extends BaseControllerTest 'plugin' => 'CakeProducts', 'controller' => 'ProductSkus', 'action' => 'add', + 'cfc98a9a-29b2-44c8-b587-8156adc05317' ]; $this->get($url); $this->assertResponseCode(200); @@ -154,13 +157,16 @@ class ProductSkusControllerTest extends BaseControllerTest 'plugin' => 'CakeProducts', 'controller' => 'ProductSkus', 'action' => 'add', + 'cfc98a9a-29b2-44c8-b587-8156adc05317', ]; $data = [ - 'product_id' => 'cfc98a9a-29b2-44c8-b587-8156adc05317', - 'sku' => 'cfc98a9a-29b2-44c8-b587-8156adc05317', - 'barcode' => 'cfc98a9a-29b2-44c8-b587-8156adc05317', - 'price' => 1.5, - 'cost' => 1.5, + 0 => [ + 'add' => '1', + 'sku' => 'cfc98a9a-29b2-44c8-b587-8156adc05317', + 'barcode' => 'cfc98a9a-29b2-44c8-b587-8156adc05317', + 'price' => 1.5, + 'cost' => 1.5, + ], ]; $this->post($url, $data); $this->assertResponseCode(302); @@ -189,13 +195,15 @@ class ProductSkusControllerTest extends BaseControllerTest 'plugin' => 'CakeProducts', 'controller' => 'ProductSkus', 'action' => 'add', + 'cfc98a9a-29b2-44c8-b587-8156adc05317', ]; $data = [ - 'product_id' => '999999999', //does not exist - 'sku' => 'cfc98a9a-29b2-44c8-b587-8156adc05317', - 'barcode' => 'cfc98a9a-29b2-44c8-b587-8156adc05317', - 'price' => 1.5, - 'cost' => 1.5, + 0 => [ + 'sku' => '', + 'barcode' => 'cfc98a9a-29b2-44c8-b587-8156adc05317', + 'price' => 1.5, + 'cost' => 1.5, + ], ]; $this->post($url, $data); $this->assertResponseCode(200); diff --git a/tests/bootstrap.php b/tests/bootstrap.php index ec76d1f..209c47c 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -59,7 +59,7 @@ Configure::write('App', [ Configure::write('debug', true); Configure::write('CakeProducts', [ 'photos' => [ - 'directory' => PLUGIN_ROOT . DS . 'tests' . DS . 'test_app' . DS . 'webroot' . DS . 'images' . DS . 'products' . DS, + 'directory' => PLUGIN_ROOT . DS . 'tests' . DS . 'test_app' . DS . 'webroot' . DS . 'uploads' . DS . 'images' . DS . 'products' . DS, ], /** * internal CakeProducts settings - used in the source of truth/internal only system.
Add? SKU Barcode Price Add?
Form->control('skus.' . $cnt . '.sku', $labelFalse); ?>Form->control('skus.' . $cnt . '.barcode', $labelFalse); ?>Form->control('skus.' . $cnt . '.price', $labelFalse); ?>Form->control('skus.' . $cnt . '.cost', $labelFalse); ?>Form->control($cnt . '.add', ['label' => false, 'type' => 'checkbox', 'checked' => true]); ?>Form->control($cnt . '.sku', $labelFalse); ?>Form->control($cnt . '.barcode', $labelFalse); ?>Form->control($cnt . '.price', $labelFalse); ?>Form->control($cnt . '.cost', $labelFalse); ?> - Form->hidden('skus.' . $cnt . '.variants.' . $singleVariantId, ['value' => $combination[$singleVariantId] ?? null]); ?> + Form->hidden($cnt . '.product_skus_variant_values.' . $variantCnt . '.product_category_variant_id', ['value' => $singleVariantId ?? null]); ?> + Form->hidden($cnt . '.product_skus_variant_values.' . $variantCnt . '.product_category_variant_option_id', ['value' => $combination[$singleVariantId] ?? null]); ?> Form->control('add', ['label' => false, 'type' => 'checkbox', 'checked' => true]); ?>