diff --git a/config/app.example.php b/config/app.example.php index 6a2c6b6..4a65788 100644 --- a/config/app.example.php +++ b/config/app.example.php @@ -26,7 +26,7 @@ return [ 'controller' => 'ContactUsFormSubmissions', 'action' => 'view', ], - /** + /** * @link https://developers.cloudflare.com/turnstile/troubleshooting/testing/ * * secretKeys for testing: @@ -35,7 +35,7 @@ return [ * token already spent error 3x0000000000000000000000000000000AA */ 'turnstile' => [ - 'enabled' => true, + '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 f35aa30..d2e2534 100644 --- a/src/CakeContactUsPlugin.php +++ b/src/CakeContactUsPlugin.php @@ -30,15 +30,15 @@ class CakeContactUsPlugin extends BasePlugin { */ 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'; + 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'; + 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 2195ada..0f18403 100644 --- a/src/Controller/Component/ContactUsComponent.php +++ b/src/Controller/Component/ContactUsComponent.php @@ -82,8 +82,8 @@ class ContactUsComponent extends Component { $requireCaptcha = Configure::readOrFail('ContactUs.fields.captcha'); $useTurnstile = Configure::read('ContactUs.turnstile', [ - 'enabled' => false, - ]); + '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')); @@ -196,8 +196,8 @@ class ContactUsComponent extends Component { 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'); + $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([ diff --git a/src/View/Helper/TurnstileHelper.php b/src/View/Helper/TurnstileHelper.php index cece4a0..2cb2aed 100644 --- a/src/View/Helper/TurnstileHelper.php +++ b/src/View/Helper/TurnstileHelper.php @@ -12,141 +12,136 @@ use Cake\View\View; * * @property \Cake\View\Helper\HtmlHelper $Html */ -class TurnstileHelper extends Helper -{ - /** +class TurnstileHelper extends Helper { + + /** * Default configuration. * * @var array */ - protected array $_defaultConfig = []; + protected array $_defaultConfig = []; - /** + /** * @var array|string[] */ - protected array $helpers = ['Html']; + 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'); + public function __construct(View $view, array $config = []) { + $config += (array)Configure::read('ContactUs.turnstile'); - parent::__construct($view, $config); - } + parent::__construct($view, $config); + } - /** + /** * @return string */ - public function preConnect() - { - return ''; - } + 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']); + 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); - } + if ($options['eventListeners'] ?? false) { + return $this->Html->script('https://challenges.cloudflare.com/turnstile/v0/api.js', $scriptOptions) . $this->eventListener($options['eventListeners']); + } - /** - * @param $options + return $this->Html->script('https://challenges.cloudflare.com/turnstile/v0/api.js', $scriptOptions); + } + + /** + * @param array $options * * @return string */ - public function eventListener($options) - { - $options += [ - 'dynamicButton' => $this->getConfig('dynamicButton', false), - 'log' => $this->getConfig('log', false), - 'submitButtonId' => $this->getConfig('submitButtonId', 'submit-btn') - ]; - $final = ''; + 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; - } + return $final; + } - /** - * @param $options + /** + * @param array $options * * @return string */ - protected function _onTurnstileSuccess($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 .= '}'; + 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; - } + return $jsFunction; + } - /** - * @param $options + /** + * @param array $options * * @return string */ - protected function onTurnstileError($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 .= '}'; + 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; - } + return $jsFunction; + } - /** - * @param $options + /** + * @param array $options * * @return string */ - protected function onTurnstileExpired($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;'; - } + 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 .= '}'; + $jsFunction .= '}'; - return $jsFunction; - } + return $jsFunction; + } } diff --git a/tests/TestCase/View/Helper/TurnstileHelperTest.php b/tests/TestCase/View/Helper/TurnstileHelperTest.php index b5690d0..97bb40b 100644 --- a/tests/TestCase/View/Helper/TurnstileHelperTest.php +++ b/tests/TestCase/View/Helper/TurnstileHelperTest.php @@ -3,98 +3,93 @@ declare(strict_types=1); namespace CakeContactUs\Test\TestCase\View\Helper; -use CakeContactUs\View\Helper\TurnstileHelper; use Cake\TestSuite\TestCase; use Cake\View\View; +use CakeContactUs\View\Helper\TurnstileHelper; /** * App\View\Helper\TurnstileHelper Test Case */ -class TurnstileHelperTest extends TestCase -{ - /** +class TurnstileHelperTest extends TestCase { + + /** * Test subject * * @var \CakeContactUs\View\Helper\TurnstileHelper */ - protected $Turnstile; + protected $Turnstile; - /** + /** * setUp method * * @return void */ - protected function setUp(): void - { - parent::setUp(); - $view = new View(); - $this->Turnstile = new TurnstileHelper($view); - } + protected function setUp(): void { + parent::setUp(); + $view = new View(); + $this->Turnstile = new TurnstileHelper($view); + } - /** + /** * tearDown method * * @return void */ - protected function tearDown(): void - { - unset($this->Turnstile); + protected function tearDown(): void { + unset($this->Turnstile); - parent::tearDown(); - } + parent::tearDown(); + } - /** + /** * @return void */ - public function testPreConnect(): void - { - $this->assertStringContainsString( - '', - $this->Turnstile->preConnect() - ); - } + public function testPreConnect(): void { + $this->assertStringContainsString( + '', + $this->Turnstile->preConnect(), + ); + } - /** + /** * @return void */ - public function testScriptNoEventListener(): void - { - $result = $this->Turnstile->script(); - $this->assertStringContainsString( - '