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

Test Setup Failed
Pull Request — master (#4161)
by
unknown
15:22
created

testFieldNameDotNotationIsRelationshipUsingFluentSynthax()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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

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

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

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

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

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

534
        $this->crudPanel->/** @scrutinizer ignore-call */ 
535
                          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...
535
    }
536
537
    public function testDecodeJsonCastedAttributes()
538
    {
539
        $this->markTestIncomplete();
540
541
        // TODO: the decode JSON method should not be in fields trait and should not be exposed in the public API.
542
    }
543
544
    public function testFieldNameDotNotationIsRelationship()
545
    {
546
        $this->crudPanel->setModel(User::class);
547
        $this->crudPanel->addField('accountDetails.nickname');
548
        $fieldReadyForHtml = $this->crudPanel->fields()['accountDetails.nickname'];
549
        $fieldCleanState = $this->crudPanel->getCleanStateFields()['accountDetails.nickname'];
550
        $this->assertEquals(Arr::except($fieldReadyForHtml, ['name']), Arr::except($fieldCleanState, ['name']));
551
        $this->assertEquals($fieldCleanState['relation_type'], 'HasOne');
552
        $this->assertEquals($fieldReadyForHtml['name'], 'accountDetails[nickname]');
553
        $this->assertEquals($fieldCleanState['name'], 'accountDetails.nickname');
554
    }
555
556
    public function testFieldNameDotNotationIsRelationshipUsingFluentSynthax()
557
    {
558
        $this->crudPanel->setModel(User::class);
559
        $this->crudPanel->field('accountDetails.nickname')->label('custom label');
560
        $fieldReadyForHtml = $this->crudPanel->fields()['accountDetails.nickname'];
561
        $fieldCleanState = $this->crudPanel->getCleanStateFields()['accountDetails.nickname'];
562
        $this->assertEquals(Arr::except($fieldReadyForHtml, ['name']), Arr::except($fieldCleanState, ['name']));
563
        $this->assertEquals($fieldCleanState['relation_type'], 'HasOne');
564
        $this->assertEquals($fieldReadyForHtml['name'], 'accountDetails[nickname]');
565
        $this->assertEquals($fieldCleanState['name'], 'accountDetails.nickname');
566
    }
567
568
    public function testFieldNameIsRelationInCrudModel()
569
    {
570
        $this->crudPanel->setModel(User::class);
571
        $this->crudPanel->addField('roles');
572
        $field = $this->crudPanel->fields()['roles'];
573
        $this->assertEquals($field['relation_type'], 'BelongsToMany');
574
    }
575
576
    public function testFieldNameIsPartialRelationInCrudModel()
577
    {
578
        $this->crudPanel->setModel(User::class);
579
        $this->crudPanel->addField('articles_id');
580
        $field = $this->crudPanel->fields()['articles_id'];
581
        $this->assertEquals($field['relation_type'], 'HasMany');
582
    }
583
}
584