1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\Tests\Unit\CrudPanel; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\Attributes\DataProvider; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @covers Backpack\CRUD\app\Library\CrudPanel\Traits\SaveActions |
9
|
|
|
*/ |
10
|
|
|
class CrudPanelSaveActionsTest extends \Backpack\CRUD\Tests\config\CrudPanel\BaseCrudPanel |
11
|
|
|
{ |
12
|
|
|
private $singleSaveAction; |
13
|
|
|
|
14
|
|
|
private $multipleSaveActions; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Setup the test environment. |
18
|
|
|
* |
19
|
|
|
* @return void |
20
|
|
|
*/ |
21
|
|
|
protected function setUp(): void |
22
|
|
|
{ |
23
|
|
|
parent::setUp(); |
24
|
|
|
|
25
|
|
|
$this->crudPanel->setOperation('create'); |
26
|
|
|
|
27
|
|
|
$this->singleSaveAction = [ |
28
|
|
|
'name' => 'save_action_one', |
29
|
|
|
'button_text' => 'custom', |
30
|
|
|
'redirect' => function ($crud, $request, $itemId) { |
|
|
|
|
31
|
|
|
return 'https://backpackforlaravel.com'; |
32
|
|
|
}, |
33
|
|
|
'referrer_url' => function ($crud, $request, $itemId) { |
|
|
|
|
34
|
|
|
return 'https://backpackforlaravel.com'; |
35
|
|
|
}, |
36
|
|
|
'visible' => function ($crud) { |
|
|
|
|
37
|
|
|
return true; |
38
|
|
|
}, |
39
|
|
|
]; |
40
|
|
|
|
41
|
|
|
$this->multipleSaveActions = [ |
42
|
|
|
[ |
43
|
|
|
'name' => 'save_action_one', |
44
|
|
|
'redirect' => function ($crud, $request, $itemId) { |
|
|
|
|
45
|
|
|
return $crud->route; |
46
|
|
|
}, |
47
|
|
|
'visible' => function ($crud) { |
|
|
|
|
48
|
|
|
return true; |
49
|
|
|
}, |
50
|
|
|
], |
51
|
|
|
[ |
52
|
|
|
'name' => 'save_action_two', |
53
|
|
|
'redirect' => function ($crud, $request, $itemId) { |
|
|
|
|
54
|
|
|
return $crud->route; |
55
|
|
|
}, |
56
|
|
|
'visible' => function ($crud) { |
|
|
|
|
57
|
|
|
return true; |
58
|
|
|
}, |
59
|
|
|
], |
60
|
|
|
]; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function testAddDefaultSaveActions() |
64
|
|
|
{ |
65
|
|
|
$this->crudPanel->setupDefaultSaveActions(); |
66
|
|
|
$this->assertEquals(3, count($this->crudPanel->getOperationSetting('save_actions'))); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function testAddOneSaveAction() |
70
|
|
|
{ |
71
|
|
|
$this->crudPanel->setupDefaultSaveActions(); |
72
|
|
|
$this->crudPanel->addSaveAction($this->singleSaveAction); |
73
|
|
|
|
74
|
|
|
$this->assertEquals(4, count($this->crudPanel->getOperationSetting('save_actions'))); |
|
|
|
|
75
|
|
|
$this->assertEquals(['save_and_back', 'save_and_edit', 'save_and_new', 'save_action_one'], array_keys($this->crudPanel->getOperationSetting('save_actions'))); |
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function testAddMultipleSaveActions() |
79
|
|
|
{ |
80
|
|
|
$this->crudPanel->setupDefaultSaveActions(); |
81
|
|
|
$this->crudPanel->addSaveActions($this->multipleSaveActions); |
82
|
|
|
|
83
|
|
|
$this->assertEquals(5, count($this->crudPanel->getOperationSetting('save_actions'))); |
|
|
|
|
84
|
|
|
$this->assertEquals(['save_and_back', 'save_and_edit', 'save_and_new', 'save_action_one', 'save_action_two'], array_keys($this->crudPanel->getOperationSetting('save_actions'))); |
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function testRemoveOneSaveAction() |
88
|
|
|
{ |
89
|
|
|
$this->crudPanel->setupDefaultSaveActions(); |
90
|
|
|
$this->crudPanel->removeSaveAction('save_and_new'); |
91
|
|
|
$this->assertEquals(2, count($this->crudPanel->getOperationSetting('save_actions'))); |
|
|
|
|
92
|
|
|
$this->assertEquals(['save_and_back', 'save_and_edit'], array_keys($this->crudPanel->getOperationSetting('save_actions'))); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function testRemoveMultipleSaveActions() |
96
|
|
|
{ |
97
|
|
|
$this->crudPanel->setupDefaultSaveActions(); |
98
|
|
|
$this->crudPanel->removeSaveActions(['save_and_new', 'save_and_edit']); |
99
|
|
|
$this->assertEquals(1, count($this->crudPanel->getOperationSetting('save_actions'))); |
|
|
|
|
100
|
|
|
$this->assertEquals(['save_and_back'], array_keys($this->crudPanel->getOperationSetting('save_actions'))); |
|
|
|
|
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function testReplaceSaveActionsWithOneSaveAction() |
104
|
|
|
{ |
105
|
|
|
$this->crudPanel->setupDefaultSaveActions(); |
106
|
|
|
$this->crudPanel->setSaveActions($this->singleSaveAction); |
107
|
|
|
$this->assertEquals(1, count($this->crudPanel->getOperationSetting('save_actions'))); |
|
|
|
|
108
|
|
|
$this->assertEquals(['save_action_one'], array_keys($this->crudPanel->getOperationSetting('save_actions'))); |
|
|
|
|
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function testReplaceSaveActionsWithMultipleSaveActions() |
112
|
|
|
{ |
113
|
|
|
$this->crudPanel->setupDefaultSaveActions(); |
114
|
|
|
$this->crudPanel->replaceSaveActions($this->multipleSaveActions); |
115
|
|
|
$this->assertEquals(2, count($this->crudPanel->getOperationSetting('save_actions'))); |
|
|
|
|
116
|
|
|
$this->assertEquals(['save_action_one', 'save_action_two'], array_keys($this->crudPanel->getOperationSetting('save_actions'))); |
|
|
|
|
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function testOrderOneSaveAction() |
120
|
|
|
{ |
121
|
|
|
$this->crudPanel->setupDefaultSaveActions(); |
122
|
|
|
$this->crudPanel->orderSaveAction('save_and_new', 1); |
123
|
|
|
$this->assertEquals(1, $this->crudPanel->getOperationSetting('save_actions')['save_and_new']['order']); |
124
|
|
|
$this->assertEquals('save_and_new', $this->crudPanel->getFallBackSaveAction()); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function testOrderMultipleSaveActions() |
128
|
|
|
{ |
129
|
|
|
$this->crudPanel->setupDefaultSaveActions(); |
130
|
|
|
$this->crudPanel->orderSaveActions(['save_and_new', 'save_and_back']); |
131
|
|
|
$this->assertEquals(1, $this->crudPanel->getOperationSetting('save_actions')['save_and_new']['order']); |
132
|
|
|
$this->assertEquals(2, $this->crudPanel->getOperationSetting('save_actions')['save_and_back']['order']); |
133
|
|
|
$this->assertEquals(3, $this->crudPanel->getOperationSetting('save_actions')['save_and_edit']['order']); |
134
|
|
|
$this->crudPanel->orderSaveActions(['save_and_edit' => 1]); |
135
|
|
|
$this->assertEquals('save_and_edit', $this->crudPanel->getFallBackSaveAction()); |
136
|
|
|
$this->assertEquals(['save_and_edit', 'save_and_back', 'save_and_new'], array_keys($this->crudPanel->getOrderedSaveActions())); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function testItCanGetTheDefaultSaveActionForCurrentOperation() |
140
|
|
|
{ |
141
|
|
|
$this->crudPanel->setupDefaultSaveActions(); |
142
|
|
|
$saveAction = $this->crudPanel->getSaveActionDefaultForCurrentOperation(); |
143
|
|
|
$this->assertEquals('save_and_back', $saveAction); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function testItCanGetTheDefaultSaveActionFromOperationSettings() |
147
|
|
|
{ |
148
|
|
|
$this->crudPanel->setupDefaultSaveActions(); |
149
|
|
|
$this->assertEquals('save_and_back', $this->crudPanel->getFallBackSaveAction()); |
150
|
|
|
$this->crudPanel->setOperationSetting('defaultSaveAction', 'save_and_new'); |
151
|
|
|
$this->assertEquals('save_and_new', $this->crudPanel->getFallBackSaveAction()); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function testItCanRemoveAllTheSaveActions() |
155
|
|
|
{ |
156
|
|
|
$this->crudPanel->setupDefaultSaveActions(); |
157
|
|
|
$this->assertCount(3, $this->crudPanel->getOperationSetting('save_actions')); |
|
|
|
|
158
|
|
|
$this->crudPanel->removeAllSaveActions(); |
159
|
|
|
$this->assertCount(0, $this->crudPanel->getOperationSetting('save_actions')); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function testItCanHideSaveActions() |
163
|
|
|
{ |
164
|
|
|
$this->setupDefaultSaveActionsOnCrudPanel(); |
165
|
|
|
$saveAction = $this->singleSaveAction; |
166
|
|
|
$saveAction['visible'] = false; |
167
|
|
|
$this->crudPanel->addSaveAction($saveAction); |
168
|
|
|
$this->assertCount(4, $this->crudPanel->getOperationSetting('save_actions')); |
|
|
|
|
169
|
|
|
$this->assertCount(3, $this->crudPanel->getVisibleSaveActions()); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function testItCanGetSaveActionFromSession() |
173
|
|
|
{ |
174
|
|
|
$this->setupDefaultSaveActionsOnCrudPanel(); |
175
|
|
|
$this->crudPanel->addSaveAction($this->singleSaveAction); |
176
|
|
|
|
177
|
|
|
session()->put('create.saveAction', 'save_action_one'); |
178
|
|
|
|
179
|
|
|
$saveActions = $this->crudPanel->getSaveAction(); |
180
|
|
|
|
181
|
|
|
$expected = [ |
182
|
|
|
'active' => [ |
183
|
|
|
'value' => 'save_action_one', |
184
|
|
|
'label' => 'custom', |
185
|
|
|
], |
186
|
|
|
'options' => [ |
187
|
|
|
'save_and_back' => 'Save and back', |
188
|
|
|
'save_and_edit' => 'Save and edit this item', |
189
|
|
|
'save_and_new' => 'Save and new item', |
190
|
|
|
], |
191
|
|
|
]; |
192
|
|
|
$this->assertEquals($expected, $saveActions); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
public function testItGetsTheFirstSaveActionIfTheRequiredActionIsNotASaveAction() |
196
|
|
|
{ |
197
|
|
|
$this->setupDefaultSaveActionsOnCrudPanel(); |
198
|
|
|
session()->put('create.saveAction', 'not_a_save_action'); |
199
|
|
|
$this->assertEquals('save_and_back', $this->crudPanel->getSaveAction()['active']['value']); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
public function testItCanSetTheSaveActionInSessionFromRequest() |
203
|
|
|
{ |
204
|
|
|
$this->setupDefaultSaveActionsOnCrudPanel(); |
205
|
|
|
|
206
|
|
|
$this->setupUserCreateRequest(); |
207
|
|
|
|
208
|
|
|
$this->crudPanel->getRequest()->merge(['_save_action' => 'save_action_one']); |
209
|
|
|
|
210
|
|
|
$this->crudPanel->setSaveAction(); |
211
|
|
|
|
212
|
|
|
$this->assertEquals('save_action_one', session()->get('create.saveAction')); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
public function testItCanPerformTheSaveActionAndReturnTheRedirect() |
216
|
|
|
{ |
217
|
|
|
$this->setupDefaultSaveActionsOnCrudPanel(); |
218
|
|
|
|
219
|
|
|
$redirect = $this->crudPanel->performSaveAction(); |
220
|
|
|
$this->assertEquals(url('/'), $redirect->getTargetUrl()); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
public function testItCanPerformTheSaveActionAndReturnTheRedirectFromTheRequest() |
224
|
|
|
{ |
225
|
|
|
$this->setupDefaultSaveActionsOnCrudPanel(); |
226
|
|
|
|
227
|
|
|
$this->setupUserCreateRequest(); |
228
|
|
|
|
229
|
|
|
$this->crudPanel->addSaveAction($this->singleSaveAction); |
230
|
|
|
|
231
|
|
|
$this->crudPanel->getRequest()->merge(['_save_action' => 'save_action_one']); |
232
|
|
|
|
233
|
|
|
$redirect = $this->crudPanel->performSaveAction(); |
234
|
|
|
|
235
|
|
|
$this->assertEquals('https://backpackforlaravel.com', $redirect->getTargetUrl()); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
public function testItCanSetGetTheRefeererFromSaveAction() |
239
|
|
|
{ |
240
|
|
|
$this->setupDefaultSaveActionsOnCrudPanel(); |
241
|
|
|
|
242
|
|
|
$this->crudPanel->addSaveAction($this->singleSaveAction); |
243
|
|
|
|
244
|
|
|
$this->crudPanel->getRequest()->merge(['_save_action' => 'save_action_one']); |
245
|
|
|
|
246
|
|
|
$this->crudPanel->performSaveAction(); |
247
|
|
|
|
248
|
|
|
$referer = session('referrer_url_override'); |
249
|
|
|
|
250
|
|
|
$this->assertEquals('https://backpackforlaravel.com', $referer); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
public function testItCanPerformTheSaveActionAndRespondWithJson() |
254
|
|
|
{ |
255
|
|
|
$this->setupDefaultSaveActionsOnCrudPanel(); |
256
|
|
|
|
257
|
|
|
$this->crudPanel->getRequest()->headers->set('X-Requested-With', 'XMLHttpRequest'); |
258
|
|
|
|
259
|
|
|
$this->crudPanel->getRequest()->merge(['_save_action' => 'save_and_back']); |
260
|
|
|
|
261
|
|
|
$response = $this->crudPanel->performSaveAction(); |
262
|
|
|
|
263
|
|
|
$this->assertEquals('application/json', $response->headers->get('Content-Type')); |
264
|
|
|
|
265
|
|
|
$this->assertEquals([ |
266
|
|
|
'success' => true, |
267
|
|
|
'redirect_url' => null, |
268
|
|
|
'referrer_url' => false, |
269
|
|
|
'data' => null, |
270
|
|
|
], json_decode($response->getContent(), true)); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
|
274
|
|
|
#[DataProvider('saveActionsDataProvider')] |
275
|
|
|
public function testSaveActionsRedirectAndRefererUrl($action, $redirect, $referrer) |
276
|
|
|
{ |
277
|
|
|
$this->setupDefaultSaveActionsOnCrudPanel(); |
278
|
|
|
|
279
|
|
|
$this->crudPanel->getRequest()->merge(['_save_action' => $action, 'id' => 1, '_locale' => 'pt', '_current_tab' => 'tab1']); |
280
|
|
|
|
281
|
|
|
$redirectUrl = $this->crudPanel->performSaveAction(); |
282
|
|
|
|
283
|
|
|
$this->assertEquals($redirect, $redirectUrl->getTargetUrl()); |
284
|
|
|
|
285
|
|
|
$this->assertEquals($referrer, session('referrer_url_override') ?? false); |
286
|
|
|
|
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
public static function saveActionsDataProvider() |
290
|
|
|
{ |
291
|
|
|
return [ |
292
|
|
|
[ |
293
|
|
|
'action' => 'save_and_back', |
294
|
|
|
'redirect' => 'http://localhost', |
295
|
|
|
'referrer' => false, |
296
|
|
|
], |
297
|
|
|
[ |
298
|
|
|
'action' => 'save_and_edit', |
299
|
|
|
'redirect' => 'http://localhost/1/edit?_locale=pt#tab1', |
300
|
|
|
'referrer' => 'http://localhost/1/edit', |
301
|
|
|
], |
302
|
|
|
[ |
303
|
|
|
'action' => 'save_and_new', |
304
|
|
|
'redirect' => 'http://localhost/create', |
305
|
|
|
'referrer' => false, |
306
|
|
|
], |
307
|
|
|
]; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
private function setupDefaultSaveActionsOnCrudPanel() |
311
|
|
|
{ |
312
|
|
|
$this->crudPanel->allowAccess(['create', 'update', 'list']); |
313
|
|
|
$this->crudPanel->setupDefaultSaveActions(); |
314
|
|
|
} |
315
|
|
|
} |
316
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.