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