Add namespaces, separate test classes, add PSR-4 autoloader, update PSR-2 compliance

This commit is contained in:
Robbie Averill 2017-08-28 10:53:32 +12:00
parent a60f592598
commit c5f0827c2e
25 changed files with 214 additions and 111 deletions

View File

@ -13,7 +13,7 @@
## Requirements
SilverStripe 3.0.0 or greater
SilverStripe 4.0 or greater
## Documentation

View File

@ -3,4 +3,4 @@ name: spamprotection
---
SilverStripe\Forms\Form:
extensions:
- SilverStripe\Spamprotection\FormSpamProtectionExtension
- SilverStripe\SpamProtection\Extension\FormSpamProtectionExtension

View File

@ -1,5 +1,18 @@
<?php
namespace SilverStripe\Spamprotection;
namespace SilverStripe\SpamProtection;
use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\FieldGroup;
use SilverStripe\Forms\FieldList;
use SilverStripe\SpamProtection\Extension\FormSpamProtectionExtension;
use SilverStripe\ORM\UnsavedRelationList;
// @todo
use EditableEmailField;
use EditableFormField;
use EditableNumericField;
use EditableTextField;
/**
* Editable Spam Protecter Field. Used with the User Defined Forms module (if
@ -7,12 +20,16 @@ namespace SilverStripe\Spamprotection;
*
* @package spamprotection
*/
// @todo update namespaced for userforms when it is 4.0 compatible
if (class_exists('EditableFormField')) {
class EditableSpamProtectionField extends EditableFormField
{
private static $singular_name = 'Spam Protection Field';
private static $plural_name = 'Spam Protection Fields';
private static $table_name = 'EditableSpamProtectionField';
/**
* Fields to include spam detection for
*
@ -20,9 +37,9 @@ if (class_exists('EditableFormField')) {
* @config
*/
private static $check_fields = array(
'EditableEmailField',
'EditableTextField',
'EditableNumericField'
EditableEmailField::class,
EditableTextField::class,
EditableNumericField::class
);
private static $db = array(
@ -148,15 +165,15 @@ if (class_exists('EditableFormField')) {
// Each other text field in this group can be assigned a field mapping
$mapGroup = FieldGroup::create()
->setTitle(_t('EditableSpamProtectionField.SPAMFIELDMAPPING', 'Spam Field Mapping'))
->setTitle(_t(__CLASS__.'.SPAMFIELDMAPPING', 'Spam Field Mapping'))
->setName('SpamFieldMapping')
->setDescription(_t(
'EditableSpamProtectionField.SPAMFIELDMAPPINGDESCRIPTION',
__CLASS__.'.SPAMFIELDMAPPINGDESCRIPTION',
'Select the form fields that correspond to any relevant spam protection identifiers'
));
// Generate field specific settings
$mappableFields = Config::inst()->get('FormSpamProtectionExtension', 'mappable_fields');
$mappableFields = Config::inst()->get(FormSpamProtectionExtension::class, 'mappable_fields');
$mappableFieldsMerged = array_combine($mappableFields, $mappableFields);
foreach ($this->getCandidateFields() as $otherField) {
$mapSetting = "Map-{$otherField->Name}";
@ -228,10 +245,10 @@ if (class_exists('EditableFormField')) {
if ($foundError !== false) {
// use error messaging already set from validate method
$form->addErrorMessage($this->Name, $foundError['message'], $foundError['messageType'], false);
$form->sessionMessage($foundError['message'], $foundError['messageType']);
} else {
// fallback to custom message set in CMS or default message if none set
$form->addErrorMessage($this->Name, $this->getErrorMessage()->HTML(), 'error', false);
$form->sessionError($this->getErrorMessage()->HTML());
}
}
}

View File

@ -1,6 +1,6 @@
<?php
namespace SilverStripe\Spamprotection;
namespace SilverStripe\SpamProtection\Extension;
use SilverStripe\Core\Extension;

View File

@ -1,5 +1,6 @@
<?php
namespace SilverStripe\Spamprotection;
namespace SilverStripe\SpamProtection\Extension;
use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Extension;
@ -45,7 +46,7 @@ class FormSpamProtectionExtension extends Extension
'authorIp',
'authorId'
);
/**
* @config
*
@ -54,12 +55,12 @@ class FormSpamProtectionExtension extends Extension
* @var string $spam_protector
*/
private static $field_name = "Captcha";
/**
* Instantiate a SpamProtector instance
*
* @param array $options Configuration options
* @return SpamProtector
* @return SpamProtector|null
*/
public static function get_protector($options = null)
{
@ -67,7 +68,7 @@ class FormSpamProtectionExtension extends Extension
if (isset($options['protector'])) {
$protector = $options['protector'];
} else {
$protector = Config::inst()->get('FormSpamProtectionExtension', 'default_spam_protector');
$protector = Config::inst()->get(self::class, 'default_spam_protector');
}
if ($protector && class_exists($protector)) {
@ -85,12 +86,12 @@ class FormSpamProtectionExtension extends Extension
*/
public function enableSpamProtection($options = array())
{
// captcha form field name (must be unique)
if (isset($options['name'])) {
$name = $options['name'];
} else {
$name = Config::inst()->get('FormSpamProtectionExtension', 'field_name');
$name = Config::inst()->get(self::class, 'field_name');
}
// captcha field title
@ -111,7 +112,7 @@ class FormSpamProtectionExtension extends Extension
// add the form field
if ($field = $protector->getFormField($name, $title)) {
$field->setForm($this->owner);
// Add before field specified by insertBefore
$inserted = false;
if (!empty($options['insertBefore'])) {
@ -123,7 +124,7 @@ class FormSpamProtectionExtension extends Extension
}
}
}
return $this->owner;
}
}

View File

@ -1,5 +1,6 @@
<?php
namespace SilverStripe\Spamprotection;
namespace SilverStripe\SpamProtection;
use SilverStripe\Forms\FormField;

View File

@ -1,48 +0,0 @@
<?php
namespace SilverStripe\Spamprotection;
use SilverStripe\Dev\Deprecation;
/**
* @package spamprotection
*
* @deprecated 1.0
*/
class SpamProtectorManager
{
private static $spam_protector = null;
public static function set_spam_protector($protector)
{
Deprecation::notice(
'1.1',
'SpamProtectorManager::set_spam_protector() is deprecated. '.
'Use the new config system. FormSpamProtectorExtension.default_spam_protector'
);
static::$spam_protector = $protector;
}
public static function get_spam_protector()
{
Deprecation::notice(
'1.1',
'SpamProtectorManager::get_spam_protector() is deprecated'.
'Use the new config system. FormSpamProtectorExtension.default_spam_protector'
);
return static::$spam_protector;
}
public static function update_form($form, $before = null, $fieldsToSpamServiceMapping = array(), $title = null, $rightTitle = null)
{
Deprecation::notice(
'1.1',
'SpamProtectorManager::update_form is deprecated'.
'Please use $form->enableSpamProtection() for adding spamprotection'
);
return $form->enableSpamProtection();
}
}

View File

@ -22,7 +22,13 @@
"dev-master": "3.0.x-dev"
}
},
"license": "BSD-3-Clause",
"autoload": {
"psr-4": {
"SilverStripe\\SpamProtection\\": "code/",
"SilverStripe\\SpamProtection\\Tests\\": "tests/"
}
},
"license": "BSD-3-Clause",
"minimum-stability": "dev",
"prefer-stable": true
}

View File

@ -1,5 +1,5 @@
de:
EditableSpamProtectionField:
SilverStripe\SpamProtection\EditableSpamProtectionField:
PLURALNAME: Spamschutzfelder
SINGULARNAME: Spamschutzfeld
SPAMFIELDMAPPING: 'Spamschutzfeld Zuordnung'

View File

@ -1,5 +1,5 @@
en:
EditableSpamProtectionField:
SilverStripe\SpamProtection\EditableSpamProtectionField:
PLURALNAME: 'Spam Protection Fields'
SINGULARNAME: 'Spam Protection Field'
SPAMFIELDMAPPING: 'Spam Field Mapping'

View File

@ -1,5 +1,5 @@
eo:
EditableSpamProtectionField:
SilverStripe\SpamProtection\EditableSpamProtectionField:
PLURALNAME: 'Spamprotektaj kampoj'
SINGULARNAME: 'Spamprotekta kampo'
SPAMFIELDMAPPING: 'Spamkampa mapigo'

View File

@ -1,5 +1,5 @@
es:
EditableSpamProtectionField:
SilverStripe\SpamProtection\EditableSpamProtectionField:
PLURALNAME: 'Campos de protección de spam'
SINGULARNAME: 'Campo de protección de spam'
SPAMFIELDMAPPING: 'Mapeo del campo spam'

View File

@ -1,5 +1,5 @@
fa_IR:
EditableSpamProtectionField:
SilverStripe\SpamProtection\EditableSpamProtectionField:
PLURALNAME: 'فیلدهای محافظت از هرزنوشته'
SINGULARNAME: 'فیلد محافظت از هرزنوشته'
SPAMFIELDMAPPING: 'نقشه‌برداری فیلد هرزنوشته'

View File

@ -1,5 +1,5 @@
hr:
EditableSpamProtectionField:
SilverStripe\SpamProtection\EditableSpamProtectionField:
PLURALNAME: 'Polja Spam zaštite'
SINGULARNAME: 'Polje Spam zaštite'
SPAMFIELDMAPPING: 'Mapiranje polja Spama'

View File

@ -1,5 +1,5 @@
ru:
EditableSpamProtectionField:
SilverStripe\SpamProtection\EditableSpamProtectionField:
PLURALNAME: 'Поля защиты от спама'
SINGULARNAME: 'Поле защиты от спама'
SPAMFIELDMAPPING: 'Привязка полей для защиты от спама'

View File

@ -1,5 +1,5 @@
sk:
EditableSpamProtectionField:
SilverStripe\SpamProtection\EditableSpamProtectionField:
PLURALNAME: 'Polia ochrany proti spamu'
SINGULARNAME: 'Pole ochrany proti spamu'
SPAMFIELDMAPPING: 'Mapovanie spamového poľa'

View File

@ -1,11 +1,21 @@
<?php
namespace SilverStripe\SpamProtection\Tests;
use UserDefinedForm;
use SilverStripe\Core\Config\Config;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\RequiredFields;
use SilverStripe\SpamProtection\EditableSpamProtectionField;
use SilverStripe\SpamProtection\Extension\FormSpamProtectionExtension;
use SilverStripe\SpamProtection\Tests\EditableSpamProtectionFieldTest\Protector;
class EditableSpamProtectionFieldTest extends SapphireTest
{
protected $usesDatabase = true;
public function setUp()
protected function setUp()
{
parent::setUp();
@ -13,10 +23,10 @@ class EditableSpamProtectionFieldTest extends SapphireTest
$this->markTestSkipped('"userforms" module not installed');
}
Config::inst()->update(
'FormSpamProtectionExtension',
Config::modify()->set(
FormSpamProtectionExtension::class,
'default_spam_protector',
'EditableSpamProtectionFieldTest_Protector'
Protector::class
);
}
@ -33,7 +43,7 @@ class EditableSpamProtectionFieldTest extends SapphireTest
$formMock
->expects($this->never())
->method('addErrorMessage');
->method('sessionMessage');
$formFieldMock->validateField(array('MyField' => null), $formMock);
}
@ -53,9 +63,13 @@ class EditableSpamProtectionFieldTest extends SapphireTest
$formMock
->expects($this->once())
->method('addErrorMessage')
->with($this->anything(), $this->stringContains('some field message'), $this->anything(), $this->anything());
;
->method('sessionMessage')
->with(
$this->anything(),
$this->stringContains('some field message'),
$this->anything(),
$this->anything()
);
$formFieldMock->validateField(array('MyField' => null), $formMock);
}
@ -75,8 +89,13 @@ class EditableSpamProtectionFieldTest extends SapphireTest
$formMock
->expects($this->once())
->method('addErrorMessage')
->with($this->anything(), $this->stringContains('default error message'), $this->anything(), $this->anything());
->method('sessionMessage')
->with(
$this->anything(),
$this->stringContains('default error message'),
$this->anything(),
$this->anything()
);
$formFieldMock->validateField(array('MyField' => null), $formMock);
}
@ -102,7 +121,7 @@ class EditableSpamProtectionFieldTest extends SapphireTest
protected function getFormMock()
{
$formMock = $this->getMockBuilder('Form', array('addErrorMessage'))
$formMock = $this->getMockBuilder(Form::class, array('sessionMessage'))
->disableOriginalConstructor()
->getMock();
$formMock

View File

@ -1,5 +1,18 @@
<?php
namespace SilverStripe\SpamProtection\Tests;
use SilverStripe\Control\Controller;
use SilverStripe\Core\Config\Config;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\TextField;
use SilverStripe\SpamProtection\Extension\FormSpamProtectionExtension;
use SilverStripe\SpamProtection\Tests\Stub\FooProtector;
use SilverStripe\SpamProtection\Tests\Stub\BarProtector;
use SilverStripe\SpamProtection\Tests\Stub\BazProtector;
/**
* @package spamprotection
*/
@ -12,24 +25,25 @@ class FormSpamProtectionExtensionTest extends SapphireTest
*/
protected $form = null;
public function setUp()
protected function setUp()
{
parent::setUp();
$this->form = new Form($this, 'Form', new FieldList(
$this->form = new Form(new Controller, 'Form', new FieldList(
new TextField('Title'),
new TextField('Comment'),
new TextField('URL')
), new FieldList());
$this->form->disableSecurityToken();
}
public function testEnableSpamProtection()
{
Config::inst()->update(
'FormSpamProtectionExtension',
Config::modify()->set(
FormSpamProtectionExtension::class,
'default_spam_protector',
'FormSpamProtectionExtensionTest_FooProtector'
FooProtector::class
);
$form = $this->form->enableSpamProtection();
@ -40,7 +54,7 @@ class FormSpamProtectionExtensionTest extends SapphireTest
public function testEnableSpamProtectionCustomProtector()
{
$form = $this->form->enableSpamProtection(array(
'protector' => 'FormSpamProtectionExtensionTest_BarProtector'
'protector' => BarProtector::class
));
$this->assertEquals('Bar', $form->Fields()->fieldByName('Captcha')->Title());
@ -49,7 +63,7 @@ class FormSpamProtectionExtensionTest extends SapphireTest
public function testEnableSpamProtectionCustomTitle()
{
$form = $this->form->enableSpamProtection(array(
'protector' => 'FormSpamProtectionExtensionTest_BarProtector',
'protector' => BarProtector::class,
'title' => 'Baz',
));
@ -59,7 +73,7 @@ class FormSpamProtectionExtensionTest extends SapphireTest
public function testCustomOptions()
{
$form = $this->form->enableSpamProtection(array(
'protector' => 'FormSpamProtectionExtensionTest_BazProtector',
'protector' => BazProtector::class,
'title' => 'Qux',
'name' => 'Borris'
));
@ -70,17 +84,19 @@ class FormSpamProtectionExtensionTest extends SapphireTest
public function testConfigurableName()
{
$field_name = "test_configurable_name";
Config::inst()->update(
'FormSpamProtectionExtension', 'default_spam_protector',
'FormSpamProtectionExtensionTest_FooProtector'
Config::modify()->set(
FormSpamProtectionExtension::class,
'default_spam_protector',
FooProtector::class
);
Config::inst()->update(
'FormSpamProtectionExtension', 'field_name',
Config::modify()->set(
FormSpamProtectionExtension::class,
'field_name',
$field_name
);
$form = $this->form->enableSpamProtection();
// remove for subsequent tests
Config::inst()->remove('FormSpamProtectionExtension', 'field_name');
Config::modify()->remove(FormSpamProtectionExtension::class, 'field_name');
// field should take up configured name
$this->assertEquals('Foo', $form->Fields()->fieldByName($field_name)->Title());
}
@ -88,7 +104,7 @@ class FormSpamProtectionExtensionTest extends SapphireTest
public function testInsertBefore()
{
$form = $this->form->enableSpamProtection(array(
'protector' => 'FormSpamProtectionExtensionTest_FooProtector',
'protector' => FooProtector::class,
'insertBefore' => 'URL'
));
@ -102,7 +118,7 @@ class FormSpamProtectionExtensionTest extends SapphireTest
public function testInsertBeforeMissing()
{
$form = $this->form->enableSpamProtection(array(
'protector' => 'FormSpamProtectionExtensionTest_FooProtector',
'protector' => FooProtector::class,
'insertBefore' => 'NotAField'
));

View File

@ -0,0 +1,23 @@
<?php
namespace SilverStripe\SpamProtection\Tests\FormSpamProtectionExtensionTest;
use SilverStripe\Dev\TestOnly;
use SilverStripe\Forms\TextField;
use SilverStripe\SpamProtection\SpamProtector;
/**
* @package spamprotection
*/
class BarProtector implements SpamProtector, TestOnly
{
public function getFormField($name = null, $title = null, $value = null)
{
$title = $title ?: 'Bar';
return new TextField($name, $title, $value);
}
public function setFieldMapping($fieldMapping)
{
}
}

View File

@ -0,0 +1,22 @@
<?php
namespace SilverStripe\SpamProtection\Tests\FormSpamProtectionExtensionTest;
use SilverStripe\Dev\TestOnly;
use SilverStripe\Forms\TextField;
use SilverStripe\SpamProtection\SpamProtector;
/**
* @package spamprotection
*/
class BazProtector implements SpamProtector, TestOnly
{
public function getFormField($name = null, $title = null, $value = null)
{
return new TextField($name, $title, $value);
}
public function setFieldMapping($fieldMapping)
{
}
}

View File

@ -0,0 +1,22 @@
<?php
namespace SilverStripe\SpamProtection\Tests\FormSpamProtectionExtensionTest;
use SilverStripe\Dev\TestOnly;
use SilverStripe\Forms\TextField;
use SilverStripe\SpamProtection\SpamProtector;
/**
* @package spamprotection
*/
class FooProtector implements SpamProtector, TestOnly
{
public function getFormField($name = null, $title = null, $value = null)
{
return new TextField($name, 'Foo', $value);
}
public function setFieldMapping($fieldMapping)
{
}
}

View File

@ -1,9 +1,15 @@
<?php
namespace SilverStripe\SpamProtection\Tests\Stub;
use SilverStripe\Dev\TestOnly;
use SilverStripe\Forms\TextField;
use SilverStripe\SpamProtection\SpamProtector;
/**
* @package spamprotection
*/
class FormSpamProtectionExtensionTest_BarProtector implements SpamProtector, TestOnly
class BarProtector implements SpamProtector, TestOnly
{
public function getFormField($name = null, $title = null, $value = null)
{

View File

@ -1,9 +1,15 @@
<?php
namespace SilverStripe\SpamProtection\Tests\Stub;
use SilverStripe\Dev\TestOnly;
use SilverStripe\Forms\TextField;
use SilverStripe\SpamProtection\SpamProtector;
/**
* @package spamprotection
*/
class FormSpamProtectionExtensionTest_BazProtector implements SpamProtector, TestOnly
class BazProtector implements SpamProtector, TestOnly
{
public function getFormField($name = null, $title = null, $value = null)
{

View File

@ -1,9 +1,15 @@
<?php
namespace SilverStripe\SpamProtection\Tests\Stub;
use SilverStripe\Dev\TestOnly;
use SilverStripe\Forms\TextField;
use SilverStripe\SpamProtection\SpamProtector;
/**
* @package spamprotection
*/
class FormSpamProtectionExtensionTest_FooProtector implements SpamProtector, TestOnly
class FooProtector implements SpamProtector, TestOnly
{
public function getFormField($name = null, $title = null, $value = null)
{

View File

@ -1,6 +1,12 @@
<?php
class EditableSpamProtectionFieldTest_Protector implements SpamProtector, TestOnly
namespace SilverStripe\SpamProtection\Tests\Stub;
use SilverStripe\Dev\TestOnly;
use SilverStripe\Forms\TextField;
use SilverStripe\SpamProtection\SpamProtector;
class Protector implements SpamProtector, TestOnly
{
public function getFormField($name = null, $title = null, $value = null)
{