This commit is contained in:
2025-11-10 22:26:52 -08:00
parent 77f09055c9
commit a659368867
19 changed files with 301 additions and 358 deletions

View File

@@ -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);
}
}

View File

@@ -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']);

View File

@@ -0,0 +1 @@
<?php

1
tests/config/routes.php Normal file
View File

@@ -0,0 +1 @@
<?php

View File

@@ -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();
});
}
}

View File

@@ -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');
}
}

View File

@@ -0,0 +1,11 @@
<?php
namespace TestApp\View;
use Cake\View\View;
/**
* @property \TinyAuth\View\Helper\AuthUserHelper $AuthUser
*/
class AppView extends View {
}

View File

@@ -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>

View File

@@ -0,0 +1,6 @@
<?php
/**
* @var \App\View\AppView $this
*/
?>
<?= $this->fetch('content') ?>

View File

@@ -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