tests
This commit is contained in:
parent
77f09055c9
commit
a659368867
|
|
@ -6,3 +6,4 @@
|
|||
/config/Migrations/schema-dump-default.lock
|
||||
/vendor/
|
||||
/.idea/
|
||||
/tmp
|
||||
11
LICENSE
11
LICENSE
|
|
@ -1,10 +1,9 @@
|
|||
This is free and unencumbered software released into the public domain.
|
||||
MIT License
|
||||
|
||||
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means.
|
||||
Copyright (c) 2025 HI POWERED DEV, LLC
|
||||
|
||||
In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and
|
||||
successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law.
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
For more information, please refer to <http://unlicense.org/>
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
|
|
|||
|
|
@ -2,13 +2,14 @@
|
|||
"name": "hi-powered-dev/cake-contact-us",
|
||||
"description": "Contact Us plugin for CakePHP",
|
||||
"type": "cakephp-plugin",
|
||||
"license": "UNLICENSE",
|
||||
"license": "MIT",
|
||||
"require": {
|
||||
"php": ">=8.1",
|
||||
"cakephp/cakephp": "^5.0.1"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^10.1"
|
||||
"phpunit/phpunit": "^10.1",
|
||||
"cakephp/migrations": "^4.0.0"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
|
|
@ -18,7 +19,8 @@
|
|||
"autoload-dev": {
|
||||
"psr-4": {
|
||||
"CakeContactUs\\Test\\": "tests/",
|
||||
"Cake\\Test\\": "vendor/cakephp/cakephp/tests/"
|
||||
"Cake\\Test\\": "vendor/cakephp/cakephp/tests/",
|
||||
"TestApp\\": "tests/test_app/src/"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ class CreateContactUsFormSubmissions extends AbstractMigration
|
|||
$table->addColumn('client_ip', 'string', [
|
||||
'default' => null,
|
||||
'limit' => 45,
|
||||
'null' => false,
|
||||
'null' => true,
|
||||
]);
|
||||
$table->addColumn('name', 'string', [
|
||||
'default' => null,
|
||||
|
|
|
|||
|
|
@ -70,6 +70,7 @@ class ContactUsComponent extends Component
|
|||
if ($this->getConfig('requireCaptcha')) {
|
||||
$this->ContactUsFormSubmissions->addBehavior('Captcha.Captcha');
|
||||
}
|
||||
|
||||
return $this->ContactUsFormSubmissions->newEmptyEntity();
|
||||
}
|
||||
|
||||
|
|
@ -81,7 +82,12 @@ class ContactUsComponent extends Component
|
|||
if (!isset($postData)) {
|
||||
$postData = $this->getController()->getRequest()->getData();
|
||||
}
|
||||
$postData['client_ip'] = array_key_exists('client_ip', $postData) && $postData['client_ip'] ? $postData['client_ip'] : $this->getController()->getRequest()->clientIp();
|
||||
$postData['client_ip'] = array_key_exists('client_ip', $postData) && $postData['client_ip'] ?
|
||||
$postData['client_ip'] :
|
||||
($this->getConfig('clientIpHeader') ?
|
||||
$this->getController()->getRequest()->getHeaderLine($this->getConfig('clientIpHeader')) :
|
||||
$this->getController()->getRequest()->clientIp()
|
||||
);
|
||||
$postData['submitted_at'] = DateTime::now();
|
||||
|
||||
$event = $this->getController()->dispatchEvent(CakeContactUsPlugin::EVENT_BEFORE_CONTACT_US_FORM_SAVED, [
|
||||
|
|
@ -119,7 +125,10 @@ class ContactUsComponent extends Component
|
|||
Log::debug(print_r('$contactUsFormSubmission->getErrors()', true));
|
||||
Log::debug(print_r($contactUsFormSubmission->getErrors(), true));
|
||||
}
|
||||
|
||||
|
||||
$contactUsFormSubmissionSaved = $this->ContactUsFormSubmissions->save($contactUsFormSubmission);
|
||||
|
||||
if ($contactUsFormSubmissionSaved) {
|
||||
return $this->_afterFormSaved($contactUsFormSubmissionSaved);
|
||||
}
|
||||
|
|
@ -140,7 +149,6 @@ class ContactUsComponent extends Component
|
|||
protected function _afterFormSaved(EntityInterface $contactUsFormSubmissionSaved)
|
||||
{
|
||||
$message = __d('cake_contact_us', 'Message received, thank you. We will be in touch soon.');
|
||||
|
||||
$event = $this->getController()->dispatchEvent(CakeContactUsPlugin::EVENT_AFTER_CONTACT_US_FORM_SAVED, [
|
||||
'contactUsFormSubmission' => $contactUsFormSubmissionSaved,
|
||||
], $this->getController());
|
||||
|
|
|
|||
|
|
@ -73,8 +73,7 @@ class ContactUsFormSubmissionsTable extends Table
|
|||
$validator
|
||||
->scalar('client_ip')
|
||||
->maxLength('client_ip', 45)
|
||||
->requirePresence('client_ip', 'create')
|
||||
->notEmptyString('client_ip');
|
||||
->allowEmptyString('client_ip');
|
||||
|
||||
$validator
|
||||
->scalar('name')
|
||||
|
|
@ -84,7 +83,7 @@ class ContactUsFormSubmissionsTable extends Table
|
|||
|
||||
// email
|
||||
$validator->email('email');
|
||||
if ($fields['email']) {
|
||||
if ($fields['email'] ?? false) {
|
||||
$validator->notEmptyString('email');
|
||||
} else {
|
||||
$validator->allowEmptyString('email');
|
||||
|
|
@ -94,7 +93,7 @@ class ContactUsFormSubmissionsTable extends Table
|
|||
$validator
|
||||
->scalar('subject')
|
||||
->maxLength('subject', 255);
|
||||
if ($fields['subject']) {
|
||||
if ($fields['subject'] ?? false) {
|
||||
$validator->notEmptyString('subject');
|
||||
} else {
|
||||
$validator->allowEmptyString('subject');
|
||||
|
|
@ -129,7 +128,7 @@ class ContactUsFormSubmissionsTable extends Table
|
|||
}
|
||||
$now = DateTime::now()->format(CakeContactUsPlugin::CAKE_CONTACT_US_MYSQL_DATETIME);
|
||||
$updateData = [];
|
||||
if (Configure::read('ContactUs.email.confirmation.enabled') && isset($contactUsFormSubmission->email)) {
|
||||
if (Configure::read('ContactUs.fields.email.confirmation') && isset($contactUsFormSubmission->email)) {
|
||||
$mailer = Configure::read('ContactUs.email.mailerClass', 'CakeContactUs.ContactUsFormSubmissions');
|
||||
try {
|
||||
$this->getMailer($mailer)->send('confirmation', [$contactUsFormSubmission]);
|
||||
|
|
@ -139,7 +138,7 @@ class ContactUsFormSubmissionsTable extends Table
|
|||
}
|
||||
}
|
||||
|
||||
if (Configure::readOrFail('ContactUs.email.backend.enabled')) {
|
||||
if (Configure::readOrFail('ContactUs.fields.email.backend')) {
|
||||
$mailer = Configure::read('ContactUs.email.mailerClass', 'CakeContactUs.ContactUsFormSubmissions');
|
||||
try {
|
||||
$this->getMailer($mailer)->send('backend', [$contactUsFormSubmission]);
|
||||
|
|
|
|||
|
|
@ -8,9 +8,9 @@ $fields = \Cake\Core\Configure::readOrFail('ContactUs.fields');
|
|||
?>
|
||||
<?php
|
||||
echo $this->Form->control('name');
|
||||
echo $fields['email'] ? $this->Form->control('email', ['required' => true]) : '';
|
||||
echo $fields['subject'] ? $this->Form->control('subject', ['required' => true]) : '';
|
||||
echo isset($fields['email']) && $fields['email'] ? $this->Form->control('email', ['required' => true]) : '';
|
||||
echo isset($fields['subject']) && $fields['subject'] ? $this->Form->control('subject', ['required' => true]) : '';
|
||||
echo $this->Form->control('message');
|
||||
echo $fields['captcha'] ? $this->Captcha->render(['placeholder' => __('Please solve the riddle')]) : '';
|
||||
echo isset($fields['captcha']) && $fields['captcha'] ? $this->Captcha->render(['placeholder' => __('Please solve the riddle')]) : '';
|
||||
|
||||
?>
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,43 +1,112 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Test suite bootstrap for CakeContactUs.
|
||||
*
|
||||
* This function is used to find the location of CakePHP whether CakePHP
|
||||
* has been installed as a dependency of the plugin, or the plugin is itself
|
||||
* installed as a dependency of an application.
|
||||
*/
|
||||
$findRoot = function ($root) {
|
||||
do {
|
||||
$lastRoot = $root;
|
||||
$root = dirname($root);
|
||||
if (is_dir($root . '/vendor/cakephp/cakephp')) {
|
||||
return $root;
|
||||
}
|
||||
} while ($root !== $lastRoot);
|
||||
use Cake\Cache\Cache;
|
||||
use Cake\Chronos\Chronos;
|
||||
use Cake\Core\Configure;
|
||||
use Cake\Core\Plugin;
|
||||
use Cake\Database\Connection;
|
||||
use Cake\Datasource\ConnectionManager;
|
||||
use Cake\TestSuite\Fixture\SchemaLoader;
|
||||
use CakeContactUs\CakeContactUsPlugin;
|
||||
use Migrations\TestSuite\Migrator;
|
||||
use TestApp\Controller\AppController;
|
||||
|
||||
throw new Exception('Cannot find the root of the application, unable to run tests');
|
||||
};
|
||||
$root = $findRoot(__FILE__);
|
||||
unset($findRoot);
|
||||
|
||||
chdir($root);
|
||||
|
||||
require_once $root . '/vendor/autoload.php';
|
||||
|
||||
/**
|
||||
* Define fallback values for required constants and configuration.
|
||||
* To customize constants and configuration remove this require
|
||||
* and define the data required by your plugin here.
|
||||
*/
|
||||
require_once $root . '/vendor/cakephp/cakephp/tests/bootstrap.php';
|
||||
|
||||
if (file_exists($root . '/config/bootstrap.php')) {
|
||||
require $root . '/config/bootstrap.php';
|
||||
|
||||
return;
|
||||
if (!defined('DS')) {
|
||||
define('DS', DIRECTORY_SEPARATOR);
|
||||
}
|
||||
if (!defined('WINDOWS')) {
|
||||
if (DS === '\\' || substr(PHP_OS, 0, 3) === 'WIN') {
|
||||
define('WINDOWS', true);
|
||||
} else {
|
||||
define('WINDOWS', false);
|
||||
}
|
||||
}
|
||||
|
||||
define('PLUGIN_ROOT', dirname(__DIR__));
|
||||
define('ROOT', PLUGIN_ROOT . DS . 'tests' . DS . 'test_app');
|
||||
define('TMP', PLUGIN_ROOT . DS . 'tmp' . DS);
|
||||
define('LOGS', TMP . 'logs' . DS);
|
||||
define('CACHE', TMP . 'cache' . DS);
|
||||
define('APP', ROOT . DS . 'src' . DS);
|
||||
define('APP_DIR', 'src');
|
||||
define('CAKE_CORE_INCLUDE_PATH', PLUGIN_ROOT . '/vendor/cakephp/cakephp');
|
||||
define('CORE_PATH', CAKE_CORE_INCLUDE_PATH . DS);
|
||||
define('CAKE', CORE_PATH . APP_DIR . DS);
|
||||
|
||||
define('WWW_ROOT', PLUGIN_ROOT . DS . 'webroot' . DS);
|
||||
define('TESTS', __DIR__ . DS);
|
||||
define('CONFIG', TESTS . 'config' . DS);
|
||||
|
||||
ini_set('intl.default_locale', 'de-DE');
|
||||
|
||||
require PLUGIN_ROOT . '/vendor/autoload.php';
|
||||
require CORE_PATH . 'config/bootstrap.php';
|
||||
require CAKE . 'functions.php';
|
||||
|
||||
Configure::write('App', [
|
||||
'namespace' => 'TestApp',
|
||||
'encoding' => 'UTF-8',
|
||||
'paths' => [
|
||||
'testWebroot' => PLUGIN_ROOT . DS . 'tests' . DS . 'test_app' . DS . 'webroot' . DS,
|
||||
'webroot' => PLUGIN_ROOT . DS . 'webroot' . DS,
|
||||
'templates' => [
|
||||
PLUGIN_ROOT . DS . 'tests' . DS . 'test_app' . DS . 'templates' . DS,
|
||||
],
|
||||
],
|
||||
]);
|
||||
|
||||
Configure::write('debug', true);
|
||||
Configure::write('ContactUs', [
|
||||
'fields' => [
|
||||
'captcha' => false,
|
||||
'email' => [
|
||||
'confirmation' => false,
|
||||
'backend' => false,
|
||||
],
|
||||
],
|
||||
]);
|
||||
|
||||
$cache = [
|
||||
'default' => [
|
||||
'engine' => 'File',
|
||||
'path' => CACHE,
|
||||
],
|
||||
'_cake_translations_' => [
|
||||
'className' => 'File',
|
||||
'prefix' => 'crud_myapp_cake_core_',
|
||||
'path' => CACHE . 'persistent/',
|
||||
'serialize' => true,
|
||||
'duration' => '+10 seconds',
|
||||
],
|
||||
'_cake_model_' => [
|
||||
'className' => 'File',
|
||||
'prefix' => 'crud_my_app_cake_model_',
|
||||
'path' => CACHE . 'models/',
|
||||
'serialize' => 'File',
|
||||
'duration' => '+10 seconds',
|
||||
],
|
||||
];
|
||||
|
||||
Cache::setConfig($cache);
|
||||
|
||||
class_alias(AppController::class, 'App\Controller\AppController');
|
||||
|
||||
Plugin::getCollection()->add(new CakeContactUsPlugin());
|
||||
|
||||
Chronos::setTestNow(Chronos::now());
|
||||
|
||||
if (!getenv('DB_URL')) {
|
||||
putenv('DB_URL=sqlite:///:memory:');
|
||||
}
|
||||
|
||||
ConnectionManager::setConfig('test', [
|
||||
'className' => Connection::class,
|
||||
'url' => getenv('DB_URL') ?: null,
|
||||
'timezone' => 'UTC',
|
||||
'quoteIdentifiers' => false,
|
||||
'cacheMetadata' => true,
|
||||
]);
|
||||
|
||||
/**
|
||||
* Load schema from a SQL dump file.
|
||||
|
|
@ -49,7 +118,9 @@ if (file_exists($root . '/config/bootstrap.php')) {
|
|||
* using migrations to provide schema for your plugin,
|
||||
* and using \Migrations\TestSuite\Migrator to load schema.
|
||||
*/
|
||||
use Cake\TestSuite\Fixture\SchemaLoader;
|
||||
|
||||
// Load a schema dump file.
|
||||
(new SchemaLoader())->loadSqlFiles('tests/schema.sql', 'test');
|
||||
//(new SchemaLoader())->loadSqlFiles('tests/schema.sql', 'test');
|
||||
|
||||
|
||||
$migrator = new Migrator();
|
||||
$migrator->run(['plugin' => 'CakeContactUs']);
|
||||
|
|
|
|||
|
|
@ -0,0 +1 @@
|
|||
<?php
|
||||
|
|
@ -0,0 +1 @@
|
|||
<?php
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
<?php
|
||||
|
||||
namespace TestApp;
|
||||
|
||||
use Cake\Http\BaseApplication;
|
||||
use Cake\Http\MiddlewareQueue;
|
||||
use Cake\Routing\Middleware\RoutingMiddleware;
|
||||
use Cake\Routing\Route\DashedRoute;
|
||||
use Cake\Routing\RouteBuilder;
|
||||
|
||||
class Application extends BaseApplication {
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function bootstrap(): void {
|
||||
$this->addPlugin('CakeContactUs');
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function middleware(MiddlewareQueue $middleware): MiddlewareQueue {
|
||||
$middleware->add(new RoutingMiddleware($this));
|
||||
|
||||
return $middleware;
|
||||
}
|
||||
|
||||
public function routes(RouteBuilder $routes): void
|
||||
{
|
||||
parent::routes($routes); // TODO: Change the autogenerated stub
|
||||
$routes->setRouteClass(DashedRoute::class);
|
||||
|
||||
$routes->plugin('CakeContactUs', ['path' => '/cake-contact-us'], function (RouteBuilder $pluginRoutes):void {
|
||||
$pluginRoutes->fallbacks();
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
<?php
|
||||
|
||||
namespace TestApp\Controller;
|
||||
|
||||
use Cake\Controller\Controller;
|
||||
|
||||
class AppController extends Controller {
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function initialize(): void {
|
||||
parent::initialize();
|
||||
|
||||
$this->loadComponent('Flash');
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
<?php
|
||||
|
||||
namespace TestApp\View;
|
||||
|
||||
use Cake\View\View;
|
||||
|
||||
/**
|
||||
* @property \TinyAuth\View\Helper\AuthUserHelper $AuthUser
|
||||
*/
|
||||
class AppView extends View {
|
||||
}
|
||||
|
|
@ -0,0 +1,44 @@
|
|||
<?php
|
||||
|
||||
use Cake\Core\Configure;
|
||||
use Cake\Error\Debugger;
|
||||
|
||||
$this->layout = 'error';
|
||||
|
||||
if (Configure::read('debug')):
|
||||
$this->layout = 'dev_error';
|
||||
|
||||
$this->assign('title', $message);
|
||||
$this->assign('templateName', 'error500.ctp');
|
||||
|
||||
$this->start('file');
|
||||
?>
|
||||
<?php if (!empty($error->queryString)) : ?>
|
||||
<p class="notice">
|
||||
<strong>SQL Query: </strong>
|
||||
<?= h($error->queryString) ?>
|
||||
</p>
|
||||
<?php endif; ?>
|
||||
<?php if (!empty($error->params)) : ?>
|
||||
<strong>SQL Query Params: </strong>
|
||||
<?php Debugger::dump($error->params) ?>
|
||||
<?php endif; ?>
|
||||
<?php if ($error instanceof Error) : ?>
|
||||
<strong>Error in: </strong>
|
||||
<?= sprintf('%s, line %s', str_replace(ROOT, 'ROOT', $error->getFile()), $error->getLine()) ?>
|
||||
<?php endif; ?>
|
||||
<?php
|
||||
echo $this->element('auto_table_warning');
|
||||
|
||||
if (extension_loaded('xdebug')):
|
||||
xdebug_print_function_stack();
|
||||
endif;
|
||||
|
||||
$this->end();
|
||||
endif;
|
||||
?>
|
||||
<h2><?= __d('cake', 'An Internal Error Has Occurred') ?></h2>
|
||||
<p class="error">
|
||||
<strong><?= __d('cake', 'Error') ?>: </strong>
|
||||
<?= h($message) ?>
|
||||
</p>
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
<?php
|
||||
/**
|
||||
* @var \App\View\AppView $this
|
||||
*/
|
||||
?>
|
||||
<?= $this->fetch('content') ?>
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
<?php
|
||||
/**
|
||||
* @var \App\View\AppView $this
|
||||
*/
|
||||
?>
|
||||
<?= $this->fetch('content') ?>
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 943 B |
Binary file not shown.
|
After Width: | Height: | Size: 943 B |
Loading…
Reference in New Issue