CakeContactUs/tests/TestCase/Controller/ContactUsFormSubmissionsCon...

158 lines
4.0 KiB
PHP
Raw Normal View History

2025-01-10 07:47:18 +00:00
<?php
declare(strict_types=1);
namespace CakeContactUs\Test\TestCase\Controller;
2025-11-11 06:26:52 +00:00
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;
2025-01-10 07:47:18 +00:00
use Cake\TestSuite\IntegrationTestTrait;
use Cake\TestSuite\TestCase;
2025-11-11 06:26:52 +00:00
use CakeContactUs\CakeContactUsPlugin;
2025-01-10 07:47:18 +00:00
use CakeContactUs\Controller\ContactUsFormSubmissionsController;
use PHPUnit\Exception;
/**
* CakeContactUs\Controller\ContactUsFormSubmissionsController Test Case
*
2025-11-11 06:26:52 +00:00
* @uses ContactUsFormSubmissionsController
2025-01-10 07:47:18 +00:00
*/
class ContactUsFormSubmissionsControllerTest extends TestCase
{
use IntegrationTestTrait;
2025-11-11 06:26:52 +00:00
/**
* @var ContactUsFormSubmissionsTable|Table
*/
protected $ContactUsFormSubmissions;
2025-01-10 07:47:18 +00:00
/**
* Fixtures
*
* @var array<string>
*/
protected array $fixtures = [
'plugin.CakeContactUs.ContactUsFormSubmissions',
];
/**
* setUp method
*
* @return void
*/
protected function setUp(): void
{
parent::setUp();
$this->ContactUsFormSubmissions = $this->getTableLocator()->get('ContactUsFormSubmissions');
2025-11-11 06:26:52 +00:00
EventManager::instance()->setEventList(new EventList());
2025-01-10 07:47:18 +00:00
}
/**
* tearDown method
*
* @return void
*/
protected function tearDown(): void
{
unset($this->ContactUsFormSubmissions);
parent::tearDown();
}
/**
* Test add method
*
* Tests the add action with a logged in user
*
* @return void
2025-11-11 06:26:52 +00:00
* @throws Exception
*
* @uses ContactUsFormSubmissionsController::add
2025-01-10 07:47:18 +00:00
*/
2025-11-11 06:26:52 +00:00
public function testAddGet(): void
2025-01-10 07:47:18 +00:00
{
$cntBefore = $this->ContactUsFormSubmissions->find()->count();
$url = [
'plugin' => 'CakeContactUs',
'controller' => 'ContactUsFormSubmissions',
'action' => 'add',
];
$this->get($url);
$this->assertResponseCode(200);
$cntAfter = $this->ContactUsFormSubmissions->find()->count();
$this->assertEquals($cntBefore, $cntAfter);
}
/**
* Test add method
*
* Tests a POST request to the add action with a logged in user
*
* @return void
2025-11-11 06:26:52 +00:00
* @throws Exception
*
* @uses ContactUsFormSubmissionsController::add
2025-01-10 07:47:18 +00:00
*/
2025-11-11 06:26:52 +00:00
public function testAddPostSuccess(): void
2025-01-10 07:47:18 +00:00
{
$cntBefore = $this->ContactUsFormSubmissions->find()->count();
$url = [
'plugin' => 'CakeContactUs',
'controller' => 'ContactUsFormSubmissions',
'action' => 'add',
];
2025-11-11 06:26:52 +00:00
$data = [
'name' => 'valid name',
'email' => 'valid_email@test.com',
'message' => 'valid message goes here',
];
2025-01-10 07:47:18 +00:00
$this->post($url, $data);
$this->assertResponseCode(302);
$cntAfter = $this->ContactUsFormSubmissions->find()->count();
$this->assertEquals($cntBefore + 1, $cntAfter);
2025-11-11 06:26:52 +00:00
$this->assertEventFired(CakeContactUsPlugin::EVENT_AFTER_CONTACT_US_FORM_SAVED);
2025-01-10 07:47:18 +00:00
}
/**
* Test add method
*
* Tests a POST request to the add action with a logged in user
*
* @return void
2025-11-11 06:26:52 +00:00
* @throws Exception
*
* @uses ContactUsFormSubmissionsController::add
2025-01-10 07:47:18 +00:00
*/
public function testAddPostLoggedInFailure(): void
{
$cntBefore = $this->ContactUsFormSubmissions->find()->count();
// $this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeContactUs',
'controller' => 'ContactUsFormSubmissions',
'action' => 'add',
];
$data = [
2025-11-11 06:26:52 +00:00
'name' => 'valid name',
'email' => 'not_valid_email',
'message' => 'this is a valid message ',
2025-01-10 07:47:18 +00:00
];
2025-11-11 06:26:52 +00:00
$this->post($url, $data);
2025-01-10 07:47:18 +00:00
$this->assertResponseCode(200);
$cntAfter = $this->ContactUsFormSubmissions->find()->count();
$this->assertEquals($cntBefore, $cntAfter);
2025-11-11 06:26:52 +00:00
$this->assertEventFired(CakeContactUsPlugin::EVENT_BEFORE_CONTACT_US_FORM_SAVED);
2025-01-10 07:47:18 +00:00
}
}