Completed
Pull Request — master (#51)
by Robbie
02:02 queued 51s
created

EditableSpamProtectionFieldTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 6
dl 0
loc 121
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 10 1
A testValidateFieldDoesntAddErrorOnSuccess() 0 22 2
B testValidateFieldAddsErrorFromField() 0 24 2
B testValidateFieldAddsDefaultError() 0 24 2
A getFormMock() 0 12 1
A getEditableFormFieldMock() 0 19 1
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
        Config::modify()->set(
23
            FormSpamProtectionExtension::class,
24
            'default_spam_protector',
25
            Protector::class
26
        );
27
    }
28
29
    public function testValidateFieldDoesntAddErrorOnSuccess()
30
    {
31
        // @todo update namespaced for userforms when it is 4.0 compatible
32
        if (!class_exists(EditableSpamProtectionField::class)) {
33
            $this->markTestSkipped('"userforms" module not installed');
34
        }
35
36
        $formMock = $this->getFormMock();
37
        $formFieldMock = $this->getEditableFormFieldMock();
38
39
        $formFieldMock
40
            ->getFormField() // mock
41
            ->expects($this->once())
42
            ->method('validate')
43
            ->will($this->returnValue(true));
44
45
        $formMock
46
            ->expects($this->never())
47
            ->method('sessionMessage');
48
49
        $formFieldMock->validateField(array('MyField' => null), $formMock);
50
    }
51
52
    public function testValidateFieldAddsErrorFromField()
53
    {
54
        if (!class_exists('EditableSpamProtectionField')) {
55
            $this->markTestSkipped('"userforms" module not installed');
56
        }
57
58
        $formMock = $this->getFormMock();
59
        $formFieldMock = $this->getEditableFormFieldMock();
60
61
        $formFieldMock
62
            ->getFormField() // mock
63
            ->expects($this->once())
64
            ->method('validate')
65
            ->will($this->returnValue(false));
66
67
        $formMock->getValidator()->validationError('MyField', 'some field message', 'required');
68
69
        $formMock
70
            ->expects($this->once())
71
            ->method('sessionMessage')
72
            ->with($this->anything(), $this->stringContains('some field message'), $this->anything(), $this->anything());
73
74
        $formFieldMock->validateField(array('MyField' => null), $formMock);
75
    }
76
77
    public function testValidateFieldAddsDefaultError()
78
    {
79
        if (!class_exists('EditableSpamProtectionField')) {
80
            $this->markTestSkipped('"userforms" module not installed');
81
        }
82
83
        $formMock = $this->getFormMock();
84
        $formFieldMock = $this->getEditableFormFieldMock();
85
86
        $formFieldMock
87
            ->getFormField() // mock
88
            ->expects($this->once())
89
            ->method('validate')
90
            ->will($this->returnValue(false));
91
92
        // field doesn't set any validation errors here
93
94
        $formMock
95
            ->expects($this->once())
96
            ->method('sessionMessage')
97
            ->with($this->anything(), $this->stringContains('default error message'), $this->anything(), $this->anything());
98
99
        $formFieldMock->validateField(array('MyField' => null), $formMock);
100
    }
101
102
    protected function getFormMock()
103
    {
104
        $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...
105
            ->disableOriginalConstructor()
106
            ->getMock();
107
        $formMock
108
            ->expects($this->any())
109
            ->method('getValidator')
110
            ->will($this->returnValue(new RequiredFields()));
111
112
        return $formMock;
113
    }
114
115
    protected function getEditableFormFieldMock()
116
    {
117
        $page = new UserDefinedForm();
118
        $page->write();
119
120
        $formFieldMock = $this->getMockBuilder('TextField')
121
            ->disableOriginalConstructor()
122
            ->getMock();
123
124
        $editableFormFieldMock = new EditableSpamProtectionField(array(
125
            'ParentID' => $page->ID,
126
            'Name' => 'MyField',
127
            'CustomErrorMessage' => 'default error message'
128
        ));
129
        $editableFormFieldMock->write();
130
        $editableFormFieldMock->setFormField($formFieldMock);
131
132
        return $editableFormFieldMock;
133
    }
134
}
135