Source of file FormSpamProtectionExtensionTest.php
Size: 4,018 Bytes - Last Modified: 2021-12-23T10:34:42+00:00
/var/www/docs.ssmods.com/process/src/tests/FormSpamProtectionExtensionTest.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 | <?php 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; /** * @package spamprotection */ class FormSpamProtectionExtensionTest extends SapphireTest { protected $usesDatabase = false; /** * @var Form */ protected $form = null; protected function setUp(): void { parent::setUp(); $this->form = new Form(new Controller, 'Form', new FieldList( new TextField('Title'), new TextField('Comment'), new TextField('URL') ), new FieldList()); $this->form->disableSecurityToken(); } public function testEnableSpamProtection() { Config::modify()->set( FormSpamProtectionExtension::class, 'default_spam_protector', FooProtector::class ); $form = $this->form->enableSpamProtection(); $this->assertEquals('Foo', $form->Fields()->fieldByName('Captcha')->Title()); } public function testEnableSpamProtectionCustomProtector() { $form = $this->form->enableSpamProtection(array( 'protector' => BarProtector::class )); $this->assertEquals('Bar', $form->Fields()->fieldByName('Captcha')->Title()); } public function testEnableSpamProtectionCustomTitle() { $form = $this->form->enableSpamProtection(array( 'protector' => BarProtector::class, 'title' => 'Baz', )); $this->assertEquals('Baz', $form->Fields()->fieldByName('Captcha')->Title()); } public function testCustomOptions() { $form = $this->form->enableSpamProtection(array( 'protector' => BazProtector::class, '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()); } public function testInsertBefore() { $form = $this->form->enableSpamProtection(array( 'protector' => FooProtector::class, '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' => FooProtector::class, '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()); } } |