Merge pull request #48 from gordonbanderson/upgradess4

SS4 Upgrade
This commit is contained in:
Robbie Averill 2018-04-23 08:57:06 +12:00 committed by GitHub
commit 1905fa8874
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 77 additions and 52 deletions

View File

@ -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

View File

@ -36,7 +36,7 @@ composer require "silverstripe/mathspamprotection:dev-master"
Set the default spam protector in *mysite/_config/spamprotection.yml*
---
name: spamprotection
name: myspamprotection
---
FormSpamProtectionExtension:
default_spam_protector: MathSpamProtector
SilverStripe\SpamProtection\Extension\FormSpamProtectionExtension:
default_spam_protector: SilverStripe\MathSpamProtection\MathSpamProtector

View File

@ -4,6 +4,10 @@
* @package mathspamprotection
*/
namespace SilverStripe\MathSpamProtection;
use SilverStripe\SpamProtection\SpamProtector;
class MathSpamProtector implements SpamProtector
{
/**

View File

@ -1,4 +1,10 @@
<?php
namespace SilverStripe\MathSpamProtection;
use SilverStripe\Control\Controller;
use SilverStripe\Core\Config\Config;
use SilverStripe\Control\Session;
use SilverStripe\Forms\TextField;
/**
* {@link FormField} for adding an optional maths protection question to a form.
@ -31,7 +37,7 @@ class MathSpamProtectorField extends TextField
public function Field($properties = array())
{
if (Config::inst()->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,7 +68,7 @@ class MathSpamProtectorField extends TextField
return sprintf(
$prefix,
self::get_math_question()
$this->getMathsQuestion()
);
}
@ -75,11 +81,11 @@ class MathSpamProtectorField extends TextField
*/
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(
@ -129,17 +138,20 @@ class MathSpamProtectorField extends TextField
*
* @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)));
}
/**

View File

@ -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"
}