We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Total Complexity | 41 |
Total Lines | 1521 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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'); |
||
397 | |||
398 | $faker = Factory::create(); |
||
399 | $inputData = [ |
||
400 | 'name' => $faker->name, |
||
401 | 'email' => $faker->safeEmail, |
||
402 | 'password' => Hash::make($faker->password()), |
||
403 | 'remember_token' => null, |
||
404 | 'bills' => [1], |
||
405 | ]; |
||
406 | |||
407 | $entry = $this->crudPanel->create($inputData); |
||
408 | |||
409 | $updateFields = $this->crudPanel->getUpdateFields($entry->id); |
||
410 | |||
411 | $this->assertCount(1, $entry->bills); |
||
412 | |||
413 | $this->assertEquals(1, $entry->bills()->first()->id); |
||
414 | |||
415 | $inputData['bills'] = [1, 2]; |
||
416 | |||
417 | $this->crudPanel->update($entry->id, $inputData); |
||
418 | |||
419 | $this->assertCount(2, $entry->fresh()->bills); |
||
420 | |||
421 | $this->assertEquals([1, 2], $entry->fresh()->bills->pluck('id')->toArray()); |
||
422 | } |
||
423 | |||
424 | public function testMorphToManyCreatableRelationship() |
||
425 | { |
||
426 | $this->crudPanel->setModel(User::class); |
||
427 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both'); |
||
428 | $this->crudPanel->addField(['name' => 'recommends', 'subfields' => [ |
||
429 | [ |
||
430 | 'name' => 'text', |
||
431 | ], |
||
432 | ]], 'both'); |
||
433 | |||
434 | $faker = Factory::create(); |
||
435 | $inputData = [ |
||
436 | 'name' => $faker->name, |
||
437 | 'email' => $faker->safeEmail, |
||
438 | 'password' => Hash::make($faker->password()), |
||
439 | 'remember_token' => null, |
||
440 | 'recommends' => [ |
||
441 | [ |
||
442 | 'recommends' => 1, |
||
443 | 'text' => 'my pivot recommend field', |
||
444 | ], |
||
445 | ], |
||
446 | ]; |
||
447 | |||
448 | $entry = $this->crudPanel->create($inputData); |
||
449 | $updateFields = $this->crudPanel->getUpdateFields($entry->id); |
||
450 | |||
451 | $this->assertCount(1, $entry->recommends); |
||
452 | |||
453 | $this->assertEquals(1, $entry->recommends()->first()->id); |
||
454 | |||
455 | $inputData['recommends'] = [ |
||
456 | [ |
||
457 | 'recommends' => 2, |
||
458 | 'text' => 'I changed the recommend and the pivot text', |
||
459 | ], |
||
460 | ]; |
||
461 | |||
462 | $this->crudPanel->update($entry->id, $inputData); |
||
463 | |||
464 | $this->assertCount(1, $entry->fresh()->recommends); |
||
465 | |||
466 | $this->assertEquals(2, $entry->recommends()->first()->id); |
||
467 | |||
468 | $this->assertEquals('I changed the recommend and the pivot text', $entry->fresh()->recommends->first()->pivot->text); |
||
469 | } |
||
470 | |||
471 | public function testBelongsToManyWithPivotDataRelationship() |
||
511 | } |
||
512 | |||
513 | public function testBelongsToManyWithMultipleSameRelationIdAndPivotDataRelationship() |
||
514 | { |
||
515 | $this->crudPanel->setModel(User::class); |
||
516 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships); |
||
517 | $this->crudPanel->addField([ |
||
518 | 'name' => 'superArticles', |
||
519 | 'subfields' => [ |
||
520 | [ |
||
521 | 'name' => 'notes', |
||
522 | ], |
||
523 | ], |
||
524 | ]); |
||
525 | |||
526 | $faker = Factory::create(); |
||
527 | $articleData = [ |
||
528 | 'content' => $faker->text(), |
||
529 | 'tags' => $faker->words(3, true), |
||
530 | 'user_id' => 1, |
||
531 | ]; |
||
532 | |||
533 | $article = Article::create([ |
||
534 | 'content' => $faker->text(), |
||
535 | 'tags' => $faker->words(3, true), |
||
536 | 'user_id' => 1, |
||
537 | ]); |
||
538 | $article2 = Article::create([ |
||
539 | 'content' => $faker->text(), |
||
540 | 'tags' => $faker->words(3, true), |
||
541 | 'user_id' => 1, |
||
542 | ]); |
||
543 | $inputData = [ |
||
544 | 'name' => $faker->name, |
||
545 | 'email' => $faker->safeEmail, |
||
546 | 'password' => Hash::make($faker->password()), |
||
547 | 'remember_token' => null, |
||
548 | 'superArticles' => [ |
||
549 | [ |
||
550 | 'superArticles' => $article->id, |
||
551 | 'notes' => 'my first article note', |
||
552 | ], |
||
553 | [ |
||
554 | 'superArticles' => $article->id, |
||
555 | 'notes' => 'my second article note', |
||
556 | ], |
||
557 | [ |
||
558 | 'superArticles' => $article2->id, |
||
559 | 'notes' => 'my first article2 note', |
||
560 | ], |
||
561 | ], |
||
562 | ]; |
||
563 | |||
564 | $entry = $this->crudPanel->create($inputData); |
||
565 | $updateFields = $this->crudPanel->getUpdateFields($entry->id); |
||
566 | |||
567 | $this->assertCount(3, $entry->fresh()->superArticles); |
||
568 | $this->assertEquals('my first article note', $entry->fresh()->superArticles->first()->pivot->notes); |
||
569 | $this->assertEquals('my second article note', $entry->fresh()->superArticles[1]->pivot->notes); |
||
570 | $this->assertEquals('my first article2 note', $entry->fresh()->superArticles[2]->pivot->notes); |
||
571 | } |
||
572 | |||
573 | public function testCreateHasOneWithNestedRelationsRepeatableInterface() |
||
574 | { |
||
575 | $this->crudPanel->setModel(User::class); |
||
576 | $this->crudPanel->setOperation('create'); |
||
577 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships); |
||
578 | $this->crudPanel->addField( |
||
579 | [ |
||
580 | 'name' => 'accountDetails', |
||
581 | 'subfields' => [ |
||
582 | [ |
||
583 | 'name' => 'nickname', |
||
584 | ], |
||
585 | [ |
||
586 | 'name' => 'profile_picture', |
||
587 | ], |
||
588 | [ |
||
589 | 'name' => 'article', |
||
590 | ], |
||
591 | [ |
||
592 | 'name' => 'addresses', |
||
593 | 'subfields' => [ |
||
594 | [ |
||
595 | 'name' => 'bang', |
||
596 | ], |
||
597 | [ |
||
598 | 'name' => 'street', |
||
599 | ], |
||
600 | [ |
||
601 | 'name' => 'number', |
||
602 | ], |
||
603 | ], |
||
604 | ], |
||
605 | [ |
||
606 | 'name' => 'bangs', |
||
607 | ], |
||
608 | [ |
||
609 | 'name' => 'bangsPivot', |
||
610 | 'subfields' => [ |
||
611 | [ |
||
612 | 'name' => 'pivot_field', |
||
613 | ], |
||
614 | ], |
||
615 | ], |
||
616 | ], |
||
617 | ]); |
||
618 | |||
619 | $faker = Factory::create(); |
||
620 | |||
621 | $inputData = [ |
||
622 | 'name' => $faker->name, |
||
623 | 'email' => $faker->safeEmail, |
||
624 | 'password' => Hash::make($faker->password()), |
||
625 | 'remember_token' => null, |
||
626 | 'roles' => [1, 2], |
||
627 | 'accountDetails' => [ |
||
628 | [ |
||
629 | 'nickname' => 'i_have_has_one', |
||
630 | 'profile_picture' => 'ohh my picture 1.jpg', |
||
631 | 'article' => 1, |
||
632 | 'addresses' => [ |
||
633 | [ |
||
634 | 'bang' => 1, |
||
635 | 'street' => 'test', |
||
636 | 'number' => 1, |
||
637 | ], |
||
638 | [ |
||
639 | 'bang' => 1, |
||
640 | 'street' => 'test2', |
||
641 | 'number' => 2, |
||
642 | ], |
||
643 | ], |
||
644 | 'bangs' => [1, 2], |
||
645 | 'bangsPivot' => [ |
||
646 | ['bangsPivot' => 1, 'pivot_field' => 'test1'], |
||
647 | ['bangsPivot' => 2, 'pivot_field' => 'test2'], |
||
648 | ], |
||
649 | ], |
||
650 | ], |
||
651 | ]; |
||
652 | |||
653 | $entry = $this->crudPanel->create($inputData); |
||
654 | $updateFields = $this->crudPanel->getUpdateFields($entry->id); |
||
655 | $account_details = $entry->accountDetails()->first(); |
||
656 | |||
657 | $this->assertEquals($account_details->article, Article::find(1)); |
||
658 | $this->assertEquals($account_details->addresses->count(), 2); |
||
659 | $this->assertEquals($account_details->addresses->first()->city, 1); |
||
660 | $this->assertEquals($account_details->addresses->first()->street, 'test'); |
||
661 | $this->assertEquals($account_details->addresses->first()->number, 1); |
||
662 | $this->assertEquals($account_details->bangs->first()->name, Bang::find(1)->name); |
||
663 | $this->assertEquals($account_details->bangsPivot->count(), 2); |
||
664 | $this->assertEquals($account_details->bangsPivot->first()->pivot->pivot_field, 'test1'); |
||
665 | } |
||
666 | |||
667 | public function testCreateBelongsToFake() |
||
668 | { |
||
669 | $belongsToField = [ // select_grouped |
||
670 | 'label' => 'Select_grouped', |
||
671 | 'type' => 'select_grouped', //https://github.com/Laravel-Backpack/CRUD/issues/502 |
||
672 | 'name' => 'bang_relation_field', |
||
673 | 'fake' => true, |
||
674 | 'entity' => 'bang', |
||
675 | 'model' => 'Backpack\CRUD\Tests\config\Models\Bang', |
||
676 | 'attribute' => 'title', |
||
677 | 'group_by' => 'category', // the relationship to entity you want to use for grouping |
||
678 | 'group_by_attribute' => 'name', // the attribute on related model, that you want shown |
||
679 | 'group_by_relationship_back' => 'articles', // relationship from related model back to this model |
||
680 | 'tab' => 'Selects', |
||
681 | 'wrapperAttributes' => ['class' => 'form-group col-md-6'], |
||
682 | ]; |
||
683 | |||
684 | $this->crudPanel->setModel(User::class); |
||
685 | $this->crudPanel->setOperation('create'); |
||
686 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships); |
||
687 | $this->crudPanel->addField($belongsToField); |
||
688 | |||
689 | $faker = Factory::create(); |
||
690 | |||
691 | $inputData = [ |
||
692 | 'name' => $faker->name, |
||
693 | 'email' => $faker->safeEmail, |
||
694 | 'password' => Hash::make($faker->password()), |
||
695 | 'remember_token' => null, |
||
696 | 'bang_relation_field' => 1, |
||
697 | ]; |
||
698 | |||
699 | $entry = $this->crudPanel->create($inputData); |
||
700 | $updateFields = $this->crudPanel->getUpdateFields($entry->id); |
||
701 | $this->crudPanel->entry = $entry->withFakes(); |
||
702 | $this->assertEquals($entry->bang_relation_field, 1); |
||
703 | } |
||
704 | |||
705 | public function testCreateHasOneWithNestedRelations() |
||
706 | { |
||
707 | $this->crudPanel->setModel(User::class); |
||
708 | $this->crudPanel->setOperation('create'); |
||
709 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships); |
||
710 | $this->crudPanel->addFields([ |
||
711 | [ |
||
712 | 'name' => 'accountDetails.nickname', |
||
713 | ], |
||
714 | [ |
||
715 | 'name' => 'accountDetails.profile_picture', |
||
716 | ], |
||
717 | [ |
||
718 | 'name' => 'accountDetails.article', |
||
719 | ], |
||
720 | [ |
||
721 | 'name' => 'accountDetails.addresses', |
||
722 | 'subfields' => [ |
||
723 | [ |
||
724 | 'name' => 'city', |
||
725 | 'entity' => 'bang', |
||
726 | ], |
||
727 | [ |
||
728 | 'name' => 'street', |
||
729 | ], |
||
730 | [ |
||
731 | 'name' => 'number', |
||
732 | ], |
||
733 | ], |
||
734 | ], |
||
735 | [ |
||
736 | 'name' => 'accountDetails.bangs', |
||
737 | ], |
||
738 | [ |
||
739 | 'name' => 'accountDetails.bangsPivot', |
||
740 | 'subfields' => [ |
||
741 | [ |
||
742 | 'name' => 'pivot_field', |
||
743 | ], |
||
744 | ], |
||
745 | ], |
||
746 | ]); |
||
747 | |||
748 | $faker = Factory::create(); |
||
749 | |||
750 | $inputData = [ |
||
751 | 'name' => $faker->name, |
||
752 | 'email' => $faker->safeEmail, |
||
753 | 'password' => Hash::make($faker->password()), |
||
754 | 'remember_token' => null, |
||
755 | 'roles' => [1, 2], |
||
756 | 'accountDetails' => [ |
||
757 | 'nickname' => 'i_have_has_one', |
||
758 | 'profile_picture' => 'ohh my picture 1.jpg', |
||
759 | 'article' => 1, |
||
760 | 'addresses' => [ |
||
761 | [ |
||
762 | 'city' => 1, |
||
763 | 'street' => 'test', |
||
764 | 'number' => 1, |
||
765 | ], |
||
766 | [ |
||
767 | 'city' => 2, |
||
768 | 'street' => 'test2', |
||
769 | 'number' => 2, |
||
770 | ], |
||
771 | ], |
||
772 | 'bangs' => [1, 2], |
||
773 | 'bangsPivot' => [ |
||
774 | ['bangsPivot' => 1, 'pivot_field' => 'test1'], |
||
775 | ['bangsPivot' => 2, 'pivot_field' => 'test2'], |
||
776 | ], |
||
777 | ], |
||
778 | ]; |
||
779 | |||
780 | $entry = $this->crudPanel->create($inputData); |
||
781 | $updateFields = $this->crudPanel->getUpdateFields($entry->id); |
||
782 | $account_details = $entry->accountDetails()->first(); |
||
783 | |||
784 | $this->assertEquals($account_details->article, Article::find(1)); |
||
785 | $this->assertEquals($account_details->addresses->count(), 2); |
||
786 | $this->assertEquals($account_details->addresses->first()->bang->id, 1); |
||
787 | $this->assertEquals($account_details->addresses->first()->street, 'test'); |
||
788 | $this->assertEquals($account_details->addresses->first()->number, 1); |
||
789 | $this->assertEquals($account_details->bangs->first()->name, Bang::find(1)->name); |
||
790 | $this->assertEquals($account_details->bangsPivot->count(), 2); |
||
791 | $this->assertEquals($account_details->bangsPivot->first()->pivot->pivot_field, 'test1'); |
||
792 | |||
793 | // Now test the remove process |
||
794 | |||
795 | $inputData = [ |
||
796 | 'name' => $faker->name, |
||
797 | 'email' => $faker->safeEmail, |
||
798 | 'password' => Hash::make($faker->password()), |
||
799 | 'remember_token' => null, |
||
800 | 'roles' => [1, 2], |
||
801 | 'accountDetails' => [ |
||
802 | 'nickname' => 'i_have_has_one', |
||
803 | 'profile_picture' => 'ohh my picture 1.jpg', |
||
804 | 'article' => 1, |
||
805 | 'addresses' => [ // HasOne is tested in other test function |
||
806 | [ |
||
807 | 'city' => 2, |
||
808 | 'street' => 'test', |
||
809 | 'number' => 1, |
||
810 | ], |
||
811 | [ |
||
812 | 'city' => 1, |
||
813 | 'street' => 'test2', |
||
814 | 'number' => 2, |
||
815 | ], |
||
816 | ], |
||
817 | 'bangs' => [], |
||
818 | 'bangsPivot' => [], |
||
819 | ], |
||
820 | ]; |
||
821 | |||
822 | $entry = $this->crudPanel->update($entry->id, $inputData); |
||
823 | $account_details = $entry->accountDetails()->first(); |
||
824 | $this->assertEquals($account_details->addresses->count(), 2); |
||
825 | $this->assertEquals($account_details->addresses->first()->bang->id, 2); |
||
826 | $this->assertEquals($account_details->bangs->count(), 0); |
||
827 | $this->assertEquals($account_details->bangsPivot->count(), 0); |
||
828 | } |
||
829 | |||
830 | public function testMorphOneRelationship() |
||
831 | { |
||
832 | $this->crudPanel->setModel(User::class); |
||
833 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both'); |
||
834 | $this->crudPanel->addField([ |
||
835 | 'name' => 'comment.text', |
||
836 | ], 'both'); |
||
837 | |||
838 | $faker = Factory::create(); |
||
839 | $inputData = [ |
||
840 | 'name' => $faker->name, |
||
841 | 'email' => $faker->safeEmail, |
||
842 | 'password' => Hash::make($faker->password()), |
||
843 | 'remember_token' => null, |
||
844 | 'comment' => [ |
||
845 | 'text' => 'some test comment text', |
||
846 | ], |
||
847 | ]; |
||
848 | |||
849 | $entry = $this->crudPanel->create($inputData); |
||
850 | $updateFields = $this->crudPanel->getUpdateFields($entry->id); |
||
851 | |||
852 | $this->assertEquals($inputData['comment']['text'], $entry->comment->text); |
||
853 | |||
854 | $inputData['comment']['text'] = 'updated comment text'; |
||
855 | |||
856 | $this->crudPanel->update($entry->id, $inputData); |
||
857 | |||
858 | $this->assertEquals($inputData['comment']['text'], $entry->fresh()->comment->text); |
||
859 | } |
||
860 | |||
861 | public function testMorphManyCreatableRelationship() |
||
862 | { |
||
863 | $this->crudPanel->setModel(User::class); |
||
864 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both'); |
||
865 | $this->crudPanel->addField([ |
||
866 | 'name' => 'stars', |
||
867 | 'subfields' => [ |
||
868 | [ |
||
869 | 'name' => 'title', |
||
870 | ], |
||
871 | ], |
||
872 | ], 'both'); |
||
873 | |||
874 | $faker = Factory::create(); |
||
875 | $inputData = [ |
||
876 | 'name' => $faker->name, |
||
877 | 'email' => $faker->safeEmail, |
||
878 | 'password' => Hash::make($faker->password()), |
||
879 | 'remember_token' => null, |
||
880 | 'stars' => [ |
||
881 | [ |
||
882 | 'id' => null, |
||
883 | 'title' => 'this is the star 1 title', |
||
884 | ], |
||
885 | [ |
||
886 | 'id' => null, |
||
887 | 'title' => 'this is the star 2 title', |
||
888 | ], |
||
889 | ], |
||
890 | ]; |
||
891 | |||
892 | $entry = $this->crudPanel->create($inputData); |
||
893 | $updateFields = $this->crudPanel->getUpdateFields($entry->id); |
||
894 | |||
895 | $this->assertCount(2, $entry->stars); |
||
896 | |||
897 | $this->assertEquals($inputData['stars'][0]['title'], $entry->stars()->first()->title); |
||
898 | |||
899 | $inputData['stars'] = [ |
||
900 | [ |
||
901 | 'id' => 1, |
||
902 | 'title' => 'only one star with changed title', |
||
903 | ], |
||
904 | ]; |
||
905 | |||
906 | $this->crudPanel->update($entry->id, $inputData); |
||
907 | |||
908 | $this->assertCount(1, $entry->fresh()->stars); |
||
909 | |||
910 | $this->assertEquals($inputData['stars'][0]['title'], $entry->fresh()->stars->first()->title); |
||
911 | $this->assertEquals($inputData['stars'][0]['id'], $entry->fresh()->stars->first()->id); |
||
912 | } |
||
913 | |||
914 | public function testHasManyCreatableRelationship() |
||
915 | { |
||
916 | $this->crudPanel->setModel(User::class); |
||
917 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both'); |
||
918 | $this->crudPanel->addField([ |
||
919 | 'name' => 'universes', |
||
920 | 'subfields' => [ |
||
921 | [ |
||
922 | 'name' => 'title', |
||
923 | ], |
||
924 | ], |
||
925 | ], 'both'); |
||
926 | |||
927 | $faker = Factory::create(); |
||
928 | $inputData = [ |
||
929 | 'name' => $faker->name, |
||
930 | 'email' => $faker->safeEmail, |
||
931 | 'password' => Hash::make($faker->password()), |
||
932 | 'remember_token' => null, |
||
933 | 'universes' => [ |
||
934 | [ |
||
935 | 'id' => null, |
||
936 | 'title' => 'this is the star 1 title', |
||
937 | ], |
||
938 | [ |
||
939 | 'title' => 'this is the star 2 title', |
||
940 | ], |
||
941 | ], |
||
942 | ]; |
||
943 | |||
944 | $entry = $this->crudPanel->create($inputData); |
||
945 | $updateFields = $this->crudPanel->getUpdateFields($entry->id); |
||
946 | |||
947 | $this->assertCount(2, $entry->universes); |
||
948 | |||
949 | $this->assertEquals($inputData['universes'][0]['title'], $entry->universes()->first()->title); |
||
950 | |||
951 | $inputData['universes'] = [ |
||
952 | [ |
||
953 | 'id' => 1, |
||
954 | 'title' => 'star 1 with changed title', |
||
955 | ], |
||
956 | [ |
||
957 | 'id' => 2, |
||
958 | 'title' => 'star 2 with changed title', |
||
959 | ], |
||
960 | ]; |
||
961 | |||
962 | $this->crudPanel->update($entry->id, $inputData); |
||
963 | |||
964 | $universes = $entry->fresh()->universes; |
||
965 | $this->assertCount(2, $universes); |
||
966 | $this->assertEquals([1, 2], $universes->pluck('id')->toArray()); |
||
967 | |||
968 | $inputData['universes'] = [ |
||
969 | [ |
||
970 | 'id' => 1, |
||
971 | 'title' => 'only one star with changed title', |
||
972 | ], |
||
973 | ]; |
||
974 | |||
975 | $this->crudPanel->update($entry->id, $inputData); |
||
976 | |||
977 | $this->assertEquals($inputData['universes'][0]['title'], $entry->fresh()->universes->first()->title); |
||
978 | $this->assertEquals($inputData['universes'][0]['id'], $entry->fresh()->universes->first()->id); |
||
979 | $this->assertEquals(1, Universe::all()->count()); |
||
980 | |||
981 | $inputData['universes'] = [ |
||
982 | [ |
||
983 | 'id' => null, |
||
984 | 'title' => 'new star 3', |
||
985 | ], |
||
986 | ]; |
||
987 | |||
988 | $this->crudPanel->update($entry->id, $inputData); |
||
989 | |||
990 | $this->assertEquals($inputData['universes'][0]['title'], $entry->fresh()->universes->first()->title); |
||
991 | $this->assertEquals(3, $entry->fresh()->universes->first()->id); |
||
992 | $this->assertEquals(1, Universe::all()->count()); |
||
993 | |||
994 | $inputData['universes'] = null; |
||
995 | |||
996 | $this->crudPanel->update($entry->id, $inputData); |
||
997 | |||
998 | $this->assertEquals(0, count($entry->fresh()->universes)); |
||
999 | $this->assertEquals(0, Universe::all()->count()); |
||
1000 | } |
||
1001 | |||
1002 | public function testHasManySelectableRelationshipWithoutForceDelete() |
||
1003 | { |
||
1004 | $this->crudPanel->setModel(User::class); |
||
1005 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both'); |
||
1006 | $this->crudPanel->addField([ |
||
1007 | 'name' => 'planets', |
||
1008 | 'force_delete' => false, |
||
1009 | 'fallback_id' => false, |
||
1010 | ], 'both'); |
||
1011 | |||
1012 | $faker = Factory::create(); |
||
1013 | $inputData = [ |
||
1014 | 'name' => $faker->name, |
||
1015 | 'email' => $faker->safeEmail, |
||
1016 | 'password' => Hash::make($faker->password()), |
||
1017 | 'remember_token' => null, |
||
1018 | 'planets' => [1, 2], |
||
1019 | ]; |
||
1020 | |||
1021 | $entry = $this->crudPanel->create($inputData); |
||
1022 | $updateFields = $this->crudPanel->getUpdateFields($entry->id); |
||
1023 | |||
1024 | $this->assertCount(2, $entry->planets); |
||
1025 | |||
1026 | $inputData['planets'] = [1]; |
||
1027 | |||
1028 | $this->crudPanel->update($entry->id, $inputData); |
||
1029 | |||
1030 | $this->assertCount(1, $entry->fresh()->planets); |
||
1031 | |||
1032 | $planets = Planet::all(); |
||
1033 | |||
1034 | $this->assertCount(2, $planets); |
||
1035 | } |
||
1036 | |||
1037 | public function testHasManySelectableRelationshipRemoveAllRelations() |
||
1038 | { |
||
1039 | $this->crudPanel->setModel(User::class); |
||
1040 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both'); |
||
1041 | $this->crudPanel->addField([ |
||
1042 | 'name' => 'planets', |
||
1043 | 'force_delete' => false, |
||
1044 | 'fallback_id' => false, |
||
1045 | ], 'both'); |
||
1046 | |||
1047 | $faker = Factory::create(); |
||
1048 | $inputData = [ |
||
1049 | 'name' => $faker->name, |
||
1050 | 'email' => $faker->safeEmail, |
||
1051 | 'password' => Hash::make($faker->password()), |
||
1052 | 'remember_token' => null, |
||
1053 | 'planets' => [1, 2], |
||
1054 | ]; |
||
1055 | |||
1056 | $entry = $this->crudPanel->create($inputData); |
||
1057 | |||
1058 | $this->assertCount(2, $entry->planets); |
||
1059 | |||
1060 | $inputData['planets'] = []; |
||
1061 | |||
1062 | $this->crudPanel->update($entry->id, $inputData); |
||
1063 | |||
1064 | $this->assertCount(0, $entry->fresh()->planets); |
||
1065 | |||
1066 | $planets = Planet::all(); |
||
1067 | |||
1068 | $this->assertCount(2, $planets); |
||
1069 | } |
||
1070 | |||
1071 | public function testHasManyWithRelationScoped() |
||
1189 | } |
||
1190 | |||
1191 | public function testHasManySelectableRelationshipWithFallbackId() |
||
1192 | { |
||
1193 | $this->crudPanel->setModel(User::class); |
||
1194 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both'); |
||
1195 | $this->crudPanel->addField([ |
||
1196 | 'name' => 'planets', |
||
1197 | 'fallback_id' => 0, |
||
1198 | 'force_delete' => false, |
||
1199 | ], 'both'); |
||
1200 | |||
1201 | $faker = Factory::create(); |
||
1202 | $inputData = [ |
||
1203 | 'name' => $faker->name, |
||
1204 | 'email' => $faker->safeEmail, |
||
1205 | 'password' => Hash::make($faker->password()), |
||
1206 | 'remember_token' => null, |
||
1207 | 'planets' => [1, 2], |
||
1208 | ]; |
||
1209 | |||
1210 | $entry = $this->crudPanel->create($inputData); |
||
1211 | |||
1212 | $this->assertCount(2, $entry->planets); |
||
1213 | |||
1214 | $inputData['planets'] = [2]; |
||
1215 | |||
1216 | $this->crudPanel->update($entry->id, $inputData); |
||
1217 | |||
1218 | $this->assertCount(1, $entry->fresh()->planets); |
||
1219 | |||
1220 | $planets = Planet::all(); |
||
1221 | $this->assertCount(2, $planets); |
||
1222 | $this->assertEquals(0, $planets->first()->user_id); |
||
1223 | } |
||
1224 | |||
1225 | public function testHasManySelectableRelationshipWithForceDelete() |
||
1226 | { |
||
1227 | $this->crudPanel->setModel(User::class); |
||
1228 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both'); |
||
1229 | $this->crudPanel->addField([ |
||
1230 | 'name' => 'planets', |
||
1231 | 'force_delete' => true, |
||
1232 | 'fallback_id' => false, |
||
1233 | ], 'both'); |
||
1234 | |||
1235 | $faker = Factory::create(); |
||
1236 | $inputData = [ |
||
1237 | 'name' => $faker->name, |
||
1238 | 'email' => $faker->safeEmail, |
||
1239 | 'password' => Hash::make($faker->password()), |
||
1240 | 'remember_token' => null, |
||
1241 | 'planets' => [1, 2], |
||
1242 | ]; |
||
1243 | |||
1244 | $entry = $this->crudPanel->create($inputData); |
||
1245 | |||
1246 | $this->assertCount(2, $entry->planets); |
||
1247 | |||
1248 | $inputData['planets'] = [2]; |
||
1249 | |||
1250 | $this->crudPanel->update($entry->id, $inputData); |
||
1251 | |||
1252 | $this->assertCount(1, $entry->fresh()->planets); |
||
1253 | |||
1254 | $planets = Planet::all(); |
||
1255 | $this->assertCount(1, $planets); |
||
1256 | } |
||
1257 | |||
1258 | public function testHasManySelectableRelationshipNonNullableForeignKeyAndDefaultInDatabase() |
||
1259 | { |
||
1260 | $this->crudPanel->setModel(User::class); |
||
1261 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both'); |
||
1262 | $this->crudPanel->addField([ |
||
1263 | 'name' => 'comets', |
||
1264 | 'force_delete' => false, |
||
1265 | 'fallback_id' => false, |
||
1266 | ], 'both'); |
||
1267 | |||
1268 | $faker = Factory::create(); |
||
1269 | $inputData = [ |
||
1270 | 'name' => $faker->name, |
||
1271 | 'email' => $faker->safeEmail, |
||
1272 | 'password' => Hash::make($faker->password()), |
||
1273 | 'remember_token' => null, |
||
1274 | 'comets' => [1, 2], |
||
1275 | ]; |
||
1276 | |||
1277 | $entry = $this->crudPanel->create($inputData); |
||
1278 | |||
1279 | $this->assertCount(2, $entry->comets); |
||
1280 | |||
1281 | $inputData['comets'] = [2]; |
||
1282 | |||
1283 | $this->crudPanel->update($entry->id, $inputData); |
||
1284 | |||
1285 | $this->assertCount(1, $entry->fresh()->comets); |
||
1286 | |||
1287 | $comets = Comet::all(); |
||
1288 | $this->assertCount(2, $comets); |
||
1289 | $this->assertEquals(0, $comets->first()->user_id); |
||
1290 | } |
||
1291 | |||
1292 | public function testHasManySelectableRelationshipNonNullable() |
||
1293 | { |
||
1294 | $this->crudPanel->setModel(User::class); |
||
1295 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both'); |
||
1296 | $this->crudPanel->addField([ |
||
1297 | 'name' => 'planetsNonNullable', |
||
1298 | 'force_delete' => false, |
||
1299 | 'fallback_id' => false, |
||
1300 | ], 'both'); |
||
1301 | |||
1302 | $faker = Factory::create(); |
||
1303 | $inputData = [ |
||
1304 | 'name' => $faker->name, |
||
1305 | 'email' => $faker->safeEmail, |
||
1306 | 'password' => Hash::make($faker->password()), |
||
1307 | 'remember_token' => null, |
||
1308 | 'planetsNonNullable' => [1, 2], |
||
1309 | ]; |
||
1310 | |||
1311 | $entry = $this->crudPanel->create($inputData); |
||
1312 | |||
1313 | $this->assertCount(2, $entry->planetsNonNullable); |
||
1314 | |||
1315 | $inputData['planetsNonNullable'] = null; |
||
1316 | |||
1317 | $this->crudPanel->update($entry->id, $inputData); |
||
1318 | |||
1319 | $this->assertCount(0, $entry->fresh()->planetsNonNullable); |
||
1320 | |||
1321 | $planets = PlanetNonNullable::all(); |
||
1322 | $this->assertCount(0, $planets); |
||
1323 | } |
||
1324 | |||
1325 | public function testCreateHasManyRelationWithDelimitedNameSubfields() |
||
1326 | { |
||
1327 | $this->crudPanel->setModel(User::class); |
||
1328 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both'); |
||
1329 | $this->crudPanel->addField([ |
||
1330 | 'name' => 'universes', |
||
1331 | 'subfields' => [ |
||
1332 | [ |
||
1333 | 'name' => 'title', |
||
1334 | ], |
||
1335 | [ |
||
1336 | 'name' => 'start_date,end_date', |
||
1337 | 'type' => 'date_range', |
||
1338 | ], |
||
1339 | ], |
||
1340 | ], 'both'); |
||
1341 | |||
1342 | $faker = Factory::create(); |
||
1343 | $inputData = [ |
||
1344 | 'name' => $faker->name, |
||
1345 | 'email' => $faker->safeEmail, |
||
1346 | 'password' => Hash::make($faker->password()), |
||
1347 | 'remember_token' => null, |
||
1348 | 'universes' => [ |
||
1349 | [ |
||
1350 | 'id' => null, |
||
1351 | 'title' => 'this is the star 1 title', |
||
1352 | 'start_date' => '2021-02-26', |
||
1353 | 'end_date' => '2091-01-26', |
||
1354 | ], |
||
1355 | [ |
||
1356 | 'title' => 'this is the star 2 title', |
||
1357 | 'end_date' => '2021-02-26', |
||
1358 | 'start_date' => '2091-01-26', |
||
1359 | ], |
||
1360 | ], |
||
1361 | ]; |
||
1362 | |||
1363 | $entry = $this->crudPanel->create($inputData); |
||
1364 | |||
1365 | $this->assertCount(2, $entry->universes); |
||
1366 | |||
1367 | $this->assertEquals($inputData['universes'][0]['start_date'], $entry->universes()->first()->start_date); |
||
1368 | $this->assertEquals($inputData['universes'][0]['end_date'], $entry->universes()->first()->end_date); |
||
1369 | $this->assertEquals($inputData['universes'][1]['end_date'], $entry->universes()->find(2)->end_date); |
||
1370 | $this->assertEquals($inputData['universes'][1]['start_date'], $entry->universes()->find(2)->start_date); |
||
1371 | } |
||
1372 | |||
1373 | public function testCreateHasOneRelationWithDelimitedNameSubfields() |
||
1374 | { |
||
1375 | $this->crudPanel->setModel(User::class); |
||
1376 | $this->crudPanel->setOperation('create'); |
||
1377 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships); |
||
1378 | $this->crudPanel->addField( |
||
1379 | [ |
||
1380 | 'name' => 'accountDetails', |
||
1381 | 'subfields' => [ |
||
1382 | [ |
||
1383 | 'name' => 'nickname', |
||
1384 | ], |
||
1385 | [ |
||
1386 | 'name' => 'start_date,end_date', |
||
1387 | ], |
||
1388 | [ |
||
1389 | 'name' => 'profile_picture', |
||
1390 | ], |
||
1391 | ], |
||
1392 | ]); |
||
1393 | |||
1394 | $faker = Factory::create(); |
||
1395 | |||
1396 | $inputData = [ |
||
1397 | 'name' => $faker->name, |
||
1398 | 'email' => $faker->safeEmail, |
||
1399 | 'password' => Hash::make($faker->password()), |
||
1400 | 'remember_token' => null, |
||
1401 | 'roles' => [1, 2], |
||
1402 | 'accountDetails' => [ |
||
1403 | [ |
||
1404 | 'nickname' => 'i_have_has_one', |
||
1405 | 'profile_picture' => 'ohh my picture 1.jpg', |
||
1406 | 'start_date' => '2021-02-26', |
||
1407 | 'end_date' => '2091-01-26', |
||
1408 | ], |
||
1409 | ], |
||
1410 | ]; |
||
1411 | |||
1412 | $entry = $this->crudPanel->create($inputData); |
||
1413 | $account_details = $entry->accountDetails()->first(); |
||
1414 | |||
1415 | $this->assertEquals($account_details->start_date, '2021-02-26'); |
||
1416 | $this->assertEquals($account_details->end_date, '2091-01-26'); |
||
1417 | } |
||
1418 | |||
1419 | public function testBelongsToManyWithDelimitedNameSubfields() |
||
1420 | { |
||
1421 | $this->crudPanel->setModel(User::class); |
||
1422 | $this->crudPanel->addFields($this->userInputFieldsNoRelationships); |
||
1423 | $this->crudPanel->addField([ |
||
1424 | 'name' => 'superArticles', |
||
1425 | 'subfields' => [ |
||
1426 | [ |
||
1427 | 'name' => 'notes', |
||
1428 | ], |
||
1429 | [ |
||
1430 | 'name' => 'start_date,end_date', |
||
1431 | ], |
||
1432 | ], |
||
1433 | ]); |
||
1434 | |||
1435 | $faker = Factory::create(); |
||
1436 | $articleData = [ |
||
1437 | 'content' => $faker->text(), |
||
1438 | 'tags' => $faker->words(3, true), |
||
1439 | 'user_id' => 1, |
||
1440 | ]; |
||
1441 | |||
1442 | $article = Article::create($articleData); |
||
1443 | |||
1444 | $inputData = [ |
||
1445 | 'name' => $faker->name, |
||
1446 | 'email' => $faker->safeEmail, |
||
1447 | 'password' => Hash::make($faker->password()), |
||
1448 | 'remember_token' => null, |
||
1449 | 'superArticles' => [ |
||
1450 | [ |
||
1451 | 'superArticles' => $article->id, |
||
1452 | 'notes' => 'my first article note', |
||
1453 | 'start_date' => '2021-02-26', |
||
1454 | 'end_date' => '2091-01-26', |
||
1455 | ], |
||
1456 | ], |
||
1457 | ]; |
||
1458 | |||
1459 | $entry = $this->crudPanel->create($inputData); |
||
1460 | |||
1461 | $this->assertCount(1, $entry->fresh()->superArticles); |
||
1462 | $superArticle = $entry->fresh()->superArticles->first(); |
||
1463 | $this->assertEquals($superArticle->pivot->start_date, '2021-02-26'); |
||
1464 | $this->assertEquals($superArticle->pivot->end_date, '2091-01-26'); |
||
1465 | |||
1466 | $this->crudPanel->getUpdateFields($superArticle->id); |
||
1467 | } |
||
1468 | |||
1469 | public function testItCanCreateMorphToFieldsStructure() |
||
1470 | { |
||
1471 | $this->crudPanel->setModel(Star::class); |
||
1472 | $this->crudPanel->addField([ |
||
1473 | 'name' => 'starable', |
||
1474 | 'morphOptions' => [ |
||
1475 | ['Backpack\CRUD\Tests\config\Models\User', 'User'], |
||
1476 | ], |
||
1477 | ]); |
||
1478 | |||
1479 | $this->assertCount(2, $this->crudPanel->fields()['starable']['subfields']); |
||
1480 | |||
1481 | [$morphTypeField, $morphIdField] = $this->crudPanel->fields()['starable']['subfields']; |
||
1482 | |||
1483 | $this->assertTrue($morphTypeField['name'] === 'starable_type'); |
||
1484 | $this->assertTrue($morphIdField['name'] === 'starable_id'); |
||
1485 | } |
||
1486 | |||
1487 | public function testIPreventsAddingRepeateadMorphOptions() |
||
1488 | { |
||
1489 | $this->crudPanel->setModel(Star::class); |
||
1490 | $this->expectException(\Exception::class); |
||
1491 | |||
1492 | $this->crudPanel->addField([ |
||
1493 | 'name' => 'starable', |
||
1494 | 'morphOptions' => [ |
||
1495 | ['Backpack\CRUD\Tests\config\Models\User', 'User'], |
||
1496 | ['Backpack\CRUD\Tests\config\Models\User', 'User'], |
||
1497 | ], |
||
1498 | ]); |
||
1499 | } |
||
1500 | |||
1501 | public function testItThrowsErrorIfStringIsNotOnMorphMap() |
||
1502 | { |
||
1503 | $this->crudPanel->setModel(Star::class); |
||
1504 | $this->expectException(\Exception::class); |
||
1505 | |||
1506 | $this->crudPanel->addField([ |
||
1507 | 'name' => 'starable', |
||
1508 | 'morphOptions' => [ |
||
1509 | ['somethingThatDoesNotExist'], |
||
1510 | ], |
||
1511 | ]); |
||
1512 | } |
||
1513 | |||
1514 | public function testItCanAddTheOptionsFromTheMorphMap() |
||
1531 | } |
||
1532 | |||
1533 | public function testItThrowsErrorIfDuplicateMorphMapName() |
||
1534 | { |
||
1547 | ], |
||
1548 | ]); |
||
1549 | } |
||
1550 | } |
||
1551 |