Completed
Push — master ( ef3e6d...5f8d2a )
by Robbie
06:20
created

tests/EditableSpamProtectionFieldTest.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
class EditableSpamProtectionFieldTest extends SapphireTest 
4
{
5
    
6
    protected $usesDatabase = true;
7
8
    public function setUp()
9
    {
10
        parent::setUp();
11
12
        Config::inst()->update(
13
            'FormSpamProtectionExtension', 'default_spam_protector',
14
            'EditableSpamProtectionFieldTest_Protector'
15
        );
16
    }
17
18
    public function testValidateFieldDoesntAddErrorOnSuccess()
19
    {
20
        if (!class_exists('EditableSpamProtectionField')) {
21
            $this->markTestSkipped('"userforms" module not installed');
22
        }
23
24
        $formMock = $this->getFormMock();
25
        $formFieldMock = $this->getEditableFormFieldMock();
26
27
        $formFieldMock
28
            ->getFormField() // mock
29
            ->expects($this->once())
30
            ->method('validate')
31
            ->will($this->returnValue(true));
32
33
        $formMock
34
            ->expects($this->never())
35
            ->method('addErrorMessage');
36
37
        $formFieldMock->validateField(array('MyField' => null), $formMock);
38
    }
39
40
    public function testValidateFieldAddsErrorFromField()
41
    {
42
        if (!class_exists('EditableSpamProtectionField')) {
43
            $this->markTestSkipped('"userforms" module not installed');
44
        }
45
46
        $formMock = $this->getFormMock();
47
        $formFieldMock = $this->getEditableFormFieldMock();
48
49
        $formFieldMock
50
            ->getFormField() // mock
51
            ->expects($this->once())
52
            ->method('validate')
53
            ->will($this->returnValue(false));
54
55
        $formMock->getValidator()->validationError('MyField', 'some field message', 'required');
56
57
        $formMock
58
            ->expects($this->once())
59
            ->method('addErrorMessage')
60
            ->with($this->anything(), $this->stringContains('some field message'), $this->anything(), $this->anything());;
61
62
        $formFieldMock->validateField(array('MyField' => null), $formMock);
63
    }
64
65
    public function testValidateFieldAddsDefaultError()
66
    {
67
        if (!class_exists('EditableSpamProtectionField')) {
68
            $this->markTestSkipped('"userforms" module not installed');
69
        }
70
71
        $formMock = $this->getFormMock();
72
        $formFieldMock = $this->getEditableFormFieldMock();
73
74
        $formFieldMock
75
            ->getFormField() // mock
76
            ->expects($this->once())
77
            ->method('validate')
78
            ->will($this->returnValue(false));
79
80
        // field doesn't set any validation errors here
81
82
        $formMock
83
            ->expects($this->once())
84
            ->method('addErrorMessage')
85
            ->with($this->anything(), $this->stringContains('default error message'), $this->anything(), $this->anything());
86
87
        $formFieldMock->validateField(array('MyField' => null), $formMock);
88
    }
89
90
    protected function getFormMock()
91
    {
92
        $formMock = $this->getMockBuilder('Form', array('addErrorMessage'))
93
            ->disableOriginalConstructor()
94
            ->getMock();
95
        $formMock
96
            ->expects($this->any())
97
            ->method('getValidator')
98
            ->will($this->returnValue(new RequiredFields()));
99
100
        return $formMock;
101
    }
102
103
    protected function getEditableFormFieldMock()
104
    {
105
        $page = new UserDefinedForm();
106
        $page->write();
107
108
        $formFieldMock = $this->getMockBuilder('TextField')
109
            ->disableOriginalConstructor()
110
            ->getMock();
111
112
        $editableFormFieldMock = new EditableSpamProtectionField(array(
113
            'ParentID' => $page->ID,
114
            'Name' => 'MyField',
115
            'CustomErrorMessage' => 'default error message'
116
        ));
117
        $editableFormFieldMock->write();
118
        $editableFormFieldMock->setFormField($formFieldMock);
119
120
        return $editableFormFieldMock;
121
    }
122
123
}
124
125 View Code Duplication
class EditableSpamProtectionFieldTest_Protector implements SpamProtector, TestOnly
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
126
{
127
    public function getFormField($name = null, $title = null, $value = null)
128
    {
129
        return new TextField($name, 'Foo', $value);
130
    }
131
132
    public function setFieldMapping($fieldMapping)
133
    {
134
    }
135
}
136