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

testSpamMapSettingsAreSerialised()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
3
class EditableSpamProtectionFieldTest extends SapphireTest
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
{
5
6
    protected $usesDatabase = true;
7
8
    public function setUp()
9
    {
10
        parent::setUp();
11
12
        if (!class_exists('EditableSpamProtectionField')) {
13
            $this->markTestSkipped('"userforms" module not installed');
0 ignored issues
show
Bug introduced by
The method markTestSkipped() does not seem to exist on object<EditableSpamProtectionFieldTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
14
        }
15
16
        Config::inst()->update(
17
            'FormSpamProtectionExtension',
18
            'default_spam_protector',
19
            'EditableSpamProtectionFieldTest_Protector'
20
        );
21
    }
22
23
    public function testValidateFieldDoesntAddErrorOnSuccess()
24
    {
25
        $formMock = $this->getFormMock();
26
        $formFieldMock = $this->getEditableFormFieldMock();
27
28
        $formFieldMock
29
            ->getFormField() // mock
30
            ->expects($this->once())
0 ignored issues
show
Bug introduced by
The method once() does not seem to exist on object<EditableSpamProtectionFieldTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
31
            ->method('validate')
32
            ->will($this->returnValue(true));
0 ignored issues
show
Bug introduced by
The method returnValue() does not seem to exist on object<EditableSpamProtectionFieldTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
33
34
        $formMock
35
            ->expects($this->never())
0 ignored issues
show
Bug introduced by
The method never() does not seem to exist on object<EditableSpamProtectionFieldTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
36
            ->method('addErrorMessage');
37
38
        $formFieldMock->validateField(array('MyField' => null), $formMock);
39
    }
40
41
    public function testValidateFieldAddsErrorFromField()
42
    {
43
        $formMock = $this->getFormMock();
44
        $formFieldMock = $this->getEditableFormFieldMock();
45
46
        $formFieldMock
47
            ->getFormField() // mock
48
            ->expects($this->once())
0 ignored issues
show
Bug introduced by
The method once() does not seem to exist on object<EditableSpamProtectionFieldTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
49
            ->method('validate')
50
            ->will($this->returnValue(false));
0 ignored issues
show
Bug introduced by
The method returnValue() does not seem to exist on object<EditableSpamProtectionFieldTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
51
52
        $formMock->getValidator()->validationError('MyField', 'some field message', 'required');
53
54
        $formMock
55
            ->expects($this->once())
0 ignored issues
show
Bug introduced by
The method once() does not seem to exist on object<EditableSpamProtectionFieldTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
56
            ->method('addErrorMessage')
57
            ->with($this->anything(), $this->stringContains('some field message'), $this->anything(), $this->anything());
0 ignored issues
show
Bug introduced by
The method anything() does not seem to exist on object<EditableSpamProtectionFieldTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method stringContains() does not seem to exist on object<EditableSpamProtectionFieldTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
58
        ;
59
60
        $formFieldMock->validateField(array('MyField' => null), $formMock);
61
    }
62
63
    public function testValidateFieldAddsDefaultError()
64
    {
65
        $formMock = $this->getFormMock();
66
        $formFieldMock = $this->getEditableFormFieldMock();
67
68
        $formFieldMock
69
            ->getFormField() // mock
70
            ->expects($this->once())
0 ignored issues
show
Bug introduced by
The method once() does not seem to exist on object<EditableSpamProtectionFieldTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
71
            ->method('validate')
72
            ->will($this->returnValue(false));
0 ignored issues
show
Bug introduced by
The method returnValue() does not seem to exist on object<EditableSpamProtectionFieldTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
73
74
        // field doesn't set any validation errors here
75
76
        $formMock
77
            ->expects($this->once())
0 ignored issues
show
Bug introduced by
The method once() does not seem to exist on object<EditableSpamProtectionFieldTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
78
            ->method('addErrorMessage')
79
            ->with($this->anything(), $this->stringContains('default error message'), $this->anything(), $this->anything());
0 ignored issues
show
Bug introduced by
The method anything() does not seem to exist on object<EditableSpamProtectionFieldTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method stringContains() does not seem to exist on object<EditableSpamProtectionFieldTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
80
81
        $formFieldMock->validateField(array('MyField' => null), $formMock);
82
    }
83
84
    public function testSpamConfigurationShowsInCms()
85
    {
86
        $field = $this->getEditableFormFieldMock();
87
        $fields = $field->getCMSFields();
88
89
        $this->assertInstanceOf('FieldGroup', $fields->fieldByName('Root.Main.SpamFieldMapping'));
0 ignored issues
show
Bug introduced by
The method assertInstanceOf() does not seem to exist on object<EditableSpamProtectionFieldTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
90
    }
91
92
    public function testSpamMapSettingsAreSerialised()
93
    {
94
        $field = $this->getEditableFormFieldMock();
95
        $field->SpamFieldSettings = json_encode(array('foo' => 'bar', 'bar' => 'baz'));
96
        $field->write();
97
98
        $this->assertJson($field->SpamFieldSettings);
0 ignored issues
show
Bug introduced by
The method assertJson() does not seem to exist on object<EditableSpamProtectionFieldTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
99
        $this->assertSame('bar', $field->spamMapValue('foo'));
0 ignored issues
show
Bug introduced by
The method assertSame() does not seem to exist on object<EditableSpamProtectionFieldTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
100
        $this->assertSame('baz', $field->spamMapValue('bar'));
0 ignored issues
show
Bug introduced by
The method assertSame() does not seem to exist on object<EditableSpamProtectionFieldTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
101
    }
102
103
    protected function getFormMock()
104
    {
105
        $formMock = $this->getMockBuilder('Form', array('addErrorMessage'))
0 ignored issues
show
Bug introduced by
The method getMockBuilder() does not seem to exist on object<EditableSpamProtectionFieldTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
106
            ->disableOriginalConstructor()
107
            ->getMock();
108
        $formMock
109
            ->expects($this->any())
0 ignored issues
show
Bug introduced by
The method any() does not seem to exist on object<EditableSpamProtectionFieldTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
110
            ->method('getValidator')
111
            ->will($this->returnValue(new RequiredFields()));
0 ignored issues
show
Bug introduced by
The method returnValue() does not seem to exist on object<EditableSpamProtectionFieldTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
112
113
        return $formMock;
114
    }
115
116
    protected function getEditableFormFieldMock()
117
    {
118
        $page = new UserDefinedForm();
119
        $page->write();
120
121
        $formFieldMock = $this->getMockBuilder('TextField')
0 ignored issues
show
Bug introduced by
The method getMockBuilder() does not seem to exist on object<EditableSpamProtectionFieldTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
122
            ->disableOriginalConstructor()
123
            ->getMock();
124
125
        $editableFormFieldMock = new EditableSpamProtectionField(array(
126
            'ParentID' => $page->ID,
127
            'Name' => 'MyField',
128
            'CustomErrorMessage' => 'default error message'
129
        ));
130
        $editableFormFieldMock->write();
131
        $editableFormFieldMock->setFormField($formFieldMock);
132
133
        return $editableFormFieldMock;
134
    }
135
}
136