We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Total Complexity | 46 |
Total Lines | 1715 |
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() |
||
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'); |
||
422 | } |
||
423 | |||
424 | public function testMorphToManyCreatableRelationship() |
||
469 | } |
||
470 | |||
471 | public function testMorphToManyCreatableRelationshipWithMultiple() |
||
472 | { |
||
473 | $inputData = $this->getPivotInputData(['recommendsDuplicate' => [ |
||
474 | [ |
||
475 | 'recommendsDuplicate' => 1, |
||
476 | 'text' => 'my pivot recommend field 1', |
||
477 | ], |
||
478 | [ |
||
479 | 'recommendsDuplicate' => 2, |
||
480 | 'text' => 'my pivot recommend field 2', |
||
481 | ], |
||
482 | [ |
||
483 | 'recommendsDuplicate' => 1, |
||
484 | 'text' => 'my pivot recommend field 1x1', |
||
485 | ], |
||
486 | ] |
||
487 | ], true, true); |
||
488 | |||
489 | $entry = $this->crudPanel->create($inputData); |
||
490 | |||
491 | $entry = $entry->fresh(); |
||
492 | |||
493 | $this->assertCount(3, $entry->recommendsDuplicate); |
||
494 | |||
495 | $this->assertEquals(1, $entry->recommendsDuplicate[0]->id); |
||
496 | $this->assertEquals(1, $entry->recommendsDuplicate[2]->id); |
||
497 | |||
498 | $inputData['recommendsDuplicate'] = [ |
||
499 | [ |
||
500 | 'recommendsDuplicate' => 1, |
||
501 | 'text' => 'I changed the recommend and the pivot text', |
||
502 | 'id' => 1, |
||
503 | ], |
||
504 | [ |
||
505 | 'recommendsDuplicate' => 2, |
||
506 | 'text' => 'I changed the recommend and the pivot text 2', |
||
507 | 'id' => 2 |
||
508 | ], |
||
509 | [ |
||
510 | 'recommendsDuplicate' => 3, |
||
511 | 'text' => 'new recommend and the pivot text 3', |
||
512 | 'id' => null, |
||
513 | ], |
||
514 | ]; |
||
515 | |||
516 | $this->crudPanel->update($entry->id, $inputData); |
||
517 | |||
518 | $entry = $entry->fresh(); |
||
519 | |||
520 | $this->assertCount(3, $entry->recommendsDuplicate); |
||
521 | $this->assertDatabaseCount('recommendables', 3); |
||
522 | |||
523 | $this->assertEquals('I changed the recommend and the pivot text', $entry->recommendsDuplicate[0]->pivot->text); |
||
524 | $this->assertEquals('I changed the recommend and the pivot text 2', $entry->recommendsDuplicate[1]->pivot->text); |
||
525 | $this->assertEquals('new recommend and the pivot text 3', $entry->recommendsDuplicate[2]->pivot->text); |
||
526 | |||
527 | } |
||
528 | |||
529 | public function testBelongsToManyWithPivotDataRelationship() |
||
530 | { |
||
531 | $this->crudPanel->setModel(User::class); |
||
532 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships); |
||
533 | $this->crudPanel->addField([ |
||
534 | 'name' => 'superArticles', |
||
535 | 'subfields' => [ |
||
536 | [ |
||
537 | 'name' => 'notes', |
||
538 | ], |
||
539 | ], |
||
540 | ]); |
||
541 | |||
542 | $faker = Factory::create(); |
||
543 | $articleData = [ |
||
544 | 'content' => $faker->text(), |
||
545 | 'tags' => $faker->words(3, true), |
||
546 | 'user_id' => 1, |
||
547 | ]; |
||
548 | |||
549 | $article = Article::create($articleData); |
||
550 | |||
551 | $inputData = [ |
||
552 | 'name' => $faker->name, |
||
553 | 'email' => $faker->safeEmail, |
||
554 | 'password' => Hash::make($faker->password()), |
||
555 | 'remember_token' => null, |
||
556 | 'superArticles' => [ |
||
557 | [ |
||
558 | 'superArticles' => $article->id, |
||
559 | 'notes' => 'my first article note', |
||
560 | ], |
||
561 | ], |
||
562 | ]; |
||
563 | |||
564 | $entry = $this->crudPanel->create($inputData); |
||
565 | |||
566 | $this->assertCount(1, $entry->fresh()->superArticles); |
||
567 | $this->assertEquals('my first article note', $entry->fresh()->superArticles->first()->pivot->notes); |
||
568 | } |
||
569 | |||
570 | public function testBelongsToManyWithMultipleSameRelationIdAndPivotDataRelationship() |
||
571 | { |
||
572 | $inputData = $this->getPivotInputData(['superArticlesDuplicates' => [ |
||
573 | [ |
||
574 | 'superArticlesDuplicates' => 1, |
||
575 | 'notes' => 'my first article note', |
||
576 | 'id' => null, |
||
577 | ], |
||
578 | [ |
||
579 | 'superArticlesDuplicates' => 1, |
||
580 | 'notes' => 'my second article note', |
||
581 | 'id' => null, |
||
582 | ], |
||
583 | [ |
||
584 | 'superArticlesDuplicates' => 2, |
||
585 | 'notes' => 'my first article2 note', |
||
586 | 'id' => null, |
||
587 | ], |
||
588 | ], |
||
589 | ], true, true); |
||
590 | |||
591 | $entry = $this->crudPanel->create($inputData); |
||
592 | $relationField = $this->crudPanel->getUpdateFields($entry->id)['superArticlesDuplicates']; |
||
593 | |||
594 | $this->assertCount(3, $relationField['value']); |
||
595 | |||
596 | $entry = $entry->fresh(); |
||
597 | |||
598 | $this->assertCount(3, $entry->superArticlesDuplicates); |
||
599 | $this->assertEquals('my first article note', $entry->superArticles->first()->pivot->notes); |
||
600 | $this->assertEquals('my second article note', $entry->superArticles[1]->pivot->notes); |
||
601 | $this->assertEquals('my first article2 note', $entry->superArticles[2]->pivot->notes); |
||
602 | |||
603 | $inputData = $this->getPivotInputData(['superArticlesDuplicates' => [ |
||
604 | [ |
||
605 | 'superArticlesDuplicates' => 1, |
||
606 | 'notes' => 'my first article note updated', |
||
607 | 'id' => 1, |
||
608 | ], |
||
609 | [ |
||
610 | 'superArticlesDuplicates' => 1, |
||
611 | 'notes' => 'my second article note updated', |
||
612 | 'id' => 2, |
||
613 | ], |
||
614 | [ |
||
615 | 'superArticlesDuplicates' => 2, |
||
616 | 'notes' => 'my first article2 note updated', |
||
617 | 'id' => 3, |
||
618 | ], |
||
619 | ], |
||
620 | ], false, true); |
||
621 | |||
622 | $entry = $this->crudPanel->update($entry->id, $inputData); |
||
623 | $relationField = $this->crudPanel->getUpdateFields($entry->id)['superArticlesDuplicates']; |
||
624 | $this->assertCount(3, $relationField['value']); |
||
625 | |||
626 | $entry = $entry->fresh(); |
||
627 | |||
628 | $this->assertCount(3, $entry->superArticlesDuplicates); |
||
629 | $this->assertEquals('my first article note updated', $entry->superArticles[0]->pivot->notes); |
||
630 | $this->assertEquals('my second article note updated', $entry->superArticles[1]->pivot->notes); |
||
631 | $this->assertEquals('my first article2 note updated', $entry->superArticles[2]->pivot->notes); |
||
632 | } |
||
633 | |||
634 | public function testBelongsToManyAlwaysSaveSinglePivotWhenMultipleNotAllowed() |
||
635 | { |
||
636 | $inputData = $this->getPivotInputData(['superArticlesDuplicates' => [ |
||
637 | [ |
||
638 | 'superArticlesDuplicates' => 1, |
||
639 | 'notes' => 'my first article note', |
||
640 | 'id' => null, |
||
641 | ], |
||
642 | [ |
||
643 | 'superArticlesDuplicates' => 1, |
||
644 | 'notes' => 'my second article note', |
||
645 | 'id' => null, |
||
646 | ], |
||
647 | [ |
||
648 | 'superArticlesDuplicates' => 2, |
||
649 | 'notes' => 'my first article2 note', |
||
650 | 'id' => null, |
||
651 | ], |
||
652 | ], |
||
653 | ]); |
||
654 | |||
655 | $entry = $this->crudPanel->create($inputData); |
||
656 | $relationField = $this->crudPanel->getUpdateFields($entry->id)['superArticlesDuplicates']; |
||
657 | |||
658 | $this->assertCount(2, $relationField['value']); |
||
659 | |||
660 | $entry = $entry->fresh(); |
||
661 | |||
662 | $this->assertCount(2, $entry->superArticlesDuplicates); |
||
663 | $this->assertEquals('my second article note', $entry->superArticles[0]->pivot->notes); |
||
664 | $this->assertEquals('my first article2 note', $entry->superArticles[1]->pivot->notes); |
||
665 | } |
||
666 | |||
667 | public function testBelongsToManyDeletesPivotData() |
||
668 | { |
||
669 | $inputData = $this->getPivotInputData(['superArticlesDuplicates' => [ |
||
670 | [ |
||
671 | 'superArticlesDuplicates' => 1, |
||
672 | 'notes' => 'my first article note', |
||
673 | 'id' => null, |
||
674 | ], |
||
675 | [ |
||
676 | 'superArticlesDuplicates' => 1, |
||
677 | 'notes' => 'my second article note', |
||
678 | 'id' => null, |
||
679 | ], |
||
680 | [ |
||
681 | 'superArticlesDuplicates' => 2, |
||
682 | 'notes' => 'my first article2 note', |
||
683 | 'id' => null, |
||
684 | ], |
||
685 | ], |
||
686 | ], true, true); |
||
687 | |||
688 | $entry = $this->crudPanel->create($inputData); |
||
689 | $relationField = $this->crudPanel->getUpdateFields($entry->id)['superArticlesDuplicates']; |
||
690 | |||
691 | $this->assertCount(3, $relationField['value']); |
||
692 | |||
693 | $inputData = $this->getPivotInputData(['superArticlesDuplicates' => [ |
||
694 | [ |
||
695 | 'superArticlesDuplicates' => 1, |
||
696 | 'notes' => 'new first article note', |
||
697 | 'id' => null, |
||
698 | ], |
||
699 | [ |
||
700 | 'superArticlesDuplicates' => 1, |
||
701 | 'notes' => 'my second article note updated', |
||
702 | 'id' => 2, |
||
703 | ], |
||
704 | [ |
||
705 | 'superArticlesDuplicates' => 3, |
||
706 | 'notes' => 'my first article2 note updated', |
||
707 | 'id' => 3, |
||
708 | ], |
||
709 | ], |
||
710 | ], false, true); |
||
711 | |||
712 | $entry = $this->crudPanel->update($entry->id, $inputData); |
||
713 | $relationField = $this->crudPanel->getUpdateFields($entry->id)['superArticlesDuplicates']; |
||
714 | $this->assertCount(3, $relationField['value']); |
||
715 | |||
716 | $entry = $entry->fresh(); |
||
717 | |||
718 | $this->assertCount(3, $entry->superArticlesDuplicates); |
||
719 | $this->assertEquals('new first article note', $entry->superArticles[2]->pivot->notes); |
||
720 | $this->assertEquals('my second article note updated', $entry->superArticles[0]->pivot->notes); |
||
721 | $this->assertEquals('my first article2 note updated', $entry->superArticles[1]->pivot->notes); |
||
722 | } |
||
723 | |||
724 | public function testCreateHasOneWithNestedRelationsRepeatableInterface() |
||
725 | { |
||
726 | $this->crudPanel->setModel(User::class); |
||
727 | $this->crudPanel->setOperation('create'); |
||
728 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships); |
||
729 | $this->crudPanel->addField( |
||
730 | [ |
||
731 | 'name' => 'accountDetails', |
||
732 | 'subfields' => [ |
||
733 | [ |
||
734 | 'name' => 'nickname', |
||
735 | ], |
||
736 | [ |
||
737 | 'name' => 'profile_picture', |
||
738 | ], |
||
739 | [ |
||
740 | 'name' => 'article', |
||
741 | ], |
||
742 | [ |
||
743 | 'name' => 'addresses', |
||
744 | 'subfields' => [ |
||
745 | [ |
||
746 | 'name' => 'bang', |
||
747 | ], |
||
748 | [ |
||
749 | 'name' => 'street', |
||
750 | ], |
||
751 | [ |
||
752 | 'name' => 'number', |
||
753 | ], |
||
754 | ], |
||
755 | ], |
||
756 | [ |
||
757 | 'name' => 'bangs', |
||
758 | ], |
||
759 | [ |
||
760 | 'name' => 'bangsPivot', |
||
761 | 'subfields' => [ |
||
762 | [ |
||
763 | 'name' => 'pivot_field', |
||
764 | ], |
||
765 | ], |
||
766 | ], |
||
767 | ], |
||
768 | ]); |
||
769 | |||
770 | $faker = Factory::create(); |
||
771 | |||
772 | $inputData = [ |
||
773 | 'name' => $faker->name, |
||
774 | 'email' => $faker->safeEmail, |
||
775 | 'password' => Hash::make($faker->password()), |
||
776 | 'remember_token' => null, |
||
777 | 'roles' => [1, 2], |
||
778 | 'accountDetails' => [ |
||
779 | [ |
||
780 | 'nickname' => 'i_have_has_one', |
||
781 | 'profile_picture' => 'ohh my picture 1.jpg', |
||
782 | 'article' => 1, |
||
783 | 'addresses' => [ |
||
784 | [ |
||
785 | 'bang' => 1, |
||
786 | 'street' => 'test', |
||
787 | 'number' => 1, |
||
788 | ], |
||
789 | [ |
||
790 | 'bang' => 1, |
||
791 | 'street' => 'test2', |
||
792 | 'number' => 2, |
||
793 | ], |
||
794 | ], |
||
795 | 'bangs' => [1, 2], |
||
796 | 'bangsPivot' => [ |
||
797 | ['bangsPivot' => 1, 'pivot_field' => 'test1'], |
||
798 | ['bangsPivot' => 2, 'pivot_field' => 'test2'], |
||
799 | ], |
||
800 | ], |
||
801 | ], |
||
802 | ]; |
||
803 | |||
804 | $entry = $this->crudPanel->create($inputData); |
||
805 | $updateFields = $this->crudPanel->getUpdateFields($entry->id); |
||
806 | $account_details = $entry->accountDetails()->first(); |
||
807 | |||
808 | $this->assertEquals($account_details->article, Article::find(1)); |
||
809 | $this->assertEquals($account_details->addresses->count(), 2); |
||
810 | $this->assertEquals($account_details->addresses->first()->city, 1); |
||
811 | $this->assertEquals($account_details->addresses->first()->street, 'test'); |
||
812 | $this->assertEquals($account_details->addresses->first()->number, 1); |
||
813 | $this->assertEquals($account_details->bangs->first()->name, Bang::find(1)->name); |
||
814 | $this->assertEquals($account_details->bangsPivot->count(), 2); |
||
815 | $this->assertEquals($account_details->bangsPivot->first()->pivot->pivot_field, 'test1'); |
||
816 | } |
||
817 | |||
818 | public function testCreateBelongsToFake() |
||
819 | { |
||
820 | $belongsToField = [ // select_grouped |
||
821 | 'label' => 'Select_grouped', |
||
822 | 'type' => 'select_grouped', //https://github.com/Laravel-Backpack/CRUD/issues/502 |
||
823 | 'name' => 'bang_relation_field', |
||
824 | 'fake' => true, |
||
825 | 'entity' => 'bang', |
||
826 | 'model' => 'Backpack\CRUD\Tests\config\Models\Bang', |
||
827 | 'attribute' => 'title', |
||
828 | 'group_by' => 'category', // the relationship to entity you want to use for grouping |
||
829 | 'group_by_attribute' => 'name', // the attribute on related model, that you want shown |
||
830 | 'group_by_relationship_back' => 'articles', // relationship from related model back to this model |
||
831 | 'tab' => 'Selects', |
||
832 | 'wrapperAttributes' => ['class' => 'form-group col-md-6'], |
||
833 | ]; |
||
834 | |||
835 | $this->crudPanel->setModel(User::class); |
||
836 | $this->crudPanel->setOperation('create'); |
||
837 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships); |
||
838 | $this->crudPanel->addField($belongsToField); |
||
839 | |||
840 | $faker = Factory::create(); |
||
841 | |||
842 | $inputData = [ |
||
843 | 'name' => $faker->name, |
||
844 | 'email' => $faker->safeEmail, |
||
845 | 'password' => Hash::make($faker->password()), |
||
846 | 'remember_token' => null, |
||
847 | 'bang_relation_field' => 1, |
||
848 | ]; |
||
849 | |||
850 | $entry = $this->crudPanel->create($inputData); |
||
851 | $updateFields = $this->crudPanel->getUpdateFields($entry->id); |
||
852 | $this->crudPanel->entry = $entry->withFakes(); |
||
853 | $this->assertEquals($entry->bang_relation_field, 1); |
||
854 | } |
||
855 | |||
856 | public function testCreateHasOneWithNestedRelations() |
||
857 | { |
||
858 | $this->crudPanel->setModel(User::class); |
||
859 | $this->crudPanel->setOperation('create'); |
||
860 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships); |
||
861 | $this->crudPanel->addFields([ |
||
862 | [ |
||
863 | 'name' => 'accountDetails.nickname', |
||
864 | ], |
||
865 | [ |
||
866 | 'name' => 'accountDetails.profile_picture', |
||
867 | ], |
||
868 | [ |
||
869 | 'name' => 'accountDetails.article', |
||
870 | ], |
||
871 | [ |
||
872 | 'name' => 'accountDetails.addresses', |
||
873 | 'subfields' => [ |
||
874 | [ |
||
875 | 'name' => 'city', |
||
876 | 'entity' => 'bang', |
||
877 | ], |
||
878 | [ |
||
879 | 'name' => 'street', |
||
880 | ], |
||
881 | [ |
||
882 | 'name' => 'number', |
||
883 | ], |
||
884 | ], |
||
885 | ], |
||
886 | [ |
||
887 | 'name' => 'accountDetails.bangs', |
||
888 | ], |
||
889 | [ |
||
890 | 'name' => 'accountDetails.bangsPivot', |
||
891 | 'subfields' => [ |
||
892 | [ |
||
893 | 'name' => 'pivot_field', |
||
894 | ], |
||
895 | ], |
||
896 | ], |
||
897 | ]); |
||
898 | |||
899 | $faker = Factory::create(); |
||
900 | |||
901 | $inputData = [ |
||
902 | 'name' => $faker->name, |
||
903 | 'email' => $faker->safeEmail, |
||
904 | 'password' => Hash::make($faker->password()), |
||
905 | 'remember_token' => null, |
||
906 | 'roles' => [1, 2], |
||
907 | 'accountDetails' => [ |
||
908 | 'nickname' => 'i_have_has_one', |
||
909 | 'profile_picture' => 'ohh my picture 1.jpg', |
||
910 | 'article' => 1, |
||
911 | 'addresses' => [ |
||
912 | [ |
||
913 | 'city' => 1, |
||
914 | 'street' => 'test', |
||
915 | 'number' => 1, |
||
916 | ], |
||
917 | [ |
||
918 | 'city' => 2, |
||
919 | 'street' => 'test2', |
||
920 | 'number' => 2, |
||
921 | ], |
||
922 | ], |
||
923 | 'bangs' => [1, 2], |
||
924 | 'bangsPivot' => [ |
||
925 | ['bangsPivot' => 1, 'pivot_field' => 'test1'], |
||
926 | ['bangsPivot' => 2, 'pivot_field' => 'test2'], |
||
927 | ], |
||
928 | ], |
||
929 | ]; |
||
930 | |||
931 | $entry = $this->crudPanel->create($inputData); |
||
932 | $updateFields = $this->crudPanel->getUpdateFields($entry->id); |
||
933 | $account_details = $entry->accountDetails()->first(); |
||
934 | |||
935 | $this->assertEquals($account_details->article, Article::find(1)); |
||
936 | $this->assertEquals($account_details->addresses->count(), 2); |
||
937 | $this->assertEquals($account_details->addresses->first()->bang->id, 1); |
||
938 | $this->assertEquals($account_details->addresses->first()->street, 'test'); |
||
939 | $this->assertEquals($account_details->addresses->first()->number, 1); |
||
940 | $this->assertEquals($account_details->bangs->first()->name, Bang::find(1)->name); |
||
941 | $this->assertEquals($account_details->bangsPivot->count(), 2); |
||
942 | $this->assertEquals($account_details->bangsPivot->first()->pivot->pivot_field, 'test1'); |
||
943 | |||
944 | // Now test the remove process |
||
945 | |||
946 | $inputData = [ |
||
947 | 'name' => $faker->name, |
||
948 | 'email' => $faker->safeEmail, |
||
949 | 'password' => Hash::make($faker->password()), |
||
950 | 'remember_token' => null, |
||
951 | 'roles' => [1, 2], |
||
952 | 'accountDetails' => [ |
||
953 | 'nickname' => 'i_have_has_one', |
||
954 | 'profile_picture' => 'ohh my picture 1.jpg', |
||
955 | 'article' => 1, |
||
956 | 'addresses' => [ // HasOne is tested in other test function |
||
957 | [ |
||
958 | 'city' => 2, |
||
959 | 'street' => 'test', |
||
960 | 'number' => 1, |
||
961 | ], |
||
962 | [ |
||
963 | 'city' => 1, |
||
964 | 'street' => 'test2', |
||
965 | 'number' => 2, |
||
966 | ], |
||
967 | ], |
||
968 | 'bangs' => [], |
||
969 | 'bangsPivot' => [], |
||
970 | ], |
||
971 | ]; |
||
972 | |||
973 | $entry = $this->crudPanel->update($entry->id, $inputData); |
||
974 | $account_details = $entry->accountDetails()->first(); |
||
975 | $this->assertEquals($account_details->addresses->count(), 2); |
||
976 | $this->assertEquals($account_details->addresses->first()->bang->id, 2); |
||
977 | $this->assertEquals($account_details->bangs->count(), 0); |
||
978 | $this->assertEquals($account_details->bangsPivot->count(), 0); |
||
979 | } |
||
980 | |||
981 | public function testMorphOneRelationship() |
||
982 | { |
||
983 | $this->crudPanel->setModel(User::class); |
||
984 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both'); |
||
985 | $this->crudPanel->addField([ |
||
986 | 'name' => 'comment.text', |
||
987 | ], 'both'); |
||
988 | |||
989 | $faker = Factory::create(); |
||
990 | $inputData = [ |
||
991 | 'name' => $faker->name, |
||
992 | 'email' => $faker->safeEmail, |
||
993 | 'password' => Hash::make($faker->password()), |
||
994 | 'remember_token' => null, |
||
995 | 'comment' => [ |
||
996 | 'text' => 'some test comment text', |
||
997 | ], |
||
998 | ]; |
||
999 | |||
1000 | $entry = $this->crudPanel->create($inputData); |
||
1001 | $updateFields = $this->crudPanel->getUpdateFields($entry->id); |
||
1002 | |||
1003 | $this->assertEquals($inputData['comment']['text'], $entry->comment->text); |
||
1004 | |||
1005 | $inputData['comment']['text'] = 'updated comment text'; |
||
1006 | |||
1007 | $this->crudPanel->update($entry->id, $inputData); |
||
1008 | |||
1009 | $this->assertEquals($inputData['comment']['text'], $entry->fresh()->comment->text); |
||
1010 | } |
||
1011 | |||
1012 | public function testMorphManyCreatableRelationship() |
||
1013 | { |
||
1014 | $this->crudPanel->setModel(User::class); |
||
1015 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both'); |
||
1016 | $this->crudPanel->addField([ |
||
1017 | 'name' => 'stars', |
||
1018 | 'subfields' => [ |
||
1019 | [ |
||
1020 | 'name' => 'title', |
||
1021 | ], |
||
1022 | ], |
||
1023 | ], 'both'); |
||
1024 | |||
1025 | $faker = Factory::create(); |
||
1026 | $inputData = [ |
||
1027 | 'name' => $faker->name, |
||
1028 | 'email' => $faker->safeEmail, |
||
1029 | 'password' => Hash::make($faker->password()), |
||
1030 | 'remember_token' => null, |
||
1031 | 'stars' => [ |
||
1032 | [ |
||
1033 | 'id' => null, |
||
1034 | 'title' => 'this is the star 1 title', |
||
1035 | ], |
||
1036 | [ |
||
1037 | 'id' => null, |
||
1038 | 'title' => 'this is the star 2 title', |
||
1039 | ], |
||
1040 | ], |
||
1041 | ]; |
||
1042 | |||
1043 | $entry = $this->crudPanel->create($inputData); |
||
1044 | $updateFields = $this->crudPanel->getUpdateFields($entry->id); |
||
1045 | |||
1046 | $this->assertCount(2, $entry->stars); |
||
1047 | |||
1048 | $this->assertEquals($inputData['stars'][0]['title'], $entry->stars()->first()->title); |
||
1049 | |||
1050 | $inputData['stars'] = [ |
||
1051 | [ |
||
1052 | 'id' => 1, |
||
1053 | 'title' => 'only one star with changed title', |
||
1054 | ], |
||
1055 | ]; |
||
1056 | |||
1057 | $this->crudPanel->update($entry->id, $inputData); |
||
1058 | |||
1059 | $this->assertCount(1, $entry->fresh()->stars); |
||
1060 | |||
1061 | $this->assertEquals($inputData['stars'][0]['title'], $entry->fresh()->stars->first()->title); |
||
1062 | $this->assertEquals($inputData['stars'][0]['id'], $entry->fresh()->stars->first()->id); |
||
1063 | } |
||
1064 | |||
1065 | public function testHasManyCreatableRelationship() |
||
1066 | { |
||
1067 | $this->crudPanel->setModel(User::class); |
||
1068 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both'); |
||
1069 | $this->crudPanel->addField([ |
||
1070 | 'name' => 'universes', |
||
1071 | 'subfields' => [ |
||
1072 | [ |
||
1073 | 'name' => 'title', |
||
1074 | ], |
||
1075 | ], |
||
1076 | ], 'both'); |
||
1077 | |||
1078 | $faker = Factory::create(); |
||
1079 | $inputData = [ |
||
1080 | 'name' => $faker->name, |
||
1081 | 'email' => $faker->safeEmail, |
||
1082 | 'password' => Hash::make($faker->password()), |
||
1083 | 'remember_token' => null, |
||
1084 | 'universes' => [ |
||
1085 | [ |
||
1086 | 'id' => null, |
||
1087 | 'title' => 'this is the star 1 title', |
||
1088 | ], |
||
1089 | [ |
||
1090 | 'title' => 'this is the star 2 title', |
||
1091 | ], |
||
1092 | ], |
||
1093 | ]; |
||
1094 | |||
1095 | $entry = $this->crudPanel->create($inputData); |
||
1096 | $updateFields = $this->crudPanel->getUpdateFields($entry->id); |
||
1097 | |||
1098 | $this->assertCount(2, $entry->universes); |
||
1099 | |||
1100 | $this->assertEquals($inputData['universes'][0]['title'], $entry->universes()->first()->title); |
||
1101 | |||
1102 | $inputData['universes'] = [ |
||
1103 | [ |
||
1104 | 'id' => 1, |
||
1105 | 'title' => 'star 1 with changed title', |
||
1106 | ], |
||
1107 | [ |
||
1108 | 'id' => 2, |
||
1109 | 'title' => 'star 2 with changed title', |
||
1110 | ], |
||
1111 | ]; |
||
1112 | |||
1113 | $this->crudPanel->update($entry->id, $inputData); |
||
1114 | |||
1115 | $universes = $entry->fresh()->universes; |
||
1116 | $this->assertCount(2, $universes); |
||
1117 | $this->assertEquals([1, 2], $universes->pluck('id')->toArray()); |
||
1118 | |||
1119 | $inputData['universes'] = [ |
||
1120 | [ |
||
1121 | 'id' => 1, |
||
1122 | 'title' => 'only one star with changed title', |
||
1123 | ], |
||
1124 | ]; |
||
1125 | |||
1126 | $this->crudPanel->update($entry->id, $inputData); |
||
1127 | |||
1128 | $this->assertEquals($inputData['universes'][0]['title'], $entry->fresh()->universes->first()->title); |
||
1129 | $this->assertEquals($inputData['universes'][0]['id'], $entry->fresh()->universes->first()->id); |
||
1130 | $this->assertEquals(1, Universe::all()->count()); |
||
1131 | |||
1132 | $inputData['universes'] = [ |
||
1133 | [ |
||
1134 | 'id' => null, |
||
1135 | 'title' => 'new star 3', |
||
1136 | ], |
||
1137 | ]; |
||
1138 | |||
1139 | $this->crudPanel->update($entry->id, $inputData); |
||
1140 | |||
1141 | $this->assertEquals($inputData['universes'][0]['title'], $entry->fresh()->universes->first()->title); |
||
1142 | $this->assertEquals(3, $entry->fresh()->universes->first()->id); |
||
1143 | $this->assertEquals(1, Universe::all()->count()); |
||
1144 | |||
1145 | $inputData['universes'] = null; |
||
1146 | |||
1147 | $this->crudPanel->update($entry->id, $inputData); |
||
1148 | |||
1149 | $this->assertEquals(0, count($entry->fresh()->universes)); |
||
1150 | $this->assertEquals(0, Universe::all()->count()); |
||
1151 | } |
||
1152 | |||
1153 | public function testHasManySelectableRelationshipWithoutForceDelete() |
||
1154 | { |
||
1155 | $this->crudPanel->setModel(User::class); |
||
1156 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both'); |
||
1157 | $this->crudPanel->addField([ |
||
1158 | 'name' => 'planets', |
||
1159 | 'force_delete' => false, |
||
1160 | 'fallback_id' => false, |
||
1161 | ], 'both'); |
||
1162 | |||
1163 | $faker = Factory::create(); |
||
1164 | $inputData = [ |
||
1165 | 'name' => $faker->name, |
||
1166 | 'email' => $faker->safeEmail, |
||
1167 | 'password' => Hash::make($faker->password()), |
||
1168 | 'remember_token' => null, |
||
1169 | 'planets' => [1, 2], |
||
1170 | ]; |
||
1171 | |||
1172 | $entry = $this->crudPanel->create($inputData); |
||
1173 | $updateFields = $this->crudPanel->getUpdateFields($entry->id); |
||
1174 | |||
1175 | $this->assertCount(2, $entry->planets); |
||
1176 | |||
1177 | $inputData['planets'] = [1]; |
||
1178 | |||
1179 | $this->crudPanel->update($entry->id, $inputData); |
||
1180 | |||
1181 | $this->assertCount(1, $entry->fresh()->planets); |
||
1182 | |||
1183 | $planets = Planet::all(); |
||
1184 | |||
1185 | $this->assertCount(2, $planets); |
||
1186 | } |
||
1187 | |||
1188 | public function testHasManySelectableRelationshipRemoveAllRelations() |
||
1189 | { |
||
1190 | $this->crudPanel->setModel(User::class); |
||
1191 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both'); |
||
1192 | $this->crudPanel->addField([ |
||
1193 | 'name' => 'planets', |
||
1194 | 'force_delete' => false, |
||
1195 | 'fallback_id' => false, |
||
1196 | ], 'both'); |
||
1197 | |||
1198 | $faker = Factory::create(); |
||
1199 | $inputData = [ |
||
1200 | 'name' => $faker->name, |
||
1201 | 'email' => $faker->safeEmail, |
||
1202 | 'password' => Hash::make($faker->password()), |
||
1203 | 'remember_token' => null, |
||
1204 | 'planets' => [1, 2], |
||
1205 | ]; |
||
1206 | |||
1207 | $entry = $this->crudPanel->create($inputData); |
||
1208 | |||
1209 | $this->assertCount(2, $entry->planets); |
||
1210 | |||
1211 | $inputData['planets'] = []; |
||
1212 | |||
1213 | $this->crudPanel->update($entry->id, $inputData); |
||
1214 | |||
1215 | $this->assertCount(0, $entry->fresh()->planets); |
||
1216 | |||
1217 | $planets = Planet::all(); |
||
1218 | |||
1219 | $this->assertCount(2, $planets); |
||
1220 | } |
||
1221 | |||
1222 | public function testHasManyWithRelationScoped() |
||
1340 | } |
||
1341 | |||
1342 | public function testHasManySelectableRelationshipWithFallbackId() |
||
1343 | { |
||
1344 | $this->crudPanel->setModel(User::class); |
||
1345 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both'); |
||
1346 | $this->crudPanel->addField([ |
||
1347 | 'name' => 'planets', |
||
1348 | 'fallback_id' => 0, |
||
1349 | 'force_delete' => false, |
||
1350 | ], 'both'); |
||
1351 | |||
1352 | $faker = Factory::create(); |
||
1353 | $inputData = [ |
||
1354 | 'name' => $faker->name, |
||
1355 | 'email' => $faker->safeEmail, |
||
1356 | 'password' => Hash::make($faker->password()), |
||
1357 | 'remember_token' => null, |
||
1358 | 'planets' => [1, 2], |
||
1359 | ]; |
||
1360 | |||
1361 | $entry = $this->crudPanel->create($inputData); |
||
1362 | |||
1363 | $this->assertCount(2, $entry->planets); |
||
1364 | |||
1365 | $inputData['planets'] = [2]; |
||
1366 | |||
1367 | $this->crudPanel->update($entry->id, $inputData); |
||
1368 | |||
1369 | $this->assertCount(1, $entry->fresh()->planets); |
||
1370 | |||
1371 | $planets = Planet::all(); |
||
1372 | $this->assertCount(2, $planets); |
||
1373 | $this->assertEquals(0, $planets->first()->user_id); |
||
1374 | } |
||
1375 | |||
1376 | public function testHasManySelectableRelationshipWithForceDelete() |
||
1377 | { |
||
1378 | $this->crudPanel->setModel(User::class); |
||
1379 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both'); |
||
1380 | $this->crudPanel->addField([ |
||
1381 | 'name' => 'planets', |
||
1382 | 'force_delete' => true, |
||
1383 | 'fallback_id' => false, |
||
1384 | ], 'both'); |
||
1385 | |||
1386 | $faker = Factory::create(); |
||
1387 | $inputData = [ |
||
1388 | 'name' => $faker->name, |
||
1389 | 'email' => $faker->safeEmail, |
||
1390 | 'password' => Hash::make($faker->password()), |
||
1391 | 'remember_token' => null, |
||
1392 | 'planets' => [1, 2], |
||
1393 | ]; |
||
1394 | |||
1395 | $entry = $this->crudPanel->create($inputData); |
||
1396 | |||
1397 | $this->assertCount(2, $entry->planets); |
||
1398 | |||
1399 | $inputData['planets'] = [2]; |
||
1400 | |||
1401 | $this->crudPanel->update($entry->id, $inputData); |
||
1402 | |||
1403 | $this->assertCount(1, $entry->fresh()->planets); |
||
1404 | |||
1405 | $planets = Planet::all(); |
||
1406 | $this->assertCount(1, $planets); |
||
1407 | } |
||
1408 | |||
1409 | public function testHasManySelectableRelationshipNonNullableForeignKeyAndDefaultInDatabase() |
||
1410 | { |
||
1411 | $this->crudPanel->setModel(User::class); |
||
1412 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both'); |
||
1413 | $this->crudPanel->addField([ |
||
1414 | 'name' => 'comets', |
||
1415 | 'force_delete' => false, |
||
1416 | 'fallback_id' => false, |
||
1417 | ], 'both'); |
||
1418 | |||
1419 | $faker = Factory::create(); |
||
1420 | $inputData = [ |
||
1421 | 'name' => $faker->name, |
||
1422 | 'email' => $faker->safeEmail, |
||
1423 | 'password' => Hash::make($faker->password()), |
||
1424 | 'remember_token' => null, |
||
1425 | 'comets' => [1, 2], |
||
1426 | ]; |
||
1427 | |||
1428 | $entry = $this->crudPanel->create($inputData); |
||
1429 | |||
1430 | $this->assertCount(2, $entry->comets); |
||
1431 | |||
1432 | $inputData['comets'] = [2]; |
||
1433 | |||
1434 | $this->crudPanel->update($entry->id, $inputData); |
||
1435 | |||
1436 | $this->assertCount(1, $entry->fresh()->comets); |
||
1437 | |||
1438 | $comets = Comet::all(); |
||
1439 | $this->assertCount(2, $comets); |
||
1440 | $this->assertEquals(0, $comets->first()->user_id); |
||
1441 | } |
||
1442 | |||
1443 | public function testHasManySelectableRelationshipNonNullable() |
||
1444 | { |
||
1445 | $this->crudPanel->setModel(User::class); |
||
1446 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both'); |
||
1447 | $this->crudPanel->addField([ |
||
1448 | 'name' => 'planetsNonNullable', |
||
1449 | 'force_delete' => false, |
||
1450 | 'fallback_id' => false, |
||
1451 | ], 'both'); |
||
1452 | |||
1453 | $faker = Factory::create(); |
||
1454 | $inputData = [ |
||
1455 | 'name' => $faker->name, |
||
1456 | 'email' => $faker->safeEmail, |
||
1457 | 'password' => Hash::make($faker->password()), |
||
1458 | 'remember_token' => null, |
||
1459 | 'planetsNonNullable' => [1, 2], |
||
1460 | ]; |
||
1461 | |||
1462 | $entry = $this->crudPanel->create($inputData); |
||
1463 | |||
1464 | $this->assertCount(2, $entry->planetsNonNullable); |
||
1465 | |||
1466 | $inputData['planetsNonNullable'] = null; |
||
1467 | |||
1468 | $this->crudPanel->update($entry->id, $inputData); |
||
1469 | |||
1470 | $this->assertCount(0, $entry->fresh()->planetsNonNullable); |
||
1471 | |||
1472 | $planets = PlanetNonNullable::all(); |
||
1473 | $this->assertCount(0, $planets); |
||
1474 | } |
||
1475 | |||
1476 | public function testCreateHasManyRelationWithDelimitedNameSubfields() |
||
1477 | { |
||
1478 | $this->crudPanel->setModel(User::class); |
||
1479 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both'); |
||
1480 | $this->crudPanel->addField([ |
||
1481 | 'name' => 'universes', |
||
1482 | 'subfields' => [ |
||
1483 | [ |
||
1484 | 'name' => 'title', |
||
1485 | ], |
||
1486 | [ |
||
1487 | 'name' => 'start_date,end_date', |
||
1488 | 'type' => 'date_range', |
||
1489 | ], |
||
1490 | ], |
||
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 | 'universes' => [ |
||
1500 | [ |
||
1501 | 'id' => null, |
||
1502 | 'title' => 'this is the star 1 title', |
||
1503 | 'start_date' => '2021-02-26', |
||
1504 | 'end_date' => '2091-01-26', |
||
1505 | ], |
||
1506 | [ |
||
1507 | 'title' => 'this is the star 2 title', |
||
1508 | 'end_date' => '2021-02-26', |
||
1509 | 'start_date' => '2091-01-26', |
||
1510 | ], |
||
1511 | ], |
||
1512 | ]; |
||
1513 | |||
1514 | $entry = $this->crudPanel->create($inputData); |
||
1515 | |||
1516 | $this->assertCount(2, $entry->universes); |
||
1517 | |||
1518 | $this->assertEquals($inputData['universes'][0]['start_date'], $entry->universes()->first()->start_date); |
||
1519 | $this->assertEquals($inputData['universes'][0]['end_date'], $entry->universes()->first()->end_date); |
||
1520 | $this->assertEquals($inputData['universes'][1]['end_date'], $entry->universes()->find(2)->end_date); |
||
1521 | $this->assertEquals($inputData['universes'][1]['start_date'], $entry->universes()->find(2)->start_date); |
||
1522 | } |
||
1523 | |||
1524 | public function testCreateHasOneRelationWithDelimitedNameSubfields() |
||
1525 | { |
||
1526 | $this->crudPanel->setModel(User::class); |
||
1527 | $this->crudPanel->setOperation('create'); |
||
1528 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships); |
||
1529 | $this->crudPanel->addField( |
||
1530 | [ |
||
1531 | 'name' => 'accountDetails', |
||
1532 | 'subfields' => [ |
||
1533 | [ |
||
1534 | 'name' => 'nickname', |
||
1535 | ], |
||
1536 | [ |
||
1537 | 'name' => 'start_date,end_date', |
||
1538 | ], |
||
1539 | [ |
||
1540 | 'name' => 'profile_picture', |
||
1541 | ], |
||
1542 | ], |
||
1543 | ]); |
||
1544 | |||
1545 | $faker = Factory::create(); |
||
1546 | |||
1547 | $inputData = [ |
||
1548 | 'name' => $faker->name, |
||
1549 | 'email' => $faker->safeEmail, |
||
1550 | 'password' => Hash::make($faker->password()), |
||
1551 | 'remember_token' => null, |
||
1552 | 'roles' => [1, 2], |
||
1553 | 'accountDetails' => [ |
||
1554 | [ |
||
1555 | 'nickname' => 'i_have_has_one', |
||
1556 | 'profile_picture' => 'ohh my picture 1.jpg', |
||
1557 | 'start_date' => '2021-02-26', |
||
1558 | 'end_date' => '2091-01-26', |
||
1559 | ], |
||
1560 | ], |
||
1561 | ]; |
||
1562 | |||
1563 | $entry = $this->crudPanel->create($inputData); |
||
1564 | $account_details = $entry->accountDetails()->first(); |
||
1565 | |||
1566 | $this->assertEquals($account_details->start_date, '2021-02-26'); |
||
1567 | $this->assertEquals($account_details->end_date, '2091-01-26'); |
||
1568 | } |
||
1569 | |||
1570 | public function testBelongsToManyWithDelimitedNameSubfields() |
||
1571 | { |
||
1572 | $this->crudPanel->setModel(User::class); |
||
1573 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships); |
||
1574 | $this->crudPanel->addField([ |
||
1575 | 'name' => 'superArticles', |
||
1576 | 'subfields' => [ |
||
1577 | [ |
||
1578 | 'name' => 'notes', |
||
1579 | ], |
||
1580 | [ |
||
1581 | 'name' => 'start_date,end_date', |
||
1582 | ], |
||
1583 | ], |
||
1584 | ]); |
||
1585 | |||
1586 | $faker = Factory::create(); |
||
1587 | $articleData = [ |
||
1588 | 'content' => $faker->text(), |
||
1589 | 'tags' => $faker->words(3, true), |
||
1590 | 'user_id' => 1, |
||
1591 | ]; |
||
1592 | |||
1593 | $article = Article::create($articleData); |
||
1594 | |||
1595 | $inputData = [ |
||
1596 | 'name' => $faker->name, |
||
1597 | 'email' => $faker->safeEmail, |
||
1598 | 'password' => Hash::make($faker->password()), |
||
1599 | 'remember_token' => null, |
||
1600 | 'superArticles' => [ |
||
1601 | [ |
||
1602 | 'superArticles' => $article->id, |
||
1603 | 'notes' => 'my first article note', |
||
1604 | 'start_date' => '2021-02-26', |
||
1605 | 'end_date' => '2091-01-26', |
||
1606 | ], |
||
1607 | ], |
||
1608 | ]; |
||
1609 | |||
1610 | $entry = $this->crudPanel->create($inputData); |
||
1611 | |||
1612 | $this->assertCount(1, $entry->fresh()->superArticles); |
||
1613 | $superArticle = $entry->fresh()->superArticles->first(); |
||
1614 | $this->assertEquals($superArticle->pivot->start_date, '2021-02-26'); |
||
1615 | $this->assertEquals($superArticle->pivot->end_date, '2091-01-26'); |
||
1616 | |||
1617 | $this->crudPanel->getUpdateFields($superArticle->id); |
||
1618 | } |
||
1619 | |||
1620 | public function testItCanCreateMorphToFieldsStructure() |
||
1621 | { |
||
1622 | $this->crudPanel->setModel(Star::class); |
||
1623 | $this->crudPanel->addField([ |
||
1624 | 'name' => 'starable', |
||
1625 | 'morphOptions' => [ |
||
1626 | ['Backpack\CRUD\Tests\config\Models\User', 'User'], |
||
1627 | ], |
||
1628 | ]); |
||
1629 | |||
1630 | $this->assertCount(2, $this->crudPanel->fields()['starable']['subfields']); |
||
1631 | |||
1632 | [$morphTypeField, $morphIdField] = $this->crudPanel->fields()['starable']['subfields']; |
||
1633 | |||
1634 | $this->assertTrue($morphTypeField['name'] === 'starable_type'); |
||
1635 | $this->assertTrue($morphIdField['name'] === 'starable_id'); |
||
1636 | } |
||
1637 | |||
1638 | public function testIPreventsAddingRepeateadMorphOptions() |
||
1639 | { |
||
1640 | $this->crudPanel->setModel(Star::class); |
||
1641 | $this->expectException(\Exception::class); |
||
1642 | |||
1643 | $this->crudPanel->addField([ |
||
1644 | 'name' => 'starable', |
||
1645 | 'morphOptions' => [ |
||
1646 | ['Backpack\CRUD\Tests\config\Models\User', 'User'], |
||
1647 | ['Backpack\CRUD\Tests\config\Models\User', 'User'], |
||
1648 | ], |
||
1649 | ]); |
||
1650 | } |
||
1651 | |||
1652 | public function testItThrowsErrorIfStringIsNotOnMorphMap() |
||
1653 | { |
||
1654 | $this->crudPanel->setModel(Star::class); |
||
1655 | $this->expectException(\Exception::class); |
||
1656 | |||
1657 | $this->crudPanel->addField([ |
||
1658 | 'name' => 'starable', |
||
1659 | 'morphOptions' => [ |
||
1660 | ['somethingThatDoesNotExist'], |
||
1661 | ], |
||
1662 | ]); |
||
1663 | } |
||
1664 | |||
1665 | public function testItCanAddTheOptionsFromTheMorphMap() |
||
1682 | } |
||
1683 | |||
1684 | public function testItThrowsErrorIfDuplicateMorphMapName() |
||
1685 | { |
||
1698 | ], |
||
1699 | ]); |
||
1700 | } |
||
1701 | |||
1702 | private function getPivotInputData(array $pivotRelationData, bool $initCrud = true, bool $allowDuplicates = false) |
||
1703 | { |
||
1704 | $faker = Factory::create(); |
||
1705 | |||
1706 | if ($initCrud) { |
||
1707 | $this->crudPanel->setModel(User::class); |
||
1708 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships); |
||
1709 | $this->crudPanel->addField([ |
||
1710 | 'name' => array_key_first($pivotRelationData), |
||
1741 | } |
||
1742 | } |
||
1743 |