2014-02-10 08:54:13 +01:00
|
|
|
<?php
|
|
|
|
|
2017-08-28 00:53:32 +02:00
|
|
|
namespace SilverStripe\SpamProtection\Tests;
|
|
|
|
|
|
|
|
use SilverStripe\Control\Controller;
|
|
|
|
use SilverStripe\Core\Config\Config;
|
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
use SilverStripe\Forms\FieldList;
|
|
|
|
use SilverStripe\Forms\Form;
|
|
|
|
use SilverStripe\Forms\TextField;
|
|
|
|
use SilverStripe\SpamProtection\Extension\FormSpamProtectionExtension;
|
|
|
|
use SilverStripe\SpamProtection\Tests\Stub\FooProtector;
|
|
|
|
use SilverStripe\SpamProtection\Tests\Stub\BarProtector;
|
|
|
|
use SilverStripe\SpamProtection\Tests\Stub\BazProtector;
|
|
|
|
|
2014-02-10 08:54:13 +01:00
|
|
|
/**
|
|
|
|
* @package spamprotection
|
|
|
|
*/
|
2015-11-21 07:15:15 +01:00
|
|
|
class FormSpamProtectionExtensionTest extends SapphireTest
|
|
|
|
{
|
|
|
|
protected $usesDatabase = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Form
|
|
|
|
*/
|
|
|
|
protected $form = null;
|
2017-07-07 04:23:01 +02:00
|
|
|
|
2021-10-27 07:14:42 +02:00
|
|
|
protected function setUp(): void
|
2015-11-21 07:15:15 +01:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
2017-08-28 00:53:32 +02:00
|
|
|
$this->form = new Form(new Controller, 'Form', new FieldList(
|
2015-11-21 07:15:15 +01:00
|
|
|
new TextField('Title'),
|
|
|
|
new TextField('Comment'),
|
|
|
|
new TextField('URL')
|
2017-07-07 04:23:01 +02:00
|
|
|
), new FieldList());
|
2017-08-28 00:53:32 +02:00
|
|
|
|
2015-11-21 07:15:15 +01:00
|
|
|
$this->form->disableSecurityToken();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testEnableSpamProtection()
|
|
|
|
{
|
2017-08-28 00:53:32 +02:00
|
|
|
Config::modify()->set(
|
|
|
|
FormSpamProtectionExtension::class,
|
2017-07-07 04:23:01 +02:00
|
|
|
'default_spam_protector',
|
2017-08-28 00:53:32 +02:00
|
|
|
FooProtector::class
|
2015-11-21 07:15:15 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$form = $this->form->enableSpamProtection();
|
|
|
|
|
|
|
|
$this->assertEquals('Foo', $form->Fields()->fieldByName('Captcha')->Title());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testEnableSpamProtectionCustomProtector()
|
|
|
|
{
|
|
|
|
$form = $this->form->enableSpamProtection(array(
|
2017-08-28 00:53:32 +02:00
|
|
|
'protector' => BarProtector::class
|
2015-11-21 07:15:15 +01:00
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertEquals('Bar', $form->Fields()->fieldByName('Captcha')->Title());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testEnableSpamProtectionCustomTitle()
|
|
|
|
{
|
|
|
|
$form = $this->form->enableSpamProtection(array(
|
2017-08-28 00:53:32 +02:00
|
|
|
'protector' => BarProtector::class,
|
2015-11-21 07:15:15 +01:00
|
|
|
'title' => 'Baz',
|
|
|
|
));
|
2017-07-07 04:23:01 +02:00
|
|
|
|
2015-11-21 07:15:15 +01:00
|
|
|
$this->assertEquals('Baz', $form->Fields()->fieldByName('Captcha')->Title());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCustomOptions()
|
|
|
|
{
|
|
|
|
$form = $this->form->enableSpamProtection(array(
|
2017-08-28 00:53:32 +02:00
|
|
|
'protector' => BazProtector::class,
|
2015-11-21 07:15:15 +01:00
|
|
|
'title' => 'Qux',
|
|
|
|
'name' => 'Borris'
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertEquals('Qux', $form->Fields()->fieldByName('Borris')->Title());
|
|
|
|
}
|
2017-07-07 04:23:01 +02:00
|
|
|
|
2017-08-24 12:16:19 +02:00
|
|
|
public function testConfigurableName()
|
|
|
|
{
|
|
|
|
$field_name = "test_configurable_name";
|
2017-08-28 00:53:32 +02:00
|
|
|
Config::modify()->set(
|
|
|
|
FormSpamProtectionExtension::class,
|
|
|
|
'default_spam_protector',
|
|
|
|
FooProtector::class
|
2017-08-24 12:16:19 +02:00
|
|
|
);
|
2017-08-28 00:53:32 +02:00
|
|
|
Config::modify()->set(
|
|
|
|
FormSpamProtectionExtension::class,
|
|
|
|
'field_name',
|
2017-08-24 12:16:19 +02:00
|
|
|
$field_name
|
|
|
|
);
|
|
|
|
$form = $this->form->enableSpamProtection();
|
|
|
|
// remove for subsequent tests
|
2017-08-28 00:53:32 +02:00
|
|
|
Config::modify()->remove(FormSpamProtectionExtension::class, 'field_name');
|
2017-08-24 12:16:19 +02:00
|
|
|
// field should take up configured name
|
|
|
|
$this->assertEquals('Foo', $form->Fields()->fieldByName($field_name)->Title());
|
|
|
|
}
|
2017-08-28 01:20:58 +02:00
|
|
|
|
2015-11-21 07:15:15 +01:00
|
|
|
public function testInsertBefore()
|
|
|
|
{
|
|
|
|
$form = $this->form->enableSpamProtection(array(
|
2017-08-28 00:53:32 +02:00
|
|
|
'protector' => FooProtector::class,
|
2015-11-21 07:15:15 +01:00
|
|
|
'insertBefore' => 'URL'
|
|
|
|
));
|
2017-07-07 04:23:01 +02:00
|
|
|
|
2015-11-21 07:15:15 +01:00
|
|
|
$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());
|
|
|
|
}
|
2017-07-07 04:23:01 +02:00
|
|
|
|
2015-11-21 07:15:15 +01:00
|
|
|
public function testInsertBeforeMissing()
|
|
|
|
{
|
|
|
|
$form = $this->form->enableSpamProtection(array(
|
2017-08-28 00:53:32 +02:00
|
|
|
'protector' => FooProtector::class,
|
2015-11-21 07:15:15 +01:00
|
|
|
'insertBefore' => 'NotAField'
|
|
|
|
));
|
2017-07-07 04:23:01 +02:00
|
|
|
|
2015-11-21 07:15:15 +01:00
|
|
|
// 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
|
|
|
}
|