diff --git a/.travis.yml b/.travis.yml index 0456f64..8c4f483 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,36 +1,40 @@ -# See https://github.com/silverstripe-labs/silverstripe-travis-support for setup details - -sudo: false - language: php -php: - - 5.3 - - 5.4 - - 5.5 - - 5.6 - - 7.0 +dist: trusty + +cache: + directories: + - $HOME/.composer/cache/files env: - - DB=MYSQL CORE_RELEASE=3.2 + global: + - COMPOSER_ROOT_VERSION=4.0.x-dev matrix: include: - php: 5.6 - env: DB=MYSQL CORE_RELEASE=3 - - php: 5.6 - env: DB=MYSQL CORE_RELEASE=3.1 - - php: 5.6 - env: DB=PGSQL CORE_RELEASE=3.2 - allow_failures: + env: DB=MYSQL INSTALLER_VERSION=4.0.x-dev PHPCS_TEST=1 PHPUNIT_TEST=1 - php: 7.0 + env: DB=PGSQL INSTALLER_VERSION=4.1.x-dev PHPUNIT_TEST=1 + - php: 7.1 + env: DB=MYSQL INSTALLER_VERSION=4.2.x-dev PHPUNIT_COVERAGE_TEST=1 + - php: 7.2 + env: DB=MYSQL INSTALLER_VERSION=4.x-dev PHPUNIT_TEST=1 before_script: - - composer self-update || true - - git clone git://github.com/silverstripe-labs/silverstripe-travis-support.git ~/travis-support - - php ~/travis-support/travis_setup.php --source `pwd` --target ~/builds/ss - - cd ~/builds/ss - - composer install + # Init PHP + - phpenv rehash + - phpenv config-rm xdebug.ini + + # Install composer dependencies + - composer validate + - echo $INSTALLER_VERSION + - composer require --prefer-source --no-update silverstripe/recipe-cms:1.1.x-dev + - if [[ $DB == PGSQL ]]; then composer require --no-update silverstripe/postgresql:2.0.x-dev; fi + - composer install --prefer-source --no-interaction --no-progress --no-suggest --optimize-autoloader --verbose --profile script: - - vendor/bin/phpunit mathspamprotection/tests + - if [[ $PHPUNIT_COVERAGE_TEST ]]; then phpdbg -qrr vendor/bin/phpunit --coverage-clover=coverage.xml tests/; fi + +after_success: + - if [[ $PHPUNIT_COVERAGE_TEST ]]; then bash <(curl -s https://codecov.io/bash) -f coverage.xml; fi diff --git a/README.md b/README.md index 1231144..6dd1fd6 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ composer require "silverstripe/mathspamprotection:dev-master" Set the default spam protector in *mysite/_config/spamprotection.yml* --- - name: spamprotection - --- - FormSpamProtectionExtension: - default_spam_protector: MathSpamProtector + name: myspamprotection + --- + SilverStripe\SpamProtection\Extension\FormSpamProtectionExtension: + default_spam_protector: SilverStripe\MathSpamProtection\MathSpamProtector diff --git a/code/MathSpamProtector.php b/code/MathSpamProtector.php index 365333c..fbd7328 100644 --- a/code/MathSpamProtector.php +++ b/code/MathSpamProtector.php @@ -4,6 +4,10 @@ * @package mathspamprotection */ +namespace SilverStripe\MathSpamProtection; + +use SilverStripe\SpamProtection\SpamProtector; + class MathSpamProtector implements SpamProtector { /** diff --git a/code/MathSpamProtectorField.php b/code/MathSpamProtectorField.php index 33702ca..7ac91b6 100644 --- a/code/MathSpamProtectorField.php +++ b/code/MathSpamProtectorField.php @@ -1,4 +1,10 @@ get('MathSpamProtectorField', 'enabled')) { + if ($this->config()->get('enabled')) { return parent::Field($properties); } @@ -40,7 +46,7 @@ class MathSpamProtectorField extends TextField public function FieldHolder($properties = array()) { - if (Config::inst()->get('MathSpamProtectorField', 'enabled')) { + if ($this->config()->get('enabled')) { return parent::FieldHolder($properties); } @@ -54,7 +60,7 @@ class MathSpamProtectorField extends TextField */ public function Title() { - $prefix = Config::inst()->get('MathSpamProtection', 'question_prefix'); + $prefix = $this->config()->get('question_prefix'); if (!$prefix) { $prefix = _t('MathSpamProtectionField.SPAMQUESTION', "Spam protection question: %s"); @@ -62,24 +68,24 @@ class MathSpamProtectorField extends TextField return sprintf( $prefix, - self::get_math_question() + $this->getMathsQuestion() ); } /** - * Validates the value submitted by the user with the one saved into the - * {@link Session} and then notify callback object with the spam checking + * Validates the value submitted by the user with the one saved into the + * {@link Session} and then notify callback object with the spam checking * result. * * @return bool */ public function validate($validator) { - if (!Config::inst()->get('MathSpamProtectorField', 'enabled')) { + if (!$this->config()->get( 'enabled')) { return true; } - if (!self::correct_answer($this->Value())) { + if (!$this->isCorrectAnswer($this->Value())) { $validator->validationError( $this->name, _t( @@ -101,17 +107,20 @@ class MathSpamProtectorField extends TextField * * @return string */ - public static function get_math_question() + public function getMathsQuestion() { - if (!Session::get("mathQuestionV1") && !Session::get("mathQuestionV2")) { + /** @var Session $session */ + $session = Controller::curr()->getRequest()->getSession(); + + if (!$session->get("mathQuestionV1") && !$session->get("mathQuestionV2")) { $v1 = rand(1, 9); $v2 = rand(1, 9); - Session::set("mathQuestionV1", $v1); - Session::set("mathQuestionV2", $v2); + $session->set("mathQuestionV1", $v1); + $session->set("mathQuestionV2", $v2); } else { - $v1 = Session::get("mathQuestionV1"); - $v2 = Session::get("mathQuestionV2"); + $v1 = $session->get("mathQuestionV1"); + $v2 = $session->get("mathQuestionV2"); } return sprintf( @@ -122,24 +131,27 @@ class MathSpamProtectorField extends TextField } /** - * Checks the given answer if it matches the addition of the saved session + * Checks the given answer if it matches the addition of the saved session * variables. * * Users can answer using words or digits. * * @return bool */ - public static function correct_answer($answer) + public function isCorrectAnswer($answer) { - $v1 = Session::get("mathQuestionV1"); - $v2 = Session::get("mathQuestionV2"); - Session::clear('mathQuestionV1'); - Session::clear('mathQuestionV2'); + $session = Controller::curr()->getRequest()->getSession(); + + $v1 = $session->get("mathQuestionV1"); + $v2 = $session->get("mathQuestionV2"); + + $session->clear('mathQuestionV1'); + $session->clear('mathQuestionV2'); $word = MathSpamProtectorField::digit_to_word($v1 + $v2); - return ($word == strtolower($answer) || (Config::inst()->get('MathSpamProtectorField', 'allow_numeric_answer') && (($v1 + $v2) == $answer))); + return ($word == strtolower($answer) || ($this->config()->get('allow_numeric_answer') && (($v1 + $v2) == $answer))); } /** diff --git a/composer.json b/composer.json index baac0dc..e6ff864 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "silverstripe/mathspamprotection", "description": "This module provides a simple math protection mechanism for prevent spam from your forms.Includes an EditableMathSpamField to integrate with the UserForms module.", - "type": "silverstripe-module", + "type": "silverstripe-vendormodule", "keywords": ["silverstripe", "spamprotection", "mathspamprotection"], "authors": [{ "name": "Will Rossiter", @@ -9,7 +9,12 @@ }], "minimum-stability": "dev", "require": { - "silverstripe/spamprotection": "^2" + "silverstripe/spamprotection": "^3" }, + "autoload": { + "psr-4": { + "SilverStripe\\MathSpamProtection\\": "code/" + } + }, "license": "BSD-3-Clause" }