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

158 lines
4.0 KiB
PHP

<?php
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 ContactUsFormSubmissionsController
*/
class ContactUsFormSubmissionsControllerTest extends TestCase
{
use IntegrationTestTrait;
/**
* @var ContactUsFormSubmissionsTable|Table
*/
protected $ContactUsFormSubmissions;
/**
* 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');
EventManager::instance()->setEventList(new EventList());
}
/**
* 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
* @throws Exception
*
* @uses ContactUsFormSubmissionsController::add
*/
public function testAddGet(): void
{
$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
* @throws Exception
*
* @uses ContactUsFormSubmissionsController::add
*/
public function testAddPostSuccess(): void
{
$cntBefore = $this->ContactUsFormSubmissions->find()->count();
$url = [
'plugin' => 'CakeContactUs',
'controller' => 'ContactUsFormSubmissions',
'action' => 'add',
];
$data = [
'name' => 'valid name',
'email' => 'valid_email@test.com',
'message' => 'valid message goes here',
];
$this->post($url, $data);
$this->assertResponseCode(302);
$cntAfter = $this->ContactUsFormSubmissions->find()->count();
$this->assertEquals($cntBefore + 1, $cntAfter);
$this->assertEventFired(CakeContactUsPlugin::EVENT_AFTER_CONTACT_US_FORM_SAVED);
}
/**
* Test add method
*
* Tests a POST request to the add action with a logged in user
*
* @return void
* @throws Exception
*
* @uses ContactUsFormSubmissionsController::add
*/
public function testAddPostLoggedInFailure(): void
{
$cntBefore = $this->ContactUsFormSubmissions->find()->count();
// $this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeContactUs',
'controller' => 'ContactUsFormSubmissions',
'action' => 'add',
];
$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);
}
}