FIX Missing syntax opening in readme, and tweaks for pre-emptive userforms compatibility

This commit is contained in:
Robbie Averill 2017-08-28 13:44:53 +12:00
parent a1ea0df540
commit 680411e2ac
4 changed files with 239 additions and 244 deletions

View File

@ -101,6 +101,7 @@ be set as the spam protector. The `getFormField()` method returns the
`FormField` to be inserted into the `Form`. The `FormField` returned should be `FormField` to be inserted into the `Form`. The `FormField` returned should be
in charge of the validation process. in charge of the validation process.
```php
<?php <?php
use CaptchaField; use CaptchaField;

View File

@ -2,17 +2,25 @@
namespace SilverStripe\SpamProtection; namespace SilverStripe\SpamProtection;
use SilverStripe\Core\ClassInfo;
use SilverStripe\Core\Convert;
use SilverStripe\Forms\DropdownField; use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\FieldGroup; use SilverStripe\Forms\FieldGroup;
use SilverStripe\Forms\FieldList; use SilverStripe\Forms\FieldList;
use SilverStripe\SpamProtection\Extension\FormSpamProtectionExtension; use SilverStripe\Forms\FormField;
use SilverStripe\ORM\UnsavedRelationList; use SilverStripe\ORM\UnsavedRelationList;
use SilverStripe\SpamProtection\Extension\FormSpamProtectionExtension;
use SilverStripe\UserForms\Model\EditableFormField;
use SilverStripe\UserForms\Model\EditableFormField\EditableEmailField;
use SilverStripe\UserForms\Model\EditableFormField\EditableNumericField;
use SilverStripe\UserForms\Model\EditableFormField\EditableTextField;
// @todo /**
use EditableEmailField; * @todo The userforms namespaces may still change, as the branch is not merged in yet
use EditableFormField; */
use EditableNumericField; if (!class_exists(EditableFormFields::class)) {
use EditableTextField; return;
}
/** /**
* Editable Spam Protecter Field. Used with the User Defined Forms module (if * Editable Spam Protecter Field. Used with the User Defined Forms module (if
@ -20,8 +28,6 @@ use EditableTextField;
* *
* @package spamprotection * @package spamprotection
*/ */
// @todo update namespaced for userforms when it is 4.0 compatible
if (class_exists('EditableFormField')) {
class EditableSpamProtectionField extends EditableFormField class EditableSpamProtectionField extends EditableFormField
{ {
private static $singular_name = 'Spam Protection Field'; private static $singular_name = 'Spam Protection Field';
@ -96,7 +102,7 @@ if (class_exists('EditableFormField')) {
{ {
// Get list of all configured classes available for spam detection // Get list of all configured classes available for spam detection
$types = self::config()->check_fields; $types = $this->config()->get('check_fields');
$typesInherit = array(); $typesInherit = array();
foreach ($types as $type) { foreach ($types as $type) {
$subTypes = ClassInfo::subclassesFor($type); $subTypes = ClassInfo::subclassesFor($type);
@ -111,16 +117,6 @@ if (class_exists('EditableFormField')) {
->exclude('Title', ''); // Ignore this field and those without titles ->exclude('Title', ''); // Ignore this field and those without titles
} }
/**
* This method is in place for userforms 2.x
*
* @deprecated 3.0 Please use {@link getCMSFields()} instead
*/
public function getFieldConfiguration()
{
return $this->getCMSFields();
}
/** /**
* Write the spam field mapping values to a serialised DB field * Write the spam field mapping values to a serialised DB field
* *
@ -156,10 +152,12 @@ if (class_exists('EditableFormField')) {
// Get protector // Get protector
$protector = FormSpamProtectionExtension::get_protector(); $protector = FormSpamProtectionExtension::get_protector();
if (!$protector) { if (!$protector) {
var_dump('a');
return $fields; return $fields;
} }
if ($this->Parent()->Fields() instanceof UnsavedRelationList) { if ($this->Parent()->Fields() instanceof UnsavedRelationList) {
var_dump('b');
return $fields; return $fields;
} }
@ -173,7 +171,7 @@ if (class_exists('EditableFormField')) {
)); ));
// Generate field specific settings // Generate field specific settings
$mappableFields = Config::inst()->get(FormSpamProtectionExtension::class, 'mappable_fields'); $mappableFields = FormSpamProtectionExtension::config()->get('mappable_fields');
$mappableFieldsMerged = array_combine($mappableFields, $mappableFields); $mappableFieldsMerged = array_combine($mappableFields, $mappableFields);
foreach ($this->getCandidateFields() as $otherField) { foreach ($this->getCandidateFields() as $otherField) {
$mapSetting = "Map-{$otherField->Name}"; $mapSetting = "Map-{$otherField->Name}";
@ -255,7 +253,7 @@ if (class_exists('EditableFormField')) {
public function getFieldValidationOptions() public function getFieldValidationOptions()
{ {
return new FieldList(); return FieldList::create();
} }
public function getRequired() public function getRequired()
@ -273,4 +271,3 @@ if (class_exists('EditableFormField')) {
return false; return false;
} }
} }
}

View File

@ -3,6 +3,7 @@
namespace SilverStripe\SpamProtection\Extension; namespace SilverStripe\SpamProtection\Extension;
use SilverStripe\Core\Config\Config; use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Config\Configurable;
use SilverStripe\Core\Extension; use SilverStripe\Core\Extension;
use SilverStripe\Core\Injector\Injector; use SilverStripe\Core\Injector\Injector;
@ -15,6 +16,8 @@ use SilverStripe\Core\Injector\Injector;
class FormSpamProtectionExtension extends Extension class FormSpamProtectionExtension extends Extension
{ {
use Configurable;
/** /**
* @config * @config
* *

View File

@ -2,14 +2,16 @@
namespace SilverStripe\SpamProtection\Tests; namespace SilverStripe\SpamProtection\Tests;
use UserDefinedForm; use SilverStripe\UserForms\Model\UserDefinedForm;
use SilverStripe\Core\Config\Config; use SilverStripe\Core\Config\Config;
use SilverStripe\Dev\SapphireTest; use SilverStripe\Dev\SapphireTest;
use SilverStripe\Forms\FieldGroup;
use SilverStripe\Forms\Form; use SilverStripe\Forms\Form;
use SilverStripe\Forms\RequiredFields; use SilverStripe\Forms\RequiredFields;
use SilverStripe\Forms\TextField;
use SilverStripe\SpamProtection\EditableSpamProtectionField; use SilverStripe\SpamProtection\EditableSpamProtectionField;
use SilverStripe\SpamProtection\Extension\FormSpamProtectionExtension; use SilverStripe\SpamProtection\Extension\FormSpamProtectionExtension;
use SilverStripe\SpamProtection\Tests\EditableSpamProtectionFieldTest\Protector; use SilverStripe\SpamProtection\Tests\Stub\Protector;
class EditableSpamProtectionFieldTest extends SapphireTest class EditableSpamProtectionFieldTest extends SapphireTest
{ {
@ -19,7 +21,7 @@ class EditableSpamProtectionFieldTest extends SapphireTest
{ {
parent::setUp(); parent::setUp();
if (!class_exists('EditableSpamProtectionField')) { if (!class_exists(EditableSpamProtectionField::class)) {
$this->markTestSkipped('"userforms" module not installed'); $this->markTestSkipped('"userforms" module not installed');
} }
@ -64,12 +66,7 @@ class EditableSpamProtectionFieldTest extends SapphireTest
$formMock $formMock
->expects($this->once()) ->expects($this->once())
->method('sessionMessage') ->method('sessionMessage')
->with( ->with($this->stringContains('some field message'), $this->anything());
$this->anything(),
$this->stringContains('some field message'),
$this->anything(),
$this->anything()
);
$formFieldMock->validateField(array('MyField' => null), $formMock); $formFieldMock->validateField(array('MyField' => null), $formMock);
} }
@ -89,13 +86,8 @@ class EditableSpamProtectionFieldTest extends SapphireTest
$formMock $formMock
->expects($this->once()) ->expects($this->once())
->method('sessionMessage') ->method('sessionError')
->with( ->with($this->stringContains('default error message'));
$this->anything(),
$this->stringContains('default error message'),
$this->anything(),
$this->anything()
);
$formFieldMock->validateField(array('MyField' => null), $formMock); $formFieldMock->validateField(array('MyField' => null), $formMock);
} }
@ -105,7 +97,7 @@ class EditableSpamProtectionFieldTest extends SapphireTest
$field = $this->getEditableFormFieldMock(); $field = $this->getEditableFormFieldMock();
$fields = $field->getCMSFields(); $fields = $field->getCMSFields();
$this->assertInstanceOf('FieldGroup', $fields->fieldByName('Root.Main.SpamFieldMapping')); $this->assertInstanceOf(FieldGroup::class, $fields->fieldByName('Root.Main.SpamFieldMapping'));
} }
public function testSpamMapSettingsAreSerialised() public function testSpamMapSettingsAreSerialised()
@ -121,9 +113,11 @@ class EditableSpamProtectionFieldTest extends SapphireTest
protected function getFormMock() protected function getFormMock()
{ {
$formMock = $this->getMockBuilder(Form::class, array('sessionMessage')) $formMock = $this->getMockBuilder(Form::class)
->setMethods(['sessionMessage', 'sessionError', 'getValidator'])
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$formMock $formMock
->expects($this->any()) ->expects($this->any())
->method('getValidator') ->method('getValidator')
@ -137,7 +131,7 @@ class EditableSpamProtectionFieldTest extends SapphireTest
$page = new UserDefinedForm(); $page = new UserDefinedForm();
$page->write(); $page->write();
$formFieldMock = $this->getMockBuilder('TextField') $formFieldMock = $this->getMockBuilder(TextField::class)
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();