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 — main (#5640)
by Pedro
24:59 queued 10:03
created

testItDoesNotAttemptToGetEntityWhenColumnNameIsArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Backpack\CRUD\Tests\Unit\CrudPanel;
4
5
use Backpack\CRUD\app\Library\CrudPanel\CrudColumn;
6
use Backpack\CRUD\Tests\config\Models\User;
7
8
/**
9
 * @covers Backpack\CRUD\app\Library\CrudPanel\Traits\Columns
10
 * @covers Backpack\CRUD\app\Library\CrudPanel\Traits\ColumnsProtectedMethods
11
 * @covers Backpack\CRUD\app\Library\CrudPanel\CrudColumn
12
 * @covers Backpack\CRUD\app\Library\CrudPanel\CrudPanel
13
 */
14
class CrudPanelColumnsTest extends \Backpack\CRUD\Tests\config\CrudPanel\BaseCrudPanel
15
{
16
    private $oneColumnArray = [
17
        'name' => 'column1',
18
        'label' => 'Column1',
19
    ];
20
21
    private $expectedOneColumnArray = [
22
        'column1' => [
23
            'label' => 'Column1',
24
            'name' => 'column1',
25
            'key' => 'column1',
26
            'type' => 'text',
27
            'tableColumn' => false,
28
            'orderable' => false,
29
            'searchLogic' => false,
30
            'priority' => 0,
31
        ],
32
    ];
33
34
    private $otherOneColumnArray = [
35
        'name' => 'column4',
36
        'label' => 'Column4',
37
    ];
38
39
    private $twoColumnsArray = [
40
        [
41
            'name' => 'column1',
42
            'label' => 'Column1',
43
        ],
44
        [
45
            'name' => 'column2',
46
            'label' => 'Column2',
47
        ],
48
    ];
49
50
    private $expectedTwoColumnsArray = [
51
        'column1' => [
52
            'name' => 'column1',
53
            'key' => 'column1',
54
            'label' => 'Column1',
55
            'type' => 'text',
56
            'tableColumn' => false,
57
            'orderable' => false,
58
            'searchLogic' => false,
59
            'priority' => 0,
60
61
        ],
62
        'column2' => [
63
            'name' => 'column2',
64
            'key' => 'column2',
65
            'label' => 'Column2',
66
            'type' => 'text',
67
            'tableColumn' => false,
68
            'orderable' => false,
69
            'searchLogic' => false,
70
            'priority' => 1,
71
        ],
72
    ];
73
74
    private $threeColumnsArray = [
75
        [
76
            'name' => 'column1',
77
            'label' => 'Column1',
78
        ],
79
        [
80
            'name' => 'column2',
81
            'label' => 'Column2',
82
        ],
83
        [
84
            'name' => 'column3',
85
            'label' => 'Column3',
86
        ],
87
    ];
88
89
    private $expectedThreeColumnsArray = [
90
        'column1' => [
91
            'name' => 'column1',
92
            'key' => 'column1',
93
            'label' => 'Column1',
94
            'type' => 'text',
95
            'tableColumn' => false,
96
            'orderable' => false,
97
            'searchLogic' => false,
98
            'priority' => 0,
99
        ],
100
        'column2' => [
101
            'name' => 'column2',
102
            'key' => 'column2',
103
            'label' => 'Column2',
104
            'type' => 'text',
105
            'tableColumn' => false,
106
            'orderable' => false,
107
            'searchLogic' => false,
108
            'priority' => 1,
109
        ],
110
        'column3' => [
111
            'name' => 'column3',
112
            'key' => 'column3',
113
            'label' => 'Column3',
114
            'type' => 'text',
115
            'tableColumn' => false,
116
            'orderable' => false,
117
            'searchLogic' => false,
118
            'priority' => 2,
119
        ],
120
    ];
121
122
    private $expectedRelationColumnsArrayWithoutPro = [
123
        'accountDetails' => [
124
            'name' => 'accountDetails',
125
            'label' => 'AccountDetails',
126
            'type' => 'text',
127
            'key' => 'accountDetails',
128
            'priority' => 0,
129
            'tableColumn' => false,
130
            'orderable' => false,
131
            'searchLogic' => false,
132
            'entity' => 'accountDetails',
133
            'model' => 'Backpack\CRUD\Tests\Config\Models\AccountDetails',
134
            'relation_type' => 'HasOne',
135
            'attribute' => 'nickname',
136
        ],
137
        'accountDetails__nickname' => [
138
            'name' => 'accountDetails.nickname',
139
            'label' => 'AccountDetails.nickname',
140
            'type' => 'text',
141
            'key' => 'accountDetails__nickname',
142
            'priority' => 1,
143
            'attribute' => 'nickname',
144
            'tableColumn' => false,
145
            'orderable' => false,
146
            'searchLogic' => false,
147
            'relation_type' => 'HasOne',
148
            'entity' => 'accountDetails.nickname',
149
            'model' => 'Backpack\CRUD\Tests\Config\Models\AccountDetails',
150
        ],
151
        'accountDetails__user' => [
152
            'name' => 'accountDetails.user',
153
            'label' => 'AccountDetails.user',
154
            'type' => 'select',
155
            'key' => 'accountDetails__user',
156
            'priority' => 2,
157
            'tableColumn' => false,
158
            'orderable' => false,
159
            'searchLogic' => false,
160
            'relation_type' => 'BelongsTo',
161
            'entity' => 'accountDetails.user',
162
            'model' => 'Backpack\CRUD\Tests\Config\Models\User',
163
            'attribute' => 'name',
164
        ],
165
    ];
166
167
    private $expectedRelationColumnsArrayWithPro = [
168
        'accountDetails' => [
169
            'name' => 'accountDetails',
170
            'label' => 'AccountDetails',
171
            'type' => 'relationship',
172
            'key' => 'accountDetails',
173
            'priority' => 0,
174
            'tableColumn' => false,
175
            'orderable' => false,
176
            'searchLogic' => false,
177
            'entity' => 'accountDetails',
178
            'model' => 'Backpack\CRUD\Tests\Config\Models\AccountDetails',
179
            'relation_type' => 'HasOne',
180
            'attribute' => 'nickname',
181
        ],
182
        'accountDetails__nickname' => [
183
            'name' => 'accountDetails.nickname',
184
            'label' => 'AccountDetails.nickname',
185
            'type' => 'relationship',
186
            'key' => 'accountDetails__nickname',
187
            'priority' => 1,
188
            'attribute' => 'nickname',
189
            'tableColumn' => false,
190
            'orderable' => false,
191
            'searchLogic' => false,
192
            'relation_type' => 'HasOne',
193
            'entity' => 'accountDetails.nickname',
194
            'model' => 'Backpack\CRUD\Tests\Config\Models\AccountDetails',
195
        ],
196
        'accountDetails__user' => [
197
            'name' => 'accountDetails.user',
198
            'label' => 'AccountDetails.user',
199
            'type' => 'relationship',
200
            'key' => 'accountDetails__user',
201
            'priority' => 2,
202
            'tableColumn' => false,
203
            'orderable' => false,
204
            'searchLogic' => false,
205
            'relation_type' => 'BelongsTo',
206
            'entity' => 'accountDetails.user',
207
            'model' => 'Backpack\CRUD\Tests\Config\Models\User',
208
            'attribute' => 'name',
209
        ],
210
    ];
211
212
    private $relationColumnArray = [
213
        'name' => 'nickname',
214
        'type' => 'select',
215
        'entity' => 'accountDetails',
216
        'attribute' => 'nickname',
217
    ];
218
219
    private $expectedRelationColumnArray = [
220
        'nickname' => [
221
            'name' => 'nickname',
222
            'type' => 'select',
223
            'entity' => 'accountDetails',
224
            'attribute' => 'nickname',
225
            'label' => 'Nickname',
226
            'model' => 'Backpack\CRUD\Tests\Config\Models\AccountDetails',
227
            'key' => 'nickname',
228
            'tableColumn' => false,
229
            'orderable' => false,
230
            'searchLogic' => false,
231
            'priority' => 0,
232
            'relation_type' => 'HasOne',
233
        ],
234
    ];
235
236
    private $nestedRelationColumnArray = [
237
        'name' => 'accountDetails.article',
238
    ];
239
240
    private $secondNestedRelationColumnArray = [
241
        'name' => 'accountDetails.article',
242
        'attribute' => 'content',
243
        'key' => 'ac_article_content',
244
    ];
245
246
    private $expectedNestedRelationColumnArrayWithPro = [
247
        'accountDetails__article' => [
248
            'name' => 'accountDetails.article',
249
            'type' => 'relationship',
250
            'entity' => 'accountDetails.article',
251
            'label' => 'AccountDetails.article',
252
            'model' => 'Backpack\CRUD\Tests\Config\Models\Article',
253
            'key' => 'accountDetails__article',
254
            'tableColumn' => false,
255
            'orderable' => false,
256
            'searchLogic' => false,
257
            'priority' => 0,
258
            'relation_type' => 'BelongsTo',
259
            'attribute' => 'content',
260
        ],
261
        'ac_article_content' => [
262
            'name' => 'accountDetails.article',
263
            'type' => 'relationship',
264
            'entity' => 'accountDetails.article',
265
            'label' => 'AccountDetails.article',
266
            'model' => 'Backpack\CRUD\Tests\Config\Models\Article',
267
            'key' => 'ac_article_content',
268
            'tableColumn' => false,
269
            'orderable' => false,
270
            'searchLogic' => false,
271
            'priority' => 1,
272
            'relation_type' => 'BelongsTo',
273
            'attribute' => 'content',
274
        ],
275
    ];
276
277
    private $expectedNestedRelationColumnArrayWithoutPro = [
278
        'accountDetails__article' => [
279
            'name' => 'accountDetails.article',
280
            'type' => 'select',
281
            'entity' => 'accountDetails.article',
282
            'label' => 'AccountDetails.article',
283
            'model' => 'Backpack\CRUD\Tests\Config\Models\Article',
284
            'key' => 'accountDetails__article',
285
            'tableColumn' => false,
286
            'orderable' => false,
287
            'searchLogic' => false,
288
            'priority' => 0,
289
            'relation_type' => 'BelongsTo',
290
            'attribute' => 'content',
291
        ],
292
        'ac_article_content' => [
293
            'name' => 'accountDetails.article',
294
            'type' => 'select',
295
            'entity' => 'accountDetails.article',
296
            'label' => 'AccountDetails.article',
297
            'model' => 'Backpack\CRUD\Tests\Config\Models\Article',
298
            'key' => 'ac_article_content',
299
            'tableColumn' => false,
300
            'orderable' => false,
301
            'searchLogic' => false,
302
            'priority' => 1,
303
            'relation_type' => 'BelongsTo',
304
            'attribute' => 'content',
305
        ],
306
    ];
307
308
    /**
309
     * Setup the test environment.
310
     *
311
     * @return void
312
     */
313
    protected function setUp(): void
314
    {
315
        parent::setUp();
316
317
        $this->crudPanel->setOperation('list');
318
    }
319
320
    public function testAddColumnByName()
321
    {
322
        $this->crudPanel->addColumn('column1');
323
324
        $this->assertEquals($this->expectedOneColumnArray, $this->crudPanel->columns());
325
    }
326
327
    public function testAddColumnsByName()
328
    {
329
        $this->crudPanel->addColumns(['column1', 'column2']);
330
331
        $this->assertEquals(2, count($this->crudPanel->columns()));
332
        $this->assertEquals($this->expectedTwoColumnsArray, $this->crudPanel->columns());
333
    }
334
335
    public function testAddColumnAsArray()
336
    {
337
        $this->crudPanel->addColumn($this->oneColumnArray);
338
339
        $this->assertEquals($this->expectedOneColumnArray, $this->crudPanel->columns());
340
    }
341
342
    public function testAddColumnsAsArray()
343
    {
344
        $this->crudPanel->addColumns($this->twoColumnsArray);
345
346
        $this->assertEquals(2, count($this->crudPanel->columns()));
347
        $this->assertEquals($this->expectedTwoColumnsArray, $this->crudPanel->columns());
348
    }
349
350
    public function testAddColumnNotArray()
351
    {
352
        $this->expectException(PHP_MAJOR_VERSION == 7 ? \ErrorException::class : \TypeError::class);
353
        // Why? When calling count() on a non-countable entity,
354
        // PHP 7.x will through ErrorException, but PHP 8.x will throw TypeError.
355
356
        $this->crudPanel->addColumns('column1');
0 ignored issues
show
Bug introduced by
'column1' of type string is incompatible with the type array expected by parameter $columns of Backpack\CRUD\app\Librar...CrudPanel::addColumns(). ( Ignorable by Annotation )

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

356
        $this->crudPanel->addColumns(/** @scrutinizer ignore-type */ 'column1');
Loading history...
357
    }
358
359
    public function testAddRelationsByName()
360
    {
361
        $this->crudPanel->setModel(User::class);
362
        $this->crudPanel->addColumn('accountDetails');
363
        $this->crudPanel->addColumn('accountDetails.nickname');
364
        $this->crudPanel->addColumn('accountDetails.user');
365
366
        if (backpack_pro()) {
367
            $this->assertEquals($this->expectedRelationColumnsArrayWithPro, $this->crudPanel->columns());
368
        } else {
369
            $this->assertEquals($this->expectedRelationColumnsArrayWithoutPro, $this->crudPanel->columns());
370
        }
371
    }
372
373
    public function testAddRelationColumn()
374
    {
375
        $this->crudPanel->setModel(User::class);
376
        $this->crudPanel->addColumn($this->relationColumnArray);
377
378
        $this->assertEquals($this->expectedRelationColumnArray, $this->crudPanel->columns());
379
    }
380
381
    public function testAddNestedRelationColumn()
382
    {
383
        $this->crudPanel->setModel(User::class);
384
        $this->crudPanel->addColumn($this->nestedRelationColumnArray);
385
        $this->crudPanel->addColumn($this->secondNestedRelationColumnArray);
386
387
        if (backpack_pro()) {
388
            $this->assertEquals($this->expectedNestedRelationColumnArrayWithPro, $this->crudPanel->columns());
389
        } else {
390
            $this->assertEquals($this->expectedNestedRelationColumnArrayWithoutPro, $this->crudPanel->columns());
391
        }
392
    }
393
394
    public function testMoveColumnBefore()
395
    {
396
        $this->crudPanel->addColumns($this->twoColumnsArray);
397
398
        $this->crudPanel->beforeColumn('column1');
399
400
        $keys = array_keys($this->crudPanel->columns());
401
        $expected = $this->expectedTwoColumnsArray['column2'];
402
        $expected['priority'] = 0;
403
        $this->assertEquals($expected, $this->crudPanel->columns()[$keys[0]]);
404
        $this->assertEquals(['column2', 'column1'], $keys);
405
    }
406
407
    public function testItDoesNotChangeThePriorityIfDeveloperDefinedIt()
408
    {
409
        $this->crudPanel->addColumn('column1');
410
        $this->crudPanel->addColumn(['name' => 'column2', 'priority' => 5])->beforeColumn('column1');
411
        $this->assertEquals(5, $this->crudPanel->firstColumnWhere('name', 'column2')['priority']);
412
    }
413
414
    public function testMoveColumnBeforeUnknownColumnName()
415
    {
416
        $this->crudPanel->addColumns($this->twoColumnsArray);
417
418
        $this->crudPanel->beforeColumn('column3');
419
420
        $this->assertEquals(array_keys($this->expectedTwoColumnsArray), array_keys($this->crudPanel->columns()));
421
    }
422
423
    public function testMoveColumnAfter()
424
    {
425
        $this->crudPanel->addColumns($this->threeColumnsArray);
426
427
        $this->crudPanel->afterColumn('column1');
428
429
        $keys = array_keys($this->crudPanel->columns());
430
        $expected = $this->expectedThreeColumnsArray['column3'];
431
        $expected['priority'] = 1;
432
        $this->assertEquals($expected, $this->crudPanel->columns()[$keys[1]]);
433
        $this->assertEquals(['column1', 'column3', 'column2'], $keys);
434
    }
435
436
    public function testMoveColumnAfterUnknownColumnName()
437
    {
438
        $this->crudPanel->addColumns($this->twoColumnsArray);
439
440
        $this->crudPanel->afterColumn('column3');
441
442
        $this->assertEquals(array_keys($this->expectedTwoColumnsArray), array_keys($this->crudPanel->columns()));
443
    }
444
445
    public function testRemoveColumnByName()
446
    {
447
        $this->crudPanel->addColumns(['column1', 'column2', 'column3']);
448
449
        $this->crudPanel->removeColumn('column1');
450
451
        $this->assertEquals(2, count($this->crudPanel->columns()));
452
        $this->assertEquals(['column2', 'column3'], array_keys($this->crudPanel->columns()));
453
        $this->assertNotContains($this->oneColumnArray, $this->crudPanel->columns());
454
    }
455
456
    public function testItCanRemoveAllColumns()
457
    {
458
        $this->crudPanel->addColumns(['column1', 'column2', 'column3']);
459
460
        $this->crudPanel->removeAllColumns();
461
462
        $this->assertEmpty($this->crudPanel->columns());
463
    }
464
465
    public function testItCanSetColumnDetails()
466
    {
467
        $this->crudPanel->addColumns(['column1', 'column2', 'column3', 'column4', 'column5']);
468
469
        $this->crudPanel->setColumnsDetails(['column1', 'column2'], ['label' => 'New Label']);
470
        $this->crudPanel->setColumnDetails('column3', ['label' => 'Old Label']);
471
        $this->crudPanel->modifyColumn('column4', ['label' => 'Alias Label']);
472
        $this->crudPanel->setColumnLabel('column5', 'Setting Label');
473
474
        $this->assertEquals('New Label', $this->crudPanel->columns()['column1']['label']);
475
        $this->assertEquals('New Label', $this->crudPanel->columns()['column2']['label']);
476
        $this->assertEquals('Old Label', $this->crudPanel->columns()['column3']['label']);
477
        $this->assertEquals('Alias Label', $this->crudPanel->columns()['column4']['label']);
478
        $this->assertEquals('Setting Label', $this->crudPanel->columns()['column5']['label']);
479
    }
480
481
    public function testItCanFindAColumnById()
482
    {
483
        $this->crudPanel->addColumns(['column1', 'column2', 'column3']);
484
485
        $column = $this->crudPanel->findColumnById(1);
486
487
        $this->assertEquals('column2', $column['name']);
488
    }
489
490
    public function testItCanGetAndSetActionsColumnPriority()
491
    {
492
        $this->assertEquals(1, $this->crudPanel->getActionsColumnPriority());
493
        $this->crudPanel->setActionsColumnPriority(2);
494
        $this->assertEquals(2, $this->crudPanel->getActionsColumnPriority());
495
    }
496
497
    public function testItCanGetAndSetColumnsRemovingPreviouslySet()
498
    {
499
        $this->crudPanel->addColumns(['column1', 'column2', 'column3']);
500
501
        $this->crudPanel->setColumns('column4');
502
503
        $this->assertEquals(1, count($this->crudPanel->columns()));
504
        $this->assertEquals(['column4'], array_keys($this->crudPanel->columns()));
505
506
        $this->crudPanel->setColumns(['column5', 'column6']);
507
        $this->assertEquals(2, count($this->crudPanel->columns()));
508
        $this->assertEquals(['column5', 'column6'], array_keys($this->crudPanel->columns()));
509
510
        $this->crudPanel->setColumns(['column7', [
511
            'name' => 'column8',
512
        ]]);
513
        $this->assertEquals(2, count($this->crudPanel->columns()));
514
        $this->assertEquals(['column7', 'column8'], array_keys($this->crudPanel->columns()));
515
    }
516
517
    public function testRemoveUnknownColumnName()
518
    {
519
        $unknownColumnName = 'column4';
520
        $this->crudPanel->addColumns(['column1', 'column2', 'column3']);
521
522
        $this->crudPanel->removeColumn($unknownColumnName);
523
524
        $this->assertEquals(3, count($this->crudPanel->columns()));
525
        $this->assertEquals(['column1', 'column2', 'column3'], array_keys($this->crudPanel->columns()));
526
        $this->assertNotContains($this->otherOneColumnArray, $this->crudPanel->columns());
527
    }
528
529
    public function testRemoveColumnsByName()
530
    {
531
        $this->crudPanel->addColumns(['column1', 'column2', 'column3']);
532
533
        $this->crudPanel->removeColumns($this->twoColumnsArray);
534
535
        $this->assertEquals(1, count($this->crudPanel->columns()));
536
        $this->assertEquals(['column3'], array_keys($this->crudPanel->columns()));
537
        $this->assertNotEquals($this->expectedThreeColumnsArray, $this->crudPanel->columns());
538
    }
539
540
    public function testRemoveUnknownColumnsByName()
541
    {
542
        $unknownColumnNames = ['column4', 'column5'];
543
        $this->crudPanel->addColumns(['column1', 'column2', 'column3']);
544
545
        $this->crudPanel->removeColumns($unknownColumnNames);
546
547
        $this->assertEquals(3, count($this->crudPanel->columns()));
548
        $this->assertEquals(['column1', 'column2', 'column3'], array_keys($this->crudPanel->columns()));
549
        $this->assertNotContains($this->otherOneColumnArray, $this->crudPanel->columns());
550
    }
551
552
    public function testItThrowsAnErrorWhenAttemptingToChangeTheKeyInAColumnWithoutName()
553
    {
554
        $this->expectException(\Exception::class);
555
        $this->crudPanel->addColumn(['type' => 'text'])->key('new_key');
0 ignored issues
show
Bug introduced by
The method key() does not exist on Backpack\CRUD\app\Library\CrudPanel\CrudPanel. 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

555
        $this->crudPanel->addColumn(['type' => 'text'])->/** @scrutinizer ignore-call */ key('new_key');
Loading history...
556
    }
557
558
    public function itCanSetTheUploadAttributeOnColumn()
559
    {
560
        $this->crudPanel->addColumn('column1')->upload();
0 ignored issues
show
Bug introduced by
The method upload() does not exist on Backpack\CRUD\app\Library\CrudPanel\CrudPanel. 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

560
        $this->crudPanel->addColumn('column1')->/** @scrutinizer ignore-call */ upload();
Loading history...
561
562
        $this->assertEquals(true, $this->crudPanel->columns()['column1']['upload']);
563
    }
564
565
    public function testOrderColumns()
566
    {
567
        $this->crudPanel->addColumns($this->threeColumnsArray);
568
569
        $this->crudPanel->orderColumns(['column2', 'column1', 'column3']);
570
571
        $this->assertEquals(['column2', 'column1', 'column3'], array_keys($this->crudPanel->columns()));
572
    }
573
574
    public function testOrderColumnsIncompleteList()
575
    {
576
        $this->crudPanel->addColumns($this->threeColumnsArray);
577
578
        $this->crudPanel->orderColumns(['column2', 'column3']);
579
580
        $this->assertEquals(['column2', 'column3', 'column1'], array_keys($this->crudPanel->columns()));
581
    }
582
583
    public function testOrderColumnsEmptyList()
584
    {
585
        $this->crudPanel->addColumns($this->threeColumnsArray);
586
587
        $this->crudPanel->orderColumns([]);
588
589
        $this->assertEquals($this->expectedThreeColumnsArray, $this->crudPanel->columns());
590
    }
591
592
    public function testOrderColumnsUnknownList()
593
    {
594
        $this->crudPanel->addColumns($this->threeColumnsArray);
595
596
        $this->crudPanel->orderColumns(['column4', 'column5', 'column6']);
597
598
        $this->assertEquals($this->expectedThreeColumnsArray, $this->crudPanel->columns());
599
    }
600
601
    public function testOrderColumnsMixedList()
602
    {
603
        $this->crudPanel->addColumns($this->threeColumnsArray);
604
605
        $this->crudPanel->orderColumns(['column2', 'column5', 'column6']);
606
607
        $this->assertEquals(['column2', 'column1', 'column3'], array_keys($this->crudPanel->columns()));
608
    }
609
610
    public function testItDoesNotAttemptToGetEntityWhenColumnNameIsArray()
611
    {
612
        $this->crudPanel->addColumn(['name' => ['test1', 'test2'], 'label' => 'Column1', 'type' => 'text', 'key' => 'column1']);
613
        $this->assertArrayNotHasKey('entity', $this->crudPanel->firstColumnWhere('key', 'column1'));
0 ignored issues
show
Bug introduced by
$this->crudPanel->firstC...Where('key', 'column1') of type boolean is incompatible with the type ArrayAccess|array expected by parameter $array of PHPUnit\Framework\Assert::assertArrayNotHasKey(). ( Ignorable by Annotation )

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

613
        $this->assertArrayNotHasKey('entity', /** @scrutinizer ignore-type */ $this->crudPanel->firstColumnWhere('key', 'column1'));
Loading history...
614
    }
615
616
    public function testItCanInferTheEntityFromColumnNameUsingEntity_idConvention()
617
    {
618
        $this->crudPanel->addColumn('article_id');
619
620
        $this->assertEquals('article', $this->crudPanel->firstColumnWhere('name', 'article_id')['entity']);
621
    }
622
623
    public function testItAlwaysHasDatabaseColumnWhenDriverIsNotSql()
624
    {
625
        $this->crudPanel = new \Backpack\CRUD\Tests\config\CrudPanel\NoSqlDriverCrudPanel();
626
        $this->crudPanel->setModel(User::class);
627
628
        $this->assertTrue($this->invokeMethod($this->crudPanel, 'hasDatabaseColumn', ['test', 'test']));
629
    }
630
631
    public function testItCanGetTheColumnTypeFromModelCasts()
632
    {
633
        $this->crudPanel->addColumn('arrayCast');
634
        $this->crudPanel->addColumn('jsonCast');
635
        $this->crudPanel->addColumn('dateCast');
636
        $this->crudPanel->addColumn('booleanCast');
637
        $this->crudPanel->addColumn('datetimeCast');
638
        $this->crudPanel->addColumn('numberCast');
639
640
        $this->assertEquals('array', $this->crudPanel->firstColumnWhere('name', 'arrayCast')['type']);
641
        $this->assertEquals('json', $this->crudPanel->firstColumnWhere('name', 'jsonCast')['type']);
642
        $this->assertEquals('date', $this->crudPanel->firstColumnWhere('name', 'dateCast')['type']);
643
        $this->assertEquals('check', $this->crudPanel->firstColumnWhere('name', 'booleanCast')['type']);
644
        $this->assertEquals('datetime', $this->crudPanel->firstColumnWhere('name', 'datetimeCast')['type']);
645
        $this->assertEquals('number', $this->crudPanel->firstColumnWhere('name', 'numberCast')['type']);
646
    }
647
648
    public function testItCanGetTheColumnTypeFromModelDates()
649
    {
650
        $this->crudPanel->addColumn('created_at');
651
652
        $this->assertEquals('datetime', $this->crudPanel->firstColumnWhere('name', 'created_at')['type']);
653
    }
654
655
    public function testMakeFirstColumnReturnFalseWhenNoColumnsExist()
656
    {
657
        $this->assertEmpty($this->crudPanel->columns());
658
        $column = $this->crudPanel->makeFirstColumn();
659
        $this->assertFalse($column);
660
    }
661
662
    public function testItSetsTextColumnTypeForTranslatableColumns()
663
    {
664
        $this->crudPanel->setModel(\Backpack\CRUD\Tests\config\Models\TestModelWithTranslations::class);
665
        $this->crudPanel->addColumn('translatableColumn');
666
667
        $this->assertEquals('text', $this->crudPanel->firstColumnWhere('name', 'translatableColumn')['type']);
668
    }
669
670
    public function testItCanAddADefaultTypeToTheColumn()
671
    {
672
        $column = $this->crudPanel->addDefaultTypeToColumn(['name' => 'name']);
673
674
        $this->assertEquals('text', $column['type']);
675
    }
676
677
    public function testItReturnFalseWhenTryingToAddTypeToAColumnWithoutName()
678
    {
679
        $column = $this->crudPanel->addDefaultTypeToColumn(['attribute' => 'name']);
680
681
        $this->assertFalse($column);
682
    }
683
684
    public function testItCanChangeTheColumnKey()
685
    {
686
        $this->crudPanel->column('test');
687
688
        $this->assertEquals('test', $this->crudPanel->columns()['test']['key']);
689
690
        $this->crudPanel->column('test')->key('new_key');
691
692
        $this->assertEquals('new_key', $this->crudPanel->columns()['new_key']['key']);
693
    }
694
695
    public function testItCanAddAFluentColumn()
696
    {
697
        $this->crudPanel->setModel(User::class);
698
699
        $this->crudPanel->column('my_column')->label('my_column');
700
701
        $this->assertCount(1, $this->crudPanel->columns());
702
703
        $this->assertEquals([
704
            'name' => 'my_column',
705
            'type' => 'text',
706
            'label' => 'my_column',
707
            'key' => 'my_column',
708
            'priority' => 0,
709
            'tableColumn' => false,
710
            'orderable' => false,
711
            'searchLogic' => false,
712
        ], $this->crudPanel->columns()['my_column']);
713
    }
714
715
    public function testItCanMakeAColumnFirstFluently()
716
    {
717
        $this->crudPanel->column('test1');
718
        $this->crudPanel->column('test2')->makeFirst();
719
        $crudColumns = $this->crudPanel->columns();
720
        $firstColumn = reset($crudColumns);
721
        $this->assertEquals($firstColumn['name'], 'test2');
722
    }
723
724
    public function testItCanMakeAColumnLastFluently()
725
    {
726
        $this->crudPanel->column('test1');
727
        $this->crudPanel->column('test2');
728
        $this->crudPanel->column('test1')->makeLast();
729
        $crudColumns = $this->crudPanel->columns();
730
        $firstColumn = reset($crudColumns);
731
        $this->assertEquals($firstColumn['name'], 'test2');
732
    }
733
734
    public function testItCanPlaceColumnsFluently()
735
    {
736
        $this->crudPanel->column('test1');
737
        $this->crudPanel->column('test2');
738
        $this->crudPanel->column('test3')->after('test1');
739
740
        $crudColumnsNames = array_column($this->crudPanel->columns(), 'name');
741
        $this->assertEquals($crudColumnsNames, ['test1', 'test3', 'test2']);
742
743
        $this->crudPanel->column('test4')->before('test1');
744
        $crudColumnsNames = array_column($this->crudPanel->columns(), 'name');
745
        $this->assertEquals($crudColumnsNames, ['test4', 'test1', 'test3', 'test2']);
746
747
        $this->crudPanel->column('test5')->afterColumn('test1');
748
        $crudColumnsNames = array_column($this->crudPanel->columns(), 'name');
749
        $this->assertEquals($crudColumnsNames, ['test4', 'test1', 'test5', 'test3', 'test2']);
750
    }
751
752
    public function testItCanRemoveColumnAttributesFluently()
753
    {
754
        $this->crudPanel->column('test1')->type('test');
755
        $this->assertEquals($this->crudPanel->columns()['test1']['type'], 'test');
756
        $this->crudPanel->column('test1')->forget('type');
757
        $this->assertNull($this->crudPanel->columns()['test1']['type'] ?? null);
758
    }
759
760
    public function testItCanRemoveColumnFluently()
761
    {
762
        $this->crudPanel->column('test1')->type('test');
763
        $this->assertCount(1, $this->crudPanel->columns());
764
        $this->crudPanel->column('test1')->remove();
765
        $this->assertCount(0, $this->crudPanel->columns());
766
    }
767
768
    public function testItCanAddAColumnToCrudFromClass()
769
    {
770
        CrudColumn::name('test');
771
        $this->assertCount(1, $this->crudPanel->columns());
772
    }
773
774
    public function testItCanAddAFluentColumnUsingArray()
775
    {
776
        $this->crudPanel->column($this->oneColumnArray);
777
        $this->assertCount(1, $this->crudPanel->columns());
778
    }
779
780
    public function testItCanAddAFluentColumnUsingArrayWithoutName()
781
    {
782
        $this->crudPanel->column(['type' => 'text']);
783
        $this->assertCount(1, $this->crudPanel->columns());
784
    }
785
786
    public function testColumnLinkToThrowsExceptionWhenNotAllRequiredParametersAreFilled()
787
    {
788
        $this->expectException(\Exception::class);
789
        $this->expectExceptionMessage('Route [article.show.detail] expects parameters [id, detail]. Insufficient parameters provided in column: [articles].');
790
        $this->crudPanel->column('articles')->entity('articles')->linkTo('article.show.detail', ['test' => 'testing']);
0 ignored issues
show
Bug introduced by
The method entity() does not exist on Backpack\CRUD\app\Library\CrudPanel\CrudColumn. 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

790
        $this->crudPanel->column('articles')->/** @scrutinizer ignore-call */ entity('articles')->linkTo('article.show.detail', ['test' => 'testing']);
Loading history...
791
    }
792
793
    public function testItThrowsExceptionIfRouteNotFound()
794
    {
795
        $this->expectException(\Exception::class);
796
        $this->expectExceptionMessage('Route [users.route.doesnt.exist] not found while building the link for column [id].');
797
798
        CrudColumn::name('id')->linkTo('users.route.doesnt.exist')->toArray();
0 ignored issues
show
Bug introduced by
The method toArray() does not exist on Backpack\CRUD\app\Library\CrudPanel\CrudColumn. 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

798
        CrudColumn::name('id')->linkTo('users.route.doesnt.exist')->/** @scrutinizer ignore-call */ toArray();
Loading history...
799
    }
800
801
    public function testColumnLinkToWithRouteNameOnly()
802
    {
803
        $this->crudPanel->column('articles')->entity('articles')->linkTo('articles.show');
804
        $columnArray = $this->crudPanel->columns()['articles'];
805
        $reflection = new \ReflectionFunction($columnArray['wrapper']['href']);
806
        $arguments = $reflection->getClosureUsedVariables();
0 ignored issues
show
Bug introduced by
The method getClosureUsedVariables() does not exist on ReflectionFunction. Did you maybe mean getClosure()? ( Ignorable by Annotation )

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

806
        /** @scrutinizer ignore-call */ 
807
        $arguments = $reflection->getClosureUsedVariables();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
807
        $this->crudPanel->entry = $this->makeAnArticleModel();
808
        $url = $columnArray['wrapper']['href']($this->crudPanel, $columnArray, $this->crudPanel->entry, 1);
809
        $this->assertEquals('articles.show', $arguments['route']);
810
        $this->assertCount(1, $arguments['parameters']);
811
        $this->assertEquals('http://localhost/admin/articles/1/show', $url);
812
    }
813
814
    public function testColumnLinkToWithRouteNameAndAdditionalParameters()
815
    {
816
        $this->crudPanel->column('articles')->entity('articles')->linkTo('articles.show', ['test' => 'testing', 'test2' => 'testing2']);
817
        $columnArray = $this->crudPanel->columns()['articles'];
818
        $reflection = new \ReflectionFunction($columnArray['wrapper']['href']);
819
        $arguments = $reflection->getClosureUsedVariables();
820
        $this->assertEquals('articles.show', $arguments['route']);
821
        $this->assertCount(3, $arguments['parameters']);
822
        $this->crudPanel->entry = $this->makeAnArticleModel();
823
        $url = $columnArray['wrapper']['href']($this->crudPanel, $columnArray, $this->crudPanel->entry, 1);
824
        $this->assertEquals('http://localhost/admin/articles/1/show?test=testing&test2=testing2', $url);
825
    }
826
827
    public function testColumnLinkToWithCustomParameters()
828
    {
829
        $this->crudPanel->column('articles')->entity('articles')->linkTo('article.show.detail', ['detail' => 'testing', 'otherParam' => 'test']);
830
        $columnArray = $this->crudPanel->columns()['articles'];
831
        $this->crudPanel->entry = $this->makeAnArticleModel();
832
        $url = $columnArray['wrapper']['href']($this->crudPanel, $columnArray, $this->crudPanel->entry, 1);
833
        $this->assertEquals('http://localhost/admin/articles/1/show/testing?otherParam=test', $url);
834
    }
835
836
    public function testColumnLinkToWithCustomClosureParameters()
837
    {
838
        $this->crudPanel->column('articles')
839
                        ->entity('articles')
840
                        ->linkTo('article.show.detail', ['detail' => fn ($entry, $related_key) => $related_key, 'otherParam' => fn ($entry) => $entry->content]);
841
        $columnArray = $this->crudPanel->columns()['articles'];
842
        $this->crudPanel->entry = $this->makeAnArticleModel();
843
        $url = $columnArray['wrapper']['href']($this->crudPanel, $columnArray, $this->crudPanel->entry, 1);
844
        $this->assertEquals('http://localhost/admin/articles/1/show/1?otherParam=Some%20Content', $url);
845
    }
846
847
    public function testColumnLinkToDontAutoInferParametersIfAllProvided()
848
    {
849
        $this->crudPanel->column('articles')
850
                        ->entity('articles')
851
                        ->linkTo('article.show.detail', ['id' => 123, 'detail' => fn ($entry, $related_key) => $related_key, 'otherParam' => fn ($entry) => $entry->content]);
852
        $columnArray = $this->crudPanel->columns()['articles'];
853
        $this->crudPanel->entry = $this->makeAnArticleModel();
854
        $url = $columnArray['wrapper']['href']($this->crudPanel, $columnArray, $this->crudPanel->entry, 1);
855
        $this->assertEquals('http://localhost/admin/articles/123/show/1?otherParam=Some%20Content', $url);
856
    }
857
858
    public function testColumnLinkToAutoInferAnySingleParameter()
859
    {
860
        $this->crudPanel->column('articles')
861
                        ->entity('articles')
862
                        ->linkTo('article.show.detail', ['id' => 123, 'otherParam' => fn ($entry) => $entry->content]);
863
        $columnArray = $this->crudPanel->columns()['articles'];
864
        $this->crudPanel->entry = $this->makeAnArticleModel();
865
        $url = $columnArray['wrapper']['href']($this->crudPanel, $columnArray, $this->crudPanel->entry, 1);
866
        $this->assertEquals('http://localhost/admin/articles/123/show/1?otherParam=Some%20Content', $url);
867
    }
868
869
    public function testColumnLinkToWithClosure()
870
    {
871
        $this->crudPanel->column('articles')
872
                        ->entity('articles')
873
                        ->linkTo(fn ($entry) => route('articles.show', $entry->content));
0 ignored issues
show
Bug introduced by
function(...) { /* ... */ } of type callable is incompatible with the type string expected by parameter $routeName of Backpack\CRUD\app\Librar...el\CrudColumn::linkTo(). ( Ignorable by Annotation )

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

873
                        ->linkTo(/** @scrutinizer ignore-type */ fn ($entry) => route('articles.show', $entry->content));
Loading history...
874
        $columnArray = $this->crudPanel->columns()['articles'];
875
        $this->crudPanel->entry = $this->makeAnArticleModel();
876
        $url = $columnArray['wrapper']['href']($this->crudPanel, $columnArray, $this->crudPanel->entry, 1);
877
        $this->assertEquals('http://localhost/admin/articles/Some%20Content/show', $url);
878
    }
879
880
    public function testColumnArrayDefinitionLinkToRouteAsClosure()
881
    {
882
        $this->crudPanel->setModel(User::class);
883
        $this->crudPanel->column([
884
            'name' => 'articles',
885
            'entity' => 'articles',
886
            'linkTo' => fn ($entry) => route('articles.show', ['id' => $entry->id, 'test' => 'testing']),
887
        ]);
888
        $columnArray = $this->crudPanel->columns()['articles'];
889
        $this->crudPanel->entry = $this->makeAnArticleModel();
890
        $url = $columnArray['wrapper']['href']($this->crudPanel, $columnArray, $this->crudPanel->entry, 1);
891
        $this->assertEquals('http://localhost/admin/articles/1/show?test=testing', $url);
892
    }
893
894
    public function testColumnArrayDefinitionLinkToRouteNameOnly()
895
    {
896
        $this->crudPanel->setModel(User::class);
897
        $this->crudPanel->column([
898
            'name' => 'articles',
899
            'entity' => 'articles',
900
            'linkTo' => 'articles.show',
901
        ]);
902
        $columnArray = $this->crudPanel->columns()['articles'];
903
        $this->crudPanel->entry = $this->makeAnArticleModel();
904
        $url = $columnArray['wrapper']['href']($this->crudPanel, $columnArray, $this->crudPanel->entry, 1);
905
        $this->assertEquals('http://localhost/admin/articles/1/show', $url);
906
    }
907
908
    public function testColumnArrayDefinitionLinkToRouteNameAndAdditionalParameters()
909
    {
910
        $this->crudPanel->setModel(User::class);
911
        $this->crudPanel->column([
912
            'name' => 'articles',
913
            'entity' => 'articles',
914
            'linkTo' => [
915
                'route' => 'articles.show',
916
                'parameters' => [
917
                    'test' => 'testing',
918
                    'test2' => fn ($entry) => $entry->content,
919
                ],
920
            ],
921
        ]);
922
        $columnArray = $this->crudPanel->columns()['articles'];
923
        $reflection = new \ReflectionFunction($columnArray['wrapper']['href']);
924
        $arguments = $reflection->getClosureUsedVariables();
925
        $this->assertEquals('articles.show', $arguments['route']);
926
        $this->assertCount(3, $arguments['parameters']);
927
        $this->crudPanel->entry = $this->makeAnArticleModel();
928
        $url = $columnArray['wrapper']['href']($this->crudPanel, $columnArray, $this->crudPanel->entry, 1);
929
        $this->assertEquals('http://localhost/admin/articles/1/show?test=testing&test2=Some%20Content', $url);
930
    }
931
932
    public function testItCanInferFieldAttributesFromADynamicRelation()
933
    {
934
        User::resolveRelationUsing('dynamicRelation', function ($user) {
935
            return $user->belongsTo(\Backpack\CRUD\Tests\config\Models\Bang::class);
936
        });
937
938
        $this->crudPanel->setModel(User::class);
939
        $this->crudPanel->addColumn('dynamicRelation');
940
941
        $column = $this->crudPanel->columns()['dynamicRelation'];
942
943
        $this->assertEquals('dynamicRelation', $column['name']);
944
        $this->assertEquals('name', $column['attribute']);
945
        $this->assertEquals('relationship', $column['type']);
946
        $this->assertEquals('BelongsTo', $column['relation_type']);
947
    }
948
}
949