We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Total Complexity | 69 |
Total Lines | 913 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like CrudPanelFieldsTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CrudPanelFieldsTest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class CrudPanelFieldsTest extends \Backpack\CRUD\Tests\config\CrudPanel\BaseCrudPanel |
||
20 | { |
||
21 | private $oneTextFieldArray = [ |
||
22 | 'name' => 'field1', |
||
23 | 'label' => 'Field1', |
||
24 | 'type' => 'text', |
||
25 | ]; |
||
26 | |||
27 | private $expectedOneTextFieldArray = [ |
||
28 | 'field1' => [ |
||
29 | 'name' => 'field1', |
||
30 | 'label' => 'Field1', |
||
31 | 'type' => 'text', |
||
32 | 'entity' => false, |
||
33 | ], |
||
34 | ]; |
||
35 | |||
36 | private $unknownFieldTypeArray = [ |
||
37 | 'name' => 'field1', |
||
38 | 'type' => 'unknownType', |
||
39 | ]; |
||
40 | |||
41 | private $invalidTwoFieldsArray = [ |
||
42 | [ |
||
43 | 'keyOne' => 'field1', |
||
44 | 'keyTwo' => 'Field1', |
||
45 | ], |
||
46 | [ |
||
47 | 'otherKey' => 'field2', |
||
48 | ], |
||
49 | ]; |
||
50 | |||
51 | private $twoTextFieldsArray = [ |
||
52 | [ |
||
53 | 'name' => 'field1', |
||
54 | 'label' => 'Field1', |
||
55 | 'type' => 'text', |
||
56 | ], |
||
57 | [ |
||
58 | 'name' => 'field2', |
||
59 | 'label' => 'Field2', |
||
60 | ], |
||
61 | ]; |
||
62 | |||
63 | private $expectedTwoTextFieldsArray = [ |
||
|
|||
64 | 'field1' => [ |
||
65 | 'name' => 'field1', |
||
66 | 'label' => 'Field1', |
||
67 | 'type' => 'text', |
||
68 | 'entity' => false, |
||
69 | ], |
||
70 | 'field2' => [ |
||
71 | 'name' => 'field2', |
||
72 | 'label' => 'Field2', |
||
73 | 'type' => 'text', |
||
74 | 'entity' => false, |
||
75 | ], |
||
76 | ]; |
||
77 | |||
78 | private $threeTextFieldsArray = [ |
||
79 | [ |
||
80 | 'name' => 'field1', |
||
81 | 'label' => 'Field1', |
||
82 | 'type' => 'text', |
||
83 | ], |
||
84 | [ |
||
85 | 'name' => 'field2', |
||
86 | 'label' => 'Field2', |
||
87 | ], |
||
88 | [ |
||
89 | 'name' => 'field3', |
||
90 | ], |
||
91 | ]; |
||
92 | |||
93 | private $expectedThreeTextFieldsArray = [ |
||
94 | 'field1' => [ |
||
95 | 'name' => 'field1', |
||
96 | 'label' => 'Field1', |
||
97 | 'type' => 'text', |
||
98 | 'entity' => false, |
||
99 | ], |
||
100 | 'field2' => [ |
||
101 | 'name' => 'field2', |
||
102 | 'label' => 'Field2', |
||
103 | 'type' => 'text', |
||
104 | 'entity' => false, |
||
105 | ], |
||
106 | 'field3' => [ |
||
107 | 'name' => 'field3', |
||
108 | 'label' => 'Field3', |
||
109 | 'type' => 'text', |
||
110 | 'entity' => false, |
||
111 | ], |
||
112 | ]; |
||
113 | |||
114 | private $multipleFieldTypesArray = [ |
||
115 | [ |
||
116 | 'name' => 'field1', |
||
117 | 'label' => 'Field1', |
||
118 | ], |
||
119 | [ |
||
120 | 'name' => 'field2', |
||
121 | 'type' => 'address', |
||
122 | ], |
||
123 | [ |
||
124 | 'name' => 'field3', |
||
125 | 'type' => 'address', |
||
126 | ], |
||
127 | [ |
||
128 | 'name' => 'field4', |
||
129 | 'type' => 'checkbox', |
||
130 | ], |
||
131 | [ |
||
132 | 'name' => 'field5', |
||
133 | 'type' => 'date', |
||
134 | ], |
||
135 | [ |
||
136 | 'name' => 'field6', |
||
137 | 'type' => 'email', |
||
138 | ], |
||
139 | [ |
||
140 | 'name' => 'field7', |
||
141 | 'type' => 'hidden', |
||
142 | ], |
||
143 | [ |
||
144 | 'name' => 'field8', |
||
145 | 'type' => 'password', |
||
146 | ], |
||
147 | [ |
||
148 | 'name' => 'field9', |
||
149 | 'type' => 'select2', |
||
150 | ], |
||
151 | [ |
||
152 | 'name' => 'field10', |
||
153 | 'type' => 'select2_multiple', |
||
154 | ], |
||
155 | [ |
||
156 | 'name' => 'field11', |
||
157 | 'type' => 'table', |
||
158 | ], |
||
159 | [ |
||
160 | 'name' => 'field12', |
||
161 | 'type' => 'url', |
||
162 | ], |
||
163 | ]; |
||
164 | |||
165 | private $expectedMultipleFieldTypesArray = [ |
||
166 | 'field1' => [ |
||
167 | 'name' => 'field1', |
||
168 | 'label' => 'Field1', |
||
169 | 'type' => 'text', |
||
170 | 'entity' => false, |
||
171 | ], |
||
172 | 'field2' => [ |
||
173 | 'name' => 'field2', |
||
174 | 'type' => 'address', |
||
175 | 'label' => 'Field2', |
||
176 | 'entity' => false, |
||
177 | ], |
||
178 | 'field3' => [ |
||
179 | 'name' => 'field3', |
||
180 | 'type' => 'address', |
||
181 | 'label' => 'Field3', |
||
182 | 'entity' => false, |
||
183 | ], |
||
184 | 'field4' => [ |
||
185 | 'name' => 'field4', |
||
186 | 'type' => 'checkbox', |
||
187 | 'label' => 'Field4', |
||
188 | 'entity' => false, |
||
189 | ], |
||
190 | 'field5' => [ |
||
191 | 'name' => 'field5', |
||
192 | 'type' => 'date', |
||
193 | 'label' => 'Field5', |
||
194 | 'entity' => false, |
||
195 | ], |
||
196 | 'field6' => [ |
||
197 | 'name' => 'field6', |
||
198 | 'type' => 'email', |
||
199 | 'label' => 'Field6', |
||
200 | 'entity' => false, |
||
201 | ], |
||
202 | 'field7' => [ |
||
203 | 'name' => 'field7', |
||
204 | 'type' => 'hidden', |
||
205 | 'label' => 'Field7', |
||
206 | 'entity' => false, |
||
207 | ], |
||
208 | 'field8' => [ |
||
209 | 'name' => 'field8', |
||
210 | 'type' => 'password', |
||
211 | 'label' => 'Field8', |
||
212 | 'entity' => false, |
||
213 | ], |
||
214 | 'field9' => [ |
||
215 | 'name' => 'field9', |
||
216 | 'type' => 'select2', |
||
217 | 'label' => 'Field9', |
||
218 | 'entity' => false, |
||
219 | ], |
||
220 | 'field10' => [ |
||
221 | 'name' => 'field10', |
||
222 | 'type' => 'select2_multiple', |
||
223 | 'label' => 'Field10', |
||
224 | 'entity' => false, |
||
225 | ], |
||
226 | 'field11' => [ |
||
227 | 'name' => 'field11', |
||
228 | 'type' => 'table', |
||
229 | 'label' => 'Field11', |
||
230 | 'entity' => false, |
||
231 | ], |
||
232 | 'field12' => [ |
||
233 | 'name' => 'field12', |
||
234 | 'type' => 'url', |
||
235 | 'label' => 'Field12', |
||
236 | 'entity' => false, |
||
237 | ], |
||
238 | ]; |
||
239 | |||
240 | /** |
||
241 | * Setup the test environment. |
||
242 | * |
||
243 | * @return void |
||
244 | */ |
||
245 | protected function setUp(): void |
||
246 | { |
||
247 | parent::setUp(); |
||
248 | |||
249 | $this->crudPanel->setOperation('create'); |
||
250 | } |
||
251 | |||
252 | public function testAddFieldByName() |
||
253 | { |
||
254 | $this->crudPanel->addField('field1'); |
||
255 | |||
256 | $this->assertEquals(1, count($this->crudPanel->fields())); |
||
257 | $this->assertEquals($this->expectedOneTextFieldArray, $this->crudPanel->fields()); |
||
258 | } |
||
259 | |||
260 | public function testAddFieldsByName() |
||
261 | { |
||
262 | $this->crudPanel->addFields(['field1', 'field2', 'field3']); |
||
263 | |||
264 | $this->assertEquals(3, count($this->crudPanel->fields())); |
||
265 | $this->assertEquals($this->expectedThreeTextFieldsArray, $this->crudPanel->fields()); |
||
266 | } |
||
267 | |||
268 | public function testAddFieldsAsArray() |
||
269 | { |
||
270 | $this->crudPanel->addFields($this->threeTextFieldsArray); |
||
271 | |||
272 | $this->assertEquals(3, count($this->crudPanel->fields())); |
||
273 | $this->assertEquals($this->expectedThreeTextFieldsArray, $this->crudPanel->fields()); |
||
274 | } |
||
275 | |||
276 | public function testAddFieldsDifferentTypes() |
||
277 | { |
||
278 | $this->crudPanel->addFields($this->multipleFieldTypesArray); |
||
279 | |||
280 | $this->assertEquals(12, count($this->crudPanel->fields())); |
||
281 | $this->assertEquals($this->expectedMultipleFieldTypesArray, $this->crudPanel->fields()); |
||
282 | } |
||
283 | |||
284 | public function testAddFieldsInvalidArray() |
||
285 | { |
||
286 | $this->expectException(\Exception::class); |
||
287 | |||
288 | $this->crudPanel->addFields($this->invalidTwoFieldsArray); |
||
289 | } |
||
290 | |||
291 | public function testAddFieldWithInvalidType() |
||
292 | { |
||
293 | $this->markTestIncomplete('Not correctly implemented'); |
||
294 | |||
295 | // TODO: should we validate field types and throw an error if they're not in the pre-defined list of fields or |
||
296 | // in the list of custom field? |
||
297 | $this->crudPanel->addFields($this->unknownFieldTypeArray); |
||
298 | } |
||
299 | |||
300 | public function testAddFieldsForCreateForm() |
||
301 | { |
||
302 | $this->crudPanel->addFields($this->threeTextFieldsArray, 'create'); |
||
303 | |||
304 | $this->assertEquals(3, count($this->crudPanel->fields())); |
||
305 | $this->assertEquals($this->expectedThreeTextFieldsArray, $this->crudPanel->getCreateFields()); |
||
306 | } |
||
307 | |||
308 | public function testAddFieldsForUpdateForm() |
||
309 | { |
||
310 | $this->crudPanel->addFields($this->threeTextFieldsArray, 'update'); |
||
311 | |||
312 | $this->assertEquals(3, count($this->crudPanel->fields())); |
||
313 | $this->assertEquals($this->expectedThreeTextFieldsArray, $this->crudPanel->fields()); |
||
314 | } |
||
315 | |||
316 | public function testBeforeField() |
||
317 | { |
||
318 | $this->crudPanel->addFields($this->threeTextFieldsArray); |
||
319 | $this->crudPanel->beforeField('field2'); |
||
320 | |||
321 | $createKeys = array_keys($this->crudPanel->fields()); |
||
322 | $this->assertEquals($this->expectedThreeTextFieldsArray['field3'], $this->crudPanel->fields()[$createKeys[1]]); |
||
323 | $this->assertEquals(['field1', 'field3', 'field2'], $createKeys); |
||
324 | } |
||
325 | |||
326 | public function testBeforeFieldFirstField() |
||
327 | { |
||
328 | $this->crudPanel->addFields($this->threeTextFieldsArray); |
||
329 | $this->crudPanel->beforeField('field1'); |
||
330 | |||
331 | $createKeys = array_keys($this->crudPanel->fields()); |
||
332 | $this->assertEquals($this->expectedThreeTextFieldsArray['field3'], $this->crudPanel->fields()[$createKeys[0]]); |
||
333 | $this->assertEquals(['field3', 'field1', 'field2'], $createKeys); |
||
334 | |||
335 | $updateKeys = array_keys($this->crudPanel->fields()); |
||
336 | $this->assertEquals($this->expectedThreeTextFieldsArray['field3'], $this->crudPanel->fields()[$updateKeys[0]]); |
||
337 | $this->assertEquals(['field3', 'field1', 'field2'], $updateKeys); |
||
338 | } |
||
339 | |||
340 | public function testBeforeFieldLastField() |
||
341 | { |
||
342 | $this->crudPanel->addFields($this->threeTextFieldsArray); |
||
343 | $this->crudPanel->beforeField('field3'); |
||
344 | |||
345 | $createKeys = array_keys($this->crudPanel->fields()); |
||
346 | $this->assertEquals($this->expectedThreeTextFieldsArray['field3'], $this->crudPanel->fields()[$createKeys[2]]); |
||
347 | $this->assertEquals(['field1', 'field2', 'field3'], $createKeys); |
||
348 | } |
||
349 | |||
350 | public function testBeforeFieldCreateForm() |
||
351 | { |
||
352 | $this->crudPanel->addFields($this->threeTextFieldsArray); |
||
353 | $this->crudPanel->beforeField('field1'); |
||
354 | |||
355 | $createKeys = array_keys($this->crudPanel->fields()); |
||
356 | $this->assertEquals($this->expectedThreeTextFieldsArray['field3'], $this->crudPanel->fields()[$createKeys[0]]); |
||
357 | $this->assertEquals(['field3', 'field1', 'field2'], $createKeys); |
||
358 | } |
||
359 | |||
360 | public function testBeforeUnknownField() |
||
361 | { |
||
362 | $this->crudPanel->addFields($this->threeTextFieldsArray); |
||
363 | |||
364 | $this->crudPanel->beforeField('field4'); |
||
365 | |||
366 | $this->assertEquals(3, count($this->crudPanel->fields())); |
||
367 | $this->assertEquals(array_keys($this->expectedThreeTextFieldsArray), array_keys($this->crudPanel->fields())); |
||
368 | } |
||
369 | |||
370 | public function testAfterField() |
||
371 | { |
||
372 | $this->crudPanel->addFields($this->threeTextFieldsArray); |
||
373 | |||
374 | $this->crudPanel->afterField('field1'); |
||
375 | |||
376 | $createKeys = array_keys($this->crudPanel->fields()); |
||
377 | $this->assertEquals($this->expectedThreeTextFieldsArray['field3'], $this->crudPanel->fields()[$createKeys[1]]); |
||
378 | $this->assertEquals(['field1', 'field3', 'field2'], $createKeys); |
||
379 | |||
380 | $updateKeys = array_keys($this->crudPanel->fields()); |
||
381 | $this->assertEquals($this->expectedThreeTextFieldsArray['field3'], $this->crudPanel->fields()[$updateKeys[1]]); |
||
382 | $this->assertEquals(['field1', 'field3', 'field2'], $updateKeys); |
||
383 | } |
||
384 | |||
385 | public function testAfterFieldLastField() |
||
386 | { |
||
387 | $this->crudPanel->addFields($this->threeTextFieldsArray); |
||
388 | |||
389 | $this->crudPanel->afterField('field3'); |
||
390 | |||
391 | $createKeys = array_keys($this->crudPanel->fields()); |
||
392 | $this->assertEquals($this->expectedThreeTextFieldsArray['field3'], $this->crudPanel->fields()[$createKeys[2]]); |
||
393 | $this->assertEquals(['field1', 'field2', 'field3'], $createKeys); |
||
394 | |||
395 | $updateKeys = array_keys($this->crudPanel->fields()); |
||
396 | $this->assertEquals($this->expectedThreeTextFieldsArray['field3'], $this->crudPanel->fields()[$updateKeys[2]]); |
||
397 | $this->assertEquals(['field1', 'field2', 'field3'], $updateKeys); |
||
398 | } |
||
399 | |||
400 | public function testAfterFieldOnCertainField() |
||
401 | { |
||
402 | $this->crudPanel->addFields($this->threeTextFieldsArray); |
||
403 | $this->crudPanel->addField('custom')->afterField('field1'); |
||
404 | |||
405 | $createKeys = array_keys($this->crudPanel->fields()); |
||
406 | $this->assertEquals(['field1', 'custom', 'field2', 'field3'], $createKeys); |
||
407 | } |
||
408 | |||
409 | public function testAfterUnknownField() |
||
410 | { |
||
411 | $this->crudPanel->addFields($this->threeTextFieldsArray); |
||
412 | |||
413 | $this->crudPanel->afterField('field4'); |
||
414 | |||
415 | $this->assertEquals(3, count($this->crudPanel->fields())); |
||
416 | $this->assertEquals(array_keys($this->expectedThreeTextFieldsArray), array_keys($this->crudPanel->fields())); |
||
417 | } |
||
418 | |||
419 | public function testRemoveFieldsByName() |
||
420 | { |
||
421 | $this->crudPanel->addFields($this->threeTextFieldsArray); |
||
422 | |||
423 | $this->crudPanel->removeFields(['field1']); |
||
424 | |||
425 | $this->assertEquals(2, count($this->crudPanel->fields())); |
||
426 | $this->assertEquals(['field2', 'field3'], array_keys($this->crudPanel->fields())); |
||
427 | } |
||
428 | |||
429 | public function testRemoveFieldsByNameInvalidArray() |
||
430 | { |
||
431 | $this->markTestIncomplete('Not correctly implemented'); |
||
432 | |||
433 | $this->crudPanel->addFields($this->threeTextFieldsArray); |
||
434 | |||
435 | // TODO: this should not work because the method specifically asks for an array of field keys, but it does |
||
436 | // because the removeField method will actually work with arrays instead of a string |
||
437 | $this->crudPanel->removeFields($this->twoTextFieldsArray); |
||
438 | |||
439 | $this->assertEquals(3, count($this->crudPanel->fields())); |
||
440 | $this->assertEquals(array_keys($this->expectedThreeTextFieldsArray), array_keys($this->crudPanel->fields())); |
||
441 | } |
||
442 | |||
443 | public function testRemoveFieldsFromCreateForm() |
||
444 | { |
||
445 | $this->crudPanel->addFields($this->threeTextFieldsArray); |
||
446 | $this->crudPanel->removeFields(['field1']); |
||
447 | |||
448 | $this->assertEquals(2, count($this->crudPanel->fields())); |
||
449 | $this->assertEquals(['field2', 'field3'], array_keys($this->crudPanel->fields())); |
||
450 | } |
||
451 | |||
452 | public function testRemoveFieldsFromUpdateForm() |
||
453 | { |
||
454 | $this->crudPanel->addFields($this->threeTextFieldsArray); |
||
455 | $this->crudPanel->removeFields(['field1']); |
||
456 | |||
457 | $this->assertEquals(2, count($this->crudPanel->fields())); |
||
458 | $this->assertEquals(['field2', 'field3'], array_keys($this->crudPanel->fields())); |
||
459 | } |
||
460 | |||
461 | public function testRemoveUnknownFields() |
||
462 | { |
||
463 | $this->crudPanel->addFields($this->threeTextFieldsArray); |
||
464 | |||
465 | $this->crudPanel->removeFields(['field4']); |
||
466 | |||
467 | $this->assertEquals(3, count($this->crudPanel->fields())); |
||
468 | $this->assertEquals(3, count($this->crudPanel->fields())); |
||
469 | $this->assertEquals(array_keys($this->expectedThreeTextFieldsArray), array_keys($this->crudPanel->fields())); |
||
470 | $this->assertEquals(array_keys($this->expectedThreeTextFieldsArray), array_keys($this->crudPanel->fields())); |
||
471 | } |
||
472 | |||
473 | public function testOrderFields() |
||
474 | { |
||
475 | $this->crudPanel->addFields($this->threeTextFieldsArray); |
||
476 | |||
477 | $this->crudPanel->orderFields(['field2', 'field1', 'field3']); |
||
478 | |||
479 | $this->assertEquals(['field2', 'field1', 'field3'], array_keys($this->crudPanel->fields())); |
||
480 | } |
||
481 | |||
482 | public function testOrderFieldsCreateForm() |
||
483 | { |
||
484 | $this->crudPanel->addFields($this->threeTextFieldsArray); |
||
485 | |||
486 | $this->crudPanel->orderFields(['field2', 'field1', 'field3'], 'create'); |
||
487 | |||
488 | $this->assertEquals(['field2', 'field1', 'field3'], array_keys($this->crudPanel->fields())); |
||
489 | $this->assertEquals($this->expectedThreeTextFieldsArray, $this->crudPanel->fields()); |
||
490 | } |
||
491 | |||
492 | public function testOrderFieldsUpdateForm() |
||
493 | { |
||
494 | $this->crudPanel->addFields($this->threeTextFieldsArray); |
||
495 | |||
496 | $this->crudPanel->orderFields(['field2', 'field1', 'field3'], 'update'); |
||
497 | |||
498 | $this->assertEquals($this->expectedThreeTextFieldsArray, $this->crudPanel->fields()); |
||
499 | $this->assertEquals(['field2', 'field1', 'field3'], array_keys($this->crudPanel->fields())); |
||
500 | } |
||
501 | |||
502 | public function testOrderFieldsIncompleteList() |
||
503 | { |
||
504 | $this->crudPanel->addFields($this->threeTextFieldsArray); |
||
505 | |||
506 | $this->crudPanel->orderFields(['field2', 'field3']); |
||
507 | |||
508 | $this->assertEquals(['field2', 'field3', 'field1'], array_keys($this->crudPanel->fields())); |
||
509 | } |
||
510 | |||
511 | public function testOrderFieldsEmptyList() |
||
512 | { |
||
513 | $this->crudPanel->addFields($this->threeTextFieldsArray); |
||
514 | |||
515 | $this->crudPanel->orderFields([]); |
||
516 | |||
517 | $this->assertEquals($this->expectedThreeTextFieldsArray, $this->crudPanel->fields()); |
||
518 | } |
||
519 | |||
520 | public function testOrderFieldsUnknownList() |
||
521 | { |
||
522 | $this->crudPanel->addFields($this->threeTextFieldsArray); |
||
523 | |||
524 | $this->crudPanel->orderFields(['field4', 'field5', 'field6']); |
||
525 | |||
526 | $this->assertEquals($this->expectedThreeTextFieldsArray, $this->crudPanel->fields()); |
||
527 | } |
||
528 | |||
529 | public function testOrderColumnsMixedList() |
||
530 | { |
||
531 | $this->crudPanel->addFields($this->threeTextFieldsArray); |
||
532 | |||
533 | $this->crudPanel->orderFields(['field2', 'field5', 'field6']); |
||
534 | |||
535 | $this->assertEquals(['field2', 'field1', 'field3'], array_keys($this->crudPanel->fields())); |
||
536 | } |
||
537 | |||
538 | public function testCheckIfFieldIsFirstOfItsType() |
||
539 | { |
||
540 | $this->crudPanel->addFields($this->multipleFieldTypesArray); |
||
541 | |||
542 | $isFirstAddressFieldFirst = $this->crudPanel->checkIfFieldIsFirstOfItsType($this->multipleFieldTypesArray[1]); |
||
543 | $isSecondAddressFieldFirst = $this->crudPanel->checkIfFieldIsFirstOfItsType($this->multipleFieldTypesArray[2]); |
||
544 | |||
545 | $this->assertTrue($isFirstAddressFieldFirst); |
||
546 | $this->assertFalse($isSecondAddressFieldFirst); |
||
547 | } |
||
548 | |||
549 | public function testCheckIfUnknownFieldIsFirstOfItsType() |
||
550 | { |
||
551 | $isUnknownFieldFirst = $this->crudPanel->checkIfFieldIsFirstOfItsType($this->unknownFieldTypeArray, $this->expectedMultipleFieldTypesArray); |
||
552 | |||
553 | $this->assertFalse($isUnknownFieldFirst); |
||
554 | } |
||
555 | |||
556 | public function testCheckIfInvalidFieldIsFirstOfItsType() |
||
557 | { |
||
558 | $this->expectException(\ErrorException::class); |
||
559 | |||
560 | $this->crudPanel->checkIfFieldIsFirstOfItsType($this->invalidTwoFieldsArray[0], $this->expectedMultipleFieldTypesArray); |
||
561 | } |
||
562 | |||
563 | public function testDecodeJsonCastedAttributes() |
||
564 | { |
||
565 | $this->markTestIncomplete(); |
||
566 | |||
567 | // TODO: the decode JSON method should not be in fields trait and should not be exposed in the public API. |
||
568 | } |
||
569 | |||
570 | public function testFieldNameDotNotationIsRelationship() |
||
571 | { |
||
572 | $this->crudPanel->setModel(User::class); |
||
573 | $this->crudPanel->addField('accountDetails.nickname'); |
||
574 | $fieldReadyForHtml = $this->crudPanel->fields()['accountDetails.nickname']; |
||
575 | $fieldCleanState = $this->crudPanel->getCleanStateFields()['accountDetails.nickname']; |
||
576 | $this->assertEquals(Arr::except($fieldReadyForHtml, ['name']), Arr::except($fieldCleanState, ['name'])); |
||
577 | $this->assertEquals($fieldCleanState['relation_type'], 'HasOne'); |
||
578 | $this->assertEquals($fieldReadyForHtml['name'], 'accountDetails[nickname]'); |
||
579 | $this->assertEquals($fieldCleanState['name'], 'accountDetails.nickname'); |
||
580 | } |
||
581 | |||
582 | public function testFieldNameDotNotationIsRelationshipUsingFluentSynthax() |
||
583 | { |
||
584 | $this->crudPanel->setModel(User::class); |
||
585 | $this->crudPanel->field('accountDetails.nickname')->label('custom label'); |
||
586 | $fieldReadyForHtml = $this->crudPanel->fields()['accountDetails.nickname']; |
||
587 | $fieldCleanState = $this->crudPanel->getCleanStateFields()['accountDetails.nickname']; |
||
588 | $this->assertEquals(Arr::except($fieldReadyForHtml, ['name']), Arr::except($fieldCleanState, ['name'])); |
||
589 | $this->assertEquals($fieldCleanState['relation_type'], 'HasOne'); |
||
590 | $this->assertEquals($fieldReadyForHtml['name'], 'accountDetails[nickname]'); |
||
591 | $this->assertEquals($fieldCleanState['name'], 'accountDetails.nickname'); |
||
592 | } |
||
593 | |||
594 | public function testFieldNameIsRelationInCrudModel() |
||
595 | { |
||
596 | $this->crudPanel->setModel(User::class); |
||
597 | $this->crudPanel->addField('roles'); |
||
598 | $field = $this->crudPanel->fields()['roles']; |
||
599 | $this->assertEquals($field['relation_type'], 'BelongsToMany'); |
||
600 | } |
||
601 | |||
602 | public function testFieldNameIsPartialRelationInCrudModel() |
||
603 | { |
||
604 | $this->crudPanel->setModel(User::class); |
||
605 | $this->crudPanel->addField('articles_id'); |
||
606 | $field = $this->crudPanel->fields()['articles_id']; |
||
607 | $this->assertEquals($field['relation_type'], 'HasMany'); |
||
608 | } |
||
609 | |||
610 | public function testGetStrippedSaveRequestWithClosure() |
||
611 | { |
||
612 | $this->crudPanel->setOperationSetting( |
||
613 | 'strippedRequest', |
||
614 | static function (Request $request) { |
||
615 | return $request->toArray(); |
||
616 | }, |
||
617 | 'update' |
||
618 | ); |
||
619 | $this->crudPanel->setOperation('update'); |
||
620 | $this->crudPanel->setModel(User::class); |
||
621 | $request = request()->create('/users/1/edit', 'POST', ['name' => 'john']); |
||
622 | $result = $this->crudPanel->getStrippedSaveRequest($request); |
||
623 | $this->assertIsArray($result); |
||
624 | $this->assertSame(['name' => 'john'], $result); |
||
625 | } |
||
626 | |||
627 | public function testGetStrippedSaveRequestWithClass() |
||
628 | { |
||
629 | $this->crudPanel->setOperationSetting( |
||
630 | 'strippedRequest', |
||
631 | Invokable::class, |
||
632 | 'update' |
||
633 | ); |
||
634 | $this->crudPanel->setOperation('update'); |
||
635 | $this->crudPanel->setModel(User::class); |
||
636 | $request = request()->create('/users/1/edit', 'POST', ['name' => 'john']); |
||
637 | $result = $this->crudPanel->getStrippedSaveRequest($request); |
||
638 | $this->assertIsArray($result); |
||
639 | $this->assertSame(['invokable' => 'invokable'], $result); |
||
640 | } |
||
641 | |||
642 | public function testItDoesNotUseProtectedMethodsAsRelationshipMethods() |
||
643 | { |
||
644 | $this->crudPanel->setModel(User::class); |
||
645 | $this->crudPanel->addField('isNotRelation'); |
||
646 | $this->crudPanel->addField('isNotRelationPublic'); |
||
647 | |||
648 | $this->assertEquals(false, $this->crudPanel->fields()['isNotRelation']['entity']); |
||
649 | $this->assertEquals(false, $this->crudPanel->fields()['isNotRelationPublic']['entity']); |
||
650 | } |
||
651 | |||
652 | public function testItAbortsOnUnexpectedEntity() |
||
653 | { |
||
654 | try { |
||
655 | $this->crudPanel->getRelationInstance(['name' => 'doesNotExist', 'entity' => 'doesNotExist']); |
||
656 | } catch (\Throwable $e) { |
||
657 | } |
||
658 | $this->assertEquals( |
||
659 | 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\Config\Models\TestModel</code> model.'), |
||
660 | $e |
||
661 | ); |
||
662 | } |
||
663 | |||
664 | public function testItCanRemoveAllFields() |
||
665 | { |
||
666 | $this->crudPanel->addFields([ |
||
667 | ['name' => 'test1'], |
||
668 | ['name' => 'test2'], |
||
669 | ]); |
||
670 | |||
671 | $this->assertCount(2, $this->crudPanel->fieldS()); |
||
672 | $this->crudPanel->removeAllFields(); |
||
673 | $this->assertCount(0, $this->crudPanel->fieldS()); |
||
674 | } |
||
675 | |||
676 | public function testItCanRemoveAnAttributeFromAField() |
||
683 | } |
||
684 | |||
685 | public function testItCanSetALabelForAField() |
||
686 | { |
||
687 | $this->crudPanel->addField(['name' => 'test', 'tab' => 'test']); |
||
688 | $this->crudPanel->setFieldLabel('test', 'my-test-label'); |
||
689 | $this->assertEquals('my-test-label', $this->crudPanel->fieldS()['test']['label']); |
||
690 | } |
||
691 | |||
692 | public function testItCanMakeAFieldFirst() |
||
693 | { |
||
694 | $firstField = $this->crudPanel->addField(['name' => 'test1']); |
||
695 | $secondField = $this->crudPanel->addField(['name' => 'test2']); |
||
696 | $this->assertEquals(['test1', 'test2'], array_keys($this->crudPanel->fields())); |
||
697 | $secondField->makeFirstField(); |
||
698 | $this->assertEquals(['test2', 'test1'], array_keys($this->crudPanel->fields())); |
||
699 | } |
||
700 | |||
701 | public function testItCanGetTheCurrentFields() |
||
702 | { |
||
703 | $this->crudPanel->addField(['name' => 'test1']); |
||
704 | $this->crudPanel->addField(['name' => 'test2']); |
||
705 | |||
706 | $this->assertCount(2, $this->crudPanel->getCurrentFields()); |
||
707 | $this->assertCount(2, $this->crudPanel->getFields()); |
||
708 | } |
||
709 | |||
710 | public function testItCanGetUploadFields() |
||
711 | { |
||
712 | $this->crudPanel->addField(['name' => 'test1', 'upload' => true]); |
||
713 | $this->crudPanel->addField(['name' => 'test2']); |
||
714 | |||
715 | $this->assertCount(2, $this->crudPanel->getFields()); |
||
716 | $this->assertTrue($this->crudPanel->hasUploadFields()); |
||
717 | } |
||
718 | |||
719 | public function testItCanGetTheFieldTypeWithViewNamespace() |
||
720 | { |
||
721 | $this->crudPanel->addField(['name' => 'test', 'view_namespace' => 'test_namespace']); |
||
722 | $this->assertEquals('test_namespace.text', $this->crudPanel->getFieldTypeWithNamespace($this->crudPanel->fields()['test'])); |
||
723 | } |
||
724 | |||
725 | public function testItCanGetAllFieldNames() |
||
726 | { |
||
727 | $this->crudPanel->addField(['name' => 'test1']); |
||
728 | $this->crudPanel->addField(['name' => 'test2']); |
||
729 | $this->assertEquals(['test1', 'test2'], $this->crudPanel->getAllFieldNames()); |
||
730 | } |
||
731 | |||
732 | public function testItCanAddAFluentField() |
||
733 | { |
||
734 | $this->crudPanel->setModel(User::class); |
||
735 | |||
736 | $this->crudPanel->field('my_field') |
||
737 | ->type('my_custom_type') |
||
738 | ->label('my_label') |
||
739 | ->tab('custom_tab') |
||
740 | ->prefix('prefix') |
||
741 | ->suffix('suffix') |
||
742 | ->hint('hinter') |
||
743 | ->fake(false) |
||
744 | ->validationRules('required|min:2') |
||
745 | ->validationMessages(['required' => 'is_required', 'min' => 'min_2']) |
||
746 | ->store_in('some') |
||
747 | ->size(6) |
||
748 | ->on('created', function () { |
||
749 | }) |
||
750 | ->subfields([['name' => 'sub_1']]) |
||
751 | ->entity('bang'); |
||
752 | |||
753 | $this->assertCount(1, $this->crudPanel->fields()); |
||
754 | |||
755 | $this->assertEquals([ |
||
756 | 'name' => 'my_field', |
||
757 | 'type' => 'my_custom_type', |
||
758 | 'entity' => 'bang', |
||
759 | 'relation_type' => 'BelongsTo', |
||
760 | 'attribute' => 'name', |
||
761 | 'model' => 'Backpack\CRUD\Tests\Config\Models\Bang', |
||
762 | 'multiple' => false, |
||
763 | 'pivot' => false, |
||
764 | 'label' => 'my_label', |
||
765 | 'tab' => 'custom_tab', |
||
766 | 'suffix' => 'suffix', |
||
767 | 'prefix' => 'prefix', |
||
768 | 'hint' => 'hinter', |
||
769 | 'fake' => false, |
||
770 | 'validationRules' => 'required|min:2', |
||
771 | 'validationMessages' => [ |
||
772 | 'required' => 'is_required', |
||
773 | 'min' => 'min_2', |
||
774 | ], |
||
775 | 'store_in' => 'some', |
||
776 | 'wrapper' => [ |
||
777 | 'class' => 'form-group col-md-6', |
||
778 | ], |
||
779 | 'events' => [ |
||
780 | 'created' => function () { |
||
781 | }, |
||
782 | ], |
||
783 | 'subfields' => [ |
||
784 | [ |
||
785 | 'name' => 'sub_1', |
||
786 | 'parentFieldName' => 'my_field', |
||
787 | 'type' => 'text', |
||
788 | 'entity' => false, |
||
789 | 'label' => 'Sub 1', |
||
790 | ], |
||
791 | ], |
||
792 | |||
793 | ], $this->crudPanel->fields()['my_field']); |
||
794 | } |
||
795 | |||
796 | public function testAddFieldFluentClassUsingArrayDefinition() |
||
797 | { |
||
798 | $this->crudPanel->field($this->oneTextFieldArray); |
||
799 | |||
800 | $this->assertEquals(1, count($this->crudPanel->fields())); |
||
801 | $this->assertEquals($this->expectedOneTextFieldArray, $this->crudPanel->fields()); |
||
802 | } |
||
803 | |||
804 | public function testItCanFluentlyAddUploadAttribute() |
||
805 | { |
||
806 | $this->crudPanel->field('avatar')->upload(); |
||
807 | $this->assertEquals(true, $this->crudPanel->fields()['avatar']['upload']); |
||
808 | } |
||
809 | |||
810 | public function testItCanMakeAFieldFirstFluently() |
||
811 | { |
||
812 | $this->crudPanel->field('test1'); |
||
813 | $this->crudPanel->field('test2')->makeFirst(); |
||
814 | $crudFields = $this->crudPanel->fields(); |
||
815 | $firstField = reset($crudFields); |
||
816 | $this->assertEquals($firstField['name'], 'test2'); |
||
817 | } |
||
818 | |||
819 | public function testItCanMakeAFieldLastFluently() |
||
827 | } |
||
828 | |||
829 | public function testItCanPlaceFieldsFluently() |
||
830 | { |
||
831 | $this->crudPanel->field('test1'); |
||
832 | $this->crudPanel->field('test2'); |
||
833 | $this->crudPanel->field('test3')->after('test1'); |
||
834 | |||
835 | $crudFieldsNames = array_column($this->crudPanel->fields(), 'name'); |
||
836 | $this->assertEquals($crudFieldsNames, ['test1', 'test3', 'test2']); |
||
837 | |||
838 | $this->crudPanel->field('test4')->before('test1'); |
||
839 | $crudFieldsNames = array_column($this->crudPanel->fields(), 'name'); |
||
840 | $this->assertEquals($crudFieldsNames, ['test4', 'test1', 'test3', 'test2']); |
||
841 | } |
||
842 | |||
843 | public function testItCanRemoveFieldAttributesFluently() |
||
844 | { |
||
845 | $this->crudPanel->field('test1')->type('test'); |
||
846 | $this->assertEquals($this->crudPanel->fields()['test1']['type'], 'test'); |
||
847 | $this->crudPanel->field('test1')->forget('type'); |
||
848 | $this->assertNull($this->crudPanel->fields()['test1']['type'] ?? null); |
||
849 | } |
||
850 | |||
851 | public function testItCanRemoveFieldFluently() |
||
852 | { |
||
853 | $this->crudPanel->field('test1')->type('test'); |
||
854 | $this->assertCount(1, $this->crudPanel->fields()); |
||
855 | $this->crudPanel->field('test1')->remove(); |
||
856 | $this->assertCount(0, $this->crudPanel->fields()); |
||
857 | } |
||
858 | |||
859 | public function testItCanAddMorphFieldsFluently() |
||
860 | { |
||
861 | $this->crudPanel->setModel(Star::class); |
||
862 | $this->crudPanel->field('starable') |
||
863 | ->addMorphOption('Backpack\CRUD\Tests\config\Models\User', 'User') |
||
864 | ->morphTypeField(['attributes' => ['custom-attribute' => true]]) |
||
865 | ->morphIdField(['attributes' => ['custom-attribute' => true]]); |
||
866 | |||
867 | [$morphTypeField, $morphIdField] = $this->crudPanel->fields()['starable']['subfields']; |
||
868 | |||
869 | $this->assertTrue($morphTypeField['attributes']['custom-attribute']); |
||
870 | $this->assertTrue($morphIdField['attributes']['custom-attribute']); |
||
871 | } |
||
872 | |||
873 | public function testAbortsConfiguringNonMorphTypeField() |
||
874 | { |
||
875 | $this->crudPanel->setModel(Star::class); |
||
876 | $this->expectException(\Exception::class); |
||
877 | $this->crudPanel->field('some_field') |
||
878 | ->morphTypeField(['attributes' => ['custom-attribute' => true]]); |
||
879 | } |
||
880 | |||
881 | public function testAbortsConfiguringNonMorphIdField() |
||
882 | { |
||
883 | $this->crudPanel->setModel(Star::class); |
||
884 | $this->expectException(\Exception::class); |
||
885 | $this->crudPanel->field('some_field') |
||
886 | ->morphIdField(['attributes' => ['custom-attribute' => true]]); |
||
887 | } |
||
888 | |||
889 | public function testItCanGetTheFieldAttributes() |
||
894 | } |
||
895 | |||
896 | public function testItAbortsWithEmptyNamesFluently() |
||
897 | { |
||
898 | try { |
||
899 | CrudField::name(''); |
||
900 | } catch (\Throwable $e) { |
||
901 | } |
||
902 | $this->assertEquals( |
||
903 | new \Symfony\Component\HttpKernel\Exception\HttpException(500, 'Field name can\'t be empty.'), |
||
904 | $e |
||
905 | ); |
||
906 | } |
||
907 | |||
908 | public function testCheckReturnTypesForWhenInferringRelation() |
||
909 | { |
||
910 | $this->crudPanel->setModel(\Backpack\CRUD\Tests\config\Models\UserWithReturnTypes::class); |
||
911 | $this->crudPanel->addField('isAnAttribute'); |
||
912 | $this->crudPanel->addField('isARelation'); |
||
913 | |||
914 | $this->assertEquals(false, $this->crudPanel->fields()['isAnAttribute']['entity']); |
||
915 | $this->assertEquals('isARelation', $this->crudPanel->fields()['isARelation']['entity']); |
||
916 | } |
||
917 | |||
918 | public function testItCanGetTheFirstFieldViewWithoutProvidingNamespace() |
||
919 | { |
||
920 | $this->assertEquals('backpack.theme-coreuiv2::fields.test', $this->crudPanel->getFirstFieldView('test')); |
||
921 | } |
||
922 | |||
923 | public function testItCanGetTheFirstFieldViewInProvidedNamespace() |
||
924 | { |
||
925 | $this->assertEquals('backpack.theme-coreuiv2::fields.custom_namespace.test', $this->crudPanel->getFirstFieldView('test', 'backpack.theme-coreuiv2::fields.custom_namespace')); |
||
926 | } |
||
927 | |||
928 | public function testItThrowExceptionWhenViewNotFound() |
||
929 | { |
||
930 | $this->expectException(\Exception::class); |
||
931 | $this->crudPanel->getFirstFieldView('test2'); |
||
932 | } |
||
933 | } |
||
934 | |||
935 | class Invokable |
||
936 | { |
||
937 | public function __invoke(): array |
||
938 | { |
||
939 | return ['invokable' => 'invokable']; |
||
940 | } |
||
941 | } |
||
942 |