Skip to content

Commit

Permalink
Merge ff3d587 into 08ba081
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan committed Jan 22, 2018
2 parents 08ba081 + ff3d587 commit 44e79cc
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 64 deletions.
29 changes: 10 additions & 19 deletions .travis.yml
@@ -1,30 +1,21 @@
language: php

php:
- 5.3
- 5.4
- 5.5
- 5.6
- 7.0
- hhvm

# This triggers builds to run on the new TravisCI infrastructure.
# See: http://docs.travis-ci.com/user/workers/container-based-infrastructure/
sudo: false

matrix:
allow_failures:
- php: 7.0
include:
- php: 5.3
env: 'COMPOSER_FLAGS="--prefer-stable --prefer-lowest"'
- php: 5.6
env: DB=MYSQL
- php: 7.1
env: DB=MYSQL

before_script:
- travis_retry composer self-update
- travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-source
- phpenv rehash

- composer validate
- composer require --no-update silverstripe/recipe-cms:1.0.x-dev
- composer install --prefer-dist --no-interaction --no-progress --no-suggest --optimize-autoloader --verbose --profile

script:
- vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover

after_script:
- if [[ $TRAVIS_PHP_VERSION != 'hhvm' && $TRAVIS_PHP_VERSION != '7.0' ]]; then php vendor/bin/ocular code-coverage:upload --format=php-clover coverage.clover; fi
- php vendor/bin/ocular code-coverage:upload --format=php-clover coverage.clover
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -40,7 +40,7 @@ Create a configuration file `spamprotection.yml` in `mysite/_config` with the fo
---
name: spamprotection
---
FormSpamProtectionExtension:
SilverStripe\SpamProtection\Extension\FormSpamProtectionExtension:
default_spam_protector: '\StudioBonito\SilverStripe\SpamProtection\Honeypot\SpamProtector\HoneypotSpamProtector'
```

Expand Down
21 changes: 13 additions & 8 deletions composer.json
@@ -1,7 +1,7 @@
{
"name": "studiobonito/silverstripe-spamprotection-honeypot",
"description": "Provide Honeypot spam protection for SilverStripe CMS.",
"type": "silverstripe-module",
"type": "silverstripe-vendormodule",
"keywords": [
"studiobonito",
"silverstripe-spamprotection-honeypot",
Expand All @@ -22,14 +22,12 @@
}
],
"require": {
"php": ">=5.3.0",
"silverstripe/framework": "^3.1",
"silverstripe/spamprotection": "~1.2.0"
"silverstripe/framework": "~4.0",
"silverstripe/spamprotection": "~3.0"
},
"require-dev": {
"phpunit/phpunit": "4.*",
"mockery/mockery": "0.9.*",
"symfony/class-loader": "~2.3",
"phpunit/phpunit": "~5.0",
"mockery/mockery": "~1.0",
"scrutinizer/ocular": "~1.1"
},
"autoload": {
Expand All @@ -41,5 +39,12 @@
"psr-4": {
"StudioBonito\\SilverStripe\\SpamProtection\\Honeypot\\Tests\\": "tests"
}
}
},
"extra": {
"branch-alias": {
"dev-master": "2.0.x-dev"
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
2 changes: 1 addition & 1 deletion phpunit.xml.dist
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="tests/bootstrap.php"
<phpunit bootstrap="vendor/silverstripe/framework/tests/bootstrap.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
Expand Down
50 changes: 43 additions & 7 deletions src/FormField/HoneypotField.php
@@ -1,8 +1,11 @@
<?php namespace StudioBonito\SilverStripe\SpamProtection\Honeypot\FormField;

use FormField;
use SilverStripe\Core\Config\Config;
use SilverStripe\Forms\TextField;
use SilverStripe\View\HTML;
use SilverStripe\ORM\FieldType\DBHTMLText;

class HoneypotField extends \HiddenField
class HoneypotField extends TextField
{
/**
* The number of seconds before you can submit a valid request.
Expand Down Expand Up @@ -41,6 +44,19 @@ public function validate($validator)
return true;
}

/**
* Since this isn't a hidden field, the title will continue to show in the form.
* This prevents that from happening, since a hidden field will not show the validation message.
*
* @codeCoverageIgnore
*
* @return string
*/
public function Title()
{
return '';
}

/**
* Override the Type to remove the class namespace.
*
Expand All @@ -64,7 +80,9 @@ public function Type()
*/
public function Field($properties = array())
{
return $this->createHoneypotField() . $this->createTimestampField();
$field = DBHTMLText::create($this->getName());
$field->setValue($this->createHoneypotField() . $this->createTimestampField());
return $field;
}

/**
Expand All @@ -76,14 +94,14 @@ public function Field($properties = array())
*/
protected function createHoneypotField()
{
return FormField::create_tag(
return HTML::createTag(
'input',
array(
'type' => 'text',
'id' => $this->ID(),
'name' => $this->getName(),
'value' => $this->Value(),
'style' => 'display:none!important',
'style' => $this->getFieldStyle(),
)
);
}
Expand All @@ -97,15 +115,33 @@ protected function createHoneypotField()
*/
protected function createTimestampField()
{
return FormField::create_tag(
return HTML::createTag(
'input',
array(
'type' => 'text',
'id' => $this->ID() . '_Timestamp',
'name' => $this->getName() . '_Timestamp',
'value' => time(),
'style' => 'display:none!important',
'style' => $this->getFieldStyle(),
)
);
}

/**
* Return a configured style rule for the fields, if none is configured use a default display:none rule
*
* @codeCoverageIgnore
*
* @return string
*/
public function getFieldStyle()
{
$default_css_rule = 'display:none!important';
$css_rule = Config::inst()->get(__CLASS__, 'field_style_rule');
if (!$css_rule) {
return $default_css_rule;
} else {
return $css_rule;
}
}
}
3 changes: 2 additions & 1 deletion src/SpamProtector/HoneypotSpamProtector.php
@@ -1,8 +1,9 @@
<?php namespace StudioBonito\SilverStripe\SpamProtection\Honeypot\SpamProtector;

use StudioBonito\SilverStripe\SpamProtection\Honeypot\FormField\HoneypotField;
use SilverStripe\SpamProtection\SpamProtector;

class HoneypotSpamProtector implements \SpamProtector
class HoneypotSpamProtector implements SpamProtector
{
/**
* Return the {@link FormField} associated with this protector.
Expand Down
44 changes: 39 additions & 5 deletions tests/HoneypotFieldTest.php
Expand Up @@ -2,9 +2,17 @@

use Mockery as m;
use StudioBonito\SilverStripe\SpamProtection\Honeypot\FormField\HoneypotField;
use SilverStripe\Core\Config\Config;
use SilverStripe\Control\Controller;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\RequiredFields;
use SilverStripe\Dev\SapphireTest;

class HoneypotFieldTest extends \PHPUnit_Framework_TestCase
class HoneypotFieldTest extends SapphireTest
{

protected $usesDatabase = true;

public function tearDown()
{
m::close();
Expand Down Expand Up @@ -43,6 +51,32 @@ public function testValidWithEmptyCaptcha()

$this->assertTrue($valid);
}

public function testCustomStyleAttribute()
{
$styleRule = "position:absolute!important;left:-9000px!important;";
Config::modify()->set('StudioBonito\SilverStripe\SpamProtection\Honeypot\FormField\HoneypotField', 'field_style_rule', $styleRule);

$honeypotField = new HoneypotField('Captcha');
$honeypotField->setValue(null);

$styleAttribute = $honeypotField->getFieldStyle();

$this->assertEquals($styleRule, $styleAttribute);
}

public function testDefaultStyleAttribute()
{
$defaultStyleRule = 'display:none!important';
Config::inst()->remove('StudioBonito\SilverStripe\SpamProtection\Honeypot\FormField\HoneypotField', 'field_style_rule');

$honeypotField = new HoneypotField('Captcha');
$honeypotField->setValue(null);

$styleAttribute = $honeypotField->getFieldStyle();

$this->assertEquals($defaultStyleRule, $styleAttribute);
}

/**
* @return m\MockInterface
Expand All @@ -52,14 +86,14 @@ protected function getForm()
$request = m::mock('Request');

$request->shouldReceive('postVar')
->andReturn(time() + 10);
->andReturn(time() - 10);

$controller = m::mock('Controller');
$controller = m::mock(Controller::class);

$controller->shouldReceive('getRequest')
->andReturn($request);

$form = m::mock('Form');
$form = m::mock(Form::class);

$form->shouldReceive('getController')
->andReturn($controller);
Expand All @@ -72,7 +106,7 @@ protected function getForm()
*/
protected function getValidator()
{
$validator = m::mock('RequiredFields')
$validator = m::mock(RequiredFields::class)
->shouldReceive('validationError')
->getMock();

Expand Down
22 changes: 0 additions & 22 deletions tests/bootstrap.php

This file was deleted.

0 comments on commit 44e79cc

Please sign in to comment.