From e9880ca3e5ec9a7339afd6aad51aaf47616ef20a Mon Sep 17 00:00:00 2001 From: Daniel Hensby Date: Tue, 31 Jan 2017 17:45:39 +0000 Subject: [PATCH 1/7] Fixing travis setup --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 0229e29..d9d013e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -28,7 +28,7 @@ matrix: before_script: - composer self-update || true - git clone git://github.com/silverstripe-labs/silverstripe-travis-support.git ~/travis-support - - php ~/travis-support/travis_setup.php --source `pwd` --target ~/builds/ss + - php ~/travis-support/travis_setup.php --source `pwd` --target ~/builds/ss --require silverstripe/cms:$CORE_RELEASE.x-dev - cd ~/builds/ss - composer install - composer require silverstripe/userforms From 56ec1f42c625bde31f7e13d56cd9cae99a2727f3 Mon Sep 17 00:00:00 2001 From: Brett Tasker Date: Thu, 9 Feb 2017 11:28:52 +1300 Subject: [PATCH 2/7] Update translations --- lang/fi.yml | 6 ++++++ lang/fi_FI.yml | 6 ++++++ 2 files changed, 12 insertions(+) create mode 100644 lang/fi.yml create mode 100644 lang/fi_FI.yml diff --git a/lang/fi.yml b/lang/fi.yml new file mode 100644 index 0000000..fc742b6 --- /dev/null +++ b/lang/fi.yml @@ -0,0 +1,6 @@ +fi: + EditableSpamProtectionField: + PLURALNAME: 'Roskapostisuojauksen kentät' + SINGULARNAME: 'Roskapostisuojauksen kenttä' + SPAMFIELDMAPPING: 'Roskapostikentän kuvaus' + SPAMFIELDMAPPINGDESCRIPTION: 'Valitse lomakekenttä, joka vastaa mitä tahansa oleellista roskapostisuodatuksen tunnistetta' diff --git a/lang/fi_FI.yml b/lang/fi_FI.yml new file mode 100644 index 0000000..9ef2693 --- /dev/null +++ b/lang/fi_FI.yml @@ -0,0 +1,6 @@ +fi_FI: + EditableSpamProtectionField: + PLURALNAME: 'Roskapostisuotimen kentät' + SINGULARNAME: 'Roskapostisuotimen kenttä' + SPAMFIELDMAPPING: 'Roskapostikentän kuvaus' + SPAMFIELDMAPPINGDESCRIPTION: 'Valitse lomakekentät, jotka vastaavat mitä tahansa oleellista roskapostisuotimen tunnistetta' From 4cd01b2c82ae0cdfa8f7b8713fc6028ea8fc25d0 Mon Sep 17 00:00:00 2001 From: Robbie Averill Date: Thu, 15 Jun 2017 11:36:19 +1200 Subject: [PATCH 3/7] Add PHP7 + SS3.6 build to Travis configuration --- .travis.yml | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/.travis.yml b/.travis.yml index d9d013e..20e51bf 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,4 +1,4 @@ -# See https://github.com/silverstripe-labs/silverstripe-travis-support for setup details +# See https://github.com/silverstripe/silverstripe-travis-support for setup details sudo: false @@ -8,11 +8,9 @@ php: - 5.3 - 5.4 - 5.5 - - 5.6 - - 7.0 env: - - DB=MYSQL CORE_RELEASE=3.2 + - DB=MYSQL CORE_RELEASE=3.5 matrix: include: @@ -22,13 +20,13 @@ matrix: env: DB=MYSQL CORE_RELEASE=3.1 - php: 5.6 env: DB=PGSQL CORE_RELEASE=3.2 - allow_failures: - - php: 7.0 + - php: 7.1 + env: DB=MYSQL CORE_RELEASE=3.6 before_script: - composer self-update || true - - git clone git://github.com/silverstripe-labs/silverstripe-travis-support.git ~/travis-support - - php ~/travis-support/travis_setup.php --source `pwd` --target ~/builds/ss --require silverstripe/cms:$CORE_RELEASE.x-dev + - git clone git://github.com/silverstripe/silverstripe-travis-support.git ~/travis-support + - php ~/travis-support/travis_setup.php --source `pwd` --target ~/builds/ss - cd ~/builds/ss - composer install - composer require silverstripe/userforms From 3d771d81796a373402af9f99eb7d2579a5226546 Mon Sep 17 00:00:00 2001 From: Robbie Averill Date: Thu, 15 Jun 2017 13:24:36 +1200 Subject: [PATCH 4/7] Move userforms requirement to travis setup call --- .travis.yml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 20e51bf..198f057 100644 --- a/.travis.yml +++ b/.travis.yml @@ -26,10 +26,8 @@ matrix: before_script: - composer self-update || true - git clone git://github.com/silverstripe/silverstripe-travis-support.git ~/travis-support - - php ~/travis-support/travis_setup.php --source `pwd` --target ~/builds/ss + - php ~/travis-support/travis_setup.php --source `pwd` --target ~/builds/ss --require silverstripe/userforms - cd ~/builds/ss - - composer install - - composer require silverstripe/userforms script: - vendor/bin/phpunit spamprotection/tests From 5cc9312a8d6f3358e28f365c2708c8387cbb5d26 Mon Sep 17 00:00:00 2001 From: Robbie Averill Date: Fri, 7 Jul 2017 14:23:01 +1200 Subject: [PATCH 5/7] Move stub classes to their own files, run PSR-2 linter --- code/EditableSpamProtectionField.php | 4 +- code/SpamProtectorManager.php | 14 ++-- code/extensions/CommentSpamProtection.php | 2 +- .../FormSpamProtectionExtension.php | 4 +- code/interfaces/SpamProtector.php | 8 +-- tests/EditableSpamProtectionFieldTest.php | 39 +++-------- tests/FormSpamProtectionExtensionTest.php | 64 +++---------------- ...tableSpamProtectionFieldTest_Protector.php | 13 ++++ ...amProtectionExtensionTest_BarProtector.php | 17 +++++ ...amProtectionExtensionTest_BazProtector.php | 16 +++++ ...amProtectionExtensionTest_FooProtector.php | 16 +++++ 11 files changed, 99 insertions(+), 98 deletions(-) create mode 100644 tests/Stub/EditableSpamProtectionFieldTest_Protector.php create mode 100644 tests/Stub/FormSpamProtectionExtensionTest_BarProtector.php create mode 100644 tests/Stub/FormSpamProtectionExtensionTest_BazProtector.php create mode 100644 tests/Stub/FormSpamProtectionExtensionTest_FooProtector.php diff --git a/code/EditableSpamProtectionField.php b/code/EditableSpamProtectionField.php index 24a89e7..bc68ebf 100644 --- a/code/EditableSpamProtectionField.php +++ b/code/EditableSpamProtectionField.php @@ -134,12 +134,12 @@ if (class_exists('EditableFormField')) { * Using custom validateField method * as Spam Protection Field implementations may have their own error messages * and may not be based on the field being required, e.g. Honeypot Field - * + * * @param array $data * @param Form $form * @return void */ - public function validateField($data, $form) + public function validateField($data, $form) { $formField = $this->getFormField(); $formField->setForm($form); diff --git a/code/SpamProtectorManager.php b/code/SpamProtectorManager.php index 319f38d..00eb646 100644 --- a/code/SpamProtectorManager.php +++ b/code/SpamProtectorManager.php @@ -1,6 +1,6 @@ enableSpamProtection() for adding spamprotection' ); diff --git a/code/extensions/CommentSpamProtection.php b/code/extensions/CommentSpamProtection.php index e5aca62..453707d 100644 --- a/code/extensions/CommentSpamProtection.php +++ b/code/extensions/CommentSpamProtection.php @@ -1,4 +1,4 @@ -markTestSkipped('"userforms" module not installed'); + } + Config::inst()->update( - 'FormSpamProtectionExtension', 'default_spam_protector', + 'FormSpamProtectionExtension', + 'default_spam_protector', 'EditableSpamProtectionFieldTest_Protector' ); } public function testValidateFieldDoesntAddErrorOnSuccess() { - if (!class_exists('EditableSpamProtectionField')) { - $this->markTestSkipped('"userforms" module not installed'); - } - $formMock = $this->getFormMock(); $formFieldMock = $this->getEditableFormFieldMock(); @@ -39,10 +40,6 @@ class EditableSpamProtectionFieldTest extends SapphireTest public function testValidateFieldAddsErrorFromField() { - if (!class_exists('EditableSpamProtectionField')) { - $this->markTestSkipped('"userforms" module not installed'); - } - $formMock = $this->getFormMock(); $formFieldMock = $this->getEditableFormFieldMock(); @@ -57,17 +54,14 @@ class EditableSpamProtectionFieldTest extends SapphireTest $formMock ->expects($this->once()) ->method('addErrorMessage') - ->with($this->anything(), $this->stringContains('some field message'), $this->anything(), $this->anything());; + ->with($this->anything(), $this->stringContains('some field message'), $this->anything(), $this->anything()); + ; $formFieldMock->validateField(array('MyField' => null), $formMock); } public function testValidateFieldAddsDefaultError() { - if (!class_exists('EditableSpamProtectionField')) { - $this->markTestSkipped('"userforms" module not installed'); - } - $formMock = $this->getFormMock(); $formFieldMock = $this->getEditableFormFieldMock(); @@ -119,17 +113,4 @@ class EditableSpamProtectionFieldTest extends SapphireTest return $editableFormFieldMock; } - -} - -class EditableSpamProtectionFieldTest_Protector implements SpamProtector, TestOnly -{ - public function getFormField($name = null, $title = null, $value = null) - { - return new TextField($name, 'Foo', $value); - } - - public function setFieldMapping($fieldMapping) - { - } } diff --git a/tests/FormSpamProtectionExtensionTest.php b/tests/FormSpamProtectionExtensionTest.php index 91111b5..0ed82c3 100644 --- a/tests/FormSpamProtectionExtensionTest.php +++ b/tests/FormSpamProtectionExtensionTest.php @@ -11,7 +11,7 @@ class FormSpamProtectionExtensionTest extends SapphireTest * @var Form */ protected $form = null; - + public function setUp() { parent::setUp(); @@ -20,15 +20,15 @@ class FormSpamProtectionExtensionTest extends SapphireTest new TextField('Title'), new TextField('Comment'), new TextField('URL') - ), new FieldList() - ); + ), new FieldList()); $this->form->disableSecurityToken(); } public function testEnableSpamProtection() { Config::inst()->update( - 'FormSpamProtectionExtension', 'default_spam_protector', + 'FormSpamProtectionExtension', + 'default_spam_protector', 'FormSpamProtectionExtensionTest_FooProtector' ); @@ -52,7 +52,7 @@ class FormSpamProtectionExtensionTest extends SapphireTest 'protector' => 'FormSpamProtectionExtensionTest_BarProtector', 'title' => 'Baz', )); - + $this->assertEquals('Baz', $form->Fields()->fieldByName('Captcha')->Title()); } @@ -66,28 +66,28 @@ class FormSpamProtectionExtensionTest extends SapphireTest $this->assertEquals('Qux', $form->Fields()->fieldByName('Borris')->Title()); } - + public function testInsertBefore() { $form = $this->form->enableSpamProtection(array( 'protector' => 'FormSpamProtectionExtensionTest_FooProtector', 'insertBefore' => 'URL' )); - + $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()); @@ -96,49 +96,3 @@ class FormSpamProtectionExtensionTest extends SapphireTest $this->assertEquals('Foo', $fields[3]->Title()); } } - -/** - * @package spamprotection - */ -class FormSpamProtectionExtensionTest_BazProtector implements SpamProtector, TestOnly -{ - public function getFormField($name = null, $title = null, $value = null) - { - return new TextField($name, $title, $value); - } - - public function setFieldMapping($fieldMapping) - { - } -} - -/** - * @package spamprotection - */ -class FormSpamProtectionExtensionTest_BarProtector implements SpamProtector, TestOnly -{ - public function getFormField($name = null, $title = null, $value = null) - { - $title = $title ?: 'Bar'; - return new TextField($name, $title, $value); - } - - public function setFieldMapping($fieldMapping) - { - } -} - -/** - * @package spamprotection - */ -class FormSpamProtectionExtensionTest_FooProtector implements SpamProtector, TestOnly -{ - public function getFormField($name = null, $title = null, $value = null) - { - return new TextField($name, 'Foo', $value); - } - - public function setFieldMapping($fieldMapping) - { - } -} diff --git a/tests/Stub/EditableSpamProtectionFieldTest_Protector.php b/tests/Stub/EditableSpamProtectionFieldTest_Protector.php new file mode 100644 index 0000000..eead4e5 --- /dev/null +++ b/tests/Stub/EditableSpamProtectionFieldTest_Protector.php @@ -0,0 +1,13 @@ + Date: Fri, 7 Jul 2017 16:09:39 +1200 Subject: [PATCH 6/7] FIX Compatibility with userforms 3/4 getCMSFields, remove deprecated getSettings use etc --- code/EditableSpamProtectionField.php | 85 +++++++++++++++++++---- composer.json | 2 +- tests/EditableSpamProtectionFieldTest.php | 19 +++++ 3 files changed, 93 insertions(+), 13 deletions(-) diff --git a/code/EditableSpamProtectionField.php b/code/EditableSpamProtectionField.php index bc68ebf..8993f28 100644 --- a/code/EditableSpamProtectionField.php +++ b/code/EditableSpamProtectionField.php @@ -24,6 +24,10 @@ if (class_exists('EditableFormField')) { 'EditableNumericField' ); + private static $db = array( + 'SpamFieldSettings' => 'Text' + ); + /** * @var FormField */ @@ -45,7 +49,7 @@ if (class_exists('EditableFormField')) { $fieldMapping = array(); foreach ($this->getCandidateFields() as $otherField) { $mapSetting = "Map-{$otherField->Name}"; - $spamField = $this->getSetting($mapSetting); + $spamField = $this->spamMapValue($mapSetting); $fieldMapping[$otherField->Name] = $spamField; } $protector->setFieldMapping($fieldMapping); @@ -89,9 +93,47 @@ if (class_exists('EditableFormField')) { ->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() { - $fields = parent::getFieldConfiguration(); + return $this->getCMSFields(); + } + + /** + * Write the spam field mapping values to a serialised DB field + * + * {@inheritDoc} + */ + public function onBeforeWrite() + { + $fieldMap = Convert::json2array($this->SpamFieldSettings); + if (empty($fieldMap)) { + $fieldMap = array(); + } + + foreach ($this->record as $key => $value) { + if (substr($key, 0, 8) === 'spammap-') { + $fieldMap[substr($key, 8)] = $value; + } + } + $this->setField('SpamFieldSettings', Convert::raw2json($fieldMap)); + + return parent::onBeforeWrite(); + } + + /** + * Used in userforms 3.x and above + * + * {@inheritDoc} + */ + public function getCMSFields() + { + /** @var FieldList $fields */ + $fields = parent::getCMSFields(); // Get protector $protector = FormSpamProtectionExtension::get_protector(); @@ -104,13 +146,13 @@ if (class_exists('EditableFormField')) { } // Each other text field in this group can be assigned a field mapping - $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' - )); + $mapGroup = FieldGroup::create() + ->setTitle(_t('EditableSpamProtectionField.SPAMFIELDMAPPING', 'Spam Field Mapping')) + ->setName('SpamFieldMapping') + ->setDescription(_t( + 'EditableSpamProtectionField.SPAMFIELDMAPPINGDESCRIPTION', + 'Select the form fields that correspond to any relevant spam protection identifiers' + )); // Generate field specific settings $mappableFields = Config::inst()->get('FormSpamProtectionExtension', 'mappable_fields'); @@ -118,18 +160,37 @@ if (class_exists('EditableFormField')) { foreach ($this->getCandidateFields() as $otherField) { $mapSetting = "Map-{$otherField->Name}"; $fieldOption = DropdownField::create( - $this->getSettingName($mapSetting), + 'spammap-' . $mapSetting, $otherField->Title, $mappableFieldsMerged, - $this->getSetting($mapSetting) + $this->spamMapValue($mapSetting) )->setEmptyString(''); $mapGroup->push($fieldOption); } - $fields->insertBefore($mapGroup, $this->getSettingName('ExtraClass')); + $fields->addFieldToTab('Root.Main', $mapGroup); return $fields; } + /** + * Try to retrieve a value for the given spam field map name from the serialised data + * + * @param string $mapSetting + * @return string + */ + public function spamMapValue($mapSetting) + { + $map = Convert::json2array($this->SpamFieldSettings); + if (empty($map)) { + $map = array(); + } + + if (array_key_exists($mapSetting, $map)) { + return $map[$mapSetting]; + } + return ''; + } + /** * Using custom validateField method * as Spam Protection Field implementations may have their own error messages diff --git a/composer.json b/composer.json index f6ff030..9084db8 100644 --- a/composer.json +++ b/composer.json @@ -24,4 +24,4 @@ }, "extra": [], "license": "BSD-3-Clause" -} \ No newline at end of file +} diff --git a/tests/EditableSpamProtectionFieldTest.php b/tests/EditableSpamProtectionFieldTest.php index 228711d..5a5660f 100644 --- a/tests/EditableSpamProtectionFieldTest.php +++ b/tests/EditableSpamProtectionFieldTest.php @@ -81,6 +81,25 @@ class EditableSpamProtectionFieldTest extends SapphireTest $formFieldMock->validateField(array('MyField' => null), $formMock); } + public function testSpamConfigurationShowsInCms() + { + $field = $this->getEditableFormFieldMock(); + $fields = $field->getCMSFields(); + + $this->assertInstanceOf('FieldGroup', $fields->fieldByName('Root.Main.SpamFieldMapping')); + } + + public function testSpamMapSettingsAreSerialised() + { + $field = $this->getEditableFormFieldMock(); + $field->SpamFieldSettings = json_encode(array('foo' => 'bar', 'bar' => 'baz')); + $field->write(); + + $this->assertJson($field->SpamFieldSettings); + $this->assertSame('bar', $field->spamMapValue('foo')); + $this->assertSame('baz', $field->spamMapValue('bar')); + } + protected function getFormMock() { $formMock = $this->getMockBuilder('Form', array('addErrorMessage')) From 27da2d6ab4a95819456feb89b8b87bc3faff2c60 Mon Sep 17 00:00:00 2001 From: Robbie Averill Date: Mon, 28 Aug 2017 11:18:51 +1200 Subject: [PATCH 7/7] Add composer alias for 2.1.x-dev --- composer.json | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 9084db8..3e7bfb6 100644 --- a/composer.json +++ b/composer.json @@ -22,6 +22,10 @@ "require-dev": { "phpunit/PHPUnit": "~3.7@stable" }, - "extra": [], + "extra": { + "branch-alias": { + "2.x-dev": "2.1.x-dev" + } + }, "license": "BSD-3-Clause" }