Completed
Pull Request — master (#495)
by Damian
31:33
created

UserFormsUpgradeServiceTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 218
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 4
c 2
b 0
f 0
lcom 1
cbo 3
dl 0
loc 218
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B setUp() 0 98 1
A getService() 0 4 1
B testCustomRulesMigration() 0 24 1
A testCustomSettingsMigration() 0 74 1
1
<?php
2
3
class UserFormsUpgradeServiceTest 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
    public static $fixture_file = 'UserFormsUpgradeServiceTest.yml';
7
8
    public function setUp()
9
    {
10
        Config::inst()->update('UserDefinedForm', 'upgrade_on_build', false);
11
        parent::setUp();
12
13
        // Assign rules programatically
14
        $field1 = $this->objFromFixture('EditableTextField', 'text1');
15
        $field2 = $this->objFromFixture('EditableTextField', 'text2');
16
        $field3 = $this->objFromFixture('EditableTextField', 'text3');
17
18
        $field3->CustomRules = serialize(array(
19
            array(
20
                'Display' => 'Show',
21
                'ConditionField' => $field1->Name,
22
                'ConditionOption' => 'IsBlank',
23
                'Value' => ''
24
            ),
25
            array(
26
                'Display' => 'Hide',
27
                'ConditionField' => $field2->Name,
28
                'ConditionOption' => 'HasValue',
29
                'Value' => 'bob'
30
            )
31
        ));
32
        $field3->write();
33
34
        // Assign settings programatically
35
        $field4 = $this->objFromFixture('EditableTextField', 'text4');
36
        $field4->CustomSettings = serialize(array(
37
            'MinLength' => 20,
38
            'MaxLength' => 100,
39
            'Rows' => 4,
40
            'ExtraClass' => 'special class',
41
            'RightTitle' => 'My Field',
42
            'ShowOnLoad' => '',
43
            'Default' => 'Enter your text here'
44
        ));
45
        $field4->write();
46
47
        $numeric1 = $this->objFromFixture('EditableNumericField', 'numeric1');
48
        $numeric1->CustomSettings = serialize(array(
49
            'RightTitle' => 'Number of %',
50
            'Default' => 1,
51
            'MinValue' => 1,
52
            'MaxValue' => 100,
53
            'ShowOnLoad' => 'Show'
54
        ));
55
        $numeric1->write();
56
57
        $group1 = $this->objFromFixture('Group', 'group1');
58
        $members1 = $this->objFromFixture('EditableMemberListField', 'members1');
59
        $members1->CustomSettings = serialize(array(
60
            'RightTitle' => 'Select group',
61
            'GroupID' => $group1->ID,
62
            'ShowOnLoad' => 'Hide'
63
        ));
64
        $members1->write();
65
66
        $literal1 = $this->objFromFixture('EditableLiteralField', 'literal1');
67
        $literal1->CustomSettings = serialize(array(
68
            'HideFromReports' => 1,
69
            'RightTitle' => 'Literal',
70
            'Content' => '<p>Content</p>',
71
            'ShowOnLoad' => true
72
        ));
73
        $literal1->write();
74
75
        $heading1 = $this->objFromFixture('EditableFormHeading', 'heading1');
76
        $heading1->CustomSettings = serialize(array(
77
            'RightTitle' => 'Right',
78
            'Level' => 3,
79
            'HideFromReports' => true,
80
            'ShowOnLoad' => false
81
        ));
82
        $heading1->write();
83
84
        $folder = $this->objFromFixture('Folder', 'folder1');
85
        $file1 = $this->objFromFixture('EditableFileField', 'file1');
86
        $file1->CustomSettings = serialize(array(
87
            'RightTitle' => 'File field',
88
            'Folder' => $folder->ID
89
        ));
90
        $file1->write();
91
92
        $date1 = $this->objFromFixture('EditableDateField', 'date1');
93
        $date1->CustomSettings = serialize(array(
94
            'RightTitle' => 'Date field',
95
            'DefaultToToday' => '1'
96
        ));
97
        $date1->write();
98
99
        $checkbox1 = $this->objFromFixture('EditableCheckbox', 'checkbox1');
100
        $checkbox1->CustomSettings = serialize(array(
101
            'Default' => true,
102
            'RightTitle' => 'Check this'
103
        ));
104
        $checkbox1->write();
105
    }
106
107
    /**
108
     * @return UserFormsUpgradeService;
0 ignored issues
show
Documentation introduced by
The doc-type UserFormsUpgradeService; could not be parsed: Expected "|" or "end of type", but got ";" at position 23. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
109
     */
110
    protected function getService()
111
    {
112
        return singleton('UserFormsUpgradeService');
113
    }
114
115
    /**
116
     * Tests migration of custom rules
117
     */
118
    public function testCustomRulesMigration()
119
    {
120
        $service = $this->getService();
121
        $service->setQuiet(true);
122
        $service->run();
123
124
        $field1 = $this->objFromFixture('EditableTextField', 'text1');
125
        $field2 = $this->objFromFixture('EditableTextField', 'text2');
126
        $field3 = $this->objFromFixture('EditableTextField', 'text3');
127
128
        $this->assertDOSEquals(array(
129
            array(
130
                'Display' => 'Show',
131
                'ConditionFieldID' => $field1->ID,
132
                'ConditionOption' => 'IsBlank'
133
            ),
134
            array(
135
                'Display' => 'Hide',
136
                'ConditionFieldID' => $field2->ID,
137
                'ConditionOption' => 'HasValue',
138
                'FieldValue' => 'bob'
139
            )
140
        ), $field3->DisplayRules());
141
    }
142
143
    /**
144
     * Tests migration of all custom settings
145
     */
146
    public function testCustomSettingsMigration()
147
    {
148
        $service = $this->getService();
149
        $service->setQuiet(true);
150
        $service->run();
151
152
        $group1 = $this->objFromFixture('Group', 'group1');
153
        $form = $this->objFromFixture('UserDefinedForm', 'form-with-settings');
154
        $folder = $this->objFromFixture('Folder', 'folder1');
155
156
        $this->assertDOSEquals(array(
157
            array(
158
                'ClassName' => 'EditableTextField',
159
                'Title' => 'Text with rule',
160
                'MinLength' => 20,
161
                'MaxLength' => 100,
162
                'Rows' => 4,
163
                'ExtraClass' => 'special class',
164
                'RightTitle' => 'My Field',
165
                'ShowOnLoad' => true,
166
                'Default' => 'Enter your text here',
167
            ),
168
            array(
169
                'ClassName' => 'EditableNumericField',
170
                'Title' => 'Numeric 1',
171
                'RightTitle' => 'Number of %',
172
                'Default' => 1,
173
                'MinValue' => 1,
174
                'MaxValue' => 100,
175
                'ShowOnLoad' => true,
176
            ),
177
            array(
178
                'ClassName' => 'EditableMemberListField',
179
                'Title' => 'Members 1',
180
                'RightTitle' => 'Select group',
181
                'GroupID' => $group1->ID,
182
                'ShowOnLoad' => false,
183
            ),
184
            array(
185
                'ClassName' => 'EditableLiteralField',
186
                'Title' => 'Literal 1',
187
                'HideFromReports' => true,
188
                'RightTitle' => 'Literal',
189
                'Content' => '<p>Content</p>',
190
                'ShowOnLoad' => true,
191
            ),
192
            array(
193
                'ClassName' => 'EditableFormHeading',
194
                'Title' => 'Heading 1',
195
                'RightTitle' => 'Right',
196
                'Level' => 3,
197
                'HideFromReports' => true,
198
                'ShowOnLoad' => false,
199
            ),
200
            array(
201
                'ClassName' => 'EditableFileField',
202
                'Title' => 'File 1',
203
                'RightTitle' => 'File field',
204
                'FolderID' => $folder->ID,
205
            ),
206
            array(
207
                'ClassName' => 'EditableDateField',
208
                'Title' => 'Date 1',
209
                'RightTitle' => 'Date field',
210
                'DefaultToToday' => true,
211
            ),
212
            array(
213
                'ClassName' => 'EditableCheckbox',
214
                'Title' => 'Checkbox 1',
215
                'CheckedDefault' => true,
216
                'RightTitle' => 'Check this',
217
            ),
218
        ), $form->Fields());
0 ignored issues
show
Bug introduced by
The method Fields() does not exist on DataObject. Did you maybe mean database_fields()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
219
    }
220
}
221