test fixes for product skus add
This commit is contained in:
parent
99f52422c1
commit
f3a6384c55
|
@ -4,7 +4,6 @@ declare(strict_types=1);
|
||||||
namespace CakeProducts\Controller;
|
namespace CakeProducts\Controller;
|
||||||
|
|
||||||
use App\Controller\AppController as BaseController;
|
use App\Controller\AppController as BaseController;
|
||||||
use Cake\Log\Log;
|
|
||||||
|
|
||||||
class AppController extends BaseController
|
class AppController extends BaseController
|
||||||
{
|
{
|
||||||
|
|
|
@ -5,7 +5,6 @@ namespace CakeProducts\Controller;
|
||||||
|
|
||||||
use Cake\Log\Log;
|
use Cake\Log\Log;
|
||||||
use Cake\Utility\Hash;
|
use Cake\Utility\Hash;
|
||||||
use CakeProducts\Controller\AppController;
|
|
||||||
use CheeseCake\Controller\Traits\OverrideTableTrait;
|
use CheeseCake\Controller\Traits\OverrideTableTrait;
|
||||||
use function BenTools\CartesianProduct\combinations;
|
use function BenTools\CartesianProduct\combinations;
|
||||||
|
|
||||||
|
@ -80,24 +79,6 @@ class ProductSkusController extends AppController
|
||||||
for ($i = 0; $i < $numSkusToAdd; $i++) {
|
for ($i = 0; $i < $numSkusToAdd; $i++) {
|
||||||
$productSkus[$i] = $this->getTable()->newEmptyEntity();
|
$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(
|
$this->set(compact(
|
||||||
'productSkus',
|
'productSkus',
|
||||||
'productCategoryVariants',
|
'productCategoryVariants',
|
||||||
|
@ -106,6 +87,43 @@ class ProductSkusController extends AppController
|
||||||
'variantNameMapping',
|
'variantNameMapping',
|
||||||
'numSkusToAdd'
|
'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'
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||||
namespace CakeProducts\Model\Table;
|
namespace CakeProducts\Model\Table;
|
||||||
|
|
||||||
use Cake\Core\Configure;
|
use Cake\Core\Configure;
|
||||||
|
use Cake\ORM\Query\SelectQuery;
|
||||||
use CakeProducts\Model\Table\ProductsTable;
|
use CakeProducts\Model\Table\ProductsTable;
|
||||||
use Cake\Datasource\EntityInterface;
|
use Cake\Datasource\EntityInterface;
|
||||||
use Cake\Datasource\ResultSetInterface;
|
use Cake\Datasource\ResultSetInterface;
|
||||||
|
@ -60,6 +61,7 @@ class ProductSkusTable extends Table
|
||||||
$this->addBehavior('Timestamp');
|
$this->addBehavior('Timestamp');
|
||||||
|
|
||||||
$this->belongsTo('Products', [
|
$this->belongsTo('Products', [
|
||||||
|
'className' => 'CakeProducts.Products',
|
||||||
'foreignKey' => 'product_id',
|
'foreignKey' => 'product_id',
|
||||||
'joinType' => 'INNER',
|
'joinType' => 'INNER',
|
||||||
]);
|
]);
|
||||||
|
|
|
@ -4,8 +4,7 @@ use function BenTools\CartesianProduct\combinations;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var \App\View\AppView $this
|
* @var \App\View\AppView $this
|
||||||
* @var \App\Model\Entity\ProductSku $productSku
|
* @var \App\Model\Entity\ProductSku[] $productSkus
|
||||||
* @var \Cake\Collection\CollectionInterface|string[] $products
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
@ -26,6 +25,7 @@ use function BenTools\CartesianProduct\combinations;
|
||||||
<table class="table">
|
<table class="table">
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
|
<th>Add?</th>
|
||||||
<th>SKU</th>
|
<th>SKU</th>
|
||||||
<th>Barcode</th>
|
<th>Barcode</th>
|
||||||
<th>Price</th>
|
<th>Price</th>
|
||||||
|
@ -35,7 +35,6 @@ use function BenTools\CartesianProduct\combinations;
|
||||||
foreach ($variantNameMapping as $singleVariantName) : ?>
|
foreach ($variantNameMapping as $singleVariantName) : ?>
|
||||||
<th><?= $singleVariantName; ?></th>
|
<th><?= $singleVariantName; ?></th>
|
||||||
<?php endforeach; ?>
|
<?php endforeach; ?>
|
||||||
<th>Add?</th>
|
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
|
@ -44,17 +43,22 @@ use function BenTools\CartesianProduct\combinations;
|
||||||
$labelFalse = ['label' => false];
|
$labelFalse = ['label' => false];
|
||||||
foreach (combinations($toGetCartesianProductsFrom) as $c => $combination) : ?>
|
foreach (combinations($toGetCartesianProductsFrom) as $c => $combination) : ?>
|
||||||
<tr>
|
<tr>
|
||||||
<td><?= $this->Form->control('skus.' . $cnt . '.sku', $labelFalse); ?></td>
|
<td><?= $this->Form->control($cnt . '.add', ['label' => false, 'type' => 'checkbox', 'checked' => true]); ?></td>
|
||||||
<td><?= $this->Form->control('skus.' . $cnt . '.barcode', $labelFalse); ?></td>
|
<td><?= $this->Form->control($cnt . '.sku', $labelFalse); ?></td>
|
||||||
<td><?= $this->Form->control('skus.' . $cnt . '.price', $labelFalse); ?></td>
|
<td><?= $this->Form->control($cnt . '.barcode', $labelFalse); ?></td>
|
||||||
<td><?= $this->Form->control('skus.' . $cnt . '.cost', $labelFalse); ?></td>
|
<td><?= $this->Form->control($cnt . '.price', $labelFalse); ?></td>
|
||||||
<?php foreach ($variantNameMapping as $singleVariantId => $singleVariantName) : ?>
|
<td><?= $this->Form->control($cnt . '.cost', $labelFalse); ?></td>
|
||||||
|
<?php
|
||||||
|
$variantCnt = 0;
|
||||||
|
foreach ($variantNameMapping as $singleVariantId => $singleVariantName) : ?>
|
||||||
<td>
|
<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]]; ?>
|
<?= $optionMapping[$combination[$singleVariantId]]; ?>
|
||||||
</td>
|
</td>
|
||||||
<?php endforeach; ?>
|
<?php
|
||||||
<td><?= $this->Form->control('add', ['label' => false, 'type' => 'checkbox', 'checked' => true]); ?></td>
|
$variantCnt++;
|
||||||
|
endforeach; ?>
|
||||||
</tr>
|
</tr>
|
||||||
<?php
|
<?php
|
||||||
$cnt++;
|
$cnt++;
|
||||||
|
|
|
@ -36,6 +36,24 @@ class ProductCategoryVariantOptionsFixture extends TestFixture
|
||||||
'modified' => '2025-07-04 12:00:00',
|
'modified' => '2025-07-04 12:00:00',
|
||||||
'enabled' => 1,
|
'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();
|
parent::init();
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,7 +36,7 @@ class BaseControllerTest extends TestCase
|
||||||
{
|
{
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
$toCopy = PLUGIN_ROOT . DS . 'tests' . DS . 'test_app' . DS . 'webroot' . DS . 'images' . DS . '2c386086-f4c5-4093-bea5-ee9c29479f58.png';
|
$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';
|
'images' . DS . 'products' . DS . 'cfc98a9a-29b2-44c8-b587-8156adc05317';
|
||||||
$newName = $productsFolder . DS . '2c386086-f4c5-4093-bea5-ee9c29479f58.png';
|
$newName = $productsFolder . DS . '2c386086-f4c5-4093-bea5-ee9c29479f58.png';
|
||||||
if (file_exists($toCopy)) {
|
if (file_exists($toCopy)) {
|
||||||
|
|
|
@ -46,6 +46,8 @@ class ProductSkusControllerTest extends BaseControllerTest
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
// $this->enableCsrfToken();
|
// $this->enableCsrfToken();
|
||||||
// $this->enableSecurityToken();
|
// $this->enableSecurityToken();
|
||||||
|
$this->disableErrorHandlerMiddleware();
|
||||||
|
|
||||||
$config = $this->getTableLocator()->exists('ProductSkus') ? [] : ['className' => ProductSkusTable::class];
|
$config = $this->getTableLocator()->exists('ProductSkus') ? [] : ['className' => ProductSkusTable::class];
|
||||||
$this->ProductSkus = $this->getTableLocator()->get('ProductSkus', $config);
|
$this->ProductSkus = $this->getTableLocator()->get('ProductSkus', $config);
|
||||||
}
|
}
|
||||||
|
@ -127,6 +129,7 @@ class ProductSkusControllerTest extends BaseControllerTest
|
||||||
'plugin' => 'CakeProducts',
|
'plugin' => 'CakeProducts',
|
||||||
'controller' => 'ProductSkus',
|
'controller' => 'ProductSkus',
|
||||||
'action' => 'add',
|
'action' => 'add',
|
||||||
|
'cfc98a9a-29b2-44c8-b587-8156adc05317'
|
||||||
];
|
];
|
||||||
$this->get($url);
|
$this->get($url);
|
||||||
$this->assertResponseCode(200);
|
$this->assertResponseCode(200);
|
||||||
|
@ -154,13 +157,16 @@ class ProductSkusControllerTest extends BaseControllerTest
|
||||||
'plugin' => 'CakeProducts',
|
'plugin' => 'CakeProducts',
|
||||||
'controller' => 'ProductSkus',
|
'controller' => 'ProductSkus',
|
||||||
'action' => 'add',
|
'action' => 'add',
|
||||||
|
'cfc98a9a-29b2-44c8-b587-8156adc05317',
|
||||||
];
|
];
|
||||||
$data = [
|
$data = [
|
||||||
'product_id' => 'cfc98a9a-29b2-44c8-b587-8156adc05317',
|
0 => [
|
||||||
'sku' => 'cfc98a9a-29b2-44c8-b587-8156adc05317',
|
'add' => '1',
|
||||||
'barcode' => 'cfc98a9a-29b2-44c8-b587-8156adc05317',
|
'sku' => 'cfc98a9a-29b2-44c8-b587-8156adc05317',
|
||||||
'price' => 1.5,
|
'barcode' => 'cfc98a9a-29b2-44c8-b587-8156adc05317',
|
||||||
'cost' => 1.5,
|
'price' => 1.5,
|
||||||
|
'cost' => 1.5,
|
||||||
|
],
|
||||||
];
|
];
|
||||||
$this->post($url, $data);
|
$this->post($url, $data);
|
||||||
$this->assertResponseCode(302);
|
$this->assertResponseCode(302);
|
||||||
|
@ -189,13 +195,15 @@ class ProductSkusControllerTest extends BaseControllerTest
|
||||||
'plugin' => 'CakeProducts',
|
'plugin' => 'CakeProducts',
|
||||||
'controller' => 'ProductSkus',
|
'controller' => 'ProductSkus',
|
||||||
'action' => 'add',
|
'action' => 'add',
|
||||||
|
'cfc98a9a-29b2-44c8-b587-8156adc05317',
|
||||||
];
|
];
|
||||||
$data = [
|
$data = [
|
||||||
'product_id' => '999999999', //does not exist
|
0 => [
|
||||||
'sku' => 'cfc98a9a-29b2-44c8-b587-8156adc05317',
|
'sku' => '',
|
||||||
'barcode' => 'cfc98a9a-29b2-44c8-b587-8156adc05317',
|
'barcode' => 'cfc98a9a-29b2-44c8-b587-8156adc05317',
|
||||||
'price' => 1.5,
|
'price' => 1.5,
|
||||||
'cost' => 1.5,
|
'cost' => 1.5,
|
||||||
|
],
|
||||||
];
|
];
|
||||||
$this->post($url, $data);
|
$this->post($url, $data);
|
||||||
$this->assertResponseCode(200);
|
$this->assertResponseCode(200);
|
||||||
|
|
|
@ -59,7 +59,7 @@ Configure::write('App', [
|
||||||
Configure::write('debug', true);
|
Configure::write('debug', true);
|
||||||
Configure::write('CakeProducts', [
|
Configure::write('CakeProducts', [
|
||||||
'photos' => [
|
'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.
|
* internal CakeProducts settings - used in the source of truth/internal only system.
|
||||||
|
|
Loading…
Reference in New Issue