Completed
Pull Request — master (#51)
by Robbie
04:56
created

testValidateFieldDoesntAddErrorOnSuccess()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 12
nc 1
nop 0
1
<?php
2
3
namespace SilverStripe\SpamProtection\Tests;
4
5
use UserDefinedForm;
6
use SilverStripe\Core\Config\Config;
7
use SilverStripe\Dev\SapphireTest;
8
use SilverStripe\Forms\Form;
9
use SilverStripe\Forms\RequiredFields;
10
use SilverStripe\SpamProtection\EditableSpamProtectionField;
11
use SilverStripe\SpamProtection\Extension\FormSpamProtectionExtension;
12
use SilverStripe\SpamProtection\Tests\EditableSpamProtectionFieldTest\Protector;
13
14
class EditableSpamProtectionFieldTest extends SapphireTest
15
{
16
    protected $usesDatabase = true;
17
18
    protected function setUp()
19
    {
20
        parent::setUp();
21
22
        if (!class_exists('EditableSpamProtectionField')) {
23
            $this->markTestSkipped('"userforms" module not installed');
24
        }
25
26
        Config::modify()->set(
27
            FormSpamProtectionExtension::class,
28
            'default_spam_protector',
29
            Protector::class
30
        );
31
    }
32
33
    public function testValidateFieldDoesntAddErrorOnSuccess()
34
    {
35
        $formMock = $this->getFormMock();
36
        $formFieldMock = $this->getEditableFormFieldMock();
37
38
        $formFieldMock
39
            ->getFormField() // mock
40
            ->expects($this->once())
41
            ->method('validate')
42
            ->will($this->returnValue(true));
43
44
        $formMock
45
            ->expects($this->never())
46
            ->method('sessionMessage');
47
48
        $formFieldMock->validateField(array('MyField' => null), $formMock);
49
    }
50
51
    public function testValidateFieldAddsErrorFromField()
52
    {
53
        $formMock = $this->getFormMock();
54
        $formFieldMock = $this->getEditableFormFieldMock();
55
56
        $formFieldMock
57
            ->getFormField() // mock
58
            ->expects($this->once())
59
            ->method('validate')
60
            ->will($this->returnValue(false));
61
62
        $formMock->getValidator()->validationError('MyField', 'some field message', 'required');
63
64
        $formMock
65
            ->expects($this->once())
66
            ->method('sessionMessage')
67
            ->with(
68
                $this->anything(),
69
                $this->stringContains('some field message'),
70
                $this->anything(),
71
                $this->anything()
72
            );
73
74
        $formFieldMock->validateField(array('MyField' => null), $formMock);
75
    }
76
77
    public function testValidateFieldAddsDefaultError()
78
    {
79
        $formMock = $this->getFormMock();
80
        $formFieldMock = $this->getEditableFormFieldMock();
81
82
        $formFieldMock
83
            ->getFormField() // mock
84
            ->expects($this->once())
85
            ->method('validate')
86
            ->will($this->returnValue(false));
87
88
        // field doesn't set any validation errors here
89
90
        $formMock
91
            ->expects($this->once())
92
            ->method('sessionMessage')
93
            ->with(
94
                $this->anything(),
95
                $this->stringContains('default error message'),
96
                $this->anything(),
97
                $this->anything()
98
            );
99
100
        $formFieldMock->validateField(array('MyField' => null), $formMock);
101
    }
102
103
    public function testSpamConfigurationShowsInCms()
104
    {
105
        $field = $this->getEditableFormFieldMock();
106
        $fields = $field->getCMSFields();
107
108
        $this->assertInstanceOf('FieldGroup', $fields->fieldByName('Root.Main.SpamFieldMapping'));
109
    }
110
111
    public function testSpamMapSettingsAreSerialised()
112
    {
113
        $field = $this->getEditableFormFieldMock();
114
        $field->SpamFieldSettings = json_encode(array('foo' => 'bar', 'bar' => 'baz'));
115
        $field->write();
116
117
        $this->assertJson($field->SpamFieldSettings);
118
        $this->assertSame('bar', $field->spamMapValue('foo'));
119
        $this->assertSame('baz', $field->spamMapValue('bar'));
120
    }
121
122
    protected function getFormMock()
123
    {
124
        $formMock = $this->getMockBuilder(Form::class, array('sessionMessage'))
0 ignored issues
show
Unused Code introduced by
The call to EditableSpamProtectionFieldTest::getMockBuilder() has too many arguments starting with array('sessionMessage').

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
125
            ->disableOriginalConstructor()
126
            ->getMock();
127
        $formMock
128
            ->expects($this->any())
129
            ->method('getValidator')
130
            ->will($this->returnValue(new RequiredFields()));
131
132
        return $formMock;
133
    }
134
135
    protected function getEditableFormFieldMock()
136
    {
137
        $page = new UserDefinedForm();
138
        $page->write();
139
140
        $formFieldMock = $this->getMockBuilder('TextField')
141
            ->disableOriginalConstructor()
142
            ->getMock();
143
144
        $editableFormFieldMock = new EditableSpamProtectionField(array(
145
            'ParentID' => $page->ID,
146
            'Name' => 'MyField',
147
            'CustomErrorMessage' => 'default error message'
148
        ));
149
        $editableFormFieldMock->write();
150
        $editableFormFieldMock->setFormField($formFieldMock);
151
152
        return $editableFormFieldMock;
153
    }
154
}
155