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\User; |
7
|
|
|
|
8
|
|
|
class CrudPanelColumnsTest extends BaseDBCrudPanelTest |
9
|
|
|
{ |
10
|
|
|
private $oneColumnArray = [ |
11
|
|
|
'name' => 'column1', |
12
|
|
|
'label' => 'Column1', |
13
|
|
|
]; |
14
|
|
|
|
15
|
|
|
private $expectedOneColumnArray = [ |
16
|
|
|
'column1' => [ |
17
|
|
|
'label' => 'Column1', |
18
|
|
|
'name' => 'column1', |
19
|
|
|
'key' => 'column1', |
20
|
|
|
'type' => 'text', |
21
|
|
|
'tableColumn' => false, |
22
|
|
|
'orderable' => false, |
23
|
|
|
'searchLogic' => false, |
24
|
|
|
'priority' => 0, |
25
|
|
|
], |
26
|
|
|
]; |
27
|
|
|
|
28
|
|
|
private $otherOneColumnArray = [ |
29
|
|
|
'name' => 'column4', |
30
|
|
|
'label' => 'Column4', |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
private $twoColumnsArray = [ |
34
|
|
|
[ |
35
|
|
|
'name' => 'column1', |
36
|
|
|
'label' => 'Column1', |
37
|
|
|
], |
38
|
|
|
[ |
39
|
|
|
'name' => 'column2', |
40
|
|
|
'label' => 'Column2', |
41
|
|
|
], |
42
|
|
|
]; |
43
|
|
|
|
44
|
|
|
private $expectedTwoColumnsArray = [ |
45
|
|
|
'column1' => [ |
46
|
|
|
'name' => 'column1', |
47
|
|
|
'key' => 'column1', |
48
|
|
|
'label' => 'Column1', |
49
|
|
|
'type' => 'text', |
50
|
|
|
'tableColumn' => false, |
51
|
|
|
'orderable' => false, |
52
|
|
|
'searchLogic' => false, |
53
|
|
|
'priority' => 0, |
54
|
|
|
|
55
|
|
|
], |
56
|
|
|
'column2' => [ |
57
|
|
|
'name' => 'column2', |
58
|
|
|
'key' => 'column2', |
59
|
|
|
'label' => 'Column2', |
60
|
|
|
'type' => 'text', |
61
|
|
|
'tableColumn' => false, |
62
|
|
|
'orderable' => false, |
63
|
|
|
'searchLogic' => false, |
64
|
|
|
'priority' => 1, |
65
|
|
|
], |
66
|
|
|
]; |
67
|
|
|
|
68
|
|
|
private $threeColumnsArray = [ |
69
|
|
|
[ |
70
|
|
|
'name' => 'column1', |
71
|
|
|
'label' => 'Column1', |
72
|
|
|
], |
73
|
|
|
[ |
74
|
|
|
'name' => 'column2', |
75
|
|
|
'label' => 'Column2', |
76
|
|
|
], |
77
|
|
|
[ |
78
|
|
|
'name' => 'column3', |
79
|
|
|
'label' => 'Column3', |
80
|
|
|
], |
81
|
|
|
]; |
82
|
|
|
|
83
|
|
|
private $expectedThreeColumnsArray = [ |
84
|
|
|
'column1' => [ |
85
|
|
|
'name' => 'column1', |
86
|
|
|
'key' => 'column1', |
87
|
|
|
'label' => 'Column1', |
88
|
|
|
'type' => 'text', |
89
|
|
|
'tableColumn' => false, |
90
|
|
|
'orderable' => false, |
91
|
|
|
'searchLogic' => false, |
92
|
|
|
'priority' => 0, |
93
|
|
|
], |
94
|
|
|
'column2' => [ |
95
|
|
|
'name' => 'column2', |
96
|
|
|
'key' => 'column2', |
97
|
|
|
'label' => 'Column2', |
98
|
|
|
'type' => 'text', |
99
|
|
|
'tableColumn' => false, |
100
|
|
|
'orderable' => false, |
101
|
|
|
'searchLogic' => false, |
102
|
|
|
'priority' => 1, |
103
|
|
|
], |
104
|
|
|
'column3' => [ |
105
|
|
|
'name' => 'column3', |
106
|
|
|
'key' => 'column3', |
107
|
|
|
'label' => 'Column3', |
108
|
|
|
'type' => 'text', |
109
|
|
|
'tableColumn' => false, |
110
|
|
|
'orderable' => false, |
111
|
|
|
'searchLogic' => false, |
112
|
|
|
'priority' => 2, |
113
|
|
|
], |
114
|
|
|
]; |
115
|
|
|
|
116
|
|
|
private $expectedRelationColumnsArray = [ |
117
|
|
|
'accountDetails' => [ |
118
|
|
|
'name' => 'accountDetails', |
119
|
|
|
'label' => 'AccountDetails', |
120
|
|
|
'type' => 'relationship', |
121
|
|
|
'key' => 'accountDetails', |
122
|
|
|
'priority' => 0, |
123
|
|
|
'tableColumn' => false, |
124
|
|
|
'orderable' => false, |
125
|
|
|
'searchLogic' => false, |
126
|
|
|
'entity' => 'accountDetails', |
127
|
|
|
'model' => 'Backpack\CRUD\Tests\Unit\Models\AccountDetails', |
128
|
|
|
], |
129
|
|
|
'accountDetails__nickname' => [ |
130
|
|
|
'name' => 'accountDetails.nickname', |
131
|
|
|
'label' => 'AccountDetails.nickname', |
132
|
|
|
'type' => 'text', |
133
|
|
|
'key' => 'accountDetails__nickname', |
134
|
|
|
'priority' => 1, |
135
|
|
|
'tableColumn' => false, |
136
|
|
|
'orderable' => false, |
137
|
|
|
'searchLogic' => false, |
138
|
|
|
], |
139
|
|
|
'accountDetails__user' => [ |
140
|
|
|
'name' => 'accountDetails.user', |
141
|
|
|
'label' => 'AccountDetails.user', |
142
|
|
|
'type' => 'text', |
143
|
|
|
'key' => 'accountDetails__user', |
144
|
|
|
'priority' => 2, |
145
|
|
|
'tableColumn' => false, |
146
|
|
|
'orderable' => false, |
147
|
|
|
'searchLogic' => false, |
148
|
|
|
], |
149
|
|
|
]; |
150
|
|
|
|
151
|
|
|
private $relationColumnArray = [ |
152
|
|
|
'name' => 'nickname', |
153
|
|
|
'type' => 'select', |
154
|
|
|
'entity' => 'accountDetails', |
155
|
|
|
'attribute' => 'nickname', |
156
|
|
|
]; |
157
|
|
|
|
158
|
|
|
private $expectedRelationColumnArray = [ |
159
|
|
|
'nickname' => [ |
160
|
|
|
'name' => 'nickname', |
161
|
|
|
'type' => 'select', |
162
|
|
|
'entity' => 'accountDetails', |
163
|
|
|
'attribute' => 'nickname', |
164
|
|
|
'label' => 'Nickname', |
165
|
|
|
'model' => 'Backpack\CRUD\Tests\Unit\Models\AccountDetails', |
166
|
|
|
'key' => 'nickname', |
167
|
|
|
'tableColumn' => false, |
168
|
|
|
'orderable' => false, |
169
|
|
|
'searchLogic' => false, |
170
|
|
|
'priority' => 0, |
171
|
|
|
], |
172
|
|
|
]; |
173
|
|
|
|
174
|
|
|
private $nestedRelationColumnArray = [ |
175
|
|
|
'name' => 'nickname', |
176
|
|
|
'type' => 'select', |
177
|
|
|
'entity' => 'user.accountDetails', |
178
|
|
|
'attribute' => 'nickname', |
179
|
|
|
]; |
180
|
|
|
|
181
|
|
|
private $expectedNestedRelationColumnArray = [ |
182
|
|
|
'nickname' => [ |
183
|
|
|
'name' => 'nickname', |
184
|
|
|
'type' => 'select', |
185
|
|
|
'entity' => 'user.accountDetails', |
186
|
|
|
'attribute' => 'nickname', |
187
|
|
|
'label' => 'Nickname', |
188
|
|
|
'model' => 'Backpack\CRUD\Tests\Unit\Models\AccountDetails', |
189
|
|
|
'key' => 'nickname', |
190
|
|
|
'tableColumn' => false, |
191
|
|
|
'orderable' => false, |
192
|
|
|
'searchLogic' => false, |
193
|
|
|
'priority' => 0, |
194
|
|
|
], |
195
|
|
|
]; |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Setup the test environment. |
199
|
|
|
* |
200
|
|
|
* @return void |
201
|
|
|
*/ |
202
|
|
|
protected function setUp(): void |
203
|
|
|
{ |
204
|
|
|
parent::setUp(); |
205
|
|
|
|
206
|
|
|
$this->crudPanel->setOperation('list'); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
public function testAddColumnByName() |
210
|
|
|
{ |
211
|
|
|
$this->crudPanel->addColumn('column1'); |
212
|
|
|
|
213
|
|
|
$this->assertEquals($this->expectedOneColumnArray, $this->crudPanel->columns()); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
public function testAddColumnsByName() |
217
|
|
|
{ |
218
|
|
|
$this->crudPanel->addColumns(['column1', 'column2']); |
219
|
|
|
|
220
|
|
|
$this->assertEquals(2, count($this->crudPanel->columns())); |
221
|
|
|
$this->assertEquals($this->expectedTwoColumnsArray, $this->crudPanel->columns()); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
public function testAddColumnAsArray() |
225
|
|
|
{ |
226
|
|
|
$this->crudPanel->addColumn($this->oneColumnArray); |
227
|
|
|
|
228
|
|
|
$this->assertEquals($this->expectedOneColumnArray, $this->crudPanel->columns()); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
public function testAddColumnsAsArray() |
232
|
|
|
{ |
233
|
|
|
$this->crudPanel->addColumns($this->twoColumnsArray); |
234
|
|
|
|
235
|
|
|
$this->assertEquals(2, count($this->crudPanel->columns())); |
236
|
|
|
$this->assertEquals($this->expectedTwoColumnsArray, $this->crudPanel->columns()); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
public function testAddColumnNotArray() |
240
|
|
|
{ |
241
|
|
|
$this->expectException(\ErrorException::class); |
242
|
|
|
|
243
|
|
|
$this->crudPanel->addColumns('column1'); |
|
|
|
|
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
public function testAddRelationsByName() |
247
|
|
|
{ |
248
|
|
|
$this->crudPanel->setModel(User::class); |
249
|
|
|
$this->crudPanel->addColumn('accountDetails'); |
250
|
|
|
$this->crudPanel->addColumn('accountDetails.nickname'); |
251
|
|
|
$this->crudPanel->addColumn('accountDetails.user'); |
252
|
|
|
|
253
|
|
|
$this->assertEquals($this->expectedRelationColumnsArray, $this->crudPanel->columns()); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
public function testAddRelationColumn() |
257
|
|
|
{ |
258
|
|
|
$this->crudPanel->setModel(User::class); |
259
|
|
|
$this->crudPanel->addColumn($this->relationColumnArray); |
260
|
|
|
|
261
|
|
|
$this->assertEquals($this->expectedRelationColumnArray, $this->crudPanel->columns()); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
public function testAddNestedRelationColumn() |
265
|
|
|
{ |
266
|
|
|
$this->crudPanel->setModel(Article::class); |
267
|
|
|
$this->crudPanel->addColumn($this->nestedRelationColumnArray); |
268
|
|
|
|
269
|
|
|
$this->assertEquals($this->expectedNestedRelationColumnArray, $this->crudPanel->columns()); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
public function testMoveColumnBefore() |
273
|
|
|
{ |
274
|
|
|
$this->crudPanel->addColumns($this->twoColumnsArray); |
275
|
|
|
|
276
|
|
|
$this->crudPanel->beforeColumn('column1'); |
277
|
|
|
|
278
|
|
|
$keys = array_keys($this->crudPanel->columns()); |
279
|
|
|
$this->assertEquals($this->expectedTwoColumnsArray['column2'], $this->crudPanel->columns()[$keys[0]]); |
280
|
|
|
$this->assertEquals(['column2', 'column1'], $keys); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
public function testMoveColumnBeforeUnknownColumnName() |
284
|
|
|
{ |
285
|
|
|
$this->crudPanel->addColumns($this->twoColumnsArray); |
286
|
|
|
|
287
|
|
|
$this->crudPanel->beforeColumn('column3'); |
288
|
|
|
|
289
|
|
|
$this->assertEquals(array_keys($this->expectedTwoColumnsArray), array_keys($this->crudPanel->columns())); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
public function testMoveColumnAfter() |
293
|
|
|
{ |
294
|
|
|
$this->crudPanel->addColumns($this->threeColumnsArray); |
295
|
|
|
|
296
|
|
|
$this->crudPanel->afterColumn('column1'); |
297
|
|
|
|
298
|
|
|
$keys = array_keys($this->crudPanel->columns()); |
299
|
|
|
$this->assertEquals($this->expectedThreeColumnsArray['column3'], $this->crudPanel->columns()[$keys[1]]); |
300
|
|
|
$this->assertEquals(['column1', 'column3', 'column2'], $keys); |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
public function testMoveColumnAfterUnknownColumnName() |
304
|
|
|
{ |
305
|
|
|
$this->crudPanel->addColumns($this->twoColumnsArray); |
306
|
|
|
|
307
|
|
|
$this->crudPanel->afterColumn('column3'); |
308
|
|
|
|
309
|
|
|
$this->assertEquals(array_keys($this->expectedTwoColumnsArray), array_keys($this->crudPanel->columns())); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
public function testRemoveColumnByName() |
313
|
|
|
{ |
314
|
|
|
$this->crudPanel->addColumns(['column1', 'column2', 'column3']); |
315
|
|
|
|
316
|
|
|
$this->crudPanel->removeColumn('column1'); |
317
|
|
|
|
318
|
|
|
$this->assertEquals(2, count($this->crudPanel->columns())); |
319
|
|
|
$this->assertEquals(['column2', 'column3'], array_keys($this->crudPanel->columns())); |
320
|
|
|
$this->assertNotContains($this->oneColumnArray, $this->crudPanel->columns()); |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
public function testRemoveUnknownColumnName() |
324
|
|
|
{ |
325
|
|
|
$unknownColumnName = 'column4'; |
326
|
|
|
$this->crudPanel->addColumns(['column1', 'column2', 'column3']); |
327
|
|
|
|
328
|
|
|
$this->crudPanel->removeColumn($unknownColumnName); |
329
|
|
|
|
330
|
|
|
$this->assertEquals(3, count($this->crudPanel->columns())); |
331
|
|
|
$this->assertEquals(['column1', 'column2', 'column3'], array_keys($this->crudPanel->columns())); |
332
|
|
|
$this->assertNotContains($this->otherOneColumnArray, $this->crudPanel->columns()); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
public function testRemoveColumnsByName() |
336
|
|
|
{ |
337
|
|
|
$this->crudPanel->addColumns(['column1', 'column2', 'column3']); |
338
|
|
|
|
339
|
|
|
$this->crudPanel->removeColumns($this->twoColumnsArray); |
340
|
|
|
|
341
|
|
|
$this->assertEquals(1, count($this->crudPanel->columns())); |
342
|
|
|
$this->assertEquals(['column3'], array_keys($this->crudPanel->columns())); |
343
|
|
|
$this->assertNotEquals($this->expectedThreeColumnsArray, $this->crudPanel->columns()); |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
public function testRemoveUnknownColumnsByName() |
347
|
|
|
{ |
348
|
|
|
$unknownColumnNames = ['column4', 'column5']; |
349
|
|
|
$this->crudPanel->addColumns(['column1', 'column2', 'column3']); |
350
|
|
|
|
351
|
|
|
$this->crudPanel->removeColumns($unknownColumnNames); |
352
|
|
|
|
353
|
|
|
$this->assertEquals(3, count($this->crudPanel->columns())); |
354
|
|
|
$this->assertEquals(['column1', 'column2', 'column3'], array_keys($this->crudPanel->columns())); |
355
|
|
|
$this->assertNotContains($this->otherOneColumnArray, $this->crudPanel->columns()); |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
public function testSetColumnDetails() |
359
|
|
|
{ |
360
|
|
|
$this->markTestIncomplete('Not correctly implemented'); |
361
|
|
|
|
362
|
|
|
// TODO: refactor crud panel sync method |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
public function testSetColumnsDetails() |
366
|
|
|
{ |
367
|
|
|
$this->markTestIncomplete('Not correctly implemented'); |
368
|
|
|
|
369
|
|
|
// TODO: refactor crud panel sync method |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
public function testOrderColumns() |
373
|
|
|
{ |
374
|
|
|
$this->crudPanel->addColumns($this->threeColumnsArray); |
375
|
|
|
|
376
|
|
|
$this->crudPanel->orderColumns(['column2', 'column1', 'column3']); |
377
|
|
|
|
378
|
|
|
$this->assertEquals(['column2', 'column1', 'column3'], array_keys($this->crudPanel->columns())); |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
public function testOrderColumnsIncompleteList() |
382
|
|
|
{ |
383
|
|
|
$this->crudPanel->addColumns($this->threeColumnsArray); |
384
|
|
|
|
385
|
|
|
$this->crudPanel->orderColumns(['column2', 'column3']); |
386
|
|
|
|
387
|
|
|
$this->assertEquals(['column2', 'column3', 'column1'], array_keys($this->crudPanel->columns())); |
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
public function testOrderColumnsEmptyList() |
391
|
|
|
{ |
392
|
|
|
$this->crudPanel->addColumns($this->threeColumnsArray); |
393
|
|
|
|
394
|
|
|
$this->crudPanel->orderColumns([]); |
395
|
|
|
|
396
|
|
|
$this->assertEquals($this->expectedThreeColumnsArray, $this->crudPanel->columns()); |
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
public function testOrderColumnsUnknownList() |
400
|
|
|
{ |
401
|
|
|
$this->crudPanel->addColumns($this->threeColumnsArray); |
402
|
|
|
|
403
|
|
|
$this->crudPanel->orderColumns(['column4', 'column5', 'column6']); |
404
|
|
|
|
405
|
|
|
$this->assertEquals($this->expectedThreeColumnsArray, $this->crudPanel->columns()); |
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
public function testOrderColumnsMixedList() |
409
|
|
|
{ |
410
|
|
|
$this->crudPanel->addColumns($this->threeColumnsArray); |
411
|
|
|
|
412
|
|
|
$this->crudPanel->orderColumns(['column2', 'column5', 'column6']); |
413
|
|
|
|
414
|
|
|
$this->assertEquals(['column2', 'column1', 'column3'], array_keys($this->crudPanel->columns())); |
415
|
|
|
} |
416
|
|
|
} |
417
|
|
|
|