test fixes for product skus add
CI / testsuite (mysql, 8.2, ) (push) Failing after 5m57s Details
CI / testsuite (mysql, 8.4, ) (push) Successful in 10m23s Details
CI / testsuite (sqlite, 8.2, prefer-lowest) (push) Failing after 6m10s Details
CI / Coding Standard & Static Analysis (push) Failing after 5m40s Details

This commit is contained in:
Brandon Shipley 2025-09-06 01:11:51 -07:00
parent 99f52422c1
commit f3a6384c55
Signed by: bmfs
GPG Key ID: 14E38571D8BB0DE4
8 changed files with 92 additions and 43 deletions

View File

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

View File

@ -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'
));
}
/**

View File

@ -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',
]);

View File

@ -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;
<table class="table">
<thead>
<tr>
<th>Add?</th>
<th>SKU</th>
<th>Barcode</th>
<th>Price</th>
@ -35,7 +35,6 @@ use function BenTools\CartesianProduct\combinations;
foreach ($variantNameMapping as $singleVariantName) : ?>
<th><?= $singleVariantName; ?></th>
<?php endforeach; ?>
<th>Add?</th>
</tr>
</thead>
<tbody>
@ -44,17 +43,22 @@ use function BenTools\CartesianProduct\combinations;
$labelFalse = ['label' => false];
foreach (combinations($toGetCartesianProductsFrom) as $c => $combination) : ?>
<tr>
<td><?= $this->Form->control('skus.' . $cnt . '.sku', $labelFalse); ?></td>
<td><?= $this->Form->control('skus.' . $cnt . '.barcode', $labelFalse); ?></td>
<td><?= $this->Form->control('skus.' . $cnt . '.price', $labelFalse); ?></td>
<td><?= $this->Form->control('skus.' . $cnt . '.cost', $labelFalse); ?></td>
<?php foreach ($variantNameMapping as $singleVariantId => $singleVariantName) : ?>
<td><?= $this->Form->control($cnt . '.add', ['label' => false, 'type' => 'checkbox', 'checked' => true]); ?></td>
<td><?= $this->Form->control($cnt . '.sku', $labelFalse); ?></td>
<td><?= $this->Form->control($cnt . '.barcode', $labelFalse); ?></td>
<td><?= $this->Form->control($cnt . '.price', $labelFalse); ?></td>
<td><?= $this->Form->control($cnt . '.cost', $labelFalse); ?></td>
<?php
$variantCnt = 0;
foreach ($variantNameMapping as $singleVariantId => $singleVariantName) : ?>
<td>
<?= $this->Form->hidden('skus.' . $cnt . '.variants.' . $singleVariantId, ['value' => $combination[$singleVariantId] ?? null]); ?>
<?= $this->Form->hidden($cnt . '.product_skus_variant_values.' . $variantCnt . '.product_category_variant_id', ['value' => $singleVariantId ?? null]); ?>
<?= $this->Form->hidden($cnt . '.product_skus_variant_values.' . $variantCnt . '.product_category_variant_option_id', ['value' => $combination[$singleVariantId] ?? null]); ?>
<?= $optionMapping[$combination[$singleVariantId]]; ?>
</td>
<?php endforeach; ?>
<td><?= $this->Form->control('add', ['label' => false, 'type' => 'checkbox', 'checked' => true]); ?></td>
<?php
$variantCnt++;
endforeach; ?>
</tr>
<?php
$cnt++;

View File

@ -36,6 +36,24 @@ class ProductCategoryVariantOptionsFixture extends TestFixture
'modified' => '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();
}

View File

@ -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)) {

View File

@ -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',
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',
0 => [
'sku' => '',
'barcode' => 'cfc98a9a-29b2-44c8-b587-8156adc05317',
'price' => 1.5,
'cost' => 1.5,
],
];
$this->post($url, $data);
$this->assertResponseCode(200);

View File

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