We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Total Complexity | 48 |
Total Lines | 1797 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
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 |
||
28 | class CrudPanelCreateTest extends \Backpack\CRUD\Tests\config\CrudPanel\BaseDBCrudPanel |
||
29 | { |
||
30 | private $nonRelationshipField = [ |
||
31 | 'name' => 'field1', |
||
32 | 'label' => 'Field1', |
||
33 | ]; |
||
34 | |||
35 | private $userInputFieldsNoRelationships = [ |
||
36 | [ |
||
37 | 'name' => 'id', |
||
38 | 'type' => 'hidden', |
||
39 | ], [ |
||
40 | 'name' => 'name', |
||
41 | ], [ |
||
42 | 'name' => 'email', |
||
43 | 'type' => 'email', |
||
44 | ], [ |
||
45 | 'name' => 'password', |
||
46 | 'type' => 'password', |
||
47 | ], |
||
48 | ]; |
||
49 | |||
50 | private $articleInputFieldsOneToMany = [ |
||
51 | [ |
||
52 | 'name' => 'id', |
||
53 | 'type' => 'hidden', |
||
54 | ], [ |
||
55 | 'name' => 'content', |
||
56 | ], [ |
||
57 | 'name' => 'tags', |
||
58 | ], [ |
||
59 | 'label' => 'Author', |
||
60 | 'type' => 'select', |
||
61 | 'name' => 'user_id', |
||
62 | 'entity' => 'user', |
||
63 | 'attribute' => 'name', |
||
64 | ], |
||
65 | ]; |
||
66 | |||
67 | private $userInputFieldsManyToMany = [ |
||
68 | [ |
||
69 | 'name' => 'id', |
||
70 | 'type' => 'hidden', |
||
71 | ], [ |
||
72 | 'name' => 'name', |
||
73 | ], [ |
||
74 | 'name' => 'email', |
||
75 | 'type' => 'email', |
||
76 | ], [ |
||
77 | 'name' => 'password', |
||
78 | 'type' => 'password', |
||
79 | ], [ |
||
80 | 'label' => 'Roles', |
||
81 | 'type' => 'select_multiple', |
||
82 | 'name' => 'roles', |
||
83 | 'entity' => 'roles', |
||
84 | 'attribute' => 'name', |
||
85 | 'pivot' => true, |
||
86 | ], |
||
87 | ]; |
||
88 | |||
89 | private $userInputFieldsDotNotation = [ |
||
90 | [ |
||
91 | 'name' => 'id', |
||
92 | 'type' => 'hidden', |
||
93 | ], [ |
||
94 | 'name' => 'name', |
||
95 | ], [ |
||
96 | 'name' => 'email', |
||
97 | 'type' => 'email', |
||
98 | ], [ |
||
99 | 'name' => 'password', |
||
100 | 'type' => 'password', |
||
101 | ], [ |
||
102 | 'label' => 'Roles', |
||
103 | 'type' => 'relationship', |
||
104 | 'name' => 'roles', |
||
105 | 'entity' => 'roles', |
||
106 | 'attribute' => 'name', |
||
107 | ], [ |
||
108 | 'label' => 'Street', |
||
109 | 'name' => 'street', |
||
110 | 'entity' => 'accountDetails.addresses', |
||
111 | 'attribute' => 'street', |
||
112 | ], |
||
113 | ]; |
||
114 | |||
115 | private $userInputHasOneRelation = [ |
||
116 | [ |
||
117 | 'name' => 'accountDetails.nickname', |
||
118 | ], |
||
119 | [ |
||
120 | 'name' => 'accountDetails.profile_picture', |
||
121 | ], |
||
122 | ]; |
||
123 | |||
124 | private $articleInputBelongsToRelationName = [ |
||
125 | [ |
||
126 | 'name' => 'user', |
||
127 | ], |
||
128 | ]; |
||
129 | |||
130 | public function testCreate() |
||
131 | { |
||
132 | $this->crudPanel->setModel(User::class); |
||
133 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships); |
||
134 | $faker = Factory::create(); |
||
135 | $inputData = [ |
||
136 | 'name' => $faker->name, |
||
137 | 'email' => $faker->safeEmail, |
||
138 | 'password' => Hash::make($faker->password()), |
||
139 | ]; |
||
140 | |||
141 | $entry = $this->crudPanel->create($inputData); |
||
142 | |||
143 | $this->assertInstanceOf(User::class, $entry); |
||
144 | $this->assertEntryEquals($inputData, $entry); |
||
145 | $this->assertEmpty($entry->articles); |
||
146 | } |
||
147 | |||
148 | public function testCreateWithOneToOneRelationship() |
||
149 | { |
||
150 | $this->crudPanel->setModel(User::class); |
||
151 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships); |
||
152 | $this->crudPanel->addFields($this->userInputHasOneRelation); |
||
153 | $faker = Factory::create(); |
||
154 | $account_details_nickname = $faker->name; |
||
155 | $inputData = [ |
||
156 | 'name' => $faker->name, |
||
157 | 'email' => $faker->safeEmail, |
||
158 | 'password' => Hash::make($faker->password()), |
||
159 | 'accountDetails' => [ |
||
160 | 'nickname' => $account_details_nickname, |
||
161 | 'profile_picture' => 'test.jpg', |
||
162 | ], |
||
163 | ]; |
||
164 | $entry = $this->crudPanel->create($inputData); |
||
165 | $account_details = $entry->accountDetails()->first(); |
||
166 | |||
167 | $this->assertEquals($account_details->nickname, $account_details_nickname); |
||
168 | } |
||
169 | |||
170 | public function testCreateWithOneToOneRelationshipUsingRepeatableInterface() |
||
171 | { |
||
172 | $this->crudPanel->setModel(User::class); |
||
173 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships); |
||
174 | $this->crudPanel->addField([ |
||
175 | 'name' => 'accountDetails', |
||
176 | 'fields' => [ |
||
177 | [ |
||
178 | 'name' => 'nickname', |
||
179 | ], |
||
180 | [ |
||
181 | 'name' => 'profile_picture', |
||
182 | ], |
||
183 | ], |
||
184 | ]); |
||
185 | $faker = Factory::create(); |
||
186 | $account_details_nickname = $faker->name; |
||
187 | $inputData = [ |
||
188 | 'name' => $faker->name, |
||
189 | 'email' => $faker->safeEmail, |
||
190 | 'password' => Hash::make($faker->password()), |
||
191 | 'accountDetails' => [ |
||
192 | ['nickname' => $account_details_nickname, 'profile_picture' => 'test.jpg'], |
||
193 | ], |
||
194 | ]; |
||
195 | $entry = $this->crudPanel->create($inputData); |
||
196 | $account_details = $entry->accountDetails()->first(); |
||
197 | |||
198 | $this->assertEquals($account_details->nickname, $account_details_nickname); |
||
199 | } |
||
200 | |||
201 | public function testCreateBelongsToWithRelationName() |
||
202 | { |
||
203 | $this->crudPanel->setModel(Article::class); |
||
204 | $this->crudPanel->addFields($this->articleInputFieldsOneToMany); |
||
205 | $this->crudPanel->removeField('user_id'); |
||
206 | $this->crudPanel->addFields($this->articleInputBelongsToRelationName); |
||
207 | $faker = Factory::create(); |
||
208 | $inputData = [ |
||
209 | 'content' => $faker->text(), |
||
210 | 'tags' => $faker->words(3, true), |
||
211 | 'user' => 1, |
||
212 | 'metas' => null, |
||
213 | 'extras' => null, |
||
214 | 'cast_metas' => null, |
||
215 | 'cast_tags' => null, |
||
216 | 'cast_extras' => null, |
||
217 | ]; |
||
218 | $entry = $this->crudPanel->create($inputData); |
||
219 | $userEntry = User::find(1); |
||
220 | $article = Article::where('user_id', 1)->with('user')->get()->last(); |
||
221 | $this->assertEquals($article->user_id, $entry->user_id); |
||
222 | $this->assertEquals($article->id, $entry->id); |
||
223 | } |
||
224 | |||
225 | public function testCreateWithOneToManyRelationship() |
||
226 | { |
||
227 | $this->crudPanel->setModel(Article::class); |
||
228 | $this->crudPanel->addFields($this->articleInputFieldsOneToMany); |
||
229 | $faker = Factory::create(); |
||
230 | $inputData = [ |
||
231 | 'content' => $faker->text(), |
||
232 | 'tags' => $faker->words(3, true), |
||
233 | 'user_id' => 1, |
||
234 | 'metas' => null, |
||
235 | 'extras' => null, |
||
236 | 'cast_metas' => null, |
||
237 | 'cast_tags' => null, |
||
238 | 'cast_extras' => null, |
||
239 | ]; |
||
240 | |||
241 | $entry = $this->crudPanel->create($inputData); |
||
242 | $userEntry = User::find(1); |
||
243 | $article = Article::where('user_id', 1)->with('user')->get()->last(); |
||
244 | $this->assertEntryEquals($inputData, $entry); |
||
245 | $this->assertEquals($article->user_id, $entry->user_id); |
||
246 | $this->assertEquals($article->id, $entry->id); |
||
247 | } |
||
248 | |||
249 | public function testCreateWithManyToManyRelationship() |
||
250 | { |
||
251 | $this->crudPanel->setModel(User::class); |
||
252 | $this->crudPanel->addFields($this->userInputFieldsManyToMany); |
||
253 | $faker = Factory::create(); |
||
254 | $inputData = [ |
||
255 | 'name' => $faker->name, |
||
256 | 'email' => $faker->safeEmail, |
||
257 | 'password' => Hash::make($faker->password()), |
||
258 | 'remember_token' => null, |
||
259 | 'roles' => [1, 2], |
||
260 | ]; |
||
261 | |||
262 | $entry = $this->crudPanel->create($inputData); |
||
263 | |||
264 | $this->assertInstanceOf(User::class, $entry); |
||
265 | $this->assertEntryEquals($inputData, $entry); |
||
266 | } |
||
267 | |||
268 | public function testGetRelationFields() |
||
269 | { |
||
270 | $this->markTestIncomplete('Not correctly implemented'); |
||
271 | |||
272 | $this->crudPanel->setModel(User::class); |
||
273 | $this->crudPanel->addFields($this->userInputFieldsManyToMany, 'create'); |
||
274 | |||
275 | // TODO: fix method and documentation. when 'both' is passed as the $form value, the getRelationFields searches |
||
276 | // for relationship fields in the update fields. |
||
277 | $relationFields = $this->crudPanel->getRelationFields('both'); |
||
278 | |||
279 | $this->assertEquals($this->crudPanel->create_fields['roles'], Arr::last($relationFields)); |
||
280 | } |
||
281 | |||
282 | public function testGetRelationFieldsCreateForm() |
||
283 | { |
||
284 | $this->crudPanel->setModel(User::class); |
||
285 | $this->crudPanel->setOperation('create'); |
||
286 | $this->crudPanel->addFields($this->userInputFieldsManyToMany); |
||
287 | |||
288 | $relationFields = $this->crudPanel->getRelationFields(); |
||
289 | |||
290 | $this->assertEquals($this->crudPanel->get('create.fields')['roles'], Arr::last($relationFields)); |
||
291 | } |
||
292 | |||
293 | public function testGetRelationFieldsUpdateForm() |
||
294 | { |
||
295 | $this->crudPanel->setModel(User::class); |
||
296 | $this->crudPanel->setOperation('update'); |
||
297 | $this->crudPanel->addFields($this->userInputFieldsManyToMany); |
||
298 | |||
299 | $relationFields = $this->crudPanel->getRelationFields(); |
||
300 | |||
301 | $this->assertEquals($this->crudPanel->get('update.fields')['roles'], Arr::last($relationFields)); |
||
302 | } |
||
303 | |||
304 | public function testGetRelationFieldsUnknownForm() |
||
305 | { |
||
306 | $this->markTestIncomplete('Not correctly implemented'); |
||
307 | |||
308 | $this->expectException(\InvalidArgumentException::class); |
||
309 | |||
310 | $this->crudPanel->setModel(User::class); |
||
311 | $this->crudPanel->addFields($this->userInputFieldsManyToMany); |
||
312 | |||
313 | // TODO: this should throw an invalid argument exception but instead it searches for relationship fields in the |
||
314 | // update fields. |
||
315 | $this->crudPanel->getRelationFields('unknownForm'); |
||
316 | } |
||
317 | |||
318 | public function testGetRelationFieldsDotNotation() |
||
319 | { |
||
320 | $this->crudPanel->setModel(User::class); |
||
321 | $this->crudPanel->setOperation('create'); |
||
322 | |||
323 | $this->crudPanel->addFields($this->userInputFieldsDotNotation); |
||
324 | |||
325 | //get all fields with a relation |
||
326 | $relationFields = $this->crudPanel->getRelationFields(); |
||
327 | |||
328 | $this->assertEquals($this->crudPanel->get('create.fields')['street'], Arr::last($relationFields)); |
||
329 | } |
||
330 | |||
331 | public function testCreateHasOneRelations() |
||
332 | { |
||
333 | $this->crudPanel->setModel(User::class); |
||
334 | $this->crudPanel->setOperation('create'); |
||
335 | |||
336 | $this->crudPanel->addFields($this->userInputHasOneRelation); |
||
337 | $faker = Factory::create(); |
||
338 | |||
339 | $inputData = [ |
||
340 | 'name' => $faker->name, |
||
341 | 'email' => $faker->safeEmail, |
||
342 | 'password' => Hash::make($faker->password()), |
||
343 | 'remember_token' => null, |
||
344 | 'roles' => [1, 2], |
||
345 | 'accountDetails' => [ |
||
346 | 'nickname' => 'i_have_has_one', |
||
347 | 'profile_picture' => 'simple_picture.jpg', |
||
348 | ], |
||
349 | ]; |
||
350 | $entry = $this->crudPanel->create($inputData); |
||
351 | $updateFields = $this->crudPanel->getUpdateFields($entry->id); |
||
352 | $account_details = $entry->accountDetails()->first(); |
||
353 | |||
354 | $this->assertEquals($account_details->nickname, 'i_have_has_one'); |
||
355 | } |
||
356 | |||
357 | public function testGetRelationFieldsNoRelations() |
||
358 | { |
||
359 | $this->crudPanel->addField($this->nonRelationshipField); |
||
360 | |||
361 | $relationFields = $this->crudPanel->getRelationFields(); |
||
362 | |||
363 | $this->assertEmpty($relationFields); |
||
364 | } |
||
365 | |||
366 | public function testGetRelationFieldsNoFields() |
||
367 | { |
||
368 | $relationFields = $this->crudPanel->getRelationFields(); |
||
369 | |||
370 | $this->assertEmpty($relationFields); |
||
371 | } |
||
372 | |||
373 | public function testGetRelationFieldsWithPivot() |
||
374 | { |
||
375 | $this->crudPanel->setModel(User::class); |
||
376 | $this->crudPanel->setOperation('create'); |
||
377 | $this->crudPanel->addFields($this->userInputFieldsDotNotation); |
||
378 | |||
379 | $relationFields = $this->crudPanel->getRelationFieldsWithPivot(); |
||
380 | $this->assertEquals($this->crudPanel->get('create.fields')['roles'], Arr::first($relationFields)); |
||
381 | } |
||
382 | |||
383 | public function testGetRelationFieldsWithPivotNoRelations() |
||
392 | } |
||
393 | |||
394 | public function testMorphToManySelectableRelationship() |
||
395 | { |
||
396 | $this->crudPanel->setModel(User::class); |
||
397 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both'); |
||
398 | $this->crudPanel->addField(['name' => 'bills'], 'both'); |
||
399 | |||
400 | $faker = Factory::create(); |
||
401 | $inputData = [ |
||
402 | 'name' => $faker->name, |
||
403 | 'email' => $faker->safeEmail, |
||
404 | 'password' => Hash::make($faker->password()), |
||
405 | 'remember_token' => null, |
||
406 | 'bills' => [1], |
||
407 | ]; |
||
408 | |||
409 | $entry = $this->crudPanel->create($inputData); |
||
410 | |||
411 | $updateFields = $this->crudPanel->getUpdateFields($entry->id); |
||
412 | |||
413 | $this->assertCount(1, $entry->bills); |
||
414 | |||
415 | $this->assertEquals(1, $entry->bills()->first()->id); |
||
416 | |||
417 | $inputData['bills'] = [1, 2]; |
||
418 | |||
419 | $this->crudPanel->update($entry->id, $inputData); |
||
420 | |||
421 | $this->assertCount(2, $entry->fresh()->bills); |
||
422 | |||
423 | $this->assertEquals([1, 2], $entry->fresh()->bills->pluck('id')->toArray()); |
||
424 | } |
||
425 | |||
426 | public function testMorphToManyCreatableRelationship() |
||
427 | { |
||
428 | $this->crudPanel->setModel(User::class); |
||
429 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both'); |
||
430 | $this->crudPanel->addField(['name' => 'recommends', 'subfields' => [ |
||
431 | [ |
||
432 | 'name' => 'text', |
||
433 | ], |
||
434 | ]], 'both'); |
||
435 | |||
436 | $faker = Factory::create(); |
||
437 | $inputData = [ |
||
438 | 'name' => $faker->name, |
||
439 | 'email' => $faker->safeEmail, |
||
440 | 'password' => Hash::make($faker->password()), |
||
441 | 'remember_token' => null, |
||
442 | 'recommends' => [ |
||
443 | [ |
||
444 | 'recommends' => 1, |
||
445 | 'text' => 'my pivot recommend field', |
||
446 | ], |
||
447 | ], |
||
448 | ]; |
||
449 | |||
450 | $entry = $this->crudPanel->create($inputData); |
||
451 | $updateFields = $this->crudPanel->getUpdateFields($entry->id); |
||
452 | |||
453 | $this->assertCount(1, $entry->recommends); |
||
454 | |||
455 | $this->assertEquals(1, $entry->recommends()->first()->id); |
||
456 | |||
457 | $inputData['recommends'] = [ |
||
458 | [ |
||
459 | 'recommends' => 2, |
||
460 | 'text' => 'I changed the recommend and the pivot text', |
||
461 | ], |
||
462 | ]; |
||
463 | |||
464 | $this->crudPanel->update($entry->id, $inputData); |
||
465 | |||
466 | $this->assertCount(1, $entry->fresh()->recommends); |
||
467 | |||
468 | $this->assertEquals(2, $entry->recommends()->first()->id); |
||
469 | |||
470 | $this->assertEquals('I changed the recommend and the pivot text', $entry->fresh()->recommends->first()->pivot->text); |
||
471 | } |
||
472 | |||
473 | public function testMorphToManyCreatableRelationshipWithMultiple() |
||
474 | { |
||
475 | $inputData = $this->getPivotInputData(['recommendsDuplicate' => [ |
||
476 | [ |
||
477 | 'recommendsDuplicate' => 1, |
||
478 | 'text' => 'my pivot recommend field 1', |
||
479 | ], |
||
480 | [ |
||
481 | 'recommendsDuplicate' => 2, |
||
482 | 'text' => 'my pivot recommend field 2', |
||
483 | ], |
||
484 | [ |
||
485 | 'recommendsDuplicate' => 1, |
||
486 | 'text' => 'my pivot recommend field 1x1', |
||
487 | ], |
||
488 | ], |
||
489 | ], true, true); |
||
490 | |||
491 | $entry = $this->crudPanel->create($inputData); |
||
492 | |||
493 | $entry = $entry->fresh(); |
||
494 | |||
495 | $this->assertCount(3, $entry->recommendsDuplicate); |
||
496 | |||
497 | $this->assertEquals(1, $entry->recommendsDuplicate[0]->id); |
||
498 | $this->assertEquals(1, $entry->recommendsDuplicate[2]->id); |
||
499 | |||
500 | $inputData['recommendsDuplicate'] = [ |
||
501 | [ |
||
502 | 'recommendsDuplicate' => 1, |
||
503 | 'text' => 'I changed the recommend and the pivot text', |
||
504 | 'id' => 1, |
||
505 | ], |
||
506 | [ |
||
507 | 'recommendsDuplicate' => 2, |
||
508 | 'text' => 'I changed the recommend and the pivot text 2', |
||
509 | 'id' => 2, |
||
510 | ], |
||
511 | [ |
||
512 | 'recommendsDuplicate' => 3, |
||
513 | 'text' => 'new recommend and the pivot text 3', |
||
514 | 'id' => null, |
||
515 | ], |
||
516 | ]; |
||
517 | |||
518 | $this->crudPanel->update($entry->id, $inputData); |
||
519 | |||
520 | $entry = $entry->fresh(); |
||
521 | |||
522 | $this->assertCount(3, $entry->recommendsDuplicate); |
||
523 | $this->assertDatabaseCount('recommendables', 3); |
||
524 | |||
525 | $this->assertEquals('I changed the recommend and the pivot text', $entry->recommendsDuplicate[0]->pivot->text); |
||
526 | $this->assertEquals('I changed the recommend and the pivot text 2', $entry->recommendsDuplicate[1]->pivot->text); |
||
527 | $this->assertEquals('new recommend and the pivot text 3', $entry->recommendsDuplicate[2]->pivot->text); |
||
528 | } |
||
529 | |||
530 | public function testBelongsToManyWithPivotDataRelationship() |
||
569 | } |
||
570 | |||
571 | public function testBelongsToManyWithMultipleSameRelationIdAndPivotDataRelationship() |
||
572 | { |
||
573 | $inputData = $this->getPivotInputData(['superArticlesDuplicates' => [ |
||
574 | [ |
||
575 | 'superArticlesDuplicates' => 1, |
||
576 | 'notes' => 'my first article note', |
||
577 | 'id' => null, |
||
578 | ], |
||
579 | [ |
||
580 | 'superArticlesDuplicates' => 1, |
||
581 | 'notes' => 'my second article note', |
||
582 | 'id' => null, |
||
583 | ], |
||
584 | [ |
||
585 | 'superArticlesDuplicates' => 2, |
||
586 | 'notes' => 'my first article2 note', |
||
587 | 'id' => null, |
||
588 | ], |
||
589 | ], |
||
590 | ], true, true); |
||
591 | |||
592 | $entry = $this->crudPanel->create($inputData); |
||
593 | $relationField = $this->crudPanel->getUpdateFields($entry->id)['superArticlesDuplicates']; |
||
594 | |||
595 | $this->assertCount(3, $relationField['value']); |
||
596 | |||
597 | $entry = $entry->fresh(); |
||
598 | |||
599 | $this->assertCount(3, $entry->superArticlesDuplicates); |
||
600 | $this->assertEquals('my first article note', $entry->superArticles->first()->pivot->notes); |
||
601 | $this->assertEquals('my second article note', $entry->superArticles[1]->pivot->notes); |
||
602 | $this->assertEquals('my first article2 note', $entry->superArticles[2]->pivot->notes); |
||
603 | |||
604 | $inputData = $this->getPivotInputData(['superArticlesDuplicates' => [ |
||
605 | [ |
||
606 | 'superArticlesDuplicates' => 1, |
||
607 | 'notes' => 'my first article note updated', |
||
608 | 'id' => 1, |
||
609 | ], |
||
610 | [ |
||
611 | 'superArticlesDuplicates' => 1, |
||
612 | 'notes' => 'my second article note updated', |
||
613 | 'id' => 2, |
||
614 | ], |
||
615 | [ |
||
616 | 'superArticlesDuplicates' => 2, |
||
617 | 'notes' => 'my first article2 note updated', |
||
618 | 'id' => 3, |
||
619 | ], |
||
620 | ], |
||
621 | ], false, true); |
||
622 | |||
623 | $entry = $this->crudPanel->update($entry->id, $inputData); |
||
624 | $relationField = $this->crudPanel->getUpdateFields($entry->id)['superArticlesDuplicates']; |
||
625 | $this->assertCount(3, $relationField['value']); |
||
626 | |||
627 | $entry = $entry->fresh(); |
||
628 | |||
629 | $this->assertCount(3, $entry->superArticlesDuplicates); |
||
630 | $this->assertEquals('my first article note updated', $entry->superArticles[0]->pivot->notes); |
||
631 | $this->assertEquals('my second article note updated', $entry->superArticles[1]->pivot->notes); |
||
632 | $this->assertEquals('my first article2 note updated', $entry->superArticles[2]->pivot->notes); |
||
633 | } |
||
634 | |||
635 | public function testBelongsToManyAlwaysSaveSinglePivotWhenMultipleNotAllowed() |
||
636 | { |
||
637 | $inputData = $this->getPivotInputData(['superArticlesDuplicates' => [ |
||
638 | [ |
||
639 | 'superArticlesDuplicates' => 1, |
||
640 | 'notes' => 'my first article note', |
||
641 | 'id' => null, |
||
642 | ], |
||
643 | [ |
||
644 | 'superArticlesDuplicates' => 1, |
||
645 | 'notes' => 'my second article note', |
||
646 | 'id' => null, |
||
647 | ], |
||
648 | [ |
||
649 | 'superArticlesDuplicates' => 2, |
||
650 | 'notes' => 'my first article2 note', |
||
651 | 'id' => null, |
||
652 | ], |
||
653 | ], |
||
654 | ]); |
||
655 | |||
656 | $entry = $this->crudPanel->create($inputData); |
||
657 | $relationField = $this->crudPanel->getUpdateFields($entry->id)['superArticlesDuplicates']; |
||
658 | |||
659 | $this->assertCount(2, $relationField['value']); |
||
660 | |||
661 | $entry = $entry->fresh(); |
||
662 | |||
663 | $this->assertCount(2, $entry->superArticlesDuplicates); |
||
664 | $this->assertEquals('my second article note', $entry->superArticles[0]->pivot->notes); |
||
665 | $this->assertEquals('my first article2 note', $entry->superArticles[1]->pivot->notes); |
||
666 | } |
||
667 | |||
668 | public function testBelongsToManyDeletesPivotData() |
||
723 | } |
||
724 | |||
725 | public function testCreateHasOneWithNestedRelationsRepeatableInterface() |
||
726 | { |
||
727 | $this->crudPanel->setModel(User::class); |
||
728 | $this->crudPanel->setOperation('create'); |
||
729 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships); |
||
730 | $this->crudPanel->addField( |
||
731 | [ |
||
732 | 'name' => 'accountDetails', |
||
733 | 'subfields' => [ |
||
734 | [ |
||
735 | 'name' => 'nickname', |
||
736 | ], |
||
737 | [ |
||
738 | 'name' => 'profile_picture', |
||
739 | ], |
||
740 | [ |
||
741 | 'name' => 'article', |
||
742 | ], |
||
743 | [ |
||
744 | 'name' => 'addresses', |
||
745 | 'subfields' => [ |
||
746 | [ |
||
747 | 'name' => 'bang', |
||
748 | ], |
||
749 | [ |
||
750 | 'name' => 'street', |
||
751 | ], |
||
752 | [ |
||
753 | 'name' => 'number', |
||
754 | ], |
||
755 | ], |
||
756 | ], |
||
757 | [ |
||
758 | 'name' => 'bangs', |
||
759 | ], |
||
760 | [ |
||
761 | 'name' => 'bangsPivot', |
||
762 | 'subfields' => [ |
||
763 | [ |
||
764 | 'name' => 'pivot_field', |
||
765 | ], |
||
766 | ], |
||
767 | ], |
||
768 | ], |
||
769 | ]); |
||
770 | |||
771 | $faker = Factory::create(); |
||
772 | |||
773 | $inputData = [ |
||
774 | 'name' => $faker->name, |
||
775 | 'email' => $faker->safeEmail, |
||
776 | 'password' => Hash::make($faker->password()), |
||
777 | 'remember_token' => null, |
||
778 | 'roles' => [1, 2], |
||
779 | 'accountDetails' => [ |
||
780 | [ |
||
781 | 'nickname' => 'i_have_has_one', |
||
782 | 'profile_picture' => 'ohh my picture 1.jpg', |
||
783 | 'article' => 1, |
||
784 | 'addresses' => [ |
||
785 | [ |
||
786 | 'bang' => 1, |
||
787 | 'street' => 'test', |
||
788 | 'number' => 1, |
||
789 | ], |
||
790 | [ |
||
791 | 'bang' => 1, |
||
792 | 'street' => 'test2', |
||
793 | 'number' => 2, |
||
794 | ], |
||
795 | ], |
||
796 | 'bangs' => [1, 2], |
||
797 | 'bangsPivot' => [ |
||
798 | ['bangsPivot' => 1, 'pivot_field' => 'test1'], |
||
799 | ['bangsPivot' => 2, 'pivot_field' => 'test2'], |
||
800 | ], |
||
801 | ], |
||
802 | ], |
||
803 | ]; |
||
804 | |||
805 | $entry = $this->crudPanel->create($inputData); |
||
806 | $updateFields = $this->crudPanel->getUpdateFields($entry->id); |
||
807 | $account_details = $entry->accountDetails()->first(); |
||
808 | |||
809 | $this->assertEquals($account_details->article, Article::find(1)); |
||
810 | $this->assertEquals($account_details->addresses->count(), 2); |
||
811 | $this->assertEquals($account_details->addresses->first()->city, 1); |
||
812 | $this->assertEquals($account_details->addresses->first()->street, 'test'); |
||
813 | $this->assertEquals($account_details->addresses->first()->number, 1); |
||
814 | $this->assertEquals($account_details->bangs->first()->name, Bang::find(1)->name); |
||
815 | $this->assertEquals($account_details->bangsPivot->count(), 2); |
||
816 | $this->assertEquals($account_details->bangsPivot->first()->pivot->pivot_field, 'test1'); |
||
817 | } |
||
818 | |||
819 | public function testCreateBelongsToFake() |
||
820 | { |
||
821 | $belongsToField = [ // select_grouped |
||
822 | 'label' => 'Select_grouped', |
||
823 | 'type' => 'select_grouped', //https://github.com/Laravel-Backpack/CRUD/issues/502 |
||
824 | 'name' => 'bang_relation_field', |
||
825 | 'fake' => true, |
||
826 | 'entity' => 'bang', |
||
827 | 'model' => 'Backpack\CRUD\Tests\config\Models\Bang', |
||
828 | 'attribute' => 'title', |
||
829 | 'group_by' => 'category', // the relationship to entity you want to use for grouping |
||
830 | 'group_by_attribute' => 'name', // the attribute on related model, that you want shown |
||
831 | 'group_by_relationship_back' => 'articles', // relationship from related model back to this model |
||
832 | 'tab' => 'Selects', |
||
833 | 'wrapperAttributes' => ['class' => 'form-group col-md-6'], |
||
834 | ]; |
||
835 | |||
836 | $this->crudPanel->setModel(User::class); |
||
837 | $this->crudPanel->setOperation('create'); |
||
838 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships); |
||
839 | $this->crudPanel->addField($belongsToField); |
||
840 | |||
841 | $faker = Factory::create(); |
||
842 | |||
843 | $inputData = [ |
||
844 | 'name' => $faker->name, |
||
845 | 'email' => $faker->safeEmail, |
||
846 | 'password' => Hash::make($faker->password()), |
||
847 | 'remember_token' => null, |
||
848 | 'bang_relation_field' => 1, |
||
849 | ]; |
||
850 | |||
851 | $entry = $this->crudPanel->create($inputData); |
||
852 | $updateFields = $this->crudPanel->getUpdateFields($entry->id); |
||
853 | $this->crudPanel->entry = $entry->withFakes(); |
||
854 | $this->assertEquals($entry->bang_relation_field, 1); |
||
855 | } |
||
856 | |||
857 | public function testCreateHasOneWithNestedRelations() |
||
980 | } |
||
981 | |||
982 | public function testCreateHasOneWithNestedRelationAsTheFirstField() |
||
983 | { |
||
984 | $this->crudPanel->setModel(User::class); |
||
985 | $this->crudPanel->setOperation('create'); |
||
986 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships); |
||
987 | $this->crudPanel->addFields([ |
||
988 | [ |
||
989 | 'name' => 'accountDetails.article', |
||
990 | ], |
||
991 | [ |
||
992 | 'name' => 'accountDetails.nickname', |
||
993 | ], |
||
994 | [ |
||
995 | 'name' => 'accountDetails.profile_picture', |
||
996 | ], |
||
997 | ]); |
||
998 | |||
999 | $faker = Factory::create(); |
||
1000 | |||
1001 | $inputData = [ |
||
1002 | 'name' => $faker->name, |
||
1003 | 'email' => $faker->safeEmail, |
||
1004 | 'password' => Hash::make($faker->password()), |
||
1005 | 'remember_token' => null, |
||
1006 | 'roles' => [1, 2], |
||
1007 | 'accountDetails' => [ |
||
1008 | 'article' => 1, |
||
1009 | 'nickname' => 'i_have_has_one', |
||
1010 | 'profile_picture' => 'ohh my picture 1.jpg', |
||
1011 | ], |
||
1012 | ]; |
||
1013 | |||
1014 | $entry = $this->crudPanel->create($inputData); |
||
1015 | $updateFields = $this->crudPanel->getUpdateFields($entry->id); |
||
1016 | $account_details = $entry->accountDetails()->first(); |
||
1017 | |||
1018 | $this->assertEquals($account_details->article, Article::find(1)); |
||
1019 | } |
||
1020 | |||
1021 | public function testMorphOneRelationship() |
||
1022 | { |
||
1023 | $this->crudPanel->setModel(User::class); |
||
1024 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both'); |
||
1025 | $this->crudPanel->addField([ |
||
1026 | 'name' => 'comment.text', |
||
1027 | ], 'both'); |
||
1028 | |||
1029 | $faker = Factory::create(); |
||
1030 | $inputData = [ |
||
1031 | 'name' => $faker->name, |
||
1032 | 'email' => $faker->safeEmail, |
||
1033 | 'password' => Hash::make($faker->password()), |
||
1034 | 'remember_token' => null, |
||
1035 | 'comment' => [ |
||
1036 | 'text' => 'some test comment text', |
||
1037 | ], |
||
1038 | ]; |
||
1039 | |||
1040 | $entry = $this->crudPanel->create($inputData); |
||
1041 | $updateFields = $this->crudPanel->getUpdateFields($entry->id); |
||
1042 | |||
1043 | $this->assertEquals($inputData['comment']['text'], $entry->comment->text); |
||
1044 | |||
1045 | $inputData['comment']['text'] = 'updated comment text'; |
||
1046 | |||
1047 | $this->crudPanel->update($entry->id, $inputData); |
||
1048 | |||
1049 | $this->assertEquals($inputData['comment']['text'], $entry->fresh()->comment->text); |
||
1050 | } |
||
1051 | |||
1052 | public function testMorphManyCreatableRelationship() |
||
1053 | { |
||
1054 | $this->crudPanel->setModel(User::class); |
||
1055 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both'); |
||
1056 | $this->crudPanel->addField([ |
||
1057 | 'name' => 'stars', |
||
1058 | 'subfields' => [ |
||
1059 | [ |
||
1060 | 'name' => 'title', |
||
1061 | ], |
||
1062 | ], |
||
1063 | ], 'both'); |
||
1064 | |||
1065 | $faker = Factory::create(); |
||
1066 | $inputData = [ |
||
1067 | 'name' => $faker->name, |
||
1068 | 'email' => $faker->safeEmail, |
||
1069 | 'password' => Hash::make($faker->password()), |
||
1070 | 'remember_token' => null, |
||
1071 | 'stars' => [ |
||
1072 | [ |
||
1073 | 'id' => null, |
||
1074 | 'title' => 'this is the star 1 title', |
||
1075 | ], |
||
1076 | [ |
||
1077 | 'id' => null, |
||
1078 | 'title' => 'this is the star 2 title', |
||
1079 | ], |
||
1080 | ], |
||
1081 | ]; |
||
1082 | |||
1083 | $entry = $this->crudPanel->create($inputData); |
||
1084 | $updateFields = $this->crudPanel->getUpdateFields($entry->id); |
||
1085 | |||
1086 | $this->assertCount(2, $entry->stars); |
||
1087 | |||
1088 | $this->assertEquals($inputData['stars'][0]['title'], $entry->stars()->first()->title); |
||
1089 | |||
1090 | $inputData['stars'] = [ |
||
1091 | [ |
||
1092 | 'id' => 1, |
||
1093 | 'title' => 'only one star with changed title', |
||
1094 | ], |
||
1095 | ]; |
||
1096 | |||
1097 | $this->crudPanel->update($entry->id, $inputData); |
||
1098 | |||
1099 | $this->assertCount(1, $entry->fresh()->stars); |
||
1100 | |||
1101 | $this->assertEquals($inputData['stars'][0]['title'], $entry->fresh()->stars->first()->title); |
||
1102 | $this->assertEquals($inputData['stars'][0]['id'], $entry->fresh()->stars->first()->id); |
||
1103 | } |
||
1104 | |||
1105 | public function testHasManyCreatableRelationship() |
||
1106 | { |
||
1107 | $this->crudPanel->setModel(User::class); |
||
1108 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both'); |
||
1109 | $this->crudPanel->addField([ |
||
1110 | 'name' => 'universes', |
||
1111 | 'subfields' => [ |
||
1112 | [ |
||
1113 | 'name' => 'title', |
||
1114 | ], |
||
1115 | ], |
||
1116 | ], 'both'); |
||
1117 | |||
1118 | $faker = Factory::create(); |
||
1119 | $inputData = [ |
||
1120 | 'name' => $faker->name, |
||
1121 | 'email' => $faker->safeEmail, |
||
1122 | 'password' => Hash::make($faker->password()), |
||
1123 | 'remember_token' => null, |
||
1124 | 'universes' => [ |
||
1125 | [ |
||
1126 | 'id' => null, |
||
1127 | 'title' => 'this is the star 1 title', |
||
1128 | ], |
||
1129 | [ |
||
1130 | 'title' => 'this is the star 2 title', |
||
1131 | ], |
||
1132 | ], |
||
1133 | ]; |
||
1134 | |||
1135 | $entry = $this->crudPanel->create($inputData); |
||
1136 | $updateFields = $this->crudPanel->getUpdateFields($entry->id); |
||
1137 | |||
1138 | $this->assertCount(2, $entry->universes); |
||
1139 | |||
1140 | $this->assertEquals($inputData['universes'][0]['title'], $entry->universes()->first()->title); |
||
1141 | |||
1142 | $inputData['universes'] = [ |
||
1143 | [ |
||
1144 | 'id' => 1, |
||
1145 | 'title' => 'star 1 with changed title', |
||
1146 | ], |
||
1147 | [ |
||
1148 | 'id' => 2, |
||
1149 | 'title' => 'star 2 with changed title', |
||
1150 | ], |
||
1151 | ]; |
||
1152 | |||
1153 | $this->crudPanel->update($entry->id, $inputData); |
||
1154 | |||
1155 | $universes = $entry->fresh()->universes; |
||
1156 | $this->assertCount(2, $universes); |
||
1157 | $this->assertEquals([1, 2], $universes->pluck('id')->toArray()); |
||
1158 | |||
1159 | $inputData['universes'] = [ |
||
1160 | [ |
||
1161 | 'id' => 1, |
||
1162 | 'title' => 'only one star with changed title', |
||
1163 | ], |
||
1164 | ]; |
||
1165 | |||
1166 | $this->crudPanel->update($entry->id, $inputData); |
||
1167 | |||
1168 | $this->assertEquals($inputData['universes'][0]['title'], $entry->fresh()->universes->first()->title); |
||
1169 | $this->assertEquals($inputData['universes'][0]['id'], $entry->fresh()->universes->first()->id); |
||
1170 | $this->assertEquals(1, Universe::all()->count()); |
||
1171 | |||
1172 | $inputData['universes'] = [ |
||
1173 | [ |
||
1174 | 'id' => null, |
||
1175 | 'title' => 'new star 3', |
||
1176 | ], |
||
1177 | ]; |
||
1178 | |||
1179 | $this->crudPanel->update($entry->id, $inputData); |
||
1180 | |||
1181 | $this->assertEquals($inputData['universes'][0]['title'], $entry->fresh()->universes->first()->title); |
||
1182 | $this->assertEquals(3, $entry->fresh()->universes->first()->id); |
||
1183 | $this->assertEquals(1, Universe::all()->count()); |
||
1184 | |||
1185 | $inputData['universes'] = null; |
||
1186 | |||
1187 | $this->crudPanel->update($entry->id, $inputData); |
||
1188 | |||
1189 | $this->assertEquals(0, count($entry->fresh()->universes)); |
||
1190 | $this->assertEquals(0, Universe::all()->count()); |
||
1191 | } |
||
1192 | |||
1193 | public function testHasManySelectableRelationshipWithoutForceDelete() |
||
1194 | { |
||
1195 | $this->crudPanel->setModel(User::class); |
||
1196 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both'); |
||
1197 | $this->crudPanel->addField([ |
||
1198 | 'name' => 'planets', |
||
1199 | 'force_delete' => false, |
||
1200 | 'fallback_id' => false, |
||
1201 | ], 'both'); |
||
1202 | |||
1203 | $faker = Factory::create(); |
||
1204 | $inputData = [ |
||
1205 | 'name' => $faker->name, |
||
1206 | 'email' => $faker->safeEmail, |
||
1207 | 'password' => Hash::make($faker->password()), |
||
1208 | 'remember_token' => null, |
||
1209 | 'planets' => [1, 2], |
||
1210 | ]; |
||
1211 | |||
1212 | $entry = $this->crudPanel->create($inputData); |
||
1213 | $updateFields = $this->crudPanel->getUpdateFields($entry->id); |
||
1214 | |||
1215 | $this->assertCount(2, $entry->planets); |
||
1216 | |||
1217 | $inputData['planets'] = [1]; |
||
1218 | |||
1219 | $this->crudPanel->update($entry->id, $inputData); |
||
1220 | |||
1221 | $this->assertCount(1, $entry->fresh()->planets); |
||
1222 | |||
1223 | $planets = Planet::all(); |
||
1224 | |||
1225 | $this->assertCount(2, $planets); |
||
1226 | } |
||
1227 | |||
1228 | public function testHasManySelectableRelationshipRemoveAllRelations() |
||
1229 | { |
||
1230 | $this->crudPanel->setModel(User::class); |
||
1231 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both'); |
||
1232 | $this->crudPanel->addField([ |
||
1233 | 'name' => 'planets', |
||
1234 | 'force_delete' => false, |
||
1235 | 'fallback_id' => false, |
||
1236 | ], 'both'); |
||
1237 | |||
1238 | $faker = Factory::create(); |
||
1239 | $inputData = [ |
||
1240 | 'name' => $faker->name, |
||
1241 | 'email' => $faker->safeEmail, |
||
1242 | 'password' => Hash::make($faker->password()), |
||
1243 | 'remember_token' => null, |
||
1244 | 'planets' => [1, 2], |
||
1245 | ]; |
||
1246 | |||
1247 | $entry = $this->crudPanel->create($inputData); |
||
1248 | |||
1249 | $this->assertCount(2, $entry->planets); |
||
1250 | |||
1251 | $inputData['planets'] = []; |
||
1252 | |||
1253 | $this->crudPanel->update($entry->id, $inputData); |
||
1254 | |||
1255 | $this->assertCount(0, $entry->fresh()->planets); |
||
1256 | |||
1257 | $planets = Planet::all(); |
||
1258 | |||
1259 | $this->assertCount(2, $planets); |
||
1260 | } |
||
1261 | |||
1262 | public function testHasManyWithRelationScoped() |
||
1380 | } |
||
1381 | |||
1382 | public function testHasManySelectableRelationshipWithFallbackId() |
||
1383 | { |
||
1384 | $this->crudPanel->setModel(User::class); |
||
1385 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both'); |
||
1386 | $this->crudPanel->addField([ |
||
1387 | 'name' => 'planets', |
||
1388 | 'fallback_id' => 0, |
||
1389 | 'force_delete' => false, |
||
1390 | ], 'both'); |
||
1391 | |||
1392 | $faker = Factory::create(); |
||
1393 | $inputData = [ |
||
1394 | 'name' => $faker->name, |
||
1395 | 'email' => $faker->safeEmail, |
||
1396 | 'password' => Hash::make($faker->password()), |
||
1397 | 'remember_token' => null, |
||
1398 | 'planets' => [1, 2], |
||
1399 | ]; |
||
1400 | |||
1401 | $entry = $this->crudPanel->create($inputData); |
||
1402 | |||
1403 | $this->assertCount(2, $entry->planets); |
||
1404 | |||
1405 | $inputData['planets'] = [2]; |
||
1406 | |||
1407 | $this->crudPanel->update($entry->id, $inputData); |
||
1408 | |||
1409 | $this->assertCount(1, $entry->fresh()->planets); |
||
1410 | |||
1411 | $planets = Planet::all(); |
||
1412 | $this->assertCount(2, $planets); |
||
1413 | $this->assertEquals(0, $planets->first()->user_id); |
||
1414 | } |
||
1415 | |||
1416 | public function testHasManySelectableRelationshipWithForceDelete() |
||
1417 | { |
||
1418 | $this->crudPanel->setModel(User::class); |
||
1419 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both'); |
||
1420 | $this->crudPanel->addField([ |
||
1421 | 'name' => 'planets', |
||
1422 | 'force_delete' => true, |
||
1423 | 'fallback_id' => false, |
||
1424 | ], 'both'); |
||
1425 | |||
1426 | $faker = Factory::create(); |
||
1427 | $inputData = [ |
||
1428 | 'name' => $faker->name, |
||
1429 | 'email' => $faker->safeEmail, |
||
1430 | 'password' => Hash::make($faker->password()), |
||
1431 | 'remember_token' => null, |
||
1432 | 'planets' => [1, 2], |
||
1433 | ]; |
||
1434 | |||
1435 | $entry = $this->crudPanel->create($inputData); |
||
1436 | |||
1437 | $this->assertCount(2, $entry->planets); |
||
1438 | |||
1439 | $inputData['planets'] = [2]; |
||
1440 | |||
1441 | $this->crudPanel->update($entry->id, $inputData); |
||
1442 | |||
1443 | $this->assertCount(1, $entry->fresh()->planets); |
||
1444 | |||
1445 | $planets = Planet::all(); |
||
1446 | $this->assertCount(1, $planets); |
||
1447 | } |
||
1448 | |||
1449 | public function testHasManySelectableRelationshipNonNullableForeignKeyAndDefaultInDatabase() |
||
1450 | { |
||
1451 | $this->crudPanel->setModel(User::class); |
||
1452 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both'); |
||
1453 | $this->crudPanel->addField([ |
||
1454 | 'name' => 'comets', |
||
1455 | 'force_delete' => false, |
||
1456 | 'fallback_id' => false, |
||
1457 | ], 'both'); |
||
1458 | |||
1459 | $faker = Factory::create(); |
||
1460 | $inputData = [ |
||
1461 | 'name' => $faker->name, |
||
1462 | 'email' => $faker->safeEmail, |
||
1463 | 'password' => Hash::make($faker->password()), |
||
1464 | 'remember_token' => null, |
||
1465 | 'comets' => [1, 2], |
||
1466 | ]; |
||
1467 | |||
1468 | $entry = $this->crudPanel->create($inputData); |
||
1469 | |||
1470 | $this->assertCount(2, $entry->comets); |
||
1471 | |||
1472 | $inputData['comets'] = [2]; |
||
1473 | |||
1474 | $this->crudPanel->update($entry->id, $inputData); |
||
1475 | |||
1476 | $this->assertCount(1, $entry->fresh()->comets); |
||
1477 | |||
1478 | $comets = Comet::all(); |
||
1479 | $this->assertCount(2, $comets); |
||
1480 | $this->assertEquals(0, $comets->first()->user_id); |
||
1481 | } |
||
1482 | |||
1483 | public function testHasManySelectableRelationshipNonNullable() |
||
1484 | { |
||
1485 | $this->crudPanel->setModel(User::class); |
||
1486 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both'); |
||
1487 | $this->crudPanel->addField([ |
||
1488 | 'name' => 'planetsNonNullable', |
||
1489 | 'force_delete' => false, |
||
1490 | 'fallback_id' => false, |
||
1491 | ], 'both'); |
||
1492 | |||
1493 | $faker = Factory::create(); |
||
1494 | $inputData = [ |
||
1495 | 'name' => $faker->name, |
||
1496 | 'email' => $faker->safeEmail, |
||
1497 | 'password' => Hash::make($faker->password()), |
||
1498 | 'remember_token' => null, |
||
1499 | 'planetsNonNullable' => [1, 2], |
||
1500 | ]; |
||
1501 | |||
1502 | $entry = $this->crudPanel->create($inputData); |
||
1503 | |||
1504 | $this->assertCount(2, $entry->planetsNonNullable); |
||
1505 | |||
1506 | $inputData['planetsNonNullable'] = null; |
||
1507 | |||
1508 | $this->crudPanel->update($entry->id, $inputData); |
||
1509 | |||
1510 | $this->assertCount(0, $entry->fresh()->planetsNonNullable); |
||
1511 | |||
1512 | $planets = PlanetNonNullable::all(); |
||
1513 | $this->assertCount(0, $planets); |
||
1514 | } |
||
1515 | |||
1516 | public function testCreateHasManyRelationWithDelimitedNameSubfields() |
||
1517 | { |
||
1518 | $this->crudPanel->setModel(User::class); |
||
1519 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both'); |
||
1520 | $this->crudPanel->addField([ |
||
1521 | 'name' => 'universes', |
||
1522 | 'subfields' => [ |
||
1523 | [ |
||
1524 | 'name' => 'title', |
||
1525 | ], |
||
1526 | [ |
||
1527 | 'name' => 'start_date,end_date', |
||
1528 | 'type' => 'date_range', |
||
1529 | ], |
||
1530 | ], |
||
1531 | ], 'both'); |
||
1532 | |||
1533 | $faker = Factory::create(); |
||
1534 | $inputData = [ |
||
1535 | 'name' => $faker->name, |
||
1536 | 'email' => $faker->safeEmail, |
||
1537 | 'password' => Hash::make($faker->password()), |
||
1538 | 'remember_token' => null, |
||
1539 | 'universes' => [ |
||
1540 | [ |
||
1541 | 'id' => null, |
||
1542 | 'title' => 'this is the star 1 title', |
||
1543 | 'start_date' => '2021-02-26', |
||
1544 | 'end_date' => '2091-01-26', |
||
1545 | ], |
||
1546 | [ |
||
1547 | 'title' => 'this is the star 2 title', |
||
1548 | 'end_date' => '2021-02-26', |
||
1549 | 'start_date' => '2091-01-26', |
||
1550 | ], |
||
1551 | ], |
||
1552 | ]; |
||
1553 | |||
1554 | $entry = $this->crudPanel->create($inputData); |
||
1555 | |||
1556 | $this->assertCount(2, $entry->universes); |
||
1557 | |||
1558 | $this->assertEquals($inputData['universes'][0]['start_date'], $entry->universes()->first()->start_date); |
||
1559 | $this->assertEquals($inputData['universes'][0]['end_date'], $entry->universes()->first()->end_date); |
||
1560 | $this->assertEquals($inputData['universes'][1]['end_date'], $entry->universes()->find(2)->end_date); |
||
1561 | $this->assertEquals($inputData['universes'][1]['start_date'], $entry->universes()->find(2)->start_date); |
||
1562 | } |
||
1563 | |||
1564 | public function testCreateHasOneRelationWithDelimitedNameSubfields() |
||
1565 | { |
||
1566 | $this->crudPanel->setModel(User::class); |
||
1567 | $this->crudPanel->setOperation('create'); |
||
1568 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships); |
||
1569 | $this->crudPanel->addField( |
||
1570 | [ |
||
1571 | 'name' => 'accountDetails', |
||
1572 | 'subfields' => [ |
||
1573 | [ |
||
1574 | 'name' => 'nickname', |
||
1575 | ], |
||
1576 | [ |
||
1577 | 'name' => 'start_date,end_date', |
||
1578 | ], |
||
1579 | [ |
||
1580 | 'name' => 'profile_picture', |
||
1581 | ], |
||
1582 | ], |
||
1583 | ]); |
||
1584 | |||
1585 | $faker = Factory::create(); |
||
1586 | |||
1587 | $inputData = [ |
||
1588 | 'name' => $faker->name, |
||
1589 | 'email' => $faker->safeEmail, |
||
1590 | 'password' => Hash::make($faker->password()), |
||
1591 | 'remember_token' => null, |
||
1592 | 'roles' => [1, 2], |
||
1593 | 'accountDetails' => [ |
||
1594 | [ |
||
1595 | 'nickname' => 'i_have_has_one', |
||
1596 | 'profile_picture' => 'ohh my picture 1.jpg', |
||
1597 | 'start_date' => '2021-02-26', |
||
1598 | 'end_date' => '2091-01-26', |
||
1599 | ], |
||
1600 | ], |
||
1601 | ]; |
||
1602 | |||
1603 | $entry = $this->crudPanel->create($inputData); |
||
1604 | $account_details = $entry->accountDetails()->first(); |
||
1605 | |||
1606 | $this->assertEquals($account_details->start_date, '2021-02-26'); |
||
1607 | $this->assertEquals($account_details->end_date, '2091-01-26'); |
||
1608 | } |
||
1609 | |||
1610 | public function testBelongsToManyWithDelimitedNameSubfields() |
||
1611 | { |
||
1612 | $this->crudPanel->setModel(User::class); |
||
1613 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships); |
||
1614 | $this->crudPanel->addField([ |
||
1615 | 'name' => 'superArticles', |
||
1616 | 'subfields' => [ |
||
1617 | [ |
||
1618 | 'name' => 'notes', |
||
1619 | ], |
||
1620 | [ |
||
1621 | 'name' => 'start_date,end_date', |
||
1622 | ], |
||
1623 | ], |
||
1624 | ]); |
||
1625 | |||
1626 | $faker = Factory::create(); |
||
1627 | $articleData = [ |
||
1628 | 'content' => $faker->text(), |
||
1629 | 'tags' => $faker->words(3, true), |
||
1630 | 'user_id' => 1, |
||
1631 | ]; |
||
1632 | |||
1633 | $article = Article::create($articleData); |
||
1634 | |||
1635 | $inputData = [ |
||
1636 | 'name' => $faker->name, |
||
1637 | 'email' => $faker->safeEmail, |
||
1638 | 'password' => Hash::make($faker->password()), |
||
1639 | 'remember_token' => null, |
||
1640 | 'superArticles' => [ |
||
1641 | [ |
||
1642 | 'superArticles' => $article->id, |
||
1643 | 'notes' => 'my first article note', |
||
1644 | 'start_date' => '2021-02-26', |
||
1645 | 'end_date' => '2091-01-26', |
||
1646 | ], |
||
1647 | ], |
||
1648 | ]; |
||
1649 | |||
1650 | $entry = $this->crudPanel->create($inputData); |
||
1651 | |||
1652 | $this->assertCount(1, $entry->fresh()->superArticles); |
||
1653 | $superArticle = $entry->fresh()->superArticles->first(); |
||
1654 | $this->assertEquals($superArticle->pivot->start_date, '2021-02-26'); |
||
1655 | $this->assertEquals($superArticle->pivot->end_date, '2091-01-26'); |
||
1656 | |||
1657 | $this->crudPanel->getUpdateFields($superArticle->id); |
||
1658 | } |
||
1659 | |||
1660 | public function testItCanCreateMorphToFieldsStructure() |
||
1661 | { |
||
1662 | $this->crudPanel->setModel(Star::class); |
||
1663 | $this->crudPanel->addField([ |
||
1664 | 'name' => 'starable', |
||
1665 | 'morphOptions' => [ |
||
1666 | ['Backpack\CRUD\Tests\config\Models\User', 'User'], |
||
1667 | ], |
||
1668 | ]); |
||
1669 | |||
1670 | $this->assertCount(2, $this->crudPanel->fields()['starable']['subfields']); |
||
1671 | |||
1672 | [$morphTypeField, $morphIdField] = $this->crudPanel->fields()['starable']['subfields']; |
||
1673 | |||
1674 | $this->assertTrue($morphTypeField['name'] === 'starable_type'); |
||
1675 | $this->assertTrue($morphIdField['name'] === 'starable_id'); |
||
1676 | } |
||
1677 | |||
1678 | public function testIPreventsAddingRepeateadMorphOptions() |
||
1679 | { |
||
1680 | $this->crudPanel->setModel(Star::class); |
||
1681 | $this->expectException(\Exception::class); |
||
1682 | |||
1683 | $this->crudPanel->addField([ |
||
1684 | 'name' => 'starable', |
||
1685 | 'morphOptions' => [ |
||
1686 | ['Backpack\CRUD\Tests\config\Models\User', 'User'], |
||
1687 | ['Backpack\CRUD\Tests\config\Models\User', 'User'], |
||
1688 | ], |
||
1689 | ]); |
||
1690 | } |
||
1691 | |||
1692 | public function testItThrowsErrorIfStringIsNotOnMorphMap() |
||
1693 | { |
||
1694 | $this->crudPanel->setModel(Star::class); |
||
1695 | $this->expectException(\Exception::class); |
||
1696 | |||
1697 | $this->crudPanel->addField([ |
||
1698 | 'name' => 'starable', |
||
1699 | 'morphOptions' => [ |
||
1700 | ['somethingThatDoesNotExist'], |
||
1701 | ], |
||
1702 | ]); |
||
1703 | } |
||
1704 | |||
1705 | public function testItCanAddTheOptionsFromTheMorphMap() |
||
1722 | } |
||
1723 | |||
1724 | public function testItThrowsErrorIfDuplicateMorphMapName() |
||
1725 | { |
||
1738 | ], |
||
1739 | ]); |
||
1740 | } |
||
1741 | |||
1742 | public function testItCanRegisterModelEventsInTheFields() |
||
1784 | } |
||
1785 | |||
1786 | private function getPivotInputData(array $pivotRelationData, bool $initCrud = true, bool $allowDuplicates = false) |
||
1787 | { |
||
1788 | $faker = Factory::create(); |
||
1825 | } |
||
1826 | } |
||
1827 |