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
This commit is contained in:
James Ellis 2017-08-24 20:16:19 +10:00 committed by Daniel Hensby
parent a25b052510
commit ef3e6d45b6
2 changed files with 28 additions and 1 deletions

View File

@ -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

View File

@ -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(