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

Issues (892)

Branch: main

tests/Unit/CrudPanel/CrudPanelButtonsTest.php (9 issues)

1
<?php
2
3
namespace Backpack\CRUD\Tests\Unit\CrudPanel;
4
5
use Backpack\CRUD\app\Library\CrudPanel\CrudButton;
6
use Backpack\CRUD\Tests\config\CrudPanel\BaseCrudPanel;
7
8
/**
9
 * @covers Backpack\CRUD\app\Library\CrudPanel\Traits\Buttons
10
 * @covers Backpack\CRUD\app\Library\CrudPanel\CrudButton
11
 */
12
class CrudPanelButtonsTest extends BaseCrudPanel
13
{
14
    private $defaultButtonNames = [];
15
16
    private $topViewButton;
17
18
    private $lineViewButton;
19
20
    private $bottomViewButton;
21
22
    private $topModelFunctionButton;
23
24
    protected function setUp(): void
25
    {
26
        parent::setUp();
27
28
        $this->crudPanel->setOperation('list');
29
30
        $this->topViewButton = [
31
            'name' => 'topViewButton',
32
            'stack' => 'top',
33
            'type' => 'view',
34
            'content' => 'crud::buttons.show',
35
            'meta' => [],
36
        ];
37
        $this->lineViewButton = [
38
            'name' => 'lineViewButton',
39
            'stack' => 'line',
40
            'type' => 'view',
41
            'content' => 'crud::buttons.show',
42
            'position' => null,
43
            'meta' => [],
44
        ];
45
        $this->bottomViewButton = [
46
            'name' => 'bottomViewButton',
47
            'stack' => 'bottom',
48
            'type' => 'view',
49
            'content' => 'crud::buttons.show',
50
            'position' => null,
51
            'meta' => [],
52
        ];
53
        $this->topModelFunctionButton = [
54
            'name' => 'topModelFunctionButton',
55
            'stack' => 'top',
56
            'type' => 'model_function',
57
            'content' => 'crud::buttons.show',
58
            'position' => null,
59
            'meta' => [],
60
        ];
61
    }
62
63
    public function testItCanAddMultipleButtons()
64
    {
65
        $this->addDefaultButtons();
66
67
        $this->assertcount(4, $this->crudPanel->buttons());
68
    }
69
70
    public function testCanAddButtonToSpecificStack()
71
    {
72
        $this->addDefaultButtons();
73
        $expectedButton = $this->topViewButton;
74
        $expectedButton['name'] = 'topViewButtonCustomName';
75
        $expectedButton['stack'] = 'top';
76
        $expectedButton['position'] = null;
77
78
        $this->crudPanel->addButton($expectedButton['stack'], $expectedButton['name'], $expectedButton['type'], $expectedButton['content']);
79
80
        $this->assertEquals($expectedButton, $this->crudPanel->buttons()->last()->toArray());
81
        $this->assertCount(3, $this->crudPanel->getButtonsForStack($expectedButton['stack']));
82
    }
83
84
    public function testAddButtonsWithSameName()
85
    {
86
        $expectedButton = $this->topViewButton;
87
88
        $this->crudPanel->addButton($expectedButton['stack'], $expectedButton['name'], $expectedButton['type'], $expectedButton['content']);
89
        $this->crudPanel->addButton($expectedButton['stack'], $expectedButton['name'], $expectedButton['type'], $expectedButton['content']);
90
91
        $this->assertCount(1, $this->crudPanel->buttons());
92
93
        $expectedButton2 = $this->bottomViewButton;
94
        CrudButton::name($expectedButton2);
95
        CrudButton::name($expectedButton2);
96
97
        $this->assertCount(2, $this->crudPanel->buttons());
98
    }
99
100
    public function testAddButtonBeginning()
101
    {
102
        $this->addTestButton('topViewButton');
103
104
        $expectedButton = $this->bottomViewButton;
105
106
        $this->crudPanel->addButton($expectedButton['stack'], $expectedButton['name'], $expectedButton['type'], $expectedButton['content'], 'beginning');
107
108
        $this->assertEquals($expectedButton, $this->crudPanel->buttons()->first()->toArray());
109
    }
110
111
    public function testAddButtonEnd()
112
    {
113
        $this->addTestButton('lineViewButton');
114
115
        $expectedButton = $this->lineViewButton;
116
117
        $this->crudPanel->addButton($expectedButton['stack'], $expectedButton['name'], $expectedButton['type'], $expectedButton['content'], 'end');
118
119
        $this->assertEquals($expectedButton, $this->crudPanel->buttons()->last()->toArray());
120
    }
121
122
    public function testAddButtonFromModelFunction()
123
    {
124
        $expectedButton = $this->topModelFunctionButton;
125
126
        $this->crudPanel->addButton($expectedButton['stack'], $expectedButton['name'], $expectedButton['type'], $expectedButton['content']);
127
128
        $this->assertEquals($expectedButton, $this->crudPanel->buttons()->last()->toArray());
129
    }
130
131
    public function testAddButtonFromView()
132
    {
133
        $expectedButton = $this->topViewButton;
134
        $viewName = 'someViewName';
135
136
        $this->crudPanel->addButtonFromView($expectedButton['stack'], $expectedButton['name'], $viewName);
137
138
        $backpackButtonViewPackage = 'crud::buttons.';
139
        $actualButton = $this->crudPanel->buttons()->last();
140
141
        $this->assertEquals($expectedButton['stack'], $actualButton->stack);
142
        $this->assertEquals($expectedButton['name'], $actualButton->name);
143
        $this->assertEquals($backpackButtonViewPackage.$viewName, $actualButton->content);
144
    }
145
146
    public function testRemoveButton()
147
    {
148
        $this->crudPanel->addButton('line', 'update', 'view', 'crud::buttons.update', 'end');
149
        $this->crudPanel->removeButton('update');
150
151
        $this->assertCount(0, $this->crudPanel->buttons());
152
        $this->assertNull($this->getButtonByName('update'));
153
    }
154
155
    public function testRemoveButtons()
156
    {
157
        $this->crudPanel->addButton('line', 'update', 'view', 'crud::buttons.update', 'end');
158
        $this->crudPanel->addButton('line', 'show', 'view', 'crud::buttons.show', 'end');
159
        $this->crudPanel->removeButtons(['show', 'update']);
160
161
        $this->assertCount(0, $this->crudPanel->buttons());
162
        $this->assertNull($this->getButtonByName('show'));
163
        $this->assertNull($this->getButtonByName('update'));
164
    }
165
166
    public function testRemoveUnknownButtons()
167
    {
168
        $buttonNames = [
169
            'someButtonName',
170
            'someOtherButtonName',
171
        ];
172
173
        $this->addDefaultButtons();
174
        $this->crudPanel->removeButtons($buttonNames);
175
176
        $this->assertCount(4, $this->crudPanel->buttons());
177
    }
178
179
    public function testRemoveUnknownButton()
180
    {
181
        $this->addTestButton('topViewButton');
182
183
        $this->crudPanel->removeButton('someButtonName');
184
185
        $this->assertCount(1, $this->crudPanel->buttons());
186
    }
187
188
    public function testRemoveAllButtons()
189
    {
190
        $this->addDefaultButtons();
191
        $this->crudPanel->removeAllButtons();
192
193
        $this->assertEmpty($this->crudPanel->buttons());
194
    }
195
196
    public function testRemoveButtonFromStack()
197
    {
198
        $this->crudPanel->addButton('line', 'update', 'view', 'crud::buttons.update', 'end');
199
200
        $button = $this->crudPanel->buttons()->first();
201
202
        $this->crudPanel->removeButtonFromStack($button->name, $button->stack);
203
204
        $this->assertCount(0, $this->crudPanel->buttons());
205
        $this->assertNull($this->getButtonByName($button->name));
206
    }
207
208
    public function testRemoveUnknownButtonFromStack()
209
    {
210
        $this->addTestButton('lineViewButton');
211
        $this->crudPanel->removeButtonFromStack('someButtonName', 'line');
212
213
        $this->assertCount(1, $this->crudPanel->buttons());
214
    }
215
216
    public function testRemoveButtonFromUnknownStack()
217
    {
218
        $this->crudPanel->addButton('line', 'update', 'view', 'crud::buttons.update', 'end');
219
        $this->crudPanel->addButton('line', 'show', 'view', 'crud::buttons.show', 'end');
220
221
        $button = $this->crudPanel->buttons()->first();
222
223
        $this->crudPanel->removeButtonFromStack($button->name, 'someStackName');
224
225
        $this->assertCount(2, $this->crudPanel->buttons());
226
    }
227
228
    public function testRemoveAllButtonsFromStack()
229
    {
230
        $this->crudPanel->addButton('line', 'update', 'view', 'crud::buttons.update', 'end');
231
        $this->crudPanel->addButton('line', 'show', 'view', 'crud::buttons.show', 'end');
232
233
        $this->crudPanel->removeAllButtonsFromStack('line');
234
235
        $this->assertCount(0, $this->crudPanel->buttons());
236
    }
237
238
    public function testRemoveAllButtonsFromUnknownStack()
239
    {
240
        $this->addTestButton('lineViewButton');
241
242
        $this->crudPanel->removeAllButtonsFromStack('someStackName');
243
244
        $this->assertCount(1, $this->crudPanel->buttons());
245
    }
246
247
    public function testOrderButtons()
248
    {
249
        $this->crudPanel->addButton('line', 'update', 'view', 'crud::buttons.update', 'end');
250
        $this->crudPanel->addButton('line', 'show', 'view', 'crud::buttons.show', 'end');
251
        $this->crudPanel->addButton('line', 'test', 'view', 'crud::buttons.test', 'end');
252
253
        $this->crudPanel->orderButtons('line', ['show', 'test']);
254
255
        $this->assertEquals(['show', 'test', 'update'], $this->crudPanel->buttons()->pluck('name')->toArray());
256
    }
257
258
    public function testOrderButtonsInStack()
259
    {
260
        $this->addDefaultButtons();
261
262
        $this->crudPanel->orderButtons('top', ['topModelFunctionButton']);
263
264
        $this->assertEquals(['topModelFunctionButton', 'topViewButton'], $this->crudPanel->getButtonsForStack('top')->pluck('name')->toArray());
265
    }
266
267
    public function testOrderThrowExceptionIfButtonDoesNotExist()
268
    {
269
        $this->addDefaultButtons();
270
271
        $this->expectException(\Symfony\Component\HttpKernel\Exception\HttpException::class);
272
273
        $this->crudPanel->orderButtons('top', ['unknownButton']);
274
    }
275
276
    public function testAddButtonFluently()
277
    {
278
        $button1 = CrudButton::name('lineTest')->to('line')->view('crud::buttons.test')->type('view');
279
        $button2 = CrudButton::add('modelFunction')->model_function(function () {
0 ignored issues
show
function(...) { /* ... */ } of type callable is incompatible with the type string expected by parameter $value of Backpack\CRUD\app\Librar...utton::model_function(). ( Ignorable by Annotation )

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

279
        $button2 = CrudButton::add('modelFunction')->model_function(/** @scrutinizer ignore-type */ function () {
Loading history...
280
            return 'test';
281
        })->section('top')->makeFirst();
282
        $this->assertEquals($button1->toArray(), $this->crudPanel->buttons()->last()->toArray());
283
        $button1->makeLast();
284
        $this->assertEquals($button2->toArray(), $this->crudPanel->buttons()->first()->toArray());
285
    }
286
287
    public function testItThrowsExceptionWhenModifyingUnknownButton()
288
    {
289
        $this->addDefaultButtons();
290
291
        $this->expectException(\Symfony\Component\HttpKernel\Exception\HttpException::class);
292
293
        $this->crudPanel->modifyButton('unknownButton', function ($button) {
0 ignored issues
show
function(...) { /* ... */ } of type callable is incompatible with the type array expected by parameter $modifications of Backpack\CRUD\app\Librar...udPanel::modifyButton(). ( Ignorable by Annotation )

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

293
        $this->crudPanel->modifyButton('unknownButton', /** @scrutinizer ignore-type */ function ($button) {
Loading history...
294
            $button->name = 'newName';
295
        });
296
    }
297
298
    public function testItCanAddAButtonFromAModelFunction()
299
    {
300
        $this->crudPanel->addButtonFromModelFunction('line', 'buttonModelFunction', 'buttonModelFunction');
301
        $this->assertEquals('buttonModelFunction', $this->crudPanel->buttons()->first()->content);
302
    }
303
304
    public function testItDoesNotMoveFieldWhenTargetIsUnknown()
305
    {
306
        $this->addDefaultButtons();
307
308
        $firstButtonName = $this->crudPanel->buttons()->first()->name;
309
310
        $this->crudPanel->moveButton('unknownButton', 'before', 'topViewButton');
311
312
        $this->assertCount(4, $this->crudPanel->buttons());
313
        $this->assertEquals($firstButtonName, $this->crudPanel->buttons()->first()->name);
0 ignored issues
show
The method first() does not exist on Countable. It seems like you code against a sub-type of Countable such as Illuminate\Pagination\LengthAwarePaginator or Illuminate\Http\Resources\Json\ResourceCollection or Nette\Utils\Html or League\CommonMark\Util\ArrayCollection or Illuminate\Pagination\CursorPaginator or Illuminate\Contracts\Support\MessageBag or Carbon\CarbonPeriod or Illuminate\Pagination\Paginator or Nette\Iterators\CachingIterator or Nette\Utils\ArrayList or Illuminate\Support\ViewErrorBag or Illuminate\Support\Enumerable or Ramsey\Collection\CollectionInterface or Ramsey\Collection\AbstractCollection or Ds\Map or Ds\Set or Ds\Sequence or Nette\Iterators\CachingIterator. ( Ignorable by Annotation )

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

313
        $this->assertEquals($firstButtonName, $this->crudPanel->buttons()->/** @scrutinizer ignore-call */ first()->name);
Loading history...
314
    }
315
316
    public function testItDoesNotMoveButtonWhenDestinationIsUnknown()
317
    {
318
        $this->addDefaultButtons();
319
320
        $firstButtonName = $this->crudPanel->buttons()->first()->name;
321
322
        $this->crudPanel->moveButton('topViewButton', 'before', 'unknownButton');
323
324
        $this->assertCount(4, $this->crudPanel->buttons());
325
        $this->assertEquals($firstButtonName, $this->crudPanel->buttons()->first()->name);
326
    }
327
328
    public function testItCanCreateANewCrudButtonInstance()
329
    {
330
        $button = $this->crudPanel->button(['name' => 'testButton', 'stack' => 'line', 'type' => 'view', 'content' => 'crud::buttons.test']);
331
        $this->assertEquals($button->toArray(), $this->crudPanel->buttons()->last()->toArray());
332
        $this->assertInstanceOf(\Backpack\CRUD\app\Library\CrudPanel\CrudButton::class, $button);
333
    }
334
335
    public function testItCanCheckIfAnyOfTheButtonsHasTheDeterminedKayValuePair()
336
    {
337
        $this->addDefaultButtons();
338
339
        $this->assertTrue($this->crudPanel->hasButtonWhere('name', 'topViewButton'));
340
        $this->assertFalse($this->crudPanel->hasButtonWhere('name', 'unknownButton'));
341
    }
342
343
    public function testItGenerateARandomButtonNameIfOneNotProvided()
344
    {
345
        $button = $this->crudPanel->button(['stack' => 'line', 'type' => 'view', 'content' => 'crud::buttons.test']);
346
        $this->assertTrue(str_starts_with($button->name, 'button_'));
347
    }
348
349
    public function testMovingTheButtonUsingPosition()
350
    {
351
        $button1 = CrudButton::name('lineTest')->to('line')->view('crud::buttons.test')->type('view');
352
        $button2 = CrudButton::name('lineTest2')->to('line')->view('crud::buttons.test')->type('view')->position('beginning');
353
        $this->assertEquals($button2->toArray(), $this->crudPanel->buttons()->first()->toArray());
354
        $button2->position('end');
355
        $this->assertEquals($button1->toArray(), $this->crudPanel->buttons()->first()->toArray());
356
    }
357
358
    public function testThrowsErrorInUnknownPosition()
359
    {
360
        try {
361
            $button1 = CrudButton::name('lineTest')->to('line')->view('crud::buttons.test')->type('view')->position('unknown');
0 ignored issues
show
The assignment to $button1 is dead and can be removed.
Loading history...
362
        } catch (\Throwable $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
363
        }
364
        $this->assertEquals(
365
            new \Symfony\Component\HttpKernel\Exception\HttpException(500, 'Unknown button position - please use \'beginning\' or \'end\'.', null, ['developer-error-exception']),
366
            $e
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $e does not seem to be defined for all execution paths leading up to this point.
Loading history...
367
        );
368
    }
369
370
    public function testItCanGetButtonKeyInTheArray()
371
    {
372
        $button = CrudButton::make('lineTest')->content('crud::buttons.test')->type('view');
0 ignored issues
show
The assignment to $button is dead and can be removed.
Loading history...
373
        $this->assertEquals(0, CrudButton::make('lineTest')->getKey());
374
    }
375
376
    public function testItCanRemoveButtonFromButtonList()
377
    {
378
        $this->addDefaultButtons();
379
380
        CrudButton::make('topViewButton')->remove();
381
382
        $this->assertCount(3, $this->crudPanel->buttons());
383
    }
384
385
    public function testItCanAddButtonsToAnHiddenStack()
386
    {
387
        $button = CrudButton::make('lineTest')->content('crud::buttons.test')->type('view');
0 ignored issues
show
The assignment to $button is dead and can be removed.
Loading history...
388
        $this->assertCount(1, $this->crudPanel->getButtonsForStack('hidden'));
389
    }
390
391
    public function testItCanAddMetadataToAButton()
392
    {
393
        $button = CrudButton::make('lineTest')->content('crud::buttons.test')->type('view')->meta(['key' => 'value']);
0 ignored issues
show
The assignment to $button is dead and can be removed.
Loading history...
394
        $this->assertEquals(['key' => 'value'], $this->crudPanel->buttons()->last()->meta);
395
    }
396
397
    public function testItCanForgetAPropertyFromAButton()
398
    {
399
        $button = CrudButton::make('lineTest')->content('crud::buttons.test')->type('view')->meta(['key' => 'value'])->group('line');
400
        $button->forget('meta');
401
        $this->assertEquals(null, $this->crudPanel->buttons()->last()->meta);
402
    }
403
404
    public function testItCanGetTheButtonHtmlToRender()
405
    {
406
        $this->crudPanel->addButtonFromModelFunction('line', 'buttonModelFunction', 'buttonModelFunction');
407
        $this->assertEquals('model function button test', $this->crudPanel->buttons()->first()->getHtml());
408
409
        $this->crudPanel->button('test')->stack('line')->type('view')->content('backpack.theme-coreuiv2::buttons.test');
410
411
        $this->assertEquals('<a href="test" class="btn btn-secondary">Test</a>', $this->crudPanel->buttons()->last()->getHtml());
412
    }
413
414
    public function testItThrowsErrorWhenAttemptingToRenderUnknowButtonView()
415
    {
416
        $this->expectException(\Symfony\Component\HttpKernel\Exception\HttpException::class);
417
        $this->crudPanel->button('dontexist')->type('view')->content('unknown_view')->getHtml();
418
    }
419
420
    public function testItThrowsErrorWhenAttemptingToRenderUnknowButtonType()
421
    {
422
        $this->expectException(\Symfony\Component\HttpKernel\Exception\HttpException::class);
423
        $this->crudPanel->button('dontexist')->type('unknown')->getHtml();
424
    }
425
426
    private function getButtonByName($name)
427
    {
428
        return $this->crudPanel->buttons()->first(function ($value) use ($name) {
429
            return $value->name == $name;
430
        });
431
    }
432
433
    private function addDefaultButtons()
434
    {
435
        $this->crudPanel->button($this->topViewButton);
436
        $this->crudPanel->button($this->lineViewButton);
437
        $this->crudPanel->button($this->bottomViewButton);
438
        $this->crudPanel->button($this->topModelFunctionButton);
439
    }
440
441
    private function addTestButton($buttonName)
442
    {
443
        $this->crudPanel->button(array_values($this->{$buttonName}));
444
    }
445
}
446