We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 40 |
| Total Lines | 1461 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like CrudPanelCreateTest 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 CrudPanelCreateTest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class CrudPanelCreateTest extends \Backpack\CRUD\Tests\config\CrudPanel\BaseDBCrudPanel |
||
| 27 | { |
||
| 28 | private $nonRelationshipField = [ |
||
| 29 | 'name' => 'field1', |
||
| 30 | 'label' => 'Field1', |
||
| 31 | ]; |
||
| 32 | |||
| 33 | private $userInputFieldsNoRelationships = [ |
||
| 34 | [ |
||
| 35 | 'name' => 'id', |
||
| 36 | 'type' => 'hidden', |
||
| 37 | ], [ |
||
| 38 | 'name' => 'name', |
||
| 39 | ], [ |
||
| 40 | 'name' => 'email', |
||
| 41 | 'type' => 'email', |
||
| 42 | ], [ |
||
| 43 | 'name' => 'password', |
||
| 44 | 'type' => 'password', |
||
| 45 | ], |
||
| 46 | ]; |
||
| 47 | |||
| 48 | private $articleInputFieldsOneToMany = [ |
||
| 49 | [ |
||
| 50 | 'name' => 'id', |
||
| 51 | 'type' => 'hidden', |
||
| 52 | ], [ |
||
| 53 | 'name' => 'content', |
||
| 54 | ], [ |
||
| 55 | 'name' => 'tags', |
||
| 56 | ], [ |
||
| 57 | 'label' => 'Author', |
||
| 58 | 'type' => 'select', |
||
| 59 | 'name' => 'user_id', |
||
| 60 | 'entity' => 'user', |
||
| 61 | 'attribute' => 'name', |
||
| 62 | ], |
||
| 63 | ]; |
||
| 64 | |||
| 65 | private $userInputFieldsManyToMany = [ |
||
| 66 | [ |
||
| 67 | 'name' => 'id', |
||
| 68 | 'type' => 'hidden', |
||
| 69 | ], [ |
||
| 70 | 'name' => 'name', |
||
| 71 | ], [ |
||
| 72 | 'name' => 'email', |
||
| 73 | 'type' => 'email', |
||
| 74 | ], [ |
||
| 75 | 'name' => 'password', |
||
| 76 | 'type' => 'password', |
||
| 77 | ], [ |
||
| 78 | 'label' => 'Roles', |
||
| 79 | 'type' => 'select_multiple', |
||
| 80 | 'name' => 'roles', |
||
| 81 | 'entity' => 'roles', |
||
| 82 | 'attribute' => 'name', |
||
| 83 | 'pivot' => true, |
||
| 84 | ], |
||
| 85 | ]; |
||
| 86 | |||
| 87 | private $userInputFieldsDotNotation = [ |
||
| 88 | [ |
||
| 89 | 'name' => 'id', |
||
| 90 | 'type' => 'hidden', |
||
| 91 | ], [ |
||
| 92 | 'name' => 'name', |
||
| 93 | ], [ |
||
| 94 | 'name' => 'email', |
||
| 95 | 'type' => 'email', |
||
| 96 | ], [ |
||
| 97 | 'name' => 'password', |
||
| 98 | 'type' => 'password', |
||
| 99 | ], [ |
||
| 100 | 'label' => 'Roles', |
||
| 101 | 'type' => 'relationship', |
||
| 102 | 'name' => 'roles', |
||
| 103 | 'entity' => 'roles', |
||
| 104 | 'attribute' => 'name', |
||
| 105 | ], [ |
||
| 106 | 'label' => 'Street', |
||
| 107 | 'name' => 'street', |
||
| 108 | 'entity' => 'accountDetails.addresses', |
||
| 109 | 'attribute' => 'street', |
||
| 110 | ], |
||
| 111 | ]; |
||
| 112 | |||
| 113 | private $userInputHasOneRelation = [ |
||
| 114 | [ |
||
| 115 | 'name' => 'accountDetails.nickname', |
||
| 116 | ], |
||
| 117 | [ |
||
| 118 | 'name' => 'accountDetails.profile_picture', |
||
| 119 | ], |
||
| 120 | ]; |
||
| 121 | |||
| 122 | private $articleInputBelongsToRelationName = [ |
||
| 123 | [ |
||
| 124 | 'name' => 'user', |
||
| 125 | ], |
||
| 126 | ]; |
||
| 127 | |||
| 128 | public function testCreate() |
||
| 129 | { |
||
| 130 | $this->crudPanel->setModel(User::class); |
||
| 131 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships); |
||
| 132 | $faker = Factory::create(); |
||
| 133 | $inputData = [ |
||
| 134 | 'name' => $faker->name, |
||
| 135 | 'email' => $faker->safeEmail, |
||
| 136 | 'password' => Hash::make($faker->password()), |
||
| 137 | ]; |
||
| 138 | |||
| 139 | $entry = $this->crudPanel->create($inputData); |
||
| 140 | |||
| 141 | $this->assertInstanceOf(User::class, $entry); |
||
| 142 | $this->assertEntryEquals($inputData, $entry); |
||
| 143 | $this->assertEmpty($entry->articles); |
||
| 144 | } |
||
| 145 | |||
| 146 | public function testCreateWithOneToOneRelationship() |
||
| 147 | { |
||
| 148 | $this->crudPanel->setModel(User::class); |
||
| 149 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships); |
||
| 150 | $this->crudPanel->addFields($this->userInputHasOneRelation); |
||
| 151 | $faker = Factory::create(); |
||
| 152 | $account_details_nickname = $faker->name; |
||
| 153 | $inputData = [ |
||
| 154 | 'name' => $faker->name, |
||
| 155 | 'email' => $faker->safeEmail, |
||
| 156 | 'password' => Hash::make($faker->password()), |
||
| 157 | 'accountDetails' => [ |
||
| 158 | 'nickname' => $account_details_nickname, |
||
| 159 | 'profile_picture' => 'test.jpg', |
||
| 160 | ], |
||
| 161 | ]; |
||
| 162 | $entry = $this->crudPanel->create($inputData); |
||
| 163 | $account_details = $entry->accountDetails()->first(); |
||
| 164 | |||
| 165 | $this->assertEquals($account_details->nickname, $account_details_nickname); |
||
| 166 | } |
||
| 167 | |||
| 168 | public function testCreateWithOneToOneRelationshipUsingRepeatableInterface() |
||
| 169 | { |
||
| 170 | $this->crudPanel->setModel(User::class); |
||
| 171 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships); |
||
| 172 | $this->crudPanel->addField([ |
||
| 173 | 'name' => 'accountDetails', |
||
| 174 | 'fields' => [ |
||
| 175 | [ |
||
| 176 | 'name' => 'nickname', |
||
| 177 | ], |
||
| 178 | [ |
||
| 179 | 'name' => 'profile_picture', |
||
| 180 | ], |
||
| 181 | ], |
||
| 182 | ]); |
||
| 183 | $faker = Factory::create(); |
||
| 184 | $account_details_nickname = $faker->name; |
||
| 185 | $inputData = [ |
||
| 186 | 'name' => $faker->name, |
||
| 187 | 'email' => $faker->safeEmail, |
||
| 188 | 'password' => Hash::make($faker->password()), |
||
| 189 | 'accountDetails' => [ |
||
| 190 | ['nickname' => $account_details_nickname, 'profile_picture' => 'test.jpg'], |
||
| 191 | ], |
||
| 192 | ]; |
||
| 193 | $entry = $this->crudPanel->create($inputData); |
||
| 194 | $account_details = $entry->accountDetails()->first(); |
||
| 195 | |||
| 196 | $this->assertEquals($account_details->nickname, $account_details_nickname); |
||
| 197 | } |
||
| 198 | |||
| 199 | public function testCreateBelongsToWithRelationName() |
||
| 200 | { |
||
| 201 | $this->crudPanel->setModel(Article::class); |
||
| 202 | $this->crudPanel->addFields($this->articleInputFieldsOneToMany); |
||
| 203 | $this->crudPanel->removeField('user_id'); |
||
| 204 | $this->crudPanel->addFields($this->articleInputBelongsToRelationName); |
||
| 205 | $faker = Factory::create(); |
||
| 206 | $inputData = [ |
||
| 207 | 'content' => $faker->text(), |
||
| 208 | 'tags' => $faker->words(3, true), |
||
| 209 | 'user' => 1, |
||
| 210 | 'metas' => null, |
||
| 211 | 'extras' => null, |
||
| 212 | 'cast_metas' => null, |
||
| 213 | 'cast_tags' => null, |
||
| 214 | 'cast_extras' => null, |
||
| 215 | ]; |
||
| 216 | $entry = $this->crudPanel->create($inputData); |
||
| 217 | $userEntry = User::find(1); |
||
| 218 | $article = Article::where('user_id', 1)->with('user')->get()->last(); |
||
| 219 | $this->assertEquals($article->user_id, $entry->user_id); |
||
| 220 | $this->assertEquals($article->id, $entry->id); |
||
| 221 | } |
||
| 222 | |||
| 223 | public function testCreateWithOneToManyRelationship() |
||
| 224 | { |
||
| 225 | $this->crudPanel->setModel(Article::class); |
||
| 226 | $this->crudPanel->addFields($this->articleInputFieldsOneToMany); |
||
| 227 | $faker = Factory::create(); |
||
| 228 | $inputData = [ |
||
| 229 | 'content' => $faker->text(), |
||
| 230 | 'tags' => $faker->words(3, true), |
||
| 231 | 'user_id' => 1, |
||
| 232 | 'metas' => null, |
||
| 233 | 'extras' => null, |
||
| 234 | 'cast_metas' => null, |
||
| 235 | 'cast_tags' => null, |
||
| 236 | 'cast_extras' => null, |
||
| 237 | ]; |
||
| 238 | |||
| 239 | $entry = $this->crudPanel->create($inputData); |
||
| 240 | $userEntry = User::find(1); |
||
| 241 | $article = Article::where('user_id', 1)->with('user')->get()->last(); |
||
| 242 | $this->assertEntryEquals($inputData, $entry); |
||
| 243 | $this->assertEquals($article->user_id, $entry->user_id); |
||
| 244 | $this->assertEquals($article->id, $entry->id); |
||
| 245 | } |
||
| 246 | |||
| 247 | public function testCreateWithManyToManyRelationship() |
||
| 248 | { |
||
| 249 | $this->crudPanel->setModel(User::class); |
||
| 250 | $this->crudPanel->addFields($this->userInputFieldsManyToMany); |
||
| 251 | $faker = Factory::create(); |
||
| 252 | $inputData = [ |
||
| 253 | 'name' => $faker->name, |
||
| 254 | 'email' => $faker->safeEmail, |
||
| 255 | 'password' => Hash::make($faker->password()), |
||
| 256 | 'remember_token' => null, |
||
| 257 | 'roles' => [1, 2], |
||
| 258 | ]; |
||
| 259 | |||
| 260 | $entry = $this->crudPanel->create($inputData); |
||
| 261 | |||
| 262 | $this->assertInstanceOf(User::class, $entry); |
||
| 263 | $this->assertEntryEquals($inputData, $entry); |
||
| 264 | } |
||
| 265 | |||
| 266 | public function testGetRelationFields() |
||
| 267 | { |
||
| 268 | $this->markTestIncomplete('Not correctly implemented'); |
||
| 269 | |||
| 270 | $this->crudPanel->setModel(User::class); |
||
| 271 | $this->crudPanel->addFields($this->userInputFieldsManyToMany, 'create'); |
||
| 272 | |||
| 273 | // TODO: fix method and documentation. when 'both' is passed as the $form value, the getRelationFields searches |
||
| 274 | // for relationship fields in the update fields. |
||
| 275 | $relationFields = $this->crudPanel->getRelationFields('both'); |
||
| 276 | |||
| 277 | $this->assertEquals($this->crudPanel->create_fields['roles'], Arr::last($relationFields)); |
||
| 278 | } |
||
| 279 | |||
| 280 | public function testGetRelationFieldsCreateForm() |
||
| 281 | { |
||
| 282 | $this->crudPanel->setModel(User::class); |
||
| 283 | $this->crudPanel->setOperation('create'); |
||
| 284 | $this->crudPanel->addFields($this->userInputFieldsManyToMany); |
||
| 285 | |||
| 286 | $relationFields = $this->crudPanel->getRelationFields(); |
||
| 287 | |||
| 288 | $this->assertEquals($this->crudPanel->get('create.fields')['roles'], Arr::last($relationFields)); |
||
| 289 | } |
||
| 290 | |||
| 291 | public function testGetRelationFieldsUpdateForm() |
||
| 292 | { |
||
| 293 | $this->crudPanel->setModel(User::class); |
||
| 294 | $this->crudPanel->setOperation('update'); |
||
| 295 | $this->crudPanel->addFields($this->userInputFieldsManyToMany); |
||
| 296 | |||
| 297 | $relationFields = $this->crudPanel->getRelationFields(); |
||
| 298 | |||
| 299 | $this->assertEquals($this->crudPanel->get('update.fields')['roles'], Arr::last($relationFields)); |
||
| 300 | } |
||
| 301 | |||
| 302 | public function testGetRelationFieldsUnknownForm() |
||
| 303 | { |
||
| 304 | $this->markTestIncomplete('Not correctly implemented'); |
||
| 305 | |||
| 306 | $this->expectException(\InvalidArgumentException::class); |
||
| 307 | |||
| 308 | $this->crudPanel->setModel(User::class); |
||
| 309 | $this->crudPanel->addFields($this->userInputFieldsManyToMany); |
||
| 310 | |||
| 311 | // TODO: this should throw an invalid argument exception but instead it searches for relationship fields in the |
||
| 312 | // update fields. |
||
| 313 | $this->crudPanel->getRelationFields('unknownForm'); |
||
| 314 | } |
||
| 315 | |||
| 316 | public function testGetRelationFieldsDotNotation() |
||
| 317 | { |
||
| 318 | $this->crudPanel->setModel(User::class); |
||
| 319 | $this->crudPanel->setOperation('create'); |
||
| 320 | |||
| 321 | $this->crudPanel->addFields($this->userInputFieldsDotNotation); |
||
| 322 | |||
| 323 | //get all fields with a relation |
||
| 324 | $relationFields = $this->crudPanel->getRelationFields(); |
||
| 325 | |||
| 326 | $this->assertEquals($this->crudPanel->get('create.fields')['street'], Arr::last($relationFields)); |
||
| 327 | } |
||
| 328 | |||
| 329 | public function testCreateHasOneRelations() |
||
| 330 | { |
||
| 331 | $this->crudPanel->setModel(User::class); |
||
| 332 | $this->crudPanel->setOperation('create'); |
||
| 333 | |||
| 334 | $this->crudPanel->addFields($this->userInputHasOneRelation); |
||
| 335 | $faker = Factory::create(); |
||
| 336 | |||
| 337 | $inputData = [ |
||
| 338 | 'name' => $faker->name, |
||
| 339 | 'email' => $faker->safeEmail, |
||
| 340 | 'password' => Hash::make($faker->password()), |
||
| 341 | 'remember_token' => null, |
||
| 342 | 'roles' => [1, 2], |
||
| 343 | 'accountDetails' => [ |
||
| 344 | 'nickname' => 'i_have_has_one', |
||
| 345 | 'profile_picture' => 'simple_picture.jpg', |
||
| 346 | ], |
||
| 347 | ]; |
||
| 348 | $entry = $this->crudPanel->create($inputData); |
||
| 349 | $updateFields = $this->crudPanel->getUpdateFields($entry->id); |
||
| 350 | $account_details = $entry->accountDetails()->first(); |
||
| 351 | |||
| 352 | $this->assertEquals($account_details->nickname, 'i_have_has_one'); |
||
| 353 | } |
||
| 354 | |||
| 355 | public function testGetRelationFieldsNoRelations() |
||
| 356 | { |
||
| 357 | $this->crudPanel->addField($this->nonRelationshipField); |
||
| 358 | |||
| 359 | $relationFields = $this->crudPanel->getRelationFields(); |
||
| 360 | |||
| 361 | $this->assertEmpty($relationFields); |
||
| 362 | } |
||
| 363 | |||
| 364 | public function testGetRelationFieldsNoFields() |
||
| 365 | { |
||
| 366 | $relationFields = $this->crudPanel->getRelationFields(); |
||
| 367 | |||
| 368 | $this->assertEmpty($relationFields); |
||
| 369 | } |
||
| 370 | |||
| 371 | public function testGetRelationFieldsWithPivot() |
||
| 372 | { |
||
| 373 | $this->crudPanel->setModel(User::class); |
||
| 374 | $this->crudPanel->setOperation('create'); |
||
| 375 | $this->crudPanel->addFields($this->userInputFieldsDotNotation); |
||
| 376 | |||
| 377 | $relationFields = $this->crudPanel->getRelationFieldsWithPivot(); |
||
| 378 | $this->assertEquals($this->crudPanel->get('create.fields')['roles'], Arr::first($relationFields)); |
||
| 379 | } |
||
| 380 | |||
| 381 | public function testGetRelationFieldsWithPivotNoRelations() |
||
| 382 | { |
||
| 383 | $this->crudPanel->setModel(User::class); |
||
| 384 | $this->crudPanel->setOperation('create'); |
||
| 385 | $this->crudPanel->addFields($this->nonRelationshipField); |
||
| 386 | |||
| 387 | $relationFields = $this->crudPanel->getRelationFieldsWithPivot(); |
||
| 388 | |||
| 389 | $this->assertEmpty($relationFields); |
||
| 390 | } |
||
| 391 | |||
| 392 | public function testMorphToManySelectableRelationship() |
||
| 393 | { |
||
| 394 | $this->crudPanel->setModel(User::class); |
||
| 395 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both'); |
||
| 396 | $this->crudPanel->addField(['name' => 'bills'], 'both'); |
||
| 397 | |||
| 398 | $faker = Factory::create(); |
||
| 399 | $inputData = [ |
||
| 400 | 'name' => $faker->name, |
||
| 401 | 'email' => $faker->safeEmail, |
||
| 402 | 'password' => Hash::make($faker->password()), |
||
| 403 | 'remember_token' => null, |
||
| 404 | 'bills' => [1], |
||
| 405 | ]; |
||
| 406 | |||
| 407 | $entry = $this->crudPanel->create($inputData); |
||
| 408 | |||
| 409 | $updateFields = $this->crudPanel->getUpdateFields($entry->id); |
||
| 410 | |||
| 411 | $this->assertCount(1, $entry->bills); |
||
| 412 | |||
| 413 | $this->assertEquals(1, $entry->bills()->first()->id); |
||
| 414 | |||
| 415 | $inputData['bills'] = [1, 2]; |
||
| 416 | |||
| 417 | $this->crudPanel->update($entry->id, $inputData); |
||
| 418 | |||
| 419 | $this->assertCount(2, $entry->fresh()->bills); |
||
| 420 | |||
| 421 | $this->assertEquals([1, 2], $entry->fresh()->bills->pluck('id')->toArray()); |
||
| 422 | } |
||
| 423 | |||
| 424 | public function testMorphToManyCreatableRelationship() |
||
| 425 | { |
||
| 426 | $this->crudPanel->setModel(User::class); |
||
| 427 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both'); |
||
| 428 | $this->crudPanel->addField(['name' => 'recommends', 'subfields' => [ |
||
| 429 | [ |
||
| 430 | 'name' => 'text', |
||
| 431 | ], |
||
| 432 | ]], 'both'); |
||
| 433 | |||
| 434 | $faker = Factory::create(); |
||
| 435 | $inputData = [ |
||
| 436 | 'name' => $faker->name, |
||
| 437 | 'email' => $faker->safeEmail, |
||
| 438 | 'password' => Hash::make($faker->password()), |
||
| 439 | 'remember_token' => null, |
||
| 440 | 'recommends' => [ |
||
| 441 | [ |
||
| 442 | 'recommends' => 1, |
||
| 443 | 'text' => 'my pivot recommend field', |
||
| 444 | ], |
||
| 445 | ], |
||
| 446 | ]; |
||
| 447 | |||
| 448 | $entry = $this->crudPanel->create($inputData); |
||
| 449 | $updateFields = $this->crudPanel->getUpdateFields($entry->id); |
||
| 450 | |||
| 451 | $this->assertCount(1, $entry->recommends); |
||
| 452 | |||
| 453 | $this->assertEquals(1, $entry->recommends()->first()->id); |
||
| 454 | |||
| 455 | $inputData['recommends'] = [ |
||
| 456 | [ |
||
| 457 | 'recommends' => 2, |
||
| 458 | 'text' => 'I changed the recommend and the pivot text', |
||
| 459 | ], |
||
| 460 | ]; |
||
| 461 | |||
| 462 | $this->crudPanel->update($entry->id, $inputData); |
||
| 463 | |||
| 464 | $this->assertCount(1, $entry->fresh()->recommends); |
||
| 465 | |||
| 466 | $this->assertEquals(2, $entry->recommends()->first()->id); |
||
| 467 | |||
| 468 | $this->assertEquals('I changed the recommend and the pivot text', $entry->fresh()->recommends->first()->pivot->text); |
||
| 469 | } |
||
| 470 | |||
| 471 | public function testBelongsToManyWithPivotDataRelationship() |
||
| 472 | { |
||
| 473 | $this->crudPanel->setModel(User::class); |
||
| 474 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships); |
||
| 475 | $this->crudPanel->addField([ |
||
| 476 | 'name' => 'superArticles', |
||
| 477 | 'subfields' => [ |
||
| 478 | [ |
||
| 479 | 'name' => 'notes', |
||
| 480 | ], |
||
| 481 | ], |
||
| 482 | ]); |
||
| 483 | |||
| 484 | $faker = Factory::create(); |
||
| 485 | $articleData = [ |
||
| 486 | 'content' => $faker->text(), |
||
| 487 | 'tags' => $faker->words(3, true), |
||
| 488 | 'user_id' => 1, |
||
| 489 | ]; |
||
| 490 | |||
| 491 | $article = Article::create($articleData); |
||
| 492 | |||
| 493 | $inputData = [ |
||
| 494 | 'name' => $faker->name, |
||
| 495 | 'email' => $faker->safeEmail, |
||
| 496 | 'password' => Hash::make($faker->password()), |
||
| 497 | 'remember_token' => null, |
||
| 498 | 'superArticles' => [ |
||
| 499 | [ |
||
| 500 | 'superArticles' => $article->id, |
||
| 501 | 'notes' => 'my first article note', |
||
| 502 | ], |
||
| 503 | ], |
||
| 504 | ]; |
||
| 505 | |||
| 506 | $entry = $this->crudPanel->create($inputData); |
||
| 507 | $updateFields = $this->crudPanel->getUpdateFields($entry->id); |
||
| 508 | |||
| 509 | $this->assertCount(1, $entry->fresh()->superArticles); |
||
| 510 | $this->assertEquals('my first article note', $entry->fresh()->superArticles->first()->pivot->notes); |
||
| 511 | } |
||
| 512 | |||
| 513 | public function testCreateHasOneWithNestedRelationsRepeatableInterface() |
||
| 514 | { |
||
| 515 | $this->crudPanel->setModel(User::class); |
||
| 516 | $this->crudPanel->setOperation('create'); |
||
| 517 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships); |
||
| 518 | $this->crudPanel->addField( |
||
| 519 | [ |
||
| 520 | 'name' => 'accountDetails', |
||
| 521 | 'subfields' => [ |
||
| 522 | [ |
||
| 523 | 'name' => 'nickname', |
||
| 524 | ], |
||
| 525 | [ |
||
| 526 | 'name' => 'profile_picture', |
||
| 527 | ], |
||
| 528 | [ |
||
| 529 | 'name' => 'article', |
||
| 530 | ], |
||
| 531 | [ |
||
| 532 | 'name' => 'addresses', |
||
| 533 | 'subfields' => [ |
||
| 534 | [ |
||
| 535 | 'name' => 'bang', |
||
| 536 | ], |
||
| 537 | [ |
||
| 538 | 'name' => 'street', |
||
| 539 | ], |
||
| 540 | [ |
||
| 541 | 'name' => 'number', |
||
| 542 | ], |
||
| 543 | ], |
||
| 544 | ], |
||
| 545 | [ |
||
| 546 | 'name' => 'bangs', |
||
| 547 | ], |
||
| 548 | [ |
||
| 549 | 'name' => 'bangsPivot', |
||
| 550 | 'subfields' => [ |
||
| 551 | [ |
||
| 552 | 'name' => 'pivot_field', |
||
| 553 | ], |
||
| 554 | ], |
||
| 555 | ], |
||
| 556 | ], |
||
| 557 | ]); |
||
| 558 | |||
| 559 | $faker = Factory::create(); |
||
| 560 | |||
| 561 | $inputData = [ |
||
| 562 | 'name' => $faker->name, |
||
| 563 | 'email' => $faker->safeEmail, |
||
| 564 | 'password' => Hash::make($faker->password()), |
||
| 565 | 'remember_token' => null, |
||
| 566 | 'roles' => [1, 2], |
||
| 567 | 'accountDetails' => [ |
||
| 568 | [ |
||
| 569 | 'nickname' => 'i_have_has_one', |
||
| 570 | 'profile_picture' => 'ohh my picture 1.jpg', |
||
| 571 | 'article' => 1, |
||
| 572 | 'addresses' => [ |
||
| 573 | [ |
||
| 574 | 'bang' => 1, |
||
| 575 | 'street' => 'test', |
||
| 576 | 'number' => 1, |
||
| 577 | ], |
||
| 578 | [ |
||
| 579 | 'bang' => 1, |
||
| 580 | 'street' => 'test2', |
||
| 581 | 'number' => 2, |
||
| 582 | ], |
||
| 583 | ], |
||
| 584 | 'bangs' => [1, 2], |
||
| 585 | 'bangsPivot' => [ |
||
| 586 | ['bangsPivot' => 1, 'pivot_field' => 'test1'], |
||
| 587 | ['bangsPivot' => 2, 'pivot_field' => 'test2'], |
||
| 588 | ], |
||
| 589 | ], |
||
| 590 | ], |
||
| 591 | ]; |
||
| 592 | |||
| 593 | $entry = $this->crudPanel->create($inputData); |
||
| 594 | $updateFields = $this->crudPanel->getUpdateFields($entry->id); |
||
| 595 | $account_details = $entry->accountDetails()->first(); |
||
| 596 | |||
| 597 | $this->assertEquals($account_details->article, Article::find(1)); |
||
| 598 | $this->assertEquals($account_details->addresses->count(), 2); |
||
| 599 | $this->assertEquals($account_details->addresses->first()->city, 1); |
||
| 600 | $this->assertEquals($account_details->addresses->first()->street, 'test'); |
||
| 601 | $this->assertEquals($account_details->addresses->first()->number, 1); |
||
| 602 | $this->assertEquals($account_details->bangs->first()->name, Bang::find(1)->name); |
||
| 603 | $this->assertEquals($account_details->bangsPivot->count(), 2); |
||
| 604 | $this->assertEquals($account_details->bangsPivot->first()->pivot->pivot_field, 'test1'); |
||
| 605 | } |
||
| 606 | |||
| 607 | public function testCreateBelongsToFake() |
||
| 608 | { |
||
| 609 | $belongsToField = [ // select_grouped |
||
| 610 | 'label' => 'Select_grouped', |
||
| 611 | 'type' => 'select_grouped', //https://github.com/Laravel-Backpack/CRUD/issues/502 |
||
| 612 | 'name' => 'bang_relation_field', |
||
| 613 | 'fake' => true, |
||
| 614 | 'entity' => 'bang', |
||
| 615 | 'model' => 'Backpack\CRUD\Tests\config\Models\Bang', |
||
| 616 | 'attribute' => 'title', |
||
| 617 | 'group_by' => 'category', // the relationship to entity you want to use for grouping |
||
| 618 | 'group_by_attribute' => 'name', // the attribute on related model, that you want shown |
||
| 619 | 'group_by_relationship_back' => 'articles', // relationship from related model back to this model |
||
| 620 | 'tab' => 'Selects', |
||
| 621 | 'wrapperAttributes' => ['class' => 'form-group col-md-6'], |
||
| 622 | ]; |
||
| 623 | |||
| 624 | $this->crudPanel->setModel(User::class); |
||
| 625 | $this->crudPanel->setOperation('create'); |
||
| 626 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships); |
||
| 627 | $this->crudPanel->addField($belongsToField); |
||
| 628 | |||
| 629 | $faker = Factory::create(); |
||
| 630 | |||
| 631 | $inputData = [ |
||
| 632 | 'name' => $faker->name, |
||
| 633 | 'email' => $faker->safeEmail, |
||
| 634 | 'password' => Hash::make($faker->password()), |
||
| 635 | 'remember_token' => null, |
||
| 636 | 'bang_relation_field' => 1, |
||
| 637 | ]; |
||
| 638 | |||
| 639 | $entry = $this->crudPanel->create($inputData); |
||
| 640 | $updateFields = $this->crudPanel->getUpdateFields($entry->id); |
||
| 641 | $this->crudPanel->entry = $entry->withFakes(); |
||
| 642 | $this->assertEquals($entry->bang_relation_field, 1); |
||
| 643 | } |
||
| 644 | |||
| 645 | public function testCreateHasOneWithNestedRelations() |
||
| 646 | { |
||
| 647 | $this->crudPanel->setModel(User::class); |
||
| 648 | $this->crudPanel->setOperation('create'); |
||
| 649 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships); |
||
| 650 | $this->crudPanel->addFields([ |
||
| 651 | [ |
||
| 652 | 'name' => 'accountDetails.nickname', |
||
| 653 | ], |
||
| 654 | [ |
||
| 655 | 'name' => 'accountDetails.profile_picture', |
||
| 656 | ], |
||
| 657 | [ |
||
| 658 | 'name' => 'accountDetails.article', |
||
| 659 | ], |
||
| 660 | [ |
||
| 661 | 'name' => 'accountDetails.addresses', |
||
| 662 | 'subfields' => [ |
||
| 663 | [ |
||
| 664 | 'name' => 'city', |
||
| 665 | 'entity' => 'bang', |
||
| 666 | ], |
||
| 667 | [ |
||
| 668 | 'name' => 'street', |
||
| 669 | ], |
||
| 670 | [ |
||
| 671 | 'name' => 'number', |
||
| 672 | ], |
||
| 673 | ], |
||
| 674 | ], |
||
| 675 | [ |
||
| 676 | 'name' => 'accountDetails.bangs', |
||
| 677 | ], |
||
| 678 | [ |
||
| 679 | 'name' => 'accountDetails.bangsPivot', |
||
| 680 | 'subfields' => [ |
||
| 681 | [ |
||
| 682 | 'name' => 'pivot_field', |
||
| 683 | ], |
||
| 684 | ], |
||
| 685 | ], |
||
| 686 | ]); |
||
| 687 | |||
| 688 | $faker = Factory::create(); |
||
| 689 | |||
| 690 | $inputData = [ |
||
| 691 | 'name' => $faker->name, |
||
| 692 | 'email' => $faker->safeEmail, |
||
| 693 | 'password' => Hash::make($faker->password()), |
||
| 694 | 'remember_token' => null, |
||
| 695 | 'roles' => [1, 2], |
||
| 696 | 'accountDetails' => [ |
||
| 697 | 'nickname' => 'i_have_has_one', |
||
| 698 | 'profile_picture' => 'ohh my picture 1.jpg', |
||
| 699 | 'article' => 1, |
||
| 700 | 'addresses' => [ |
||
| 701 | [ |
||
| 702 | 'city' => 1, |
||
| 703 | 'street' => 'test', |
||
| 704 | 'number' => 1, |
||
| 705 | ], |
||
| 706 | [ |
||
| 707 | 'city' => 2, |
||
| 708 | 'street' => 'test2', |
||
| 709 | 'number' => 2, |
||
| 710 | ], |
||
| 711 | ], |
||
| 712 | 'bangs' => [1, 2], |
||
| 713 | 'bangsPivot' => [ |
||
| 714 | ['bangsPivot' => 1, 'pivot_field' => 'test1'], |
||
| 715 | ['bangsPivot' => 2, 'pivot_field' => 'test2'], |
||
| 716 | ], |
||
| 717 | ], |
||
| 718 | ]; |
||
| 719 | |||
| 720 | $entry = $this->crudPanel->create($inputData); |
||
| 721 | $updateFields = $this->crudPanel->getUpdateFields($entry->id); |
||
| 722 | $account_details = $entry->accountDetails()->first(); |
||
| 723 | |||
| 724 | $this->assertEquals($account_details->article, Article::find(1)); |
||
| 725 | $this->assertEquals($account_details->addresses->count(), 2); |
||
| 726 | $this->assertEquals($account_details->addresses->first()->bang->id, 1); |
||
| 727 | $this->assertEquals($account_details->addresses->first()->street, 'test'); |
||
| 728 | $this->assertEquals($account_details->addresses->first()->number, 1); |
||
| 729 | $this->assertEquals($account_details->bangs->first()->name, Bang::find(1)->name); |
||
| 730 | $this->assertEquals($account_details->bangsPivot->count(), 2); |
||
| 731 | $this->assertEquals($account_details->bangsPivot->first()->pivot->pivot_field, 'test1'); |
||
| 732 | |||
| 733 | // Now test the remove process |
||
| 734 | |||
| 735 | $inputData = [ |
||
| 736 | 'name' => $faker->name, |
||
| 737 | 'email' => $faker->safeEmail, |
||
| 738 | 'password' => Hash::make($faker->password()), |
||
| 739 | 'remember_token' => null, |
||
| 740 | 'roles' => [1, 2], |
||
| 741 | 'accountDetails' => [ |
||
| 742 | 'nickname' => 'i_have_has_one', |
||
| 743 | 'profile_picture' => 'ohh my picture 1.jpg', |
||
| 744 | 'article' => 1, |
||
| 745 | 'addresses' => [ // HasOne is tested in other test function |
||
| 746 | [ |
||
| 747 | 'city' => 2, |
||
| 748 | 'street' => 'test', |
||
| 749 | 'number' => 1, |
||
| 750 | ], |
||
| 751 | [ |
||
| 752 | 'city' => 1, |
||
| 753 | 'street' => 'test2', |
||
| 754 | 'number' => 2, |
||
| 755 | ], |
||
| 756 | ], |
||
| 757 | 'bangs' => [], |
||
| 758 | 'bangsPivot' => [], |
||
| 759 | ], |
||
| 760 | ]; |
||
| 761 | |||
| 762 | $entry = $this->crudPanel->update($entry->id, $inputData); |
||
| 763 | $account_details = $entry->accountDetails()->first(); |
||
| 764 | $this->assertEquals($account_details->addresses->count(), 2); |
||
| 765 | $this->assertEquals($account_details->addresses->first()->bang->id, 2); |
||
| 766 | $this->assertEquals($account_details->bangs->count(), 0); |
||
| 767 | $this->assertEquals($account_details->bangsPivot->count(), 0); |
||
| 768 | } |
||
| 769 | |||
| 770 | public function testMorphOneRelationship() |
||
| 771 | { |
||
| 772 | $this->crudPanel->setModel(User::class); |
||
| 773 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both'); |
||
| 774 | $this->crudPanel->addField([ |
||
| 775 | 'name' => 'comment.text', |
||
| 776 | ], 'both'); |
||
| 777 | |||
| 778 | $faker = Factory::create(); |
||
| 779 | $inputData = [ |
||
| 780 | 'name' => $faker->name, |
||
| 781 | 'email' => $faker->safeEmail, |
||
| 782 | 'password' => Hash::make($faker->password()), |
||
| 783 | 'remember_token' => null, |
||
| 784 | 'comment' => [ |
||
| 785 | 'text' => 'some test comment text', |
||
| 786 | ], |
||
| 787 | ]; |
||
| 788 | |||
| 789 | $entry = $this->crudPanel->create($inputData); |
||
| 790 | $updateFields = $this->crudPanel->getUpdateFields($entry->id); |
||
| 791 | |||
| 792 | $this->assertEquals($inputData['comment']['text'], $entry->comment->text); |
||
| 793 | |||
| 794 | $inputData['comment']['text'] = 'updated comment text'; |
||
| 795 | |||
| 796 | $this->crudPanel->update($entry->id, $inputData); |
||
| 797 | |||
| 798 | $this->assertEquals($inputData['comment']['text'], $entry->fresh()->comment->text); |
||
| 799 | } |
||
| 800 | |||
| 801 | public function testMorphManyCreatableRelationship() |
||
| 802 | { |
||
| 803 | $this->crudPanel->setModel(User::class); |
||
| 804 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both'); |
||
| 805 | $this->crudPanel->addField([ |
||
| 806 | 'name' => 'stars', |
||
| 807 | 'subfields' => [ |
||
| 808 | [ |
||
| 809 | 'name' => 'title', |
||
| 810 | ], |
||
| 811 | ], |
||
| 812 | ], 'both'); |
||
| 813 | |||
| 814 | $faker = Factory::create(); |
||
| 815 | $inputData = [ |
||
| 816 | 'name' => $faker->name, |
||
| 817 | 'email' => $faker->safeEmail, |
||
| 818 | 'password' => Hash::make($faker->password()), |
||
| 819 | 'remember_token' => null, |
||
| 820 | 'stars' => [ |
||
| 821 | [ |
||
| 822 | 'id' => null, |
||
| 823 | 'title' => 'this is the star 1 title', |
||
| 824 | ], |
||
| 825 | [ |
||
| 826 | 'id' => null, |
||
| 827 | 'title' => 'this is the star 2 title', |
||
| 828 | ], |
||
| 829 | ], |
||
| 830 | ]; |
||
| 831 | |||
| 832 | $entry = $this->crudPanel->create($inputData); |
||
| 833 | $updateFields = $this->crudPanel->getUpdateFields($entry->id); |
||
| 834 | |||
| 835 | $this->assertCount(2, $entry->stars); |
||
| 836 | |||
| 837 | $this->assertEquals($inputData['stars'][0]['title'], $entry->stars()->first()->title); |
||
| 838 | |||
| 839 | $inputData['stars'] = [ |
||
| 840 | [ |
||
| 841 | 'id' => 1, |
||
| 842 | 'title' => 'only one star with changed title', |
||
| 843 | ], |
||
| 844 | ]; |
||
| 845 | |||
| 846 | $this->crudPanel->update($entry->id, $inputData); |
||
| 847 | |||
| 848 | $this->assertCount(1, $entry->fresh()->stars); |
||
| 849 | |||
| 850 | $this->assertEquals($inputData['stars'][0]['title'], $entry->fresh()->stars->first()->title); |
||
| 851 | $this->assertEquals($inputData['stars'][0]['id'], $entry->fresh()->stars->first()->id); |
||
| 852 | } |
||
| 853 | |||
| 854 | public function testHasManyCreatableRelationship() |
||
| 855 | { |
||
| 856 | $this->crudPanel->setModel(User::class); |
||
| 857 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both'); |
||
| 858 | $this->crudPanel->addField([ |
||
| 859 | 'name' => 'universes', |
||
| 860 | 'subfields' => [ |
||
| 861 | [ |
||
| 862 | 'name' => 'title', |
||
| 863 | ], |
||
| 864 | ], |
||
| 865 | ], 'both'); |
||
| 866 | |||
| 867 | $faker = Factory::create(); |
||
| 868 | $inputData = [ |
||
| 869 | 'name' => $faker->name, |
||
| 870 | 'email' => $faker->safeEmail, |
||
| 871 | 'password' => Hash::make($faker->password()), |
||
| 872 | 'remember_token' => null, |
||
| 873 | 'universes' => [ |
||
| 874 | [ |
||
| 875 | 'id' => null, |
||
| 876 | 'title' => 'this is the star 1 title', |
||
| 877 | ], |
||
| 878 | [ |
||
| 879 | 'title' => 'this is the star 2 title', |
||
| 880 | ], |
||
| 881 | ], |
||
| 882 | ]; |
||
| 883 | |||
| 884 | $entry = $this->crudPanel->create($inputData); |
||
| 885 | $updateFields = $this->crudPanel->getUpdateFields($entry->id); |
||
| 886 | |||
| 887 | $this->assertCount(2, $entry->universes); |
||
| 888 | |||
| 889 | $this->assertEquals($inputData['universes'][0]['title'], $entry->universes()->first()->title); |
||
| 890 | |||
| 891 | $inputData['universes'] = [ |
||
| 892 | [ |
||
| 893 | 'id' => 1, |
||
| 894 | 'title' => 'star 1 with changed title', |
||
| 895 | ], |
||
| 896 | [ |
||
| 897 | 'id' => 2, |
||
| 898 | 'title' => 'star 2 with changed title', |
||
| 899 | ], |
||
| 900 | ]; |
||
| 901 | |||
| 902 | $this->crudPanel->update($entry->id, $inputData); |
||
| 903 | |||
| 904 | $universes = $entry->fresh()->universes; |
||
| 905 | $this->assertCount(2, $universes); |
||
| 906 | $this->assertEquals([1, 2], $universes->pluck('id')->toArray()); |
||
| 907 | |||
| 908 | $inputData['universes'] = [ |
||
| 909 | [ |
||
| 910 | 'id' => 1, |
||
| 911 | 'title' => 'only one star with changed title', |
||
| 912 | ], |
||
| 913 | ]; |
||
| 914 | |||
| 915 | $this->crudPanel->update($entry->id, $inputData); |
||
| 916 | |||
| 917 | $this->assertEquals($inputData['universes'][0]['title'], $entry->fresh()->universes->first()->title); |
||
| 918 | $this->assertEquals($inputData['universes'][0]['id'], $entry->fresh()->universes->first()->id); |
||
| 919 | $this->assertEquals(1, Universe::all()->count()); |
||
| 920 | |||
| 921 | $inputData['universes'] = [ |
||
| 922 | [ |
||
| 923 | 'id' => null, |
||
| 924 | 'title' => 'new star 3', |
||
| 925 | ], |
||
| 926 | ]; |
||
| 927 | |||
| 928 | $this->crudPanel->update($entry->id, $inputData); |
||
| 929 | |||
| 930 | $this->assertEquals($inputData['universes'][0]['title'], $entry->fresh()->universes->first()->title); |
||
| 931 | $this->assertEquals(3, $entry->fresh()->universes->first()->id); |
||
| 932 | $this->assertEquals(1, Universe::all()->count()); |
||
| 933 | |||
| 934 | $inputData['universes'] = null; |
||
| 935 | |||
| 936 | $this->crudPanel->update($entry->id, $inputData); |
||
| 937 | |||
| 938 | $this->assertEquals(0, count($entry->fresh()->universes)); |
||
| 939 | $this->assertEquals(0, Universe::all()->count()); |
||
| 940 | } |
||
| 941 | |||
| 942 | public function testHasManySelectableRelationshipWithoutForceDelete() |
||
| 943 | { |
||
| 944 | $this->crudPanel->setModel(User::class); |
||
| 945 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both'); |
||
| 946 | $this->crudPanel->addField([ |
||
| 947 | 'name' => 'planets', |
||
| 948 | 'force_delete' => false, |
||
| 949 | 'fallback_id' => false, |
||
| 950 | ], 'both'); |
||
| 951 | |||
| 952 | $faker = Factory::create(); |
||
| 953 | $inputData = [ |
||
| 954 | 'name' => $faker->name, |
||
| 955 | 'email' => $faker->safeEmail, |
||
| 956 | 'password' => Hash::make($faker->password()), |
||
| 957 | 'remember_token' => null, |
||
| 958 | 'planets' => [1, 2], |
||
| 959 | ]; |
||
| 960 | |||
| 961 | $entry = $this->crudPanel->create($inputData); |
||
| 962 | $updateFields = $this->crudPanel->getUpdateFields($entry->id); |
||
| 963 | |||
| 964 | $this->assertCount(2, $entry->planets); |
||
| 965 | |||
| 966 | $inputData['planets'] = [1]; |
||
| 967 | |||
| 968 | $this->crudPanel->update($entry->id, $inputData); |
||
| 969 | |||
| 970 | $this->assertCount(1, $entry->fresh()->planets); |
||
| 971 | |||
| 972 | $planets = Planet::all(); |
||
| 973 | |||
| 974 | $this->assertCount(2, $planets); |
||
| 975 | } |
||
| 976 | |||
| 977 | public function testHasManySelectableRelationshipRemoveAllRelations() |
||
| 978 | { |
||
| 979 | $this->crudPanel->setModel(User::class); |
||
| 980 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both'); |
||
| 981 | $this->crudPanel->addField([ |
||
| 982 | 'name' => 'planets', |
||
| 983 | 'force_delete' => false, |
||
| 984 | 'fallback_id' => false, |
||
| 985 | ], 'both'); |
||
| 986 | |||
| 987 | $faker = Factory::create(); |
||
| 988 | $inputData = [ |
||
| 989 | 'name' => $faker->name, |
||
| 990 | 'email' => $faker->safeEmail, |
||
| 991 | 'password' => Hash::make($faker->password()), |
||
| 992 | 'remember_token' => null, |
||
| 993 | 'planets' => [1, 2], |
||
| 994 | ]; |
||
| 995 | |||
| 996 | $entry = $this->crudPanel->create($inputData); |
||
| 997 | |||
| 998 | $this->assertCount(2, $entry->planets); |
||
| 999 | |||
| 1000 | $inputData['planets'] = []; |
||
| 1001 | |||
| 1002 | $this->crudPanel->update($entry->id, $inputData); |
||
| 1003 | |||
| 1004 | $this->assertCount(0, $entry->fresh()->planets); |
||
| 1005 | |||
| 1006 | $planets = Planet::all(); |
||
| 1007 | |||
| 1008 | $this->assertCount(2, $planets); |
||
| 1009 | } |
||
| 1010 | |||
| 1011 | public function testHasManyWithRelationScoped() |
||
| 1129 | } |
||
| 1130 | |||
| 1131 | public function testHasManySelectableRelationshipWithFallbackId() |
||
| 1132 | { |
||
| 1133 | $this->crudPanel->setModel(User::class); |
||
| 1134 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both'); |
||
| 1135 | $this->crudPanel->addField([ |
||
| 1136 | 'name' => 'planets', |
||
| 1137 | 'fallback_id' => 0, |
||
| 1138 | 'force_delete' => false, |
||
| 1139 | ], 'both'); |
||
| 1140 | |||
| 1141 | $faker = Factory::create(); |
||
| 1142 | $inputData = [ |
||
| 1143 | 'name' => $faker->name, |
||
| 1144 | 'email' => $faker->safeEmail, |
||
| 1145 | 'password' => Hash::make($faker->password()), |
||
| 1146 | 'remember_token' => null, |
||
| 1147 | 'planets' => [1, 2], |
||
| 1148 | ]; |
||
| 1149 | |||
| 1150 | $entry = $this->crudPanel->create($inputData); |
||
| 1151 | |||
| 1152 | $this->assertCount(2, $entry->planets); |
||
| 1153 | |||
| 1154 | $inputData['planets'] = [2]; |
||
| 1155 | |||
| 1156 | $this->crudPanel->update($entry->id, $inputData); |
||
| 1157 | |||
| 1158 | $this->assertCount(1, $entry->fresh()->planets); |
||
| 1159 | |||
| 1160 | $planets = Planet::all(); |
||
| 1161 | $this->assertCount(2, $planets); |
||
| 1162 | $this->assertEquals(0, $planets->first()->user_id); |
||
| 1163 | } |
||
| 1164 | |||
| 1165 | public function testHasManySelectableRelationshipWithForceDelete() |
||
| 1166 | { |
||
| 1167 | $this->crudPanel->setModel(User::class); |
||
| 1168 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both'); |
||
| 1169 | $this->crudPanel->addField([ |
||
| 1170 | 'name' => 'planets', |
||
| 1171 | 'force_delete' => true, |
||
| 1172 | 'fallback_id' => false, |
||
| 1173 | ], 'both'); |
||
| 1174 | |||
| 1175 | $faker = Factory::create(); |
||
| 1176 | $inputData = [ |
||
| 1177 | 'name' => $faker->name, |
||
| 1178 | 'email' => $faker->safeEmail, |
||
| 1179 | 'password' => Hash::make($faker->password()), |
||
| 1180 | 'remember_token' => null, |
||
| 1181 | 'planets' => [1, 2], |
||
| 1182 | ]; |
||
| 1183 | |||
| 1184 | $entry = $this->crudPanel->create($inputData); |
||
| 1185 | |||
| 1186 | $this->assertCount(2, $entry->planets); |
||
| 1187 | |||
| 1188 | $inputData['planets'] = [2]; |
||
| 1189 | |||
| 1190 | $this->crudPanel->update($entry->id, $inputData); |
||
| 1191 | |||
| 1192 | $this->assertCount(1, $entry->fresh()->planets); |
||
| 1193 | |||
| 1194 | $planets = Planet::all(); |
||
| 1195 | $this->assertCount(1, $planets); |
||
| 1196 | } |
||
| 1197 | |||
| 1198 | public function testHasManySelectableRelationshipNonNullableForeignKeyAndDefaultInDatabase() |
||
| 1199 | { |
||
| 1200 | $this->crudPanel->setModel(User::class); |
||
| 1201 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both'); |
||
| 1202 | $this->crudPanel->addField([ |
||
| 1203 | 'name' => 'comets', |
||
| 1204 | 'force_delete' => false, |
||
| 1205 | 'fallback_id' => false, |
||
| 1206 | ], 'both'); |
||
| 1207 | |||
| 1208 | $faker = Factory::create(); |
||
| 1209 | $inputData = [ |
||
| 1210 | 'name' => $faker->name, |
||
| 1211 | 'email' => $faker->safeEmail, |
||
| 1212 | 'password' => Hash::make($faker->password()), |
||
| 1213 | 'remember_token' => null, |
||
| 1214 | 'comets' => [1, 2], |
||
| 1215 | ]; |
||
| 1216 | |||
| 1217 | $entry = $this->crudPanel->create($inputData); |
||
| 1218 | |||
| 1219 | $this->assertCount(2, $entry->comets); |
||
| 1220 | |||
| 1221 | $inputData['comets'] = [2]; |
||
| 1222 | |||
| 1223 | $this->crudPanel->update($entry->id, $inputData); |
||
| 1224 | |||
| 1225 | $this->assertCount(1, $entry->fresh()->comets); |
||
| 1226 | |||
| 1227 | $comets = Comet::all(); |
||
| 1228 | $this->assertCount(2, $comets); |
||
| 1229 | $this->assertEquals(0, $comets->first()->user_id); |
||
| 1230 | } |
||
| 1231 | |||
| 1232 | public function testHasManySelectableRelationshipNonNullable() |
||
| 1233 | { |
||
| 1234 | $this->crudPanel->setModel(User::class); |
||
| 1235 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both'); |
||
| 1236 | $this->crudPanel->addField([ |
||
| 1237 | 'name' => 'planetsNonNullable', |
||
| 1238 | 'force_delete' => false, |
||
| 1239 | 'fallback_id' => false, |
||
| 1240 | ], 'both'); |
||
| 1241 | |||
| 1242 | $faker = Factory::create(); |
||
| 1243 | $inputData = [ |
||
| 1244 | 'name' => $faker->name, |
||
| 1245 | 'email' => $faker->safeEmail, |
||
| 1246 | 'password' => Hash::make($faker->password()), |
||
| 1247 | 'remember_token' => null, |
||
| 1248 | 'planetsNonNullable' => [1, 2], |
||
| 1249 | ]; |
||
| 1250 | |||
| 1251 | $entry = $this->crudPanel->create($inputData); |
||
| 1252 | |||
| 1253 | $this->assertCount(2, $entry->planetsNonNullable); |
||
| 1254 | |||
| 1255 | $inputData['planetsNonNullable'] = null; |
||
| 1256 | |||
| 1257 | $this->crudPanel->update($entry->id, $inputData); |
||
| 1258 | |||
| 1259 | $this->assertCount(0, $entry->fresh()->planetsNonNullable); |
||
| 1260 | |||
| 1261 | $planets = PlanetNonNullable::all(); |
||
| 1262 | $this->assertCount(0, $planets); |
||
| 1263 | } |
||
| 1264 | |||
| 1265 | public function testCreateHasManyRelationWithDelimitedNameSubfields() |
||
| 1266 | { |
||
| 1267 | $this->crudPanel->setModel(User::class); |
||
| 1268 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both'); |
||
| 1269 | $this->crudPanel->addField([ |
||
| 1270 | 'name' => 'universes', |
||
| 1271 | 'subfields' => [ |
||
| 1272 | [ |
||
| 1273 | 'name' => 'title', |
||
| 1274 | ], |
||
| 1275 | [ |
||
| 1276 | 'name' => 'start_date,end_date', |
||
| 1277 | 'type' => 'date_range', |
||
| 1278 | ], |
||
| 1279 | ], |
||
| 1280 | ], 'both'); |
||
| 1281 | |||
| 1282 | $faker = Factory::create(); |
||
| 1283 | $inputData = [ |
||
| 1284 | 'name' => $faker->name, |
||
| 1285 | 'email' => $faker->safeEmail, |
||
| 1286 | 'password' => Hash::make($faker->password()), |
||
| 1287 | 'remember_token' => null, |
||
| 1288 | 'universes' => [ |
||
| 1289 | [ |
||
| 1290 | 'id' => null, |
||
| 1291 | 'title' => 'this is the star 1 title', |
||
| 1292 | 'start_date' => '2021-02-26', |
||
| 1293 | 'end_date' => '2091-01-26', |
||
| 1294 | ], |
||
| 1295 | [ |
||
| 1296 | 'title' => 'this is the star 2 title', |
||
| 1297 | 'end_date' => '2021-02-26', |
||
| 1298 | 'start_date' => '2091-01-26', |
||
| 1299 | ], |
||
| 1300 | ], |
||
| 1301 | ]; |
||
| 1302 | |||
| 1303 | $entry = $this->crudPanel->create($inputData); |
||
| 1304 | |||
| 1305 | $this->assertCount(2, $entry->universes); |
||
| 1306 | |||
| 1307 | $this->assertEquals($inputData['universes'][0]['start_date'], $entry->universes()->first()->start_date); |
||
| 1308 | $this->assertEquals($inputData['universes'][0]['end_date'], $entry->universes()->first()->end_date); |
||
| 1309 | $this->assertEquals($inputData['universes'][1]['end_date'], $entry->universes()->find(2)->end_date); |
||
| 1310 | $this->assertEquals($inputData['universes'][1]['start_date'], $entry->universes()->find(2)->start_date); |
||
| 1311 | } |
||
| 1312 | |||
| 1313 | public function testCreateHasOneRelationWithDelimitedNameSubfields() |
||
| 1314 | { |
||
| 1315 | $this->crudPanel->setModel(User::class); |
||
| 1316 | $this->crudPanel->setOperation('create'); |
||
| 1317 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships); |
||
| 1318 | $this->crudPanel->addField( |
||
| 1319 | [ |
||
| 1320 | 'name' => 'accountDetails', |
||
| 1321 | 'subfields' => [ |
||
| 1322 | [ |
||
| 1323 | 'name' => 'nickname', |
||
| 1324 | ], |
||
| 1325 | [ |
||
| 1326 | 'name' => 'start_date,end_date', |
||
| 1327 | ], |
||
| 1328 | [ |
||
| 1329 | 'name' => 'profile_picture', |
||
| 1330 | ], |
||
| 1331 | ], |
||
| 1332 | ]); |
||
| 1333 | |||
| 1334 | $faker = Factory::create(); |
||
| 1335 | |||
| 1336 | $inputData = [ |
||
| 1337 | 'name' => $faker->name, |
||
| 1338 | 'email' => $faker->safeEmail, |
||
| 1339 | 'password' => Hash::make($faker->password()), |
||
| 1340 | 'remember_token' => null, |
||
| 1341 | 'roles' => [1, 2], |
||
| 1342 | 'accountDetails' => [ |
||
| 1343 | [ |
||
| 1344 | 'nickname' => 'i_have_has_one', |
||
| 1345 | 'profile_picture' => 'ohh my picture 1.jpg', |
||
| 1346 | 'start_date' => '2021-02-26', |
||
| 1347 | 'end_date' => '2091-01-26', |
||
| 1348 | ], |
||
| 1349 | ], |
||
| 1350 | ]; |
||
| 1351 | |||
| 1352 | $entry = $this->crudPanel->create($inputData); |
||
| 1353 | $account_details = $entry->accountDetails()->first(); |
||
| 1354 | |||
| 1355 | $this->assertEquals($account_details->start_date, '2021-02-26'); |
||
| 1356 | $this->assertEquals($account_details->end_date, '2091-01-26'); |
||
| 1357 | } |
||
| 1358 | |||
| 1359 | public function testBelongsToManyWithDelimitedNameSubfields() |
||
| 1360 | { |
||
| 1361 | $this->crudPanel->setModel(User::class); |
||
| 1362 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships); |
||
| 1363 | $this->crudPanel->addField([ |
||
| 1364 | 'name' => 'superArticles', |
||
| 1365 | 'subfields' => [ |
||
| 1366 | [ |
||
| 1367 | 'name' => 'notes', |
||
| 1368 | ], |
||
| 1369 | [ |
||
| 1370 | 'name' => 'start_date,end_date', |
||
| 1371 | ], |
||
| 1372 | ], |
||
| 1373 | ]); |
||
| 1374 | |||
| 1375 | $faker = Factory::create(); |
||
| 1376 | $articleData = [ |
||
| 1377 | 'content' => $faker->text(), |
||
| 1378 | 'tags' => $faker->words(3, true), |
||
| 1379 | 'user_id' => 1, |
||
| 1380 | ]; |
||
| 1381 | |||
| 1382 | $article = Article::create($articleData); |
||
| 1383 | |||
| 1384 | $inputData = [ |
||
| 1385 | 'name' => $faker->name, |
||
| 1386 | 'email' => $faker->safeEmail, |
||
| 1387 | 'password' => Hash::make($faker->password()), |
||
| 1388 | 'remember_token' => null, |
||
| 1389 | 'superArticles' => [ |
||
| 1390 | [ |
||
| 1391 | 'superArticles' => $article->id, |
||
| 1392 | 'notes' => 'my first article note', |
||
| 1393 | 'start_date' => '2021-02-26', |
||
| 1394 | 'end_date' => '2091-01-26', |
||
| 1395 | ], |
||
| 1396 | ], |
||
| 1397 | ]; |
||
| 1398 | |||
| 1399 | $entry = $this->crudPanel->create($inputData); |
||
| 1400 | |||
| 1401 | $this->assertCount(1, $entry->fresh()->superArticles); |
||
| 1402 | $superArticle = $entry->fresh()->superArticles->first(); |
||
| 1403 | $this->assertEquals($superArticle->pivot->start_date, '2021-02-26'); |
||
| 1404 | $this->assertEquals($superArticle->pivot->end_date, '2091-01-26'); |
||
| 1405 | |||
| 1406 | $this->crudPanel->getUpdateFields($superArticle->id); |
||
| 1407 | } |
||
| 1408 | |||
| 1409 | public function testItCanCreateMorphToFieldsStructure() |
||
| 1410 | { |
||
| 1411 | $this->crudPanel->setModel(Star::class); |
||
| 1412 | $this->crudPanel->addField([ |
||
| 1413 | 'name' => 'starable', |
||
| 1414 | 'morphOptions' => [ |
||
| 1415 | ['Backpack\CRUD\Tests\config\Models\User', 'User'], |
||
| 1416 | ], |
||
| 1417 | ]); |
||
| 1418 | |||
| 1419 | $this->assertCount(2, $this->crudPanel->fields()['starable']['subfields']); |
||
| 1420 | |||
| 1421 | [$morphTypeField, $morphIdField] = $this->crudPanel->fields()['starable']['subfields']; |
||
| 1422 | |||
| 1423 | $this->assertTrue($morphTypeField['name'] === 'starable_type'); |
||
| 1424 | $this->assertTrue($morphIdField['name'] === 'starable_id'); |
||
| 1425 | } |
||
| 1426 | |||
| 1427 | public function testIPreventsAddingRepeateadMorphOptions() |
||
| 1428 | { |
||
| 1429 | $this->crudPanel->setModel(Star::class); |
||
| 1430 | $this->expectException(\Exception::class); |
||
| 1431 | |||
| 1432 | $this->crudPanel->addField([ |
||
| 1433 | 'name' => 'starable', |
||
| 1434 | 'morphOptions' => [ |
||
| 1435 | ['Backpack\CRUD\Tests\config\Models\User', 'User'], |
||
| 1436 | ['Backpack\CRUD\Tests\config\Models\User', 'User'], |
||
| 1437 | ], |
||
| 1438 | ]); |
||
| 1439 | } |
||
| 1440 | |||
| 1441 | public function testItThrowsErrorIfStringIsNotOnMorphMap() |
||
| 1442 | { |
||
| 1443 | $this->crudPanel->setModel(Star::class); |
||
| 1444 | $this->expectException(\Exception::class); |
||
| 1445 | |||
| 1446 | $this->crudPanel->addField([ |
||
| 1447 | 'name' => 'starable', |
||
| 1448 | 'morphOptions' => [ |
||
| 1449 | ['somethingThatDoesNotExist'], |
||
| 1450 | ], |
||
| 1451 | ]); |
||
| 1452 | } |
||
| 1453 | |||
| 1454 | public function testItCanAddTheOptionsFromTheMorphMap() |
||
| 1471 | } |
||
| 1472 | |||
| 1473 | public function testItThrowsErrorIfDuplicateMorphMapName() |
||
| 1474 | { |
||
| 1475 | $this->crudPanel->setModel(Star::class); |
||
| 1476 | $this->expectException(\Exception::class); |
||
| 1477 | |||
| 1478 | Relation::morphMap([ |
||
| 1479 | 'user' => 'Backpack\CRUD\Tests\config\Models\User', |
||
| 1480 | ]); |
||
| 1487 | ], |
||
| 1488 | ]); |
||
| 1489 | } |
||
| 1490 | } |
||
| 1491 |