Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Passed
Pull Request — master (#3410)
by
unknown
26:48 queued 11:51
created

CrudPanelFieldsTest::testOrderColumnsMixedList()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Backpack\CRUD\Tests\Unit\CrudPanel;
4
5
use Backpack\CRUD\Tests\Unit\Models\User;
6
7
class CrudPanelFieldsTest extends BaseDBCrudPanelTest
8
{
9
    private $oneTextFieldArray = [
0 ignored issues
show
introduced by
The private property $oneTextFieldArray is not used, and could be removed.
Loading history...
10
        'name'  => 'field1',
11
        'label' => 'Field1',
12
        'type'  => 'text',
13
    ];
14
15
    private $expectedOneTextFieldArray = [
16
        'field1' => [
17
            'name'  => 'field1',
18
            'label' => 'Field1',
19
            'type'  => 'text',
20
        ],
21
    ];
22
23
    private $unknownFieldTypeArray = [
24
        'name' => 'field1',
25
        'type' => 'unknownType',
26
    ];
27
28
    private $invalidTwoFieldsArray = [
29
        [
30
            'keyOne' => 'field1',
31
            'keyTwo' => 'Field1',
32
        ],
33
        [
34
            'otherKey' => 'field2',
35
        ],
36
    ];
37
38
    private $twoTextFieldsArray = [
39
        [
40
            'name'  => 'field1',
41
            'label' => 'Field1',
42
            'type'  => 'text',
43
        ],
44
        [
45
            'name'  => 'field2',
46
            'label' => 'Field2',
47
        ],
48
    ];
49
50
    private $expectedTwoTextFieldsArray = [
0 ignored issues
show
introduced by
The private property $expectedTwoTextFieldsArray is not used, and could be removed.
Loading history...
51
        'field1' => [
52
            'name'  => 'field1',
53
            'label' => 'Field1',
54
            'type'  => 'text',
55
        ],
56
        'field2' => [
57
            'name'  => 'field2',
58
            'label' => 'Field2',
59
            'type'  => 'text',
60
        ],
61
    ];
62
63
    private $threeTextFieldsArray = [
64
        [
65
            'name'  => 'field1',
66
            'label' => 'Field1',
67
            'type'  => 'text',
68
        ],
69
        [
70
            'name'  => 'field2',
71
            'label' => 'Field2',
72
        ],
73
        [
74
            'name' => 'field3',
75
        ],
76
    ];
77
78
    private $expectedThreeTextFieldsArray = [
79
        'field1' => [
80
            'name'  => 'field1',
81
            'label' => 'Field1',
82
            'type'  => 'text',
83
        ],
84
        'field2' => [
85
            'name'  => 'field2',
86
            'label' => 'Field2',
87
            'type'  => 'text',
88
        ],
89
        'field3' => [
90
            'name'  => 'field3',
91
            'label' => 'Field3',
92
            'type'  => 'text',
93
        ],
94
    ];
95
96
    private $multipleFieldTypesArray = [
97
        [
98
            'name'  => 'field1',
99
            'label' => 'Field1',
100
        ],
101
        [
102
            'name' => 'field2',
103
            'type' => 'address',
104
        ],
105
        [
106
            'name' => 'field3',
107
            'type' => 'address',
108
        ],
109
        [
110
            'name' => 'field4',
111
            'type' => 'checkbox',
112
        ],
113
        [
114
            'name' => 'field5',
115
            'type' => 'date',
116
        ],
117
        [
118
            'name' => 'field6',
119
            'type' => 'email',
120
        ],
121
        [
122
            'name' => 'field7',
123
            'type' => 'hidden',
124
        ],
125
        [
126
            'name' => 'field8',
127
            'type' => 'password',
128
        ],
129
        [
130
            'name' => 'field9',
131
            'type' => 'select2',
132
        ],
133
        [
134
            'name' => 'field10',
135
            'type' => 'select2_multiple',
136
        ],
137
        [
138
            'name' => 'field11',
139
            'type' => 'table',
140
        ],
141
        [
142
            'name' => 'field12',
143
            'type' => 'url',
144
        ],
145
    ];
146
147
    private $expectedMultipleFieldTypesArray = [
148
        'field1' => [
149
            'name'  => 'field1',
150
            'label' => 'Field1',
151
            'type'  => 'text',
152
        ],
153
        'field2' => [
154
            'name'  => 'field2',
155
            'type'  => 'address',
156
            'label' => 'Field2',
157
        ],
158
        'field3' => [
159
            'name'  => 'field3',
160
            'type'  => 'address',
161
            'label' => 'Field3',
162
        ],
163
        'field4' => [
164
            'name'  => 'field4',
165
            'type'  => 'checkbox',
166
            'label' => 'Field4',
167
        ],
168
        'field5' => [
169
            'name'  => 'field5',
170
            'type'  => 'date',
171
            'label' => 'Field5',
172
        ],
173
        'field6' => [
174
            'name'  => 'field6',
175
            'type'  => 'email',
176
            'label' => 'Field6',
177
        ],
178
        'field7' => [
179
            'name'  => 'field7',
180
            'type'  => 'hidden',
181
            'label' => 'Field7',
182
        ],
183
        'field8' => [
184
            'name'  => 'field8',
185
            'type'  => 'password',
186
            'label' => 'Field8',
187
        ],
188
        'field9' => [
189
            'name'  => 'field9',
190
            'type'  => 'select2',
191
            'label' => 'Field9',
192
        ],
193
        'field10' => [
194
            'name'  => 'field10',
195
            'type'  => 'select2_multiple',
196
            'label' => 'Field10',
197
        ],
198
        'field11' => [
199
            'name'  => 'field11',
200
            'type'  => 'table',
201
            'label' => 'Field11',
202
        ],
203
        'field12' => [
204
            'name'  => 'field12',
205
            'type'  => 'url',
206
            'label' => 'Field12',
207
        ],
208
    ];
209
210
    /**
211
     * Setup the test environment.
212
     *
213
     * @return void
214
     */
215
    protected function setUp(): void
216
    {
217
        parent::setUp();
218
219
        $this->crudPanel->setOperation('create');
220
    }
221
222
    public function testAddFieldByName()
223
    {
224
        $this->crudPanel->addField('field1');
225
226
        $this->assertEquals(1, count($this->crudPanel->fields()));
227
        $this->assertEquals($this->expectedOneTextFieldArray, $this->crudPanel->fields());
228
    }
229
230
    public function testAddFieldsByName()
231
    {
232
        $this->crudPanel->addFields(['field1', 'field2', 'field3']);
233
234
        $this->assertEquals(3, count($this->crudPanel->fields()));
235
        $this->assertEquals($this->expectedThreeTextFieldsArray, $this->crudPanel->fields());
236
    }
237
238
    public function testAddFieldsAsArray()
239
    {
240
        $this->crudPanel->addFields($this->threeTextFieldsArray);
241
242
        $this->assertEquals(3, count($this->crudPanel->fields()));
243
        $this->assertEquals($this->expectedThreeTextFieldsArray, $this->crudPanel->fields());
244
    }
245
246
    public function testAddFieldsDifferentTypes()
247
    {
248
        $this->crudPanel->addFields($this->multipleFieldTypesArray);
249
250
        $this->assertEquals(12, count($this->crudPanel->fields()));
251
        $this->assertEquals($this->expectedMultipleFieldTypesArray, $this->crudPanel->fields());
252
    }
253
254
    public function testAddFieldsInvalidArray()
255
    {
256
        $this->expectException(\Exception::class);
257
258
        $this->crudPanel->addFields($this->invalidTwoFieldsArray);
259
    }
260
261
    public function testAddFieldWithInvalidType()
262
    {
263
        $this->markTestIncomplete('Not correctly implemented');
264
265
        // TODO: should we validate field types and throw an error if they're not in the pre-defined list of fields or
266
        //       in the list of custom field?
267
        $this->crudPanel->addFields($this->unknownFieldTypeArray);
268
    }
269
270
    public function testAddFieldsForCreateForm()
271
    {
272
        $this->crudPanel->addFields($this->threeTextFieldsArray, 'create');
0 ignored issues
show
Unused Code introduced by
The call to Backpack\CRUD\app\Librar...\CrudPanel::addFields() has too many arguments starting with 'create'. ( Ignorable by Annotation )

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

272
        $this->crudPanel->/** @scrutinizer ignore-call */ 
273
                          addFields($this->threeTextFieldsArray, 'create');

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
273
274
        $this->assertEquals(3, count($this->crudPanel->fields()));
275
        $this->assertEquals($this->expectedThreeTextFieldsArray, $this->crudPanel->fields());
276
    }
277
278
    public function testAddFieldsForUpdateForm()
279
    {
280
        $this->crudPanel->addFields($this->threeTextFieldsArray, 'update');
0 ignored issues
show
Unused Code introduced by
The call to Backpack\CRUD\app\Librar...\CrudPanel::addFields() has too many arguments starting with 'update'. ( Ignorable by Annotation )

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

280
        $this->crudPanel->/** @scrutinizer ignore-call */ 
281
                          addFields($this->threeTextFieldsArray, 'update');

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
281
282
        $this->assertEquals(3, count($this->crudPanel->fields()));
283
        $this->assertEquals($this->expectedThreeTextFieldsArray, $this->crudPanel->fields());
284
    }
285
286
    public function testBeforeField()
287
    {
288
        $this->crudPanel->addFields($this->threeTextFieldsArray);
289
        $this->crudPanel->beforeField('field2');
290
291
        $createKeys = array_keys($this->crudPanel->fields());
292
        $this->assertEquals($this->expectedThreeTextFieldsArray['field3'], $this->crudPanel->fields()[$createKeys[1]]);
293
        $this->assertEquals(['field1', 'field3', 'field2'], $createKeys);
294
    }
295
296
    public function testBeforeFieldFirstField()
297
    {
298
        $this->crudPanel->addFields($this->threeTextFieldsArray);
299
        $this->crudPanel->beforeField('field1');
300
301
        $createKeys = array_keys($this->crudPanel->fields());
302
        $this->assertEquals($this->expectedThreeTextFieldsArray['field3'], $this->crudPanel->fields()[$createKeys[0]]);
303
        $this->assertEquals(['field3', 'field1', 'field2'], $createKeys);
304
305
        $updateKeys = array_keys($this->crudPanel->fields());
306
        $this->assertEquals($this->expectedThreeTextFieldsArray['field3'], $this->crudPanel->fields()[$updateKeys[0]]);
307
        $this->assertEquals(['field3', 'field1', 'field2'], $updateKeys);
308
    }
309
310
    public function testBeforeFieldLastField()
311
    {
312
        $this->crudPanel->addFields($this->threeTextFieldsArray);
313
        $this->crudPanel->beforeField('field3');
314
315
        $createKeys = array_keys($this->crudPanel->fields());
316
        $this->assertEquals($this->expectedThreeTextFieldsArray['field3'], $this->crudPanel->fields()[$createKeys[2]]);
317
        $this->assertEquals(['field1', 'field2', 'field3'], $createKeys);
318
    }
319
320
    public function testBeforeFieldCreateForm()
321
    {
322
        $this->crudPanel->addFields($this->threeTextFieldsArray);
323
        $this->crudPanel->beforeField('field1');
324
325
        $createKeys = array_keys($this->crudPanel->fields());
326
        $this->assertEquals($this->expectedThreeTextFieldsArray['field3'], $this->crudPanel->fields()[$createKeys[0]]);
327
        $this->assertEquals(['field3', 'field1', 'field2'], $createKeys);
328
    }
329
330
    public function testBeforeUnknownField()
331
    {
332
        $this->crudPanel->addFields($this->threeTextFieldsArray);
333
334
        $this->crudPanel->beforeField('field4');
335
336
        $this->assertEquals(3, count($this->crudPanel->fields()));
337
        $this->assertEquals(array_keys($this->expectedThreeTextFieldsArray), array_keys($this->crudPanel->fields()));
338
    }
339
340
    public function testAfterField()
341
    {
342
        $this->crudPanel->addFields($this->threeTextFieldsArray);
343
344
        $this->crudPanel->afterField('field1');
345
346
        $createKeys = array_keys($this->crudPanel->fields());
347
        $this->assertEquals($this->expectedThreeTextFieldsArray['field3'], $this->crudPanel->fields()[$createKeys[1]]);
348
        $this->assertEquals(['field1', 'field3', 'field2'], $createKeys);
349
350
        $updateKeys = array_keys($this->crudPanel->fields());
351
        $this->assertEquals($this->expectedThreeTextFieldsArray['field3'], $this->crudPanel->fields()[$updateKeys[1]]);
352
        $this->assertEquals(['field1', 'field3', 'field2'], $updateKeys);
353
    }
354
355
    public function testAfterFieldLastField()
356
    {
357
        $this->crudPanel->addFields($this->threeTextFieldsArray);
358
359
        $this->crudPanel->afterField('field3');
360
361
        $createKeys = array_keys($this->crudPanel->fields());
362
        $this->assertEquals($this->expectedThreeTextFieldsArray['field3'], $this->crudPanel->fields()[$createKeys[2]]);
363
        $this->assertEquals(['field1', 'field2', 'field3'], $createKeys);
364
365
        $updateKeys = array_keys($this->crudPanel->fields());
366
        $this->assertEquals($this->expectedThreeTextFieldsArray['field3'], $this->crudPanel->fields()[$updateKeys[2]]);
367
        $this->assertEquals(['field1', 'field2', 'field3'], $updateKeys);
368
    }
369
370
    public function testAfterFieldOnCertainField()
371
    {
372
        $this->crudPanel->addFields($this->threeTextFieldsArray);
373
        $this->crudPanel->addField('custom')->afterField('field1');
374
375
        $createKeys = array_keys($this->crudPanel->fields());
376
        $this->assertEquals(['field1', 'custom', 'field2', 'field3'], $createKeys);
377
    }
378
379
    public function testAfterUnknownField()
380
    {
381
        $this->crudPanel->addFields($this->threeTextFieldsArray);
382
383
        $this->crudPanel->afterField('field4');
384
385
        $this->assertEquals(3, count($this->crudPanel->fields()));
386
        $this->assertEquals(array_keys($this->expectedThreeTextFieldsArray), array_keys($this->crudPanel->fields()));
387
    }
388
389
    public function testRemoveFieldsByName()
390
    {
391
        $this->crudPanel->addFields($this->threeTextFieldsArray);
392
393
        $this->crudPanel->removeFields(['field1']);
394
395
        $this->assertEquals(2, count($this->crudPanel->fields()));
396
        $this->assertEquals(['field2', 'field3'], array_keys($this->crudPanel->fields()));
397
    }
398
399
    public function testRemoveFieldsByNameInvalidArray()
400
    {
401
        $this->markTestIncomplete('Not correctly implemented');
402
403
        $this->crudPanel->addFields($this->threeTextFieldsArray);
404
405
        // TODO: this should not work because the method specifically asks for an array of field keys, but it does
406
        //       because the removeField method will actually work with arrays instead of a string
407
        $this->crudPanel->removeFields($this->twoTextFieldsArray);
408
409
        $this->assertEquals(3, count($this->crudPanel->fields()));
410
        $this->assertEquals(array_keys($this->expectedThreeTextFieldsArray), array_keys($this->crudPanel->fields()));
411
    }
412
413
    public function testRemoveFieldsFromCreateForm()
414
    {
415
        $this->crudPanel->addFields($this->threeTextFieldsArray);
416
        $this->crudPanel->removeFields(['field1']);
417
418
        $this->assertEquals(2, count($this->crudPanel->fields()));
419
        $this->assertEquals(['field2', 'field3'], array_keys($this->crudPanel->fields()));
420
    }
421
422
    public function testRemoveFieldsFromUpdateForm()
423
    {
424
        $this->crudPanel->addFields($this->threeTextFieldsArray);
425
        $this->crudPanel->removeFields(['field1']);
426
427
        $this->assertEquals(2, count($this->crudPanel->fields()));
428
        $this->assertEquals(['field2', 'field3'], array_keys($this->crudPanel->fields()));
429
    }
430
431
    public function testRemoveUnknownFields()
432
    {
433
        $this->crudPanel->addFields($this->threeTextFieldsArray);
434
435
        $this->crudPanel->removeFields(['field4']);
436
437
        $this->assertEquals(3, count($this->crudPanel->fields()));
438
        $this->assertEquals(3, count($this->crudPanel->fields()));
439
        $this->assertEquals(array_keys($this->expectedThreeTextFieldsArray), array_keys($this->crudPanel->fields()));
440
        $this->assertEquals(array_keys($this->expectedThreeTextFieldsArray), array_keys($this->crudPanel->fields()));
441
    }
442
443
    public function testOrderFields()
444
    {
445
        $this->crudPanel->addFields($this->threeTextFieldsArray);
446
447
        $this->crudPanel->orderFields(['field2', 'field1', 'field3']);
448
449
        $this->assertEquals(['field2', 'field1', 'field3'], array_keys($this->crudPanel->fields()));
450
    }
451
452
    public function testOrderFieldsCreateForm()
453
    {
454
        $this->crudPanel->addFields($this->threeTextFieldsArray);
455
456
        $this->crudPanel->orderFields(['field2', 'field1', 'field3'], 'create');
0 ignored issues
show
Unused Code introduced by
The call to Backpack\CRUD\app\Librar...rudPanel::orderFields() has too many arguments starting with 'create'. ( Ignorable by Annotation )

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

456
        $this->crudPanel->/** @scrutinizer ignore-call */ 
457
                          orderFields(['field2', 'field1', 'field3'], 'create');

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
457
458
        $this->assertEquals(['field2', 'field1', 'field3'], array_keys($this->crudPanel->fields()));
459
        $this->assertEquals($this->expectedThreeTextFieldsArray, $this->crudPanel->fields());
460
    }
461
462
    public function testOrderFieldsUpdateForm()
463
    {
464
        $this->crudPanel->addFields($this->threeTextFieldsArray);
465
466
        $this->crudPanel->orderFields(['field2', 'field1', 'field3'], 'update');
0 ignored issues
show
Unused Code introduced by
The call to Backpack\CRUD\app\Librar...rudPanel::orderFields() has too many arguments starting with 'update'. ( Ignorable by Annotation )

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

466
        $this->crudPanel->/** @scrutinizer ignore-call */ 
467
                          orderFields(['field2', 'field1', 'field3'], 'update');

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
467
468
        $this->assertEquals($this->expectedThreeTextFieldsArray, $this->crudPanel->fields());
469
        $this->assertEquals(['field2', 'field1', 'field3'], array_keys($this->crudPanel->fields()));
470
    }
471
472
    public function testOrderFieldsIncompleteList()
473
    {
474
        $this->crudPanel->addFields($this->threeTextFieldsArray);
475
476
        $this->crudPanel->orderFields(['field2', 'field3']);
477
478
        $this->assertEquals(['field2', 'field3', 'field1'], array_keys($this->crudPanel->fields()));
479
    }
480
481
    public function testOrderFieldsEmptyList()
482
    {
483
        $this->crudPanel->addFields($this->threeTextFieldsArray);
484
485
        $this->crudPanel->orderFields([]);
486
487
        $this->assertEquals($this->expectedThreeTextFieldsArray, $this->crudPanel->fields());
488
    }
489
490
    public function testOrderFieldsUnknownList()
491
    {
492
        $this->crudPanel->addFields($this->threeTextFieldsArray);
493
494
        $this->crudPanel->orderFields(['field4', 'field5', 'field6']);
495
496
        $this->assertEquals($this->expectedThreeTextFieldsArray, $this->crudPanel->fields());
497
    }
498
499
    public function testOrderColumnsMixedList()
500
    {
501
        $this->crudPanel->addFields($this->threeTextFieldsArray);
502
503
        $this->crudPanel->orderFields(['field2', 'field5', 'field6']);
504
505
        $this->assertEquals(['field2', 'field1', 'field3'], array_keys($this->crudPanel->fields()));
506
    }
507
508
    public function testCheckIfFieldIsFirstOfItsType()
509
    {
510
        $this->crudPanel->addFields($this->multipleFieldTypesArray);
511
512
        $isFirstAddressFieldFirst = $this->crudPanel->checkIfFieldIsFirstOfItsType($this->multipleFieldTypesArray[1]);
513
        $isSecondAddressFieldFirst = $this->crudPanel->checkIfFieldIsFirstOfItsType($this->multipleFieldTypesArray[2]);
514
515
        $this->assertTrue($isFirstAddressFieldFirst);
516
        $this->assertFalse($isSecondAddressFieldFirst);
517
    }
518
519
    public function testCheckIfUnknownFieldIsFirstOfItsType()
520
    {
521
        $isUnknownFieldFirst = $this->crudPanel->checkIfFieldIsFirstOfItsType($this->unknownFieldTypeArray, $this->expectedMultipleFieldTypesArray);
0 ignored issues
show
Unused Code introduced by
The call to Backpack\CRUD\app\Librar...FieldIsFirstOfItsType() has too many arguments starting with $this->expectedMultipleFieldTypesArray. ( Ignorable by Annotation )

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

521
        /** @scrutinizer ignore-call */ 
522
        $isUnknownFieldFirst = $this->crudPanel->checkIfFieldIsFirstOfItsType($this->unknownFieldTypeArray, $this->expectedMultipleFieldTypesArray);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
522
523
        $this->assertFalse($isUnknownFieldFirst);
524
    }
525
526
    public function testCheckIfInvalidFieldIsFirstOfItsType()
527
    {
528
        $this->expectException(\ErrorException::class);
529
530
        $this->crudPanel->checkIfFieldIsFirstOfItsType($this->invalidTwoFieldsArray[0], $this->expectedMultipleFieldTypesArray);
0 ignored issues
show
Unused Code introduced by
The call to Backpack\CRUD\app\Librar...FieldIsFirstOfItsType() has too many arguments starting with $this->expectedMultipleFieldTypesArray. ( Ignorable by Annotation )

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

530
        $this->crudPanel->/** @scrutinizer ignore-call */ 
531
                          checkIfFieldIsFirstOfItsType($this->invalidTwoFieldsArray[0], $this->expectedMultipleFieldTypesArray);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
531
    }
532
533
    public function testDecodeJsonCastedAttributes()
534
    {
535
        $this->markTestIncomplete();
536
537
        // TODO: the decode JSON method should not be in fields trait and should not be exposed in the public API.
538
    }
539
540
    public function testFieldNameDotNotationIsRelationship()
541
    {
542
        $this->crudPanel->setModel(User::class);
543
        $this->crudPanel->addField('accountDetails.nickname');
544
        $field = $this->crudPanel->fields()['accountDetails[nickname]'];
545
        $this->assertEquals($field['relation_type'], 'HasOne');
546
    }
547
548
    public function testCanInferRelationTypeFromRelationName()
549
    {
550
        $this->crudPanel->setModel(User::class);
551
        $this->crudPanel->addField('roles');
552
        $field = $this->crudPanel->fields()['roles'];
553
        $this->assertEquals($field['relation_type'], 'BelongsToMany');
554
    }
555
556
    public function testFieldNamePartiallyMatchesRelationName()
557
    {
558
        $this->crudPanel->setModel(User::class);
559
        $this->crudPanel->addField('articles_id');
560
        $field = $this->crudPanel->fields()['articles_id'];
561
        $this->assertEquals($field['relation_type'], 'HasMany');
562
    }
563
564
    public function testCanGetRelationFieldsBySingleRelation()
565
    {
566
        $this->crudPanel->setModel(User::class);
567
        $this->crudPanel->addField('roles');
568
        $this->crudPanel->addField('accountDetails.nickname');
569
        $relationField = $this->crudPanel->getFieldsWithRelationType('BelongsToMany');
570
        $this->assertCount(1, $relationField);
571
    }
572
573
    public function testCanGetRelationFieldsByArrayOfRelations()
574
    {
575
        $this->crudPanel->setModel(User::class);
576
        $this->crudPanel->addField('roles');
577
        $this->crudPanel->addField('accountDetails.nickname');
578
        // this next field should not be returned because is not a direct relation
579
        $this->crudPanel->addField('accountDetails.article');
580
        $relationField = $this->crudPanel->getFieldsWithRelationType(['BelongsToMany', 'HasOne']);
581
582
        $this->assertCount(2, $relationField);
583
    }
584
}
585