1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\Tests\Unit\CrudPanel; |
4
|
|
|
|
5
|
|
|
use Backpack\CRUD\Tests\Unit\Models\ColumnType; |
6
|
|
|
|
7
|
|
|
class CrudPanelAutoSetTest extends BaseDBCrudPanelTest |
8
|
|
|
{ |
9
|
|
|
private $expectedUnknownFieldType = 'text'; |
10
|
|
|
|
11
|
|
|
private $expectedFieldTypeFromColumnType = [ |
12
|
|
|
'bigIntegerCol' => 'text', |
13
|
|
|
'binaryCol' => 'text', |
14
|
|
|
'booleanCol' => 'text', |
15
|
|
|
'charCol' => 'text', |
16
|
|
|
'dateCol' => 'date', |
17
|
|
|
'dateTimeCol' => 'datetime', |
18
|
|
|
'dateTimeTzCol' => 'datetime', |
19
|
|
|
'decimalCol' => 'text', |
20
|
|
|
'doubleCol' => 'text', |
21
|
|
|
'enumCol' => 'text', |
22
|
|
|
'floatCol' => 'text', |
23
|
|
|
'integerCol' => 'text', |
24
|
|
|
'ipAddressCol' => 'text', |
25
|
|
|
'jsonCol' => 'textarea', |
26
|
|
|
'jsonbCol' => 'textarea', |
27
|
|
|
'longTextCol' => 'textarea', |
28
|
|
|
'macAddressCol' => 'text', |
29
|
|
|
'mediumIntegerCol' => 'text', |
30
|
|
|
'mediumTextCol' => 'textarea', |
31
|
|
|
'smallIntegerCol' => 'text', |
32
|
|
|
'stringCol' => 'text', |
33
|
|
|
'textCol' => 'textarea', |
34
|
|
|
'timeCol' => 'time', |
35
|
|
|
'timeTzCol' => 'time', |
36
|
|
|
'tinyIntegerCol' => 'text', |
37
|
|
|
'timestampCol' => 'datetime', |
38
|
|
|
'timestampTzCol' => 'datetime', |
39
|
|
|
'uuidCol' => 'text', |
40
|
|
|
]; |
41
|
|
|
|
42
|
|
|
private $expectedColumnTypes = [ |
43
|
|
|
'bigIntegerCol' => [ |
44
|
|
|
'type' => 'integer', |
45
|
|
|
'default' => '', |
46
|
|
|
], |
47
|
|
|
'binaryCol' => [ |
48
|
|
|
'type' => 'blob', |
49
|
|
|
'default' => '', |
50
|
|
|
], |
51
|
|
|
'booleanCol' => [ |
52
|
|
|
'type' => 'boolean', |
53
|
|
|
'default' => '', |
54
|
|
|
], |
55
|
|
|
'charCol' => [ |
56
|
|
|
'type' => 'string', |
57
|
|
|
'default' => '', |
58
|
|
|
], |
59
|
|
|
'dateCol' => [ |
60
|
|
|
'type' => 'date', |
61
|
|
|
'default' => '', |
62
|
|
|
], |
63
|
|
|
'dateTimeCol' => [ |
64
|
|
|
'type' => 'datetime', |
65
|
|
|
'default' => '', |
66
|
|
|
], |
67
|
|
|
'dateTimeTzCol' => [ |
68
|
|
|
'type' => 'datetime', |
69
|
|
|
'default' => '', |
70
|
|
|
], |
71
|
|
|
'decimalCol' => [ |
72
|
|
|
'type' => 'decimal', |
73
|
|
|
'default' => '', |
74
|
|
|
], |
75
|
|
|
'doubleCol' => [ |
76
|
|
|
'type' => 'float', |
77
|
|
|
'default' => '', |
78
|
|
|
], |
79
|
|
|
'enumCol' => [ |
80
|
|
|
'type' => 'string', |
81
|
|
|
'default' => '', |
82
|
|
|
], |
83
|
|
|
'floatCol' => [ |
84
|
|
|
'type' => 'float', |
85
|
|
|
'default' => '', |
86
|
|
|
], |
87
|
|
|
'integerCol' => [ |
88
|
|
|
'type' => 'integer', |
89
|
|
|
'default' => '', |
90
|
|
|
], |
91
|
|
|
'ipAddressCol' => [ |
92
|
|
|
'type' => 'string', |
93
|
|
|
'default' => '', |
94
|
|
|
], |
95
|
|
|
'jsonCol' => [ |
96
|
|
|
'type' => 'text', |
97
|
|
|
'default' => '', |
98
|
|
|
], |
99
|
|
|
'jsonbCol' => [ |
100
|
|
|
'type' => 'text', |
101
|
|
|
'default' => '', |
102
|
|
|
], |
103
|
|
|
'longTextCol' => [ |
104
|
|
|
'type' => 'text', |
105
|
|
|
'default' => '', |
106
|
|
|
], |
107
|
|
|
'macAddressCol' => [ |
108
|
|
|
'type' => 'string', |
109
|
|
|
'default' => '', |
110
|
|
|
], |
111
|
|
|
'mediumIntegerCol' => [ |
112
|
|
|
'type' => 'integer', |
113
|
|
|
'default' => '', |
114
|
|
|
], |
115
|
|
|
'mediumTextCol' => [ |
116
|
|
|
'type' => 'text', |
117
|
|
|
'default' => '', |
118
|
|
|
], |
119
|
|
|
'smallIntegerCol' => [ |
120
|
|
|
'type' => 'integer', |
121
|
|
|
'default' => '', |
122
|
|
|
], |
123
|
|
|
'stringCol' => [ |
124
|
|
|
'type' => 'string', |
125
|
|
|
'default' => '', |
126
|
|
|
], |
127
|
|
|
'textCol' => [ |
128
|
|
|
'type' => 'text', |
129
|
|
|
'default' => '', |
130
|
|
|
], |
131
|
|
|
'timeCol' => [ |
132
|
|
|
'type' => 'time', |
133
|
|
|
'default' => '', |
134
|
|
|
], |
135
|
|
|
'timeTzCol' => [ |
136
|
|
|
'type' => 'time', |
137
|
|
|
'default' => '', |
138
|
|
|
], |
139
|
|
|
'tinyIntegerCol' => [ |
140
|
|
|
'type' => 'integer', |
141
|
|
|
'default' => '', |
142
|
|
|
], |
143
|
|
|
'timestampCol' => [ |
144
|
|
|
'type' => 'datetime', |
145
|
|
|
'default' => '', |
146
|
|
|
], |
147
|
|
|
'timestampTzCol' => [ |
148
|
|
|
'type' => 'datetime', |
149
|
|
|
'default' => '', |
150
|
|
|
], |
151
|
|
|
'uuidCol' => [ |
152
|
|
|
'type' => 'string', |
153
|
|
|
'default' => '', |
154
|
|
|
], |
155
|
|
|
]; |
156
|
|
|
|
157
|
|
|
private $expectedFieldsFromDb = [ |
158
|
|
|
'bigIntegerCol' => [ |
159
|
|
|
'name' => 'bigIntegerCol', |
160
|
|
|
'label' => 'BigIntegerCol', |
161
|
|
|
'value' => null, |
162
|
|
|
'type' => 'text', |
163
|
|
|
'values' => [], |
164
|
|
|
'attributes' => [], |
165
|
|
|
], |
166
|
|
|
'binaryCol' => [ |
167
|
|
|
'name' => 'binaryCol', |
168
|
|
|
'label' => 'BinaryCol', |
169
|
|
|
'value' => null, |
170
|
|
|
'type' => 'text', |
171
|
|
|
'values' => [], |
172
|
|
|
'attributes' => [], |
173
|
|
|
], |
174
|
|
|
'booleanCol' => [ |
175
|
|
|
'name' => 'booleanCol', |
176
|
|
|
'label' => 'BooleanCol', |
177
|
|
|
'value' => null, |
178
|
|
|
'type' => 'text', |
179
|
|
|
'values' => [], |
180
|
|
|
'attributes' => [], |
181
|
|
|
], |
182
|
|
|
'charCol' => [ |
183
|
|
|
'name' => 'charCol', |
184
|
|
|
'label' => 'CharCol', |
185
|
|
|
'value' => null, |
186
|
|
|
'type' => 'text', |
187
|
|
|
'values' => [], |
188
|
|
|
'attributes' => [], |
189
|
|
|
], |
190
|
|
|
'dateCol' => [ |
191
|
|
|
'name' => 'dateCol', |
192
|
|
|
'label' => 'DateCol', |
193
|
|
|
'value' => null, |
194
|
|
|
'type' => 'date', |
195
|
|
|
'values' => [], |
196
|
|
|
'attributes' => [], |
197
|
|
|
], |
198
|
|
|
'dateTimeCol' => [ |
199
|
|
|
'name' => 'dateTimeCol', |
200
|
|
|
'label' => 'DateTimeCol', |
201
|
|
|
'value' => null, |
202
|
|
|
'type' => 'datetime', |
203
|
|
|
'values' => [], |
204
|
|
|
'attributes' => [], |
205
|
|
|
], |
206
|
|
|
'dateTimeTzCol' => [ |
207
|
|
|
'name' => 'dateTimeTzCol', |
208
|
|
|
'label' => 'DateTimeTzCol', |
209
|
|
|
'value' => null, |
210
|
|
|
'type' => 'datetime', |
211
|
|
|
'values' => [], |
212
|
|
|
'attributes' => [], |
213
|
|
|
], |
214
|
|
|
'decimalCol' => [ |
215
|
|
|
'name' => 'decimalCol', |
216
|
|
|
'label' => 'DecimalCol', |
217
|
|
|
'value' => null, |
218
|
|
|
'type' => 'text', |
219
|
|
|
'values' => [], |
220
|
|
|
'attributes' => [], |
221
|
|
|
], |
222
|
|
|
'doubleCol' => [ |
223
|
|
|
'name' => 'doubleCol', |
224
|
|
|
'label' => 'DoubleCol', |
225
|
|
|
'value' => null, |
226
|
|
|
'type' => 'text', |
227
|
|
|
'values' => [], |
228
|
|
|
'attributes' => [], |
229
|
|
|
], |
230
|
|
|
'enumCol' => [ |
231
|
|
|
'name' => 'enumCol', |
232
|
|
|
'label' => 'EnumCol', |
233
|
|
|
'value' => null, |
234
|
|
|
'type' => 'text', |
235
|
|
|
'values' => [], |
236
|
|
|
'attributes' => [], |
237
|
|
|
], |
238
|
|
|
'floatCol' => [ |
239
|
|
|
'name' => 'floatCol', |
240
|
|
|
'label' => 'FloatCol', |
241
|
|
|
'value' => null, |
242
|
|
|
'type' => 'text', |
243
|
|
|
'values' => [], |
244
|
|
|
'attributes' => [], |
245
|
|
|
], |
246
|
|
|
'integerCol' => [ |
247
|
|
|
'name' => 'integerCol', |
248
|
|
|
'label' => 'IntegerCol', |
249
|
|
|
'value' => null, |
250
|
|
|
'type' => 'text', |
251
|
|
|
'values' => [], |
252
|
|
|
'attributes' => [], |
253
|
|
|
], |
254
|
|
|
'ipAddressCol' => [ |
255
|
|
|
'name' => 'ipAddressCol', |
256
|
|
|
'label' => 'IpAddressCol', |
257
|
|
|
'value' => null, |
258
|
|
|
'type' => 'text', |
259
|
|
|
'values' => [], |
260
|
|
|
'attributes' => [], |
261
|
|
|
], |
262
|
|
|
'jsonCol' => [ |
263
|
|
|
'name' => 'jsonCol', |
264
|
|
|
'label' => 'JsonCol', |
265
|
|
|
'value' => null, |
266
|
|
|
'type' => 'textarea', |
267
|
|
|
'values' => [], |
268
|
|
|
'attributes' => [], |
269
|
|
|
], |
270
|
|
|
'jsonbCol' => [ |
271
|
|
|
'name' => 'jsonbCol', |
272
|
|
|
'label' => 'JsonbCol', |
273
|
|
|
'value' => null, |
274
|
|
|
'type' => 'textarea', |
275
|
|
|
'values' => [], |
276
|
|
|
'attributes' => [], |
277
|
|
|
], |
278
|
|
|
'longTextCol' => [ |
279
|
|
|
'name' => 'longTextCol', |
280
|
|
|
'label' => 'LongTextCol', |
281
|
|
|
'value' => null, |
282
|
|
|
'type' => 'textarea', |
283
|
|
|
'values' => [], |
284
|
|
|
'attributes' => [], |
285
|
|
|
], |
286
|
|
|
'macAddressCol' => [ |
287
|
|
|
'name' => 'macAddressCol', |
288
|
|
|
'label' => 'MacAddressCol', |
289
|
|
|
'value' => null, |
290
|
|
|
'type' => 'text', |
291
|
|
|
'values' => [], |
292
|
|
|
'attributes' => [], |
293
|
|
|
], |
294
|
|
|
'mediumIntegerCol' => [ |
295
|
|
|
'name' => 'mediumIntegerCol', |
296
|
|
|
'label' => 'MediumIntegerCol', |
297
|
|
|
'value' => null, |
298
|
|
|
'type' => 'text', |
299
|
|
|
'values' => [], |
300
|
|
|
'attributes' => [], |
301
|
|
|
], |
302
|
|
|
'mediumTextCol' => [ |
303
|
|
|
'name' => 'mediumTextCol', |
304
|
|
|
'label' => 'MediumTextCol', |
305
|
|
|
'value' => null, |
306
|
|
|
'type' => 'textarea', |
307
|
|
|
'values' => [], |
308
|
|
|
'attributes' => [], |
309
|
|
|
], |
310
|
|
|
'smallIntegerCol' => [ |
311
|
|
|
'name' => 'smallIntegerCol', |
312
|
|
|
'label' => 'SmallIntegerCol', |
313
|
|
|
'value' => null, |
314
|
|
|
'type' => 'text', |
315
|
|
|
'values' => [], |
316
|
|
|
'attributes' => [], |
317
|
|
|
], |
318
|
|
|
'stringCol' => [ |
319
|
|
|
'name' => 'stringCol', |
320
|
|
|
'label' => 'StringCol', |
321
|
|
|
'value' => null, |
322
|
|
|
'type' => 'text', |
323
|
|
|
'values' => [], |
324
|
|
|
'attributes' => [], |
325
|
|
|
], |
326
|
|
|
'textCol' => [ |
327
|
|
|
'name' => 'textCol', |
328
|
|
|
'label' => 'TextCol', |
329
|
|
|
'value' => null, |
330
|
|
|
'type' => 'textarea', |
331
|
|
|
'values' => [], |
332
|
|
|
'attributes' => [], |
333
|
|
|
], |
334
|
|
|
'timeCol' => [ |
335
|
|
|
'name' => 'timeCol', |
336
|
|
|
'label' => 'TimeCol', |
337
|
|
|
'value' => null, |
338
|
|
|
'type' => 'time', |
339
|
|
|
'values' => [], |
340
|
|
|
'attributes' => [], |
341
|
|
|
], |
342
|
|
|
'timeTzCol' => [ |
343
|
|
|
'name' => 'timeTzCol', |
344
|
|
|
'label' => 'TimeTzCol', |
345
|
|
|
'value' => null, |
346
|
|
|
'type' => 'time', |
347
|
|
|
'values' => [], |
348
|
|
|
'attributes' => [], |
349
|
|
|
], |
350
|
|
|
'tinyIntegerCol' => [ |
351
|
|
|
'name' => 'tinyIntegerCol', |
352
|
|
|
'label' => 'TinyIntegerCol', |
353
|
|
|
'value' => null, |
354
|
|
|
'type' => 'text', |
355
|
|
|
'values' => [], |
356
|
|
|
'attributes' => [], |
357
|
|
|
], |
358
|
|
|
'timestampCol' => [ |
359
|
|
|
'name' => 'timestampCol', |
360
|
|
|
'label' => 'TimestampCol', |
361
|
|
|
'value' => null, |
362
|
|
|
'type' => 'datetime', |
363
|
|
|
'values' => [], |
364
|
|
|
'attributes' => [], |
365
|
|
|
], |
366
|
|
|
'timestampTzCol' => [ |
367
|
|
|
'name' => 'timestampTzCol', |
368
|
|
|
'label' => 'TimestampTzCol', |
369
|
|
|
'value' => null, |
370
|
|
|
'type' => 'datetime', |
371
|
|
|
'values' => [], |
372
|
|
|
'attributes' => [], |
373
|
|
|
], |
374
|
|
|
'uuidCol' => [ |
375
|
|
|
'name' => 'uuidCol', |
376
|
|
|
'label' => 'UuidCol', |
377
|
|
|
'value' => null, |
378
|
|
|
'type' => 'text', |
379
|
|
|
'values' => [], |
380
|
|
|
'attributes' => [], |
381
|
|
|
], |
382
|
|
|
]; |
383
|
|
|
|
384
|
|
|
public function testGetFieldTypeFromDbColumnType() |
385
|
|
|
{ |
386
|
|
|
$this->crudPanel->setModel(ColumnType::class); |
387
|
|
|
$this->crudPanel->setFromDb(); |
388
|
|
|
|
389
|
|
|
$fieldTypesFromColumnType = []; |
390
|
|
|
foreach ($this->crudPanel->create_fields as $field) { |
391
|
|
|
$fieldTypesFromColumnType[] = $this->crudPanel->getFieldTypeFromDbColumnType($field['name']); |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
$this->assertEquals(array_values($this->expectedFieldTypeFromColumnType), $fieldTypesFromColumnType); |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
public function testSetFromDb() |
398
|
|
|
{ |
399
|
|
|
$this->crudPanel->setModel(ColumnType::class); |
400
|
|
|
|
401
|
|
|
$this->crudPanel->setFromDb(); |
402
|
|
|
|
403
|
|
|
$this->assertEquals($this->expectedFieldsFromDb, $this->crudPanel->create_fields); |
404
|
|
|
$this->assertEquals($this->expectedFieldsFromDb, $this->crudPanel->update_fields); |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
public function testGetDbColumnTypes() |
408
|
|
|
{ |
409
|
|
|
$this->crudPanel->setModel(ColumnType::class); |
410
|
|
|
|
411
|
|
|
$columnTypes = $this->crudPanel->getDbColumnTypes(); |
412
|
|
|
|
413
|
|
|
$this->assertEquals($this->expectedColumnTypes, $columnTypes); |
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
public function testGetFieldTypeFromDbColumnTypeUnknownField() |
417
|
|
|
{ |
418
|
|
|
$fieldType = $this->crudPanel->getFieldTypeFromDbColumnType('someFieldName1'); |
419
|
|
|
|
420
|
|
|
$this->assertEquals($this->expectedUnknownFieldType, $fieldType); |
421
|
|
|
} |
422
|
|
|
|
423
|
|
|
public function testMakeLabel() |
424
|
|
|
{ |
425
|
|
|
$this->markTestIncomplete('Not correctly implemented'); |
426
|
|
|
|
427
|
|
|
$idLabel = $this->crudPanel->makeLabel('id'); |
428
|
|
|
$snakeCaseFKLabel = $this->crudPanel->makeLabel('id_user'); |
429
|
|
|
$camelCaseFKLabel = $this->crudPanel->makeLabel('idUser'); |
430
|
|
|
$camelCaseFKLabelReversed = $this->crudPanel->makeLabel('userId'); |
431
|
|
|
$dateLabel = $this->crudPanel->makeLabel('created_at'); |
432
|
|
|
$camelCaseLabel = $this->crudPanel->makeLabel('camelCaseLabel'); |
433
|
|
|
$camelCaseRandomLabel = $this->crudPanel->makeLabel('camelCaseLabelRANDOMCase'); |
434
|
|
|
$simpleLabel = $this->crudPanel->makeLabel('label'); |
435
|
|
|
$snakeCaseLabel = $this->crudPanel->makeLabel('snake_case_label'); |
436
|
|
|
$snakeCaseRandomLabel = $this->crudPanel->makeLabel('snake_Case_random_CASE'); |
437
|
|
|
$allCapsLabel = $this->crudPanel->makeLabel('ALLCAPSLABEL'); |
438
|
|
|
|
439
|
|
|
// TODO: the id label gets removed. it should not be removed if it is not followed by anything. |
440
|
|
|
// TODO: improve method documentation to know what to expect. |
441
|
|
|
$this->assertEquals('Id', $idLabel); |
442
|
|
|
$this->assertEquals('Id user', $snakeCaseFKLabel); |
443
|
|
|
$this->assertEquals('IdUser', $camelCaseFKLabel); |
444
|
|
|
$this->assertEquals('User', $camelCaseFKLabelReversed); |
445
|
|
|
$this->assertEquals('Created', $dateLabel); |
446
|
|
|
$this->assertEquals('CamelCaseLabel', $camelCaseLabel); |
447
|
|
|
$this->assertEquals('CamelCaseLabelRANDOMCase', $camelCaseRandomLabel); |
448
|
|
|
$this->assertEquals('Label', $simpleLabel); |
449
|
|
|
$this->assertEquals('Snake case label', $snakeCaseLabel); |
450
|
|
|
$this->assertEquals('Snake Case random CASE', $snakeCaseRandomLabel); |
451
|
|
|
$this->assertEquals('ALLCAPSLABEL', $allCapsLabel); |
452
|
|
|
} |
453
|
|
|
|
454
|
|
|
public function testMakeLabelEmpty() |
455
|
|
|
{ |
456
|
|
|
$label = $this->crudPanel->makeLabel(''); |
457
|
|
|
|
458
|
|
|
$this->assertEmpty($label); |
459
|
|
|
} |
460
|
|
|
|
461
|
|
|
public function testGetDbColumnsNames() |
462
|
|
|
{ |
463
|
|
|
$this->crudPanel->setModel(ColumnType::class); |
464
|
|
|
|
465
|
|
|
$columnNames = $this->crudPanel->getDbColumnsNames(); |
466
|
|
|
|
467
|
|
|
$this->assertEquals(array_keys($this->expectedColumnTypes), $columnNames); |
468
|
|
|
} |
469
|
|
|
} |
470
|
|
|
|