testValidateFieldAddsDefaultError()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 20

Duplication

Lines 20
Ratio 100 %

Importance

Changes 0
Metric Value
dl 20
loc 20
rs 9.6
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace SilverStripe\SpamProtection\Tests;
4
5
use SilverStripe\UserForms\Model\UserDefinedForm;
6
use SilverStripe\Core\Config\Config;
7
use SilverStripe\Dev\SapphireTest;
8
use SilverStripe\Forms\FieldGroup;
9
use SilverStripe\Forms\Form;
10
use SilverStripe\Forms\RequiredFields;
11
use SilverStripe\Forms\TextField;
12
use SilverStripe\SpamProtection\EditableSpamProtectionField;
13
use SilverStripe\SpamProtection\Extension\FormSpamProtectionExtension;
14
use SilverStripe\SpamProtection\Tests\Stub\Protector;
15
16
class EditableSpamProtectionFieldTest extends SapphireTest
17
{
18
    protected $usesDatabase = true;
19
20
    protected function setUp()
21
    {
22
        parent::setUp();
23
24
        if (!class_exists(EditableSpamProtectionField::class)) {
25
            $this->markTestSkipped('"userforms" module not installed');
26
        }
27
28
        Config::modify()->set(
29
            FormSpamProtectionExtension::class,
30
            'default_spam_protector',
31
            Protector::class
32
        );
33
    }
34
35 View Code Duplication
    public function testValidateFieldDoesntAddErrorOnSuccess()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
36
    {
37
        $formMock = $this->getFormMock();
38
        $formFieldMock = $this->getEditableFormFieldMock();
39
40
        $formFieldMock
41
            ->getFormField() // mock
42
            ->expects($this->once())
43
            ->method('validate')
44
            ->will($this->returnValue(true));
45
46
        $formMock
47
            ->expects($this->never())
48
            ->method('sessionMessage');
49
50
        $formFieldMock->validateField(array('MyField' => null), $formMock);
51
    }
52
53
    public function testValidateFieldAddsErrorFromField()
54
    {
55
        $formMock = $this->getFormMock();
56
        $formFieldMock = $this->getEditableFormFieldMock();
57
58
        $formFieldMock
59
            ->getFormField() // mock
60
            ->expects($this->once())
61
            ->method('validate')
62
            ->will($this->returnValue(false));
63
64
        $formMock->getValidator()->validationError('MyField', 'some field message', 'required');
65
66
        $formMock
67
            ->expects($this->once())
68
            ->method('sessionMessage')
69
            ->with($this->stringContains('some field message'), $this->anything());
70
71
        $formFieldMock->validateField(array('MyField' => null), $formMock);
72
    }
73
74 View Code Duplication
    public function testValidateFieldAddsDefaultError()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
75
    {
76
        $formMock = $this->getFormMock();
77
        $formFieldMock = $this->getEditableFormFieldMock();
78
79
        $formFieldMock
80
            ->getFormField() // mock
81
            ->expects($this->once())
82
            ->method('validate')
83
            ->will($this->returnValue(false));
84
85
        // field doesn't set any validation errors here
86
87
        $formMock
88
            ->expects($this->once())
89
            ->method('sessionError')
90
            ->with($this->stringContains('default error message'));
91
92
        $formFieldMock->validateField(array('MyField' => null), $formMock);
93
    }
94
95
    public function testSpamConfigurationShowsInCms()
96
    {
97
        $field = $this->getEditableFormFieldMock();
98
        $fields = $field->getCMSFields();
99
100
        $this->assertInstanceOf(FieldGroup::class, $fields->fieldByName('Root.Main.SpamFieldMapping'));
101
    }
102
103
    public function testSpamMapSettingsAreSerialised()
104
    {
105
        $field = $this->getEditableFormFieldMock();
106
        $field->SpamFieldSettings = json_encode(array('foo' => 'bar', 'bar' => 'baz'));
107
        $field->write();
108
109
        $this->assertJson($field->SpamFieldSettings);
110
        $this->assertSame('bar', $field->spamMapValue('foo'));
111
        $this->assertSame('baz', $field->spamMapValue('bar'));
112
    }
113
114
    public function testGetIcon()
115
    {
116
        $field = new EditableSpamProtectionField;
117
118
        $this->assertContains('/images/editablespamprotectionfield.png', $field->getIcon());
119
    }
120
121
    protected function getFormMock()
122
    {
123
        $formMock = $this->getMockBuilder(Form::class)
124
            ->setMethods(['sessionMessage', 'sessionError', 'getValidator'])
125
            ->disableOriginalConstructor()
126
            ->getMock();
127
128
        $formMock
129
            ->expects($this->any())
130
            ->method('getValidator')
131
            ->will($this->returnValue(new RequiredFields()));
132
133
        return $formMock;
134
    }
135
136
    protected function getEditableFormFieldMock()
137
    {
138
        $page = new UserDefinedForm();
139
        $page->write();
140
141
        $formFieldMock = $this->getMockBuilder(TextField::class)
142
            ->disableOriginalConstructor()
143
            ->getMock();
144
145
        $editableFormFieldMock = new EditableSpamProtectionField(array(
146
            'ParentID' => $page->ID,
147
            'ParentClass' => get_class($page),
148
            'Name' => 'MyField',
149
            'CustomErrorMessage' => 'default error message'
150
        ));
151
        $editableFormFieldMock->write();
152
        $editableFormFieldMock->setFormField($formFieldMock);
153
154
        return $editableFormFieldMock;
155
    }
156
}
157