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