Skip to content

Commit

Permalink
Add namespaces, separate test classes, add PSR-4 autoloader, update P…
Browse files Browse the repository at this point in the history
…SR-2 compliance
  • Loading branch information
robbieaverill committed Aug 28, 2017
1 parent a60f592 commit c5f0827
Show file tree
Hide file tree
Showing 25 changed files with 214 additions and 111 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

## Requirements

SilverStripe 3.0.0 or greater
SilverStripe 4.0 or greater

## Documentation

Expand Down
2 changes: 1 addition & 1 deletion _config/spamprotection.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ name: spamprotection
---
SilverStripe\Forms\Form:
extensions:
- SilverStripe\Spamprotection\FormSpamProtectionExtension
- SilverStripe\SpamProtection\Extension\FormSpamProtectionExtension
35 changes: 26 additions & 9 deletions code/EditableSpamProtectionField.php
Original file line number Diff line number Diff line change
@@ -1,28 +1,45 @@
<?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
* installed) to allow the user to have captcha fields with their custom forms
*
* @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
*
* @var array
* @config
*/
private static $check_fields = array(
'EditableEmailField',
'EditableTextField',
'EditableNumericField'
EditableEmailField::class,
EditableTextField::class,
EditableNumericField::class
);

private static $db = array(
Expand Down Expand Up @@ -148,15 +165,15 @@ public function getCMSFields()

// 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}";
Expand Down Expand Up @@ -228,10 +245,10 @@ public function validateField($data, $form)

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());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace SilverStripe\Spamprotection;
namespace SilverStripe\SpamProtection\Extension;

use SilverStripe\Core\Extension;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
namespace SilverStripe\Spamprotection;

namespace SilverStripe\SpamProtection\Extension;

use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Extension;
Expand Down Expand Up @@ -45,7 +46,7 @@ class FormSpamProtectionExtension extends Extension
'authorIp',
'authorId'
);

/**
* @config
*
Expand All @@ -54,20 +55,20 @@ 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)
{
// generate the spam protector
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)) {
Expand All @@ -85,12 +86,12 @@ public static function get_protector($options = null)
*/
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
Expand All @@ -111,7 +112,7 @@ public function enableSpamProtection($options = array())
// 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'])) {
Expand All @@ -123,7 +124,7 @@ public function enableSpamProtection($options = array())
}
}
}

return $this->owner;
}
}
3 changes: 2 additions & 1 deletion code/interfaces/SpamProtector.php → code/SpamProtector.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
namespace SilverStripe\Spamprotection;

namespace SilverStripe\SpamProtection;

use SilverStripe\Forms\FormField;

Expand Down
48 changes: 0 additions & 48 deletions code/SpamProtectorManager.php

This file was deleted.

8 changes: 7 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion lang/de.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
de:
EditableSpamProtectionField:
SilverStripe\SpamProtection\EditableSpamProtectionField:
PLURALNAME: Spamschutzfelder
SINGULARNAME: Spamschutzfeld
SPAMFIELDMAPPING: 'Spamschutzfeld Zuordnung'
Expand Down
2 changes: 1 addition & 1 deletion lang/en.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
en:
EditableSpamProtectionField:
SilverStripe\SpamProtection\EditableSpamProtectionField:
PLURALNAME: 'Spam Protection Fields'
SINGULARNAME: 'Spam Protection Field'
SPAMFIELDMAPPING: 'Spam Field Mapping'
Expand Down
2 changes: 1 addition & 1 deletion lang/eo.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
eo:
EditableSpamProtectionField:
SilverStripe\SpamProtection\EditableSpamProtectionField:
PLURALNAME: 'Spamprotektaj kampoj'
SINGULARNAME: 'Spamprotekta kampo'
SPAMFIELDMAPPING: 'Spamkampa mapigo'
Expand Down
2 changes: 1 addition & 1 deletion lang/es.yml
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
2 changes: 1 addition & 1 deletion lang/fa_IR.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
fa_IR:
EditableSpamProtectionField:
SilverStripe\SpamProtection\EditableSpamProtectionField:
PLURALNAME: 'فیلدهای محافظت از هرزنوشته'
SINGULARNAME: 'فیلد محافظت از هرزنوشته'
SPAMFIELDMAPPING: 'نقشه‌برداری فیلد هرزنوشته'
Expand Down
2 changes: 1 addition & 1 deletion lang/hr.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
hr:
EditableSpamProtectionField:
SilverStripe\SpamProtection\EditableSpamProtectionField:
PLURALNAME: 'Polja Spam zaštite'
SINGULARNAME: 'Polje Spam zaštite'
SPAMFIELDMAPPING: 'Mapiranje polja Spama'
Expand Down
2 changes: 1 addition & 1 deletion lang/ru.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
ru:
EditableSpamProtectionField:
SilverStripe\SpamProtection\EditableSpamProtectionField:
PLURALNAME: 'Поля защиты от спама'
SINGULARNAME: 'Поле защиты от спама'
SPAMFIELDMAPPING: 'Привязка полей для защиты от спама'
Expand Down
2 changes: 1 addition & 1 deletion lang/sk.yml
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
43 changes: 31 additions & 12 deletions tests/EditableSpamProtectionFieldTest.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
<?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();

if (!class_exists('EditableSpamProtectionField')) {
$this->markTestSkipped('"userforms" module not installed');
}

Config::inst()->update(
'FormSpamProtectionExtension',
Config::modify()->set(
FormSpamProtectionExtension::class,
'default_spam_protector',
'EditableSpamProtectionFieldTest_Protector'
Protector::class
);
}

Expand All @@ -33,7 +43,7 @@ public function testValidateFieldDoesntAddErrorOnSuccess()

$formMock
->expects($this->never())
->method('addErrorMessage');
->method('sessionMessage');

$formFieldMock->validateField(array('MyField' => null), $formMock);
}
Expand All @@ -53,9 +63,13 @@ public function testValidateFieldAddsErrorFromField()

$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);
}
Expand All @@ -75,8 +89,13 @@ public function testValidateFieldAddsDefaultError()

$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);
}
Expand All @@ -102,7 +121,7 @@ public function testSpamMapSettingsAreSerialised()

protected function getFormMock()
{
$formMock = $this->getMockBuilder('Form', array('addErrorMessage'))
$formMock = $this->getMockBuilder(Form::class, array('sessionMessage'))
->disableOriginalConstructor()
->getMock();
$formMock
Expand Down
Loading

0 comments on commit c5f0827

Please sign in to comment.