diff --git a/config/app.example.php b/config/app.example.php index cbcf10d..4a65788 100644 --- a/config/app.example.php +++ b/config/app.example.php @@ -8,13 +8,13 @@ return [ 'email' => true, 'captcha' => true, ], - 'clientIpHeader' => '', + 'clientIpHeader' => null, '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' => 'bshipley@hipowered.dev', + 'to' => 'contact@hipowered.dev', 'cc' => '', // email string or array of emails 'bcc' => '', // email string or array of emails ], @@ -26,5 +26,18 @@ return [ 'controller' => 'ContactUsFormSubmissions', 'action' => 'view', ], + /** + * @link https://developers.cloudflare.com/turnstile/troubleshooting/testing/ + * + * secretKeys for testing: + * always passes = 1x0000000000000000000000000000000AA + * always fails = 2x0000000000000000000000000000000AA + * token already spent error 3x0000000000000000000000000000000AA + */ + 'turnstile' => [ + 'enabled' => true, + 'siteKey' => '{SITE_KEY_GOES_HERE}', // define in app_local or an .env variable - CLOUDFLARE_TURNSTILE_SITE_KEY + 'secretKey' => '{SECRET_GOES_HERE}', // define in app_local or an .env variable - CLOUDFLARE_TURNSTILE_SECRET_KEY + ], ], ]; diff --git a/src/CakeContactUsPlugin.php b/src/CakeContactUsPlugin.php index 6d93b31..d2e2534 100644 --- a/src/CakeContactUsPlugin.php +++ b/src/CakeContactUsPlugin.php @@ -18,18 +18,28 @@ class CakeContactUsPlugin extends BasePlugin { /** * @var string */ - public const EVENT_BEFORE_CONTACT_US_FORM_SAVED = 'ContactUs.Global.beforeRegister'; + public const EVENT_BEFORE_CONTACT_US_FORM_SAVED = 'ContactUs.Global.beforeSave'; /** * @var string */ - public const EVENT_AFTER_CONTACT_US_FORM_SAVED = 'ContactUs.Global.afterRegister'; + public const EVENT_AFTER_CONTACT_US_FORM_SAVED = 'ContactUs.Global.afterSave'; /** * @var string */ public const CAKE_CONTACT_US_MYSQL_DATETIME = 'Y-m-d H:i:s'; + /** + * @var string + */ + public const CAKE_CONTACT_US_CLOUDFLARE_TURNSTILE_SECRET_KEY = 'CLOUDFLARE_TURNSTILE_SECRET_KEY'; + + /** + * @var string + */ + public const CAKE_CONTACT_US_CLOUDFLARE_TURNSTILE_SITE_KEY = 'CLOUDFLARE_TURNSTILE_SITE_KEY'; + /** * Load all the plugin configuration and bootstrap logic. * diff --git a/src/Controller/Component/ContactUsComponent.php b/src/Controller/Component/ContactUsComponent.php index 890a907..0f18403 100644 --- a/src/Controller/Component/ContactUsComponent.php +++ b/src/Controller/Component/ContactUsComponent.php @@ -6,6 +6,7 @@ namespace CakeContactUs\Controller\Component; use Cake\Controller\Component; use Cake\Core\Configure; use Cake\Datasource\EntityInterface; +use Cake\Event\EventInterface; use Cake\Http\Response; use Cake\I18n\DateTime; use Cake\ORM\Table; @@ -37,6 +38,37 @@ class ContactUsComponent extends Component { */ protected array $components = ['Flash']; + /** + * Get the Controller callbacks this Component is interested in. + * + * Uses Conventions to map controller events to standard component + * callback method names. By defining one of the callback methods a + * component is assumed to be interested in the related event. + * + * Override this method if you need to add non-conventional event listeners. + * Or if you want components to listen to non-standard events. + * + * @return array + */ + public function implementedEvents(): array { + $eventMap = [ + 'Controller.initialize' => 'beforeFilter', + 'Controller.startup' => 'startup', + 'Controller.beforeRender' => 'beforeRender', + 'Controller.beforeRedirect' => 'beforeRedirect', + 'Controller.shutdown' => 'afterFilter', + CakeContactUsPlugin::EVENT_BEFORE_CONTACT_US_FORM_SAVED => 'beforeFormSaved', + ]; + $events = []; + foreach ($eventMap as $event => $method) { + if (method_exists($this, $method)) { + $events[$event] = $method; + } + } + + return $events; + } + /** * @param array $config * @throws \Exception @@ -49,13 +81,20 @@ class ContactUsComponent extends Component { $this->ContactUsFormSubmissions = TableRegistry::getTableLocator()->get('CakeContactUs.ContactUsFormSubmissions'); $requireCaptcha = Configure::readOrFail('ContactUs.fields.captcha'); + $useTurnstile = Configure::read('ContactUs.turnstile', [ + 'enabled' => false, + ]); $this->setConfig('redirectUrl', Configure::read('ContactUs.redirectUrl', '/')); $this->setConfig('addIdToRedirect', Configure::read('ContactUs.addIdToRedirect', false)); $this->setConfig('requireEmail', Configure::readOrFail('ContactUs.fields.email')); $this->setConfig('requireCaptcha', $requireCaptcha); + $this->setConfig('turnstile', $useTurnstile); if ($requireCaptcha) { $this->getController()->viewBuilder()->addHelpers(['Captcha.Captcha']); } + if ($useTurnstile['enabled'] ?? false) { +// $this->getController()->viewBuilder()->addHelpers(['Turnstile']); + } } /** @@ -78,46 +117,51 @@ class ContactUsComponent extends Component { if (!isset($postData)) { $postData = $this->getController()->getRequest()->getData(); } - $postData['client_ip'] = $postData['client_ip'] ?? - ($this->getConfig('clientIpHeader') ? - $this->getController()->getRequest()->getHeaderLine($this->getConfig('clientIpHeader')) : - $this->getController()->getRequest()->clientIp() - ); + $postData['client_ip'] = $this->_getIp(); $postData['submitted_at'] = DateTime::now(); + $contactUsFormSubmission = $this->ContactUsFormSubmissions->patchEntity($contactUsFormSubmission, $postData); + $this->getController()->set(['contactUsFormSubmission' => $contactUsFormSubmission]); $event = $this->getController()->dispatchEvent(CakeContactUsPlugin::EVENT_BEFORE_CONTACT_US_FORM_SAVED, [ 'contactUsFormSubmission' => $contactUsFormSubmission, ], $this->getController()); $result = $event->getResult(); - if ($result instanceof EntityInterface) { - $postData = $result->toArray(); - $contactUsFormSubmission = $this->ContactUsFormSubmissions->patchEntity($contactUsFormSubmission, $postData); - $contactUsFormSubmissionSaved = $this->ContactUsFormSubmissions->save($contactUsFormSubmission); - if ($contactUsFormSubmissionSaved) { - return $this->_afterFormSaved($contactUsFormSubmissionSaved); - } - // @TODO contact us form submission failed - handle here - } - if ($event->isStopped()) { - return $this->getController()->redirect($event->getResult()); - } - if (!$postData) { + if ($result['redirect'] ?? false) { + return $this->getController()->redirect($result['redirect']); + } + $message = __d('cake_contact_us', 'Verification failed. Please try again.'); + + $this->Flash->error($message); + return; } - $contactUsFormSubmission = $this->ContactUsFormSubmissions->patchEntity($contactUsFormSubmission, $postData); - $contactUsFormSubmissionSaved = $this->ContactUsFormSubmissions->save($contactUsFormSubmission); + if ($result instanceof EntityInterface) { + $final = $this->_finishSave($contactUsFormSubmission); + if ($final instanceof EntityInterface) { // validation failed + $this->getController()->set(['contactUsFormSubmission' => $final]); - if ($contactUsFormSubmissionSaved) { - return $this->_afterFormSaved($contactUsFormSubmissionSaved); + return; + } + + return $final; } - $message = __d('cake_contact_us', 'Something doesn\'t look quite right. Please try again.'); - $this->Flash->error($message); + $final = $this->_finishSave($contactUsFormSubmission); + if ($final instanceof EntityInterface) { + // @TODO contact us form submission failed - handle here + $message = __d('cake_contact_us', 'Something doesn\'t look quite right. Please try again.'); - // @TODO contact us form submission failed - handle here - $this->getController()->set(['contactUsFormSubmission' => $contactUsFormSubmission]); + $this->Flash->error($message); + + // @TODO contact us form submission failed - handle here + $this->getController()->set(['contactUsFormSubmission' => $final]); + + return; + } + + return $final; } /** @@ -145,4 +189,112 @@ class ContactUsComponent extends Component { return $this->getController()->redirect($redirectUrl); } + /** + * @return void + */ + public function beforeFormSaved(EventInterface $event) { + if (!$this->getConfig('turnstile')) { + return; + } + $siteKey = getenv(CakeContactUsPlugin::CAKE_CONTACT_US_CLOUDFLARE_TURNSTILE_SITE_KEY) ?: $this->getConfig('turnstile.siteKey'); + $secretKey = getenv(CakeContactUsPlugin::CAKE_CONTACT_US_CLOUDFLARE_TURNSTILE_SECRET_KEY) ?: $this->getConfig('turnstile.secretKey'); + if (!$siteKey || !$secretKey) { + $event->stopPropagation(); + $event->setResult([ + 'success' => false, + 'error-codes' => ['missing-turnstile-site-configuration'], + ]); + + return; + } + $entity = $event->getData('contactUsFormSubmission'); + $token = $entity->get('cf-turnstile-response') ?: $this->getController()->getRequest()->getData('cf-turnstile-response'); + if (!$token) { + $event->stopPropagation(); + $event->setResult([ + 'success' => false, + 'error-codes' => ['missing-token'], + ]); + + return; + } + $turnstileValidation = $this->_validateTurnstile( + $token, + $entity->get('client_ip') ?? $this->_getIp(), + ); + if ($turnstileValidation['success'] ?? false) { + return; // good to proceed + } + $event->stopPropagation(); + $event->setResult($turnstileValidation); + } + + /** + * @return string - + */ + protected function _getIp() { + if ($this->getConfig('clientIpHeader')) { + return $this->getController()->getRequest()->getHeaderLine($this->getConfig('clientIpHeader')) + ?: $this->getController()->getRequest()->clientIp(); + } + + return $this->getController()->getRequest()->clientIp(); + } + + /** + * @param \Cake\Datasource\EntityInterface $contactUsFormSubmission + * + * @return \Cake\Datasource\EntityInterface|\Cake\Http\Response + */ + protected function _finishSave(EntityInterface $contactUsFormSubmission) { + $contactUsFormSubmissionSaved = $this->ContactUsFormSubmissions->save($contactUsFormSubmission); + + if ($contactUsFormSubmissionSaved) { + return $this->_afterFormSaved($contactUsFormSubmissionSaved); + } + + return $contactUsFormSubmission; // has errors - return entity + } + + /** + * @param string $token + * @param string|null $remoteIp + * + * @return array + */ + protected function _validateTurnstile(string $token, string|null $remoteIp = null): array { +// if (PHP_SAPI === 'cli') { +// return ['success' => true]; +// } + $url = 'https://challenges.cloudflare.com/turnstile/v0/siteverify'; + $data = [ + 'secret' => $this->getConfig('turnstile.secretKey'), + 'response' => $token, + ]; + + if ($remoteIp) { + $data['remoteip'] = $remoteIp; + } + + $ch = curl_init($url); + curl_setopt_array($ch, [ + CURLOPT_POST => true, + CURLOPT_POSTFIELDS => http_build_query($data), + CURLOPT_HTTPHEADER => ['Content-Type: application/x-www-form-urlencoded'], + CURLOPT_RETURNTRANSFER => true, + CURLOPT_CONNECTTIMEOUT => 5, + CURLOPT_TIMEOUT => 10, + ]); + + $response = curl_exec($ch); + $error = curl_error($ch); + curl_close($ch); + + if ($response === false || $error !== '') { + return ['success' => false, 'error-codes' => ['internal-error']]; + } + + return json_decode($response, true); + } + } diff --git a/src/View/Helper/TurnstileHelper.php b/src/View/Helper/TurnstileHelper.php new file mode 100644 index 0000000..2cb2aed --- /dev/null +++ b/src/View/Helper/TurnstileHelper.php @@ -0,0 +1,147 @@ + + */ + protected array $_defaultConfig = []; + + /** + * @var array|string[] + */ + protected array $helpers = ['Html']; + + /** + * @param \Cake\View\View $view + * @param array $config + */ + public function __construct(View $view, array $config = []) { + $config += (array)Configure::read('ContactUs.turnstile'); + + parent::__construct($view, $config); + } + + /** + * @return string + */ + public function preConnect() { + return ''; + } + + /** + * @param array $options + * + * @return string|null + */ + public function script(array $options = []) { + $options += [ + 'async' => true, + 'defer' => true, + ]; + $scriptOptions = $options; + unset($scriptOptions['eventListeners']); + + if ($options['eventListeners'] ?? false) { + return $this->Html->script('https://challenges.cloudflare.com/turnstile/v0/api.js', $scriptOptions) . $this->eventListener($options['eventListeners']); + } + + return $this->Html->script('https://challenges.cloudflare.com/turnstile/v0/api.js', $scriptOptions); + } + + /** + * @param array $options + * + * @return string + */ + public function eventListener(array $options) { + $options += [ + 'dynamicButton' => $this->getConfig('dynamicButton', false), + 'log' => $this->getConfig('log', false), + 'submitButtonId' => $this->getConfig('submitButtonId', 'submit-btn'), + ]; + $final = ''; + + return $final; + } + + /** + * @param array $options + * + * @return string + */ + protected function _onTurnstileSuccess(array $options = []): string { + $jsFunction = 'function onTurnstileSuccess(token) {'; + if ($options['log'] ?? false) { + $jsFunction .= 'console.log("Turnstile success:", token);'; + } + if ($options['dynamicButton'] ?? false) { + $jsFunction .= 'document.getElementById("' + . ($options['submitButtonId'] ?? 'submit-btn') + . '")?.disabled = false;'; + } + $jsFunction .= '}'; + + return $jsFunction; + } + + /** + * @param array $options + * + * @return string + */ + protected function onTurnstileError(array $options = []): string { + $jsFunction = 'function onTurnstileError(errorCode) {'; + if ($options['log'] ?? false) { + $jsFunction .= 'console.error("Turnstile error:", errorCode);'; + } + if ($options['dynamicButton'] ?? false) { + $jsFunction .= 'document.getElementById("' + . ($options['submitButtonId'] ?? 'submit-btn') + . '")?.disabled = true;'; + } + $jsFunction .= '}'; + + return $jsFunction; + } + + /** + * @param array $options + * + * @return string + */ + protected function onTurnstileExpired(array $options = []): string { + $jsFunction = 'function onTurnstileExpired(errorCode) {'; + if ($options['log'] ?? false) { + $jsFunction .= 'console.warn("Turnstile token expired");'; + } + if ($options['dynamicButton'] ?? false) { + $jsFunction .= 'document.getElementById("' + . ($options['submitButtonId'] ?? 'submit-btn') + . '")?.disabled = true;'; + } + + $jsFunction .= '}'; + + return $jsFunction; + } + +} diff --git a/tests/TestCase/Controller/ContactUsFormSubmissionsControllerTest.php b/tests/TestCase/Controller/ContactUsFormSubmissionsControllerTest.php index abb67f3..02fa224 100644 --- a/tests/TestCase/Controller/ContactUsFormSubmissionsControllerTest.php +++ b/tests/TestCase/Controller/ContactUsFormSubmissionsControllerTest.php @@ -6,7 +6,6 @@ namespace CakeContactUs\Test\TestCase\Controller; use Cake\Core\Configure; use Cake\Event\EventList; use Cake\Event\EventManager; -use Cake\Mailer\Transport\DebugTransport; use Cake\TestSuite\EmailTrait; use Cake\TestSuite\IntegrationTestTrait; use Cake\TestSuite\TestCase; @@ -89,7 +88,7 @@ class ContactUsFormSubmissionsControllerTest extends TestCase { * * Tests a POST request to the add action with a logged in user * - * @throws Exception + * @throws \Exception * * @return void */ @@ -110,6 +109,7 @@ class ContactUsFormSubmissionsControllerTest extends TestCase { $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(); } @@ -119,28 +119,92 @@ class ContactUsFormSubmissionsControllerTest extends TestCase { * * Tests a POST request to the add action with a logged in user * - * @throws Exception + * @throws \Exception + * + * @return void + */ + public function testAddPostSuccessWithTurnstile(): void { + $cntBefore = $this->ContactUsFormSubmissions->find()->count(); + Configure::write('ContactUs.turnstile', [ + '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.turnstile', [ + '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); + $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, - ], - '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('ContactUs.fields', [ + 'captcha' => false, + 'email' => true, ]); - Configure::write('EmailTransport', [ - 'default' => [ - 'className' => DebugTransport::class, + 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', ], ]); @@ -161,6 +225,7 @@ class ContactUsFormSubmissionsControllerTest extends TestCase { $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 @@ -171,23 +236,21 @@ class ContactUsFormSubmissionsControllerTest extends TestCase { * * Tests a POST request to the add action with a logged in user * - * @throws Exception + * @throws \Exception * * @return void */ 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', - ], + Configure::write('ContactUs.fields', [ + 'captcha' => false, + 'email' => true, + ]); + 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(); @@ -207,6 +270,7 @@ class ContactUsFormSubmissionsControllerTest extends TestCase { $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 @@ -217,23 +281,17 @@ class ContactUsFormSubmissionsControllerTest extends TestCase { * * Tests a POST request to the add action with a logged in user * - * @throws Exception + * @throws \Exception * * @return void */ 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', - ], + 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', ], ]); @@ -259,6 +317,7 @@ class ContactUsFormSubmissionsControllerTest extends TestCase { $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 @@ -269,7 +328,7 @@ class ContactUsFormSubmissionsControllerTest extends TestCase { * * Tests a POST request to the add action with a logged in user * - * @throws Exception + * @throws \Exception * * @return void */ @@ -289,7 +348,6 @@ class ContactUsFormSubmissionsControllerTest extends TestCase { ]; $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); diff --git a/tests/TestCase/View/Helper/TurnstileHelperTest.php b/tests/TestCase/View/Helper/TurnstileHelperTest.php new file mode 100644 index 0000000..97bb40b --- /dev/null +++ b/tests/TestCase/View/Helper/TurnstileHelperTest.php @@ -0,0 +1,95 @@ +Turnstile = new TurnstileHelper($view); + } + + /** + * tearDown method + * + * @return void + */ + protected function tearDown(): void { + unset($this->Turnstile); + + parent::tearDown(); + } + + /** + * @return void + */ + public function testPreConnect(): void { + $this->assertStringContainsString( + '', + $this->Turnstile->preConnect(), + ); + } + + /** + * @return void + */ + public function testScriptNoEventListener(): void { + $result = $this->Turnstile->script(); + $this->assertStringContainsString( + '