From 0c96d861319c9e0ac4b92362516f7f6a7d23160f Mon Sep 17 00:00:00 2001 From: Gordon Anderson Date: Thu, 19 Apr 2018 20:26:38 +0700 Subject: [PATCH 01/15] UPGRADE: Namespace fixes after running upgrade tool --- code/MathSpamProtectorField.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/code/MathSpamProtectorField.php b/code/MathSpamProtectorField.php index 33702ca..7566f73 100644 --- a/code/MathSpamProtectorField.php +++ b/code/MathSpamProtectorField.php @@ -1,5 +1,9 @@ Date: Thu, 19 Apr 2018 22:06:16 +0700 Subject: [PATCH 02/15] FIX: Namespace in documentation --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1231144..4a93e94 100644 --- a/README.md +++ b/README.md @@ -38,5 +38,5 @@ Set the default spam protector in *mysite/_config/spamprotection.yml* --- name: spamprotection --- - FormSpamProtectionExtension: - default_spam_protector: MathSpamProtector + SilverStripe\SpamProtection\Extension\FormSpamProtectionExtension: + default_spam_protector: SilverStripe\SpamProtection\Maths\MathSpamProtector From af1693b7eadea0f604361198175f126563e46f20 Mon Sep 17 00:00:00 2001 From: Gordon Anderson Date: Thu, 19 Apr 2018 22:06:41 +0700 Subject: [PATCH 03/15] FIX: Missing use statement --- code/MathSpamProtector.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/code/MathSpamProtector.php b/code/MathSpamProtector.php index 365333c..2cc2166 100644 --- a/code/MathSpamProtector.php +++ b/code/MathSpamProtector.php @@ -4,6 +4,10 @@ * @package mathspamprotection */ +namespace SilverStripe\SpamProtection\Maths; + +use SilverStripe\SpamProtection\SpamProtector; + class MathSpamProtector implements SpamProtector { /** From bd4defe7936aa08f9c13bfc831a8aac44ede25b7 Mon Sep 17 00:00:00 2001 From: Gordon Anderson Date: Thu, 19 Apr 2018 22:07:24 +0700 Subject: [PATCH 04/15] MINOR: IDE whitespace tidy --- code/MathSpamProtectorField.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/code/MathSpamProtectorField.php b/code/MathSpamProtectorField.php index 7566f73..6db5d36 100644 --- a/code/MathSpamProtectorField.php +++ b/code/MathSpamProtectorField.php @@ -1,4 +1,5 @@ get('MathSpamProtectorField', 'enabled')) { @@ -71,8 +72,8 @@ class MathSpamProtectorField extends TextField } /** - * 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 @@ -126,7 +127,7 @@ 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. From 83b3fc22ad0f2260ab0777049dd4908be2bec98e Mon Sep 17 00:00:00 2001 From: Gordon Anderson Date: Thu, 19 Apr 2018 22:07:50 +0700 Subject: [PATCH 05/15] FIX: Namespace as a sub namespace of the spam protection module --- composer.json | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/composer.json b/composer.json index baac0dc..bf6c03f 100644 --- a/composer.json +++ b/composer.json @@ -11,5 +11,10 @@ "require": { "silverstripe/spamprotection": "^2" }, + "autoload": { + "psr-4": { + "SilverStripe\\SpamProtection\\Maths": "code/" + } + }, "license": "BSD-3-Clause" } From 7398064cd5d5748c284488b56eb8b353d76d025b Mon Sep 17 00:00:00 2001 From: Gordon Anderson Date: Fri, 20 Apr 2018 10:18:25 +0700 Subject: [PATCH 06/15] FIX: Config and session --- code/MathSpamProtectorField.php | 41 +++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/code/MathSpamProtectorField.php b/code/MathSpamProtectorField.php index 6db5d36..918ddb5 100644 --- a/code/MathSpamProtectorField.php +++ b/code/MathSpamProtectorField.php @@ -1,6 +1,7 @@ get('MathSpamProtectorField', 'enabled')) { + if (Config::inst()->get('SilverStripe\SpamProtection\Maths\MathSpamProtectorField', 'enabled')) { return parent::Field($properties); } @@ -45,7 +46,7 @@ class MathSpamProtectorField extends TextField public function FieldHolder($properties = array()) { - if (Config::inst()->get('MathSpamProtectorField', 'enabled')) { + if (Config::inst()->get('SilverStripe\SpamProtection\Maths\MathSpamProtectorField', 'enabled')) { return parent::FieldHolder($properties); } @@ -59,7 +60,7 @@ class MathSpamProtectorField extends TextField */ public function Title() { - $prefix = Config::inst()->get('MathSpamProtection', 'question_prefix'); + $prefix = Config::inst()->get('SilverStripe\SpamProtection\Maths\MathSpamProtection', 'question_prefix'); if (!$prefix) { $prefix = _t('MathSpamProtectionField.SPAMQUESTION', "Spam protection question: %s"); @@ -67,7 +68,7 @@ class MathSpamProtectorField extends TextField return sprintf( $prefix, - self::get_math_question() + $this->get_math_question() ); } @@ -80,11 +81,11 @@ class MathSpamProtectorField extends TextField */ public function validate($validator) { - if (!Config::inst()->get('MathSpamProtectorField', 'enabled')) { + if (!Config::inst()->get('SilverStripe\SpamProtection\Maths\MathSpamProtectorField', 'enabled')) { return true; } - if (!self::correct_answer($this->Value())) { + if (!$this->correct_answer($this->Value())) { $validator->validationError( $this->name, _t( @@ -106,17 +107,20 @@ class MathSpamProtectorField extends TextField * * @return string */ - public static function get_math_question() + public function get_math_question() { - 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( @@ -134,13 +138,16 @@ class MathSpamProtectorField extends TextField * * @return bool */ - public static function correct_answer($answer) + public function correct_answer($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); From fb23959f60bfb07562b7c8f1f04ba773779aa533 Mon Sep 17 00:00:00 2001 From: Gordon Anderson Date: Fri, 20 Apr 2018 11:13:33 +0700 Subject: [PATCH 07/15] FIX: Correct version of parent spam protection module --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index bf6c03f..60c5934 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,7 @@ }], "minimum-stability": "dev", "require": { - "silverstripe/spamprotection": "^2" + "silverstripe/spamprotection": "^3" }, "autoload": { "psr-4": { From c8084968c500e058b848b95cf74d1ed62cd49423 Mon Sep 17 00:00:00 2001 From: Gordon Anderson Date: Fri, 20 Apr 2018 11:13:48 +0700 Subject: [PATCH 08/15] FIX: Fixes suggested in PR, mostly minor --- code/MathSpamProtectorField.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/code/MathSpamProtectorField.php b/code/MathSpamProtectorField.php index 918ddb5..5400bf5 100644 --- a/code/MathSpamProtectorField.php +++ b/code/MathSpamProtectorField.php @@ -37,7 +37,7 @@ class MathSpamProtectorField extends TextField public function Field($properties = array()) { - if (Config::inst()->get('SilverStripe\SpamProtection\Maths\MathSpamProtectorField', 'enabled')) { + if ($this->config()->get('enabled')) { return parent::Field($properties); } @@ -46,7 +46,7 @@ class MathSpamProtectorField extends TextField public function FieldHolder($properties = array()) { - if (Config::inst()->get('SilverStripe\SpamProtection\Maths\MathSpamProtectorField', 'enabled')) { + if ($this->config()->get('enabled')) { return parent::FieldHolder($properties); } @@ -60,7 +60,7 @@ class MathSpamProtectorField extends TextField */ public function Title() { - $prefix = Config::inst()->get('SilverStripe\SpamProtection\Maths\MathSpamProtection', 'question_prefix'); + $prefix = $this->config()->get('question_prefix'); if (!$prefix) { $prefix = _t('MathSpamProtectionField.SPAMQUESTION', "Spam protection question: %s"); @@ -68,7 +68,7 @@ class MathSpamProtectorField extends TextField return sprintf( $prefix, - $this->get_math_question() + $this->getMathsQuestion() ); } @@ -81,11 +81,11 @@ class MathSpamProtectorField extends TextField */ public function validate($validator) { - if (!Config::inst()->get('SilverStripe\SpamProtection\Maths\MathSpamProtectorField', 'enabled')) { + if (!$this->config()->get( 'enabled')) { return true; } - if (!$this->correct_answer($this->Value())) { + if (!$this->isCorrectAnswer($this->Value())) { $validator->validationError( $this->name, _t( @@ -107,7 +107,7 @@ class MathSpamProtectorField extends TextField * * @return string */ - public function get_math_question() + public function getMathsQuestion() { /** @var Session $session */ $session = Controller::curr()->getRequest()->getSession(); @@ -138,7 +138,7 @@ class MathSpamProtectorField extends TextField * * @return bool */ - public function correct_answer($answer) + public function isCorrectAnswer($answer) { $session = Controller::curr()->getRequest()->getSession(); @@ -151,7 +151,7 @@ class MathSpamProtectorField extends TextField $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))); } /** From 9313f18f024298db1cebf05e2d538ae90802a964 Mon Sep 17 00:00:00 2001 From: Gordon Anderson Date: Fri, 20 Apr 2018 11:18:27 +0700 Subject: [PATCH 09/15] WIP: Upgrade of Travis file --- .travis.yml | 54 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 31 insertions(+), 23 deletions(-) diff --git a/.travis.yml b/.travis.yml index 0456f64..0c9bba9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,36 +1,44 @@ -# 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 + - vendor/bin/sake flush=all + + - php vendor/silverstripe/framework/tests/dump_constants.php script: - - vendor/bin/phpunit mathspamprotection/tests + - if [[ $PHPUNIT_TEST ]]; then find . | grep Sapphire && vendor/bin/phpunit tests/; fi + - 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 From d8768c2e219753b3f7d8598d211f05545d4eba8e Mon Sep 17 00:00:00 2001 From: Gordon Anderson Date: Fri, 20 Apr 2018 11:33:30 +0700 Subject: [PATCH 10/15] FIX: Lack of trailing slashes on namespace --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 60c5934..5992668 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,7 @@ }, "autoload": { "psr-4": { - "SilverStripe\\SpamProtection\\Maths": "code/" + "SilverStripe\\SpamProtection\\Maths\\": "code/" } }, "license": "BSD-3-Clause" From a2e6bc422d3b0f24af7c7a9490495c417e5ce7bb Mon Sep 17 00:00:00 2001 From: Gordon Anderson Date: Fri, 20 Apr 2018 11:49:42 +0700 Subject: [PATCH 11/15] WIP: Debugging travis, just confirming no tests exist --- .travis.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 0c9bba9..d6cb4bb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -36,8 +36,10 @@ before_script: - php vendor/silverstripe/framework/tests/dump_constants.php + - ls -lh + - find . | grep tests + script: - - if [[ $PHPUNIT_TEST ]]; then find . | grep Sapphire && vendor/bin/phpunit tests/; fi - if [[ $PHPUNIT_COVERAGE_TEST ]]; then phpdbg -qrr vendor/bin/phpunit --coverage-clover=coverage.xml tests/; fi after_success: From 01b19b980ce24ef553d981f1a9166019e0d1f0d5 Mon Sep 17 00:00:00 2001 From: Gordon Anderson Date: Fri, 20 Apr 2018 11:53:26 +0700 Subject: [PATCH 12/15] MINOR: Remove debug --- .travis.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index d6cb4bb..2978ca3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -36,9 +36,6 @@ before_script: - php vendor/silverstripe/framework/tests/dump_constants.php - - ls -lh - - find . | grep tests - script: - if [[ $PHPUNIT_COVERAGE_TEST ]]; then phpdbg -qrr vendor/bin/phpunit --coverage-clover=coverage.xml tests/; fi From aaa4de1e95890e8460d5f1da7918ac47c094669f Mon Sep 17 00:00:00 2001 From: Gordon Anderson Date: Fri, 20 Apr 2018 14:19:15 +0700 Subject: [PATCH 13/15] FIX: Remove redundant lines --- .travis.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 2978ca3..8c4f483 100644 --- a/.travis.yml +++ b/.travis.yml @@ -32,9 +32,6 @@ before_script: - 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 - - vendor/bin/sake flush=all - - - php vendor/silverstripe/framework/tests/dump_constants.php script: - if [[ $PHPUNIT_COVERAGE_TEST ]]; then phpdbg -qrr vendor/bin/phpunit --coverage-clover=coverage.xml tests/; fi From 82d37954bbfa501f0a8b58abb833d631f0c7a2b7 Mon Sep 17 00:00:00 2001 From: Gordon Anderson Date: Fri, 20 Apr 2018 14:19:30 +0700 Subject: [PATCH 14/15] FIX: Change namespace as requested --- code/MathSpamProtector.php | 2 +- code/MathSpamProtectorField.php | 2 +- composer.json | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/code/MathSpamProtector.php b/code/MathSpamProtector.php index 2cc2166..fbd7328 100644 --- a/code/MathSpamProtector.php +++ b/code/MathSpamProtector.php @@ -4,7 +4,7 @@ * @package mathspamprotection */ -namespace SilverStripe\SpamProtection\Maths; +namespace SilverStripe\MathSpamProtection; use SilverStripe\SpamProtection\SpamProtector; diff --git a/code/MathSpamProtectorField.php b/code/MathSpamProtectorField.php index 5400bf5..7ac91b6 100644 --- a/code/MathSpamProtectorField.php +++ b/code/MathSpamProtectorField.php @@ -1,5 +1,5 @@ Date: Fri, 20 Apr 2018 14:35:28 +0700 Subject: [PATCH 15/15] FIX: Docs were incorrect post namespace change --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 4a93e94..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 - --- + name: myspamprotection + --- SilverStripe\SpamProtection\Extension\FormSpamProtectionExtension: - default_spam_protector: SilverStripe\SpamProtection\Maths\MathSpamProtector + default_spam_protector: SilverStripe\MathSpamProtection\MathSpamProtector