product photos first commit - only upload base photo
This commit is contained in:
338
tests/TestCase/Controller/ProductPhotosControllerTest.php
Normal file
338
tests/TestCase/Controller/ProductPhotosControllerTest.php
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user