bring into standalone plugin for distribution

This commit is contained in:
2024-11-24 18:38:29 -08:00
parent 279d46f14b
commit ded60d16bf
83 changed files with 7020 additions and 1 deletions

View File

@@ -0,0 +1,25 @@
<?php
namespace CakeProducts\Test\TestCase\Controller;
use Cake\TestSuite\IntegrationTestTrait;
use Cake\TestSuite\TestCase;
class BaseControllerTest extends TestCase
{
use IntegrationTestTrait;
public function loginUserByRole(string $role = 'admin'): void
{
$this->session(['Auth.User.id' => 1]);
$this->session(['Auth.id' => 1]);
}
/**
* @return void
*/
public function testTest()
{
$this->assertEquals(1, 1);
}
}

View File

@@ -0,0 +1,446 @@
<?php
declare(strict_types=1);
namespace CakeProducts\Test\TestCase\Controller;
use Cake\ORM\Table;
use Cake\TestSuite\IntegrationTestTrait;
use Cake\TestSuite\TestCase;
use CakeProducts\Controller\ExternalProductCatalogsController;
use CakeProducts\Model\Table\ExternalProductCatalogsTable;
use PHPUnit\Exception;
/**
* CakeProducts\Controller\ExternalProductCatalogsController Test Case
*
* @uses \CakeProducts\Controller\ExternalProductCatalogsController
*/
class ExternalProductCatalogsControllerTest extends BaseControllerTest
{
/**
* Test subject table
*
* @var ExternalProductCatalogsTable|Table
*/
protected $ExternalProductCatalogs;
/**
* Fixtures
*
* @var array<string>
*/
protected array $fixtures = [
'plugin.CakeProducts.ExternalProductCatalogs',
'plugin.CakeProducts.ProductCatalogs',
];
/**
* setUp method
*
* @return void
*/
protected function setUp(): void
{
parent::setUp();
$this->enableCsrfToken();
$this->enableSecurityToken();
$this->ExternalProductCatalogs = $this->getTableLocator()->get('ExternalProductCatalogs');
}
/**
* tearDown method
*
* @return void
*/
protected function tearDown(): void
{
unset($this->ExternalProductCatalogs);
parent::tearDown();
}
/**
* Test index method
*
* Tests the index action with an unauthenticated user (not logged in)
*
* @uses \CakeProducts\Controller\ExternalProductCatalogsController::index()
* @throws Exception
*
* @return void
*/
public function testIndexGetUnauthenticated(): void
{
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ExternalProductCatalogs',
'action' => 'index',
];
$this->get($url);
$this->assertResponseCode(302);
$this->assertRedirectContains('login');
}
/**
* Test index method
*
* Tests the index action with a logged in user
*
* @uses \CakeProducts\Controller\ExternalProductCatalogsController::index()
* @throws Exception
*
* @return void
*/
public function testIndexGetLoggedIn(): void
{
$this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ExternalProductCatalogs',
'action' => 'index',
];
$this->get($url);
$this->assertResponseCode(200);
}
/**
* Test view method
*
* Tests the view action with an unauthenticated user (not logged in)
*
* @uses \CakeProducts\Controller\ExternalProductCatalogsController::view()
* @throws Exception
*
* @return void
*/
public function testViewGetUnauthenticated(): void
{
$id = 1;
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ExternalProductCatalogs',
'action' => 'view',
$id,
];
$this->get($url);
$this->assertResponseCode(302);
$this->assertRedirectContains('login');
}
/**
* Test view method
*
* Tests the view action with a logged in user
*
* @uses \CakeProducts\Controller\ExternalProductCatalogsController::view()
* @throws Exception
*
* @return void
*/
public function testViewGetLoggedIn(): void
{
$id = 1;
$this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ExternalProductCatalogs',
'action' => 'view',
$id,
];
$this->get($url);
$this->assertResponseCode(200);
}
/**
* Test add method
*
* Tests the add action with an unauthenticated user (not logged in)
*
* @uses \CakeProducts\Controller\ExternalProductCatalogsController::add()
* @throws Exception
*
* @return void
*/
public function testAddGetUnauthenticated(): void
{
$cntBefore = $this->ExternalProductCatalogs->find()->count();
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ExternalProductCatalogs',
'action' => 'add',
];
$this->get($url);
$this->assertResponseCode(302);
$this->assertRedirectContains('login');
$cntAfter = $this->ExternalProductCatalogs->find()->count();
$this->assertEquals($cntBefore, $cntAfter);
}
/**
* Test add method
*
* Tests the add action with a logged in user
*
* @uses \CakeProducts\Controller\ExternalProductCatalogsController::add()
* @throws Exception
*
* @return void
*/
public function testAddGetLoggedIn(): void
{
$cntBefore = $this->ExternalProductCatalogs->find()->count();
$this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ExternalProductCatalogs',
'action' => 'add',
];
$this->get($url);
$this->assertResponseCode(200);
$cntAfter = $this->ExternalProductCatalogs->find()->count();
$this->assertEquals($cntBefore, $cntAfter);
}
/**
* Test add method
*
* Tests a POST request to the add action with a logged in user
*
* @uses \CakeProducts\Controller\ExternalProductCatalogsController::add()
* @throws Exception
*
* @return void
*/
public function testAddPostLoggedInSuccess(): void
{
$cntBefore = $this->ExternalProductCatalogs->find()->count();
$this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ExternalProductCatalogs',
'action' => 'add',
];
$data = [
'product_catalog_id' => 'f56f3412-ed23-490b-be6e-016208c415d2',
'base_url' => 'http://localhost:8766',
'api_url' => 'http://localhost:8766/api/v1/',
'enabled' => true,
];
$this->post($url, $data);
$this->assertResponseCode(302);
$this->assertRedirectContains('external-product-catalogs');
$cntAfter = $this->ExternalProductCatalogs->find()->count();
$this->assertEquals($cntBefore + 1, $cntAfter);
}
/**
* Test add method
*
* Tests a POST request to the add action with a logged in user
*
* @uses \CakeProducts\Controller\ExternalProductCatalogsController::add()
* @throws Exception
*
* @return void
*/
public function testAddPostLoggedInFailure(): void
{
$cntBefore = $this->ExternalProductCatalogs->find()->count();
$this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ExternalProductCatalogs',
'action' => 'add',
];
$data = [
'product_catalog_id' => 999999,
'base_url' => '',
'api_url' => 'http://localhost:8766/api/v1/',
'enabled' => true,
];
$this->post($url, $data);
$this->assertResponseCode(200);
$cntAfter = $this->ExternalProductCatalogs->find()->count();
$this->assertEquals($cntBefore, $cntAfter);
}
/**
* Test edit method
*
* Tests the edit action with an unauthenticated user (not logged in)
*
* @uses \CakeProducts\Controller\ExternalProductCatalogsController::edit()
* @throws Exception
*
* @return void
*/
public function testEditGetUnauthenticated(): void
{
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ExternalProductCatalogs',
'action' => 'edit',
1,
];
$this->get($url);
$this->assertResponseCode(302);
$this->assertRedirectContains('login');
}
/**
* Test edit method
*
* Tests the edit action with a logged in user
*
* @uses \CakeProducts\Controller\ExternalProductCatalogsController::edit()
* @throws Exception
*
* @return void
*/
public function testEditGetLoggedIn(): void
{
$this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ExternalProductCatalogs',
'action' => 'edit',
1,
];
$this->get($url);
$this->assertResponseCode(200);
}
/**
* Test edit method
*
* Tests a PUT request to the edit action with a logged in user
*
* @uses \CakeProducts\Controller\ExternalProductCatalogsController::edit()
* @throws Exception
*
* @return void
*/
public function testEditPutLoggedInSuccess(): void
{
$this->loginUserByRole('admin');
$id = 1;
$before = $this->ExternalProductCatalogs->get($id);
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ExternalProductCatalogs',
'action' => 'edit',
$id,
];
$data = [
'product_catalog_id' => '115153f3-2f59-4234-8ff8-e1b205761428',
'base_url' => 'http://localhost:8766',
'api_url' => 'http://localhost:8766/api/v1/',
'enabled' => true,
];
$this->put($url, $data);
$this->assertResponseCode(302);
$this->assertRedirectContains('external-product-catalogs');
$after = $this->ExternalProductCatalogs->get($id);
// assert saved properly below
}
/**
* Test edit method
*
* Tests a PUT request to the edit action with a logged in user
*
* @uses \CakeProducts\Controller\ExternalProductCatalogsController::edit()
* @throws Exception
*
* @return void
*/
public function testEditPutLoggedInFailure(): void
{
$this->loginUserByRole('admin');
$id = 1;
$before = $this->ExternalProductCatalogs->get($id);
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ExternalProductCatalogs',
'action' => 'edit',
$id,
];
$data = [
'product_catalog_id' => 9999999,
'base_url' => '',
'api_url' => 'http://localhost:8766/api/v1/',
'enabled' => true,
];
$this->put($url, $data);
$this->assertResponseCode(200);
$after = $this->ExternalProductCatalogs->get($id);
// assert save failed below
}
/**
* Test delete method
*
* Tests the delete action with an unauthenticated user (not logged in)
*
* @uses \CakeProducts\Controller\ExternalProductCatalogsController::delete()
* @throws Exception
*
* @return void
*/
public function testDeleteUnauthenticated(): void
{
$cntBefore = $this->ExternalProductCatalogs->find()->count();
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ExternalProductCatalogs',
'action' => 'delete',
1,
];
$this->delete($url);
$this->assertResponseCode(302);
$this->assertRedirectContains('login');
$cntAfter = $this->ExternalProductCatalogs->find()->count();
$this->assertEquals($cntBefore, $cntAfter);
}
/**
* Test delete method
*
* Tests the delete action with a logged in user
*
* @uses \CakeProducts\Controller\ExternalProductCatalogsController::delete()
* @throws Exception
*
* @return void
*/
public function testDeleteLoggedIn(): void
{
$cntBefore = $this->ExternalProductCatalogs->find()->count();
$this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ExternalProductCatalogs',
'action' => 'delete',
1,
];
$this->delete($url);
$this->assertResponseCode(302);
$this->assertRedirectContains('external-product-catalogs');
$cntAfter = $this->ExternalProductCatalogs->find()->count();
$this->assertEquals($cntBefore - 1, $cntAfter);
}
}

View File

@@ -0,0 +1,450 @@
<?php
declare(strict_types=1);
namespace CakeProducts\Test\TestCase\Controller;
use Cake\ORM\Table;
use Cake\TestSuite\IntegrationTestTrait;
use Cake\TestSuite\TestCase;
use CakeProducts\Controller\ProductCatalogsController;
use CakeProducts\Model\Table\ProductCatalogsTable;
use PHPUnit\Exception;
/**
* CakeProducts\Controller\ProductCatalogsController Test Case
*
* @uses \CakeProducts\Controller\ProductCatalogsController
*/
class ProductCatalogsControllerTest extends BaseControllerTest
{
/**
* Test subject table
*
* @var ProductCatalogsTable|Table
*/
protected $ProductCatalogs;
/**
* Fixtures
*
* @var array<string>
*/
protected array $fixtures = [
'plugin.CakeProducts.ProductCatalogs',
'plugin.CakeProducts.ProductCategories',
];
/**
* setUp method
*
* @return void
*/
protected function setUp(): void
{
parent::setUp();
$this->enableCsrfToken();
$this->enableSecurityToken();
$config = $this->getTableLocator()->exists('ProductCatalogs') ? [] : ['className' => ProductCatalogsTable::class];
$this->ProductCatalogs = $this->getTableLocator()->get('ProductCatalogs', $config);
}
/**
* tearDown method
*
* @return void
*/
protected function tearDown(): void
{
unset($this->ProductCatalogs);
parent::tearDown();
}
/**
* Test index method
*
* Tests the index action with an unauthenticated user (not logged in)
*
* @uses ProductCatalogsController::index()
* @throws Exception
*
* @return void
*/
public function testIndexGetUnauthenticated(): void
{
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ProductCatalogs',
'action' => 'index',
];
$this->get($url);
$this->assertResponseCode(302);
$this->assertRedirectContains('login');
}
/**
* Test index method
*
* Tests the index action with a logged in user
*
* @uses \CakeProducts\Controller\ProductCatalogsController::index()
* @throws Exception
*
* @return void
*/
public function testIndexGetLoggedIn(): void
{
$this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ProductCatalogs',
'action' => 'index',
];
$this->get($url);
$this->assertResponseCode(200);
}
/**
* Test view method
*
* Tests the view action with an unauthenticated user (not logged in)
*
* @uses ProductCatalogsController::view()
* @throws Exception
*
* @return void
*/
public function testViewGetUnauthenticated(): void
{
$id = '115153f3-2f59-4234-8ff8-e1b205761428';
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ProductCatalogs',
'action' => 'view',
$id,
];
$this->get($url);
$this->assertResponseCode(302);
$this->assertRedirectContains('login');
}
/**
* Test view method
*
* Tests the view action with a logged in user
*
* @uses ProductCatalogsController::view()
* @throws Exception
*
* @return void
*/
public function testViewGetLoggedIn(): void
{
$id = '115153f3-2f59-4234-8ff8-e1b205761428';
$this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ProductCatalogs',
'action' => 'view',
$id,
];
$this->get($url);
$this->assertResponseCode(200);
}
/**
* Test add method
*
* Tests the add action with an unauthenticated user (not logged in)
*
* @uses ProductCatalogsController::add()
* @throws Exception
*
* @return void
*/
public function testAddGetUnauthenticated(): void
{
$cntBefore = $this->ProductCatalogs->find()->count();
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ProductCatalogs',
'action' => 'add',
];
$this->get($url);
$this->assertResponseCode(302);
$this->assertRedirectContains('login');
$cntAfter = $this->ProductCatalogs->find()->count();
$this->assertEquals($cntBefore, $cntAfter);
}
/**
* Test add method
*
* Tests the add action with a logged in user
*
* @uses ProductCatalogsController::add()
* @throws Exception
*
* @return void
*/
public function testAddGetLoggedIn(): void
{
$cntBefore = $this->ProductCatalogs->find()->count();
$this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ProductCatalogs',
'action' => 'add',
];
$this->get($url);
$this->assertResponseCode(200);
$cntAfter = $this->ProductCatalogs->find()->count();
$this->assertEquals($cntBefore, $cntAfter);
}
/**
* Test add method
*
* Tests a POST request to the add action with a logged in user
*
* @uses ProductCatalogsController::add()
* @throws Exception
*
* @return void
*/
public function testAddPostLoggedInSuccess(): void
{
$cntBefore = $this->ProductCatalogs->find()->count();
$this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ProductCatalogs',
'action' => 'add',
];
$data = [
'name' => 'new catalog',
'catalog_description' => 'description',
'enabled' => true,
];
$this->post($url, $data);
$this->assertResponseCode(302);
$this->assertRedirectContains('product-catalogs');
$cntAfter = $this->ProductCatalogs->find()->count();
$this->assertEquals($cntBefore + 1, $cntAfter);
}
/**
* Test add method
*
* Tests a POST request to the add action with a logged in user
*
* @uses ProductCatalogsController::add()
* @throws Exception
*
* @return void
*/
public function testAddPostLoggedInFailure(): void
{
$cntBefore = $this->ProductCatalogs->find()->count();
$this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ProductCatalogs',
'action' => 'add',
];
$data = [
'name' => '',
'catalog_description' => '',
'enabled' => '',
];
$this->post($url, $data);
$this->assertResponseCode(200);
$cntAfter = $this->ProductCatalogs->find()->count();
$this->assertEquals($cntBefore, $cntAfter);
}
/**
* Test edit method
*
* Tests the edit action with an unauthenticated user (not logged in)
*
* @uses ProductCatalogsController::edit()
* @throws Exception
*
* @return void
*/
public function testEditGetUnauthenticated(): void
{
$id = '115153f3-2f59-4234-8ff8-e1b205761428';
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ProductCatalogs',
'action' => 'edit',
$id,
];
$this->get($url);
$this->assertResponseCode(302);
$this->assertRedirectContains('login');
}
/**
* Test edit method
*
* Tests the edit action with a logged in user
*
* @uses ProductCatalogsController::edit()
* @throws Exception
*
* @return void
*/
public function testEditGetLoggedIn(): void
{
$this->loginUserByRole('admin');
$id = '115153f3-2f59-4234-8ff8-e1b205761428';
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ProductCatalogs',
'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
*
* @uses ProductCatalogsController::edit()
* @throws Exception
*
* @return void
*/
public function testEditPutLoggedInSuccess(): void
{
$this->loginUserByRole('admin');
$id = '115153f3-2f59-4234-8ff8-e1b205761428';
// $before = $this->ProductCatalogs->get($id);
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ProductCatalogs',
'action' => 'edit',
$id,
];
$data = [
// test new data here
'name' => 'edited name',
'catalog_description' => 'new catalog description',
'enabled' => true,
];
$this->put($url, $data);
$this->assertResponseCode(302);
$this->assertRedirectContains('product-catalogs');
$after = $this->ProductCatalogs->get($id);
$this->assertEquals($data['name'], $after->name);
$this->assertEquals($data['catalog_description'], $after->catalog_description);
// assert saved properly below
}
/**
* Test edit method
*
* Tests a PUT request to the edit action with a logged in user
*
* @uses ProductCatalogsController::edit()
* @throws Exception
*
* @return void
*/
public function testEditPutLoggedInFailure(): void
{
$this->loginUserByRole('admin');
$id = '115153f3-2f59-4234-8ff8-e1b205761428';
$before = $this->ProductCatalogs->get($id);
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ProductCatalogs',
'action' => 'edit',
$id,
];
$data = [
'name' => '',
'catalog_description' => 'edited description',
'enabled' => '',
];
$this->put($url, $data);
$this->assertResponseCode(200);
$after = $this->ProductCatalogs->get($id);
$this->assertEquals($before->name, $after->name);
$this->assertEquals($before->catalog_description, $after->catalog_description);
// assert save failed below
}
/**
* Test delete method
*
* Tests the delete action with an unauthenticated user (not logged in)
*
* @uses ProductCatalogsController::delete()
* @throws Exception
*
* @return void
*/
public function testDeleteUnauthenticated(): void
{
$cntBefore = $this->ProductCatalogs->find()->count();
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ProductCatalogs',
'action' => 'delete',
1,
];
$this->delete($url);
$this->assertResponseCode(302);
$this->assertRedirectContains('login');
$cntAfter = $this->ProductCatalogs->find()->count();
$this->assertEquals($cntBefore, $cntAfter);
}
/**
* Test delete method
*
* Tests the delete action with a logged in user
*
* @uses ProductCatalogsController::delete()
* @throws Exception
*
* @return void
*/
public function testDeleteLoggedIn(): void
{
$cntBefore = $this->ProductCatalogs->find()->count();
$this->loginUserByRole('admin');
$id = '115153f3-2f59-4234-8ff8-e1b205761428';
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ProductCatalogs',
'action' => 'delete',
$id,
];
$this->delete($url);
$this->assertResponseCode(302);
$this->assertRedirectContains('product-catalogs');
$cntAfter = $this->ProductCatalogs->find()->count();
$this->assertEquals($cntBefore - 1, $cntAfter);
}
}

View File

@@ -0,0 +1,455 @@
<?php
declare(strict_types=1);
namespace CakeProducts\Test\TestCase\Controller;
use Cake\ORM\Table;
use Cake\TestSuite\IntegrationTestTrait;
use Cake\TestSuite\TestCase;
use CakeProducts\Controller\ProductCategoriesController;
use CakeProducts\Model\Table\ProductCategoriesTable;
use PHPUnit\Exception;
/**
* CakeProducts\Controller\ProductCategoriesController Test Case
*
* @uses ProductCategoriesController
*/
class ProductCategoriesControllerTest extends BaseControllerTest
{
/**
* Test subject table
*
* @var ProductCategoriesTable|Table
*/
protected $ProductCategories;
/**
* Fixtures
*
* @var array<string>
*/
protected array $fixtures = [
'plugin.CakeProducts.ProductCatalogs',
'plugin.CakeProducts.ProductCategories',
];
/**
* setUp method
*
* @return void
*/
protected function setUp(): void
{
parent::setUp();
$this->enableCsrfToken();
$this->enableSecurityToken();
$this->ProductCategories = $this->getTableLocator()->get('ProductCategories');
}
/**
* tearDown method
*
* @return void
*/
protected function tearDown(): void
{
unset($this->ProductCategories);
parent::tearDown();
}
/**
* Test index method
*
* Tests the index action with an unauthenticated user (not logged in)
*
* @return void
* @throws Exception
*
* @uses ProductCategoriesController::index
*/
public function testIndexGetUnauthenticated(): void
{
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ProductCategories',
'action' => 'index',
];
$this->get($url);
$this->assertResponseCode(302);
$this->assertRedirectContains('login');
}
/**
* Test index method
*
* Tests the index action with a logged in user
*
* @return void
* @throws Exception
*
* @uses ProductCategoriesController::index
*/
public function testIndexGetLoggedIn(): void
{
$this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ProductCategories',
'action' => 'index',
];
$this->get($url);
$this->assertResponseCode(200);
}
/**
* Test view method
*
* Tests the view action with an unauthenticated user (not logged in)
*
* @return void
* @throws Exception
*
* @uses ProductCategoriesController::view
*/
public function testViewGetUnauthenticated(): void
{
$id = 1;
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ProductCategories',
'action' => 'view',
$id,
];
$this->get($url);
$this->assertResponseCode(302);
$this->assertRedirectContains('login');
}
/**
* Test view method
*
* Tests the view action with a logged in user
*
* @return void
* @throws Exception
*
* @uses ProductCategoriesController::view
*/
public function testViewGetLoggedIn(): void
{
$id = 1;
$this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ProductCategories',
'action' => 'view',
$id,
];
$this->get($url);
$this->assertResponseCode(200);
}
/**
* Test add method
*
* Tests the add action with an unauthenticated user (not logged in)
*
* @return void
* @throws Exception
*
* @uses ProductCategoriesController::add
*/
public function testAddGetUnauthenticated(): void
{
$cntBefore = $this->ProductCategories->find()->count();
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ProductCategories',
'action' => 'add',
];
$this->get($url);
$this->assertResponseCode(302);
$this->assertRedirectContains('login');
$cntAfter = $this->ProductCategories->find()->count();
$this->assertEquals($cntBefore, $cntAfter);
}
/**
* Test add method
*
* Tests the add action with a logged in user
*
* @return void
* @throws Exception
*
* @uses ProductCategoriesController::add
*/
public function testAddGetLoggedIn(): void
{
$cntBefore = $this->ProductCategories->find()->count();
$this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ProductCategories',
'action' => 'add',
];
$this->get($url);
$this->assertResponseCode(200);
$cntAfter = $this->ProductCategories->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 ProductCategoriesController::add
*/
public function testAddPostLoggedInSuccess(): void
{
$cntBefore = $this->ProductCategories->find()->count();
$this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ProductCategories',
'action' => 'add',
];
$data = [
'name' => 'Electrical Plugs',
'product_catalog_id' => '115153f3-2f59-4234-8ff8-e1b205761428',
'category_description' => 'electrical',
'parent_id' => 3,
'enabled' => true,
];
$this->post($url, $data);
$this->assertResponseCode(302);
$this->assertRedirectContains('product-categories');
$cntAfter = $this->ProductCategories->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 ProductCategoriesController::add
*/
public function testAddPostLoggedInFailure(): void
{
$cntBefore = $this->ProductCategories->find()->count();
$this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ProductCategories',
'action' => 'add',
];
$data = [
'name' => '',
'product_catalog_id' => '',
'category_description' => 'electrical',
'parent_id' => '',
'enabled' => true,
];
$this->post($url, $data);
$this->assertResponseCode(200);
$cntAfter = $this->ProductCategories->find()->count();
$this->assertEquals($cntBefore, $cntAfter);
}
/**
* Test edit method
*
* Tests the edit action with an unauthenticated user (not logged in)
*
* @return void
* @throws Exception
*
* @uses ProductCategoriesController::edit
*/
public function testEditGetUnauthenticated(): void
{
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ProductCategories',
'action' => 'edit',
1,
];
$this->get($url);
$this->assertResponseCode(302);
$this->assertRedirectContains('login');
}
/**
* Test edit method
*
* Tests the edit action with a logged in user
*
* @return void
* @throws Exception
*
* @uses ProductCategoriesController::edit
*/
public function testEditGetLoggedIn(): void
{
$this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ProductCategories',
'action' => 'edit',
1,
];
$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 ProductCategoriesController::edit
*/
public function testEditPutLoggedInSuccess(): void
{
$this->loginUserByRole('admin');
$id = 1;
$before = $this->ProductCategories->get($id);
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ProductCategories',
'action' => 'edit',
$id,
];
$data = [
// test new data here
'name' => 'Electrical v2',
'product_catalog_id' => '115153f3-2f59-4234-8ff8-e1b205761428',
'category_description' => 'electrical v2',
'parent_id' => '',
'enabled' => true,
];
$this->put($url, $data);
$this->assertResponseCode(302);
$this->assertRedirectContains('product-categories');
$after = $this->ProductCategories->get($id);
$this->assertEquals($data['name'], $after->name);
$this->assertEquals($data['product_catalog_id'], $after->product_catalog_id);
$this->assertEquals($data['category_description'], $after->category_description);
$this->assertNull($after->parent_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 ProductCategoriesController::edit
*/
public function testEditPutLoggedInFailure(): void
{
$this->loginUserByRole('admin');
$id = 1;
$before = $this->ProductCategories->get($id);
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ProductCategories',
'action' => 'edit',
$id,
];
$data = [
'name' => '',
'product_catalog_id' => '',
'category_description' => 'electrical',
'parent_id' => '',
'enabled' => true,
];
$this->put($url, $data);
$this->assertResponseCode(200);
$after = $this->ProductCategories->get($id);
// assert save failed below
}
/**
* Test delete method
*
* Tests the delete action with an unauthenticated user (not logged in)
*
* @return void
* @throws Exception
*
* @uses ProductCategoriesController::delete
*/
public function testDeleteUnauthenticated(): void
{
$cntBefore = $this->ProductCategories->find()->count();
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ProductCategories',
'action' => 'delete',
1,
];
$this->delete($url);
$this->assertResponseCode(302);
$this->assertRedirectContains('login');
$cntAfter = $this->ProductCategories->find()->count();
$this->assertEquals($cntBefore, $cntAfter);
}
/**
* Test delete method
*
* Tests the delete action with a logged in user
*
* @return void
*@throws Exception
*
* @uses ProductCategoriesController::delete
*/
public function testDeleteLoggedIn(): void
{
$cntBefore = $this->ProductCategories->find()->count();
$this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ProductCategories',
'action' => 'delete',
1,
];
$this->delete($url);
$this->assertResponseCode(302);
$this->assertRedirectContains('product-categories');
$cntAfter = $this->ProductCategories->find()->count();
$this->assertEquals($cntBefore - 1, $cntAfter);
}
}

View File

@@ -0,0 +1,198 @@
<?php
declare(strict_types=1);
namespace CakeProducts\Test\TestCase\Controller;
use Cake\ORM\Table;
use CakeProducts\Controller\ProductCategoryAttributeOptionsController;
use CakeProducts\Model\Table\ProductCategoryAttributeOptionsTable;
use PHPUnit\Exception;
/**
* CakeProducts\Controller\ProductCategoryAttributeOptionsController Test Case
*
* @uses \CakeProducts\Controller\ProductCategoryAttributeOptionsController
*/
class ProductCategoryAttributeOptionsControllerTest extends BaseControllerTest
{
/**
* Test subject
*
* @var ProductCategoryAttributeOptionsTable|Table
*/
protected $ProductCategoryAttributeOptions;
/**
* Fixtures
*
* @var array<string>
*/
protected array $fixtures = [
'plugin.CakeProducts.ProductCategoryAttributeOptions',
'plugin.CakeProducts.ProductCategoryAttributes',
];
/**
* setUp method
*
* @return void
*/
protected function setUp(): void
{
parent::setUp();
$this->enableCsrfToken();
$this->enableSecurityToken();
$config = $this->getTableLocator()->exists('ProductCategoryAttributeOptions') ? [] : ['className' => ProductCategoryAttributeOptionsTable::class];
$this->ProductCategoryAttributeOptions = $this->getTableLocator()->get('ProductCategoryAttributeOptions', $config);
}
/**
* tearDown method
*
* @return void
*/
protected function tearDown(): void
{
unset($this->ProductCategoryAttributeOptions);
parent::tearDown();
}
/**
* Test add method
*
* Tests the add action with an unauthenticated user (not logged in)
*
* @return void
* @throws Exception
*
* @uses CustomersContactsController::add
*/
public function testAddGetUnauthenticated(): void
{
$cntBefore = $this->ProductCategoryAttributeOptions->find()->count();
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ProductCategoryAttributeOptions',
'action' => 'add',
];
$this->get($url);
$this->assertResponseCode(302);
$this->assertRedirectContains('login');
$cntAfter = $this->ProductCategoryAttributeOptions->find()->count();
$this->assertEquals($cntBefore, $cntAfter);
}
/**
* Test add method
*
* Tests the add action with a logged in user
*
* @return void
* @throws Exception
*
* @uses CustomersContactsController::add
*/
public function testAddGetLoggedIn(): void
{
$cntBefore = $this->ProductCategoryAttributeOptions->find()->count();
$this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ProductCategoryAttributeOptions',
'action' => 'add',
];
$this->get($url);
$this->assertResponseCode(200);
$cntAfter = $this->ProductCategoryAttributeOptions->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 CustomersContactsController::add
*/
public function testAddPostLoggedInHasNoEffect(): void
{
$cntBefore = $this->ProductCategoryAttributeOptions->find()->count();
$this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ProductCategoryAttributeOptions',
'action' => 'add',
];
$data = [];
$this->post($url, $data);
$this->assertResponseCode(200);
$cntAfter = $this->ProductCategoryAttributeOptions->find()->count();
$this->assertEquals($cntBefore, $cntAfter);
}
/**
* Test delete method
*
* Tests the delete action with an unauthenticated user (not logged in)
*
* @return void
* @throws Exception
*
* @uses CustomersContactsController::delete
*/
public function testDeleteUnauthenticated(): void
{
$cntBefore = $this->ProductCategoryAttributeOptions->find()->count();
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ProductCategoryAttributeOptions',
'action' => 'delete',
'e06f1723-2456-483a-b3c4-004603e032a8',
];
$this->delete($url);
$this->assertResponseCode(302);
$this->assertRedirectContains('login');
$cntAfter = $this->ProductCategoryAttributeOptions->find()->count();
$this->assertEquals($cntBefore, $cntAfter);
}
/**
* Test delete method
*
* Tests the delete action with a logged in user
*
* @return void
*@throws Exception
*
* @uses CustomersContactsController::delete
*/
public function testDeleteLoggedIn(): void
{
$cntBefore = $this->ProductCategoryAttributeOptions->find()->count();
$this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ProductCategoryAttributeOptions',
'action' => 'delete',
'e06f1723-2456-483a-b3c4-004603e032a8',
];
$this->delete($url);
$this->assertResponseCode(302);
$this->assertRedirectContains('product-category-attributes');
$cntAfter = $this->ProductCategoryAttributeOptions->find()->count();
$this->assertEquals($cntBefore - 1, $cntAfter);
}
}

View File

@@ -0,0 +1,498 @@
<?php
declare(strict_types=1);
namespace CakeProducts\Test\TestCase\Controller;
use Cake\ORM\Table;
use Cake\TestSuite\IntegrationTestTrait;
use Cake\TestSuite\TestCase;
use CakeProducts\Controller\ProductCategoryAttributesController;
use CakeProducts\Model\Table\ProductCategoryAttributesTable;
use PHPUnit\Exception;
/**
* CakeProducts\Controller\ProductCategoryAttributesController Test Case
*
* @uses ProductCategoryAttributesController
*/
class ProductCategoryAttributesControllerTest extends BaseControllerTest
{
/**
* Test subject
*
* @var ProductCategoryAttributesTable|Table
*/
protected $ProductCategoryAttributes;
/**
* Fixtures
*
* @var array<string>
*/
protected array $fixtures = [
'plugin.CakeProducts.ProductCategoryAttributes',
'plugin.CakeProducts.ProductCategories',
];
/**
* setUp method
*
* @return void
*/
protected function setUp(): void
{
parent::setUp();
$this->enableCsrfToken();
$this->enableSecurityToken();
$config = $this->getTableLocator()->exists('ProductCategoryAttributes') ? [] : ['className' => ProductCategoryAttributesTable::class];
$this->ProductCategoryAttributes = $this->getTableLocator()->get('ProductCategoryAttributes', $config);
}
/**
* tearDown method
*
* @return void
*/
protected function tearDown(): void
{
unset($this->ProductCategoryAttributes);
parent::tearDown();
}
/**
* Test index method
*
* Tests the index action with an unauthenticated user (not logged in)
*
* @return void
* @throws Exception
*
* @uses ProductCategoryAttributesController::index
*/
public function testIndexGetUnauthenticated(): void
{
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ProductCategoryAttributes',
'action' => 'index',
];
$this->get($url);
$this->assertResponseCode(302);
$this->assertRedirectContains('login');
}
/**
* Test index method
*
* Tests the index action with a logged in user
*
* @return void
* @throws Exception
*
* @uses ProductCategoryAttributesController::index
*/
public function testIndexGetLoggedIn(): void
{
$this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ProductCategoryAttributes',
'action' => 'index',
];
$this->get($url);
$this->assertResponseCode(200);
}
/**
* Test view method
*
* Tests the view action with an unauthenticated user (not logged in)
*
* @return void
* @throws Exception
*
* @uses ProductCategoryAttributesController::view
*/
public function testViewGetUnauthenticated(): void
{
$id = '37078cf0-0130-4b93-bb7e-abe7d665ed2c';
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ProductCategoryAttributes',
'action' => 'view',
$id,
];
$this->get($url);
$this->assertResponseCode(302);
$this->assertRedirectContains('login');
}
/**
* Test view method
*
* Tests the view action with a logged in user
*
* @return void
* @throws Exception
*
* @uses ProductCategoryAttributesController::view
*/
public function testViewGetLoggedIn(): void
{
$id = '37078cf0-0130-4b93-bb7e-abe7d665ed2c';
$this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ProductCategoryAttributes',
'action' => 'view',
$id,
];
$this->get($url);
$this->assertResponseCode(200);
}
/**
* Test add method
*
* Tests the add action with an unauthenticated user (not logged in)
*
* @return void
* @throws Exception
*
* @uses ProductCategoryAttributesController::add
*/
public function testAddGetUnauthenticated(): void
{
$cntBefore = $this->ProductCategoryAttributes->find()->count();
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ProductCategoryAttributes',
'action' => 'add',
];
$this->get($url);
$this->assertResponseCode(302);
$this->assertRedirectContains('login');
$cntAfter = $this->ProductCategoryAttributes->find()->count();
$this->assertEquals($cntBefore, $cntAfter);
}
/**
* Test add method
*
* Tests the add action with a logged in user
*
* @return void
* @throws Exception
*
* @uses ProductCategoryAttributesController::add
*/
public function testAddGetLoggedIn(): void
{
$cntBefore = $this->ProductCategoryAttributes->find()->count();
$this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ProductCategoryAttributes',
'action' => 'add',
];
$this->get($url);
$this->assertResponseCode(200);
$cntAfter = $this->ProductCategoryAttributes->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 ProductCategoryAttributesController::add
*/
public function testAddPostLoggedInSuccess(): void
{
$cntBefore = $this->ProductCategoryAttributes->find()->count();
$this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ProductCategoryAttributes',
'action' => 'add',
];
$data = [
'name' => 'Size',
'product_category_id' => 'db4b4273-eddc-46d4-93c8-45cf7c6e058e',
'attribute_type_id' => 2,
'enabled' => true,
];
$this->post($url, $data);
$this->assertResponseCode(302);
$this->assertRedirectContains('product-category-attributes');
$cntAfter = $this->ProductCategoryAttributes->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 ProductCategoryAttributesController::add
*/
public function testAddPostLoggedInSuccessConstrainedWithOptions(): void
{
$cntBefore = $this->ProductCategoryAttributes->find()->count();
$cntOptionsBefore = $this->ProductCategoryAttributes->ProductCategoryAttributeOptions->find()->count();
$this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ProductCategoryAttributes',
'action' => 'add',
];
$data = [
'name' => 'Size',
'product_category_id' => 'db4b4273-eddc-46d4-93c8-45cf7c6e058e',
'attribute_type_id' => 1,
'enabled' => true,
'product_category_attribute_options' => [
[
'attribute_value' => 'XL',
'attribute_label' => 'XL',
'enabled' => true,
],
[
'attribute_value' => 'L',
'attribute_label' => 'L',
'enabled' => true,
]
],
];
$this->post($url, $data);
$this->assertResponseCode(302);
$this->assertRedirectContains('product-category-attributes');
$cntAfter = $this->ProductCategoryAttributes->find()->count();
$cntOptionsAfter = $this->ProductCategoryAttributes->ProductCategoryAttributeOptions->find()->count();
$this->assertEquals($cntBefore + 1, $cntAfter);
$this->assertEquals($cntOptionsBefore + 2, $cntOptionsAfter);
}
/**
* Test add method
*
* Tests a POST request to the add action with a logged in user
*
* @return void
* @throws Exception
*
* @uses ProductCategoryAttributesController::add
*/
public function testAddPostLoggedInFailure(): void
{
$cntBefore = $this->ProductCategoryAttributes->find()->count();
$this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ProductCategoryAttributes',
'action' => 'add',
];
$data = [
'name' => '',
'product_category_id' => 1,
'attribute_type_id' => 1,
'enabled' => true,
];
$this->post($url, $data);
$this->assertResponseCode(200);
$cntAfter = $this->ProductCategoryAttributes->find()->count();
$this->assertEquals($cntBefore, $cntAfter);
}
/**
* Test edit method
*
* Tests the edit action with an unauthenticated user (not logged in)
*
* @return void
* @throws Exception
*
* @uses ProductCategoryAttributesController::edit
*/
public function testEditGetUnauthenticated(): void
{
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ProductCategoryAttributes',
'action' => 'edit',
'37078cf0-0130-4b93-bb7e-abe7d665ed2c',
];
$this->get($url);
$this->assertResponseCode(302);
$this->assertRedirectContains('login');
}
/**
* Test edit method
*
* Tests the edit action with a logged in user
*
* @return void
* @throws Exception
*
* @uses ProductCategoryAttributesController::edit
*/
public function testEditGetLoggedIn(): void
{
$this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ProductCategoryAttributes',
'action' => 'edit',
'37078cf0-0130-4b93-bb7e-abe7d665ed2c',
];
$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 ProductCategoryAttributesController::edit
*/
public function testEditPutLoggedInSuccess(): void
{
$this->loginUserByRole('admin');
$id = '37078cf0-0130-4b93-bb7e-abe7d665ed2c';
$before = $this->ProductCategoryAttributes->get($id);
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ProductCategoryAttributes',
'action' => 'edit',
$id,
];
$data = [
// test new data here
'name' => 'Color',
'product_category_id' => 'db4b4273-eddc-46d4-93c8-45cf7c6e058e',
'attribute_type_id' => 1,
'enabled' => true,
];
$this->put($url, $data);
$this->assertResponseCode(302);
$this->assertRedirectContains('product-category-attributes');
$after = $this->ProductCategoryAttributes->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 ProductCategoryAttributesController::edit
*/
public function testEditPutLoggedInFailure(): void
{
$this->loginUserByRole('admin');
$id = '37078cf0-0130-4b93-bb7e-abe7d665ed2c';
$before = $this->ProductCategoryAttributes->get($id);
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ProductCategoryAttributes',
'action' => 'edit',
$id,
];
$data = [
'name' => '',
'product_category_id' => 1,
'attribute_type_id' => 1,
'enabled' => true,
];
$this->put($url, $data);
$this->assertResponseCode(200);
$after = $this->ProductCategoryAttributes->get($id);
// assert save failed below
}
/**
* Test delete method
*
* Tests the delete action with an unauthenticated user (not logged in)
*
* @return void
* @throws Exception
*
* @uses ProductCategoryAttributesController::delete
*/
public function testDeleteUnauthenticated(): void
{
$cntBefore = $this->ProductCategoryAttributes->find()->count();
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ProductCategoryAttributes',
'action' => 'delete',
'37078cf0-0130-4b93-bb7e-abe7d665ed2c',
];
$this->delete($url);
$this->assertResponseCode(302);
$this->assertRedirectContains('login');
$cntAfter = $this->ProductCategoryAttributes->find()->count();
$this->assertEquals($cntBefore, $cntAfter);
}
/**
* Test delete method
*
* Tests the delete action with a logged in user
*
* @return void
*@throws Exception
*
* @uses ProductCategoryAttributesController::delete
*/
public function testDeleteLoggedIn(): void
{
$cntBefore = $this->ProductCategoryAttributes->find()->count();
$this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeProducts',
'controller' => 'ProductCategoryAttributes',
'action' => 'delete',
'37078cf0-0130-4b93-bb7e-abe7d665ed2c',
];
$this->delete($url);
$this->assertResponseCode(302);
$this->assertRedirectContains('product-category-attributes');
$cntAfter = $this->ProductCategoryAttributes->find()->count();
$this->assertEquals($cntBefore - 1, $cntAfter);
}
}

View File

@@ -0,0 +1,452 @@
<?php
declare(strict_types=1);
namespace CakeProducts\Test\TestCase\Controller;
use Cake\ORM\Table;
use Cake\TestSuite\IntegrationTestTrait;
use Cake\TestSuite\TestCase;
use CakeProducts\Controller\ProductsController;
use CakeProducts\Model\Table\ProductCatalogsTable;
use CakeProducts\Model\Table\ProductsTable;
use PHPUnit\Exception;
/**
* CakeProducts\Controller\ProductsController Test Case
*
* @uses ProductsController
*/
class ProductsControllerTest extends BaseControllerTest
{
/**
* Test subject table
*
* @var ProductsTable|Table
*/
protected $Products;
/**
* Fixtures
*
* @var array<string>
*/
protected array $fixtures = [
'plugin.CakeProducts.Products',
'plugin.CakeProducts.ProductCategories',
// 'plugin.CakeProducts.ProductCatalogs',
];
/**
* setUp method
*
* @return void
*/
protected function setUp(): void
{
parent::setUp();
$this->enableCsrfToken();
$this->enableSecurityToken();
$config = $this->getTableLocator()->exists('Products') ? [] : ['className' => ProductsTable::class];
$this->Products = $this->getTableLocator()->get('Products', $config);
}
/**
* tearDown method
*
* @return void
*/
protected function tearDown(): void
{
unset($this->Products);
parent::tearDown();
}
/**
* Test index method
*
* Tests the index action with an unauthenticated user (not logged in)
*
* @return void
* @throws Exception
*
* @uses ProductsController::index
*/
public function testIndexGetUnauthenticated(): void
{
$url = [
'plugin' => 'CakeProducts',
'controller' => 'Products',
'action' => 'index',
];
$this->get($url);
$this->assertResponseCode(302);
$this->assertRedirectContains('login');
}
/**
* Test index method
*
* Tests the index action with a logged in user
*
* @return void
* @throws Exception
*
* @uses ProductsController::index
*/
public function testIndexGetLoggedIn(): void
{
$this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeProducts',
'controller' => 'Products',
'action' => 'index',
];
$this->get($url);
$this->assertResponseCode(200);
}
/**
* Test view method
*
* Tests the view action with an unauthenticated user (not logged in)
*
* @return void
* @throws Exception
*
* @uses ProductsController::view
*/
public function testViewGetUnauthenticated(): void
{
$id = 'cfc98a9a-29b2-44c8-b587-8156adc05317';
$url = [
'plugin' => 'CakeProducts',
'controller' => 'Products',
'action' => 'view',
$id,
];
$this->get($url);
$this->assertResponseCode(302);
$this->assertRedirectContains('login');
}
/**
* Test view method
*
* Tests the view action with a logged in user
*
* @return void
* @throws Exception
*
* @uses ProductsController::view
*/
public function testViewGetLoggedIn(): void
{
$id = 'cfc98a9a-29b2-44c8-b587-8156adc05317';
$this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeProducts',
'controller' => 'Products',
'action' => 'view',
$id,
];
$this->get($url);
$this->assertResponseCode(200);
}
/**
* Test add method
*
* Tests the add action with an unauthenticated user (not logged in)
*
* @return void
* @throws Exception
*
* @uses ProductsController::add
*/
public function testAddGetUnauthenticated(): void
{
$cntBefore = $this->Products->find()->count();
$url = [
'plugin' => 'CakeProducts',
'controller' => 'Products',
'action' => 'add',
];
$this->get($url);
$this->assertResponseCode(302);
$this->assertRedirectContains('login');
$cntAfter = $this->Products->find()->count();
$this->assertEquals($cntBefore, $cntAfter);
}
/**
* Test add method
*
* Tests the add action with a logged in user
*
* @return void
* @throws Exception
*
* @uses ProductsController::add
*/
public function testAddGetLoggedIn(): void
{
$cntBefore = $this->Products->find()->count();
$this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeProducts',
'controller' => 'Products',
'action' => 'add',
];
$this->get($url);
$this->assertResponseCode(200);
$cntAfter = $this->Products->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 ProductsController::add
*/
public function testAddPostLoggedInSuccess(): void
{
$cntBefore = $this->Products->find()->count();
$this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeProducts',
'controller' => 'Products',
'action' => 'add',
];
$data = [
'product_catalog_id' => '115153f3-2f59-4234-8ff8-e1b205761428',
'product_category_id' => '6d223283-361b-4f9f-a7f1-c97aa0ca4c23',
'name' => '16AWG WIRE RED',
'product_type_id' => 1,
];
$this->post($url, $data);
$this->assertResponseCode(302);
$this->assertRedirectContains('products');
$cntAfter = $this->Products->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 ProductsController::add
*/
public function testAddPostLoggedInFailure(): void
{
$cntBefore = $this->Products->find()->count();
$this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeProducts',
'controller' => 'Products',
'action' => 'add',
];
$data = [
'product_catalog_id' => '',
'product_category_id' => '',
'name' => '',
'product_type_id' => 1,
];
$this->post($url, $data);
$this->assertResponseCode(200);
$cntAfter = $this->Products->find()->count();
$this->assertEquals($cntBefore, $cntAfter);
}
/**
* Test edit method
*
* Tests the edit action with an unauthenticated user (not logged in)
*
* @return void
* @throws Exception
*
* @uses ProductsController::edit
*/
public function testEditGetUnauthenticated(): void
{
$url = [
'plugin' => 'CakeProducts',
'controller' => 'Products',
'action' => 'edit',
'cfc98a9a-29b2-44c8-b587-8156adc05317',
];
$this->get($url);
$this->assertResponseCode(302);
$this->assertRedirectContains('login');
}
/**
* Test edit method
*
* Tests the edit action with a logged in user
*
* @return void
* @throws Exception
*
* @uses ProductsController::edit
*/
public function testEditGetLoggedIn(): void
{
$this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeProducts',
'controller' => 'Products',
'action' => 'edit',
'cfc98a9a-29b2-44c8-b587-8156adc05317',
];
$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 ProductsController::edit
*/
public function testEditPutLoggedInSuccess(): void
{
$this->loginUserByRole('admin');
$id = 'cfc98a9a-29b2-44c8-b587-8156adc05317';
$before = $this->Products->get($id);
$url = [
'plugin' => 'CakeProducts',
'controller' => 'Products',
'action' => 'edit',
$id,
];
$data = [
// test new data here
'product_catalog_id' => '115153f3-2f59-4234-8ff8-e1b205761428',
'product_category_id' => '6d223283-361b-4f9f-a7f1-c97aa0ca4c23',
'name' => 'edited product name',
'product_type_id' => 1,
];
$this->put($url, $data);
$this->assertResponseCode(302);
$this->assertRedirectContains('products');
$after = $this->Products->get($id);
$this->assertEquals($data['name'], $after->name);
// 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 ProductsController::edit
*/
public function testEditPutLoggedInFailure(): void
{
$this->loginUserByRole('admin');
$id = 'cfc98a9a-29b2-44c8-b587-8156adc05317';
$before = $this->Products->get($id);
$url = [
'plugin' => 'CakeProducts',
'controller' => 'Products',
'action' => 'edit',
$id,
];
$data = [
'product_catalog_id' => '',
'product_category_id' => '',
'name' => 'edited name not gonna take',
'product_type_id' => 1,
];
$this->put($url, $data);
$this->assertResponseCode(200);
$after = $this->Products->get($id);
$this->assertEquals($before->name, $after->name);
$this->assertEquals($before->product_category_id, $after->product_category_id);
// assert save failed below
}
/**
* Test delete method
*
* Tests the delete action with an unauthenticated user (not logged in)
*
* @return void
* @throws Exception
*
* @uses ProductsController::delete
*/
public function testDeleteUnauthenticated(): void
{
$cntBefore = $this->Products->find()->count();
$url = [
'plugin' => 'CakeProducts',
'controller' => 'Products',
'action' => 'delete',
'cfc98a9a-29b2-44c8-b587-8156adc05317',
];
$this->delete($url);
$this->assertResponseCode(302);
$this->assertRedirectContains('login');
$cntAfter = $this->Products->find()->count();
$this->assertEquals($cntBefore, $cntAfter);
}
/**
* Test delete method
*
* Tests the delete action with a logged in user
*
* @return void
*@throws Exception
*
* @uses ProductsController::delete
*/
public function testDeleteLoggedIn(): void
{
$cntBefore = $this->Products->find()->count();
$this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeProducts',
'controller' => 'Products',
'action' => 'delete',
'cfc98a9a-29b2-44c8-b587-8156adc05317',
];
$this->delete($url);
$this->assertResponseCode(302);
$this->assertRedirectContains('products');
$cntAfter = $this->Products->find()->count();
$this->assertEquals($cntBefore - 1, $cntAfter);
}
}

View File

@@ -0,0 +1,107 @@
<?php
declare(strict_types=1);
namespace CakeProducts\Test\TestCase\Model\Table;
use Cake\TestSuite\TestCase;
use CakeProducts\Model\Table\ExternalProductCatalogsTable;
/**
* CakeProducts\Model\Table\ExternalProductCatalogsTable Test Case
*/
class ExternalProductCatalogsTableTest extends TestCase
{
/**
* Test subject
*
* @var \CakeProducts\Model\Table\ExternalProductCatalogsTable
*/
protected $ExternalProductCatalogs;
/**
* Fixtures
*
* @var array<string>
*/
protected array $fixtures = [
'plugin.CakeProducts.ExternalProductCatalogs',
'plugin.CakeProducts.ProductCatalogs',
];
/**
* setUp method
*
* @return void
*/
protected function setUp(): void
{
parent::setUp();
$config = $this->getTableLocator()->exists('ExternalProductCatalogs') ? [] : ['className' => ExternalProductCatalogsTable::class];
$this->ExternalProductCatalogs = $this->getTableLocator()->get('ExternalProductCatalogs', $config);
}
/**
* tearDown method
*
* @return void
*/
protected function tearDown(): void
{
unset($this->ExternalProductCatalogs);
parent::tearDown();
}
/**
* TestInitialize method
*
* @return void
* @uses \CakeProducts\Model\Table\ExternalProductCatalogsTable::initialize()
*/
public function testInitialize(): void
{
// verify all associations loaded
$expectedAssociations = [
'ProductCatalogs',
];
$associations = $this->ExternalProductCatalogs->associations();
$this->assertCount(count($expectedAssociations), $associations);
foreach ($expectedAssociations as $expectedAssociation) {
$this->assertTrue($this->ExternalProductCatalogs->hasAssociation($expectedAssociation));
}
// verify all behaviors loaded
$expectedBehaviors = [
'Timestamp',
];
$behaviors = $this->ExternalProductCatalogs->behaviors();
$this->assertCount(count($expectedBehaviors), $behaviors);
foreach ($expectedBehaviors as $expectedBehavior) {
$this->assertTrue($this->ExternalProductCatalogs->hasBehavior($expectedBehavior));
}
}
/**
* Test validationDefault method
*
* @return void
* @uses \CakeProducts\Model\Table\ExternalProductCatalogsTable::validationDefault()
*/
public function testValidationDefault(): void
{
$this->markTestIncomplete('Not implemented yet.');
}
/**
* Test buildRules method
*
* @return void
* @uses \CakeProducts\Model\Table\ExternalProductCatalogsTable::buildRules()
*/
public function testBuildRules(): void
{
$this->markTestIncomplete('Not implemented yet.');
}
}

View File

@@ -0,0 +1,96 @@
<?php
declare(strict_types=1);
namespace CakeProducts\Test\TestCase\Model\Table;
use Cake\ORM\Table;
use Cake\TestSuite\TestCase;
use CakeProducts\Model\Table\ProductCatalogsTable;
/**
* CakeProducts\Model\Table\ProductCatalogsTable Test Case
*/
class ProductCatalogsTableTest extends TestCase
{
/**
* Test subject
*
* @var ProductCatalogsTable|Table
*/
protected $ProductCatalogs;
/**
* Fixtures
*
* @var array<string>
*/
protected array $fixtures = [
'plugin.CakeProducts.ProductCatalogs',
'plugin.CakeProducts.ProductCategories',
];
/**
* setUp method
*
* @return void
*/
protected function setUp(): void
{
parent::setUp();
$config = $this->getTableLocator()->exists('ProductCatalogs') ? [] : ['className' => ProductCatalogsTable::class];
$this->ProductCatalogs = $this->getTableLocator()->get('ProductCatalogs', $config);
}
/**
* tearDown method
*
* @return void
*/
protected function tearDown(): void
{
unset($this->ProductCatalogs);
parent::tearDown();
}
/**
* TestInitialize method
*
* @return void
* @uses ProductCatalogsTable::initialize
*/
public function testInitialize(): void
{
// verify all associations loaded
$expectedAssociations = [
'ProductCategories',
'ExternalProductCatalogs',
];
$associations = $this->ProductCatalogs->associations();
$this->assertCount(count($expectedAssociations), $associations);
foreach ($expectedAssociations as $expectedAssociation) {
$this->assertTrue($this->ProductCatalogs->hasAssociation($expectedAssociation));
}
// verify all behaviors loaded
$expectedBehaviors = [];
$behaviors = $this->ProductCatalogs->behaviors();
$this->assertCount(count($expectedBehaviors), $behaviors);
foreach ($expectedBehaviors as $expectedBehavior) {
$this->assertTrue($this->ProductCatalogs->hasBehavior($expectedBehavior));
}
}
/**
* Test validationDefault method
*
* @return void
* @uses ProductCatalogsTable::validationDefault
*/
public function testValidationDefault(): void
{
$this->markTestIncomplete('Not implemented yet.');
}
}

View File

@@ -0,0 +1,111 @@
<?php
declare(strict_types=1);
namespace CakeProducts\Test\TestCase\Model\Table;
use Cake\TestSuite\TestCase;
use CakeProducts\Model\Table\ProductCategoriesTable;
/**
* CakeProducts\Model\Table\ProductCategoriesTable Test Case
*/
class ProductCategoriesTableTest extends TestCase
{
/**
* Test subject
*
* @var \CakeProducts\Model\Table\ProductCategoriesTable
*/
protected $ProductCategories;
/**
* Fixtures
*
* @var array<string>
*/
protected array $fixtures = [
'plugin.CakeProducts.ProductCategories',
'plugin.CakeProducts.ProductCatalogs',
];
/**
* setUp method
*
* @return void
*/
protected function setUp(): void
{
parent::setUp();
$config = $this->getTableLocator()->exists('ProductCategories') ? [] : ['className' => ProductCategoriesTable::class];
$this->ProductCategories = $this->getTableLocator()->get('ProductCategories', $config);
}
/**
* tearDown method
*
* @return void
*/
protected function tearDown(): void
{
unset($this->ProductCategories);
parent::tearDown();
}
/**
* TestInitialize method
*
* @return void
* @uses \CakeProducts\Model\Table\ProductCategoriesTable::initialize()
*/
public function testInitialize(): void
{
// verify all associations loaded
$expectedAssociations = [
'ProductCatalogs',
'ParentProductCategories',
'ChildProductCategories',
// 'Products',
// 'ProductCategoryAttributes',
];
$associations = $this->ProductCategories->associations();
$this->assertCount(count($expectedAssociations), $associations);
foreach ($expectedAssociations as $expectedAssociation) {
$this->assertTrue($this->ProductCategories->hasAssociation($expectedAssociation));
}
// verify all behaviors loaded
$expectedBehaviors = [
'Tree',
];
$behaviors = $this->ProductCategories->behaviors();
$this->assertCount(count($expectedBehaviors), $behaviors);
foreach ($expectedBehaviors as $expectedBehavior) {
$this->assertTrue($this->ProductCategories->hasBehavior($expectedBehavior));
}
}
/**
* Test validationDefault method
*
* @return void
* @uses \CakeProducts\Model\Table\ProductCategoriesTable::validationDefault()
*/
public function testValidationDefault(): void
{
$this->markTestIncomplete('Not implemented yet.');
}
/**
* Test buildRules method
*
* @return void
* @uses \CakeProducts\Model\Table\ProductCategoriesTable::buildRules()
*/
public function testBuildRules(): void
{
$this->markTestIncomplete('Not implemented yet.');
}
}

View File

@@ -0,0 +1,104 @@
<?php
declare(strict_types=1);
namespace CakeProducts\Test\TestCase\Model\Table;
use Cake\TestSuite\TestCase;
use CakeProducts\Model\Table\ProductCategoryAttributeOptionsTable;
/**
* CakeProducts\Model\Table\ProductCategoryAttributeOptionsTable Test Case
*/
class ProductCategoryAttributeOptionsTableTest extends TestCase
{
/**
* Test subject
*
* @var ProductCategoryAttributeOptionsTable
*/
protected $ProductCategoryAttributeOptions;
/**
* Fixtures
*
* @var array<string>
*/
protected array $fixtures = [
'plugin.CakeProducts.ProductCategoryAttributeOptions',
];
/**
* setUp method
*
* @return void
*/
protected function setUp(): void
{
parent::setUp();
$config = $this->getTableLocator()->exists('ProductCategoryAttributeOptions') ? [] : ['className' => ProductCategoryAttributeOptionsTable::class];
$this->ProductCategoryAttributeOptions = $this->getTableLocator()->get('ProductCategoryAttributeOptions', $config);
}
/**
* tearDown method
*
* @return void
*/
protected function tearDown(): void
{
unset($this->ProductCategoryAttributeOptions);
parent::tearDown();
}
/**
* TestInitialize method
*
* @return void
* @uses ProductCategoryAttributeOptionsTable::initialize
*/
public function testInitialize(): void
{
// verify all associations loaded
$expectedAssociations = [
'ProductCategoryAttributes',
];
$associations = $this->ProductCategoryAttributeOptions->associations();
$this->assertCount(count($expectedAssociations), $associations);
foreach ($expectedAssociations as $expectedAssociation) {
$this->assertTrue($this->ProductCategoryAttributeOptions->hasAssociation($expectedAssociation));
}
// verify all behaviors loaded
$expectedBehaviors = [];
$behaviors = $this->ProductCategoryAttributeOptions->behaviors();
$this->assertCount(count($expectedBehaviors), $behaviors);
foreach ($expectedBehaviors as $expectedBehavior) {
$this->assertTrue($this->ProductCategoryAttributeOptions->hasBehavior($expectedBehavior));
}
}
/**
* Test validationDefault method
*
* @return void
* @uses ProductCategoryAttributeOptionsTable::validationDefault
*/
public function testValidationDefault(): void
{
$this->markTestIncomplete('Not implemented yet.');
}
/**
* Test buildRules method
*
* @return void
* @uses ProductCategoryAttributeOptionsTable::buildRules
*/
public function testBuildRules(): void
{
$this->markTestIncomplete('Not implemented yet.');
}
}

View File

@@ -0,0 +1,107 @@
<?php
declare(strict_types=1);
namespace CakeProducts\Test\TestCase\Model\Table;
use Cake\TestSuite\TestCase;
use CakeProducts\Model\Table\ProductCategoryAttributesTable;
/**
* CakeProducts\Model\Table\ProductCategoryAttributesTable Test Case
*/
class ProductCategoryAttributesTableTest extends TestCase
{
/**
* Test subject
*
* @var ProductCategoryAttributesTable
*/
protected $ProductCategoryAttributes;
/**
* Fixtures
*
* @var array<string>
*/
protected array $fixtures = [
'plugin.CakeProducts.ProductCategoryAttributes',
'plugin.CakeProducts.ProductCategoryAttributeOptions',
'plugin.CakeProducts.ProductCategories',
];
/**
* setUp method
*
* @return void
*/
protected function setUp(): void
{
parent::setUp();
$config = $this->getTableLocator()->exists('ProductCategoryAttributes') ? [] : ['className' => ProductCategoryAttributesTable::class];
$this->ProductCategoryAttributes = $this->getTableLocator()->get('ProductCategoryAttributes', $config);
}
/**
* tearDown method
*
* @return void
*/
protected function tearDown(): void
{
unset($this->ProductCategoryAttributes);
parent::tearDown();
}
/**
* TestInitialize method
*
* @return void
* @uses ProductCategoryAttributesTable::initialize
*/
public function testInitialize(): void
{
// verify all associations loaded
$expectedAssociations = [
'ProductCategories',
'ProductCategoryAttributeOptions',
];
$associations = $this->ProductCategoryAttributes->associations();
$this->assertCount(count($expectedAssociations), $associations);
foreach ($expectedAssociations as $expectedAssociation) {
$this->assertTrue($this->ProductCategoryAttributes->hasAssociation($expectedAssociation));
}
// verify all behaviors loaded
$expectedBehaviors = [];
$behaviors = $this->ProductCategoryAttributes->behaviors();
$this->assertCount(count($expectedBehaviors), $behaviors);
foreach ($expectedBehaviors as $expectedBehavior) {
$this->assertTrue($this->ProductCategoryAttributes->hasBehavior($expectedBehavior));
}
}
/**
* Test validationDefault method
*
* @return void
* @uses ProductCategoryAttributesTable::validationDefault
*/
public function testValidationDefault(): void
{
$this->markTestIncomplete('Not implemented yet.');
}
/**
* Test buildRules method
*
* @return void
* @uses ProductCategoryAttributesTable::buildRules
*/
public function testBuildRules(): void
{
$this->markTestIncomplete('Not implemented yet.');
}
}

View File

@@ -0,0 +1,105 @@
<?php
declare(strict_types=1);
namespace CakeProducts\Test\TestCase\Model\Table;
use Cake\TestSuite\TestCase;
use CakeProducts\Model\Table\ProductsTable;
/**
* CakeProducts\Model\Table\ProductsTable Test Case
*/
class ProductsTableTest extends TestCase
{
/**
* Test subject
*
* @var ProductsTable
*/
protected $Products;
/**
* Fixtures
*
* @var array<string>
*/
protected array $fixtures = [
'plugin.CakeProducts.Products',
'plugin.CakeProducts.ProductCategories',
];
/**
* setUp method
*
* @return void
*/
protected function setUp(): void
{
parent::setUp();
$config = $this->getTableLocator()->exists('Products') ? [] : ['className' => ProductsTable::class];
$this->Products = $this->getTableLocator()->get('Products', $config);
}
/**
* tearDown method
*
* @return void
*/
protected function tearDown(): void
{
unset($this->Products);
parent::tearDown();
}
/**
* TestInitialize method
*
* @return void
* @uses ProductsTable::initialize
*/
public function testInitialize(): void
{
// verify all associations loaded
$expectedAssociations = [
'ProductCategories',
];
$associations = $this->Products->associations();
$this->assertCount(count($expectedAssociations), $associations);
foreach ($expectedAssociations as $expectedAssociation) {
$this->assertTrue($this->Products->hasAssociation($expectedAssociation));
}
// verify all behaviors loaded
$expectedBehaviors = [];
$behaviors = $this->Products->behaviors();
$this->assertCount(count($expectedBehaviors), $behaviors);
foreach ($expectedBehaviors as $expectedBehavior) {
$this->assertTrue($this->Products->hasBehavior($expectedBehavior));
}
}
/**
* Test validationDefault method
*
* @return void
* @uses ProductsTable::validationDefault
*/
public function testValidationDefault(): void
{
$this->markTestIncomplete('Not implemented yet.');
}
/**
* Test buildRules method
*
* @return void
* @uses ProductsTable::buildRules
*/
public function testBuildRules(): void
{
$this->markTestIncomplete('Not implemented yet.');
}
}