CakeAccounting/tests/TestCase/Controller/DeExternalAccountsControlle...

440 lines
12 KiB
PHP
Raw Normal View History

2025-08-09 21:07:39 +00:00
<?php
declare(strict_types=1);
namespace CakeAccounting\Test\TestCase\Controller;
use Cake\ORM\Table;
use Cake\TestSuite\IntegrationTestTrait;
use Cake\TestSuite\TestCase;
use CakeAccounting\Controller\DeExternalAccountsController;
use CakeAccounting\Model\Table\DeExternalAccountsTable;
use PHPUnit\Exception;
/**
* CakeAccounting\Controller\DeExternalAccountsController Test Case
*
* @uses \CakeAccounting\Controller\DeExternalAccountsController
*/
class DeExternalAccountsControllerTest extends BaseControllerTest
{
/**
* @var DeExternalAccountsTable|Table
*/
protected DeExternalAccountsTable|Table $DeExternalAccounts;
/**
* Fixtures
*
* @var array<string>
*/
protected array $fixtures = [
'plugin.CakeAccounting.DeExternalAccounts',
];
/**
* setUp method
*
* @return void
*/
protected function setUp(): void
{
parent::setUp();
$this->DeExternalAccounts = $this->getTableLocator()->get('DeExternalAccounts');
$this->enableCsrfToken();
$this->enableSecurityToken();
}
/**
* tearDown method
*
* @return void
*/
protected function tearDown(): void
{
unset($this->DeExternalAccounts);
parent::tearDown();
}
/**
* Test index method
*
* Tests the index action with an unauthenticated user (not logged in)
*
* @uses \CakeAccounting\Controller\DeExternalAccountsController::index()
* @throws Exception
*
* @return void
*/
public function testIndexGetUnauthenticated(): void
{
$url = [
'plugin' => 'CakeAccounting',
'controller' => 'DeExternalAccounts',
'action' => 'index',
];
$this->get($url);
$this->assertResponseCode(302);
$this->assertRedirectContains('login');
}
/**
* Test index method
*
* Tests the index action with a logged in user
*
* @uses \CakeAccounting\Controller\DeExternalAccountsController::index()
* @throws Exception
*
* @return void
*/
public function testIndexGetLoggedIn(): void
{
$this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeAccounting',
'controller' => 'DeExternalAccounts',
'action' => 'index',
];
$this->get($url);
$this->assertResponseCode(200);
}
/**
* Test view method
*
* Tests the view action with an unauthenticated user (not logged in)
*
* @uses \CakeAccounting\Controller\DeExternalAccountsController::view()
* @throws Exception
*
* @return void
*/
public function testViewGetUnauthenticated(): void
{
$id = '2463dc4f-6f38-4492-8c8e-918f11e13748';
$url = [
'plugin' => 'CakeAccounting',
'controller' => 'DeExternalAccounts',
'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 \CakeAccounting\Controller\DeExternalAccountsController::view()
* @throws Exception
*
* @return void
*/
public function testViewGetLoggedIn(): void
{
$id = '2463dc4f-6f38-4492-8c8e-918f11e13748';
$this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeAccounting',
'controller' => 'DeExternalAccounts',
'action' => 'view',
$id,
];
$this->get($url);
$this->assertResponseCode(200);
}
/**
* Test add method
*
* Tests the add action with an unauthenticated user (not logged in)
*
* @uses \CakeAccounting\Controller\DeExternalAccountsController::add()
* @throws Exception
*
* @return void
*/
public function testAddGetUnauthenticated(): void
{
$cntBefore = $this->DeExternalAccounts->find()->count();
$url = [
'plugin' => 'CakeAccounting',
'controller' => 'DeExternalAccounts',
'action' => 'add',
];
$this->get($url);
$this->assertResponseCode(302);
$this->assertRedirectContains('login');
$cntAfter = $this->DeExternalAccounts->find()->count();
$this->assertEquals($cntBefore, $cntAfter);
}
/**
* Test add method
*
* Tests the add action with a logged in user
*
* @uses \CakeAccounting\Controller\DeExternalAccountsController::add()
* @throws Exception
*
* @return void
*/
public function testAddGetLoggedIn(): void
{
$cntBefore = $this->DeExternalAccounts->find()->count();
$this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeAccounting',
'controller' => 'DeExternalAccounts',
'action' => 'add',
];
$this->get($url);
$this->assertResponseCode(200);
$cntAfter = $this->DeExternalAccounts->find()->count();
$this->assertEquals($cntBefore, $cntAfter);
}
/**
* Test add method
*
* Tests a POST request to the add action with a logged in user
*
* @uses \CakeAccounting\Controller\DeExternalAccountsController::add()
* @throws Exception
*
* @return void
*/
public function testAddPostLoggedInSuccess(): void
{
$cntBefore = $this->DeExternalAccounts->find()->count();
$this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeAccounting',
'controller' => 'DeExternalAccounts',
'action' => 'add',
];
$data = [
'entity_type_code' => DE_ENTITY_TYPE_ORGANIZATION,
'related_model' => '',
'related_model_fk' => '',
];
$this->post($url, $data);
$this->assertResponseCode(302);
$this->assertRedirectContains('de-external-accounts');
$cntAfter = $this->DeExternalAccounts->find()->count();
$this->assertEquals($cntBefore + 1, $cntAfter);
}
/**
* Test add method
*
* Tests a POST request to the add action with a logged in user
*
* @uses \CakeAccounting\Controller\DeExternalAccountsController::add()
* @throws Exception
*
* @return void
*/
public function testAddPostLoggedInFailure(): void
{
$cntBefore = $this->DeExternalAccounts->find()->count();
$this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeAccounting',
'controller' => 'DeExternalAccounts',
'action' => 'add',
];
$data = [];
$this->post($url, $data);
$this->assertResponseCode(200);
$cntAfter = $this->DeExternalAccounts->find()->count();
$this->assertEquals($cntBefore, $cntAfter);
}
/**
* Test edit method
*
* Tests the edit action with an unauthenticated user (not logged in)
*
* @uses \CakeAccounting\Controller\DeExternalAccountsController::edit()
* @throws Exception
*
* @return void
*/
public function testEditGetUnauthenticated(): void
{
$id = '2463dc4f-6f38-4492-8c8e-918f11e13748';
$url = [
'plugin' => 'CakeAccounting',
'controller' => 'DeExternalAccounts',
'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 \CakeAccounting\Controller\DeExternalAccountsController::edit()
* @throws Exception
*
* @return void
*/
public function testEditGetLoggedIn(): void
{
$id = '2463dc4f-6f38-4492-8c8e-918f11e13748';
$this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeAccounting',
'controller' => 'DeExternalAccounts',
'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 \CakeAccounting\Controller\DeExternalAccountsController::edit()
* @throws Exception
*
* @return void
*/
public function testEditPutLoggedInSuccess(): void
{
$this->loginUserByRole('admin');
$id = '2463dc4f-6f38-4492-8c8e-918f11e13748';
$before = $this->DeExternalAccounts->get($id);
$url = [
'plugin' => 'CakeAccounting',
'controller' => 'DeExternalAccounts',
'action' => 'edit',
$id,
];
$data = [
// test new data here
'entity_type_code' => DE_ENTITY_TYPE_PROPERTY,
];
$this->put($url, $data);
$this->assertResponseCode(302);
$this->assertRedirectContains('de-external-accounts');
$after = $this->DeExternalAccounts->get($id);
$this->assertEquals($data['entity_type_code'], $after->entity_type_code);
// assert saved properly below
}
/**
* Test edit method
*
* Tests a PUT request to the edit action with a logged in user
*
* @uses \CakeAccounting\Controller\DeExternalAccountsController::edit()
* @throws Exception
*
* @return void
*/
public function testEditPutLoggedInFailure(): void
{
$this->loginUserByRole('admin');
$id = '2463dc4f-6f38-4492-8c8e-918f11e13748';
$before = $this->DeExternalAccounts->get($id);
$url = [
'plugin' => 'CakeAccounting',
'controller' => 'DeExternalAccounts',
'action' => 'edit',
$id,
];
$data = [
'entity_type_code' => '',
];
$this->put($url, $data);
$this->assertResponseCode(200);
$after = $this->DeExternalAccounts->get($id);
$this->assertEquals($before->entity_type_code, $after->entity_type_code);
// assert save failed below
}
/**
* Test delete method
*
* Tests the delete action with an unauthenticated user (not logged in)
*
* @uses \CakeAccounting\Controller\DeExternalAccountsController::delete()
* @throws Exception
*
* @return void
*/
public function testDeleteUnauthenticated(): void
{
$cntBefore = $this->DeExternalAccounts->find()->count();
$id = '2463dc4f-6f38-4492-8c8e-918f11e13748';
$url = [
'plugin' => 'CakeAccounting',
'controller' => 'DeExternalAccounts',
'action' => 'delete',
$id,
];
$this->delete($url);
$this->assertResponseCode(302);
$this->assertRedirectContains('login');
$cntAfter = $this->DeExternalAccounts->find()->count();
$this->assertEquals($cntBefore, $cntAfter);
}
/**
* Test delete method
*
* Tests the delete action with a logged in user
*
* @uses \CakeAccounting\Controller\DeExternalAccountsController::delete()
* @throws Exception
*
* @return void
*/
public function testDeleteLoggedIn(): void
{
$cntBefore = $this->DeExternalAccounts->find()->count();
$id = '2463dc4f-6f38-4492-8c8e-918f11e13748';
$this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeAccounting',
'controller' => 'DeExternalAccounts',
'action' => 'delete',
$id,
];
$this->delete($url);
$this->assertResponseCode(302);
$this->assertRedirectContains('de-external-accounts');
$cntAfter = $this->DeExternalAccounts->find()->count();
$this->assertEquals($cntBefore - 1, $cntAfter);
}
}