366 lines
9.7 KiB
PHP
366 lines
9.7 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace CakeAccounting\Test\TestCase\Controller;
|
|
|
|
use Cake\ORM\Table;
|
|
use CakeAccounting\Controller\DeAccountTemplatesController;
|
|
use CakeAccounting\Model\Table\DeAccountsTable;
|
|
use CakeAccounting\Model\Table\DeAccountTemplatesTable;
|
|
use CakeAccounting\Model\Table\DeTemplatedAccountsTable;
|
|
use PHPUnit\Exception;
|
|
|
|
/**
|
|
* CakeAccounting\Controller\DeAccountTemplatesController Test Case
|
|
*
|
|
* @uses DeAccountTemplatesController
|
|
*/
|
|
class DeAccountTemplatesControllerTest extends BaseControllerTest
|
|
{
|
|
/**
|
|
* @var DeAccountTemplatesTable|Table
|
|
*/
|
|
protected DeAccountTemplatesTable|Table $DeAccountTemplates;
|
|
|
|
/**
|
|
* @var DeAccountsTable|Table
|
|
*/
|
|
protected DeAccountsTable|Table $DeAccounts;
|
|
|
|
/**
|
|
* @var DeTemplatedAccountsTable|Table
|
|
*/
|
|
protected DeTemplatedAccountsTable|Table $DeTemplatedAccounts;
|
|
|
|
/**
|
|
* Fixtures
|
|
*
|
|
* @var array<string>
|
|
*/
|
|
protected array $fixtures = [
|
|
'plugin.CakeAccounting.DeAccountTemplates',
|
|
];
|
|
|
|
/**
|
|
* setUp method
|
|
*
|
|
* @return void
|
|
*/
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
// $this->enableCsrfToken();
|
|
// $this->enableSecurityToken();
|
|
$this->disableErrorHandlerMiddleware();
|
|
|
|
$configDeAccountTemplates = $this->getTableLocator()->exists('DeAccountTemplates') ? [] : ['className' => DeAccountTemplatesTable::class];
|
|
$this->DeAccountTemplates = $this->getTableLocator()->get('DeAccountTemplates', $configDeAccountTemplates);
|
|
|
|
$configDeAccounts = $this->getTableLocator()->exists('DeAccounts') ? [] : ['className' => DeAccountsTable::class];
|
|
$this->DeAccounts = $this->getTableLocator()->get('DeAccounts', $configDeAccounts);
|
|
|
|
$configDeTemplatedAccounts = $this->getTableLocator()->exists('DeTemplatedAccounts') ? [] : ['className' => DeTemplatedAccountsTable::class];
|
|
$this->DeTemplatedAccounts = $this->getTableLocator()->get('DeTemplatedAccounts', $configDeTemplatedAccounts);
|
|
}
|
|
|
|
/**
|
|
* tearDown method
|
|
*
|
|
* @return void
|
|
*/
|
|
protected function tearDown(): void
|
|
{
|
|
unset($this->DeAccountTemplates);
|
|
|
|
parent::tearDown();
|
|
}
|
|
|
|
/**
|
|
* Test index method
|
|
*
|
|
* Tests the index action with a logged in user
|
|
*
|
|
* @return void
|
|
* @throws Exception
|
|
*
|
|
* @uses DeAccountTemplatesController::index
|
|
*/
|
|
public function testIndexGetLoggedIn(): void
|
|
{
|
|
//$this->loginUserByRole('admin');
|
|
$url = [
|
|
'plugin' => 'CakeAccounting',
|
|
'controller' => 'DeAccountTemplates',
|
|
'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 DeAccountTemplatesController::view
|
|
*/
|
|
public function testViewGetLoggedIn(): void
|
|
{
|
|
$id = 1;
|
|
//$this->loginUserByRole('admin');
|
|
$url = [
|
|
'plugin' => 'CakeAccounting',
|
|
'controller' => 'DeAccountTemplates',
|
|
'action' => 'view',
|
|
$id,
|
|
];
|
|
$this->get($url);
|
|
$this->assertResponseCode(200);
|
|
}
|
|
|
|
/**
|
|
* Test select method
|
|
*
|
|
* Tests the select method which should import templated accounts as real accounts
|
|
*
|
|
* @throws Exception
|
|
* @return void
|
|
*
|
|
* @uses DeAccountTemplatesController::select
|
|
*/
|
|
public function testSelectPostValid(): void
|
|
{
|
|
$beforeAccounts = $this->DeAccounts->find();
|
|
foreach ($beforeAccounts as $beforeAccount) {
|
|
$this->DeAccounts->delete($beforeAccount);
|
|
}
|
|
|
|
$beforeImporting = $this->DeAccounts->find()->count();
|
|
$this->assertEquals(0, $beforeImporting);
|
|
|
|
$templatedAccounts = $this->DeTemplatedAccounts->find()->where(['account_template_id' => 1])->count();
|
|
$url = [
|
|
'plugin' => 'CakeAccounting',
|
|
'controller' => 'DeAccountTemplates',
|
|
'action' => 'select',
|
|
1,
|
|
];
|
|
//$this->loginUserByRole('admin');
|
|
$this->post($url);
|
|
$this->assertResponseCode(302);
|
|
$this->assertRedirectContains('de-accounts');
|
|
|
|
$after = $this->DeAccounts->find()->count();
|
|
$this->assertEquals($templatedAccounts, $after);
|
|
}
|
|
|
|
/**
|
|
* Test add method
|
|
*
|
|
* Tests the add action with a logged in user
|
|
*
|
|
* @return void
|
|
* @throws Exception
|
|
*
|
|
* @uses DeAccountTemplatesController::add
|
|
*/
|
|
public function testAddGetLoggedIn(): void
|
|
{
|
|
$cntBefore = $this->DeAccountTemplates->find()->count();
|
|
|
|
//$this->loginUserByRole('admin');
|
|
$url = [
|
|
'plugin' => 'CakeAccounting',
|
|
'controller' => 'DeAccountTemplates',
|
|
'action' => 'add',
|
|
];
|
|
$this->get($url);
|
|
$this->assertResponseCode(200);
|
|
|
|
$cntAfter = $this->DeAccountTemplates->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 DeAccountTemplatesController::add
|
|
*/
|
|
public function testAddPostLoggedInSuccess(): void
|
|
{
|
|
$cntBefore = $this->DeAccountTemplates->find()->count();
|
|
|
|
//$this->loginUserByRole('admin');
|
|
$url = [
|
|
'plugin' => 'CakeAccounting',
|
|
'controller' => 'DeAccountTemplates',
|
|
'action' => 'add',
|
|
];
|
|
$data = [
|
|
'name' => 'testing',
|
|
];
|
|
$this->post($url, $data);
|
|
$this->assertResponseCode(302);
|
|
$this->assertRedirectContains('de-account-templates');
|
|
|
|
$cntAfter = $this->DeAccountTemplates->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 DeAccountTemplatesController::add
|
|
*/
|
|
public function testAddPostLoggedInFailure(): void
|
|
{
|
|
$cntBefore = $this->DeAccountTemplates->find()->count();
|
|
|
|
//$this->loginUserByRole('admin');
|
|
$url = [
|
|
'plugin' => 'CakeAccounting',
|
|
'controller' => 'DeAccountTemplates',
|
|
'action' => 'add',
|
|
];
|
|
$data = [
|
|
'name' => '',
|
|
];
|
|
$this->post($url, $data);
|
|
$this->assertResponseCode(200);
|
|
|
|
$cntAfter = $this->DeAccountTemplates->find()->count();
|
|
$this->assertEquals($cntBefore, $cntAfter);
|
|
}
|
|
|
|
/**
|
|
* Test edit method
|
|
*
|
|
* Tests the edit action with a logged in user
|
|
*
|
|
* @return void
|
|
* @throws Exception
|
|
*
|
|
* @uses DeAccountTemplatesController::edit
|
|
*/
|
|
public function testEditGetLoggedIn(): void
|
|
{
|
|
//$this->loginUserByRole('admin');
|
|
$url = [
|
|
'plugin' => 'CakeAccounting',
|
|
'controller' => 'DeAccountTemplates',
|
|
'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 DeAccountTemplatesController::edit
|
|
*/
|
|
public function testEditPutLoggedInSuccess(): void
|
|
{
|
|
//$this->loginUserByRole('admin');
|
|
$id = 1;
|
|
$before = $this->DeAccountTemplates->get($id);
|
|
$url = [
|
|
'plugin' => 'CakeAccounting',
|
|
'controller' => 'DeAccountTemplates',
|
|
'action' => 'edit',
|
|
$id,
|
|
];
|
|
$data = [
|
|
// test new data here
|
|
'name' => 'updated name',
|
|
];
|
|
$this->put($url, $data);
|
|
|
|
$this->assertResponseCode(302);
|
|
$this->assertRedirectContains('de-account-templates');
|
|
|
|
$after = $this->DeAccountTemplates->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 DeAccountTemplatesController::edit
|
|
*/
|
|
public function testEditPutLoggedInFailure(): void
|
|
{
|
|
//$this->loginUserByRole('admin');
|
|
$id = 1;
|
|
$before = $this->DeAccountTemplates->get($id);
|
|
$url = [
|
|
'plugin' => 'CakeAccounting',
|
|
'controller' => 'DeAccountTemplates',
|
|
'action' => 'edit',
|
|
$id,
|
|
];
|
|
$data = [
|
|
'name' => '',
|
|
];
|
|
$this->put($url, $data);
|
|
$this->assertResponseCode(200);
|
|
$after = $this->DeAccountTemplates->get($id);
|
|
|
|
// assert save failed below
|
|
}
|
|
|
|
/**
|
|
* Test delete method
|
|
*
|
|
* Tests the delete action with a logged in user
|
|
*
|
|
* @return void
|
|
*@throws Exception
|
|
*
|
|
* @uses DeAccountTemplatesController::delete
|
|
*/
|
|
public function testDeleteLoggedIn(): void
|
|
{
|
|
$cntBefore = $this->DeAccountTemplates->find()->count();
|
|
|
|
//$this->loginUserByRole('admin');
|
|
$url = [
|
|
'plugin' => 'CakeAccounting',
|
|
'controller' => 'DeAccountTemplates',
|
|
'action' => 'delete',
|
|
1,
|
|
];
|
|
$this->delete($url);
|
|
$this->assertResponseCode(302);
|
|
$this->assertRedirectContains('de-account-templates');
|
|
|
|
$cntAfter = $this->DeAccountTemplates->find()->count();
|
|
$this->assertEquals($cntBefore - 1, $cntAfter);
|
|
}
|
|
}
|