diff --git a/README.md b/README.md index 1f418cd..98e9711 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ ## Requirements -SilverStripe 3.0.0 or greater +SilverStripe 4.0 or greater ## Documentation diff --git a/_config/spamprotection.yml b/_config/spamprotection.yml index b48c4bb..5d34909 100644 --- a/_config/spamprotection.yml +++ b/_config/spamprotection.yml @@ -3,4 +3,4 @@ name: spamprotection --- SilverStripe\Forms\Form: extensions: - - SilverStripe\Spamprotection\FormSpamProtectionExtension \ No newline at end of file + - SilverStripe\SpamProtection\Extension\FormSpamProtectionExtension diff --git a/code/EditableSpamProtectionField.php b/code/EditableSpamProtectionField.php index 34a4b9f..9322132 100644 --- a/code/EditableSpamProtectionField.php +++ b/code/EditableSpamProtectionField.php @@ -1,5 +1,18 @@ 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()); } } } diff --git a/code/extensions/CommentSpamProtection.php b/code/Extension/CommentSpamProtection.php similarity index 93% rename from code/extensions/CommentSpamProtection.php rename to code/Extension/CommentSpamProtection.php index ca96702..f01e44c 100644 --- a/code/extensions/CommentSpamProtection.php +++ b/code/Extension/CommentSpamProtection.php @@ -1,6 +1,6 @@ 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; } } diff --git a/code/interfaces/SpamProtector.php b/code/SpamProtector.php similarity index 96% rename from code/interfaces/SpamProtector.php rename to code/SpamProtector.php index f9bd3b3..e4a36e0 100644 --- a/code/interfaces/SpamProtector.php +++ b/code/SpamProtector.php @@ -1,5 +1,6 @@ enableSpamProtection() for adding spamprotection' - ); - - return $form->enableSpamProtection(); - } -} diff --git a/composer.json b/composer.json index 393f809..75de91e 100644 --- a/composer.json +++ b/composer.json @@ -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 } diff --git a/lang/de.yml b/lang/de.yml index b26ed56..06c7d4a 100644 --- a/lang/de.yml +++ b/lang/de.yml @@ -1,5 +1,5 @@ de: - EditableSpamProtectionField: + SilverStripe\SpamProtection\EditableSpamProtectionField: PLURALNAME: Spamschutzfelder SINGULARNAME: Spamschutzfeld SPAMFIELDMAPPING: 'Spamschutzfeld Zuordnung' diff --git a/lang/en.yml b/lang/en.yml index 03eef43..8d59053 100644 --- a/lang/en.yml +++ b/lang/en.yml @@ -1,5 +1,5 @@ en: - EditableSpamProtectionField: + SilverStripe\SpamProtection\EditableSpamProtectionField: PLURALNAME: 'Spam Protection Fields' SINGULARNAME: 'Spam Protection Field' SPAMFIELDMAPPING: 'Spam Field Mapping' diff --git a/lang/eo.yml b/lang/eo.yml index c1e9828..e41380c 100644 --- a/lang/eo.yml +++ b/lang/eo.yml @@ -1,5 +1,5 @@ eo: - EditableSpamProtectionField: + SilverStripe\SpamProtection\EditableSpamProtectionField: PLURALNAME: 'Spamprotektaj kampoj' SINGULARNAME: 'Spamprotekta kampo' SPAMFIELDMAPPING: 'Spamkampa mapigo' diff --git a/lang/es.yml b/lang/es.yml index cea540c..7b5258a 100644 --- a/lang/es.yml +++ b/lang/es.yml @@ -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' diff --git a/lang/fa_IR.yml b/lang/fa_IR.yml index a555f8f..888ffb9 100644 --- a/lang/fa_IR.yml +++ b/lang/fa_IR.yml @@ -1,5 +1,5 @@ fa_IR: - EditableSpamProtectionField: + SilverStripe\SpamProtection\EditableSpamProtectionField: PLURALNAME: 'فیلدهای محافظت از هرزنوشته' SINGULARNAME: 'فیلد محافظت از هرزنوشته' SPAMFIELDMAPPING: 'نقشه‌برداری فیلد هرزنوشته' diff --git a/lang/hr.yml b/lang/hr.yml index 5c45fbb..7055d28 100644 --- a/lang/hr.yml +++ b/lang/hr.yml @@ -1,5 +1,5 @@ hr: - EditableSpamProtectionField: + SilverStripe\SpamProtection\EditableSpamProtectionField: PLURALNAME: 'Polja Spam zaštite' SINGULARNAME: 'Polje Spam zaštite' SPAMFIELDMAPPING: 'Mapiranje polja Spama' diff --git a/lang/ru.yml b/lang/ru.yml index cf473f6..be73ebf 100644 --- a/lang/ru.yml +++ b/lang/ru.yml @@ -1,5 +1,5 @@ ru: - EditableSpamProtectionField: + SilverStripe\SpamProtection\EditableSpamProtectionField: PLURALNAME: 'Поля защиты от спама' SINGULARNAME: 'Поле защиты от спама' SPAMFIELDMAPPING: 'Привязка полей для защиты от спама' diff --git a/lang/sk.yml b/lang/sk.yml index 5ddf352..7bfa04d 100644 --- a/lang/sk.yml +++ b/lang/sk.yml @@ -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' diff --git a/tests/EditableSpamProtectionFieldTest.php b/tests/EditableSpamProtectionFieldTest.php index 5a5660f..2e35a06 100644 --- a/tests/EditableSpamProtectionFieldTest.php +++ b/tests/EditableSpamProtectionFieldTest.php @@ -1,11 +1,21 @@ 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 diff --git a/tests/FormSpamProtectionExtensionTest.php b/tests/FormSpamProtectionExtensionTest.php index 80ff1a9..08e2c31 100644 --- a/tests/FormSpamProtectionExtensionTest.php +++ b/tests/FormSpamProtectionExtensionTest.php @@ -1,5 +1,18 @@ 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' )); diff --git a/tests/FormSpamProtectionExtensionTest/BarProtector.php b/tests/FormSpamProtectionExtensionTest/BarProtector.php new file mode 100644 index 0000000..05fae5d --- /dev/null +++ b/tests/FormSpamProtectionExtensionTest/BarProtector.php @@ -0,0 +1,23 @@ +