2016-01-08 04:35:46 +01:00
|
|
|
<?php
|
|
|
|
|
2017-08-28 00:53:32 +02:00
|
|
|
namespace SilverStripe\SpamProtection\Tests;
|
|
|
|
|
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;
|
|
|
|
|
2017-08-28 00:53:32 +02:00
|
|
|
protected function setUp()
|
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'));
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|