Files
cake-contact-us/tests/TestCase/Controller/ContactUsFormSubmissionsControllerTest.php

300 lines
7.4 KiB
PHP
Raw Normal View History

2025-01-09 23:47:18 -08:00
<?php
declare(strict_types=1);
namespace CakeContactUs\Test\TestCase\Controller;
2025-11-10 23:51:18 -08:00
use Cake\Core\Configure;
2025-11-10 22:26:52 -08:00
use Cake\Event\EventList;
use Cake\Event\EventManager;
2025-11-10 23:51:18 -08:00
use Cake\Mailer\Transport\DebugTransport;
use Cake\TestSuite\EmailTrait;
2025-01-09 23:47:18 -08:00
use Cake\TestSuite\IntegrationTestTrait;
use Cake\TestSuite\TestCase;
2025-11-10 22:26:52 -08:00
use CakeContactUs\CakeContactUsPlugin;
use CakeContactUs\Test\Fixture\ContactUsFormSubmissionsFixture;
2025-01-09 23:47:18 -08:00
/**
* CakeContactUs\Controller\ContactUsFormSubmissionsController Test Case
*/
class ContactUsFormSubmissionsControllerTest extends TestCase {
2025-01-09 23:47:18 -08:00
use IntegrationTestTrait;
use EmailTrait;
/**
* @var \App\Model\Table\ContactUsFormSubmissionsTable|\Cake\ORM\Table
2025-11-10 22:26:52 -08:00
*/
protected $ContactUsFormSubmissions;
2025-11-10 22:26:52 -08:00
/**
2025-01-09 23:47:18 -08:00
* Fixtures
*
* @var array<string>
*/
protected array $fixtures = [
ContactUsFormSubmissionsFixture::class,
];
2025-01-09 23:47:18 -08:00
/**
2025-01-09 23:47:18 -08:00
* setUp method
*
* @return void
*/
protected function setUp(): void {
parent::setUp();
$this->ContactUsFormSubmissions = $this->getTableLocator()->get('CakeContactUs.ContactUsFormSubmissions');
2025-11-10 22:26:52 -08:00
EventManager::instance()->setEventList(new EventList());
}
2025-01-09 23:47:18 -08:00
/**
2025-01-09 23:47:18 -08:00
* tearDown method
*
* @return void
*/
protected function tearDown(): void {
unset($this->ContactUsFormSubmissions);
2025-01-09 23:47:18 -08:00
parent::tearDown();
}
2025-01-09 23:47:18 -08:00
/**
2025-01-09 23:47:18 -08:00
* Test add method
*
* Tests the add action with a logged in user
*
2025-11-10 22:26:52 -08:00
* @throws Exception
*
* @return void
2025-01-09 23:47:18 -08:00
*/
public function testAddGet(): void {
$cntBefore = $this->ContactUsFormSubmissions->find()->count();
2025-01-09 23:47:18 -08:00
$url = [
'plugin' => 'CakeContactUs',
'controller' => 'ContactUsFormSubmissions',
'action' => 'add',
];
$this->get($url);
$this->assertResponseCode(200);
2025-01-09 23:47:18 -08:00
$cntAfter = $this->ContactUsFormSubmissions->find()->count();
$this->assertEquals($cntBefore, $cntAfter);
2025-11-10 23:51:18 -08:00
$this->assertNoMailSent();
}
2025-01-09 23:47:18 -08:00
/**
2025-01-09 23:47:18 -08:00
* Test add method
*
* Tests a POST request to the add action with a logged in user
*
2025-11-10 22:26:52 -08:00
* @throws Exception
*
* @return void
2025-01-09 23:47:18 -08:00
*/
public function testAddPostSuccessNoEmail(): void {
$cntBefore = $this->ContactUsFormSubmissions->find()->count();
$url = [
'plugin' => 'CakeContactUs',
'controller' => 'ContactUsFormSubmissions',
'action' => 'add',
];
$data = [
'first_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);
$this->assertNoMailSent();
}
/**
2025-11-10 23:51:18 -08:00
* Test add method
*
* Tests a POST request to the add action with a logged in user
*
* @throws Exception
*
* @return void
2025-11-10 23:51:18 -08:00
*/
public function testAddPostSuccessBothEmailsSent(): void {
Configure::write('ContactUs', [
'fields' => [
'captcha' => false,
'email' => true,
],
'email' => [
'mailerClass' => 'CakeContactUs.ContactUsFormSubmissions',
'confirmation' => true, // true or false
'backend' => [ // array with enabled and the to/cc/bcc/ fields as strings or arrays
'enabled' => true,
'to' => 'test@example.com',
],
],
]);
Configure::write('EmailTransport', [
'default' => [
'className' => DebugTransport::class,
],
]);
$cntBefore = $this->ContactUsFormSubmissions->find()->count();
$url = [
'plugin' => 'CakeContactUs',
'controller' => 'ContactUsFormSubmissions',
'action' => 'add',
];
$data = [
'first_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);
$this->assertMailCount(2); // confirmation + backend emails
}
/**
2025-11-10 23:51:18 -08:00
* Test add method
*
* Tests a POST request to the add action with a logged in user
*
* @throws Exception
*
* @return void
2025-11-10 23:51:18 -08:00
*/
public function testAddPostSuccessConfirmationEmailSent(): void {
Configure::write('ContactUs', [
'fields' => [
'captcha' => false,
'email' => true,
],
'email' => [
'mailerClass' => 'CakeContactUs.ContactUsFormSubmissions',
'confirmation' => true, // true or false
'backend' => [ // array with enabled and the to/cc/bcc/ fields as strings or arrays
'enabled' => false,
'to' => 'test@example.com',
],
],
]);
$cntBefore = $this->ContactUsFormSubmissions->find()->count();
$url = [
'plugin' => 'CakeContactUs',
'controller' => 'ContactUsFormSubmissions',
'action' => 'add',
];
$data = [
'first_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);
$this->assertMailCount(1); // confirmation only
}
/**
2025-11-10 23:51:18 -08:00
* Test add method
*
* Tests a POST request to the add action with a logged in user
*
* @throws Exception
*
* @return void
2025-11-10 23:51:18 -08:00
*/
public function testAddPostSuccessBackendEmailSent(): void {
Configure::write('ContactUs', [
'fields' => [
'captcha' => false,
'email' => true,
],
'email' => [
'mailerClass' => 'CakeContactUs.ContactUsFormSubmissions',
'confirmation' => false, // true or false
'backend' => [ // array with enabled and the to/cc/bcc/ fields as strings or arrays
'enabled' => true,
'to' => 'test@example.com',
],
],
]);
$cntBefore = $this->ContactUsFormSubmissions->find()->count();
$url = [
'plugin' => 'CakeContactUs',
'controller' => 'ContactUsFormSubmissions',
'action' => 'add',
];
$data = [
'first_name' => 'valid name',
'email' => 'valid_email@test.com',
'message' => 'valid message goes here',
];
$this->post($url, $data);
$responseBody = (string)$this->_response->getBody();
$tmpLog = fopen(TMP . 'phpunit.log', 'w');
if ($tmpLog) {
fwrite($tmpLog, $responseBody);
}
$this->assertResponseCode(302);
$cntAfter = $this->ContactUsFormSubmissions->find()->count();
$this->assertEquals($cntBefore + 1, $cntAfter);
$this->assertEventFired(CakeContactUsPlugin::EVENT_AFTER_CONTACT_US_FORM_SAVED);
$this->assertMailCount(1); // backend email only
}
/**
2025-01-09 23:47:18 -08:00
* Test add method
*
* Tests a POST request to the add action with a logged in user
*
2025-11-10 22:26:52 -08:00
* @throws Exception
*
* @return void
2025-01-09 23:47:18 -08:00
*/
public function testAddPostLoggedInFailure(): void {
$cntBefore = $this->ContactUsFormSubmissions->find()->count();
// $this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeContactUs',
'controller' => 'ContactUsFormSubmissions',
'action' => 'add',
];
$data = [
'first_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);
$this->assertNoMailSent();
}
2025-01-09 23:47:18 -08:00
}