Converted to PSR-2

This commit is contained in:
helpfulrobot 2015-11-21 19:15:15 +13:00
parent 8e4f03a409
commit 7ee2dcea0f
6 changed files with 393 additions and 367 deletions

View File

@ -6,125 +6,136 @@
* *
* @package spamprotection * @package spamprotection
*/ */
if(class_exists('EditableFormField')) { if (class_exists('EditableFormField')) {
class EditableSpamProtectionField extends EditableFormField
{
private static $singular_name = 'Spam Protection Field';
class EditableSpamProtectionField extends EditableFormField { private static $plural_name = 'Spam Protection Fields';
/**
* Fields to include spam detection for
*
* @var array
* @config
*/
private static $check_fields = array(
'EditableEmailField',
'EditableTextField',
'EditableNumericField'
);
private static $singular_name = 'Spam Protection Field'; public function getFormField()
{
// Get protector
$protector = FormSpamProtectionExtension::get_protector();
if (!$protector) {
return false;
}
private static $plural_name = 'Spam Protection Fields'; // Extract saved field mappings and update this field.
/** $fieldMapping = array();
* Fields to include spam detection for foreach ($this->getCandidateFields() as $otherField) {
* $mapSetting = "Map-{$otherField->Name}";
* @var array $spamField = $this->getSetting($mapSetting);
* @config $fieldMapping[$otherField->Name] = $spamField;
*/ }
private static $check_fields = array( $protector->setFieldMapping($fieldMapping);
'EditableEmailField',
'EditableTextField',
'EditableNumericField'
);
public function getFormField() { // Generate field
// Get protector return $protector->getFormField($this->Name, $this->Title, null);
$protector = FormSpamProtectionExtension::get_protector(); }
if(!$protector) return false;
// Extract saved field mappings and update this field. /**
$fieldMapping = array(); * Gets the list of all candidate spam detectable fields on this field's form
foreach($this->getCandidateFields() as $otherField) { *
$mapSetting = "Map-{$otherField->Name}"; * @return DataList
$spamField = $this->getSetting($mapSetting); */
$fieldMapping[$otherField->Name] = $spamField; protected function getCandidateFields()
} {
$protector->setFieldMapping($fieldMapping);
// Generate field // Get list of all configured classes available for spam detection
return $protector->getFormField($this->Name, $this->Title, null); $types = self::config()->check_fields;
} $typesInherit = array();
foreach ($types as $type) {
$subTypes = ClassInfo::subclassesFor($type);
$typesInherit = array_merge($typesInherit, $subTypes);
}
/** // Get all candidates of the above types
* Gets the list of all candidate spam detectable fields on this field's form return $this
* ->Parent()
* @return DataList ->Fields()
*/ ->filter('ClassName', $typesInherit)
protected function getCandidateFields() { ->exclude('Title', ''); // Ignore this field and those without titles
}
// Get list of all configured classes available for spam detection public function getFieldConfiguration()
$types = self::config()->check_fields; {
$typesInherit = array(); $fields = parent::getFieldConfiguration();
foreach ($types as $type) {
$subTypes = ClassInfo::subclassesFor($type);
$typesInherit = array_merge($typesInherit, $subTypes);
}
// Get all candidates of the above types // Get protector
return $this $protector = FormSpamProtectionExtension::get_protector();
->Parent() if (!$protector) {
->Fields() return $fields;
->filter('ClassName', $typesInherit) }
->exclude('Title', ''); // Ignore this field and those without titles
}
public function getFieldConfiguration() { if ($this->Parent()->Fields() instanceof UnsavedRelationList) {
$fields = parent::getFieldConfiguration(); return $fields;
}
// Get protector // Each other text field in this group can be assigned a field mapping
$protector = FormSpamProtectionExtension::get_protector(); $mapGroup = FieldGroup::create(_t(
if (!$protector) return $fields; 'EditableSpamProtectionField.SPAMFIELDMAPPING',
'Spam Field Mapping'
))->setDescription(_t(
'EditableSpamProtectionField.SPAMFIELDMAPPINGDESCRIPTION',
'Select the form fields that correspond to any relevant spam protection identifiers'
));
if ($this->Parent()->Fields() instanceof UnsavedRelationList) { // Generate field specific settings
return $fields; $mappableFields = Config::inst()->get('FormSpamProtectionExtension', 'mappable_fields');
} $mappableFieldsMerged = array_combine($mappableFields, $mappableFields);
foreach ($this->getCandidateFields() as $otherField) {
$mapSetting = "Map-{$otherField->Name}";
$fieldOption = DropdownField::create(
$this->getSettingName($mapSetting),
$otherField->Title,
$mappableFieldsMerged,
$this->getSetting($mapSetting)
)->setEmptyString('');
$mapGroup->push($fieldOption);
}
$fields->insertBefore($mapGroup, $this->getSettingName('ExtraClass'));
// Each other text field in this group can be assigned a field mapping return $fields;
$mapGroup = FieldGroup::create(_t( }
'EditableSpamProtectionField.SPAMFIELDMAPPING',
'Spam Field Mapping'
))->setDescription(_t(
'EditableSpamProtectionField.SPAMFIELDMAPPINGDESCRIPTION',
'Select the form fields that correspond to any relevant spam protection identifiers'
));
// Generate field specific settings public function validateField($data, $form)
$mappableFields = Config::inst()->get('FormSpamProtectionExtension', 'mappable_fields'); {
$mappableFieldsMerged = array_combine($mappableFields, $mappableFields); $formField = $this->getFormField();
foreach ($this->getCandidateFields() as $otherField) { if (!$formField->validate($form->getValidator())) {
$mapSetting = "Map-{$otherField->Name}"; $form->addErrorMessage($this->Name, $this->getErrorMessage()->HTML(), 'error', false);
$fieldOption = DropdownField::create( }
$this->getSettingName($mapSetting), }
$otherField->Title,
$mappableFieldsMerged,
$this->getSetting($mapSetting)
)->setEmptyString('');
$mapGroup->push($fieldOption);
}
$fields->insertBefore($mapGroup, $this->getSettingName('ExtraClass'));
return $fields; public function getFieldValidationOptions()
} {
return new FieldList();
}
public function validateField($data, $form) { public function getRequired()
$formField = $this->getFormField(); {
if (!$formField->validate($form->getValidator())) { return false;
$form->addErrorMessage($this->Name, $this->getErrorMessage()->HTML(), 'error', false); }
}
}
public function getFieldValidationOptions() { public function getIcon()
return new FieldList(); {
} return 'spamprotection/images/' . strtolower($this->class) . '.png';
}
public function getRequired() { public function showInReports()
return false; {
} return false;
}
public function getIcon() { }
return 'spamprotection/images/' . strtolower($this->class) . '.png';
}
public function showInReports() {
return false;
}
}
} }

View File

@ -6,33 +6,36 @@
* @deprecated 1.0 * @deprecated 1.0
*/ */
class SpamProtectorManager { class SpamProtectorManager
{
private static $spam_protector = null; private static $spam_protector = null;
public static function set_spam_protector($protector) { public static function set_spam_protector($protector)
Deprecation::notice('1.1', {
'SpamProtectorManager::set_spam_protector() is deprecated. '. Deprecation::notice('1.1',
'Use the new config system. FormSpamProtectorExtension.default_spam_protector' 'SpamProtectorManager::set_spam_protector() is deprecated. '.
); 'Use the new config system. FormSpamProtectorExtension.default_spam_protector'
);
self::$spam_protector = $protector; self::$spam_protector = $protector;
} }
public static function get_spam_protector() { public static function get_spam_protector()
Deprecation::notice('1.1', {
'SpamProtectorManager::get_spam_protector() is deprecated'. Deprecation::notice('1.1',
'Use the new config system. FormSpamProtectorExtension.default_spam_protector'); 'SpamProtectorManager::get_spam_protector() is deprecated'.
'Use the new config system. FormSpamProtectorExtension.default_spam_protector');
return self::$spam_protector; return self::$spam_protector;
} }
public static function update_form($form, $before = null, $fieldsToSpamServiceMapping = array(), $title = null, $rightTitle = null) { public static function update_form($form, $before = null, $fieldsToSpamServiceMapping = array(), $title = null, $rightTitle = null)
Deprecation::notice('1.1', {
'SpamProtectorManager::update_form is deprecated'. Deprecation::notice('1.1',
'Please use $form->enableSpamProtection() for adding spamprotection' 'SpamProtectorManager::update_form is deprecated'.
); 'Please use $form->enableSpamProtection() for adding spamprotection'
);
return $form->enableSpamProtection(); return $form->enableSpamProtection();
} }
} }

View File

@ -6,22 +6,23 @@
* @package spamprotection * @package spamprotection
*/ */
class CommentSpamProtection extends Extension { class CommentSpamProtection extends Extension
{
public function alterCommentForm(&$form) { public function alterCommentForm(&$form)
$form->enableSpamProtection(array( {
'name' => 'IsSpam', $form->enableSpamProtection(array(
'mapping' => array( 'name' => 'IsSpam',
'Name' => 'authorName', 'mapping' => array(
'Email' => 'authorEmail', 'Name' => 'authorName',
'URL' => 'authorUrl', 'Email' => 'authorEmail',
'Comment' => 'body', 'URL' => 'authorUrl',
'ReturnURL' => 'contextUrl' 'Comment' => 'body',
), 'ReturnURL' => 'contextUrl'
'checks' => array( ),
'spam', 'checks' => array(
'profanity' 'spam',
) 'profanity'
)); )
} ));
}
} }

View File

@ -7,106 +7,108 @@
* @package spamprotection * @package spamprotection
*/ */
class FormSpamProtectionExtension extends Extension { class FormSpamProtectionExtension extends Extension
{
/**
* @config
*
* The default spam protector class name to use. Class should implement the
* {@link SpamProtector} interface.
*
* @var string $spam_protector
*/
private static $default_spam_protector;
/** /**
* @config * @config
* *
* The default spam protector class name to use. Class should implement the * The {@link enableSpamProtection} method will define which of the form
* {@link SpamProtector} interface. * values correlates to this form mapped fields list. Totally custom forms
* * and subclassed SpamProtector instances are define their own mapping
* @var string $spam_protector *
*/ * @var array $mappable_fields
private static $default_spam_protector; */
private static $mappable_fields = array(
'id',
'title',
'body',
'contextUrl',
'contextTitle',
'authorName',
'authorMail',
'authorUrl',
'authorIp',
'authorId'
);
/**
* Instantiate a SpamProtector instance
*
* @param array $options Configuration options
* @return SpamProtector
*/
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');
}
/** if ($protector && class_exists($protector)) {
* @config return Injector::inst()->create($protector);
* } else {
* The {@link enableSpamProtection} method will define which of the form return null;
* values correlates to this form mapped fields list. Totally custom forms }
* and subclassed SpamProtector instances are define their own mapping }
*
* @var array $mappable_fields
*/
private static $mappable_fields = array(
'id',
'title',
'body',
'contextUrl',
'contextTitle',
'authorName',
'authorMail',
'authorUrl',
'authorIp',
'authorId'
);
/**
* Instantiate a SpamProtector instance
*
* @param array $options Configuration options
* @return SpamProtector
*/
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');
}
if($protector && class_exists($protector)) { /**
return Injector::inst()->create($protector); * Activates the spam protection module.
} else { *
return null; * @param array $options
} */
} public function enableSpamProtection($options = array())
{
// captcha form field name (must be unique)
if (isset($options['name'])) {
$name = $options['name'];
} else {
$name = 'Captcha';
}
/** // captcha field title
* Activates the spam protection module. if (isset($options['title'])) {
* $title = $options['title'];
* @param array $options } else {
*/ $title = '';
public function enableSpamProtection($options = array()) { }
// captcha form field name (must be unique)
if(isset($options['name'])) {
$name = $options['name'];
} else {
$name = 'Captcha';
}
// captcha field title // set custom mapping on this form
if(isset($options['title'])) { $protector = self::get_protector($options);
$title = $options['title'];
} else {
$title = '';
}
// set custom mapping on this form if (isset($options['mapping'])) {
$protector = self::get_protector($options); $protector->setFieldMapping($options['mapping']);
}
if(isset($options['mapping'])) { if ($protector) {
$protector->setFieldMapping($options['mapping']); // add the form field
} if ($field = $protector->getFormField($name, $title)) {
$field->setForm($this->owner);
if($protector) {
// add the form field // Add before field specified by insertBefore
if($field = $protector->getFormField($name, $title)) { $inserted = false;
$field->setForm($this->owner); if (!empty($options['insertBefore'])) {
$inserted = $this->owner->Fields()->insertBefore($field, $options['insertBefore']);
// Add before field specified by insertBefore }
$inserted = false; if (!$inserted) {
if(!empty($options['insertBefore'])) { // Add field to end if not added already
$inserted = $this->owner->Fields()->insertBefore($field, $options['insertBefore']); $this->owner->Fields()->push($field);
} }
if(!$inserted) { }
// Add field to end if not added already }
$this->owner->Fields()->push($field);
} return $this->owner;
} }
}
return $this->owner;
}
} }

View File

@ -12,29 +12,29 @@
* @package spamprotection * @package spamprotection
*/ */
interface SpamProtector { interface SpamProtector
{
/** /**
* Return the {@link FormField} associated with this protector. * Return the {@link FormField} associated with this protector.
* *
* Most spam methods will simply return a piece of HTML to be injected at * Most spam methods will simply return a piece of HTML to be injected at
* the end of the form. If a spam method needs to inject more than one * the end of the form. If a spam method needs to inject more than one
* form field (i.e a hidden field and a text field) then return a * form field (i.e a hidden field and a text field) then return a
* {@link FieldGroup} from this method to include both. * {@link FieldGroup} from this method to include both.
* *
* @param string $name * @param string $name
* @param string $title * @param string $title
* @param mixed $value * @param mixed $value
* @return FormField The resulting field * @return FormField The resulting field
*/ */
public function getFormField($name = null, $title = null, $value = null); public function getFormField($name = null, $title = null, $value = null);
/** /**
* Set the fields to map spam protection too * Set the fields to map spam protection too
* *
* @param array $fieldMapping array of Field Names, where the indexes of the array are * @param array $fieldMapping array of Field Names, where the indexes of the array are
* the field names of the form and the values are the standard spamprotection * the field names of the form and the values are the standard spamprotection
* fields used by the protector * fields used by the protector
*/ */
public function setFieldMapping($fieldMapping); public function setFieldMapping($fieldMapping);
} }

View File

@ -3,133 +3,142 @@
/** /**
* @package spamprotection * @package spamprotection
*/ */
class FormSpamProtectionExtensionTest extends SapphireTest { class FormSpamProtectionExtensionTest extends SapphireTest
{
protected $usesDatabase = false; protected $usesDatabase = false;
/** /**
* @var Form * @var Form
*/ */
protected $form = null; protected $form = null;
public function setUp() { public function setUp()
parent::setUp(); {
parent::setUp();
$this->form = new Form($this, 'Form', new FieldList( $this->form = new Form($this, 'Form', new FieldList(
new TextField('Title'), new TextField('Title'),
new TextField('Comment'), new TextField('Comment'),
new TextField('URL') new TextField('URL')
), new FieldList() ), new FieldList()
); );
$this->form->disableSecurityToken(); $this->form->disableSecurityToken();
} }
public function testEnableSpamProtection() { public function testEnableSpamProtection()
Config::inst()->update( {
'FormSpamProtectionExtension', 'default_spam_protector', Config::inst()->update(
'FormSpamProtectionExtensionTest_FooProtector' 'FormSpamProtectionExtension', 'default_spam_protector',
); 'FormSpamProtectionExtensionTest_FooProtector'
);
$form = $this->form->enableSpamProtection(); $form = $this->form->enableSpamProtection();
$this->assertEquals('Foo', $form->Fields()->fieldByName('Captcha')->Title()); $this->assertEquals('Foo', $form->Fields()->fieldByName('Captcha')->Title());
}
} public function testEnableSpamProtectionCustomProtector()
{
$form = $this->form->enableSpamProtection(array(
'protector' => 'FormSpamProtectionExtensionTest_BarProtector'
));
public function testEnableSpamProtectionCustomProtector() { $this->assertEquals('Bar', $form->Fields()->fieldByName('Captcha')->Title());
$form = $this->form->enableSpamProtection(array( }
'protector' => 'FormSpamProtectionExtensionTest_BarProtector'
));
$this->assertEquals('Bar', $form->Fields()->fieldByName('Captcha')->Title()); public function testEnableSpamProtectionCustomTitle()
} {
$form = $this->form->enableSpamProtection(array(
'protector' => 'FormSpamProtectionExtensionTest_BarProtector',
'title' => 'Baz',
));
$this->assertEquals('Baz', $form->Fields()->fieldByName('Captcha')->Title());
}
public function testEnableSpamProtectionCustomTitle() { public function testCustomOptions()
$form = $this->form->enableSpamProtection(array( {
'protector' => 'FormSpamProtectionExtensionTest_BarProtector', $form = $this->form->enableSpamProtection(array(
'title' => 'Baz', 'protector' => 'FormSpamProtectionExtensionTest_BazProtector',
)); 'title' => 'Qux',
'name' => 'Borris'
$this->assertEquals('Baz', $form->Fields()->fieldByName('Captcha')->Title()); ));
}
public function testCustomOptions() { $this->assertEquals('Qux', $form->Fields()->fieldByName('Borris')->Title());
$form = $this->form->enableSpamProtection(array( }
'protector' => 'FormSpamProtectionExtensionTest_BazProtector',
'title' => 'Qux', public function testInsertBefore()
'name' => 'Borris' {
)); $form = $this->form->enableSpamProtection(array(
'protector' => 'FormSpamProtectionExtensionTest_FooProtector',
$this->assertEquals('Qux', $form->Fields()->fieldByName('Borris')->Title()); 'insertBefore' => 'URL'
} ));
public function testInsertBefore() { $fields = $form->Fields();
$this->assertEquals('Title', $fields[0]->Title());
$form = $this->form->enableSpamProtection(array( $this->assertEquals('Comment', $fields[1]->Title());
'protector' => 'FormSpamProtectionExtensionTest_FooProtector', $this->assertEquals('Foo', $fields[2]->Title());
'insertBefore' => 'URL' $this->assertEquals('URL', $fields[3]->Title());
)); }
$fields = $form->Fields(); public function testInsertBeforeMissing()
$this->assertEquals('Title', $fields[0]->Title()); {
$this->assertEquals('Comment', $fields[1]->Title()); $form = $this->form->enableSpamProtection(array(
$this->assertEquals('Foo', $fields[2]->Title()); 'protector' => 'FormSpamProtectionExtensionTest_FooProtector',
$this->assertEquals('URL', $fields[3]->Title()); 'insertBefore' => 'NotAField'
} ));
public function testInsertBeforeMissing() { // field should default to the end instead
$fields = $form->Fields();
$form = $this->form->enableSpamProtection(array( $this->assertEquals('Title', $fields[0]->Title());
'protector' => 'FormSpamProtectionExtensionTest_FooProtector', $this->assertEquals('Comment', $fields[1]->Title());
'insertBefore' => 'NotAField' $this->assertEquals('URL', $fields[2]->Title());
)); $this->assertEquals('Foo', $fields[3]->Title());
}
// field should default to the end instead
$fields = $form->Fields();
$this->assertEquals('Title', $fields[0]->Title());
$this->assertEquals('Comment', $fields[1]->Title());
$this->assertEquals('URL', $fields[2]->Title());
$this->assertEquals('Foo', $fields[3]->Title());
}
} }
/** /**
* @package spamprotection * @package spamprotection
*/ */
class FormSpamProtectionExtensionTest_BazProtector implements SpamProtector, TestOnly { class FormSpamProtectionExtensionTest_BazProtector implements SpamProtector, TestOnly
{
public function getFormField($name = null, $title = null, $value = null) { public function getFormField($name = null, $title = null, $value = null)
return new TextField($name, $title, $value); {
} return new TextField($name, $title, $value);
}
public function setFieldMapping($fieldMapping) {}
public function setFieldMapping($fieldMapping)
{
}
} }
/** /**
* @package spamprotection * @package spamprotection
*/ */
class FormSpamProtectionExtensionTest_BarProtector implements SpamProtector, TestOnly { class FormSpamProtectionExtensionTest_BarProtector implements SpamProtector, TestOnly
{
public function getFormField($name = null, $title = null, $value = null) { public function getFormField($name = null, $title = null, $value = null)
$title = $title ?: 'Bar'; {
return new TextField($name, $title, $value); $title = $title ?: 'Bar';
} return new TextField($name, $title, $value);
}
public function setFieldMapping($fieldMapping) {}
public function setFieldMapping($fieldMapping)
{
}
} }
/** /**
* @package spamprotection * @package spamprotection
*/ */
class FormSpamProtectionExtensionTest_FooProtector implements SpamProtector, TestOnly { class FormSpamProtectionExtensionTest_FooProtector implements SpamProtector, TestOnly
{
public function getFormField($name = null, $title = null, $value = null)
{
return new TextField($name, 'Foo', $value);
}
public function getFormField($name = null, $title = null, $value = null) { public function setFieldMapping($fieldMapping)
return new TextField($name, 'Foo', $value); {
} }
}
public function setFieldMapping($fieldMapping) {}
}