product photos first commit - only upload base photo

This commit is contained in:
2025-08-10 02:35:25 -07:00
parent 82b3ca59ed
commit a1012b4054
15 changed files with 1021 additions and 1 deletions

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' => [