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
Push — CrudButton-fixees ( a4c9f3...1a6e15 )
by Pedro
15:19
created

testRemoveMultipleSaveActions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
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 BaseDBCrudPanelTest
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...
29
                return $crud->route;
30
            },
31
            '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

31
            '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...
32
                return true;
33
            },
34
        ];
35
36
        $this->multipleSaveActions = [
37
            [
38
                'name' => 'save_action_one',
39
                '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

39
                '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

39
                '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...
40
                    return $crud->route;
41
                },
42
                '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

42
                '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...
43
                    return true;
44
                },
45
            ],
46
            [
47
                'name' => 'save_action_two',
48
                'redirect' => function ($crud, $request, $itemId) {
0 ignored issues
show
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

48
                '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 $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

48
                '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...
49
                    return $crud->route;
50
                },
51
                '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

51
                '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...
52
                    return true;
53
                },
54
            ],
55
        ];
56
    }
57
58
    public function testAddDefaultSaveActions()
59
    {
60
        $this->crudPanel->setupDefaultSaveActions();
61
        $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

61
        $this->assertEquals(3, count(/** @scrutinizer ignore-type */ $this->crudPanel->getOperationSetting('save_actions')));
Loading history...
62
    }
63
64
    public function testAddOneSaveAction()
65
    {
66
        $this->crudPanel->setupDefaultSaveActions();
67
        $this->crudPanel->addSaveAction($this->singleSaveAction);
68
69
        $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

69
        $this->assertEquals(4, count(/** @scrutinizer ignore-type */ $this->crudPanel->getOperationSetting('save_actions')));
Loading history...
70
        $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

70
        $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...
71
    }
72
73
    public function testAddMultipleSaveActions()
74
    {
75
        $this->crudPanel->setupDefaultSaveActions();
76
        $this->crudPanel->addSaveActions($this->multipleSaveActions);
77
78
        $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

78
        $this->assertEquals(5, count(/** @scrutinizer ignore-type */ $this->crudPanel->getOperationSetting('save_actions')));
Loading history...
79
        $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

79
        $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...
80
    }
81
82
    public function testRemoveOneSaveAction()
83
    {
84
        $this->crudPanel->setupDefaultSaveActions();
85
        $this->crudPanel->removeSaveAction('save_and_new');
86
        $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

86
        $this->assertEquals(2, count(/** @scrutinizer ignore-type */ $this->crudPanel->getOperationSetting('save_actions')));
Loading history...
87
        $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

87
        $this->assertEquals(['save_and_back', 'save_and_edit'], array_keys(/** @scrutinizer ignore-type */ $this->crudPanel->getOperationSetting('save_actions')));
Loading history...
88
    }
89
90
    public function testRemoveMultipleSaveActions()
91
    {
92
        $this->crudPanel->setupDefaultSaveActions();
93
        $this->crudPanel->removeSaveActions(['save_and_new', 'save_and_edit']);
94
        $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

94
        $this->assertEquals(1, count(/** @scrutinizer ignore-type */ $this->crudPanel->getOperationSetting('save_actions')));
Loading history...
95
        $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

95
        $this->assertEquals(['save_and_back'], array_keys(/** @scrutinizer ignore-type */ $this->crudPanel->getOperationSetting('save_actions')));
Loading history...
96
    }
97
98
    public function testReplaceSaveActionsWithOneSaveAction()
99
    {
100
        $this->crudPanel->setupDefaultSaveActions();
101
        $this->crudPanel->setSaveActions($this->singleSaveAction);
102
        $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

102
        $this->assertEquals(1, count(/** @scrutinizer ignore-type */ $this->crudPanel->getOperationSetting('save_actions')));
Loading history...
103
        $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

103
        $this->assertEquals(['save_action_one'], array_keys(/** @scrutinizer ignore-type */ $this->crudPanel->getOperationSetting('save_actions')));
Loading history...
104
    }
105
106
    public function testReplaceSaveActionsWithMultipleSaveActions()
107
    {
108
        $this->crudPanel->setupDefaultSaveActions();
109
        $this->crudPanel->replaceSaveActions($this->multipleSaveActions);
110
        $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

110
        $this->assertEquals(2, count(/** @scrutinizer ignore-type */ $this->crudPanel->getOperationSetting('save_actions')));
Loading history...
111
        $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

111
        $this->assertEquals(['save_action_one', 'save_action_two'], array_keys(/** @scrutinizer ignore-type */ $this->crudPanel->getOperationSetting('save_actions')));
Loading history...
112
    }
113
114
    public function testOrderOneSaveAction()
115
    {
116
        $this->crudPanel->setupDefaultSaveActions();
117
        $this->crudPanel->orderSaveAction('save_and_new', 1);
118
        $this->assertEquals(1, $this->crudPanel->getOperationSetting('save_actions')['save_and_new']['order']);
119
        $this->assertEquals('save_and_new', $this->crudPanel->getFallBackSaveAction());
120
    }
121
122
    public function testOrderMultipleSaveActions()
123
    {
124
        $this->crudPanel->setupDefaultSaveActions();
125
        $this->crudPanel->orderSaveActions(['save_and_new', 'save_and_back']);
126
        $this->assertEquals(1, $this->crudPanel->getOperationSetting('save_actions')['save_and_new']['order']);
127
        $this->assertEquals(2, $this->crudPanel->getOperationSetting('save_actions')['save_and_back']['order']);
128
        $this->assertEquals(3, $this->crudPanel->getOperationSetting('save_actions')['save_and_edit']['order']);
129
        $this->crudPanel->orderSaveActions(['save_and_edit' => 1]);
130
        $this->assertEquals('save_and_edit', $this->crudPanel->getFallBackSaveAction());
131
        $this->assertEquals(['save_and_edit', 'save_and_back', 'save_and_new'], array_keys($this->crudPanel->getOrderedSaveActions()));
132
    }
133
134
    public function testItCanGetTheDefaultSaveActionForCurrentOperation()
135
    {
136
        $this->crudPanel->setupDefaultSaveActions();
137
        $saveAction = $this->crudPanel->getSaveActionDefaultForCurrentOperation();
138
        $this->assertEquals('save_and_back', $saveAction);
139
    }
140
141
    public function testItCanGetTheDefaultSaveActionFromOperationSettings()
142
    {
143
        $this->crudPanel->setupDefaultSaveActions();
144
        $this->assertEquals('save_and_back', $this->crudPanel->getFallBackSaveAction());
145
        $this->crudPanel->setOperationSetting('defaultSaveAction', 'save_and_new');
0 ignored issues
show
Bug introduced by
'save_and_new' of type string is incompatible with the type boolean expected by parameter $value of Backpack\CRUD\app\Librar...::setOperationSetting(). ( Ignorable by Annotation )

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

145
        $this->crudPanel->setOperationSetting('defaultSaveAction', /** @scrutinizer ignore-type */ 'save_and_new');
Loading history...
146
        $this->assertEquals('save_and_new', $this->crudPanel->getFallBackSaveAction());
147
    }
148
149
    public function testItCanRemoveAllTheSaveActions()
150
    {
151
        $this->crudPanel->setupDefaultSaveActions();
152
        $this->assertCount(3, $this->crudPanel->getOperationSetting('save_actions'));
153
        $this->crudPanel->removeAllSaveActions();
154
        $this->assertCount(0, $this->crudPanel->getOperationSetting('save_actions'));
155
    }
156
157
    public function testItCanHideSaveActions()
158
    {
159
        $this->crudPanel->allowAccess(['create', 'update', 'list']);
160
        $saveAction = $this->singleSaveAction;
161
        $saveAction['visible'] = false;
162
        $this->crudPanel->setupDefaultSaveActions();
163
        $this->crudPanel->addSaveAction($saveAction);
164
        $this->assertCount(4, $this->crudPanel->getOperationSetting('save_actions'));
165
        $this->assertCount(3, $this->crudPanel->getVisibleSaveActions());
166
    }
167
168
    public function testItCanGetSaveActionFromSession()
169
    {
170
        $this->crudPanel->allowAccess(['create', 'update', 'list']);
171
        $this->crudPanel->addSaveAction($this->singleSaveAction);
172
        $this->crudPanel->setupDefaultSaveActions();
173
        $saveActions = $this->crudPanel->getSaveAction();
174
175
        $expected = [
176
            'active' => [
177
                'value' => 'save_action_one',
178
                'label' => 'custom',
179
            ],
180
            'options' => [
181
                'save_and_back' => 'Save and back',
182
                'save_and_edit' => 'Save and edit this item',
183
                'save_and_new' => 'Save and new item',
184
            ],
185
        ];
186
        $this->assertEquals($expected, $saveActions);
187
    }
188
}
189