Completed
Push — master ( 5f8d2a...42c733 )
by Franco
11s
created

EditableSpamProtectionFieldTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 133
Duplicated Lines 27.82 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
lcom 1
cbo 7
dl 37
loc 133
rs 10
c 1
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 14 2
A testValidateFieldDoesntAddErrorOnSuccess() 17 17 1
A testValidateFieldAddsErrorFromField() 0 20 1
A testValidateFieldAddsDefaultError() 20 20 1
A testSpamConfigurationShowsInCms() 0 7 1
A testSpamMapSettingsAreSerialised() 0 10 1
A getFormMock() 0 14 1
A getEditableFormFieldMock() 0 19 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
    protected function getFormMock()
115
    {
116
        $formMock = $this->getMockBuilder(Form::class)
117
            ->setMethods(['sessionMessage', 'sessionError', 'getValidator'])
118
            ->disableOriginalConstructor()
119
            ->getMock();
120
121
        $formMock
122
            ->expects($this->any())
123
            ->method('getValidator')
124
            ->will($this->returnValue(new RequiredFields()));
125
126
        return $formMock;
127
    }
128
129
    protected function getEditableFormFieldMock()
130
    {
131
        $page = new UserDefinedForm();
132
        $page->write();
133
134
        $formFieldMock = $this->getMockBuilder(TextField::class)
135
            ->disableOriginalConstructor()
136
            ->getMock();
137
138
        $editableFormFieldMock = new EditableSpamProtectionField(array(
139
            'ParentID' => $page->ID,
140
            'Name' => 'MyField',
141
            'CustomErrorMessage' => 'default error message'
142
        ));
143
        $editableFormFieldMock->write();
144
        $editableFormFieldMock->setFormField($formFieldMock);
145
146
        return $editableFormFieldMock;
147
    }
148
}
149