Files
cake-contact-us/tests/TestCase/Controller/ContactUsFormSubmissionsControllerTest.php
T
bs 32afb9ce64
CI / testsuite (mysql, 8.2, ) (push) Successful in 3m22s
CI / testsuite (mysql, 8.4, ) (push) Successful in 4m35s
CI / testsuite (pgsql, 8.2, ) (push) Successful in 3m9s
CI / testsuite (pgsql, 8.4, ) (push) Successful in 4m13s
CI / testsuite (sqlite, 8.2, ) (push) Successful in 3m5s
CI / testsuite (sqlite, 8.2, prefer-lowest) (push) Successful in 3m5s
CI / testsuite (sqlite, 8.4, ) (push) Successful in 4m14s
CI / Coding Standard & Static Analysis (push) Successful in 2m21s
split config for turnstile + turnstile helper updates
2026-11-14 16:56:35 -08:00

365 lines
9.9 KiB
PHP

<?php
declare(strict_types=1);
namespace CakeContactUs\Test\TestCase\Controller;
use Cake\Core\Configure;
use Cake\Event\EventList;
use Cake\Event\EventManager;
use Cake\TestSuite\EmailTrait;
use Cake\TestSuite\IntegrationTestTrait;
use Cake\TestSuite\TestCase;
use CakeContactUs\CakeContactUsPlugin;
use CakeContactUs\Test\Fixture\ContactUsFormSubmissionsFixture;
/**
* CakeContactUs\Controller\ContactUsFormSubmissionsController Test Case
*/
class ContactUsFormSubmissionsControllerTest extends TestCase {
use IntegrationTestTrait;
use EmailTrait;
/**
* @var \App\Model\Table\ContactUsFormSubmissionsTable|\Cake\ORM\Table
*/
protected $ContactUsFormSubmissions;
/**
* Fixtures
*
* @var array<string>
*/
protected array $fixtures = [
ContactUsFormSubmissionsFixture::class,
];
/**
* setUp method
*
* @return void
*/
protected function setUp(): void {
parent::setUp();
$this->ContactUsFormSubmissions = $this->getTableLocator()->get('CakeContactUs.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
*
* @throws Exception
*
* @return void
*/
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);
$this->assertNoMailSent();
}
/**
* Test add method
*
* Tests a POST request to the add action with a logged in user
*
* @throws \Exception
*
* @return void
*/
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_BEFORE_CONTACT_US_FORM_SAVED);
$this->assertEventFired(CakeContactUsPlugin::EVENT_AFTER_CONTACT_US_FORM_SAVED);
$this->assertNoMailSent();
}
/**
* Test add method
*
* Tests a POST request to the add action with a logged in user
*
* @throws \Exception
*
* @return void
*/
public function testAddPostSuccessWithTurnstile(): void {
$cntBefore = $this->ContactUsFormSubmissions->find()->count();
Configure::write('ContactUs.fields.turnstile', true);
Configure::write('Turnstile', [
'enabled' => true,
'siteKey' => '1x00000000000000000000AA',
'secretKey' => '1x0000000000000000000000000000000AA',
]);
$url = [
'plugin' => 'CakeContactUs',
'controller' => 'ContactUsFormSubmissions',
'action' => 'add',
];
$data = [
'first_name' => 'valid name',
'email' => 'valid_email@test.com',
'message' => 'valid message goes here',
'cf-turnstile-response' => 'XXXX.DUMMY.TOKEN.XXXX',
];
$this->post($url, $data);
$this->assertResponseCode(302);
$cntAfter = $this->ContactUsFormSubmissions->find()->count();
$this->assertEquals($cntBefore + 1, $cntAfter);
$this->assertEventFired(CakeContactUsPlugin::EVENT_BEFORE_CONTACT_US_FORM_SAVED);
$this->assertEventFired(CakeContactUsPlugin::EVENT_AFTER_CONTACT_US_FORM_SAVED);
$this->assertNoMailSent();
}
/**
* Test add method
*
* Tests a POST request to the add action with a logged in user
*
* @throws \Exception
*
* @return void
*/
public function testAddPostInvalidTurnstile(): void {
$cntBefore = $this->ContactUsFormSubmissions->find()->count();
Configure::write('ContactUs.fields.turnstile', true);
Configure::write('Turnstile', [
'enabled' => true,
'siteKey' => '2x00000000000000000000BB',
'secretKey' => '2x0000000000000000000000000000000AA',
]);
$url = [
'plugin' => 'CakeContactUs',
'controller' => 'ContactUsFormSubmissions',
'action' => 'add',
];
$data = [
'first_name' => 'valid name',
'email' => 'valid_email@test.com',
'message' => 'valid message goes here',
'cf-turnstile-response' => 'XXXX.DUMMY.TOKEN.XXXX',
];
$this->post($url, $data);
// dd($this->_response);
$this->assertResponseCode(200);
$cntAfter = $this->ContactUsFormSubmissions->find()->count();
$this->assertEquals($cntBefore, $cntAfter);
$this->assertEventFired(CakeContactUsPlugin::EVENT_BEFORE_CONTACT_US_FORM_SAVED);
$this->assertNoMailSent();
}
/**
* Test add method
*
* Tests a POST request to the add action with a logged in user
*
* @throws \Exception
*
* @return void
*/
public function testAddPostSuccessBothEmailsSent(): void {
Configure::write('ContactUs.fields', [
'captcha' => false,
'email' => true,
'turnstile' => false,
]);
Configure::write('ContactUs.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',
],
]);
$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_BEFORE_CONTACT_US_FORM_SAVED);
$this->assertEventFired(CakeContactUsPlugin::EVENT_AFTER_CONTACT_US_FORM_SAVED);
$this->assertMailCount(2); // confirmation + backend emails
}
/**
* Test add method
*
* Tests a POST request to the add action with a logged in user
*
* @throws \Exception
*
* @return void
*/
public function testAddPostSuccessConfirmationEmailSent(): void {
Configure::write('ContactUs.fields', [
'captcha' => false,
'email' => true,
'turnstile' => false,
]);
Configure::write('ContactUs.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_BEFORE_CONTACT_US_FORM_SAVED);
$this->assertEventFired(CakeContactUsPlugin::EVENT_AFTER_CONTACT_US_FORM_SAVED);
$this->assertMailCount(1); // confirmation only
}
/**
* Test add method
*
* Tests a POST request to the add action with a logged in user
*
* @throws \Exception
*
* @return void
*/
public function testAddPostSuccessBackendEmailSent(): void {
Configure::write('ContactUs.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_BEFORE_CONTACT_US_FORM_SAVED);
$this->assertEventFired(CakeContactUsPlugin::EVENT_AFTER_CONTACT_US_FORM_SAVED);
$this->assertMailCount(1); // backend email only
}
/**
* Test add method
*
* Tests a POST request to the add action with a logged in user
*
* @throws \Exception
*
* @return void
*/
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();
}
}