1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\Tests\Unit\CrudPanel; |
4
|
|
|
|
5
|
|
|
use Arr; |
6
|
|
|
use Backpack\CRUD\Tests\Unit\Models\User; |
7
|
|
|
use Illuminate\Http\Request; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @covers Backpack\CRUD\app\Library\CrudPanel\Traits\Fields |
11
|
|
|
* @covers Backpack\CRUD\app\Library\CrudPanel\Traits\FieldsProtectedMethods |
12
|
|
|
* @covers Backpack\CRUD\app\Library\CrudPanel\Traits\FieldsPrivateMethods |
13
|
|
|
*/ |
14
|
|
|
class CrudPanelFieldsTest extends BaseDBCrudPanelTest |
15
|
|
|
{ |
16
|
|
|
private $oneTextFieldArray = [ |
|
|
|
|
17
|
|
|
'name' => 'field1', |
18
|
|
|
'label' => 'Field1', |
19
|
|
|
'type' => 'text', |
20
|
|
|
]; |
21
|
|
|
|
22
|
|
|
private $expectedOneTextFieldArray = [ |
23
|
|
|
'field1' => [ |
24
|
|
|
'name' => 'field1', |
25
|
|
|
'label' => 'Field1', |
26
|
|
|
'type' => 'text', |
27
|
|
|
'entity' => false, |
28
|
|
|
], |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
private $unknownFieldTypeArray = [ |
32
|
|
|
'name' => 'field1', |
33
|
|
|
'type' => 'unknownType', |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
private $invalidTwoFieldsArray = [ |
37
|
|
|
[ |
38
|
|
|
'keyOne' => 'field1', |
39
|
|
|
'keyTwo' => 'Field1', |
40
|
|
|
], |
41
|
|
|
[ |
42
|
|
|
'otherKey' => 'field2', |
43
|
|
|
], |
44
|
|
|
]; |
45
|
|
|
|
46
|
|
|
private $twoTextFieldsArray = [ |
47
|
|
|
[ |
48
|
|
|
'name' => 'field1', |
49
|
|
|
'label' => 'Field1', |
50
|
|
|
'type' => 'text', |
51
|
|
|
], |
52
|
|
|
[ |
53
|
|
|
'name' => 'field2', |
54
|
|
|
'label' => 'Field2', |
55
|
|
|
], |
56
|
|
|
]; |
57
|
|
|
|
58
|
|
|
private $expectedTwoTextFieldsArray = [ |
|
|
|
|
59
|
|
|
'field1' => [ |
60
|
|
|
'name' => 'field1', |
61
|
|
|
'label' => 'Field1', |
62
|
|
|
'type' => 'text', |
63
|
|
|
'entity' => false, |
64
|
|
|
], |
65
|
|
|
'field2' => [ |
66
|
|
|
'name' => 'field2', |
67
|
|
|
'label' => 'Field2', |
68
|
|
|
'type' => 'text', |
69
|
|
|
'entity' => false, |
70
|
|
|
], |
71
|
|
|
]; |
72
|
|
|
|
73
|
|
|
private $threeTextFieldsArray = [ |
74
|
|
|
[ |
75
|
|
|
'name' => 'field1', |
76
|
|
|
'label' => 'Field1', |
77
|
|
|
'type' => 'text', |
78
|
|
|
], |
79
|
|
|
[ |
80
|
|
|
'name' => 'field2', |
81
|
|
|
'label' => 'Field2', |
82
|
|
|
], |
83
|
|
|
[ |
84
|
|
|
'name' => 'field3', |
85
|
|
|
], |
86
|
|
|
]; |
87
|
|
|
|
88
|
|
|
private $expectedThreeTextFieldsArray = [ |
89
|
|
|
'field1' => [ |
90
|
|
|
'name' => 'field1', |
91
|
|
|
'label' => 'Field1', |
92
|
|
|
'type' => 'text', |
93
|
|
|
'entity' => false, |
94
|
|
|
], |
95
|
|
|
'field2' => [ |
96
|
|
|
'name' => 'field2', |
97
|
|
|
'label' => 'Field2', |
98
|
|
|
'type' => 'text', |
99
|
|
|
'entity' => false, |
100
|
|
|
], |
101
|
|
|
'field3' => [ |
102
|
|
|
'name' => 'field3', |
103
|
|
|
'label' => 'Field3', |
104
|
|
|
'type' => 'text', |
105
|
|
|
'entity' => false, |
106
|
|
|
], |
107
|
|
|
]; |
108
|
|
|
|
109
|
|
|
private $multipleFieldTypesArray = [ |
110
|
|
|
[ |
111
|
|
|
'name' => 'field1', |
112
|
|
|
'label' => 'Field1', |
113
|
|
|
], |
114
|
|
|
[ |
115
|
|
|
'name' => 'field2', |
116
|
|
|
'type' => 'address', |
117
|
|
|
], |
118
|
|
|
[ |
119
|
|
|
'name' => 'field3', |
120
|
|
|
'type' => 'address', |
121
|
|
|
], |
122
|
|
|
[ |
123
|
|
|
'name' => 'field4', |
124
|
|
|
'type' => 'checkbox', |
125
|
|
|
], |
126
|
|
|
[ |
127
|
|
|
'name' => 'field5', |
128
|
|
|
'type' => 'date', |
129
|
|
|
], |
130
|
|
|
[ |
131
|
|
|
'name' => 'field6', |
132
|
|
|
'type' => 'email', |
133
|
|
|
], |
134
|
|
|
[ |
135
|
|
|
'name' => 'field7', |
136
|
|
|
'type' => 'hidden', |
137
|
|
|
], |
138
|
|
|
[ |
139
|
|
|
'name' => 'field8', |
140
|
|
|
'type' => 'password', |
141
|
|
|
], |
142
|
|
|
[ |
143
|
|
|
'name' => 'field9', |
144
|
|
|
'type' => 'select2', |
145
|
|
|
], |
146
|
|
|
[ |
147
|
|
|
'name' => 'field10', |
148
|
|
|
'type' => 'select2_multiple', |
149
|
|
|
], |
150
|
|
|
[ |
151
|
|
|
'name' => 'field11', |
152
|
|
|
'type' => 'table', |
153
|
|
|
], |
154
|
|
|
[ |
155
|
|
|
'name' => 'field12', |
156
|
|
|
'type' => 'url', |
157
|
|
|
], |
158
|
|
|
]; |
159
|
|
|
|
160
|
|
|
private $expectedMultipleFieldTypesArray = [ |
161
|
|
|
'field1' => [ |
162
|
|
|
'name' => 'field1', |
163
|
|
|
'label' => 'Field1', |
164
|
|
|
'type' => 'text', |
165
|
|
|
'entity' => false, |
166
|
|
|
], |
167
|
|
|
'field2' => [ |
168
|
|
|
'name' => 'field2', |
169
|
|
|
'type' => 'address', |
170
|
|
|
'label' => 'Field2', |
171
|
|
|
'entity' => false, |
172
|
|
|
], |
173
|
|
|
'field3' => [ |
174
|
|
|
'name' => 'field3', |
175
|
|
|
'type' => 'address', |
176
|
|
|
'label' => 'Field3', |
177
|
|
|
'entity' => false, |
178
|
|
|
], |
179
|
|
|
'field4' => [ |
180
|
|
|
'name' => 'field4', |
181
|
|
|
'type' => 'checkbox', |
182
|
|
|
'label' => 'Field4', |
183
|
|
|
'entity' => false, |
184
|
|
|
], |
185
|
|
|
'field5' => [ |
186
|
|
|
'name' => 'field5', |
187
|
|
|
'type' => 'date', |
188
|
|
|
'label' => 'Field5', |
189
|
|
|
'entity' => false, |
190
|
|
|
], |
191
|
|
|
'field6' => [ |
192
|
|
|
'name' => 'field6', |
193
|
|
|
'type' => 'email', |
194
|
|
|
'label' => 'Field6', |
195
|
|
|
'entity' => false, |
196
|
|
|
], |
197
|
|
|
'field7' => [ |
198
|
|
|
'name' => 'field7', |
199
|
|
|
'type' => 'hidden', |
200
|
|
|
'label' => 'Field7', |
201
|
|
|
'entity' => false, |
202
|
|
|
], |
203
|
|
|
'field8' => [ |
204
|
|
|
'name' => 'field8', |
205
|
|
|
'type' => 'password', |
206
|
|
|
'label' => 'Field8', |
207
|
|
|
'entity' => false, |
208
|
|
|
], |
209
|
|
|
'field9' => [ |
210
|
|
|
'name' => 'field9', |
211
|
|
|
'type' => 'select2', |
212
|
|
|
'label' => 'Field9', |
213
|
|
|
'entity' => false, |
214
|
|
|
], |
215
|
|
|
'field10' => [ |
216
|
|
|
'name' => 'field10', |
217
|
|
|
'type' => 'select2_multiple', |
218
|
|
|
'label' => 'Field10', |
219
|
|
|
'entity' => false, |
220
|
|
|
], |
221
|
|
|
'field11' => [ |
222
|
|
|
'name' => 'field11', |
223
|
|
|
'type' => 'table', |
224
|
|
|
'label' => 'Field11', |
225
|
|
|
'entity' => false, |
226
|
|
|
], |
227
|
|
|
'field12' => [ |
228
|
|
|
'name' => 'field12', |
229
|
|
|
'type' => 'url', |
230
|
|
|
'label' => 'Field12', |
231
|
|
|
'entity' => false, |
232
|
|
|
], |
233
|
|
|
]; |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Setup the test environment. |
237
|
|
|
* |
238
|
|
|
* @return void |
239
|
|
|
*/ |
240
|
|
|
protected function setUp(): void |
241
|
|
|
{ |
242
|
|
|
parent::setUp(); |
243
|
|
|
|
244
|
|
|
$this->crudPanel->setOperation('create'); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
public function testAddFieldByName() |
248
|
|
|
{ |
249
|
|
|
$this->crudPanel->addField('field1'); |
250
|
|
|
|
251
|
|
|
$this->assertEquals(1, count($this->crudPanel->fields())); |
252
|
|
|
$this->assertEquals($this->expectedOneTextFieldArray, $this->crudPanel->fields()); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
public function testAddFieldsByName() |
256
|
|
|
{ |
257
|
|
|
$this->crudPanel->addFields(['field1', 'field2', 'field3']); |
258
|
|
|
|
259
|
|
|
$this->assertEquals(3, count($this->crudPanel->fields())); |
260
|
|
|
$this->assertEquals($this->expectedThreeTextFieldsArray, $this->crudPanel->fields()); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
public function testAddFieldsAsArray() |
264
|
|
|
{ |
265
|
|
|
$this->crudPanel->addFields($this->threeTextFieldsArray); |
266
|
|
|
|
267
|
|
|
$this->assertEquals(3, count($this->crudPanel->fields())); |
268
|
|
|
$this->assertEquals($this->expectedThreeTextFieldsArray, $this->crudPanel->fields()); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
public function testAddFieldsDifferentTypes() |
272
|
|
|
{ |
273
|
|
|
$this->crudPanel->addFields($this->multipleFieldTypesArray); |
274
|
|
|
|
275
|
|
|
$this->assertEquals(12, count($this->crudPanel->fields())); |
276
|
|
|
$this->assertEquals($this->expectedMultipleFieldTypesArray, $this->crudPanel->fields()); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
public function testAddFieldsInvalidArray() |
280
|
|
|
{ |
281
|
|
|
$this->expectException(\Exception::class); |
282
|
|
|
|
283
|
|
|
$this->crudPanel->addFields($this->invalidTwoFieldsArray); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
public function testAddFieldWithInvalidType() |
287
|
|
|
{ |
288
|
|
|
$this->markTestIncomplete('Not correctly implemented'); |
289
|
|
|
|
290
|
|
|
// TODO: should we validate field types and throw an error if they're not in the pre-defined list of fields or |
291
|
|
|
// in the list of custom field? |
292
|
|
|
$this->crudPanel->addFields($this->unknownFieldTypeArray); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
public function testAddFieldsForCreateForm() |
296
|
|
|
{ |
297
|
|
|
$this->crudPanel->addFields($this->threeTextFieldsArray, 'create'); |
|
|
|
|
298
|
|
|
|
299
|
|
|
$this->assertEquals(3, count($this->crudPanel->fields())); |
300
|
|
|
$this->assertEquals($this->expectedThreeTextFieldsArray, $this->crudPanel->fields()); |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
public function testAddFieldsForUpdateForm() |
304
|
|
|
{ |
305
|
|
|
$this->crudPanel->addFields($this->threeTextFieldsArray, 'update'); |
|
|
|
|
306
|
|
|
|
307
|
|
|
$this->assertEquals(3, count($this->crudPanel->fields())); |
308
|
|
|
$this->assertEquals($this->expectedThreeTextFieldsArray, $this->crudPanel->fields()); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
public function testBeforeField() |
312
|
|
|
{ |
313
|
|
|
$this->crudPanel->addFields($this->threeTextFieldsArray); |
314
|
|
|
$this->crudPanel->beforeField('field2'); |
315
|
|
|
|
316
|
|
|
$createKeys = array_keys($this->crudPanel->fields()); |
317
|
|
|
$this->assertEquals($this->expectedThreeTextFieldsArray['field3'], $this->crudPanel->fields()[$createKeys[1]]); |
318
|
|
|
$this->assertEquals(['field1', 'field3', 'field2'], $createKeys); |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
public function testBeforeFieldFirstField() |
322
|
|
|
{ |
323
|
|
|
$this->crudPanel->addFields($this->threeTextFieldsArray); |
324
|
|
|
$this->crudPanel->beforeField('field1'); |
325
|
|
|
|
326
|
|
|
$createKeys = array_keys($this->crudPanel->fields()); |
327
|
|
|
$this->assertEquals($this->expectedThreeTextFieldsArray['field3'], $this->crudPanel->fields()[$createKeys[0]]); |
328
|
|
|
$this->assertEquals(['field3', 'field1', 'field2'], $createKeys); |
329
|
|
|
|
330
|
|
|
$updateKeys = array_keys($this->crudPanel->fields()); |
331
|
|
|
$this->assertEquals($this->expectedThreeTextFieldsArray['field3'], $this->crudPanel->fields()[$updateKeys[0]]); |
332
|
|
|
$this->assertEquals(['field3', 'field1', 'field2'], $updateKeys); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
public function testBeforeFieldLastField() |
336
|
|
|
{ |
337
|
|
|
$this->crudPanel->addFields($this->threeTextFieldsArray); |
338
|
|
|
$this->crudPanel->beforeField('field3'); |
339
|
|
|
|
340
|
|
|
$createKeys = array_keys($this->crudPanel->fields()); |
341
|
|
|
$this->assertEquals($this->expectedThreeTextFieldsArray['field3'], $this->crudPanel->fields()[$createKeys[2]]); |
342
|
|
|
$this->assertEquals(['field1', 'field2', 'field3'], $createKeys); |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
public function testBeforeFieldCreateForm() |
346
|
|
|
{ |
347
|
|
|
$this->crudPanel->addFields($this->threeTextFieldsArray); |
348
|
|
|
$this->crudPanel->beforeField('field1'); |
349
|
|
|
|
350
|
|
|
$createKeys = array_keys($this->crudPanel->fields()); |
351
|
|
|
$this->assertEquals($this->expectedThreeTextFieldsArray['field3'], $this->crudPanel->fields()[$createKeys[0]]); |
352
|
|
|
$this->assertEquals(['field3', 'field1', 'field2'], $createKeys); |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
public function testBeforeUnknownField() |
356
|
|
|
{ |
357
|
|
|
$this->crudPanel->addFields($this->threeTextFieldsArray); |
358
|
|
|
|
359
|
|
|
$this->crudPanel->beforeField('field4'); |
360
|
|
|
|
361
|
|
|
$this->assertEquals(3, count($this->crudPanel->fields())); |
362
|
|
|
$this->assertEquals(array_keys($this->expectedThreeTextFieldsArray), array_keys($this->crudPanel->fields())); |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
public function testAfterField() |
366
|
|
|
{ |
367
|
|
|
$this->crudPanel->addFields($this->threeTextFieldsArray); |
368
|
|
|
|
369
|
|
|
$this->crudPanel->afterField('field1'); |
370
|
|
|
|
371
|
|
|
$createKeys = array_keys($this->crudPanel->fields()); |
372
|
|
|
$this->assertEquals($this->expectedThreeTextFieldsArray['field3'], $this->crudPanel->fields()[$createKeys[1]]); |
373
|
|
|
$this->assertEquals(['field1', 'field3', 'field2'], $createKeys); |
374
|
|
|
|
375
|
|
|
$updateKeys = array_keys($this->crudPanel->fields()); |
376
|
|
|
$this->assertEquals($this->expectedThreeTextFieldsArray['field3'], $this->crudPanel->fields()[$updateKeys[1]]); |
377
|
|
|
$this->assertEquals(['field1', 'field3', 'field2'], $updateKeys); |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
public function testAfterFieldLastField() |
381
|
|
|
{ |
382
|
|
|
$this->crudPanel->addFields($this->threeTextFieldsArray); |
383
|
|
|
|
384
|
|
|
$this->crudPanel->afterField('field3'); |
385
|
|
|
|
386
|
|
|
$createKeys = array_keys($this->crudPanel->fields()); |
387
|
|
|
$this->assertEquals($this->expectedThreeTextFieldsArray['field3'], $this->crudPanel->fields()[$createKeys[2]]); |
388
|
|
|
$this->assertEquals(['field1', 'field2', 'field3'], $createKeys); |
389
|
|
|
|
390
|
|
|
$updateKeys = array_keys($this->crudPanel->fields()); |
391
|
|
|
$this->assertEquals($this->expectedThreeTextFieldsArray['field3'], $this->crudPanel->fields()[$updateKeys[2]]); |
392
|
|
|
$this->assertEquals(['field1', 'field2', 'field3'], $updateKeys); |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
public function testAfterFieldOnCertainField() |
396
|
|
|
{ |
397
|
|
|
$this->crudPanel->addFields($this->threeTextFieldsArray); |
398
|
|
|
$this->crudPanel->addField('custom')->afterField('field1'); |
399
|
|
|
|
400
|
|
|
$createKeys = array_keys($this->crudPanel->fields()); |
401
|
|
|
$this->assertEquals(['field1', 'custom', 'field2', 'field3'], $createKeys); |
402
|
|
|
} |
403
|
|
|
|
404
|
|
|
public function testAfterUnknownField() |
405
|
|
|
{ |
406
|
|
|
$this->crudPanel->addFields($this->threeTextFieldsArray); |
407
|
|
|
|
408
|
|
|
$this->crudPanel->afterField('field4'); |
409
|
|
|
|
410
|
|
|
$this->assertEquals(3, count($this->crudPanel->fields())); |
411
|
|
|
$this->assertEquals(array_keys($this->expectedThreeTextFieldsArray), array_keys($this->crudPanel->fields())); |
412
|
|
|
} |
413
|
|
|
|
414
|
|
|
public function testRemoveFieldsByName() |
415
|
|
|
{ |
416
|
|
|
$this->crudPanel->addFields($this->threeTextFieldsArray); |
417
|
|
|
|
418
|
|
|
$this->crudPanel->removeFields(['field1']); |
419
|
|
|
|
420
|
|
|
$this->assertEquals(2, count($this->crudPanel->fields())); |
421
|
|
|
$this->assertEquals(['field2', 'field3'], array_keys($this->crudPanel->fields())); |
422
|
|
|
} |
423
|
|
|
|
424
|
|
|
public function testRemoveFieldsByNameInvalidArray() |
425
|
|
|
{ |
426
|
|
|
$this->markTestIncomplete('Not correctly implemented'); |
427
|
|
|
|
428
|
|
|
$this->crudPanel->addFields($this->threeTextFieldsArray); |
429
|
|
|
|
430
|
|
|
// TODO: this should not work because the method specifically asks for an array of field keys, but it does |
431
|
|
|
// because the removeField method will actually work with arrays instead of a string |
432
|
|
|
$this->crudPanel->removeFields($this->twoTextFieldsArray); |
433
|
|
|
|
434
|
|
|
$this->assertEquals(3, count($this->crudPanel->fields())); |
435
|
|
|
$this->assertEquals(array_keys($this->expectedThreeTextFieldsArray), array_keys($this->crudPanel->fields())); |
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
public function testRemoveFieldsFromCreateForm() |
439
|
|
|
{ |
440
|
|
|
$this->crudPanel->addFields($this->threeTextFieldsArray); |
441
|
|
|
$this->crudPanel->removeFields(['field1']); |
442
|
|
|
|
443
|
|
|
$this->assertEquals(2, count($this->crudPanel->fields())); |
444
|
|
|
$this->assertEquals(['field2', 'field3'], array_keys($this->crudPanel->fields())); |
445
|
|
|
} |
446
|
|
|
|
447
|
|
|
public function testRemoveFieldsFromUpdateForm() |
448
|
|
|
{ |
449
|
|
|
$this->crudPanel->addFields($this->threeTextFieldsArray); |
450
|
|
|
$this->crudPanel->removeFields(['field1']); |
451
|
|
|
|
452
|
|
|
$this->assertEquals(2, count($this->crudPanel->fields())); |
453
|
|
|
$this->assertEquals(['field2', 'field3'], array_keys($this->crudPanel->fields())); |
454
|
|
|
} |
455
|
|
|
|
456
|
|
|
public function testRemoveUnknownFields() |
457
|
|
|
{ |
458
|
|
|
$this->crudPanel->addFields($this->threeTextFieldsArray); |
459
|
|
|
|
460
|
|
|
$this->crudPanel->removeFields(['field4']); |
461
|
|
|
|
462
|
|
|
$this->assertEquals(3, count($this->crudPanel->fields())); |
463
|
|
|
$this->assertEquals(3, count($this->crudPanel->fields())); |
464
|
|
|
$this->assertEquals(array_keys($this->expectedThreeTextFieldsArray), array_keys($this->crudPanel->fields())); |
465
|
|
|
$this->assertEquals(array_keys($this->expectedThreeTextFieldsArray), array_keys($this->crudPanel->fields())); |
466
|
|
|
} |
467
|
|
|
|
468
|
|
|
public function testOrderFields() |
469
|
|
|
{ |
470
|
|
|
$this->crudPanel->addFields($this->threeTextFieldsArray); |
471
|
|
|
|
472
|
|
|
$this->crudPanel->orderFields(['field2', 'field1', 'field3']); |
473
|
|
|
|
474
|
|
|
$this->assertEquals(['field2', 'field1', 'field3'], array_keys($this->crudPanel->fields())); |
475
|
|
|
} |
476
|
|
|
|
477
|
|
|
public function testOrderFieldsCreateForm() |
478
|
|
|
{ |
479
|
|
|
$this->crudPanel->addFields($this->threeTextFieldsArray); |
480
|
|
|
|
481
|
|
|
$this->crudPanel->orderFields(['field2', 'field1', 'field3'], 'create'); |
|
|
|
|
482
|
|
|
|
483
|
|
|
$this->assertEquals(['field2', 'field1', 'field3'], array_keys($this->crudPanel->fields())); |
484
|
|
|
$this->assertEquals($this->expectedThreeTextFieldsArray, $this->crudPanel->fields()); |
485
|
|
|
} |
486
|
|
|
|
487
|
|
|
public function testOrderFieldsUpdateForm() |
488
|
|
|
{ |
489
|
|
|
$this->crudPanel->addFields($this->threeTextFieldsArray); |
490
|
|
|
|
491
|
|
|
$this->crudPanel->orderFields(['field2', 'field1', 'field3'], 'update'); |
|
|
|
|
492
|
|
|
|
493
|
|
|
$this->assertEquals($this->expectedThreeTextFieldsArray, $this->crudPanel->fields()); |
494
|
|
|
$this->assertEquals(['field2', 'field1', 'field3'], array_keys($this->crudPanel->fields())); |
495
|
|
|
} |
496
|
|
|
|
497
|
|
|
public function testOrderFieldsIncompleteList() |
498
|
|
|
{ |
499
|
|
|
$this->crudPanel->addFields($this->threeTextFieldsArray); |
500
|
|
|
|
501
|
|
|
$this->crudPanel->orderFields(['field2', 'field3']); |
502
|
|
|
|
503
|
|
|
$this->assertEquals(['field2', 'field3', 'field1'], array_keys($this->crudPanel->fields())); |
504
|
|
|
} |
505
|
|
|
|
506
|
|
|
public function testOrderFieldsEmptyList() |
507
|
|
|
{ |
508
|
|
|
$this->crudPanel->addFields($this->threeTextFieldsArray); |
509
|
|
|
|
510
|
|
|
$this->crudPanel->orderFields([]); |
511
|
|
|
|
512
|
|
|
$this->assertEquals($this->expectedThreeTextFieldsArray, $this->crudPanel->fields()); |
513
|
|
|
} |
514
|
|
|
|
515
|
|
|
public function testOrderFieldsUnknownList() |
516
|
|
|
{ |
517
|
|
|
$this->crudPanel->addFields($this->threeTextFieldsArray); |
518
|
|
|
|
519
|
|
|
$this->crudPanel->orderFields(['field4', 'field5', 'field6']); |
520
|
|
|
|
521
|
|
|
$this->assertEquals($this->expectedThreeTextFieldsArray, $this->crudPanel->fields()); |
522
|
|
|
} |
523
|
|
|
|
524
|
|
|
public function testOrderColumnsMixedList() |
525
|
|
|
{ |
526
|
|
|
$this->crudPanel->addFields($this->threeTextFieldsArray); |
527
|
|
|
|
528
|
|
|
$this->crudPanel->orderFields(['field2', 'field5', 'field6']); |
529
|
|
|
|
530
|
|
|
$this->assertEquals(['field2', 'field1', 'field3'], array_keys($this->crudPanel->fields())); |
531
|
|
|
} |
532
|
|
|
|
533
|
|
|
public function testCheckIfFieldIsFirstOfItsType() |
534
|
|
|
{ |
535
|
|
|
$this->crudPanel->addFields($this->multipleFieldTypesArray); |
536
|
|
|
|
537
|
|
|
$isFirstAddressFieldFirst = $this->crudPanel->checkIfFieldIsFirstOfItsType($this->multipleFieldTypesArray[1]); |
538
|
|
|
$isSecondAddressFieldFirst = $this->crudPanel->checkIfFieldIsFirstOfItsType($this->multipleFieldTypesArray[2]); |
539
|
|
|
|
540
|
|
|
$this->assertTrue($isFirstAddressFieldFirst); |
541
|
|
|
$this->assertFalse($isSecondAddressFieldFirst); |
542
|
|
|
} |
543
|
|
|
|
544
|
|
|
public function testCheckIfUnknownFieldIsFirstOfItsType() |
545
|
|
|
{ |
546
|
|
|
$isUnknownFieldFirst = $this->crudPanel->checkIfFieldIsFirstOfItsType($this->unknownFieldTypeArray, $this->expectedMultipleFieldTypesArray); |
|
|
|
|
547
|
|
|
|
548
|
|
|
$this->assertFalse($isUnknownFieldFirst); |
549
|
|
|
} |
550
|
|
|
|
551
|
|
|
public function testCheckIfInvalidFieldIsFirstOfItsType() |
552
|
|
|
{ |
553
|
|
|
$this->expectException(\ErrorException::class); |
554
|
|
|
|
555
|
|
|
$this->crudPanel->checkIfFieldIsFirstOfItsType($this->invalidTwoFieldsArray[0], $this->expectedMultipleFieldTypesArray); |
|
|
|
|
556
|
|
|
} |
557
|
|
|
|
558
|
|
|
public function testDecodeJsonCastedAttributes() |
559
|
|
|
{ |
560
|
|
|
$this->markTestIncomplete(); |
561
|
|
|
|
562
|
|
|
// TODO: the decode JSON method should not be in fields trait and should not be exposed in the public API. |
563
|
|
|
} |
564
|
|
|
|
565
|
|
|
public function testFieldNameDotNotationIsRelationship() |
566
|
|
|
{ |
567
|
|
|
$this->crudPanel->setModel(User::class); |
568
|
|
|
$this->crudPanel->addField('accountDetails.nickname'); |
569
|
|
|
$fieldReadyForHtml = $this->crudPanel->fields()['accountDetails.nickname']; |
570
|
|
|
$fieldCleanState = $this->crudPanel->getCleanStateFields()['accountDetails.nickname']; |
571
|
|
|
$this->assertEquals(Arr::except($fieldReadyForHtml, ['name']), Arr::except($fieldCleanState, ['name'])); |
572
|
|
|
$this->assertEquals($fieldCleanState['relation_type'], 'HasOne'); |
573
|
|
|
$this->assertEquals($fieldReadyForHtml['name'], 'accountDetails[nickname]'); |
574
|
|
|
$this->assertEquals($fieldCleanState['name'], 'accountDetails.nickname'); |
575
|
|
|
} |
576
|
|
|
|
577
|
|
|
public function testFieldNameDotNotationIsRelationshipUsingFluentSynthax() |
578
|
|
|
{ |
579
|
|
|
$this->crudPanel->setModel(User::class); |
580
|
|
|
$this->crudPanel->field('accountDetails.nickname')->label('custom label'); |
581
|
|
|
$fieldReadyForHtml = $this->crudPanel->fields()['accountDetails.nickname']; |
582
|
|
|
$fieldCleanState = $this->crudPanel->getCleanStateFields()['accountDetails.nickname']; |
583
|
|
|
$this->assertEquals(Arr::except($fieldReadyForHtml, ['name']), Arr::except($fieldCleanState, ['name'])); |
584
|
|
|
$this->assertEquals($fieldCleanState['relation_type'], 'HasOne'); |
585
|
|
|
$this->assertEquals($fieldReadyForHtml['name'], 'accountDetails[nickname]'); |
586
|
|
|
$this->assertEquals($fieldCleanState['name'], 'accountDetails.nickname'); |
587
|
|
|
} |
588
|
|
|
|
589
|
|
|
public function testFieldNameIsRelationInCrudModel() |
590
|
|
|
{ |
591
|
|
|
$this->crudPanel->setModel(User::class); |
592
|
|
|
$this->crudPanel->addField('roles'); |
593
|
|
|
$field = $this->crudPanel->fields()['roles']; |
594
|
|
|
$this->assertEquals($field['relation_type'], 'BelongsToMany'); |
595
|
|
|
} |
596
|
|
|
|
597
|
|
|
public function testFieldNameIsPartialRelationInCrudModel() |
598
|
|
|
{ |
599
|
|
|
$this->crudPanel->setModel(User::class); |
600
|
|
|
$this->crudPanel->addField('articles_id'); |
601
|
|
|
$field = $this->crudPanel->fields()['articles_id']; |
602
|
|
|
$this->assertEquals($field['relation_type'], 'HasMany'); |
603
|
|
|
} |
604
|
|
|
|
605
|
|
|
public function testGetStrippedSaveRequestWithClosure() |
606
|
|
|
{ |
607
|
|
|
$this->crudPanel->setOperationSetting( |
608
|
|
|
'strippedRequest', |
609
|
|
|
static function (Request $request) { |
|
|
|
|
610
|
|
|
return $request->toArray(); |
611
|
|
|
}, |
612
|
|
|
'update' |
613
|
|
|
); |
614
|
|
|
$this->crudPanel->setOperation('update'); |
615
|
|
|
$this->crudPanel->setModel(User::class); |
616
|
|
|
$request = request()->create('/users/1/edit', 'POST', ['name' => 'john']); |
617
|
|
|
$result = $this->crudPanel->getStrippedSaveRequest($request); |
618
|
|
|
$this->assertIsArray($result); |
619
|
|
|
$this->assertSame(['name' => 'john'], $result); |
620
|
|
|
} |
621
|
|
|
|
622
|
|
|
public function testGetStrippedSaveRequestWithClass() |
623
|
|
|
{ |
624
|
|
|
$this->crudPanel->setOperationSetting( |
625
|
|
|
'strippedRequest', |
626
|
|
|
Invokable::class, |
|
|
|
|
627
|
|
|
'update' |
628
|
|
|
); |
629
|
|
|
$this->crudPanel->setOperation('update'); |
630
|
|
|
$this->crudPanel->setModel(User::class); |
631
|
|
|
$request = request()->create('/users/1/edit', 'POST', ['name' => 'john']); |
632
|
|
|
$result = $this->crudPanel->getStrippedSaveRequest($request); |
633
|
|
|
$this->assertIsArray($result); |
634
|
|
|
$this->assertSame(['invokable' => 'invokable'], $result); |
635
|
|
|
} |
636
|
|
|
|
637
|
|
|
public function testItDoesNotUseProtectedMethodsAsRelationshipMethods() |
638
|
|
|
{ |
639
|
|
|
$this->crudPanel->setModel(User::class); |
640
|
|
|
$this->crudPanel->addField('isNotRelation'); |
641
|
|
|
$this->crudPanel->addField('isNotRelationPublic'); |
642
|
|
|
|
643
|
|
|
$this->assertEquals(false, $this->crudPanel->fields()['isNotRelation']['entity']); |
644
|
|
|
$this->assertEquals(false, $this->crudPanel->fields()['isNotRelationPublic']['entity']); |
645
|
|
|
} |
646
|
|
|
|
647
|
|
|
public function testItAbortsOnUnexpectedEntity() |
648
|
|
|
{ |
649
|
|
|
try { |
650
|
|
|
$this->crudPanel->getRelationInstance(['name' => 'doesNotExist', 'entity' => 'doesNotExist']); |
651
|
|
|
} catch (\Throwable $e) { |
|
|
|
|
652
|
|
|
} |
653
|
|
|
$this->assertEquals( |
654
|
|
|
new \Symfony\Component\HttpKernel\Exception\HttpException(500, 'Looks like field <code>doesNotExist</code> is not properly defined. The <code>doesNotExist()</code> relationship doesn\'t seem to exist on the <code>Backpack\CRUD\Tests\Unit\Models\TestModel</code> model.'), |
655
|
|
|
$e |
|
|
|
|
656
|
|
|
); |
657
|
|
|
} |
658
|
|
|
|
659
|
|
|
public function testItCanRemoveAllFields() |
660
|
|
|
{ |
661
|
|
|
$this->crudPanel->addFields([ |
662
|
|
|
['name' => 'test1'], |
663
|
|
|
['name' => 'test2'], |
664
|
|
|
]); |
665
|
|
|
|
666
|
|
|
$this->assertCount(2, $this->crudPanel->fieldS()); |
667
|
|
|
$this->crudPanel->removeAllFields(); |
668
|
|
|
$this->assertCount(0, $this->crudPanel->fieldS()); |
669
|
|
|
} |
670
|
|
|
|
671
|
|
|
public function testItCanRemoveAnAttributeFromAField() |
672
|
|
|
{ |
673
|
|
|
$this->crudPanel->addField(['name' => 'test', 'tab' => 'test']); |
674
|
|
|
|
675
|
|
|
$this->assertEquals('test', $this->crudPanel->fieldS()['test']['tab']); |
676
|
|
|
$this->crudPanel->removeFieldAttribute('test', 'tab'); |
677
|
|
|
$this->assertNull($this->crudPanel->fieldS()['test']['tab'] ?? null); |
678
|
|
|
} |
679
|
|
|
|
680
|
|
|
public function testItCanSetALabelForAField() |
681
|
|
|
{ |
682
|
|
|
$this->crudPanel->addField(['name' => 'test', 'tab' => 'test']); |
683
|
|
|
$this->crudPanel->setFieldLabel('test', 'my-test-label'); |
684
|
|
|
$this->assertEquals('my-test-label', $this->crudPanel->fieldS()['test']['label']); |
685
|
|
|
} |
686
|
|
|
|
687
|
|
|
public function testItCanMakeAFieldFirst() |
688
|
|
|
{ |
689
|
|
|
$firstField = $this->crudPanel->addField(['name' => 'test1']); |
|
|
|
|
690
|
|
|
$secondField = $this->crudPanel->addField(['name' => 'test2']); |
691
|
|
|
$this->assertEquals(['test1', 'test2'], array_keys($this->crudPanel->fields())); |
692
|
|
|
$secondField->makeFirstField(); |
693
|
|
|
$this->assertEquals(['test2', 'test1'], array_keys($this->crudPanel->fields())); |
694
|
|
|
} |
695
|
|
|
|
696
|
|
|
public function testItCanGetTheCurrentFields() |
697
|
|
|
{ |
698
|
|
|
$this->crudPanel->addField(['name' => 'test1']); |
699
|
|
|
$this->crudPanel->addField(['name' => 'test2']); |
700
|
|
|
|
701
|
|
|
$this->assertCount(2, $this->crudPanel->getCurrentFields()); |
702
|
|
|
$this->assertCount(2, $this->crudPanel->getFields()); |
703
|
|
|
} |
704
|
|
|
|
705
|
|
|
public function testItCanGetUploadFields() |
706
|
|
|
{ |
707
|
|
|
$this->crudPanel->addField(['name' => 'test1', 'upload' => true]); |
708
|
|
|
$this->crudPanel->addField(['name' => 'test2']); |
709
|
|
|
|
710
|
|
|
$this->assertCount(2, $this->crudPanel->getFields()); |
711
|
|
|
$this->assertTrue($this->crudPanel->hasUploadFields()); |
712
|
|
|
} |
713
|
|
|
|
714
|
|
|
public function testItCanGetTheFieldTypeWithViewNamespace() |
715
|
|
|
{ |
716
|
|
|
$this->crudPanel->addField(['name' => 'test', 'view_namespace' => 'test_namespace']); |
717
|
|
|
$this->assertEquals('test_namespace.text', $this->crudPanel->getFieldTypeWithNamespace($this->crudPanel->fields()['test'])); |
718
|
|
|
} |
719
|
|
|
|
720
|
|
|
public function testItCanGetAllFieldNames() |
721
|
|
|
{ |
722
|
|
|
$this->crudPanel->addField(['name' => 'test1']); |
723
|
|
|
$this->crudPanel->addField(['name' => 'test2']); |
724
|
|
|
$this->assertEquals(['test1', 'test2'], $this->crudPanel->getAllFieldNames()); |
725
|
|
|
} |
726
|
|
|
} |
727
|
|
|
|
728
|
|
|
class Invokable |
729
|
|
|
{ |
730
|
|
|
public function __invoke(): array |
731
|
|
|
{ |
732
|
|
|
return ['invokable' => 'invokable']; |
733
|
|
|
} |
734
|
|
|
} |
735
|
|
|
|