Skip to content

Commit

Permalink
Merge pull request #3267 from michalsn/honeypot_updates
Browse files Browse the repository at this point in the history
Use named variable in honeypot container
  • Loading branch information
michalsn authored Jul 11, 2020
2 parents f5d899c + 3b26e60 commit 3508f0f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 deletions.
16 changes: 8 additions & 8 deletions app/Config/Honeypot.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,7 @@ class Honeypot extends BaseConfig
* @var boolean
*/
public $hidden = true;

/**
* Div wrapper of honeypot.
*
* @var string
*/
public $container = '<div style="display:none">%s</div>';


/**
* Honeypot Label Content
*
Expand All @@ -39,4 +32,11 @@ class Honeypot extends BaseConfig
* @var string
*/
public $template = '<label>{label}</label><input type="text" name="{name}" value=""/>';

/**
* Honeypot container
*
* @var string
*/
public $container = '<div style="display:none">{template}</div>';
}
9 changes: 5 additions & 4 deletions system/Honeypot/Honeypot.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<div style="display:none">%s</div>';
$this->config->container = '<div style="display:none">{template}</div>';
}

if ($this->config->template === '')
Expand Down Expand Up @@ -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;
}

Expand Down
16 changes: 16 additions & 0 deletions tests/system/Honeypot/HoneypotTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,22 @@ public function testAttachHoneypot()

//--------------------------------------------------------------------

public function testAttachHoneypotAndContainer()
{
$this->response->setBody('<form></form>');
$this->honeypot->attachHoneypot($this->response);
$expected = '<form><div style="display:none"><label>Fill This Field</label><input type="text" name="honeypot" value=""/></div></form>';
$this->assertEquals($expected, $this->response->getBody());

$this->config->container = '<div class="hidden">{template}</div>';
$this->response->setBody('<form></form>');
$this->honeypot->attachHoneypot($this->response);
$expected = '<form><div class="hidden"><label>Fill This Field</label><input type="text" name="honeypot" value=""/></div></form>';
$this->assertEquals($expected, $this->response->getBody());
}

//--------------------------------------------------------------------

public function testHasntContent()
{
unset($_POST[$this->config->name]);
Expand Down

0 comments on commit 3508f0f

Please sign in to comment.