product photos first commit - only upload base photo

This commit is contained in:
Brandon Shipley 2025-08-10 02:35:25 -07:00
parent 82b3ca59ed
commit a1012b4054
Signed by: bmfs
GPG Key ID: 14E38571D8BB0DE4
15 changed files with 1021 additions and 1 deletions

3
.gitignore vendored
View File

@ -6,4 +6,5 @@
/config/Migrations/schema-dump-default.lock
/vendor/
/.idea/
tmp
tmp
tests/test_app/webroot/images/products

View File

@ -0,0 +1,87 @@
<?php
declare(strict_types=1);
use Migrations\BaseMigration;
class CreateProductPhotos extends BaseMigration
{
/**
* Change Method.
*
* More information on this method is available here:
* https://book.cakephp.org/migrations/4/en/migrations.html#the-change-method
*
* @return void
*/
public function change(): void
{
$table = $this->table('product_photos', ['id' => false, 'primary_key' => ['id']]);
$table->addColumn('id', 'uuid', [
'default' => null,
'null' => false,
]);
$table->addColumn('product_id', 'uuid', [
'default' => null,
'null' => false,
]);
$table->addColumn('product_sku_id', 'uuid', [
'default' => null,
'null' => true,
]);
$table->addColumn('photo_dir', 'text', [
'default' => null,
'length' => 255,
'null' => false,
]);
$table->addColumn('photo_filename', 'string', [
'default' => null,
'length' => 255,
'null' => false,
]);
$table->addColumn('primary_photo', 'boolean', [
'default' => false,
'null' => false,
]);
$table->addColumn('photo_position', 'integer', [
'default' => 100,
'limit' => 11,
'null' => false,
]);
$table->addColumn('enabled', 'boolean', [
'default' => false,
'null' => false,
]);
$table->addColumn('created', 'datetime', [
'default' => null,
'null' => false,
]);
$table->addColumn('modified', 'datetime', [
'default' => null,
'null' => true,
]);
$table->addColumn('deleted', 'datetime', [
'default' => null,
'null' => true,
]);
$table->addIndex([
'product_id',
], [
'name' => 'PRODUCT_PHOTOS_BY_PRODUCT_ID',
'unique' => false,
]);
$table->addIndex([
'product_sku_id',
], [
'name' => 'PRODUCT_PHOTOS_BY_PRODUCT_SKU_ID',
'unique' => true,
]);
$table->create();
}
}

View File

@ -4,6 +4,9 @@
return [
'CakeProducts' => [
'photos' => [
'directory' => WWW_ROOT . 'images' . DS . 'products' . DS,
],
/**
* internal CakeProducts settings - used in the source of truth/internal only system.
* Can optionally manage external catalogs

View File

@ -0,0 +1,149 @@
<?php
declare(strict_types=1);
namespace CakeProducts\Controller;
use Cake\Core\Configure;
use Cake\Http\Exception\ForbiddenException;
use Cake\Utility\Text;
use CakeProducts\Controller\AppController;
use CheeseCake\Controller\Traits\OverrideTableTrait;
use Psr\Http\Message\UploadedFileInterface;
/**
* ProductPhotos Controller
*
* @property \CakeProducts\Model\Table\ProductPhotosTable $ProductPhotos
*/
class ProductPhotosController extends AppController
{
use OverrideTableTrait;
/**
* Index method
*
* @return \Cake\Http\Response|null|void Renders view
*/
public function index()
{
$query = $this->getTable()->find()
->contain(['Products']);
$productPhotos = $this->paginate($query);
$this->set(compact('productPhotos'));
}
/**
* View method
*
* @param string|null $id Product Photo id.
* @return \Cake\Http\Response|null|void Renders view
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
*/
public function view($id = null)
{
$productPhoto = $this->getTable()->get($id, contain: ['Products']);
$this->set(compact('productPhoto'));
}
/**
* Add method
*
* @return \Cake\Http\Response|null|void Redirects on successful add, renders view otherwise.
*/
public function add()
{
$productPhotosTable = $this->getTable();
$productPhoto = $productPhotosTable->newEmptyEntity();
if ($this->request->is('post')) {
$uuid = Text::uuid();
$postData = $this->request->getData();
$postData['id'] = $uuid;
$baseDir = Configure::readOrFail('CakeProducts.photos.directory');
$product = $productPhotosTable->Products->get($this->request->getData('product_id'));
$path = $baseDir . $product->id;
if ($this->request->getData('product_sku_id')) {
$productSku = $productPhotosTable->ProductSkus->get($this->request->getData('product_sku_id'));
$path .= DS . 'skus' . DS . $productSku->id;
}
/**
* @var UploadedFileInterface $photoObject
*/
$photoObject = $this->request->getData('photo');
if (!file_exists($path)) {
if (!mkdir($path, 0777, true)) {
dd('Failed to create the required folders. Please check the folder permissions and try again. PATH: ' . $path);
throw new ForbiddenException('Failed to create the required folders. Please check the folder permissions and try again.');
}
}
$destination = $path . DS . $uuid;
// Existing files with the same name will be replaced.
$photoObject->moveTo($destination);
if (!file_exists($destination)) {
dd('Failed to move the uploaded image to the appropriate folder. Please try again.');
throw new ForbiddenException('Failed to move the uploaded image to the appropriate folder. Please try again.');
}
$postData['photo_dir'] = $path;
$postData['photo_filename'] = $uuid;
$productPhoto = $productPhotosTable->patchEntity($productPhoto, $postData);
if ($productPhotosTable->save($productPhoto)) {
$this->Flash->success(__('The product photo has been saved.'));
return $this->redirect(['action' => 'index']);
}
dd(print_r($productPhoto->getErrors(), true));
$this->Flash->error(__('The product photo could not be saved. Please, try again.'));
}
$products = $productPhotosTable->Products->find('list', limit: 200)->all();
$this->set(compact('productPhoto', 'products'));
}
/**
* Edit method
*
* @param string|null $id Product Photo id.
* @return \Cake\Http\Response|null|void Redirects on successful edit, renders view otherwise.
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
*/
public function edit($id = null)
{
$productPhotosTable = $this->getTable();
$productPhoto = $productPhotosTable->get($id, contain: []);
if ($this->request->is(['patch', 'post', 'put'])) {
$productPhoto = $productPhotosTable->patchEntity($productPhoto, $this->request->getData());
if ($productPhotosTable->save($productPhoto)) {
$this->Flash->success(__('The product photo has been saved.'));
return $this->redirect(['action' => 'index']);
}
dd(print_r($productPhoto->getErrors(), true));
$this->Flash->error(__('The product photo could not be saved. Please, try again.'));
}
$products = $productPhotosTable->Products->find('list', limit: 200)->all();
$this->set(compact('productPhoto', 'products'));
}
/**
* Delete method
*
* @param string|null $id Product Photo id.
* @return \Cake\Http\Response|null Redirects to index.
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
*/
public function delete($id = null)
{
$this->request->allowMethod(['post', 'delete']);
$productPhotosTable = $this->getTable();
$productPhoto = $productPhotosTable->get($id);
if ($productPhotosTable->delete($productPhoto)) {
$this->Flash->success(__('The product photo has been deleted.'));
} else {
$this->Flash->error(__('The product photo could not be deleted. Please, try again.'));
}
return $this->redirect(['action' => 'index']);
}
}

View File

@ -0,0 +1,49 @@
<?php
declare(strict_types=1);
namespace CakeProducts\Model\Entity;
use Cake\ORM\Entity;
/**
* ProductPhoto Entity
*
* @property string $id
* @property string $product_id
* @property string|null $product_sku_id
* @property string $photo_dir
* @property string $photo_filename
* @property bool $primary_photo
* @property int $photo_position
* @property bool $enabled
* @property \Cake\I18n\DateTime $created
* @property \Cake\I18n\DateTime|null $modified
* @property \Cake\I18n\DateTime|null $deleted
*
* @property \CakeProducts\Model\Entity\Product $product
*/
class ProductPhoto extends Entity
{
/**
* Fields that can be mass assigned using newEntity() or patchEntity().
*
* Note that when '*' is set to true, this allows all unspecified fields to
* be mass assigned. For security purposes, it is advised to set '*' to false
* (or remove it), and explicitly make individual fields accessible as needed.
*
* @var array<string, bool>
*/
protected array $_accessible = [
'product_id' => true,
'product_sku_id' => true,
'photo_dir' => true,
'photo_filename' => true,
'primary_photo' => true,
'photo_position' => true,
'enabled' => true,
'created' => true,
'modified' => true,
'deleted' => true,
'product' => true,
];
}

View File

@ -0,0 +1,119 @@
<?php
declare(strict_types=1);
namespace CakeProducts\Model\Table;
use Cake\ORM\Query\SelectQuery;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;
/**
* ProductPhotos Model
*
* @property \CakeProducts\Model\Table\ProductsTable&\Cake\ORM\Association\BelongsTo $Products
*
* @method \CakeProducts\Model\Entity\ProductPhoto newEmptyEntity()
* @method \CakeProducts\Model\Entity\ProductPhoto newEntity(array $data, array $options = [])
* @method array<\CakeProducts\Model\Entity\ProductPhoto> newEntities(array $data, array $options = [])
* @method \CakeProducts\Model\Entity\ProductPhoto get(mixed $primaryKey, array|string $finder = 'all', \Psr\SimpleCache\CacheInterface|string|null $cache = null, \Closure|string|null $cacheKey = null, mixed ...$args)
* @method \CakeProducts\Model\Entity\ProductPhoto findOrCreate($search, ?callable $callback = null, array $options = [])
* @method \CakeProducts\Model\Entity\ProductPhoto patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = [])
* @method array<\CakeProducts\Model\Entity\ProductPhoto> patchEntities(iterable $entities, array $data, array $options = [])
* @method \CakeProducts\Model\Entity\ProductPhoto|false save(\Cake\Datasource\EntityInterface $entity, array $options = [])
* @method \CakeProducts\Model\Entity\ProductPhoto saveOrFail(\Cake\Datasource\EntityInterface $entity, array $options = [])
* @method iterable<\CakeProducts\Model\Entity\ProductPhoto>|\Cake\Datasource\ResultSetInterface<\CakeProducts\Model\Entity\ProductPhoto>|false saveMany(iterable $entities, array $options = [])
* @method iterable<\CakeProducts\Model\Entity\ProductPhoto>|\Cake\Datasource\ResultSetInterface<\CakeProducts\Model\Entity\ProductPhoto> saveManyOrFail(iterable $entities, array $options = [])
* @method iterable<\CakeProducts\Model\Entity\ProductPhoto>|\Cake\Datasource\ResultSetInterface<\CakeProducts\Model\Entity\ProductPhoto>|false deleteMany(iterable $entities, array $options = [])
* @method iterable<\CakeProducts\Model\Entity\ProductPhoto>|\Cake\Datasource\ResultSetInterface<\CakeProducts\Model\Entity\ProductPhoto> deleteManyOrFail(iterable $entities, array $options = [])
*
* @mixin \Cake\ORM\Behavior\TimestampBehavior
*/
class ProductPhotosTable extends Table
{
/**
* Initialize method
*
* @param array<string, mixed> $config The configuration for the Table.
* @return void
*/
public function initialize(array $config): void
{
parent::initialize($config);
$this->setTable('product_photos');
$this->setDisplayField('photo_filename');
$this->setPrimaryKey('id');
$this->addBehavior('Timestamp');
$this->belongsTo('Products', [
'foreignKey' => 'product_id',
'joinType' => 'INNER',
'className' => 'CakeProducts.Products',
]);
}
/**
* Default validation rules.
*
* @param \Cake\Validation\Validator $validator Validator instance.
* @return \Cake\Validation\Validator
*/
public function validationDefault(Validator $validator): Validator
{
$validator
->uuid('product_id')
->notEmptyString('product_id');
$validator
->uuid('product_sku_id')
->allowEmptyString('product_sku_id')
->add('product_sku_id', 'unique', ['rule' => 'validateUnique', 'provider' => 'table']);
$validator
->scalar('photo_dir')
->maxLength('photo_dir', 255)
->requirePresence('photo_dir', 'create')
->notEmptyString('photo_dir');
$validator
->scalar('photo_filename')
->maxLength('photo_filename', 255)
->requirePresence('photo_filename', 'create')
->notEmptyString('photo_filename');
$validator
->boolean('primary_photo')
->notEmptyString('primary_photo');
$validator
->integer('photo_position')
->notEmptyString('photo_position');
$validator
->boolean('enabled')
->notEmptyString('enabled');
$validator
->dateTime('deleted')
->allowEmptyDateTime('deleted');
return $validator;
}
/**
* Returns a rules checker object that will be used for validating
* application integrity.
*
* @param \Cake\ORM\RulesChecker $rules The rules object to be modified.
* @return \Cake\ORM\RulesChecker
*/
public function buildRules(RulesChecker $rules): RulesChecker
{
$rules->add($rules->isUnique(['product_sku_id'], ['allowMultipleNulls' => true]), ['errorField' => '0']);
$rules->add($rules->existsIn(['product_id'], 'Products'), ['errorField' => '1']);
return $rules;
}
}

View File

@ -0,0 +1,26 @@
<?php
/**
* @var \App\View\AppView $this
* @var \Cake\Datasource\EntityInterface $productPhoto
* @var \Cake\Collection\CollectionInterface|string[] $products
*/
?>
<div class="row">
<aside class="column">
<div class="side-nav">
<h4 class="heading"><?= __('Actions') ?></h4>
<?= $this->Html->link(__('List Product Photos'), ['action' => 'index'], ['class' => 'side-nav-item']) ?>
</div>
</aside>
<div class="column column-80">
<div class="productPhotos form content">
<?= $this->Form->create($productPhoto, ['type' => 'file']) ?>
<fieldset>
<legend><?= __('Add Product Photo') ?></legend>
<?= $this->element('ProductPhotos/form', ['includeFile' => true]); ?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
</div>
</div>
</div>

View File

@ -0,0 +1,31 @@
<?php
/**
* @var \App\View\AppView $this
* @var \Cake\Datasource\EntityInterface $productPhoto
* @var string[]|\Cake\Collection\CollectionInterface $products
*/
?>
<div class="row">
<aside class="column">
<div class="side-nav">
<h4 class="heading"><?= __('Actions') ?></h4>
<?= $this->Form->postLink(
__('Delete'),
['action' => 'delete', $productPhoto->id],
['confirm' => __('Are you sure you want to delete # {0}?', $productPhoto->id), 'class' => 'side-nav-item']
) ?>
<?= $this->Html->link(__('List Product Photos'), ['action' => 'index'], ['class' => 'side-nav-item']) ?>
</div>
</aside>
<div class="column column-80">
<div class="productPhotos form content">
<?= $this->Form->create($productPhoto) ?>
<fieldset>
<legend><?= __('Edit Product Photo') ?></legend>
<?= $this->element('ProductPhotos/form', ['includeFile' => false]); ?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
</div>
</div>
</div>

View File

@ -0,0 +1,60 @@
<?php
/**
* @var \App\View\AppView $this
* @var iterable<\Cake\Datasource\EntityInterface> $productPhotos
*/
?>
<div class="productPhotos index content">
<?= $this->Html->link(__('New Product Photo'), ['action' => 'add'], ['class' => 'button float-right']) ?>
<h3><?= __('Product Photos') ?></h3>
<div class="table-responsive" id="table-container">
<table>
<thead>
<tr>
<th><?= $this->Paginator->sort('id') ?></th>
<th><?= $this->Paginator->sort('product_id') ?></th>
<th><?= $this->Paginator->sort('product_sku_id') ?></th>
<th><?= $this->Paginator->sort('photo_filename') ?></th>
<th><?= $this->Paginator->sort('primary_photo') ?></th>
<th><?= $this->Paginator->sort('photo_position') ?></th>
<th><?= $this->Paginator->sort('enabled') ?></th>
<th><?= $this->Paginator->sort('created') ?></th>
<th><?= $this->Paginator->sort('modified') ?></th>
<th><?= $this->Paginator->sort('deleted') ?></th>
<th class="actions"><?= __('Actions') ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($productPhotos as $productPhoto): ?>
<tr>
<td><?= h($productPhoto->id) ?></td>
<td><?= $productPhoto->hasValue('product') ? $this->Html->link($productPhoto->product->name, ['controller' => 'Products', 'action' => 'view', $productPhoto->product->id]) : '' ?></td>
<td><?= h($productPhoto->product_sku_id) ?></td>
<td><?= h($productPhoto->photo_filename) ?></td>
<td><?= h($productPhoto->primary_photo) ?></td>
<td><?= $this->Number->format($productPhoto->photo_position) ?></td>
<td><?= h($productPhoto->enabled) ?></td>
<td><?= h($productPhoto->created) ?></td>
<td><?= h($productPhoto->modified) ?></td>
<td><?= h($productPhoto->deleted) ?></td>
<td class="actions">
<?= $this->Html->link(__('View'), ['action' => 'view', $productPhoto->id]) ?>
<?= $this->Html->link(__('Edit'), ['action' => 'edit', $productPhoto->id]) ?>
<?= $this->Form->postLink(__('Delete'), ['action' => 'delete', $productPhoto->id], ['confirm' => __('Are you sure you want to delete # {0}?', $productPhoto->id)]) ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<div class="paginator">
<ul class="pagination">
<?= $this->Paginator->first('<< ' . __('first')) ?>
<?= $this->Paginator->prev('< ' . __('previous')) ?>
<?= $this->Paginator->numbers() ?>
<?= $this->Paginator->next(__('next') . ' >') ?>
<?= $this->Paginator->last(__('last') . ' >>') ?>
</ul>
<p><?= $this->Paginator->counter(__('Page {{page}} of {{pages}}, showing {{current}} record(s) out of {{count}} total')) ?></p>
</div>
</div>

View File

@ -0,0 +1,70 @@
<?php
/**
* @var \App\View\AppView $this
* @var \Cake\Datasource\EntityInterface $productPhoto
*/
?>
<div class="row">
<aside class="column">
<div class="side-nav">
<h4 class="heading"><?= __('Actions') ?></h4>
<?= $this->Html->link(__('Edit Product Photo'), ['action' => 'edit', $productPhoto->id], ['class' => 'side-nav-item']) ?>
<?= $this->Form->postLink(__('Delete Product Photo'), ['action' => 'delete', $productPhoto->id], ['confirm' => __('Are you sure you want to delete # {0}?', $productPhoto->id), 'class' => 'side-nav-item']) ?>
<?= $this->Html->link(__('List Product Photos'), ['action' => 'index'], ['class' => 'side-nav-item']) ?>
<?= $this->Html->link(__('New Product Photo'), ['action' => 'add'], ['class' => 'side-nav-item']) ?>
</div>
</aside>
<div class="column column-80">
<div class="productPhotos view content">
<h3><?= h($productPhoto->photo_filename) ?></h3>
<table>
<tr>
<th><?= __('Id') ?></th>
<td><?= h($productPhoto->id) ?></td>
</tr>
<tr>
<th><?= __('Product') ?></th>
<td><?= $productPhoto->hasValue('product') ? $this->Html->link($productPhoto->product->name, ['controller' => 'Products', 'action' => 'view', $productPhoto->product->id]) : '' ?></td>
</tr>
<tr>
<th><?= __('Product Sku Id') ?></th>
<td><?= h($productPhoto->product_sku_id) ?></td>
</tr>
<tr>
<th><?= __('Photo Filename') ?></th>
<td><?= h($productPhoto->photo_filename) ?></td>
</tr>
<tr>
<th><?= __('Photo Position') ?></th>
<td><?= $this->Number->format($productPhoto->photo_position) ?></td>
</tr>
<tr>
<th><?= __('Created') ?></th>
<td><?= h($productPhoto->created) ?></td>
</tr>
<tr>
<th><?= __('Modified') ?></th>
<td><?= h($productPhoto->modified) ?></td>
</tr>
<tr>
<th><?= __('Deleted') ?></th>
<td><?= h($productPhoto->deleted) ?></td>
</tr>
<tr>
<th><?= __('Primary Photo') ?></th>
<td><?= $productPhoto->primary_photo ? __('Yes') : __('No'); ?></td>
</tr>
<tr>
<th><?= __('Enabled') ?></th>
<td><?= $productPhoto->enabled ? __('Yes') : __('No'); ?></td>
</tr>
</table>
<div class="text">
<strong><?= __('Photo Dir') ?></strong>
<blockquote>
<?= $this->Text->autoParagraph(h($productPhoto->photo_dir)); ?>
</blockquote>
</div>
</div>
</div>
</div>

View File

@ -0,0 +1,24 @@
<?php
/**
* @var \App\View\AppView $this
* @var \CakeProducts\Model\Entity\Product[]|\Cake\Datasource\EntityInterface[] $products
* @var \CakeProducts\Model\Entity\Product[]|\Cake\Datasource\EntityInterface[] $productSkus
* @var bool $includeFile
*/
$includeFile = $includeFile ?? false;
echo $this->Form->control('product_id', [
'options' => $products ?? [],
]);
echo $this->Form->control('product_sku_id', [
'options' => $productSkus ?? []
]);
echo $includeFile ? $this->Form->control('photo', [
'type' => 'file',
]) : '';
echo $this->Form->control('primary_photo');
echo $this->Form->control('photo_position');
echo $this->Form->control('enabled');
?>

View File

@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
namespace CakeProducts\Test\Fixture;
use Cake\TestSuite\Fixture\TestFixture;
/**
* ProductPhotosFixture
*/
class ProductPhotosFixture extends TestFixture
{
/**
* Init method
*
* @return void
*/
public function init(): void
{
$this->records = [
[
'id' => '2c386086-f4c5-4093-bea5-ee9c29479f58',
'product_id' => 'cfc98a9a-29b2-44c8-b587-8156adc05317',
'product_sku_id' => null,
'photo_dir' => '/products/cfc98a9a-29b2-44c8-b587-8156adc05317/',
'photo_filename' => '2c386086-f4c5-4093-bea5-ee9c29479f58.jpg',
'primary_photo' => 1,
'photo_position' => 100,
'enabled' => 1,
'created' => '2025-08-10 04:32:10',
'modified' => '2025-08-10 04:32:10',
'deleted' => null,
],
];
parent::init();
}
}

View File

@ -0,0 +1,338 @@
<?php
declare(strict_types=1);
namespace CakeProducts\Test\TestCase\Controller;
use Cake\Core\Configure;
use Cake\ORM\Table;
use CakeProducts\Controller\ProductPhotosController;
use CakeProducts\Model\Table\ProductPhotosTable;
use FilesystemIterator;
use Laminas\Diactoros\UploadedFile;
use PHPUnit\Exception;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use const UPLOAD_ERR_OK;
/**
* CakeProducts\Controller\ProductPhotosController Test Case
*
* @uses ProductPhotosController
*/
class ProductPhotosControllerTest extends BaseControllerTest
{
/**
* Test subject table
*
* @var ProductPhotosTable|Table
*/
protected $ProductPhotos;
/**
* Fixtures
*
* @var array<string>
*/
protected array $fixtures = [
'plugin.CakeProducts.Products',
'plugin.CakeProducts.ProductSkus',
'plugin.CakeProducts.ProductPhotos',
];
/**
* setUp method
*
* @return void
*/
protected function setUp(): void
{
parent::setUp();
$this->ProductPhotos = $this->getTableLocator()->get('ProductPhotos');
}
/**
* tearDown method
*
* @return void
*/
protected function tearDown(): void
{
unset($this->ProductPhotos);
$path = Configure::readOrFail('CakeProducts.photos.directory');
if (file_exists($path)) {
$di = new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS);
$ri = new RecursiveIteratorIterator($di, RecursiveIteratorIterator::CHILD_FIRST);
foreach ( $ri as $file ) {
$file->isDir() ? rmdir($file->getRealPath()) : unlink($file->getRealPath());
}
}
parent::tearDown();
}
/**
* Test index method
*
* Tests the index action with a logged in user
*
* @return void
* @throws Exception
*
* @uses ProductPhotosController::index
*/
public function testIndexGetLoggedIn(): void
{
// $this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ProductPhotos',
'action' => 'index',
];
$this->get($url);
$this->assertResponseCode(200);
}
/**
* Test view method
*
* Tests the view action with a logged in user
*
* @return void
* @throws Exception
*
* @uses ProductPhotosController::view
*/
public function testViewGetLoggedIn(): void
{
$id = '2c386086-f4c5-4093-bea5-ee9c29479f58';
// $this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ProductPhotos',
'action' => 'view',
$id,
];
$this->get($url);
$this->assertResponseCode(200);
}
/**
* Test add method
*
* Tests the add action with a logged in user
*
* @return void
* @throws Exception
*
* @uses ProductPhotosController::add
*/
public function testAddGetLoggedIn(): void
{
$cntBefore = $this->ProductPhotos->find()->count();
// $this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ProductPhotos',
'action' => 'add',
];
$this->get($url);
$this->assertResponseCode(200);
$cntAfter = $this->ProductPhotos->find()->count();
$this->assertEquals($cntBefore, $cntAfter);
}
/**
* Test add method
*
* Tests a POST request to the add action with a logged in user
*
* @return void
* @throws Exception
*
* @uses ProductPhotosController::add
*/
public function testAddPostLoggedInSuccess(): void
{
$cntBefore = $this->ProductPhotos->find()->count();
// $this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ProductPhotos',
'action' => 'add',
];
$image = new UploadedFile(
Configure::readOrFail('App.paths.webroot') . 'images' . DS . 'cake_icon.png', // stream or path to file representing the temp file
12345, // the filesize in bytes
UPLOAD_ERR_OK, // the upload/error status
'cake_icon.png', // the filename as sent by the client
'image/png' // the mimetype as sent by the client
);
$this->configRequest([
'files' => [
'photo' => $image,
],
]);
$data = [
'product_id' => 'cfc98a9a-29b2-44c8-b587-8156adc05317',
'product_sku_id' => '',
'primary_photo' => 0,
'photo' => $image,
'enabled' => 1,
];
$this->post($url, $data);
$this->assertResponseCode(302);
$this->assertRedirectContains('product-photos');
$cntAfter = $this->ProductPhotos->find()->count();
$this->assertEquals($cntBefore + 1, $cntAfter);
}
/**
* Test add method
*
* Tests a POST request to the add action with a logged in user
*
* @return void
* @throws Exception
*
* @uses ProductPhotosController::add
*/
public function testAddPostLoggedInFailure(): void
{
$cntBefore = $this->ProductPhotos->find()->count();
// $this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ProductPhotos',
'action' => 'add',
];
$data = [];
$this->post($url, $data);
$this->assertResponseCode(200);
$cntAfter = $this->ProductPhotos->find()->count();
$this->assertEquals($cntBefore, $cntAfter);
}
/**
* Test edit method
*
* Tests the edit action with a logged in user
*
* @return void
* @throws Exception
*
* @uses ProductPhotosController::edit
*/
public function testEditGetLoggedIn(): void
{
// $this->loginUserByRole('admin');
$id = '2c386086-f4c5-4093-bea5-ee9c29479f58';
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ProductPhotos',
'action' => 'edit',
$id,
];
$this->get($url);
$this->assertResponseCode(200);
}
/**
* Test edit method
*
* Tests a PUT request to the edit action with a logged in user
*
* @return void
* @throws Exception
*
* @uses ProductPhotosController::edit
*/
public function testEditPutLoggedInSuccess(): void
{
// $this->loginUserByRole('admin');
$id = '2c386086-f4c5-4093-bea5-ee9c29479f58';
$before = $this->ProductPhotos->get($id);
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ProductPhotos',
'action' => 'edit',
$id,
];
$data = [
// test new data here
'enabled' => 1,
'primary_photo' => 1,
'photo_position' => 999,
];
// $this->put($url, $data);
$this->assertResponseCode(302);
$this->assertRedirectContains('product-photos');
$after = $this->ProductPhotos->get($id);
// assert saved properly below
}
/**
* Test edit method
*
* Tests a PUT request to the edit action with a logged in user
*
* @return void
* @throws Exception
*
* @uses ProductPhotosController::edit
*/
public function testEditPutLoggedInFailure(): void
{
// $this->loginUserByRole('admin');
$id = '2c386086-f4c5-4093-bea5-ee9c29479f58';
$before = $this->ProductPhotos->get($id);
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ProductPhotos',
'action' => 'edit',
$id,
];
$data = [];
$this->put($url, $data);
$this->assertResponseCode(200);
$after = $this->ProductPhotos->get($id);
// assert save failed below
}
/**
* Test delete method
*
* Tests the delete action with a logged in user
*
* @return void
* @throws Exception
*
* @uses ProductPhotosController::delete
*/
public function testDeleteLoggedIn(): void
{
$cntBefore = $this->ProductPhotos->find()->count();
$id = '2c386086-f4c5-4093-bea5-ee9c29479f58';
// $this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ProductPhotos',
'action' => 'delete',
$id,
];
$this->delete($url);
$this->assertResponseCode(302);
$this->assertRedirectContains('product-photos');
$cntAfter = $this->ProductPhotos->find()->count();
$this->assertEquals($cntBefore - 1, $cntAfter);
}
}

View File

@ -48,6 +48,7 @@ Configure::write('App', [
'namespace' => 'TestApp',
'encoding' => 'UTF-8',
'paths' => [
'webroot' => PLUGIN_ROOT . DS . 'webroot' . DS,
'templates' => [
PLUGIN_ROOT . DS . 'tests' . DS . 'test_app' . DS . 'templates' . DS,
],
@ -55,6 +56,31 @@ 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,
],
/**
* internal CakeProducts settings - used in the source of truth/internal only system.
* Can optionally manage external catalogs
*
* - syncExternally - defaults to false - product catalogs can have 1 or more external catalogs linked to them
* which will receive changes to the catalogs and optionally allow for external API access.
* Will have no effect if true but no external catalogs have been added or none are enabled
*/
'internal' => [
'enabled' => true,
/**
* syncExternally defaults to false - product catalogs can have 1 or more external catalogs linked to them
* which will receive changes to the catalogs and optionally allow for external API access.
* Will have no effect if true but no external catalogs have been added or none are enabled
*/
'syncExternally' => false,
],
'external' => [ // product catalog settings for external use (as an API server to power an ecommerce site for example)
'enabled' => false,
],
]);
$cache = [
'default' => [

Binary file not shown.

After

Width:  |  Height:  |  Size: 943 B