From 3b26e601ab31125a9506a8b57337e0e1b844eb54 Mon Sep 17 00:00:00 2001 From: michalsn Date: Sat, 11 Jul 2020 07:20:02 +0200 Subject: [PATCH] Use named variable in honeypot container --- app/Config/Honeypot.php | 16 ++++++++-------- system/Honeypot/Honeypot.php | 9 +++++---- tests/system/Honeypot/HoneypotTest.php | 16 ++++++++++++++++ 3 files changed, 29 insertions(+), 12 deletions(-) diff --git a/app/Config/Honeypot.php b/app/Config/Honeypot.php index 3810626dea11..3d9e37266934 100644 --- a/app/Config/Honeypot.php +++ b/app/Config/Honeypot.php @@ -11,14 +11,7 @@ class Honeypot extends BaseConfig * @var boolean */ public $hidden = true; - - /** - * Div wrapper of honeypot. - * - * @var string - */ - public $container = '
%s
'; - + /** * Honeypot Label Content * @@ -39,4 +32,11 @@ class Honeypot extends BaseConfig * @var string */ public $template = ''; + + /** + * Honeypot container + * + * @var string + */ + public $container = '
{template}
'; } diff --git a/system/Honeypot/Honeypot.php b/system/Honeypot/Honeypot.php index 4036dfe0ac71..b944395984c4 100644 --- a/system/Honeypot/Honeypot.php +++ b/system/Honeypot/Honeypot.php @@ -72,10 +72,10 @@ function __construct(BaseConfig $config) { throw HoneypotException::forNoHiddenValue(); } - - if (empty($this->config->container) || strpos($this->config->container, '%s') === false) + + if (empty($this->config->container) || strpos($this->config->container, '{template}') === false) { - $this->config->container = '
%s
'; + $this->config->container = '
{template}
'; } if ($this->config->template === '') @@ -129,8 +129,9 @@ protected function prepareTemplate(string $template): string if ($this->config->hidden) { - $template = sprintf($this->config->container, $template); + $template = str_ireplace('{template}', $template, $this->config->container); } + return $template; } diff --git a/tests/system/Honeypot/HoneypotTest.php b/tests/system/Honeypot/HoneypotTest.php index 9d39f1a338ec..f2edd3647427 100644 --- a/tests/system/Honeypot/HoneypotTest.php +++ b/tests/system/Honeypot/HoneypotTest.php @@ -47,6 +47,22 @@ public function testAttachHoneypot() //-------------------------------------------------------------------- + public function testAttachHoneypotAndContainer() + { + $this->response->setBody('
'); + $this->honeypot->attachHoneypot($this->response); + $expected = '
'; + $this->assertEquals($expected, $this->response->getBody()); + + $this->config->container = ''; + $this->response->setBody('
'); + $this->honeypot->attachHoneypot($this->response); + $expected = '
'; + $this->assertEquals($expected, $this->response->getBody()); + } + + //-------------------------------------------------------------------- + public function testHasntContent() { unset($_POST[$this->config->name]);