silverstripe-spamprotection/tests/FormSpamProtectionExtensionTest.php

143 lines
4.3 KiB
PHP
Raw Permalink Normal View History

2014-02-10 08:54:13 +01:00
<?php
namespace SilverStripe\SpamProtection\Tests;
2023-01-15 22:42:51 +01:00
use LogicException;
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;
2021-10-27 07:14:42 +02:00
protected function setUp(): void
2015-11-21 07:15:15 +01:00
{
parent::setUp();
$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')
), new FieldList());
2015-11-21 07:15:15 +01:00
$this->form->disableSecurityToken();
}
2023-01-15 22:42:51 +01:00
public function testEnableSpamProtectionThrowsException()
{
$this->expectException(LogicException::class);
$this->expectExceptionMessage('No spam protector has been set. Null is not valid value.');
Config::modify()->set(FormSpamProtectionExtension::class, 'default_spam_protector', null);
2023-01-15 22:42:51 +01:00
$this->form->enableSpamProtection();
}
2015-11-21 07:15:15 +01:00
public function testEnableSpamProtection()
{
Config::modify()->set(
FormSpamProtectionExtension::class,
'default_spam_protector',
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(
'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(
'protector' => BarProtector::class,
2015-11-21 07:15:15 +01:00
'title' => 'Baz',
));
2015-11-21 07:15:15 +01:00
$this->assertEquals('Baz', $form->Fields()->fieldByName('Captcha')->Title());
}
public function testCustomOptions()
{
$form = $this->form->enableSpamProtection(array(
'protector' => BazProtector::class,
2015-11-21 07:15:15 +01:00
'title' => 'Qux',
'name' => 'Borris'
));
$this->assertEquals('Qux', $form->Fields()->fieldByName('Borris')->Title());
}
public function testConfigurableName()
{
$field_name = "test_configurable_name";
Config::modify()->set(
FormSpamProtectionExtension::class,
'default_spam_protector',
FooProtector::class
);
Config::modify()->set(
FormSpamProtectionExtension::class,
'field_name',
$field_name
);
$form = $this->form->enableSpamProtection();
// remove for subsequent tests
Config::modify()->remove(FormSpamProtectionExtension::class, 'field_name');
// 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(
'protector' => FooProtector::class,
2015-11-21 07:15:15 +01:00
'insertBefore' => 'URL'
));
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());
}
2015-11-21 07:15:15 +01:00
public function testInsertBeforeMissing()
{
$form = $this->form->enableSpamProtection(array(
'protector' => FooProtector::class,
2015-11-21 07:15:15 +01:00
'insertBefore' => 'NotAField'
));
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
}