From 007a52eb0cc0b23bdddd277ba59842b9dc34cfb3 Mon Sep 17 00:00:00 2001 From: Damian Mooyman Date: Fri, 7 Mar 2014 17:00:55 +1300 Subject: [PATCH] API Re-introduced ability to insert spam field before another existing field --- README.md | 1 + .../FormSpamProtectionExtension.php | 10 +++++- tests/FormSpamProtectionExtensionTest.php | 32 +++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ad6241d..a1c3719 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,7 @@ Options to configure are: *`name`* the form field name argument for the Captcha. Defaults to `Catcha`. *`title`* title of the Captcha form field. Defaults to `''` +*`insertBefore`* name of existing field to insert the spam protection field prior to *`mapping`* an array mapping of the Form fields to the standardized list of field names. The list of standardized fields to pass to the spam protector are: diff --git a/code/extensions/FormSpamProtectionExtension.php b/code/extensions/FormSpamProtectionExtension.php index 49f555e..c9786bf 100644 --- a/code/extensions/FormSpamProtectionExtension.php +++ b/code/extensions/FormSpamProtectionExtension.php @@ -93,7 +93,15 @@ class FormSpamProtectionExtension extends Extension { if($field = $protector->getFormField($name, $title)) { $field->setForm($this->owner); - $this->owner->Fields()->push($field); + // Add before field specified by insertBefore + $inserted = false; + if(!empty($options['insertBefore'])) { + $inserted = $this->owner->Fields()->insertBefore($field, $options['insertBefore']); + } + if(!$inserted) { + // Add field to end if not added already + $this->owner->Fields()->push($field); + } } return $this->owner; diff --git a/tests/FormSpamProtectionExtensionTest.php b/tests/FormSpamProtectionExtensionTest.php index d078121..7292450 100644 --- a/tests/FormSpamProtectionExtensionTest.php +++ b/tests/FormSpamProtectionExtensionTest.php @@ -5,6 +5,8 @@ */ class FormSpamProtectionExtensionTest extends SapphireTest { + protected $usesDatabase = false; + public function setUp() { parent::setUp(); @@ -54,6 +56,36 @@ 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()); + $this->assertEquals('Comment', $fields[1]->Title()); + $this->assertEquals('URL', $fields[2]->Title()); + $this->assertEquals('Foo', $fields[3]->Title()); + } + } /**