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
12:56
created

testCanGetRelationFieldsBySingleRelation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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

275
        $this->crudPanel->/** @scrutinizer ignore-call */ 
276
                          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...
276
277
        $this->assertEquals(3, count($this->crudPanel->fields()));
278
        $this->assertEquals($this->expectedThreeTextFieldsArray, $this->crudPanel->fields());
279
    }
280
281
    public function testAddFieldsForUpdateForm()
282
    {
283
        $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

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

459
        $this->crudPanel->/** @scrutinizer ignore-call */ 
460
                          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...
460
461
        $this->assertEquals(['field2', 'field1', 'field3'], array_keys($this->crudPanel->fields()));
462
        $this->assertEquals($this->expectedThreeTextFieldsArray, $this->crudPanel->fields());
463
    }
464
465
    public function testOrderFieldsUpdateForm()
466
    {
467
        $this->crudPanel->addFields($this->threeTextFieldsArray);
468
469
        $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

469
        $this->crudPanel->/** @scrutinizer ignore-call */ 
470
                          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...
470
471
        $this->assertEquals($this->expectedThreeTextFieldsArray, $this->crudPanel->fields());
472
        $this->assertEquals(['field2', 'field1', 'field3'], array_keys($this->crudPanel->fields()));
473
    }
474
475
    public function testOrderFieldsIncompleteList()
476
    {
477
        $this->crudPanel->addFields($this->threeTextFieldsArray);
478
479
        $this->crudPanel->orderFields(['field2', 'field3']);
480
481
        $this->assertEquals(['field2', 'field3', 'field1'], array_keys($this->crudPanel->fields()));
482
    }
483
484
    public function testOrderFieldsEmptyList()
485
    {
486
        $this->crudPanel->addFields($this->threeTextFieldsArray);
487
488
        $this->crudPanel->orderFields([]);
489
490
        $this->assertEquals($this->expectedThreeTextFieldsArray, $this->crudPanel->fields());
491
    }
492
493
    public function testOrderFieldsUnknownList()
494
    {
495
        $this->crudPanel->addFields($this->threeTextFieldsArray);
496
497
        $this->crudPanel->orderFields(['field4', 'field5', 'field6']);
498
499
        $this->assertEquals($this->expectedThreeTextFieldsArray, $this->crudPanel->fields());
500
    }
501
502
    public function testOrderColumnsMixedList()
503
    {
504
        $this->crudPanel->addFields($this->threeTextFieldsArray);
505
506
        $this->crudPanel->orderFields(['field2', 'field5', 'field6']);
507
508
        $this->assertEquals(['field2', 'field1', 'field3'], array_keys($this->crudPanel->fields()));
509
    }
510
511
    public function testCheckIfFieldIsFirstOfItsType()
512
    {
513
        $this->crudPanel->addFields($this->multipleFieldTypesArray);
514
515
        $isFirstAddressFieldFirst = $this->crudPanel->checkIfFieldIsFirstOfItsType($this->multipleFieldTypesArray[1]);
516
        $isSecondAddressFieldFirst = $this->crudPanel->checkIfFieldIsFirstOfItsType($this->multipleFieldTypesArray[2]);
517
518
        $this->assertTrue($isFirstAddressFieldFirst);
519
        $this->assertFalse($isSecondAddressFieldFirst);
520
    }
521
522
    public function testCheckIfUnknownFieldIsFirstOfItsType()
523
    {
524
        $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

524
        /** @scrutinizer ignore-call */ 
525
        $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...
525
526
        $this->assertFalse($isUnknownFieldFirst);
527
    }
528
529
    public function testCheckIfInvalidFieldIsFirstOfItsType()
530
    {
531
        $this->expectException(\ErrorException::class);
532
533
        $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

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