From ef3e6d45b6d685b4e8b08986b1c1d09c30bd1357 Mon Sep 17 00:00:00 2001 From: James Ellis Date: Thu, 24 Aug 2017 20:16:19 +1000 Subject: [PATCH] Configurable fieldname (#56) * Allow for a configurable field name * Add a test for configurable field name * Remove extra conditional by storing the field name in a private static * No need for field_name config removal as it's now specified as a class property --- .../extensions/FormSpamProtectionExtension.php | 11 ++++++++++- tests/FormSpamProtectionExtensionTest.php | 18 ++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/code/extensions/FormSpamProtectionExtension.php b/code/extensions/FormSpamProtectionExtension.php index 7346d5e..38f6aa8 100644 --- a/code/extensions/FormSpamProtectionExtension.php +++ b/code/extensions/FormSpamProtectionExtension.php @@ -41,6 +41,15 @@ class FormSpamProtectionExtension extends Extension 'authorId' ); + /** + * @config + * + * The field name to use for the {@link SpamProtector} {@link FormField} + * + * @var string $spam_protector + */ + private static $field_name = "Captcha"; + /** * Instantiate a SpamProtector instance * @@ -75,7 +84,7 @@ class FormSpamProtectionExtension extends Extension if (isset($options['name'])) { $name = $options['name']; } else { - $name = 'Captcha'; + $name = Config::inst()->get('FormSpamProtectionExtension', 'field_name'); } // captcha field title diff --git a/tests/FormSpamProtectionExtensionTest.php b/tests/FormSpamProtectionExtensionTest.php index 91111b5..1df952e 100644 --- a/tests/FormSpamProtectionExtensionTest.php +++ b/tests/FormSpamProtectionExtensionTest.php @@ -67,6 +67,24 @@ class FormSpamProtectionExtensionTest extends SapphireTest $this->assertEquals('Qux', $form->Fields()->fieldByName('Borris')->Title()); } + public function testConfigurableName() + { + $field_name = "test_configurable_name"; + Config::inst()->update( + 'FormSpamProtectionExtension', 'default_spam_protector', + 'FormSpamProtectionExtensionTest_FooProtector' + ); + Config::inst()->update( + 'FormSpamProtectionExtension', 'field_name', + $field_name + ); + $form = $this->form->enableSpamProtection(); + // remove for subsequent tests + Config::inst()->remove('FormSpamProtectionExtension', 'field_name'); + // field should take up configured name + $this->assertEquals('Foo', $form->Fields()->fieldByName($field_name)->Title()); + } + public function testInsertBefore() { $form = $this->form->enableSpamProtection(array(