Skip to content

Commit

Permalink
API Re-introduced ability to insert spam field before another existin…
Browse files Browse the repository at this point in the history
…g field
  • Loading branch information
tractorcow committed Mar 7, 2014
1 parent 35a6ad1 commit 007a52e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
10 changes: 9 additions & 1 deletion code/extensions/FormSpamProtectionExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,15 @@ public function enableSpamProtection($options = array()) {
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;
Expand Down
32 changes: 32 additions & 0 deletions tests/FormSpamProtectionExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/
class FormSpamProtectionExtensionTest extends SapphireTest {

protected $usesDatabase = false;

public function setUp() {
parent::setUp();

Expand Down Expand Up @@ -54,6 +56,36 @@ public function testCustomOptions() {

$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());
}

}

/**
Expand Down

0 comments on commit 007a52e

Please sign in to comment.