silverstripe-spamprotection/tests/FormSpamProtectionExtensionTest.php

135 lines
3.4 KiB
PHP
Raw Normal View History

2014-02-10 08:54:13 +01:00
<?php
/**
* @package spamprotection
*/
class FormSpamProtectionExtensionTest extends SapphireTest {
protected $usesDatabase = false;
2015-03-24 05:17:15 +01:00
/**
* @var Form
*/
protected $form = null;
2014-02-10 08:54:13 +01:00
public function setUp() {
parent::setUp();
$this->form = new Form($this, 'Form', new FieldList(
new TextField('Title'),
new TextField('Comment'),
new TextField('URL')
), new FieldList()
);
2015-03-24 05:17:15 +01:00
$this->form->disableSecurityToken();
2014-02-10 08:54:13 +01:00
}
public function testEnableSpamProtection() {
Config::inst()->update(
'FormSpamProtectionExtension', 'default_spam_protector',
'FormSpamProtectionExtensionTest_FooProtector'
);
$form = $this->form->enableSpamProtection();
$this->assertEquals('Foo', $form->Fields()->fieldByName('Captcha')->Title());
}
public function testEnableSpamProtectionCustomProtector() {
$form = $this->form->enableSpamProtection(array(
'protector' => 'FormSpamProtectionExtensionTest_BarProtector'
));
$this->assertEquals('Bar', $form->Fields()->fieldByName('Captcha')->Title());
2015-03-24 05:17:15 +01:00
}
2014-02-10 08:54:13 +01:00
2015-03-24 05:17:15 +01:00
public function testEnableSpamProtectionCustomTitle() {
2014-02-10 08:54:13 +01:00
$form = $this->form->enableSpamProtection(array(
2015-03-24 05:17:15 +01:00
'protector' => 'FormSpamProtectionExtensionTest_BarProtector',
'title' => 'Baz',
2014-02-10 08:54:13 +01:00
));
2015-03-24 05:17:15 +01:00
2014-02-10 08:54:13 +01:00
$this->assertEquals('Baz', $form->Fields()->fieldByName('Captcha')->Title());
}
public function testCustomOptions() {
$form = $this->form->enableSpamProtection(array(
'protector' => 'FormSpamProtectionExtensionTest_BazProtector',
'title' => 'Qux',
'name' => 'Borris'
));
$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());
}
2014-02-10 08:54:13 +01:00
}
/**
* @package spamprotection
*/
class FormSpamProtectionExtensionTest_BazProtector implements SpamProtector, TestOnly {
public function getFormField($name = null, $title = null, $value = null) {
return new TextField($name, $title, $value);
}
public function setFieldMapping($fieldMapping) {}
2014-02-10 08:54:13 +01:00
}
/**
* @package spamprotection
*/
class FormSpamProtectionExtensionTest_BarProtector implements SpamProtector, TestOnly {
public function getFormField($name = null, $title = null, $value = null) {
2015-03-24 05:17:15 +01:00
$title = $title ?: 'Bar';
return new TextField($name, $title, $value);
2014-02-10 08:54:13 +01:00
}
public function setFieldMapping($fieldMapping) {}
2014-02-10 08:54:13 +01:00
}
/**
* @package spamprotection
*/
class FormSpamProtectionExtensionTest_FooProtector implements SpamProtector, TestOnly {
public function getFormField($name = null, $title = null, $value = null) {
return new TextField($name, 'Foo', $value);
}
public function setFieldMapping($fieldMapping) {}
2014-02-10 08:54:13 +01:00
}