first standalone plugin commit
This commit is contained in:
481
tests/TestCase/Controller/AddressesControllerTest.php
Normal file
481
tests/TestCase/Controller/AddressesControllerTest.php
Normal file
@@ -0,0 +1,481 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CakeAddresses\Test\TestCase\Controller;
|
||||
|
||||
use CakeAddresses\Controller\AddressesController;
|
||||
use CakeAddresses\Model\Table\AddressesTable;
|
||||
use PHPUnit\Exception;
|
||||
|
||||
/**
|
||||
* CakeAddresses\Controller\AddressesController Test Case
|
||||
*
|
||||
* @uses \CakeAddresses\Controller\AddressesController
|
||||
*/
|
||||
class AddressesControllerTest extends BaseControllerTest
|
||||
{
|
||||
/**
|
||||
* Addresses Table
|
||||
*
|
||||
* @var \CakeAddresses\Model\Table\AddressesTable
|
||||
*/
|
||||
protected $Addresses;
|
||||
|
||||
/**
|
||||
* Fixtures
|
||||
*
|
||||
* @var array<string>
|
||||
*/
|
||||
protected array $fixtures = [
|
||||
'plugin.CakeAddresses.Addresses',
|
||||
// 'plugin.CakeAddresses.Cities',
|
||||
'plugin.CakeAddresses.Countries',
|
||||
// 'plugin.CakeAddresses.Regions',
|
||||
'plugin.CakeAddresses.States',
|
||||
// 'plugin.CakeAddresses.Subregions',
|
||||
];
|
||||
|
||||
/**
|
||||
* setUp method
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->Addresses = $this->getTableLocator()->get('CakeAddresses.Addresses');
|
||||
$this->enableCsrfToken();
|
||||
}
|
||||
|
||||
/**
|
||||
* tearDown method
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function tearDown(): void
|
||||
{
|
||||
unset($this->Addresses);
|
||||
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test index method
|
||||
*
|
||||
* Tests the index action with an unauthenticated user (not logged in)
|
||||
*
|
||||
* @uses \CakeAddresses\Controller\AddressesController::index()
|
||||
* @throws Exception
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testIndexGetUnauthenticated(): void
|
||||
{
|
||||
$url = [
|
||||
'plugin' => 'CakeAddresses',
|
||||
'controller' => 'Addresses',
|
||||
'action' => 'index',
|
||||
];
|
||||
$this->get($url);
|
||||
$this->assertResponseCode(302);
|
||||
$this->assertRedirectContains('login');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test index method
|
||||
*
|
||||
* Tests the index action with a logged in user
|
||||
*
|
||||
* @uses \CakeAddresses\Controller\AddressesController::index()
|
||||
* @throws Exception
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testIndexGetLoggedIn(): void
|
||||
{
|
||||
$this->loginUserByRole();
|
||||
$url = [
|
||||
'plugin' => 'CakeAddresses',
|
||||
'controller' => 'Addresses',
|
||||
'action' => 'index',
|
||||
];
|
||||
$this->get($url);
|
||||
$this->assertResponseCode(200);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test view method
|
||||
*
|
||||
* Tests the view action with an unauthenticated user (not logged in)
|
||||
*
|
||||
* @uses \CakeAddresses\Controller\AddressesController::view()
|
||||
* @throws Exception
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testViewGetUnauthenticated(): void
|
||||
{
|
||||
$id = 1;
|
||||
$url = [
|
||||
'plugin' => 'CakeAddresses',
|
||||
'controller' => 'Addresses',
|
||||
'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 \CakeAddresses\Controller\AddressesController::view()
|
||||
* @throws Exception
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testViewGetLoggedIn(): void
|
||||
{
|
||||
$id = 1;
|
||||
$this->loginUserByRole();
|
||||
$url = [
|
||||
'plugin' => 'CakeAddresses',
|
||||
'controller' => 'Addresses',
|
||||
'action' => 'view',
|
||||
$id,
|
||||
];
|
||||
$this->get($url);
|
||||
$this->assertResponseCode(200);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test add method
|
||||
*
|
||||
* Tests the add action with an unauthenticated user (not logged in)
|
||||
*
|
||||
* @uses \CakeAddresses\Controller\AddressesController::add()
|
||||
* @throws Exception
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testAddGetUnauthenticated(): void
|
||||
{
|
||||
$cntBefore = $this->Addresses->find()->count();
|
||||
$url = [
|
||||
'plugin' => 'CakeAddresses',
|
||||
'controller' => 'Addresses',
|
||||
'action' => 'add',
|
||||
];
|
||||
$this->get($url);
|
||||
$this->assertResponseCode(302);
|
||||
$this->assertRedirectContains('login');
|
||||
|
||||
$cntAfter = $this->Addresses->find()->count();
|
||||
$this->assertEquals($cntBefore, $cntAfter);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test add method
|
||||
*
|
||||
* Tests the add action with a logged in user
|
||||
*
|
||||
* @uses \CakeAddresses\Controller\AddressesController::add()
|
||||
* @throws Exception
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testAddGetLoggedIn(): void
|
||||
{
|
||||
$cntBefore = $this->Addresses->find()->count();
|
||||
|
||||
$this->loginUserByRole();
|
||||
$url = [
|
||||
'plugin' => 'CakeAddresses',
|
||||
'controller' => 'Addresses',
|
||||
'action' => 'add',
|
||||
];
|
||||
$this->get($url);
|
||||
$this->assertResponseCode(200);
|
||||
|
||||
$cntAfter = $this->Addresses->find()->count();
|
||||
$this->assertEquals($cntBefore, $cntAfter);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test add method
|
||||
*
|
||||
* Tests a POST request to the add action with a logged in user
|
||||
*
|
||||
* @uses \CakeAddresses\Controller\AddressesController::add()
|
||||
* @throws Exception
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testAddPostLoggedInSuccess(): void
|
||||
{
|
||||
$cntBefore = $this->Addresses->find()->count();
|
||||
|
||||
$this->loginUserByRole();
|
||||
$url = [
|
||||
'plugin' => 'CakeAddresses',
|
||||
'controller' => 'Addresses',
|
||||
'action' => 'add',
|
||||
];
|
||||
$data = [
|
||||
'address_name' => 'new address',
|
||||
'contact_name' => 'john doe',
|
||||
'address_line1' => '123 Test ST',
|
||||
'address_line2' => '',
|
||||
'city' => 'Seattle',
|
||||
'city_id' => '',
|
||||
'state_id' => 1462,
|
||||
'postal_code' => '98056',
|
||||
'country_id' => 233,
|
||||
'phone_number' => '',
|
||||
'email' => '',
|
||||
'notes' => '',
|
||||
];
|
||||
$this->post($url, $data);
|
||||
$this->assertResponseCode(302);
|
||||
$this->assertRedirectContains('addresses');
|
||||
|
||||
$cntAfter = $this->Addresses->find()->count();
|
||||
$this->assertEquals($cntBefore + 1, $cntAfter);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test add method
|
||||
*
|
||||
* Tests a POST request to the add action with a logged in user
|
||||
*
|
||||
* @uses \CakeAddresses\Controller\AddressesController::add()
|
||||
* @throws Exception
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testAddPostLoggedInFailure(): void
|
||||
{
|
||||
$cntBefore = $this->Addresses->find()->count();
|
||||
|
||||
$this->loginUserByRole();
|
||||
$url = [
|
||||
'plugin' => 'CakeAddresses',
|
||||
'controller' => 'Addresses',
|
||||
'action' => 'add',
|
||||
];
|
||||
$data = [
|
||||
'address_name' => '',
|
||||
'contact_name' => '',
|
||||
'address_line1' => '',
|
||||
'address_line2' => '',
|
||||
'city' => 'Seattle',
|
||||
'city_id' => '',
|
||||
'state_id' => 1462,
|
||||
'postal_code' => '98056',
|
||||
'country_id' => 233,
|
||||
'phone_number' => '',
|
||||
'email' => '',
|
||||
'notes' => '',
|
||||
];
|
||||
$this->post($url, $data);
|
||||
$this->assertResponseCode(200);
|
||||
|
||||
$cntAfter = $this->Addresses->find()->count();
|
||||
$this->assertEquals($cntBefore, $cntAfter);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test edit method
|
||||
*
|
||||
* Tests the edit action with an unauthenticated user (not logged in)
|
||||
*
|
||||
* @uses \CakeAddresses\Controller\AddressesController::edit()
|
||||
* @throws Exception
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testEditGetUnauthenticated(): void
|
||||
{
|
||||
$id = 1;
|
||||
$url = [
|
||||
'plugin' => 'CakeAddresses',
|
||||
'controller' => 'Addresses',
|
||||
'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 \CakeAddresses\Controller\AddressesController::edit()
|
||||
* @throws Exception
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testEditGetLoggedIn(): void
|
||||
{
|
||||
$id = 1;
|
||||
$this->loginUserByRole();
|
||||
$url = [
|
||||
'plugin' => 'CakeAddresses',
|
||||
'controller' => 'Addresses',
|
||||
'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 \CakeAddresses\Controller\AddressesController::edit()
|
||||
* @throws Exception
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testEditPutLoggedInSuccess(): void
|
||||
{
|
||||
$this->loginUserByRole();
|
||||
$id = 1;
|
||||
$before = $this->Addresses->get($id);
|
||||
$url = [
|
||||
'plugin' => 'CakeAddresses',
|
||||
'controller' => 'Addresses',
|
||||
'action' => 'edit',
|
||||
$id,
|
||||
];
|
||||
$data = [
|
||||
// test new data here
|
||||
'address_name' => 'new address',
|
||||
'contact_name' => 'john doe',
|
||||
'address_line1' => '123 Test ST',
|
||||
'address_line2' => '',
|
||||
'city' => 'Seattle',
|
||||
'city_id' => '',
|
||||
'state_id' => 1462,
|
||||
'postal_code' => '98056',
|
||||
'country_id' => 233,
|
||||
'phone_number' => '',
|
||||
'email' => '',
|
||||
'notes' => '',
|
||||
];
|
||||
$this->put($url, $data);
|
||||
|
||||
$this->assertResponseCode(302);
|
||||
$this->assertRedirectContains('addresses');
|
||||
|
||||
$after = $this->Addresses->get($id);
|
||||
// assert saved properly below
|
||||
}
|
||||
|
||||
/**
|
||||
* Test edit method
|
||||
*
|
||||
* Tests a PUT request to the edit action with a logged in user
|
||||
*
|
||||
* @uses \CakeAddresses\Controller\AddressesController::edit()
|
||||
* @throws Exception
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testEditPutLoggedInFailure(): void
|
||||
{
|
||||
$this->loginUserByRole();
|
||||
$id = 1;
|
||||
$before = $this->Addresses->get($id);
|
||||
$url = [
|
||||
'plugin' => 'CakeAddresses',
|
||||
'controller' => 'Addresses',
|
||||
'action' => 'edit',
|
||||
$id,
|
||||
];
|
||||
$data = [
|
||||
'address_name' => '',
|
||||
'contact_name' => '',
|
||||
'address_line1' => '',
|
||||
'address_line2' => '',
|
||||
'city' => 'Seattle',
|
||||
'city_id' => '',
|
||||
'state_id' => 1462,
|
||||
'postal_code' => '98056',
|
||||
'country_id' => 233,
|
||||
'phone_number' => '',
|
||||
'email' => '',
|
||||
'notes' => '',
|
||||
];
|
||||
$this->put($url, $data);
|
||||
$this->assertResponseCode(200);
|
||||
$after = $this->Addresses->get($id);
|
||||
|
||||
// assert save failed below
|
||||
}
|
||||
|
||||
/**
|
||||
* Test delete method
|
||||
*
|
||||
* Tests the delete action with an unauthenticated user (not logged in)
|
||||
*
|
||||
* @uses \CakeAddresses\Controller\AddressesController::delete()
|
||||
* @throws Exception
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testDeleteUnauthenticated(): void
|
||||
{
|
||||
$cntBefore = $this->Addresses->find()->count();
|
||||
|
||||
$url = [
|
||||
'plugin' => 'CakeAddresses',
|
||||
'controller' => 'Addresses',
|
||||
'action' => 'delete',
|
||||
1,
|
||||
];
|
||||
$this->delete($url);
|
||||
$this->assertResponseCode(302);
|
||||
$this->assertRedirectContains('login');
|
||||
|
||||
$cntAfter = $this->Addresses->find()->count();
|
||||
$this->assertEquals($cntBefore, $cntAfter);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test delete method
|
||||
*
|
||||
* Tests the delete action with a logged in user
|
||||
*
|
||||
* @uses \CakeAddresses\Controller\AddressesController::delete()
|
||||
* @throws Exception
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testDeleteLoggedIn(): void
|
||||
{
|
||||
$cntBefore = $this->Addresses->find()->count();
|
||||
|
||||
$this->loginUserByRole();
|
||||
$url = [
|
||||
'plugin' => 'CakeAddresses',
|
||||
'controller' => 'Addresses',
|
||||
'action' => 'delete',
|
||||
1,
|
||||
];
|
||||
$this->delete($url);
|
||||
$this->assertResponseCode(302);
|
||||
$this->assertRedirectContains('addresses');
|
||||
|
||||
$cntAfter = $this->Addresses->find()->count();
|
||||
$this->assertEquals($cntBefore - 1, $cntAfter);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user