Passed
Push — 4 ( 3a852b...49a7f0 )
by Steve
07:35
created

testGetColumnAttributesBadArguments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\Forms\Tests\GridField;
4
5
use SilverStripe\Dev\CSSContentParser;
6
use SilverStripe\Dev\SapphireTest;
7
use SilverStripe\Forms\FieldList;
8
use SilverStripe\Forms\Form;
9
use SilverStripe\Forms\GridField\GridField;
10
use SilverStripe\Forms\GridField\GridFieldButtonRow;
11
use SilverStripe\Forms\GridField\GridFieldConfig;
12
use SilverStripe\Forms\GridField\GridFieldConfig_Base;
13
use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor;
14
use SilverStripe\Forms\GridField\GridFieldDataColumns;
15
use SilverStripe\Forms\GridField\GridFieldFilterHeader;
16
use SilverStripe\Forms\GridField\GridFieldPageCount;
17
use SilverStripe\Forms\GridField\GridFieldPaginator;
18
use SilverStripe\Forms\GridField\GridFieldSortableHeader;
19
use SilverStripe\Forms\GridField\GridFieldToolbarHeader;
20
use SilverStripe\Forms\GridField\GridState;
21
use SilverStripe\Forms\GridField\GridState_Component;
22
use SilverStripe\Forms\GridField\GridState_Data;
23
use SilverStripe\Forms\RequiredFields;
24
use SilverStripe\Forms\Tests\GridField\GridFieldTest\Cheerleader;
25
use SilverStripe\Forms\Tests\GridField\GridFieldTest\Component;
26
use SilverStripe\Forms\Tests\GridField\GridFieldTest\Component2;
27
use SilverStripe\Forms\Tests\GridField\GridFieldTest\HTMLFragments;
28
use SilverStripe\Forms\Tests\GridField\GridFieldTest\Permissions;
29
use SilverStripe\Forms\Tests\GridField\GridFieldTest\Player;
30
use SilverStripe\Forms\Tests\GridField\GridFieldTest\Team;
31
use SilverStripe\Forms\Tests\ValidatorTest\TestValidator;
32
use SilverStripe\ORM\ArrayList;
33
use SilverStripe\ORM\ValidationResult;
34
use SilverStripe\Security\Group;
35
use SilverStripe\Security\Member;
36
use SilverStripe\Versioned\VersionedGridFieldStateExtension;
37
38
class GridFieldTest extends SapphireTest
39
{
40
    protected static $extra_dataobjects = [
41
        Permissions::class,
42
        Cheerleader::class,
43
        Player::class,
44
        Team::class,
45
    ];
46
47
    protected static $illegal_extensions = [
48
        GridFieldConfig_RecordEditor::class => [
49
            VersionedGridFieldStateExtension::class,
50
        ],
51
        GridFieldConfig_Base::class => [
52
            VersionedGridFieldStateExtension::class,
53
        ],
54
    ];
55
56
    /**
57
     * @covers \SilverStripe\Forms\GridField\GridField::__construct
58
     */
59
    public function testGridField()
60
    {
61
        $obj = new GridField('testfield', 'testfield');
62
        $this->assertTrue($obj instanceof GridField, 'Test that the constructor arguments are valid');
63
    }
64
65
    /**
66
     * @covers \SilverStripe\Forms\GridField\GridField::__construct
67
     * @covers \SilverStripe\Forms\GridField\GridField::getList
68
     */
69
    public function testGridFieldSetList()
70
    {
71
        $list = ArrayList::create([1 => 'hello', 2 => 'goodbye']);
72
        $obj = new GridField('testfield', 'testfield', $list);
73
        $this->assertEquals($list, $obj->getList(), 'Testing getList');
74
    }
75
76
    /**
77
     * @covers \SilverStripe\Forms\GridField\GridField::__construct
78
     * @covers \SilverStripe\Forms\GridField\GridField::getConfig
79
     * @covers \SilverStripe\Forms\GridField\GridFieldConfig_Base::__construct
80
     * @covers \SilverStripe\Forms\GridField\GridFieldConfig::addComponent
81
     */
82
    public function testGridFieldDefaultConfig()
83
    {
84
        $obj = new GridField('testfield', 'testfield');
85
86
        $expectedComponents = new ArrayList([
87
            new GridFieldToolbarHeader(),
88
            new GridFieldButtonRow(),
89
            $sort = new GridFieldSortableHeader(),
90
            $filter = new GridFieldFilterHeader(),
91
            new GridFieldDataColumns(),
92
            new GridFieldPageCount('toolbar-header-right'),
93
            $pagination = new GridFieldPaginator(),
94
            new GridState_Component(),
95
        ]);
96
        $sort->setThrowExceptionOnBadDataType(false);
97
        $filter->setThrowExceptionOnBadDataType(false);
98
        $pagination->setThrowExceptionOnBadDataType(false);
99
100
        $this->assertEquals($expectedComponents, $obj->getConfig()->getComponents(), 'Testing default Config');
101
    }
102
103
    /**
104
     * @covers \SilverStripe\Forms\GridField\GridFieldConfig::__construct
105
     * @covers \SilverStripe\Forms\GridField\GridFieldConfig::addComponent
106
     */
107
    public function testGridFieldSetCustomConfig()
108
    {
109
110
        $config = GridFieldConfig::create();
111
        $config->addComponent(new GridFieldSortableHeader());
112
        $config->addComponent(new GridFieldDataColumns());
113
114
        $obj = new GridField('testfield', 'testfield', ArrayList::create([]), $config);
115
116
        $expectedComponents = new ArrayList(
117
            [
118
            0 => new GridFieldSortableHeader,
119
            1 => new GridFieldDataColumns,
120
            2 => new GridState_Component,
121
            ]
122
        );
123
124
        $this->assertEquals($expectedComponents, $obj->getConfig()->getComponents(), 'Testing default Config');
125
    }
126
127
    /**
128
     * @covers \SilverStripe\Forms\GridField\GridField::getModelClass
129
     * @covers \SilverStripe\Forms\GridField\GridField::setModelClass
130
     */
131
    public function testGridFieldModelClass()
132
    {
133
        $obj = new GridField('testfield', 'testfield', Member::get());
134
        $this->assertEquals(Member::class, $obj->getModelClass(), 'Should return Member');
135
        $obj->setModelClass(Group::class);
136
        $this->assertEquals(Group::class, $obj->getModelClass(), 'Should return Group');
137
    }
138
139
    /**
140
     * @covers \SilverStripe\Forms\GridField\GridField::getModelClass
141
     *
142
     * @expectedException \LogicException
143
     */
144
    public function testGridFieldModelClassThrowsException()
145
    {
146
        $obj = new GridField('testfield', 'testfield', ArrayList::create());
147
        $obj->getModelClass();
148
    }
149
150
    /**
151
     * @covers \SilverStripe\Forms\GridField\GridField::setList
152
     * @covers \SilverStripe\Forms\GridField\GridField::getList
153
     */
154
    public function testSetAndGetList()
155
    {
156
        $list = Member::get();
157
        $arrayList = ArrayList::create([1, 2, 3]);
158
        $obj = new GridField('testfield', 'testfield', $list);
159
        $this->assertEquals($list, $obj->getList());
160
        $obj->setList($arrayList);
161
        $this->assertEquals($arrayList, $obj->getList());
162
    }
163
164
    /**
165
     * @covers \SilverStripe\Forms\GridField\GridField::getState
166
     */
167
    public function testGetState()
168
    {
169
        $obj = new GridField('testfield', 'testfield');
170
        $this->assertTrue($obj->getState() instanceof GridState_Data);
171
        $this->assertTrue($obj->getState(false) instanceof GridState);
172
    }
173
174
    /**
175
     * Tests usage of nested GridState values
176
     *
177
     * @covers \SilverStripe\Forms\GridField\GridState_Data::__get
178
     * @covers \SilverStripe\Forms\GridField\GridState_Data::__call
179
     * @covers \SilverStripe\Forms\GridField\GridState_Data::getData
180
     */
181
    public function testGetStateData()
182
    {
183
        $obj = new GridField('testfield', 'testfield');
184
185
        // @todo - PHP 7.0.6 change requires __isset() to return true
186
        // for each reference from left to right along an isset() invocation.
187
        // See https://bugs.php.net/bug.php?id=62059
188
189
        // Check value persistance
190
        $this->assertEquals(15, $obj->State->NoValue(15));
0 ignored issues
show
Bug introduced by
The method NoValue() does not exist on SilverStripe\Forms\GridField\GridState_Data. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

190
        $this->assertEquals(15, $obj->State->/** @scrutinizer ignore-call */ NoValue(15));
Loading history...
191
        $this->assertEquals(15, $obj->State->NoValue(-1));
192
        $obj->State->NoValue = 10;
0 ignored issues
show
Bug Best Practice introduced by
The property NoValue does not exist on SilverStripe\Forms\GridField\GridState_Data. Since you implemented __set, consider adding a @property annotation.
Loading history...
193
        $this->assertEquals(10, $obj->State->NoValue);
0 ignored issues
show
Bug Best Practice introduced by
The property NoValue does not exist on SilverStripe\Forms\GridField\GridState_Data. Since you implemented __get, consider adding a @property annotation.
Loading history...
194
        $this->assertEquals(10, $obj->State->NoValue(20));
195
196
        // Test that values can be set, unset, and inspected
197
        $this->assertFalse(isset($obj->State->NotSet));
0 ignored issues
show
Bug Best Practice introduced by
The property NotSet does not exist on SilverStripe\Forms\GridField\GridState_Data. Since you implemented __get, consider adding a @property annotation.
Loading history...
198
        $obj->State->NotSet = false;
0 ignored issues
show
Bug Best Practice introduced by
The property NotSet does not exist on SilverStripe\Forms\GridField\GridState_Data. Since you implemented __set, consider adding a @property annotation.
Loading history...
199
        $this->assertTrue(isset($obj->State->NotSet));
200
        unset($obj->State->NotSet);
201
        $this->assertFalse(isset($obj->State->NotSet));
202
203
        // Test that false evaluating values are storable
204
        $this->assertEquals(0, $obj->State->Falsey0(0)); // expect 0 back
0 ignored issues
show
Bug introduced by
The method Falsey0() does not exist on SilverStripe\Forms\GridField\GridState_Data. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

204
        $this->assertEquals(0, $obj->State->/** @scrutinizer ignore-call */ Falsey0(0)); // expect 0 back
Loading history...
205
        $this->assertEquals(0, $obj->State->Falsey0(10)); // expect 0 back
206
        $this->assertEquals(0, $obj->State->Falsey0); //expect 0 back
0 ignored issues
show
Bug Best Practice introduced by
The property Falsey0 does not exist on SilverStripe\Forms\GridField\GridState_Data. Since you implemented __get, consider adding a @property annotation.
Loading history...
207
        $obj->State->Falsey0 = 0; //manually assign 0
0 ignored issues
show
Bug Best Practice introduced by
The property Falsey0 does not exist on SilverStripe\Forms\GridField\GridState_Data. Since you implemented __set, consider adding a @property annotation.
Loading history...
208
        $this->assertEquals(0, $obj->State->Falsey0); //expect 0 back
209
210
        // Test that false is storable
211
        $this->assertFalse($obj->State->Falsey2(false));
0 ignored issues
show
Bug introduced by
The method Falsey2() does not exist on SilverStripe\Forms\GridField\GridState_Data. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

211
        $this->assertFalse($obj->State->/** @scrutinizer ignore-call */ Falsey2(false));
Loading history...
212
        $this->assertFalse($obj->State->Falsey2(true));
213
        $this->assertFalse($obj->State->Falsey2);
0 ignored issues
show
Bug Best Practice introduced by
The property Falsey2 does not exist on SilverStripe\Forms\GridField\GridState_Data. Since you implemented __get, consider adding a @property annotation.
Loading history...
214
        $obj->State->Falsey2 = false;
0 ignored issues
show
Bug Best Practice introduced by
The property Falsey2 does not exist on SilverStripe\Forms\GridField\GridState_Data. Since you implemented __set, consider adding a @property annotation.
Loading history...
215
        $this->assertFalse($obj->State->Falsey2);
216
217
        // Check nested values
218
        $this->assertInstanceOf('SilverStripe\\Forms\\GridField\\GridState_Data', $obj->State->Nested);
0 ignored issues
show
Bug Best Practice introduced by
The property Nested does not exist on SilverStripe\Forms\GridField\GridState_Data. Since you implemented __get, consider adding a @property annotation.
Loading history...
219
        $this->assertInstanceOf('SilverStripe\\Forms\\GridField\\GridState_Data', $obj->State->Nested->DeeperNested());
220
        $this->assertEquals(3, $obj->State->Nested->DataValue(3));
221
        $this->assertEquals(10, $obj->State->Nested->DeeperNested->DataValue(10));
222
    }
223
224
    /**
225
     * @skipUpgrade
226
     * @covers \SilverStripe\Forms\GridField\GridField::getColumns
227
     */
228
    public function testGetColumns()
229
    {
230
        $obj = new GridField('testfield', 'testfield', Member::get());
231
        $expected = [
232
            0 => 'FirstName',
233
            1 => 'Surname',
234
            2 => 'Email',
235
        ];
236
        $this->assertEquals($expected, $obj->getColumns());
237
    }
238
239
    /**
240
     * @covers \SilverStripe\Forms\GridField\GridField::getColumnCount
241
     */
242
    public function testGetColumnCount()
243
    {
244
        $obj = new GridField('testfield', 'testfield', Member::get());
245
        $this->assertEquals(3, $obj->getColumnCount());
246
    }
247
248
    /**
249
     * @skipUpgrade
250
     * @covers \SilverStripe\Forms\GridField\GridField::getColumnContent
251
     */
252
    public function testGetColumnContent()
253
    {
254
        $list = new ArrayList(
255
            [
256
            new Member(["ID" => 1, "Email" => "[email protected]"])
257
            ]
258
        );
259
        $obj = new GridField('testfield', 'testfield', $list);
260
        $this->assertEquals('[email protected]', $obj->getColumnContent($list->first(), 'Email'));
261
    }
262
263
    /**
264
     * @skipUpgrade
265
     * @covers \SilverStripe\Forms\GridField\GridField::getColumnContent
266
     *
267
     * @expectedException \InvalidArgumentException
268
     */
269
    public function testGetColumnContentBadArguments()
270
    {
271
        $list = new ArrayList(
272
            [
273
            new Member(["ID" => 1, "Email" => "[email protected]"])
274
            ]
275
        );
276
        $obj = new GridField('testfield', 'testfield', $list);
277
        $obj->getColumnContent($list->first(), 'non-existing');
278
    }
279
280
    /**
281
     * @skipUpgrade
282
     * @covers \SilverStripe\Forms\GridField\GridField::getColumnAttributes
283
     */
284
    public function testGetColumnAttributesEmptyArray()
285
    {
286
        $list = new ArrayList(
287
            [
288
            new Member(["ID" => 1, "Email" => "[email protected]"])
289
            ]
290
        );
291
        $obj = new GridField('testfield', 'testfield', $list);
292
        $this->assertEquals(['class' => 'col-Email'], $obj->getColumnAttributes($list->first(), 'Email'));
293
    }
294
295
    /**
296
     * @skipUpgrade
297
     * @covers \SilverStripe\Forms\GridField\GridField::getColumnAttributes
298
     */
299
    public function testGetColumnAttributes()
300
    {
301
        $list = new ArrayList(
302
            [
303
            new Member(["ID" => 1, "Email" => "[email protected]"])
304
            ]
305
        );
306
        $config = GridFieldConfig::create()->addComponent(new Component);
307
        $obj = new GridField('testfield', 'testfield', $list, $config);
308
        $this->assertEquals(['class' => 'css-class'], $obj->getColumnAttributes($list->first(), 'Email'));
309
    }
310
311
    /**
312
     * @covers \SilverStripe\Forms\GridField\GridField::getColumnAttributes
313
     *
314
     * @expectedException \InvalidArgumentException
315
     */
316
    public function testGetColumnAttributesBadArguments()
317
    {
318
        $list = new ArrayList(
319
            [
320
            new Member(["ID" => 1, "Email" => "[email protected]"])
321
            ]
322
        );
323
        $config = GridFieldConfig::create()->addComponent(new Component);
324
        $obj = new GridField('testfield', 'testfield', $list, $config);
325
        $obj->getColumnAttributes($list->first(), 'Non-existing');
326
    }
327
328
    /**
329
     * @expectedException \LogicException
330
     */
331
    public function testGetColumnAttributesBadResponseFromComponent()
332
    {
333
        $list = new ArrayList(
334
            [
335
            new Member(["ID" => 1, "Email" => "[email protected]"])
336
            ]
337
        );
338
        $config = GridFieldConfig::create()->addComponent(new Component);
339
        $obj = new GridField('testfield', 'testfield', $list, $config);
340
        $obj->getColumnAttributes($list->first(), 'Surname');
341
    }
342
343
    /**
344
     * @skipUpgrade
345
     * @covers \SilverStripe\Forms\GridField\GridField::getColumnMetadata
346
     */
347
    public function testGetColumnMetadata()
348
    {
349
        $list = new ArrayList(
350
            [
351
            new Member(["ID" => 1, "Email" => "[email protected]"])
352
            ]
353
        );
354
        $config = GridFieldConfig::create()->addComponent(new Component);
355
        $obj = new GridField('testfield', 'testfield', $list, $config);
356
        $this->assertEquals(['metadata' => 'istrue'], $obj->getColumnMetadata('Email'));
357
    }
358
359
    /**
360
     * @covers \SilverStripe\Forms\GridField\GridField::getColumnMetadata
361
     *
362
     * @expectedException \LogicException
363
     */
364
    public function testGetColumnMetadataBadResponseFromComponent()
365
    {
366
        $list = new ArrayList(
367
            [
368
            new Member(["ID" => 1, "Email" => "[email protected]"])
369
            ]
370
        );
371
        $config = GridFieldConfig::create()->addComponent(new Component);
372
        $obj = new GridField('testfield', 'testfield', $list, $config);
373
        $obj->getColumnMetadata('Surname');
374
    }
375
376
    /**
377
     * @covers \SilverStripe\Forms\GridField\GridField::getColumnMetadata
378
     *
379
     * @expectedException \InvalidArgumentException
380
     */
381
    public function testGetColumnMetadataBadArguments()
382
    {
383
        $list = ArrayList::create();
384
        $config = GridFieldConfig::create()->addComponent(new Component);
385
        $obj = new GridField('testfield', 'testfield', $list, $config);
386
        $obj->getColumnMetadata('non-exist-qweqweqwe');
387
    }
388
389
    /**
390
     * @covers \SilverStripe\Forms\GridField\GridField::handleAction
391
     *
392
     * @expectedException \InvalidArgumentException
393
     */
394
    public function testHandleActionBadArgument()
395
    {
396
        $obj = new GridField('testfield', 'testfield');
397
        $obj->handleAlterAction('prft', [], []);
398
    }
399
400
    /**
401
     * @covers \SilverStripe\Forms\GridField\GridField::handleAction
402
     */
403
    public function testHandleAction()
404
    {
405
        $config = GridFieldConfig::create()->addComponent(new Component);
406
        $obj = new GridField('testfield', 'testfield', ArrayList::create(), $config);
407
        $this->assertEquals('handledAction is executed', $obj->handleAlterAction('jump', [], []));
408
    }
409
410
    /**
411
     * @covers \SilverStripe\Forms\GridField\GridField::getCastedValue
412
     */
413
    public function testGetCastedValue()
414
    {
415
        $obj = new GridField('testfield', 'testfield');
416
        $value = $obj->getCastedValue('This is a sentance. This ia another.', ['Text->FirstSentence']);
417
        $this->assertEquals('This is a sentance.', $value);
418
    }
419
420
    /**
421
     * @covers \SilverStripe\Forms\GridField\GridField::getCastedValue
422
     */
423
    public function testGetCastedValueObject()
424
    {
425
        $obj = new GridField('testfield', 'testfield');
426
        $value = $obj->getCastedValue('Here is some <html> content', 'Text');
427
        $this->assertEquals('Here is some &lt;html&gt; content', $value);
428
    }
429
430
    /**
431
     * @covers \SilverStripe\Forms\GridField\GridField::gridFieldAlterAction
432
     */
433
    public function testGridFieldAlterAction()
434
    {
435
        $this->markTestIncomplete();
436
437
        // $config = GridFieldConfig::create()->addComponent(new GridFieldTest_Component);
438
        // $obj = new GridField('testfield', 'testfield', ArrayList::create(), $config);
439
        // $id = 'testGridStateActionField';
440
        // Session::set($id, array('grid'=>'', 'actionName'=>'jump'));
441
        // $form = new Form(null, 'mockform', new FieldList(array($obj)), new FieldList());
442
        // $request = new HTTPRequest('POST', 'url');
443
        // $obj->gridFieldAlterAction(array('StateID'=>$id), $form, $request);
444
    }
445
446
    /**
447
     * Test the interface for adding custom HTML fragment slots via a component
448
     */
449
    public function testGridFieldCustomFragments()
450
    {
451
452
        new HTMLFragments(
453
            [
454
            "header-left-actions" => "left\$DefineFragment(nested-left)",
455
            "header-right-actions" => "right",
456
            ]
457
        );
458
459
        new HTMLFragments(
460
            [
461
            "nested-left" => "[inner]",
462
            ]
463
        );
464
465
466
        $config = GridFieldConfig::create()->addComponents(
467
            new HTMLFragments(
468
                [
469
                "header" => "<tr><td><div class=\"right\">\$DefineFragment(header-right-actions)</div>"
470
                    . "<div class=\"left\">\$DefineFragment(header-left-actions)</div></td></tr>",
471
                ]
472
            ),
473
            new HTMLFragments(
474
                [
475
                "header-left-actions" => "left",
476
                "header-right-actions" => "rightone",
477
                ]
478
            ),
479
            new HTMLFragments(
480
                [
481
                "header-right-actions" => "righttwo",
482
                ]
483
            )
484
        );
485
        $field = new GridField('testfield', 'testfield', ArrayList::create(), $config);
486
        $form = new Form(null, 'testform', new FieldList([$field]), new FieldList());
0 ignored issues
show
Unused Code introduced by
The assignment to $form is dead and can be removed.
Loading history...
487
488
        $this->assertContains(
489
            "<div class=\"right\">rightone\nrighttwo</div><div class=\"left\">left</div>",
490
            $field->FieldHolder()
491
        );
492
    }
493
494
    /**
495
     * Test the nesting of custom fragments
496
     */
497
    public function testGridFieldCustomFragmentsNesting()
498
    {
499
        $config = GridFieldConfig::create()->addComponents(
500
            new HTMLFragments(
501
                [
502
                "level-one" => "first",
503
                ]
504
            ),
505
            new HTMLFragments(
506
                [
507
                "before" => "<div>\$DefineFragment(level-one)</div>",
508
                ]
509
            ),
510
            new HTMLFragments(
511
                [
512
                "level-one" => "<strong>\$DefineFragment(level-two)</strong>",
513
                ]
514
            ),
515
            new HTMLFragments(
516
                [
517
                "level-two" => "second",
518
                ]
519
            )
520
        );
521
        $field = new GridField('testfield', 'testfield', ArrayList::create(), $config);
522
        $form = new Form(null, 'testform', new FieldList([$field]), new FieldList());
0 ignored issues
show
Unused Code introduced by
The assignment to $form is dead and can be removed.
Loading history...
523
524
        $this->assertContains(
525
            "<div>first\n<strong>second</strong></div>",
526
            $field->FieldHolder()
527
        );
528
    }
529
530
    /**
531
     * Test that circular dependencies throw an exception
532
     *
533
     * @expectedException \LogicException
534
     */
535
    public function testGridFieldCustomFragmentsCircularDependencyThrowsException()
536
    {
537
        $config = GridFieldConfig::create()->addComponents(
538
            new HTMLFragments(
539
                [
540
                "level-one" => "first",
541
                ]
542
            ),
543
            new HTMLFragments(
544
                [
545
                "before" => "<div>\$DefineFragment(level-one)</div>",
546
                ]
547
            ),
548
            new HTMLFragments(
549
                [
550
                "level-one" => "<strong>\$DefineFragment(level-two)</strong>",
551
                ]
552
            ),
553
            new HTMLFragments(
554
                [
555
                "level-two" => "<blink>\$DefineFragment(level-one)</blink>",
556
                ]
557
            )
558
        );
559
        $field = new GridField('testfield', 'testfield', ArrayList::create(), $config);
560
        $form = new Form(null, 'testform', new FieldList([$field]), new FieldList());
0 ignored issues
show
Unused Code introduced by
The assignment to $form is dead and can be removed.
Loading history...
561
562
        $field->FieldHolder();
563
    }
564
565
    /**
566
     * @covers \SilverStripe\Forms\GridField\GridField::FieldHolder
567
     */
568
    public function testCanViewOnlyOddIDs()
569
    {
570
        $this->logInWithPermission();
571
        $list = new ArrayList(
572
            [
573
            new Permissions(
574
                [
575
                "ID" => 1,
576
                "Email" => "[email protected]",
577
                'Name' => 'Ongi Schwimmer'
578
                ]
579
            ),
580
            new Permissions(
581
                [
582
                "ID" => 2,
583
                "Email" => "[email protected]",
584
                'Name' => 'Klaus Lozenge'
585
                ]
586
            ),
587
            new Permissions(
588
                [
589
                "ID" => 3,
590
                "Email" => "[email protected]",
591
                'Name' => 'Otto Fischer'
592
                ]
593
            )
594
            ]
595
        );
596
597
        $config = new GridFieldConfig();
598
        $config->addComponent(new GridFieldDataColumns());
599
        $obj = new GridField('testfield', 'testfield', $list, $config);
600
        $form = new Form(null, 'mockform', new FieldList([$obj]), new FieldList());
0 ignored issues
show
Unused Code introduced by
The assignment to $form is dead and can be removed.
Loading history...
601
        $content = new CSSContentParser($obj->FieldHolder());
602
603
        $members = $content->getBySelector('.ss-gridfield-item tr');
604
605
        $this->assertEquals(2, count($members));
606
607
        $this->assertEquals(
608
            (string)$members[0]->td[0],
609
            'Ongi Schwimmer',
610
            'First object Name should be Ongi Schwimmer'
611
        );
612
        $this->assertEquals(
613
            (string)$members[0]->td[1],
614
            '[email protected]',
615
            'First object Email should be [email protected]'
616
        );
617
618
        $this->assertEquals(
619
            (string)$members[1]->td[0],
620
            'Otto Fischer',
621
            'Second object Name should be Otto Fischer'
622
        );
623
        $this->assertEquals(
624
            (string)$members[1]->td[1],
625
            '[email protected]',
626
            'Second object Email should be [email protected]'
627
        );
628
    }
629
630
    public function testChainedDataManipulators()
631
    {
632
        $config = new GridFieldConfig();
633
        $data = new ArrayList([1, 2, 3, 4, 5, 6]);
634
        $gridField = new GridField('testfield', 'testfield', $data, $config);
635
        $endList = $gridField->getManipulatedList();
636
        $this->assertEquals($endList->count(), 6);
637
638
        $config->addComponent(new Component2);
639
        $endList = $gridField->getManipulatedList();
640
        $this->assertEquals($endList->count(), 12);
641
642
        $config->addComponent(new GridFieldPaginator(10));
643
        $endList = $gridField->getManipulatedList();
644
        $this->assertEquals($endList->count(), 10);
645
    }
646
647
    public function testValidationMessageInOutput()
648
    {
649
        $gridField = new GridField('testfield', 'testfield', new ArrayList(), new GridFieldConfig());
650
        $fieldList = new FieldList([$gridField]);
651
        $validator = new TestValidator();
652
        $form = new Form(null, "testForm", $fieldList, new FieldList(), $validator);
653
654
        // A form that fails validation should display the validation error in the FieldHolder output.
655
        $form->validationResult();
656
        $gridfieldOutput = $gridField->FieldHolder();
657
        $this->assertContains('<p class="message ' . ValidationResult::TYPE_ERROR . '">error</p>', $gridfieldOutput);
658
659
        // Clear validation error from previous assertion.
660
        $validator->removeValidation();
661
        $gridField->setMessage(null);
662
663
        // A form that passes validation should not display a validation error in the FieldHolder output.
664
        $form->setValidator(new RequiredFields());
665
        $form->validationResult();
666
        $gridfieldOutput = $gridField->FieldHolder();
667
        $this->assertNotContains('<p class="message ' . ValidationResult::TYPE_ERROR . '">', $gridfieldOutput);
668
    }
669
}
670