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 (#3332)
by
unknown
15:52
created

testGetRelatedEntriesAttributesFromHasMany()   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\Article;
6
use Backpack\CRUD\Tests\Unit\Models\Role;
7
use Backpack\CRUD\Tests\Unit\Models\User;
8
use Illuminate\Database\Eloquent\ModelNotFoundException;
9
use Illuminate\Support\Collection;
10
use Illuminate\Support\Facades\DB;
11
use Symfony\Component\HttpKernel\Exception\HttpException;
12
13
class CrudPanelReadTest extends BaseDBCrudPanelTest
14
{
15
    private $relationshipColumn = [
16
        'name'      => 'user_id',
17
        'type'      => 'select',
18
        'entity'    => 'user',
19
        'attribute' => 'name',
20
    ];
21
22
    private $relationshipMultipleColumn = [
0 ignored issues
show
introduced by
The private property $relationshipMultipleColumn is not used, and could be removed.
Loading history...
23
        'name'      => 'roles',
24
        'type'      => 'select',
25
        'entity'    => 'roles',
26
        'attribute' => 'name',
27
        'model' => Role::class,
28
    ];
29
30
    private $nonRelationshipColumn = [
31
        'name'  => 'field1',
32
        'label' => 'Field1',
33
    ];
34
35
    private $articleFieldsArray = [
36
        [
37
            'name'  => 'content',
38
            'label' => 'The Content',
39
            'type'  => 'text',
40
        ],
41
        [
42
            'name'  => 'metas',
43
            'label' => 'Metas',
44
        ],
45
        [
46
            'name' => 'tags',
47
        ],
48
        [
49
            'name' => 'extras',
50
        ],
51
    ];
52
53
    private $expectedCreateFormArticleFieldsArray = [
54
        'content' => [
55
            'name'  => 'content',
56
            'label' => 'The Content',
57
            'type'  => 'text',
58
        ],
59
        'metas' => [
60
            'name'  => 'metas',
61
            'label' => 'Metas',
62
            'type'  => 'text',
63
        ],
64
        'tags' => [
65
            'name'  => 'tags',
66
            'label' => 'Tags',
67
            'type'  => 'text',
68
        ],
69
        'extras' => [
70
            'name'  => 'extras',
71
            'label' => 'Extras',
72
            'type'  => 'text',
73
        ],
74
    ];
75
76
    private $expectedUpdateFormArticleFieldsArray = [
77
        'content' => [
78
            'name'  => 'content',
79
            'label' => 'The Content',
80
            'type'  => 'text',
81
            'value' => 'Some Content',
82
        ],
83
        'metas' => [
84
            'name'  => 'metas',
85
            'label' => 'Metas',
86
            'type'  => 'text',
87
            'value' => '{"meta_title":"Meta Title Value","meta_description":"Meta Description Value"}',
88
        ],
89
        'tags' => [
90
            'name'  => 'tags',
91
            'label' => 'Tags',
92
            'type'  => 'text',
93
            'value' => '{"tags":["tag1","tag2","tag3"]}',
94
        ],
95
        'extras' => [
96
            'name'  => 'extras',
97
            'label' => 'Extras',
98
            'type'  => 'text',
99
            'value' => '{"extra_details":["detail1","detail2","detail3"]}',
100
        ],
101
        'id' => [
102
            'name'  => 'id',
103
            'type'  => 'hidden',
104
            'value' => 1,
105
        ],
106
    ];
107
108
    private $uploadField = [
109
        'name'   => 'image',
110
        'label'  => 'Image',
111
        'type'   => 'upload',
112
        'upload' => true,
113
    ];
114
115
    private $multipleUploadField = [
116
        'name'   => 'photos',
117
        'label'  => 'Photos',
118
        'type'   => 'upload_multiple',
119
        'upload' => true,
120
    ];
121
122
    private $defaultPaginator = [[10, 20, 30], ['t1', 't2', 't3']];
123
124
    public function testGetEntry()
125
    {
126
        $this->crudPanel->setModel(User::class);
127
        $user = User::find(1);
128
129
        $entry = $this->crudPanel->getEntry($user->id);
130
131
        $this->assertEquals($user, $entry);
132
    }
133
134
    public function testGetEntryWithFakes()
135
    {
136
        $this->markTestIncomplete('Not correctly implemented');
137
138
        $this->crudPanel->setModel(Article::class);
139
        $article = Article::find(1);
140
141
        $entry = $this->crudPanel->getEntry($article->id);
142
143
        // TODO: the withFakes call is needed for this to work. the state of the model should not be changed by the
144
        //       getEntry method. the transformation of the fakes columns should be kept in a different crud panel
145
        //       attribute or, at most, the getEntry method should be renamed.
146
        $article->withFakes();
147
148
        $this->assertEquals($article, $entry);
149
    }
150
151
    public function testGetEntryExists()
152
    {
153
        $this->crudPanel->setModel(User::class);
154
        $userEntry = $this->crudPanel->getEntry(1);
155
156
        $this->assertInstanceOf(User::class, $userEntry);
157
158
        $this->crudPanel->setModel(Article::class);
159
        $articleEntry = $this->crudPanel->getEntry(1);
160
161
        $this->assertInstanceOf(Article::class, $articleEntry);
162
    }
163
164
    public function testGetEntryUnknownId()
165
    {
166
        $this->expectException(ModelNotFoundException::class);
167
168
        $this->crudPanel->setModel(User::class);
169
170
        $unknownId = DB::getPdo()->lastInsertId() + 2;
171
        $this->crudPanel->getEntry($unknownId);
172
    }
173
174
    public function testAutoEagerLoadRelationshipColumns()
175
    {
176
        $this->crudPanel->setModel(Article::class);
177
        $this->crudPanel->setOperation('list');
178
        $this->crudPanel->addColumn($this->relationshipColumn);
179
180
        $this->crudPanel->autoEagerLoadRelationshipColumns();
181
182
        $this->assertArrayHasKey('user', $this->crudPanel->query->getEagerLoads());
183
    }
184
185
    public function testAutoEagerLoadRelationshipColumnsNoRelationships()
186
    {
187
        $this->crudPanel->setModel(Article::class);
188
        $this->crudPanel->addColumn($this->nonRelationshipColumn);
189
190
        $this->crudPanel->autoEagerLoadRelationshipColumns();
191
192
        $this->assertEmpty($this->crudPanel->query->getEagerLoads());
193
    }
194
195
    public function testGetEntries()
196
    {
197
        $this->crudPanel->setModel(User::class);
198
199
        $entries = $this->crudPanel->getEntries();
200
201
        $this->assertInstanceOf(Collection::class, $entries);
202
        $this->assertEquals(2, $entries->count());
203
        $this->assertEquals(User::find(1), $entries->first());
204
    }
205
206
    public function testGetEntriesWithFakes()
207
    {
208
        $this->markTestIncomplete('Not correctly implemented');
209
210
        $this->crudPanel->setModel(Article::class);
211
212
        $entries = $this->crudPanel->getEntries();
213
214
        // TODO: the getEntries method automatically adds fakes. the state of the models should not be changed by the
215
        //       getEntries method. at most, the getEntries method should be renamed.
216
        $this->assertInstanceOf(Collection::class, $entries);
217
        $this->assertEquals(1, $entries->count());
218
        $this->assertEquals(Article::find(1), $entries->first());
219
    }
220
221
    public function testGetFieldsCreateForm()
222
    {
223
        $this->crudPanel->addFields($this->articleFieldsArray);
224
225
        // TODO: update method documentation. the $form parameter does not default to 'both'.
226
        $fields = $this->crudPanel->getFields('create');
0 ignored issues
show
Unused Code introduced by
The call to Backpack\CRUD\app\Librar...\CrudPanel::getFields() 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

226
        /** @scrutinizer ignore-call */ 
227
        $fields = $this->crudPanel->getFields('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...
227
228
        $this->assertEquals($this->expectedCreateFormArticleFieldsArray, $fields);
229
    }
230
231
    public function testGetFieldsUpdateForm()
232
    {
233
        $this->crudPanel->setModel(Article::class);
234
235
        $this->crudPanel->setOperation('update');
236
        $this->crudPanel->addFields($this->articleFieldsArray);
237
238
        // TODO: update method documentation. the $form parameter does not default to 'both'.
239
        $fields = $this->crudPanel->getUpdateFields(1);
240
241
        $this->assertEquals($this->expectedUpdateFormArticleFieldsArray, $fields);
242
    }
243
244
    public function testHasUploadFieldsCreateForm()
245
    {
246
        $this->crudPanel->addField($this->uploadField, 'create');
0 ignored issues
show
Unused Code introduced by
The call to Backpack\CRUD\app\Librar...l\CrudPanel::addField() 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

246
        $this->crudPanel->/** @scrutinizer ignore-call */ 
247
                          addField($this->uploadField, '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...
247
248
        // TODO: update method documentation. the $form parameter does not default to 'both'.
249
        $hasUploadFields = $this->crudPanel->hasUploadFields('create');
0 ignored issues
show
Unused Code introduced by
The call to Backpack\CRUD\app\Librar...anel::hasUploadFields() 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

249
        /** @scrutinizer ignore-call */ 
250
        $hasUploadFields = $this->crudPanel->hasUploadFields('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...
250
251
        $this->assertTrue($hasUploadFields);
252
    }
253
254
    public function testHasMultipleUploadFieldsCreateForm()
255
    {
256
        $this->crudPanel->addField($this->multipleUploadField, 'create');
0 ignored issues
show
Unused Code introduced by
The call to Backpack\CRUD\app\Librar...l\CrudPanel::addField() 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

256
        $this->crudPanel->/** @scrutinizer ignore-call */ 
257
                          addField($this->multipleUploadField, '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...
257
258
        // TODO: update method documentation. the $form parameter does not default to 'both'.
259
        $hasMultipleUploadFields = $this->crudPanel->hasUploadFields('create');
0 ignored issues
show
Unused Code introduced by
The call to Backpack\CRUD\app\Librar...anel::hasUploadFields() 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

259
        /** @scrutinizer ignore-call */ 
260
        $hasMultipleUploadFields = $this->crudPanel->hasUploadFields('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...
260
261
        $this->assertTrue($hasMultipleUploadFields);
262
    }
263
264
    public function testHasUploadFieldsUpdateForm()
265
    {
266
        $this->crudPanel->setModel(Article::class);
267
        $this->crudPanel->addField($this->uploadField, 'update');
0 ignored issues
show
Unused Code introduced by
The call to Backpack\CRUD\app\Librar...l\CrudPanel::addField() 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

267
        $this->crudPanel->/** @scrutinizer ignore-call */ 
268
                          addField($this->uploadField, '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...
268
269
        // TODO: update method documentation. the $form parameter does not default to 'both'.
270
        $hasUploadFields = $this->crudPanel->hasUploadFields('update', 1);
0 ignored issues
show
Unused Code introduced by
The call to Backpack\CRUD\app\Librar...anel::hasUploadFields() 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

270
        /** @scrutinizer ignore-call */ 
271
        $hasUploadFields = $this->crudPanel->hasUploadFields('update', 1);

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...
271
272
        $this->assertTrue($hasUploadFields);
273
    }
274
275
    public function testEnableDetailsRow()
276
    {
277
        $this->crudPanel->setOperation('create');
278
        $this->crudPanel->enableDetailsRow();
279
280
        $this->assertTrue($this->crudPanel->getOperationSetting('detailsRow'));
281
    }
282
283
    public function testDisableDetailsRow()
284
    {
285
        $this->crudPanel->setOperation('list');
286
        $this->crudPanel->disableDetailsRow();
287
288
        $this->assertFalse($this->crudPanel->get('list.detailsRow'));
289
    }
290
291
    public function testSetDefaultPageLength()
292
    {
293
        $pageLength = 20;
294
        $this->crudPanel->setDefaultPageLength($pageLength);
295
296
        $this->assertEquals($pageLength, $this->crudPanel->getDefaultPageLength());
297
    }
298
299
    public function testGetDefaultPageLength()
300
    {
301
        $defaultPageLength = $this->crudPanel->getDefaultPageLength();
302
303
        $this->assertEquals(25, $defaultPageLength);
304
    }
305
306
    public function testEnableExportButtons()
307
    {
308
        $this->crudPanel->enableExportButtons();
309
310
        $this->assertTrue($this->crudPanel->exportButtons());
311
    }
312
313
    public function testGetExportButtons()
314
    {
315
        $exportButtons = $this->crudPanel->exportButtons();
316
317
        $this->assertFalse($exportButtons);
318
    }
319
320
    public function testGetRelatedEntriesAttributesFromBelongsToMany()
321
    {
322
        $this->crudPanel->setModel(User::class);
323
        $this->crudPanel->setOperation('list');
324
        $user = $this->crudPanel->getModel()->where('id', 2)->first();
325
        $entries = $this->crudPanel->getRelatedEntriesAttributes($user, 'roles', 'name');
326
        $this->assertEquals([1 => 'admin', 2 => 'user'], $entries);
327
    }
328
329
    public function testGetRelatedEntriesAttributesFromBelongsToManyWithAcessor()
330
    {
331
        $this->crudPanel->setModel(User::class);
332
        $this->crudPanel->setOperation('list');
333
        $user = $this->crudPanel->getModel()->where('id', 2)->first();
334
        $entries = $this->crudPanel->getRelatedEntriesAttributes($user, 'roles', 'role_name');
335
        $this->assertEquals([1 => 'admin++', 2 => 'user++'], $entries);
336
    }
337
338
    public function testGetRelatedEntriesAttributesFromHasMany()
339
    {
340
        $this->crudPanel->setModel(User::class);
341
        $this->crudPanel->setOperation('list');
342
        $user = $this->crudPanel->getModel()->first();
343
        $entries = $this->crudPanel->getRelatedEntriesAttributes($user, 'articles', 'content');
344
        $this->assertCount(1, $entries);
345
    }
346
347
    public function testGetRelatedEntriesAttributesFromHasManyWithAcessor()
348
    {
349
        $this->crudPanel->setModel(User::class);
350
        $this->crudPanel->setOperation('list');
351
        $user = $this->crudPanel->getModel()->first();
352
        $entries = $this->crudPanel->getRelatedEntriesAttributes($user, 'articles', 'content_composed');
353
        $this->assertCount(1, $entries);
354
    }
355
356
    public function testGetRelatedEntriesAttributesFromBelongsTo()
357
    {
358
        $this->crudPanel->setModel(Article::class);
359
        $this->crudPanel->setOperation('list');
360
        $article = $this->crudPanel->getModel()->first();
361
        $entries = $this->crudPanel->getRelatedEntriesAttributes($article, 'user', 'name');
362
        $this->assertCount(1, $entries);
363
    }
364
365
    public function testGetRelatedEntriesAttributesFromBelongsToWithAcessor()
366
    {
367
        $this->crudPanel->setModel(Article::class);
368
        $this->crudPanel->setOperation('list');
369
        $article = $this->crudPanel->getModel()->first();
370
        $entries = $this->crudPanel->getRelatedEntriesAttributes($article, 'user', 'name_composed');
371
        $this->assertCount(1, $entries);
372
    }
373
374
    public function testGetRelatedEntriesAttributesFromHasOne()
375
    {
376
        $this->crudPanel->setModel(User::class);
377
        $this->crudPanel->setOperation('list');
378
        $user = $this->crudPanel->getModel()->first();
379
        $entries = $this->crudPanel->getRelatedEntriesAttributes($user, 'accountDetails', 'nickname');
380
        $this->assertCount(1, $entries);
381
    }
382
383
    public function testGetRelatedEntriesAttributesFromHasOneWithAcessor()
384
    {
385
        $this->crudPanel->setModel(User::class);
386
        $this->crudPanel->setOperation('list');
387
        $user = $this->crudPanel->getModel()->first();
388
        $entries = $this->crudPanel->getRelatedEntriesAttributes($user, 'accountDetails', 'nickname_composed');
389
        $this->assertCount(1, $entries);
390
    }
391
392
    /**
393
     * Tests define paginator length with single array [20, 30, 40].
394
     *
395
     */
396
    public function testCrudPanelChangePaginatorLengthSingleArrayNoLabels()
397
    {
398
        $this->crudPanel->setModel(User::class);
399
        $this->crudPanel->setOperation('list');
400
        $this->crudPanel->setPageLengthMenu([20, 30, 40]);
401
        $this->assertCount(2, $this->crudPanel->getOperationSetting('pageLengthMenu'));
402
        $this->assertTrue(in_array(40, $this->crudPanel->getOperationSetting('pageLengthMenu')[0]));
403
        $this->assertTrue(in_array(40, $this->crudPanel->getOperationSetting('pageLengthMenu')[1]));
404
    }
405
406
    /**
407
     * Tests define paginator length with single array [20 => 'v', 30 => 't', 40 => 'q'].
408
     *
409
     */
410
    public function testCrudPanelChangePaginatorLengthSingleArrayWithLabels()
411
    {
412
        $this->crudPanel->setModel(User::class);
413
        $this->crudPanel->setOperation('list');
414
        $this->crudPanel->setPageLengthMenu([20 => 'v', 30 => 't', 40 => 'q']);
415
        $this->assertCount(2, $this->crudPanel->getOperationSetting('pageLengthMenu'));
416
        $this->assertTrue(in_array(40, $this->crudPanel->getOperationSetting('pageLengthMenu')[0]));
417
        $this->assertEquals($this->crudPanel->getOperationSetting('pageLengthMenu')[1], ['v', 't', 'q']);
418
    }
419
420
    /**
421
     * Tests define paginator length with and 'all' options as -1 as defined in previous versions of BP
422
     *
423
     */
424
    public function testCrudPanelPaginatorWithAllAsOption()
425
    {
426
        $this->crudPanel->setModel(User::class);
427
        $this->crudPanel->setOperation('list');
428
        $this->crudPanel->setPageLengthMenu([-1 => 'All']);
429
        $this->assertCount(2, $this->crudPanel->getOperationSetting('pageLengthMenu'));
430
        $this->assertTrue(in_array(-1, $this->crudPanel->getOperationSetting('pageLengthMenu')[0]));
431
432
        $this->crudPanel->setPageLengthMenu([-1 , 1]);
433
        $this->assertCount(2, $this->crudPanel->getOperationSetting('pageLengthMenu'));
434
        $this->assertTrue(in_array(-1, $this->crudPanel->getOperationSetting('pageLengthMenu')[0]));
435
436
        $this->crudPanel->setPageLengthMenu(-1);
0 ignored issues
show
Bug introduced by
-1 of type integer is incompatible with the type array expected by parameter $menu of Backpack\CRUD\app\Librar...el::setPageLengthMenu(). ( Ignorable by Annotation )

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

436
        $this->crudPanel->setPageLengthMenu(/** @scrutinizer ignore-type */ -1);
Loading history...
437
        $this->assertCount(2, $this->crudPanel->getOperationSetting('pageLengthMenu'));
438
        $this->assertTrue(in_array(-1, $this->crudPanel->getOperationSetting('pageLengthMenu')[0]));
439
    }
440
441
     /**
442
     * Tests if paginator aborts when 0 is provided as key.
443
     *
444
     */
445
    public function testCrudPanelPaginatorWithZeroAsOption()
446
    {
447
        $this->crudPanel->setModel(User::class);
448
        $this->crudPanel->setOperation('list');
449
450
        try {
451
            $this->crudPanel->setPageLengthMenu([0 => 'v', 30 => 't', 40 => 'q']);
452
        } catch (\Throwable $a) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
453
        }
454
455
        $this->assertEquals(500, $a->getStatusCode());
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $a does not seem to be defined for all execution paths leading up to this point.
Loading history...
456
457
        try {
458
            $this->crudPanel->setPageLengthMenu([0, 1]);
459
        } catch (\Throwable $b) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
460
        }
461
462
        $this->assertEquals(500, $b->getStatusCode());
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $b does not seem to be defined for all execution paths leading up to this point.
Loading history...
463
464
        try {
465
            $this->crudPanel->setPageLengthMenu(0);
0 ignored issues
show
Bug introduced by
0 of type integer is incompatible with the type array expected by parameter $menu of Backpack\CRUD\app\Librar...el::setPageLengthMenu(). ( Ignorable by Annotation )

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

465
            $this->crudPanel->setPageLengthMenu(/** @scrutinizer ignore-type */ 0);
Loading history...
466
        } catch (\Throwable $c) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
467
        }
468
469
        $this->assertEquals(500, $c->getStatusCode());
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $c does not seem to be defined for all execution paths leading up to this point.
Loading history...
470
471
        try {
472
            $this->crudPanel->setPageLengthMenu([[0, 1]]);
473
        } catch (\Throwable $d) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
474
        }
475
476
        $this->assertEquals(500, $d->getStatusCode());
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $d does not seem to be defined for all execution paths leading up to this point.
Loading history...
477
    }
478
479
    /**
480
     * Tests define paginator length with multi array [[20, 30, 40],['v', 't', 'q']].
481
     *
482
     */
483
    public function testCrudPanelChangePaginatorLengthMultiArrayWithLabels()
484
    {
485
        $this->crudPanel->setModel(User::class);
486
        $this->crudPanel->setOperation('list');
487
        $this->crudPanel->setPageLengthMenu([[20, 30, 40], ['v', 't', 'q']]);
488
        $this->assertCount(2, $this->crudPanel->getOperationSetting('pageLengthMenu'));
489
        $this->assertTrue(in_array(40, $this->crudPanel->getOperationSetting('pageLengthMenu')[0]));
490
        $this->assertEquals($this->crudPanel->getOperationSetting('pageLengthMenu')[1], ['v', 't', 'q']);
491
    }
492
493
    /**
494
     * Tests define paginator length with multi array [[20, 30, 40]].
495
     *
496
     */
497
    public function testCrudPanelChangePaginatorLengthMultiArrayNoLabels()
498
    {
499
        $this->crudPanel->setModel(User::class);
500
        $this->crudPanel->setOperation('list');
501
        $this->crudPanel->setPageLengthMenu([[20, 30, 40]]);
502
        $this->assertCount(2, $this->crudPanel->getOperationSetting('pageLengthMenu'));
503
        $this->assertTrue(in_array(40, $this->crudPanel->getOperationSetting('pageLengthMenu')[0]));
504
        $this->assertTrue(in_array(40, $this->crudPanel->getOperationSetting('pageLengthMenu')[1]));
505
    }
506
507
    /**
508
     * Tests define paginator length with single value 40.
509
     *
510
     */
511
    public function testCrudPanelChangePaginatorLengthWithSingleValue()
512
    {
513
        $this->crudPanel->setModel(User::class);
514
        $this->crudPanel->setOperation('list');
515
        $this->crudPanel->setPageLengthMenu(40);
0 ignored issues
show
Bug introduced by
40 of type integer is incompatible with the type array expected by parameter $menu of Backpack\CRUD\app\Librar...el::setPageLengthMenu(). ( Ignorable by Annotation )

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

515
        $this->crudPanel->setPageLengthMenu(/** @scrutinizer ignore-type */ 40);
Loading history...
516
        $this->assertCount(2, $this->crudPanel->getOperationSetting('pageLengthMenu'));
517
        $this->assertTrue(in_array(40, $this->crudPanel->getOperationSetting('pageLengthMenu')[0]));
518
        $this->assertTrue(in_array(40, $this->crudPanel->getOperationSetting('pageLengthMenu')[1]));
519
    }
520
521
    /**
522
     * Tests if table paginator adds default option non-existent at time in the paginator.
523
     *
524
     */
525
    public function testCrudPanelPaginatorAddsDefaultOptionNonExistent()
526
    {
527
        $this->crudPanel->setModel(User::class);
528
        $this->crudPanel->setOperation('list');
529
        $this->crudPanel->setDefaultPageLength(40);
530
        $this->crudPanel->setPageLengthMenu($this->defaultPaginator);
531
532
        $this->assertCount(2, $this->crudPanel->getPageLengthMenu());
533
        $this->assertTrue(in_array(40, $this->crudPanel->getOperationSetting('pageLengthMenu')[0]));
534
        $this->assertEquals(array_values($this->crudPanel->getPageLengthMenu()[0])[0], 40);
535
    }
536
537
    /**
538
     * Tests if table paginator adds default option existent.
539
     *
540
     */
541
    public function testCrudPanelPaginatorAddsDefaultOptionExistent()
542
    {
543
        $this->crudPanel->setModel(User::class);
544
        $this->crudPanel->setOperation('list');
545
546
        $this->crudPanel->setPageLengthMenu($this->defaultPaginator);
547
        $this->crudPanel->setDefaultPageLength(20);
548
        $this->assertCount(2, $this->crudPanel->getPageLengthMenu());
549
        $this->assertTrue(in_array(10, $this->crudPanel->getOperationSetting('pageLengthMenu')[0]));
550
        $this->assertEquals(array_values($this->crudPanel->getPageLengthMenu()[0])[0], 20);
551
    }
552
}
553