Skip to content

Commit

Permalink
Configurable fieldname (#56)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
MrJamesEllis authored and dhensby committed Aug 24, 2017
1 parent a25b052 commit ef3e6d4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
11 changes: 10 additions & 1 deletion code/extensions/FormSpamProtectionExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -75,7 +84,7 @@ public function enableSpamProtection($options = array())
if (isset($options['name'])) {
$name = $options['name'];
} else {
$name = 'Captcha';
$name = Config::inst()->get('FormSpamProtectionExtension', 'field_name');
}

// captcha field title
Expand Down
18 changes: 18 additions & 0 deletions tests/FormSpamProtectionExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,24 @@ public function testCustomOptions()
$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(
Expand Down

0 comments on commit ef3e6d4

Please sign in to comment.