2016-01-08 04:35:46 +01:00
|
|
|
<?php
|
|
|
|
|
2017-08-28 00:53:32 +02:00
|
|
|
namespace SilverStripe\SpamProtection\Tests;
|
|
|
|
|
2021-08-12 03:35:02 +02:00
|
|
|
use ReflectionClass;
|
|
|
|
use SilverStripe\ORM\DataList;
|
2017-08-28 03:44:53 +02:00
|
|
|
use SilverStripe\UserForms\Model\UserDefinedForm;
|
2017-08-28 00:53:32 +02:00
|
|
|
use SilverStripe\Core\Config\Config;
|
|
|
|
use SilverStripe\Dev\SapphireTest;
|
2017-08-28 03:44:53 +02:00
|
|
|
use SilverStripe\Forms\FieldGroup;
|
2017-08-28 00:53:32 +02:00
|
|
|
use SilverStripe\Forms\Form;
|
|
|
|
use SilverStripe\Forms\RequiredFields;
|
2017-08-28 03:44:53 +02:00
|
|
|
use SilverStripe\Forms\TextField;
|
2017-08-28 00:53:32 +02:00
|
|
|
use SilverStripe\SpamProtection\EditableSpamProtectionField;
|
|
|
|
use SilverStripe\SpamProtection\Extension\FormSpamProtectionExtension;
|
2017-08-28 03:44:53 +02:00
|
|
|
use SilverStripe\SpamProtection\Tests\Stub\Protector;
|
2017-08-28 00:53:32 +02:00
|
|
|
|
2017-07-07 04:23:01 +02:00
|
|
|
class EditableSpamProtectionFieldTest extends SapphireTest
|
2016-01-08 04:35:46 +01:00
|
|
|
{
|
|
|
|
protected $usesDatabase = true;
|
|
|
|
|
2021-10-27 07:14:42 +02:00
|
|
|
protected function setUp(): void
|
2016-01-08 04:35:46 +01:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
2017-08-28 03:44:53 +02:00
|
|
|
if (!class_exists(EditableSpamProtectionField::class)) {
|
2017-07-07 04:23:01 +02:00
|
|
|
$this->markTestSkipped('"userforms" module not installed');
|
|
|
|
}
|
|
|
|
|
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
|
|
|
Protector::class
|
2016-01-08 04:35:46 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidateFieldDoesntAddErrorOnSuccess()
|
|
|
|
{
|
|
|
|
$formMock = $this->getFormMock();
|
|
|
|
$formFieldMock = $this->getEditableFormFieldMock();
|
|
|
|
|
|
|
|
$formFieldMock
|
|
|
|
->getFormField() // mock
|
|
|
|
->expects($this->once())
|
|
|
|
->method('validate')
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
|
|
|
|
$formMock
|
|
|
|
->expects($this->never())
|
2017-08-28 00:53:32 +02:00
|
|
|
->method('sessionMessage');
|
2016-01-08 04:35:46 +01:00
|
|
|
|
|
|
|
$formFieldMock->validateField(array('MyField' => null), $formMock);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidateFieldAddsErrorFromField()
|
|
|
|
{
|
|
|
|
$formMock = $this->getFormMock();
|
|
|
|
$formFieldMock = $this->getEditableFormFieldMock();
|
|
|
|
|
|
|
|
$formFieldMock
|
|
|
|
->getFormField() // mock
|
|
|
|
->expects($this->once())
|
|
|
|
->method('validate')
|
|
|
|
->will($this->returnValue(false));
|
|
|
|
|
|
|
|
$formMock->getValidator()->validationError('MyField', 'some field message', 'required');
|
|
|
|
|
|
|
|
$formMock
|
|
|
|
->expects($this->once())
|
2017-08-28 00:53:32 +02:00
|
|
|
->method('sessionMessage')
|
2017-08-28 03:44:53 +02:00
|
|
|
->with($this->stringContains('some field message'), $this->anything());
|
2016-01-08 04:35:46 +01:00
|
|
|
|
|
|
|
$formFieldMock->validateField(array('MyField' => null), $formMock);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidateFieldAddsDefaultError()
|
|
|
|
{
|
|
|
|
$formMock = $this->getFormMock();
|
|
|
|
$formFieldMock = $this->getEditableFormFieldMock();
|
|
|
|
|
|
|
|
$formFieldMock
|
|
|
|
->getFormField() // mock
|
|
|
|
->expects($this->once())
|
|
|
|
->method('validate')
|
|
|
|
->will($this->returnValue(false));
|
|
|
|
|
|
|
|
// field doesn't set any validation errors here
|
|
|
|
|
|
|
|
$formMock
|
|
|
|
->expects($this->once())
|
2017-08-28 03:44:53 +02:00
|
|
|
->method('sessionError')
|
|
|
|
->with($this->stringContains('default error message'));
|
2016-01-08 04:35:46 +01:00
|
|
|
|
|
|
|
$formFieldMock->validateField(array('MyField' => null), $formMock);
|
|
|
|
}
|
|
|
|
|
2017-07-07 06:09:39 +02:00
|
|
|
public function testSpamConfigurationShowsInCms()
|
|
|
|
{
|
|
|
|
$field = $this->getEditableFormFieldMock();
|
|
|
|
$fields = $field->getCMSFields();
|
|
|
|
|
2017-08-28 03:44:53 +02:00
|
|
|
$this->assertInstanceOf(FieldGroup::class, $fields->fieldByName('Root.Main.SpamFieldMapping'));
|
2017-07-07 06:09:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testSpamMapSettingsAreSerialised()
|
|
|
|
{
|
|
|
|
$field = $this->getEditableFormFieldMock();
|
|
|
|
$field->SpamFieldSettings = json_encode(array('foo' => 'bar', 'bar' => 'baz'));
|
|
|
|
$field->write();
|
|
|
|
|
|
|
|
$this->assertJson($field->SpamFieldSettings);
|
|
|
|
$this->assertSame('bar', $field->spamMapValue('foo'));
|
|
|
|
$this->assertSame('baz', $field->spamMapValue('bar'));
|
|
|
|
}
|
|
|
|
|
2017-11-02 23:07:41 +01:00
|
|
|
public function testGetIcon()
|
|
|
|
{
|
|
|
|
$field = new EditableSpamProtectionField;
|
|
|
|
|
2021-10-27 07:14:42 +02:00
|
|
|
$this->assertStringContainsString('/images/editablespamprotectionfield.png', $field->getIcon());
|
2017-11-02 23:07:41 +01:00
|
|
|
}
|
|
|
|
|
2016-01-08 04:35:46 +01:00
|
|
|
protected function getFormMock()
|
|
|
|
{
|
2017-08-28 03:44:53 +02:00
|
|
|
$formMock = $this->getMockBuilder(Form::class)
|
|
|
|
->setMethods(['sessionMessage', 'sessionError', 'getValidator'])
|
2016-01-08 04:35:46 +01:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2017-08-28 03:44:53 +02:00
|
|
|
|
2016-01-08 04:35:46 +01:00
|
|
|
$formMock
|
|
|
|
->expects($this->any())
|
|
|
|
->method('getValidator')
|
|
|
|
->will($this->returnValue(new RequiredFields()));
|
|
|
|
|
|
|
|
return $formMock;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getEditableFormFieldMock()
|
|
|
|
{
|
|
|
|
$page = new UserDefinedForm();
|
|
|
|
$page->write();
|
|
|
|
|
2017-08-28 03:44:53 +02:00
|
|
|
$formFieldMock = $this->getMockBuilder(TextField::class)
|
2016-01-08 04:35:46 +01:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$editableFormFieldMock = new EditableSpamProtectionField(array(
|
|
|
|
'ParentID' => $page->ID,
|
2017-11-02 02:46:15 +01:00
|
|
|
'ParentClass' => get_class($page),
|
2016-01-08 04:35:46 +01:00
|
|
|
'Name' => 'MyField',
|
|
|
|
'CustomErrorMessage' => 'default error message'
|
|
|
|
));
|
|
|
|
$editableFormFieldMock->write();
|
|
|
|
$editableFormFieldMock->setFormField($formFieldMock);
|
|
|
|
|
|
|
|
return $editableFormFieldMock;
|
|
|
|
}
|
2021-08-12 03:35:02 +02:00
|
|
|
|
|
|
|
public function testGetCandidateFieldsParentStatus()
|
|
|
|
{
|
|
|
|
$field = new EditableSpamProtectionField();
|
|
|
|
$field->Name = 'MyField';
|
|
|
|
$reflection = new ReflectionClass($field);
|
|
|
|
$method = $reflection->getMethod('getCandidateFields');
|
|
|
|
$method->setAccessible(true);
|
|
|
|
// Assert with no parent
|
|
|
|
$list = $method->invoke($field);
|
|
|
|
$this->assertTrue($list instanceof DataList);
|
|
|
|
// Assert with parent
|
|
|
|
$page = new UserDefinedForm();
|
|
|
|
$page->write();
|
|
|
|
$field->ParentID = $page->ID;
|
|
|
|
$field->ParentClass = get_class($page);
|
|
|
|
$field->write();
|
|
|
|
$list = $method->invoke($field);
|
|
|
|
$this->assertTrue($list instanceof DataList);
|
|
|
|
}
|
2016-01-08 04:35:46 +01:00
|
|
|
}
|