This commit is contained in:
2025-11-10 22:26:52 -08:00
parent 77f09055c9
commit a659368867
19 changed files with 301 additions and 358 deletions

View File

@@ -3,20 +3,32 @@ declare(strict_types=1);
namespace CakeContactUs\Test\TestCase\Controller;
use App\Model\Table\ContactUsFormSubmissionsTable;
use Cake\Controller\Controller;
use Cake\Event\EventList;
use Cake\Event\EventManager;
use Cake\Http\ServerRequest;
use Cake\ORM\Table;
use Cake\TestSuite\IntegrationTestTrait;
use Cake\TestSuite\TestCase;
use CakeContactUs\CakeContactUsPlugin;
use CakeContactUs\Controller\ContactUsFormSubmissionsController;
use PHPUnit\Exception;
/**
* CakeContactUs\Controller\ContactUsFormSubmissionsController Test Case
*
* @uses \CakeContactUs\Controller\ContactUsFormSubmissionsController
* @uses ContactUsFormSubmissionsController
*/
class ContactUsFormSubmissionsControllerTest extends TestCase
{
use IntegrationTestTrait;
/**
* @var ContactUsFormSubmissionsTable|Table
*/
protected $ContactUsFormSubmissions;
/**
* Fixtures
*
@@ -35,6 +47,8 @@ class ContactUsFormSubmissionsControllerTest extends TestCase
{
parent::setUp();
$this->ContactUsFormSubmissions = $this->getTableLocator()->get('ContactUsFormSubmissions');
EventManager::instance()->setEventList(new EventList());
}
/**
@@ -49,139 +63,20 @@ class ContactUsFormSubmissionsControllerTest extends TestCase
parent::tearDown();
}
/**
* Test index method
*
* Tests the index action with an unauthenticated user (not logged in)
*
* @uses \CakeContactUs\Controller\ContactUsFormSubmissionsController::index()
* @throws Exception
*
* @return void
*/
public function testIndexGetUnauthenticated(): void
{
$url = [
'plugin' => 'CakeContactUs',
'controller' => 'ContactUsFormSubmissions',
'action' => 'index',
];
$this->get($url);
$this->assertResponseCode(302);
$this->assertRedirectContains('login');
}
/**
* Test index method
*
* Tests the index action with a logged in user
*
* @uses \CakeContactUs\Controller\ContactUsFormSubmissionsController::index()
* @throws Exception
*
* @return void
*/
public function testIndexGetLoggedIn(): void
{
// $this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeContactUs',
'controller' => 'ContactUsFormSubmissions',
'action' => 'index',
];
$this->get($url);
$this->assertResponseCode(200);
}
/**
* Test view method
*
* Tests the view action with an unauthenticated user (not logged in)
*
* @uses \CakeContactUs\Controller\ContactUsFormSubmissionsController::view()
* @throws Exception
*
* @return void
*/
public function testViewGetUnauthenticated(): void
{
$id = 1;
$url = [
'plugin' => 'CakeContactUs',
'controller' => 'ContactUsFormSubmissions',
'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 \CakeContactUs\Controller\ContactUsFormSubmissionsController::view()
* @throws Exception
*
* @return void
*/
public function testViewGetLoggedIn(): void
{
$id = 1;
// $this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeContactUs',
'controller' => 'ContactUsFormSubmissions',
'action' => 'view',
$id,
];
$this->get($url);
$this->assertResponseCode(200);
}
/**
* Test add method
*
* Tests the add action with an unauthenticated user (not logged in)
*
* @uses \CakeContactUs\Controller\ContactUsFormSubmissionsController::add()
* @throws Exception
*
* @return void
*/
public function testAddGetUnauthenticated(): void
{
$cntBefore = $this->ContactUsFormSubmissions->find()->count();
$url = [
'plugin' => 'CakeContactUs',
'controller' => 'ContactUsFormSubmissions',
'action' => 'add',
];
$this->get($url);
$this->assertResponseCode(302);
$this->assertRedirectContains('login');
$cntAfter = $this->ContactUsFormSubmissions->find()->count();
$this->assertEquals($cntBefore, $cntAfter);
}
/**
* Test add method
*
* Tests the add action with a logged in user
*
* @uses \CakeContactUs\Controller\ContactUsFormSubmissionsController::add()
* @throws Exception
*
* @return void
* @throws Exception
*
* @uses ContactUsFormSubmissionsController::add
*/
public function testAddGetLoggedIn(): void
public function testAddGet(): void
{
$cntBefore = $this->ContactUsFormSubmissions->find()->count();
// $this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeContactUs',
'controller' => 'ContactUsFormSubmissions',
@@ -199,28 +94,31 @@ class ContactUsFormSubmissionsControllerTest extends TestCase
*
* Tests a POST request to the add action with a logged in user
*
* @uses \CakeContactUs\Controller\ContactUsFormSubmissionsController::add()
* @throws Exception
*
* @return void
* @throws Exception
*
* @uses ContactUsFormSubmissionsController::add
*/
public function testAddPostLoggedInSuccess(): void
public function testAddPostSuccess(): void
{
$cntBefore = $this->ContactUsFormSubmissions->find()->count();
// $this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeContactUs',
'controller' => 'ContactUsFormSubmissions',
'action' => 'add',
];
$data = [];
$data = [
'name' => 'valid name',
'email' => 'valid_email@test.com',
'message' => 'valid message goes here',
];
$this->post($url, $data);
$this->assertResponseCode(302);
$this->assertRedirectContains('contactusformsubmissions/view');
$cntAfter = $this->ContactUsFormSubmissions->find()->count();
$this->assertEquals($cntBefore + 1, $cntAfter);
$this->assertEventFired(CakeContactUsPlugin::EVENT_AFTER_CONTACT_US_FORM_SAVED);
}
/**
@@ -228,10 +126,10 @@ class ContactUsFormSubmissionsControllerTest extends TestCase
*
* Tests a POST request to the add action with a logged in user
*
* @uses \CakeContactUs\Controller\ContactUsFormSubmissionsController::add()
* @throws Exception
*
* @return void
* @throws Exception
*
* @uses ContactUsFormSubmissionsController::add
*/
public function testAddPostLoggedInFailure(): void
{
@@ -243,176 +141,17 @@ class ContactUsFormSubmissionsControllerTest extends TestCase
'controller' => 'ContactUsFormSubmissions',
'action' => 'add',
];
$data = [];
$data = [
'name' => 'valid name',
'email' => 'not_valid_email',
'message' => 'this is a valid message ',
];
$this->post($url, $data);
$this->assertResponseCode(200);
$cntAfter = $this->ContactUsFormSubmissions->find()->count();
$this->assertEquals($cntBefore, $cntAfter);
}
$this->assertEventFired(CakeContactUsPlugin::EVENT_BEFORE_CONTACT_US_FORM_SAVED);
/**
* Test edit method
*
* Tests the edit action with an unauthenticated user (not logged in)
*
* @uses \CakeContactUs\Controller\ContactUsFormSubmissionsController::edit()
* @throws Exception
*
* @return void
*/
public function testEditGetUnauthenticated(): void
{
$url = [
'plugin' => 'CakeContactUs',
'controller' => 'ContactUsFormSubmissions',
'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 \CakeContactUs\Controller\ContactUsFormSubmissionsController::edit()
* @throws Exception
*
* @return void
*/
public function testEditGetLoggedIn(): void
{
// $this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeContactUs',
'controller' => 'ContactUsFormSubmissions',
'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 \CakeContactUs\Controller\ContactUsFormSubmissionsController::edit()
* @throws Exception
*
* @return void
*/
public function testEditPutLoggedInSuccess(): void
{
// $this->loginUserByRole('admin');
$id = 1;
$before = $this->ContactUsFormSubmissions->get($id);
$url = [
'plugin' => 'CakeContactUs',
'controller' => 'ContactUsFormSubmissions',
'action' => 'edit',
$id,
];
$data = [
// test new data here
];
$this->put($url, $data);
$this->assertResponseCode(302);
$this->assertRedirectContains('contactusformsubmissions/view');
$after = $this->ContactUsFormSubmissions->get($id);
// assert saved properly below
}
/**
* Test edit method
*
* Tests a PUT request to the edit action with a logged in user
*
* @uses \CakeContactUs\Controller\ContactUsFormSubmissionsController::edit()
* @throws Exception
*
* @return void
*/
public function testEditPutLoggedInFailure(): void
{
// $this->loginUserByRole('admin');
$id = 1;
$before = $this->ContactUsFormSubmissions->get($id);
$url = [
'plugin' => 'CakeContactUs',
'controller' => 'ContactUsFormSubmissions',
'action' => 'edit',
$id,
];
$data = [];
$this->put($url, $data);
$this->assertResponseCode(200);
$after = $this->ContactUsFormSubmissions->get($id);
// assert save failed below
}
/**
* Test delete method
*
* Tests the delete action with an unauthenticated user (not logged in)
*
* @uses \CakeContactUs\Controller\ContactUsFormSubmissionsController::delete()
* @throws Exception
*
* @return void
*/
public function testDeleteUnauthenticated(): void
{
$cntBefore = $this->ContactUsFormSubmissions->find()->count();
$url = [
'plugin' => 'CakeContactUs',
'controller' => 'ContactUsFormSubmissions',
'action' => 'delete',
1,
];
$this->delete($url);
$this->assertResponseCode(302);
$this->assertRedirectContains('login');
$cntAfter = $this->ContactUsFormSubmissions->find()->count();
$this->assertEquals($cntBefore, $cntAfter);
}
/**
* Test delete method
*
* Tests the delete action with a logged in user
*
* @uses \CakeContactUs\Controller\ContactUsFormSubmissionsController::delete()
* @throws Exception
*
* @return void
*/
public function testDeleteLoggedIn(): void
{
$cntBefore = $this->ContactUsFormSubmissions->find()->count();
// $this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeContactUs',
'controller' => 'ContactUsFormSubmissions',
'action' => 'delete',
1,
];
$this->delete($url);
$this->assertResponseCode(302);
$this->assertRedirectContains('contactusformsubmissions');
$cntAfter = $this->ContactUsFormSubmissions->find()->count();
$this->assertEquals($cntBefore - 1, $cntAfter);
}
}