Passed
Push — master ( 4be01e...967769 )
by Robbie
25:06 queued 13:45
created

TreeMultiselectFieldTest::testEmptyReadonly()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 43
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 30
nc 1
nop 0
dl 0
loc 43
rs 9.44
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\Forms\Tests;
4
5
use SilverStripe\Assets\File;
6
use SilverStripe\Dev\SapphireTest;
7
use SilverStripe\Forms\Form;
8
use SilverStripe\Forms\FormTemplateHelper;
9
use SilverStripe\Forms\TreeMultiselectField;
10
11
class TreeMultiselectFieldTest extends SapphireTest
12
{
13
    protected static $fixture_file = 'TreeDropdownFieldTest.yml';
14
15
    protected $formId = 'TheFormID';
16
    protected $fieldName = 'TestTree';
17
18
    /**
19
     * Mock object of a generic form
20
     *
21
     * @var Form
22
     */
23
    protected $form;
24
25
    /**
26
     * Instance of the TreeMultiselectField
27
     *
28
     * @var TreeMultiselectField
29
     */
30
    protected $field;
31
32
    /**
33
     * The File objects of folders loaded from the fixture
34
     *
35
     * @var File[]
36
     */
37
    protected $folders;
38
39
    /**
40
     * The array of folder ids
41
     *
42
     * @var int[]
43
     */
44
    protected $folderIds;
45
46
    /**
47
     * Concatenated folder ids for use as a value for the field
48
     *
49
     * @var string
50
     */
51
    protected $fieldValue;
52
53
    protected function setUp()
54
    {
55
        parent::setUp();
56
57
        $this->form = $this->buildFormMock();
58
        $this->field = $this->buildField($this->form);
59
        $this->folders = $this->loadFolders();
60
61
        $this->folderIds = array_map(
62
            static function ($f) {
63
                return $f->ID;
64
            },
65
            $this->folders
66
        );
67
        $this->fieldValue = implode(',', $this->folderIds);
68
    }
69
70
    /**
71
     * Build a new mock object of a Form
72
     *
73
     * @return Form
74
     */
75
    protected function buildFormMock()
76
    {
77
        $form = $this->createMock(Form::class);
78
79
        $form->method('getTemplateHelper')
0 ignored issues
show
Bug introduced by
The method method() does not exist on PHPUnit\Framework\MockObject\MockObject. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

79
        $form->/** @scrutinizer ignore-call */ 
80
               method('getTemplateHelper')

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
            ->willReturn(FormTemplateHelper::singleton());
81
82
        $form->method('getHTMLID')
83
            ->willReturn($this->formId);
84
85
        return $form;
86
    }
87
88
    /**
89
     * Build a new instance of TreeMultiselectField
90
     *
91
     * @param Form $form The field form
92
     *
93
     * @return TreeMultiselectField
94
     */
95
    protected function buildField(Form $form)
96
    {
97
        $field = new TreeMultiselectField($this->fieldName, 'Test tree', File::class);
98
        $field->setForm($form);
99
100
        return $field;
101
    }
102
103
    /**
104
     * Load several files from the fixtures and return them in an array
105
     *
106
     * @return File[]
107
     */
108
    protected function loadFolders()
109
    {
110
        $asdf = $this->objFromFixture(File::class, 'asdf');
111
        $subfolderfile1 = $this->objFromFixture(File::class, 'subfolderfile1');
112
113
        return [$asdf, $subfolderfile1];
114
    }
115
116
    /**
117
     * Test the TreeMultiselectField behaviour with no selected values
118
     */
119
    public function testEmpty()
120
    {
121
        $field = $this->field;
122
123
        $fieldId = $field->ID();
124
        $this->assertEquals($fieldId, sprintf('%s_%s', $this->formId, $this->fieldName));
125
126
        $schemaStateDefaults = $field->getSchemaStateDefaults();
127
        $this->assertArraySubset(
128
            [
129
                'id' => $fieldId,
130
                'name' => $this->fieldName,
131
                'value' => 'unchanged'
132
            ],
133
            $schemaStateDefaults,
134
            true
135
        );
136
137
        $schemaDataDefaults = $field->getSchemaDataDefaults();
138
        $this->assertArraySubset(
139
            [
140
                'id' => $fieldId,
141
                'name' => $this->fieldName,
142
                'type' => 'text',
143
                'schemaType' => 'SingleSelect',
144
                'component' => 'TreeDropdownField',
145
                'holderId' => sprintf('%s_Holder', $fieldId),
146
                'title' => 'Test tree',
147
                'extraClass' => 'treemultiselect multiple searchable',
148
                'data' => [
149
                    'urlTree' => 'field/TestTree/tree',
150
                    'showSearch' => true,
151
                    'emptyString' => '(Choose File)',
152
                    'hasEmptyDefault' => false,
153
                    'multiple' => true
154
                ]
155
            ],
156
            $schemaDataDefaults,
157
            true
158
        );
159
160
        $items = $field->getItems();
161
        $this->assertCount(0, $items, 'there must be no items selected');
162
163
        $html = $field->Field();
164
        $this->assertContains($field->ID(), $html);
165
        $this->assertContains('unchanged', $html);
166
    }
167
168
169
    /**
170
     * Test the field with some values set
171
     */
172
    public function testChanged()
173
    {
174
        $field = $this->field;
175
176
        $field->setValue($this->fieldValue);
177
178
        $schemaStateDefaults = $field->getSchemaStateDefaults();
179
        $this->assertArraySubset(
180
            [
181
                'id' => $field->ID(),
182
                'name' => 'TestTree',
183
                'value' => $this->folderIds
184
            ],
185
            $schemaStateDefaults,
186
            true
187
        );
188
189
        $items = $field->getItems();
190
        $this->assertCount(2, $items, 'there must be exactly 2 items selected');
191
192
        $html = $field->Field();
193
        $this->assertContains($field->ID(), $html);
194
        $this->assertContains($this->fieldValue, $html);
195
    }
196
197
    /**
198
     * Test empty field in readonly mode
199
     */
200
    public function testEmptyReadonly()
201
    {
202
        $field = $this->field->performReadonlyTransformation();
203
204
        $schemaStateDefaults = $field->getSchemaStateDefaults();
205
        $this->assertArraySubset(
206
            [
207
                'id' => $field->ID(),
208
                'name' => 'TestTree',
209
                'value' => 'unchanged'
210
            ],
211
            $schemaStateDefaults,
212
            true
213
        );
214
215
        $schemaDataDefaults = $field->getSchemaDataDefaults();
216
        $this->assertArraySubset(
217
            [
218
                'id' => $field->ID(),
219
                'name' => $this->fieldName,
220
                'type' => 'text',
221
                'schemaType' => 'SingleSelect',
222
                'component' => 'TreeDropdownField',
223
                'holderId' => sprintf('%s_Holder', $field->ID()),
224
                'title' => 'Test tree',
225
                'extraClass' => 'treemultiselectfield_readonly multiple  searchable',
226
                'data' => [
227
                    'urlTree' => 'field/TestTree/tree',
228
                    'showSearch' => true,
229
                    'emptyString' => '(Choose File)',
230
                    'hasEmptyDefault' => false,
231
                    'multiple' => true
232
                ]
233
            ],
234
            $schemaDataDefaults,
235
            true
236
        );
237
238
        $items = $field->getItems();
239
        $this->assertCount(0, $items, 'there must be 0 selected items');
240
241
        $html = $field->Field();
242
        $this->assertContains($field->ID(), $html);
243
    }
244
245
    /**
246
     * Test changed field in readonly mode
247
     */
248
    public function testChangedReadonly()
249
    {
250
        $field = $this->field;
251
        $field->setValue($this->fieldValue);
252
        $field = $field->performReadonlyTransformation();
253
254
        $schemaStateDefaults = $field->getSchemaStateDefaults();
255
        $this->assertArraySubset(
256
            [
257
                'id' => $field->ID(),
258
                'name' => 'TestTree',
259
                'value' => $this->folderIds
260
            ],
261
            $schemaStateDefaults,
262
            true
263
        );
264
265
        $schemaDataDefaults = $field->getSchemaDataDefaults();
266
        $this->assertArraySubset(
267
            [
268
                'id' => $field->ID(),
269
                'name' => $this->fieldName,
270
                'type' => 'text',
271
                'schemaType' => 'SingleSelect',
272
                'component' => 'TreeDropdownField',
273
                'holderId' => sprintf('%s_Holder', $field->ID()),
274
                'title' => 'Test tree',
275
                'extraClass' => 'treemultiselectfield_readonly multiple  searchable',
276
                'data' => [
277
                    'urlTree' => 'field/TestTree/tree',
278
                    'showSearch' => true,
279
                    'emptyString' => '(Choose File)',
280
                    'hasEmptyDefault' => false,
281
                    'multiple' => true
282
                ]
283
            ],
284
            $schemaDataDefaults,
285
            true
286
        );
287
288
        $items = $field->getItems();
289
        $this->assertCount(2, $items, 'there must be exactly 2 selected items');
290
291
        $html = $field->Field();
292
        $this->assertContains($field->ID(), $html);
293
        $this->assertContains($this->fieldValue, $html);
294
    }
295
}
296