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 (#5588)
by Cristian
24:41 queued 14:04
created

testItCanSetGetTheRefeererFromSaveAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
cc 1
eloc 6
c 2
b 1
f 1
nc 1
nop 0
dl 0
loc 13
rs 10
1
<?php
2
3
namespace Backpack\CRUD\Tests\Unit\CrudPanel;
4
5
/**
6
 * @covers Backpack\CRUD\app\Library\CrudPanel\Traits\SaveActions
7
 */
8
class CrudPanelSaveActionsTest extends \Backpack\CRUD\Tests\config\CrudPanel\BaseCrudPanel
9
{
10
    private $singleSaveAction;
11
12
    private $multipleSaveActions;
13
14
    /**
15
     * Setup the test environment.
16
     *
17
     * @return void
18
     */
19
    protected function setUp(): void
20
    {
21
        parent::setUp();
22
23
        $this->crudPanel->setOperation('create');
24
25
        $this->singleSaveAction = [
26
            'name' => 'save_action_one',
27
            'button_text' => 'custom',
28
            'redirect' => function ($crud, $request, $itemId) {
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

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

28
            'redirect' => function ($crud, /** @scrutinizer ignore-unused */ $request, $itemId) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $itemId is not used and could be removed. ( Ignorable by Annotation )

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

28
            'redirect' => function ($crud, $request, /** @scrutinizer ignore-unused */ $itemId) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $crud is not used and could be removed. ( Ignorable by Annotation )

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

28
            'redirect' => function (/** @scrutinizer ignore-unused */ $crud, $request, $itemId) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
29
                return 'https://backpackforlaravel.com';
30
            },
31
            'referrer_url' => function ($crud, $request, $itemId) {
0 ignored issues
show
Unused Code introduced by
The parameter $crud is not used and could be removed. ( Ignorable by Annotation )

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

31
            'referrer_url' => function (/** @scrutinizer ignore-unused */ $crud, $request, $itemId) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

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

31
            'referrer_url' => function ($crud, /** @scrutinizer ignore-unused */ $request, $itemId) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $itemId is not used and could be removed. ( Ignorable by Annotation )

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

31
            'referrer_url' => function ($crud, $request, /** @scrutinizer ignore-unused */ $itemId) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
32
                return 'https://backpackforlaravel.com';
33
            },
34
            'visible' => function ($crud) {
0 ignored issues
show
Unused Code introduced by
The parameter $crud is not used and could be removed. ( Ignorable by Annotation )

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

34
            'visible' => function (/** @scrutinizer ignore-unused */ $crud) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
35
                return true;
36
            },
37
        ];
38
39
        $this->multipleSaveActions = [
40
            [
41
                'name' => 'save_action_one',
42
                'redirect' => function ($crud, $request, $itemId) {
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

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

42
                'redirect' => function ($crud, /** @scrutinizer ignore-unused */ $request, $itemId) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $itemId is not used and could be removed. ( Ignorable by Annotation )

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

42
                'redirect' => function ($crud, $request, /** @scrutinizer ignore-unused */ $itemId) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
43
                    return $crud->route;
44
                },
45
                'visible' => function ($crud) {
0 ignored issues
show
Unused Code introduced by
The parameter $crud is not used and could be removed. ( Ignorable by Annotation )

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

45
                'visible' => function (/** @scrutinizer ignore-unused */ $crud) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
46
                    return true;
47
                },
48
            ],
49
            [
50
                'name' => 'save_action_two',
51
                'redirect' => function ($crud, $request, $itemId) {
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

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

51
                'redirect' => function ($crud, /** @scrutinizer ignore-unused */ $request, $itemId) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $itemId is not used and could be removed. ( Ignorable by Annotation )

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

51
                'redirect' => function ($crud, $request, /** @scrutinizer ignore-unused */ $itemId) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
52
                    return $crud->route;
53
                },
54
                'visible' => function ($crud) {
0 ignored issues
show
Unused Code introduced by
The parameter $crud is not used and could be removed. ( Ignorable by Annotation )

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

54
                'visible' => function (/** @scrutinizer ignore-unused */ $crud) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
55
                    return true;
56
                },
57
            ],
58
        ];
59
    }
60
61
    public function testAddDefaultSaveActions()
62
    {
63
        $this->crudPanel->setupDefaultSaveActions();
64
        $this->assertEquals(3, count($this->crudPanel->getOperationSetting('save_actions')));
0 ignored issues
show
Bug introduced by
It seems like $this->crudPanel->getOpe...Setting('save_actions') can also be of type null; however, parameter $value of count() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

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

64
        $this->assertEquals(3, count(/** @scrutinizer ignore-type */ $this->crudPanel->getOperationSetting('save_actions')));
Loading history...
65
    }
66
67
    public function testAddOneSaveAction()
68
    {
69
        $this->crudPanel->setupDefaultSaveActions();
70
        $this->crudPanel->addSaveAction($this->singleSaveAction);
71
72
        $this->assertEquals(4, count($this->crudPanel->getOperationSetting('save_actions')));
0 ignored issues
show
Bug introduced by
It seems like $this->crudPanel->getOpe...Setting('save_actions') can also be of type null; however, parameter $value of count() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

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

72
        $this->assertEquals(4, count(/** @scrutinizer ignore-type */ $this->crudPanel->getOperationSetting('save_actions')));
Loading history...
73
        $this->assertEquals(['save_and_back', 'save_and_edit', 'save_and_new', 'save_action_one'], array_keys($this->crudPanel->getOperationSetting('save_actions')));
0 ignored issues
show
Bug introduced by
It seems like $this->crudPanel->getOpe...Setting('save_actions') can also be of type null; however, parameter $array of array_keys() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

73
        $this->assertEquals(['save_and_back', 'save_and_edit', 'save_and_new', 'save_action_one'], array_keys(/** @scrutinizer ignore-type */ $this->crudPanel->getOperationSetting('save_actions')));
Loading history...
74
    }
75
76
    public function testAddMultipleSaveActions()
77
    {
78
        $this->crudPanel->setupDefaultSaveActions();
79
        $this->crudPanel->addSaveActions($this->multipleSaveActions);
80
81
        $this->assertEquals(5, count($this->crudPanel->getOperationSetting('save_actions')));
0 ignored issues
show
Bug introduced by
It seems like $this->crudPanel->getOpe...Setting('save_actions') can also be of type null; however, parameter $value of count() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

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

81
        $this->assertEquals(5, count(/** @scrutinizer ignore-type */ $this->crudPanel->getOperationSetting('save_actions')));
Loading history...
82
        $this->assertEquals(['save_and_back', 'save_and_edit', 'save_and_new', 'save_action_one', 'save_action_two'], array_keys($this->crudPanel->getOperationSetting('save_actions')));
0 ignored issues
show
Bug introduced by
It seems like $this->crudPanel->getOpe...Setting('save_actions') can also be of type null; however, parameter $array of array_keys() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

82
        $this->assertEquals(['save_and_back', 'save_and_edit', 'save_and_new', 'save_action_one', 'save_action_two'], array_keys(/** @scrutinizer ignore-type */ $this->crudPanel->getOperationSetting('save_actions')));
Loading history...
83
    }
84
85
    public function testRemoveOneSaveAction()
86
    {
87
        $this->crudPanel->setupDefaultSaveActions();
88
        $this->crudPanel->removeSaveAction('save_and_new');
89
        $this->assertEquals(2, count($this->crudPanel->getOperationSetting('save_actions')));
0 ignored issues
show
Bug introduced by
It seems like $this->crudPanel->getOpe...Setting('save_actions') can also be of type null; however, parameter $value of count() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

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

89
        $this->assertEquals(2, count(/** @scrutinizer ignore-type */ $this->crudPanel->getOperationSetting('save_actions')));
Loading history...
90
        $this->assertEquals(['save_and_back', 'save_and_edit'], array_keys($this->crudPanel->getOperationSetting('save_actions')));
0 ignored issues
show
Bug introduced by
It seems like $this->crudPanel->getOpe...Setting('save_actions') can also be of type null; however, parameter $array of array_keys() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

90
        $this->assertEquals(['save_and_back', 'save_and_edit'], array_keys(/** @scrutinizer ignore-type */ $this->crudPanel->getOperationSetting('save_actions')));
Loading history...
91
    }
92
93
    public function testRemoveMultipleSaveActions()
94
    {
95
        $this->crudPanel->setupDefaultSaveActions();
96
        $this->crudPanel->removeSaveActions(['save_and_new', 'save_and_edit']);
97
        $this->assertEquals(1, count($this->crudPanel->getOperationSetting('save_actions')));
0 ignored issues
show
Bug introduced by
It seems like $this->crudPanel->getOpe...Setting('save_actions') can also be of type null; however, parameter $value of count() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

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

97
        $this->assertEquals(1, count(/** @scrutinizer ignore-type */ $this->crudPanel->getOperationSetting('save_actions')));
Loading history...
98
        $this->assertEquals(['save_and_back'], array_keys($this->crudPanel->getOperationSetting('save_actions')));
0 ignored issues
show
Bug introduced by
It seems like $this->crudPanel->getOpe...Setting('save_actions') can also be of type null; however, parameter $array of array_keys() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

98
        $this->assertEquals(['save_and_back'], array_keys(/** @scrutinizer ignore-type */ $this->crudPanel->getOperationSetting('save_actions')));
Loading history...
99
    }
100
101
    public function testReplaceSaveActionsWithOneSaveAction()
102
    {
103
        $this->crudPanel->setupDefaultSaveActions();
104
        $this->crudPanel->setSaveActions($this->singleSaveAction);
105
        $this->assertEquals(1, count($this->crudPanel->getOperationSetting('save_actions')));
0 ignored issues
show
Bug introduced by
It seems like $this->crudPanel->getOpe...Setting('save_actions') can also be of type null; however, parameter $value of count() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

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

105
        $this->assertEquals(1, count(/** @scrutinizer ignore-type */ $this->crudPanel->getOperationSetting('save_actions')));
Loading history...
106
        $this->assertEquals(['save_action_one'], array_keys($this->crudPanel->getOperationSetting('save_actions')));
0 ignored issues
show
Bug introduced by
It seems like $this->crudPanel->getOpe...Setting('save_actions') can also be of type null; however, parameter $array of array_keys() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

106
        $this->assertEquals(['save_action_one'], array_keys(/** @scrutinizer ignore-type */ $this->crudPanel->getOperationSetting('save_actions')));
Loading history...
107
    }
108
109
    public function testReplaceSaveActionsWithMultipleSaveActions()
110
    {
111
        $this->crudPanel->setupDefaultSaveActions();
112
        $this->crudPanel->replaceSaveActions($this->multipleSaveActions);
113
        $this->assertEquals(2, count($this->crudPanel->getOperationSetting('save_actions')));
0 ignored issues
show
Bug introduced by
It seems like $this->crudPanel->getOpe...Setting('save_actions') can also be of type null; however, parameter $value of count() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

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

113
        $this->assertEquals(2, count(/** @scrutinizer ignore-type */ $this->crudPanel->getOperationSetting('save_actions')));
Loading history...
114
        $this->assertEquals(['save_action_one', 'save_action_two'], array_keys($this->crudPanel->getOperationSetting('save_actions')));
0 ignored issues
show
Bug introduced by
It seems like $this->crudPanel->getOpe...Setting('save_actions') can also be of type null; however, parameter $array of array_keys() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

114
        $this->assertEquals(['save_action_one', 'save_action_two'], array_keys(/** @scrutinizer ignore-type */ $this->crudPanel->getOperationSetting('save_actions')));
Loading history...
115
    }
116
117
    public function testOrderOneSaveAction()
118
    {
119
        $this->crudPanel->setupDefaultSaveActions();
120
        $this->crudPanel->orderSaveAction('save_and_new', 1);
121
        $this->assertEquals(1, $this->crudPanel->getOperationSetting('save_actions')['save_and_new']['order']);
122
        $this->assertEquals('save_and_new', $this->crudPanel->getFallBackSaveAction());
123
    }
124
125
    public function testOrderMultipleSaveActions()
126
    {
127
        $this->crudPanel->setupDefaultSaveActions();
128
        $this->crudPanel->orderSaveActions(['save_and_new', 'save_and_back']);
129
        $this->assertEquals(1, $this->crudPanel->getOperationSetting('save_actions')['save_and_new']['order']);
130
        $this->assertEquals(2, $this->crudPanel->getOperationSetting('save_actions')['save_and_back']['order']);
131
        $this->assertEquals(3, $this->crudPanel->getOperationSetting('save_actions')['save_and_edit']['order']);
132
        $this->crudPanel->orderSaveActions(['save_and_edit' => 1]);
133
        $this->assertEquals('save_and_edit', $this->crudPanel->getFallBackSaveAction());
134
        $this->assertEquals(['save_and_edit', 'save_and_back', 'save_and_new'], array_keys($this->crudPanel->getOrderedSaveActions()));
135
    }
136
137
    public function testItCanGetTheDefaultSaveActionForCurrentOperation()
138
    {
139
        $this->crudPanel->setupDefaultSaveActions();
140
        $saveAction = $this->crudPanel->getSaveActionDefaultForCurrentOperation();
141
        $this->assertEquals('save_and_back', $saveAction);
142
    }
143
144
    public function testItCanGetTheDefaultSaveActionFromOperationSettings()
145
    {
146
        $this->crudPanel->setupDefaultSaveActions();
147
        $this->assertEquals('save_and_back', $this->crudPanel->getFallBackSaveAction());
148
        $this->crudPanel->setOperationSetting('defaultSaveAction', 'save_and_new');
149
        $this->assertEquals('save_and_new', $this->crudPanel->getFallBackSaveAction());
150
    }
151
152
    public function testItCanRemoveAllTheSaveActions()
153
    {
154
        $this->crudPanel->setupDefaultSaveActions();
155
        $this->assertCount(3, $this->crudPanel->getOperationSetting('save_actions'));
0 ignored issues
show
Bug introduced by
It seems like $this->crudPanel->getOpe...Setting('save_actions') can also be of type null; however, parameter $haystack of PHPUnit\Framework\Assert::assertCount() does only seem to accept Countable|iterable, maybe add an additional type check? ( Ignorable by Annotation )

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

155
        $this->assertCount(3, /** @scrutinizer ignore-type */ $this->crudPanel->getOperationSetting('save_actions'));
Loading history...
156
        $this->crudPanel->removeAllSaveActions();
157
        $this->assertCount(0, $this->crudPanel->getOperationSetting('save_actions'));
158
    }
159
160
    public function testItCanHideSaveActions()
161
    {
162
        $this->setupDefaultSaveActionsOnCrudPanel();
163
        $saveAction = $this->singleSaveAction;
164
        $saveAction['visible'] = false;
165
        $this->crudPanel->addSaveAction($saveAction);
166
        $this->assertCount(4, $this->crudPanel->getOperationSetting('save_actions'));
0 ignored issues
show
Bug introduced by
It seems like $this->crudPanel->getOpe...Setting('save_actions') can also be of type null; however, parameter $haystack of PHPUnit\Framework\Assert::assertCount() does only seem to accept Countable|iterable, maybe add an additional type check? ( Ignorable by Annotation )

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

166
        $this->assertCount(4, /** @scrutinizer ignore-type */ $this->crudPanel->getOperationSetting('save_actions'));
Loading history...
167
        $this->assertCount(3, $this->crudPanel->getVisibleSaveActions());
168
    }
169
170
    public function testItCanGetSaveActionFromSession()
171
    {
172
        $this->setupDefaultSaveActionsOnCrudPanel();
173
        $this->crudPanel->addSaveAction($this->singleSaveAction);
174
175
        session()->put('create.saveAction', 'save_action_one');
176
177
        $saveActions = $this->crudPanel->getSaveAction();
178
179
        $expected = [
180
            'active' => [
181
                'value' => 'save_action_one',
182
                'label' => 'custom',
183
            ],
184
            'options' => [
185
                'save_and_back' => 'Save and back',
186
                'save_and_edit' => 'Save and edit this item',
187
                'save_and_new' => 'Save and new item',
188
            ],
189
        ];
190
        $this->assertEquals($expected, $saveActions);
191
    }
192
193
    public function testItGetsTheFirstSaveActionIfTheRequiredActionIsNotASaveAction()
194
    {
195
        $this->setupDefaultSaveActionsOnCrudPanel();
196
        session()->put('create.saveAction', 'not_a_save_action');
197
        $this->assertEquals('save_and_back', $this->crudPanel->getSaveAction()['active']['value']);
198
    }
199
200
    public function testItCanSetTheSaveActionInSessionFromRequest()
201
    {
202
        $this->setupDefaultSaveActionsOnCrudPanel();
203
204
        $this->setupUserCreateRequest();
205
206
        $this->crudPanel->getRequest()->merge(['_save_action' => 'save_action_one']);
207
208
        $this->crudPanel->setSaveAction();
209
210
        $this->assertEquals('save_action_one', session()->get('create.saveAction'));
211
    }
212
213
    public function testItCanPerformTheSaveActionAndReturnTheRedirect()
214
    {
215
        $this->setupDefaultSaveActionsOnCrudPanel();
216
217
        $redirect = $this->crudPanel->performSaveAction();
218
        $this->assertEquals(url('/'), $redirect->getTargetUrl());
219
    }
220
221
    public function testItCanPerformTheSaveActionAndReturnTheRedirectFromTheRequest()
222
    {
223
        $this->setupDefaultSaveActionsOnCrudPanel();
224
225
        $this->setupUserCreateRequest();
226
227
        $this->crudPanel->addSaveAction($this->singleSaveAction);
228
229
        $this->crudPanel->getRequest()->merge(['_save_action' => 'save_action_one']);
230
231
        $redirect = $this->crudPanel->performSaveAction();
232
233
        $this->assertEquals('https://backpackforlaravel.com', $redirect->getTargetUrl());
234
    }
235
236
    public function testItCanSetGetTheRefeererFromSaveAction()
237
    {
238
        $this->setupDefaultSaveActionsOnCrudPanel();
239
240
        $this->crudPanel->addSaveAction($this->singleSaveAction);
241
242
        $this->crudPanel->getRequest()->merge(['_save_action' => 'save_action_one']);
243
244
        $this->crudPanel->performSaveAction();
245
246
        $referer = session('referrer_url_override');
247
248
        $this->assertEquals('https://backpackforlaravel.com', $referer);
249
    }
250
251
    public function testItCanPerformTheSaveActionAndRespondWithJson()
252
    {
253
        $this->setupDefaultSaveActionsOnCrudPanel();
254
255
        $this->crudPanel->getRequest()->headers->set('X-Requested-With', 'XMLHttpRequest');
256
257
        $this->crudPanel->getRequest()->merge(['_save_action' => 'save_and_back']);
258
259
        $response = $this->crudPanel->performSaveAction();
260
261
        $this->assertEquals('application/json', $response->headers->get('Content-Type'));
262
263
        $this->assertEquals([
264
            'success' => true,
265
            'redirect_url' => null,
266
            'referrer_url' => false,
267
            'data' => null,
268
        ], json_decode($response->getContent(), true));
269
    }
270
271
    private function setupDefaultSaveActionsOnCrudPanel()
272
    {
273
        $this->crudPanel->allowAccess(['create', 'update', 'list']);
274
        $this->crudPanel->setupDefaultSaveActions();
275
    }
276
}
277