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