Merge pull request #31 from helpfulrobot/convert-to-psr-2

Converted to PSR-2
This commit is contained in:
Daniel Hensby 2015-11-25 15:31:30 +00:00
commit 965389ded9
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)
{
Deprecation::notice('1.1',
'SpamProtectorManager::set_spam_protector() is deprecated. '.
'Use the new config system. FormSpamProtectorExtension.default_spam_protector'
);
public static function set_spam_protector($protector) { self::$spam_protector = $protector;
Deprecation::notice('1.1', }
'SpamProtectorManager::set_spam_protector() is deprecated. '.
'Use the new config system. FormSpamProtectorExtension.default_spam_protector'
);
self::$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');
public static function get_spam_protector() { return self::$spam_protector;
Deprecation::notice('1.1', }
'SpamProtectorManager::get_spam_protector() is deprecated'.
'Use the new config system. FormSpamProtectorExtension.default_spam_protector');
return self::$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'
);
public static function update_form($form, $before = null, $fieldsToSpamServiceMapping = array(), $title = null, $rightTitle = null) { return $form->enableSpamProtection();
Deprecation::notice('1.1', }
'SpamProtectorManager::update_form is deprecated'.
'Please use $form->enableSpamProtection() for adding spamprotection'
);
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'
);
/** /**
* @config * Instantiate a SpamProtector instance
* *
* The {@link enableSpamProtection} method will define which of the form * @param array $options Configuration options
* values correlates to this form mapped fields list. Totally custom forms * @return SpamProtector
* and subclassed SpamProtector instances are define their own mapping */
* public static function get_protector($options = null)
* @var array $mappable_fields {
*/ // generate the spam protector
private static $mappable_fields = array( if (isset($options['protector'])) {
'id', $protector = $options['protector'];
'title', } else {
'body', $protector = Config::inst()->get('FormSpamProtectionExtension', 'default_spam_protector');
'contextUrl', }
'contextTitle',
'authorName',
'authorMail',
'authorUrl',
'authorIp',
'authorId'
);
/** if ($protector && class_exists($protector)) {
* Instantiate a SpamProtector instance return Injector::inst()->create($protector);
* } else {
* @param array $options Configuration options return null;
* @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)
* Activates the spam protection module. if (isset($options['name'])) {
* $name = $options['name'];
* @param array $options } else {
*/ $name = 'Captcha';
public function enableSpamProtection($options = array()) { }
// captcha form field name (must be unique) // captcha field title
if(isset($options['name'])) { if (isset($options['title'])) {
$name = $options['name']; $title = $options['title'];
} else { } else {
$name = 'Captcha'; $title = '';
} }
// 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 before field specified by insertBefore
// add the form field $inserted = false;
if($field = $protector->getFormField($name, $title)) { if (!empty($options['insertBefore'])) {
$field->setForm($this->owner); $inserted = $this->owner->Fields()->insertBefore($field, $options['insertBefore']);
}
if (!$inserted) {
// Add field to end if not added already
$this->owner->Fields()->push($field);
}
}
}
// Add before field specified by insertBefore return $this->owner;
$inserted = false; }
if(!empty($options['insertBefore'])) {
$inserted = $this->owner->Fields()->insertBefore($field, $options['insertBefore']);
}
if(!$inserted) {
// Add field to end if not added already
$this->owner->Fields()->push($field);
}
}
}
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.
*
* 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
* form field (i.e a hidden field and a text field) then return a
* {@link FieldGroup} from this method to include both.
*
* @param string $name
* @param string $title
* @param mixed $value
* @return FormField The resulting field
*/
public function getFormField($name = null, $title = null, $value = null);
/** /**
* Return the {@link FormField} associated with this protector. * Set the fields to map spam protection too
* *
* Most spam methods will simply return a piece of HTML to be injected at * @param array $fieldMapping array of Field Names, where the indexes of the array are
* the end of the form. If a spam method needs to inject more than one * the field names of the form and the values are the standard spamprotection
* form field (i.e a hidden field and a text field) then return a * fields used by the protector
* {@link FieldGroup} from this method to include both. */
* public function setFieldMapping($fieldMapping);
* @param string $name
* @param string $title
* @param mixed $value
* @return FormField The resulting field
*/
public function getFormField($name = null, $title = null, $value = null);
/**
* Set the fields to map spam protection too
*
* @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
* fields used by the protector
*/
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
*/
protected $form = null;
/** public function setUp()
* @var Form {
*/ parent::setUp();
protected $form = null;
public function setUp() { $this->form = new Form($this, 'Form', new FieldList(
parent::setUp(); new TextField('Title'),
new TextField('Comment'),
new TextField('URL')
), new FieldList()
);
$this->form->disableSecurityToken();
}
$this->form = new Form($this, 'Form', new FieldList( public function testEnableSpamProtection()
new TextField('Title'), {
new TextField('Comment'), Config::inst()->update(
new TextField('URL') 'FormSpamProtectionExtension', 'default_spam_protector',
), new FieldList() 'FormSpamProtectionExtensionTest_FooProtector'
); );
$this->form->disableSecurityToken();
}
public function testEnableSpamProtection() { $form = $this->form->enableSpamProtection();
Config::inst()->update(
'FormSpamProtectionExtension', 'default_spam_protector',
'FormSpamProtectionExtensionTest_FooProtector'
);
$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'
));
} $this->assertEquals('Bar', $form->Fields()->fieldByName('Captcha')->Title());
}
public function testEnableSpamProtectionCustomProtector() { public function testEnableSpamProtectionCustomTitle()
$form = $this->form->enableSpamProtection(array( {
'protector' => 'FormSpamProtectionExtensionTest_BarProtector' $form = $this->form->enableSpamProtection(array(
)); 'protector' => 'FormSpamProtectionExtensionTest_BarProtector',
'title' => 'Baz',
));
$this->assertEquals('Bar', $form->Fields()->fieldByName('Captcha')->Title()); $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()); $this->assertEquals('Qux', $form->Fields()->fieldByName('Borris')->Title());
} }
public function testCustomOptions() { public function testInsertBefore()
$form = $this->form->enableSpamProtection(array( {
'protector' => 'FormSpamProtectionExtensionTest_BazProtector', $form = $this->form->enableSpamProtection(array(
'title' => 'Qux', 'protector' => 'FormSpamProtectionExtensionTest_FooProtector',
'name' => 'Borris' 'insertBefore' => 'URL'
)); ));
$this->assertEquals('Qux', $form->Fields()->fieldByName('Borris')->Title()); $fields = $form->Fields();
} $this->assertEquals('Title', $fields[0]->Title());
$this->assertEquals('Comment', $fields[1]->Title());
$this->assertEquals('Foo', $fields[2]->Title());
$this->assertEquals('URL', $fields[3]->Title());
}
public function testInsertBefore() { public function testInsertBeforeMissing()
{
$form = $this->form->enableSpamProtection(array( $form = $this->form->enableSpamProtection(array(
'protector' => 'FormSpamProtectionExtensionTest_FooProtector', 'protector' => 'FormSpamProtectionExtensionTest_FooProtector',
'insertBefore' => 'URL' 'insertBefore' => 'NotAField'
)); ));
$fields = $form->Fields();
$this->assertEquals('Title', $fields[0]->Title());
$this->assertEquals('Comment', $fields[1]->Title());
$this->assertEquals('Foo', $fields[2]->Title());
$this->assertEquals('URL', $fields[3]->Title());
}
public function testInsertBeforeMissing() {
$form = $this->form->enableSpamProtection(array(
'protector' => 'FormSpamProtectionExtensionTest_FooProtector',
'insertBefore' => 'NotAField'
));
// 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());
}
// 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) { public function getFormField($name = null, $title = null, $value = null)
return new TextField($name, 'Foo', $value); {
} return new TextField($name, 'Foo', $value);
}
public function setFieldMapping($fieldMapping) {}
public function setFieldMapping($fieldMapping)
{
}
} }