1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\Tests\Unit\CrudPanel; |
4
|
|
|
|
5
|
|
|
use Backpack\CRUD\app\Library\CrudPanel\CrudColumn; |
6
|
|
|
use Backpack\CRUD\Tests\config\Models\Article; |
7
|
|
|
use Backpack\CRUD\Tests\config\Models\User; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @covers Backpack\CRUD\app\Library\CrudPanel\Traits\Columns |
11
|
|
|
* @covers Backpack\CRUD\app\Library\CrudPanel\Traits\ColumnsProtectedMethods |
12
|
|
|
* @covers Backpack\CRUD\app\Library\CrudPanel\CrudColumn |
13
|
|
|
* @covers Backpack\CRUD\app\Library\CrudPanel\CrudPanel |
14
|
|
|
*/ |
15
|
|
|
class CrudPanelColumnsTest extends \Backpack\CRUD\Tests\config\CrudPanel\BaseDBCrudPanel |
16
|
|
|
{ |
17
|
|
|
private $oneColumnArray = [ |
18
|
|
|
'name' => 'column1', |
19
|
|
|
'label' => 'Column1', |
20
|
|
|
]; |
21
|
|
|
|
22
|
|
|
private $expectedOneColumnArray = [ |
23
|
|
|
'column1' => [ |
24
|
|
|
'label' => 'Column1', |
25
|
|
|
'name' => 'column1', |
26
|
|
|
'key' => 'column1', |
27
|
|
|
'type' => 'text', |
28
|
|
|
'tableColumn' => false, |
29
|
|
|
'orderable' => false, |
30
|
|
|
'searchLogic' => false, |
31
|
|
|
'priority' => 0, |
32
|
|
|
], |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
private $otherOneColumnArray = [ |
36
|
|
|
'name' => 'column4', |
37
|
|
|
'label' => 'Column4', |
38
|
|
|
]; |
39
|
|
|
|
40
|
|
|
private $twoColumnsArray = [ |
41
|
|
|
[ |
42
|
|
|
'name' => 'column1', |
43
|
|
|
'label' => 'Column1', |
44
|
|
|
], |
45
|
|
|
[ |
46
|
|
|
'name' => 'column2', |
47
|
|
|
'label' => 'Column2', |
48
|
|
|
], |
49
|
|
|
]; |
50
|
|
|
|
51
|
|
|
private $expectedTwoColumnsArray = [ |
52
|
|
|
'column1' => [ |
53
|
|
|
'name' => 'column1', |
54
|
|
|
'key' => 'column1', |
55
|
|
|
'label' => 'Column1', |
56
|
|
|
'type' => 'text', |
57
|
|
|
'tableColumn' => false, |
58
|
|
|
'orderable' => false, |
59
|
|
|
'searchLogic' => false, |
60
|
|
|
'priority' => 0, |
61
|
|
|
|
62
|
|
|
], |
63
|
|
|
'column2' => [ |
64
|
|
|
'name' => 'column2', |
65
|
|
|
'key' => 'column2', |
66
|
|
|
'label' => 'Column2', |
67
|
|
|
'type' => 'text', |
68
|
|
|
'tableColumn' => false, |
69
|
|
|
'orderable' => false, |
70
|
|
|
'searchLogic' => false, |
71
|
|
|
'priority' => 1, |
72
|
|
|
], |
73
|
|
|
]; |
74
|
|
|
|
75
|
|
|
private $threeColumnsArray = [ |
76
|
|
|
[ |
77
|
|
|
'name' => 'column1', |
78
|
|
|
'label' => 'Column1', |
79
|
|
|
], |
80
|
|
|
[ |
81
|
|
|
'name' => 'column2', |
82
|
|
|
'label' => 'Column2', |
83
|
|
|
], |
84
|
|
|
[ |
85
|
|
|
'name' => 'column3', |
86
|
|
|
'label' => 'Column3', |
87
|
|
|
], |
88
|
|
|
]; |
89
|
|
|
|
90
|
|
|
private $expectedThreeColumnsArray = [ |
91
|
|
|
'column1' => [ |
92
|
|
|
'name' => 'column1', |
93
|
|
|
'key' => 'column1', |
94
|
|
|
'label' => 'Column1', |
95
|
|
|
'type' => 'text', |
96
|
|
|
'tableColumn' => false, |
97
|
|
|
'orderable' => false, |
98
|
|
|
'searchLogic' => false, |
99
|
|
|
'priority' => 0, |
100
|
|
|
], |
101
|
|
|
'column2' => [ |
102
|
|
|
'name' => 'column2', |
103
|
|
|
'key' => 'column2', |
104
|
|
|
'label' => 'Column2', |
105
|
|
|
'type' => 'text', |
106
|
|
|
'tableColumn' => false, |
107
|
|
|
'orderable' => false, |
108
|
|
|
'searchLogic' => false, |
109
|
|
|
'priority' => 1, |
110
|
|
|
], |
111
|
|
|
'column3' => [ |
112
|
|
|
'name' => 'column3', |
113
|
|
|
'key' => 'column3', |
114
|
|
|
'label' => 'Column3', |
115
|
|
|
'type' => 'text', |
116
|
|
|
'tableColumn' => false, |
117
|
|
|
'orderable' => false, |
118
|
|
|
'searchLogic' => false, |
119
|
|
|
'priority' => 2, |
120
|
|
|
], |
121
|
|
|
]; |
122
|
|
|
|
123
|
|
|
private $expectedRelationColumnsArrayWithoutPro = [ |
124
|
|
|
'accountDetails' => [ |
125
|
|
|
'name' => 'accountDetails', |
126
|
|
|
'label' => 'AccountDetails', |
127
|
|
|
'type' => 'text', |
128
|
|
|
'key' => 'accountDetails', |
129
|
|
|
'priority' => 0, |
130
|
|
|
'tableColumn' => false, |
131
|
|
|
'orderable' => false, |
132
|
|
|
'searchLogic' => false, |
133
|
|
|
'entity' => 'accountDetails', |
134
|
|
|
'model' => 'Backpack\CRUD\Tests\Config\Models\AccountDetails', |
135
|
|
|
'relation_type' => 'HasOne', |
136
|
|
|
'attribute' => 'nickname', |
137
|
|
|
], |
138
|
|
|
'accountDetails__nickname' => [ |
139
|
|
|
'name' => 'accountDetails.nickname', |
140
|
|
|
'label' => 'AccountDetails.nickname', |
141
|
|
|
'type' => 'text', |
142
|
|
|
'key' => 'accountDetails__nickname', |
143
|
|
|
'priority' => 1, |
144
|
|
|
'attribute' => 'nickname', |
145
|
|
|
'tableColumn' => false, |
146
|
|
|
'orderable' => false, |
147
|
|
|
'searchLogic' => false, |
148
|
|
|
'relation_type' => 'HasOne', |
149
|
|
|
'entity' => 'accountDetails.nickname', |
150
|
|
|
'model' => 'Backpack\CRUD\Tests\Config\Models\AccountDetails', |
151
|
|
|
], |
152
|
|
|
'accountDetails__user' => [ |
153
|
|
|
'name' => 'accountDetails.user', |
154
|
|
|
'label' => 'AccountDetails.user', |
155
|
|
|
'type' => 'select', |
156
|
|
|
'key' => 'accountDetails__user', |
157
|
|
|
'priority' => 2, |
158
|
|
|
'tableColumn' => false, |
159
|
|
|
'orderable' => false, |
160
|
|
|
'searchLogic' => false, |
161
|
|
|
'relation_type' => 'BelongsTo', |
162
|
|
|
'entity' => 'accountDetails.user', |
163
|
|
|
'model' => 'Backpack\CRUD\Tests\Config\Models\User', |
164
|
|
|
'attribute' => 'name', |
165
|
|
|
], |
166
|
|
|
]; |
167
|
|
|
|
168
|
|
|
private $expectedRelationColumnsArrayWithPro = [ |
169
|
|
|
'accountDetails' => [ |
170
|
|
|
'name' => 'accountDetails', |
171
|
|
|
'label' => 'AccountDetails', |
172
|
|
|
'type' => 'relationship', |
173
|
|
|
'key' => 'accountDetails', |
174
|
|
|
'priority' => 0, |
175
|
|
|
'tableColumn' => false, |
176
|
|
|
'orderable' => false, |
177
|
|
|
'searchLogic' => false, |
178
|
|
|
'entity' => 'accountDetails', |
179
|
|
|
'model' => 'Backpack\CRUD\Tests\Config\Models\AccountDetails', |
180
|
|
|
'relation_type' => 'HasOne', |
181
|
|
|
'attribute' => 'nickname', |
182
|
|
|
], |
183
|
|
|
'accountDetails__nickname' => [ |
184
|
|
|
'name' => 'accountDetails.nickname', |
185
|
|
|
'label' => 'AccountDetails.nickname', |
186
|
|
|
'type' => 'relationship', |
187
|
|
|
'key' => 'accountDetails__nickname', |
188
|
|
|
'priority' => 1, |
189
|
|
|
'attribute' => 'nickname', |
190
|
|
|
'tableColumn' => false, |
191
|
|
|
'orderable' => false, |
192
|
|
|
'searchLogic' => false, |
193
|
|
|
'relation_type' => 'HasOne', |
194
|
|
|
'entity' => 'accountDetails.nickname', |
195
|
|
|
'model' => 'Backpack\CRUD\Tests\Config\Models\AccountDetails', |
196
|
|
|
], |
197
|
|
|
'accountDetails__user' => [ |
198
|
|
|
'name' => 'accountDetails.user', |
199
|
|
|
'label' => 'AccountDetails.user', |
200
|
|
|
'type' => 'relationship', |
201
|
|
|
'key' => 'accountDetails__user', |
202
|
|
|
'priority' => 2, |
203
|
|
|
'tableColumn' => false, |
204
|
|
|
'orderable' => false, |
205
|
|
|
'searchLogic' => false, |
206
|
|
|
'relation_type' => 'BelongsTo', |
207
|
|
|
'entity' => 'accountDetails.user', |
208
|
|
|
'model' => 'Backpack\CRUD\Tests\Config\Models\User', |
209
|
|
|
'attribute' => 'name', |
210
|
|
|
], |
211
|
|
|
]; |
212
|
|
|
|
213
|
|
|
private $relationColumnArray = [ |
214
|
|
|
'name' => 'nickname', |
215
|
|
|
'type' => 'select', |
216
|
|
|
'entity' => 'accountDetails', |
217
|
|
|
'attribute' => 'nickname', |
218
|
|
|
]; |
219
|
|
|
|
220
|
|
|
private $expectedRelationColumnArray = [ |
221
|
|
|
'nickname' => [ |
222
|
|
|
'name' => 'nickname', |
223
|
|
|
'type' => 'select', |
224
|
|
|
'entity' => 'accountDetails', |
225
|
|
|
'attribute' => 'nickname', |
226
|
|
|
'label' => 'Nickname', |
227
|
|
|
'model' => 'Backpack\CRUD\Tests\Config\Models\AccountDetails', |
228
|
|
|
'key' => 'nickname', |
229
|
|
|
'tableColumn' => false, |
230
|
|
|
'orderable' => false, |
231
|
|
|
'searchLogic' => false, |
232
|
|
|
'priority' => 0, |
233
|
|
|
'relation_type' => 'HasOne', |
234
|
|
|
], |
235
|
|
|
]; |
236
|
|
|
|
237
|
|
|
private $nestedRelationColumnArray = [ |
238
|
|
|
'name' => 'accountDetails.article', |
239
|
|
|
]; |
240
|
|
|
|
241
|
|
|
private $secondNestedRelationColumnArray = [ |
242
|
|
|
'name' => 'accountDetails.article', |
243
|
|
|
'attribute' => 'content', |
244
|
|
|
'key' => 'ac_article_content', |
245
|
|
|
]; |
246
|
|
|
|
247
|
|
|
private $expectedNestedRelationColumnArrayWithPro = [ |
248
|
|
|
'accountDetails__article' => [ |
249
|
|
|
'name' => 'accountDetails.article', |
250
|
|
|
'type' => 'relationship', |
251
|
|
|
'entity' => 'accountDetails.article', |
252
|
|
|
'label' => 'AccountDetails.article', |
253
|
|
|
'model' => 'Backpack\CRUD\Tests\Config\Models\Article', |
254
|
|
|
'key' => 'accountDetails__article', |
255
|
|
|
'tableColumn' => false, |
256
|
|
|
'orderable' => false, |
257
|
|
|
'searchLogic' => false, |
258
|
|
|
'priority' => 0, |
259
|
|
|
'relation_type' => 'BelongsTo', |
260
|
|
|
'attribute' => 'content', |
261
|
|
|
], |
262
|
|
|
'ac_article_content' => [ |
263
|
|
|
'name' => 'accountDetails.article', |
264
|
|
|
'type' => 'relationship', |
265
|
|
|
'entity' => 'accountDetails.article', |
266
|
|
|
'label' => 'AccountDetails.article', |
267
|
|
|
'model' => 'Backpack\CRUD\Tests\Config\Models\Article', |
268
|
|
|
'key' => 'ac_article_content', |
269
|
|
|
'tableColumn' => false, |
270
|
|
|
'orderable' => false, |
271
|
|
|
'searchLogic' => false, |
272
|
|
|
'priority' => 1, |
273
|
|
|
'relation_type' => 'BelongsTo', |
274
|
|
|
'attribute' => 'content', |
275
|
|
|
], |
276
|
|
|
]; |
277
|
|
|
|
278
|
|
|
private $expectedNestedRelationColumnArrayWithoutPro = [ |
279
|
|
|
'accountDetails__article' => [ |
280
|
|
|
'name' => 'accountDetails.article', |
281
|
|
|
'type' => 'select', |
282
|
|
|
'entity' => 'accountDetails.article', |
283
|
|
|
'label' => 'AccountDetails.article', |
284
|
|
|
'model' => 'Backpack\CRUD\Tests\Config\Models\Article', |
285
|
|
|
'key' => 'accountDetails__article', |
286
|
|
|
'tableColumn' => false, |
287
|
|
|
'orderable' => false, |
288
|
|
|
'searchLogic' => false, |
289
|
|
|
'priority' => 0, |
290
|
|
|
'relation_type' => 'BelongsTo', |
291
|
|
|
'attribute' => 'content', |
292
|
|
|
], |
293
|
|
|
'ac_article_content' => [ |
294
|
|
|
'name' => 'accountDetails.article', |
295
|
|
|
'type' => 'select', |
296
|
|
|
'entity' => 'accountDetails.article', |
297
|
|
|
'label' => 'AccountDetails.article', |
298
|
|
|
'model' => 'Backpack\CRUD\Tests\Config\Models\Article', |
299
|
|
|
'key' => 'ac_article_content', |
300
|
|
|
'tableColumn' => false, |
301
|
|
|
'orderable' => false, |
302
|
|
|
'searchLogic' => false, |
303
|
|
|
'priority' => 1, |
304
|
|
|
'relation_type' => 'BelongsTo', |
305
|
|
|
'attribute' => 'content', |
306
|
|
|
], |
307
|
|
|
]; |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* Setup the test environment. |
311
|
|
|
* |
312
|
|
|
* @return void |
313
|
|
|
*/ |
314
|
|
|
protected function setUp(): void |
315
|
|
|
{ |
316
|
|
|
parent::setUp(); |
317
|
|
|
|
318
|
|
|
$this->crudPanel->setOperation('list'); |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
public function testAddColumnByName() |
322
|
|
|
{ |
323
|
|
|
$this->crudPanel->addColumn('column1'); |
324
|
|
|
|
325
|
|
|
$this->assertEquals($this->expectedOneColumnArray, $this->crudPanel->columns()); |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
public function testAddColumnsByName() |
329
|
|
|
{ |
330
|
|
|
$this->crudPanel->addColumns(['column1', 'column2']); |
331
|
|
|
|
332
|
|
|
$this->assertEquals(2, count($this->crudPanel->columns())); |
333
|
|
|
$this->assertEquals($this->expectedTwoColumnsArray, $this->crudPanel->columns()); |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
public function testAddColumnAsArray() |
337
|
|
|
{ |
338
|
|
|
$this->crudPanel->addColumn($this->oneColumnArray); |
339
|
|
|
|
340
|
|
|
$this->assertEquals($this->expectedOneColumnArray, $this->crudPanel->columns()); |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
public function testAddColumnsAsArray() |
344
|
|
|
{ |
345
|
|
|
$this->crudPanel->addColumns($this->twoColumnsArray); |
346
|
|
|
|
347
|
|
|
$this->assertEquals(2, count($this->crudPanel->columns())); |
348
|
|
|
$this->assertEquals($this->expectedTwoColumnsArray, $this->crudPanel->columns()); |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
public function testAddColumnNotArray() |
352
|
|
|
{ |
353
|
|
|
$this->expectException(PHP_MAJOR_VERSION == 7 ? \ErrorException::class : \TypeError::class); |
354
|
|
|
// Why? When calling count() on a non-countable entity, |
355
|
|
|
// PHP 7.x will through ErrorException, but PHP 8.x will throw TypeError. |
356
|
|
|
|
357
|
|
|
$this->crudPanel->addColumns('column1'); |
|
|
|
|
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
public function testAddRelationsByName() |
361
|
|
|
{ |
362
|
|
|
$this->crudPanel->setModel(User::class); |
363
|
|
|
$this->crudPanel->addColumn('accountDetails'); |
364
|
|
|
$this->crudPanel->addColumn('accountDetails.nickname'); |
365
|
|
|
$this->crudPanel->addColumn('accountDetails.user'); |
366
|
|
|
|
367
|
|
|
if (backpack_pro()) { |
368
|
|
|
$this->assertEquals($this->expectedRelationColumnsArrayWithPro, $this->crudPanel->columns()); |
369
|
|
|
} else { |
370
|
|
|
$this->assertEquals($this->expectedRelationColumnsArrayWithoutPro, $this->crudPanel->columns()); |
371
|
|
|
} |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
public function testAddRelationColumn() |
375
|
|
|
{ |
376
|
|
|
$this->crudPanel->setModel(User::class); |
377
|
|
|
$this->crudPanel->addColumn($this->relationColumnArray); |
378
|
|
|
|
379
|
|
|
$this->assertEquals($this->expectedRelationColumnArray, $this->crudPanel->columns()); |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
public function testAddNestedRelationColumn() |
383
|
|
|
{ |
384
|
|
|
$this->crudPanel->setModel(User::class); |
385
|
|
|
$this->crudPanel->addColumn($this->nestedRelationColumnArray); |
386
|
|
|
$this->crudPanel->addColumn($this->secondNestedRelationColumnArray); |
387
|
|
|
|
388
|
|
|
if (backpack_pro()) { |
389
|
|
|
$this->assertEquals($this->expectedNestedRelationColumnArrayWithPro, $this->crudPanel->columns()); |
390
|
|
|
} else { |
391
|
|
|
$this->assertEquals($this->expectedNestedRelationColumnArrayWithoutPro, $this->crudPanel->columns()); |
392
|
|
|
} |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
public function testMoveColumnBefore() |
396
|
|
|
{ |
397
|
|
|
$this->crudPanel->addColumns($this->twoColumnsArray); |
398
|
|
|
|
399
|
|
|
$this->crudPanel->beforeColumn('column1'); |
400
|
|
|
|
401
|
|
|
$keys = array_keys($this->crudPanel->columns()); |
402
|
|
|
$this->assertEquals($this->expectedTwoColumnsArray['column2'], $this->crudPanel->columns()[$keys[0]]); |
403
|
|
|
$this->assertEquals(['column2', 'column1'], $keys); |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
public function testMoveColumnBeforeUnknownColumnName() |
407
|
|
|
{ |
408
|
|
|
$this->crudPanel->addColumns($this->twoColumnsArray); |
409
|
|
|
|
410
|
|
|
$this->crudPanel->beforeColumn('column3'); |
411
|
|
|
|
412
|
|
|
$this->assertEquals(array_keys($this->expectedTwoColumnsArray), array_keys($this->crudPanel->columns())); |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
public function testMoveColumnAfter() |
416
|
|
|
{ |
417
|
|
|
$this->crudPanel->addColumns($this->threeColumnsArray); |
418
|
|
|
|
419
|
|
|
$this->crudPanel->afterColumn('column1'); |
420
|
|
|
|
421
|
|
|
$keys = array_keys($this->crudPanel->columns()); |
422
|
|
|
$this->assertEquals($this->expectedThreeColumnsArray['column3'], $this->crudPanel->columns()[$keys[1]]); |
423
|
|
|
$this->assertEquals(['column1', 'column3', 'column2'], $keys); |
424
|
|
|
} |
425
|
|
|
|
426
|
|
|
public function testMoveColumnAfterUnknownColumnName() |
427
|
|
|
{ |
428
|
|
|
$this->crudPanel->addColumns($this->twoColumnsArray); |
429
|
|
|
|
430
|
|
|
$this->crudPanel->afterColumn('column3'); |
431
|
|
|
|
432
|
|
|
$this->assertEquals(array_keys($this->expectedTwoColumnsArray), array_keys($this->crudPanel->columns())); |
433
|
|
|
} |
434
|
|
|
|
435
|
|
|
public function testRemoveColumnByName() |
436
|
|
|
{ |
437
|
|
|
$this->crudPanel->addColumns(['column1', 'column2', 'column3']); |
438
|
|
|
|
439
|
|
|
$this->crudPanel->removeColumn('column1'); |
440
|
|
|
|
441
|
|
|
$this->assertEquals(2, count($this->crudPanel->columns())); |
442
|
|
|
$this->assertEquals(['column2', 'column3'], array_keys($this->crudPanel->columns())); |
443
|
|
|
$this->assertNotContains($this->oneColumnArray, $this->crudPanel->columns()); |
444
|
|
|
} |
445
|
|
|
|
446
|
|
|
public function testItCanRemoveAllColumns() |
447
|
|
|
{ |
448
|
|
|
$this->crudPanel->addColumns(['column1', 'column2', 'column3']); |
449
|
|
|
|
450
|
|
|
$this->crudPanel->removeAllColumns(); |
451
|
|
|
|
452
|
|
|
$this->assertEmpty($this->crudPanel->columns()); |
453
|
|
|
} |
454
|
|
|
|
455
|
|
|
public function testItCanSetColumnDetails() |
456
|
|
|
{ |
457
|
|
|
$this->crudPanel->addColumns(['column1', 'column2', 'column3', 'column4', 'column5']); |
458
|
|
|
|
459
|
|
|
$this->crudPanel->setColumnsDetails(['column1', 'column2'], ['label' => 'New Label']); |
460
|
|
|
$this->crudPanel->setColumnDetails('column3', ['label' => 'Old Label']); |
461
|
|
|
$this->crudPanel->modifyColumn('column4', ['label' => 'Alias Label']); |
462
|
|
|
$this->crudPanel->setColumnLabel('column5', 'Setting Label'); |
463
|
|
|
|
464
|
|
|
$this->assertEquals('New Label', $this->crudPanel->columns()['column1']['label']); |
465
|
|
|
$this->assertEquals('New Label', $this->crudPanel->columns()['column2']['label']); |
466
|
|
|
$this->assertEquals('Old Label', $this->crudPanel->columns()['column3']['label']); |
467
|
|
|
$this->assertEquals('Alias Label', $this->crudPanel->columns()['column4']['label']); |
468
|
|
|
$this->assertEquals('Setting Label', $this->crudPanel->columns()['column5']['label']); |
469
|
|
|
} |
470
|
|
|
|
471
|
|
|
public function testItCanFindAColumnById() |
472
|
|
|
{ |
473
|
|
|
$this->crudPanel->addColumns(['column1', 'column2', 'column3']); |
474
|
|
|
|
475
|
|
|
$column = $this->crudPanel->findColumnById(1); |
476
|
|
|
|
477
|
|
|
$this->assertEquals('column2', $column['name']); |
478
|
|
|
} |
479
|
|
|
|
480
|
|
|
public function testItCanGetAndSetActionsColumnPriority() |
481
|
|
|
{ |
482
|
|
|
$this->assertEquals(1, $this->crudPanel->getActionsColumnPriority()); |
483
|
|
|
$this->crudPanel->setActionsColumnPriority(2); |
484
|
|
|
$this->assertEquals(2, $this->crudPanel->getActionsColumnPriority()); |
485
|
|
|
} |
486
|
|
|
|
487
|
|
|
public function testItCanGetAndSetColumnsRemovingPreviouslySet() |
488
|
|
|
{ |
489
|
|
|
$this->crudPanel->addColumns(['column1', 'column2', 'column3']); |
490
|
|
|
|
491
|
|
|
$this->crudPanel->setColumns('column4'); |
492
|
|
|
|
493
|
|
|
$this->assertEquals(1, count($this->crudPanel->columns())); |
494
|
|
|
$this->assertEquals(['column4'], array_keys($this->crudPanel->columns())); |
495
|
|
|
|
496
|
|
|
$this->crudPanel->setColumns(['column5', 'column6']); |
497
|
|
|
$this->assertEquals(2, count($this->crudPanel->columns())); |
498
|
|
|
$this->assertEquals(['column5', 'column6'], array_keys($this->crudPanel->columns())); |
499
|
|
|
|
500
|
|
|
$this->crudPanel->setColumns(['column7', [ |
501
|
|
|
'name' => 'column8', |
502
|
|
|
]]); |
503
|
|
|
$this->assertEquals(2, count($this->crudPanel->columns())); |
504
|
|
|
$this->assertEquals(['column7', 'column8'], array_keys($this->crudPanel->columns())); |
505
|
|
|
} |
506
|
|
|
|
507
|
|
|
public function testRemoveUnknownColumnName() |
508
|
|
|
{ |
509
|
|
|
$unknownColumnName = 'column4'; |
510
|
|
|
$this->crudPanel->addColumns(['column1', 'column2', 'column3']); |
511
|
|
|
|
512
|
|
|
$this->crudPanel->removeColumn($unknownColumnName); |
513
|
|
|
|
514
|
|
|
$this->assertEquals(3, count($this->crudPanel->columns())); |
515
|
|
|
$this->assertEquals(['column1', 'column2', 'column3'], array_keys($this->crudPanel->columns())); |
516
|
|
|
$this->assertNotContains($this->otherOneColumnArray, $this->crudPanel->columns()); |
517
|
|
|
} |
518
|
|
|
|
519
|
|
|
public function testRemoveColumnsByName() |
520
|
|
|
{ |
521
|
|
|
$this->crudPanel->addColumns(['column1', 'column2', 'column3']); |
522
|
|
|
|
523
|
|
|
$this->crudPanel->removeColumns($this->twoColumnsArray); |
524
|
|
|
|
525
|
|
|
$this->assertEquals(1, count($this->crudPanel->columns())); |
526
|
|
|
$this->assertEquals(['column3'], array_keys($this->crudPanel->columns())); |
527
|
|
|
$this->assertNotEquals($this->expectedThreeColumnsArray, $this->crudPanel->columns()); |
528
|
|
|
} |
529
|
|
|
|
530
|
|
|
public function testRemoveUnknownColumnsByName() |
531
|
|
|
{ |
532
|
|
|
$unknownColumnNames = ['column4', 'column5']; |
533
|
|
|
$this->crudPanel->addColumns(['column1', 'column2', 'column3']); |
534
|
|
|
|
535
|
|
|
$this->crudPanel->removeColumns($unknownColumnNames); |
536
|
|
|
|
537
|
|
|
$this->assertEquals(3, count($this->crudPanel->columns())); |
538
|
|
|
$this->assertEquals(['column1', 'column2', 'column3'], array_keys($this->crudPanel->columns())); |
539
|
|
|
$this->assertNotContains($this->otherOneColumnArray, $this->crudPanel->columns()); |
540
|
|
|
} |
541
|
|
|
|
542
|
|
|
public function testSetColumnDetails() |
543
|
|
|
{ |
544
|
|
|
$this->markTestIncomplete('Not correctly implemented'); |
545
|
|
|
|
546
|
|
|
// TODO: refactor crud panel sync method |
547
|
|
|
} |
548
|
|
|
|
549
|
|
|
public function testSetColumnsDetails() |
550
|
|
|
{ |
551
|
|
|
$this->markTestIncomplete('Not correctly implemented'); |
552
|
|
|
|
553
|
|
|
// TODO: refactor crud panel sync method |
554
|
|
|
} |
555
|
|
|
|
556
|
|
|
public function testOrderColumns() |
557
|
|
|
{ |
558
|
|
|
$this->crudPanel->addColumns($this->threeColumnsArray); |
559
|
|
|
|
560
|
|
|
$this->crudPanel->orderColumns(['column2', 'column1', 'column3']); |
561
|
|
|
|
562
|
|
|
$this->assertEquals(['column2', 'column1', 'column3'], array_keys($this->crudPanel->columns())); |
563
|
|
|
} |
564
|
|
|
|
565
|
|
|
public function testOrderColumnsIncompleteList() |
566
|
|
|
{ |
567
|
|
|
$this->crudPanel->addColumns($this->threeColumnsArray); |
568
|
|
|
|
569
|
|
|
$this->crudPanel->orderColumns(['column2', 'column3']); |
570
|
|
|
|
571
|
|
|
$this->assertEquals(['column2', 'column3', 'column1'], array_keys($this->crudPanel->columns())); |
572
|
|
|
} |
573
|
|
|
|
574
|
|
|
public function testOrderColumnsEmptyList() |
575
|
|
|
{ |
576
|
|
|
$this->crudPanel->addColumns($this->threeColumnsArray); |
577
|
|
|
|
578
|
|
|
$this->crudPanel->orderColumns([]); |
579
|
|
|
|
580
|
|
|
$this->assertEquals($this->expectedThreeColumnsArray, $this->crudPanel->columns()); |
581
|
|
|
} |
582
|
|
|
|
583
|
|
|
public function testOrderColumnsUnknownList() |
584
|
|
|
{ |
585
|
|
|
$this->crudPanel->addColumns($this->threeColumnsArray); |
586
|
|
|
|
587
|
|
|
$this->crudPanel->orderColumns(['column4', 'column5', 'column6']); |
588
|
|
|
|
589
|
|
|
$this->assertEquals($this->expectedThreeColumnsArray, $this->crudPanel->columns()); |
590
|
|
|
} |
591
|
|
|
|
592
|
|
|
public function testOrderColumnsMixedList() |
593
|
|
|
{ |
594
|
|
|
$this->crudPanel->addColumns($this->threeColumnsArray); |
595
|
|
|
|
596
|
|
|
$this->crudPanel->orderColumns(['column2', 'column5', 'column6']); |
597
|
|
|
|
598
|
|
|
$this->assertEquals(['column2', 'column1', 'column3'], array_keys($this->crudPanel->columns())); |
599
|
|
|
} |
600
|
|
|
|
601
|
|
|
public function testMakeFirstColumnReturnFalseWhenNoColumnsExist() |
602
|
|
|
{ |
603
|
|
|
$this->assertEmpty($this->crudPanel->columns()); |
604
|
|
|
$column = $this->crudPanel->makeFirstColumn(); |
605
|
|
|
$this->assertFalse($column); |
606
|
|
|
} |
607
|
|
|
|
608
|
|
|
public function testItCanAddADefaultTypeToTheColumn() |
609
|
|
|
{ |
610
|
|
|
$column = $this->crudPanel->addDefaultTypeToColumn(['name' => 'name']); |
611
|
|
|
|
612
|
|
|
$this->assertEquals('text', $column['type']); |
613
|
|
|
} |
614
|
|
|
|
615
|
|
|
public function testItReturnFalseWhenTryingToAddTypeToAColumnWithoutName() |
616
|
|
|
{ |
617
|
|
|
$column = $this->crudPanel->addDefaultTypeToColumn(['attribute' => 'name']); |
618
|
|
|
|
619
|
|
|
$this->assertFalse($column); |
620
|
|
|
} |
621
|
|
|
|
622
|
|
|
public function testItCanChangeTheColumnKey() |
623
|
|
|
{ |
624
|
|
|
$this->crudPanel->column('test'); |
625
|
|
|
|
626
|
|
|
$this->assertEquals('test', $this->crudPanel->columns()['test']['key']); |
627
|
|
|
|
628
|
|
|
$this->crudPanel->column('test')->key('new_key'); |
629
|
|
|
|
630
|
|
|
$this->assertEquals('new_key', $this->crudPanel->columns()['new_key']['key']); |
631
|
|
|
} |
632
|
|
|
|
633
|
|
|
public function testItCanAddAFluentColumn() |
634
|
|
|
{ |
635
|
|
|
$this->crudPanel->setModel(User::class); |
636
|
|
|
|
637
|
|
|
$this->crudPanel->column('my_column')->label('my_column'); |
638
|
|
|
|
639
|
|
|
$this->assertCount(1, $this->crudPanel->columns()); |
640
|
|
|
|
641
|
|
|
$this->assertEquals([ |
642
|
|
|
'name' => 'my_column', |
643
|
|
|
'type' => 'text', |
644
|
|
|
'label' => 'my_column', |
645
|
|
|
'key' => 'my_column', |
646
|
|
|
'priority' => 0, |
647
|
|
|
'tableColumn' => false, |
648
|
|
|
'orderable' => false, |
649
|
|
|
'searchLogic' => false, |
650
|
|
|
], $this->crudPanel->columns()['my_column']); |
651
|
|
|
} |
652
|
|
|
|
653
|
|
|
public function testItCanMakeAColumnFirstFluently() |
654
|
|
|
{ |
655
|
|
|
$this->crudPanel->column('test1'); |
656
|
|
|
$this->crudPanel->column('test2')->makeFirst(); |
657
|
|
|
$crudColumns = $this->crudPanel->columns(); |
658
|
|
|
$firstColumn = reset($crudColumns); |
659
|
|
|
$this->assertEquals($firstColumn['name'], 'test2'); |
660
|
|
|
} |
661
|
|
|
|
662
|
|
|
public function testItCanMakeAColumnLastFluently() |
663
|
|
|
{ |
664
|
|
|
$this->crudPanel->column('test1'); |
665
|
|
|
$this->crudPanel->column('test2'); |
666
|
|
|
$this->crudPanel->column('test1')->makeLast(); |
667
|
|
|
$crudColumns = $this->crudPanel->columns(); |
668
|
|
|
$firstColumn = reset($crudColumns); |
669
|
|
|
$this->assertEquals($firstColumn['name'], 'test2'); |
670
|
|
|
} |
671
|
|
|
|
672
|
|
|
public function testItCanPlaceColumnsFluently() |
673
|
|
|
{ |
674
|
|
|
$this->crudPanel->column('test1'); |
675
|
|
|
$this->crudPanel->column('test2'); |
676
|
|
|
$this->crudPanel->column('test3')->after('test1'); |
677
|
|
|
|
678
|
|
|
$crudColumnsNames = array_column($this->crudPanel->columns(), 'name'); |
679
|
|
|
$this->assertEquals($crudColumnsNames, ['test1', 'test3', 'test2']); |
680
|
|
|
|
681
|
|
|
$this->crudPanel->column('test4')->before('test1'); |
682
|
|
|
$crudColumnsNames = array_column($this->crudPanel->columns(), 'name'); |
683
|
|
|
$this->assertEquals($crudColumnsNames, ['test4', 'test1', 'test3', 'test2']); |
684
|
|
|
} |
685
|
|
|
|
686
|
|
|
public function testItCanRemoveColumnAttributesFluently() |
687
|
|
|
{ |
688
|
|
|
$this->crudPanel->column('test1')->type('test'); |
689
|
|
|
$this->assertEquals($this->crudPanel->columns()['test1']['type'], 'test'); |
690
|
|
|
$this->crudPanel->column('test1')->forget('type'); |
691
|
|
|
$this->assertNull($this->crudPanel->columns()['test1']['type'] ?? null); |
692
|
|
|
} |
693
|
|
|
|
694
|
|
|
public function testItCanRemoveColumnFluently() |
695
|
|
|
{ |
696
|
|
|
$this->crudPanel->column('test1')->type('test'); |
697
|
|
|
$this->assertCount(1, $this->crudPanel->columns()); |
698
|
|
|
$this->crudPanel->column('test1')->remove(); |
699
|
|
|
$this->assertCount(0, $this->crudPanel->columns()); |
700
|
|
|
} |
701
|
|
|
|
702
|
|
|
public function testItCanAddAColumnToCrudFromClass() |
703
|
|
|
{ |
704
|
|
|
CrudColumn::name('test'); |
705
|
|
|
$this->assertCount(1, $this->crudPanel->columns()); |
706
|
|
|
} |
707
|
|
|
|
708
|
|
|
public function testItCanAddAFluentColumnUsingArray() |
709
|
|
|
{ |
710
|
|
|
$this->crudPanel->column($this->oneColumnArray); |
711
|
|
|
$this->assertCount(1, $this->crudPanel->columns()); |
712
|
|
|
} |
713
|
|
|
|
714
|
|
|
public function testItCanAddAFluentColumnUsingArrayWithoutName() |
715
|
|
|
{ |
716
|
|
|
$this->crudPanel->column(['type' => 'text']); |
717
|
|
|
$this->assertCount(1, $this->crudPanel->columns()); |
718
|
|
|
} |
719
|
|
|
|
720
|
|
|
public function testColumnLinkToThrowsExceptionWhenNotAllRequiredParametersAreFilled() |
721
|
|
|
{ |
722
|
|
|
$this->expectException(\Exception::class); |
723
|
|
|
$this->expectExceptionMessage('Route [article.show.detail] expects parameters [id, detail]. Insufficient parameters provided in column: [articles].'); |
724
|
|
|
$this->crudPanel->column('articles')->entity('articles')->linkTo('article.show.detail', ['test' => 'testing']); |
|
|
|
|
725
|
|
|
} |
726
|
|
|
|
727
|
|
|
public function testItThrowsExceptionIfRouteNotFound() |
728
|
|
|
{ |
729
|
|
|
$this->expectException(\Exception::class); |
730
|
|
|
$this->expectExceptionMessage('Route [users.route.doesnt.exist] not found while building the link for column [id].'); |
731
|
|
|
|
732
|
|
|
CrudColumn::name('id')->linkTo('users.route.doesnt.exist')->toArray(); |
|
|
|
|
733
|
|
|
} |
734
|
|
|
|
735
|
|
|
public function testColumnLinkToWithRouteNameOnly() |
736
|
|
|
{ |
737
|
|
|
$this->crudPanel->column('articles')->entity('articles')->linkTo('articles.show'); |
738
|
|
|
$columnArray = $this->crudPanel->columns()['articles']; |
739
|
|
|
$reflection = new \ReflectionFunction($columnArray['wrapper']['href']); |
740
|
|
|
$arguments = $reflection->getClosureUsedVariables(); |
|
|
|
|
741
|
|
|
$this->crudPanel->entry = Article::first(); |
742
|
|
|
$url = $columnArray['wrapper']['href']($this->crudPanel, $columnArray, $this->crudPanel->entry, 1); |
743
|
|
|
$this->assertEquals('articles.show', $arguments['route']); |
744
|
|
|
$this->assertCount(1, $arguments['parameters']); |
745
|
|
|
$this->assertEquals('http://localhost/admin/articles/1/show', $url); |
746
|
|
|
} |
747
|
|
|
|
748
|
|
|
public function testColumnLinkToWithRouteNameAndAdditionalParameters() |
749
|
|
|
{ |
750
|
|
|
$this->crudPanel->column('articles')->entity('articles')->linkTo('articles.show', ['test' => 'testing', 'test2' => 'testing2']); |
751
|
|
|
$columnArray = $this->crudPanel->columns()['articles']; |
752
|
|
|
$reflection = new \ReflectionFunction($columnArray['wrapper']['href']); |
753
|
|
|
$arguments = $reflection->getClosureUsedVariables(); |
754
|
|
|
$this->assertEquals('articles.show', $arguments['route']); |
755
|
|
|
$this->assertCount(3, $arguments['parameters']); |
756
|
|
|
$this->crudPanel->entry = Article::first(); |
757
|
|
|
$url = $columnArray['wrapper']['href']($this->crudPanel, $columnArray, $this->crudPanel->entry, 1); |
758
|
|
|
$this->assertEquals('http://localhost/admin/articles/1/show?test=testing&test2=testing2', $url); |
759
|
|
|
} |
760
|
|
|
|
761
|
|
|
public function testColumnLinkToWithCustomParameters() |
762
|
|
|
{ |
763
|
|
|
$this->crudPanel->column('articles')->entity('articles')->linkTo('article.show.detail', ['detail' => 'testing', 'otherParam' => 'test']); |
764
|
|
|
$columnArray = $this->crudPanel->columns()['articles']; |
765
|
|
|
$this->crudPanel->entry = Article::first(); |
766
|
|
|
$url = $columnArray['wrapper']['href']($this->crudPanel, $columnArray, $this->crudPanel->entry, 1); |
767
|
|
|
$this->assertEquals('http://localhost/admin/articles/1/show/testing?otherParam=test', $url); |
768
|
|
|
} |
769
|
|
|
|
770
|
|
|
public function testColumnLinkToWithCustomClosureParameters() |
771
|
|
|
{ |
772
|
|
|
$this->crudPanel->column('articles') |
773
|
|
|
->entity('articles') |
774
|
|
|
->linkTo('article.show.detail', ['detail' => fn ($entry, $related_key) => $related_key, 'otherParam' => fn ($entry) => $entry->content]); |
775
|
|
|
$columnArray = $this->crudPanel->columns()['articles']; |
776
|
|
|
$this->crudPanel->entry = Article::first(); |
777
|
|
|
$url = $columnArray['wrapper']['href']($this->crudPanel, $columnArray, $this->crudPanel->entry, 1); |
778
|
|
|
$this->assertEquals('http://localhost/admin/articles/1/show/1?otherParam=Some%20Content', $url); |
779
|
|
|
} |
780
|
|
|
|
781
|
|
|
public function testColumnLinkToDontAutoInferParametersIfAllProvided() |
782
|
|
|
{ |
783
|
|
|
$this->crudPanel->column('articles') |
784
|
|
|
->entity('articles') |
785
|
|
|
->linkTo('article.show.detail', ['id' => 123, 'detail' => fn ($entry, $related_key) => $related_key, 'otherParam' => fn ($entry) => $entry->content]); |
786
|
|
|
$columnArray = $this->crudPanel->columns()['articles']; |
787
|
|
|
$this->crudPanel->entry = Article::first(); |
788
|
|
|
$url = $columnArray['wrapper']['href']($this->crudPanel, $columnArray, $this->crudPanel->entry, 1); |
789
|
|
|
$this->assertEquals('http://localhost/admin/articles/123/show/1?otherParam=Some%20Content', $url); |
790
|
|
|
} |
791
|
|
|
|
792
|
|
|
public function testColumnLinkToAutoInferAnySingleParameter() |
793
|
|
|
{ |
794
|
|
|
$this->crudPanel->column('articles') |
795
|
|
|
->entity('articles') |
796
|
|
|
->linkTo('article.show.detail', ['id' => 123, 'otherParam' => fn ($entry) => $entry->content]); |
797
|
|
|
$columnArray = $this->crudPanel->columns()['articles']; |
798
|
|
|
$this->crudPanel->entry = Article::first(); |
799
|
|
|
$url = $columnArray['wrapper']['href']($this->crudPanel, $columnArray, $this->crudPanel->entry, 1); |
800
|
|
|
$this->assertEquals('http://localhost/admin/articles/123/show/1?otherParam=Some%20Content', $url); |
801
|
|
|
} |
802
|
|
|
|
803
|
|
|
public function testColumnLinkToWithClosure() |
804
|
|
|
{ |
805
|
|
|
$this->crudPanel->column('articles') |
806
|
|
|
->entity('articles') |
807
|
|
|
->linkTo(fn ($entry) => route('articles.show', $entry->content)); |
|
|
|
|
808
|
|
|
$columnArray = $this->crudPanel->columns()['articles']; |
809
|
|
|
$this->crudPanel->entry = Article::first(); |
810
|
|
|
$url = $columnArray['wrapper']['href']($this->crudPanel, $columnArray, $this->crudPanel->entry, 1); |
811
|
|
|
$this->assertEquals('http://localhost/admin/articles/Some%20Content/show', $url); |
812
|
|
|
} |
813
|
|
|
|
814
|
|
|
public function testColumnArrayDefinitionLinkToRouteAsClosure() |
815
|
|
|
{ |
816
|
|
|
$this->crudPanel->setModel(User::class); |
817
|
|
|
$this->crudPanel->column([ |
818
|
|
|
'name' => 'articles', |
819
|
|
|
'entity' => 'articles', |
820
|
|
|
'linkTo' => fn ($entry) => route('articles.show', ['id' => $entry->id, 'test' => 'testing']), |
821
|
|
|
]); |
822
|
|
|
$columnArray = $this->crudPanel->columns()['articles']; |
823
|
|
|
$this->crudPanel->entry = Article::first(); |
824
|
|
|
$url = $columnArray['wrapper']['href']($this->crudPanel, $columnArray, $this->crudPanel->entry, 1); |
825
|
|
|
$this->assertEquals('http://localhost/admin/articles/1/show?test=testing', $url); |
826
|
|
|
} |
827
|
|
|
|
828
|
|
|
public function testColumnArrayDefinitionLinkToRouteNameOnly() |
829
|
|
|
{ |
830
|
|
|
$this->crudPanel->setModel(User::class); |
831
|
|
|
$this->crudPanel->column([ |
832
|
|
|
'name' => 'articles', |
833
|
|
|
'entity' => 'articles', |
834
|
|
|
'linkTo' => 'articles.show', |
835
|
|
|
]); |
836
|
|
|
$columnArray = $this->crudPanel->columns()['articles']; |
837
|
|
|
$this->crudPanel->entry = Article::first(); |
838
|
|
|
$url = $columnArray['wrapper']['href']($this->crudPanel, $columnArray, $this->crudPanel->entry, 1); |
839
|
|
|
$this->assertEquals('http://localhost/admin/articles/1/show', $url); |
840
|
|
|
} |
841
|
|
|
|
842
|
|
|
public function testColumnArrayDefinitionLinkToRouteNameAndAdditionalParameters() |
843
|
|
|
{ |
844
|
|
|
$this->crudPanel->setModel(User::class); |
845
|
|
|
$this->crudPanel->column([ |
846
|
|
|
'name' => 'articles', |
847
|
|
|
'entity' => 'articles', |
848
|
|
|
'linkTo' => [ |
849
|
|
|
'route' => 'articles.show', |
850
|
|
|
'parameters' => [ |
851
|
|
|
'test' => 'testing', |
852
|
|
|
'test2' => fn ($entry) => $entry->content, |
853
|
|
|
], |
854
|
|
|
], |
855
|
|
|
]); |
856
|
|
|
$columnArray = $this->crudPanel->columns()['articles']; |
857
|
|
|
$reflection = new \ReflectionFunction($columnArray['wrapper']['href']); |
858
|
|
|
$arguments = $reflection->getClosureUsedVariables(); |
859
|
|
|
$this->assertEquals('articles.show', $arguments['route']); |
860
|
|
|
$this->assertCount(3, $arguments['parameters']); |
861
|
|
|
$this->crudPanel->entry = Article::first(); |
862
|
|
|
$url = $columnArray['wrapper']['href']($this->crudPanel, $columnArray, $this->crudPanel->entry, 1); |
863
|
|
|
$this->assertEquals('http://localhost/admin/articles/1/show?test=testing&test2=Some%20Content', $url); |
864
|
|
|
} |
865
|
|
|
} |
866
|
|
|
|