Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Passed
Push — translatable-with-fallbacks ( d85361...c4504a )
by Pedro
12:55
created

testMorphToManyCreatableRelationshipWithMultiple()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 55
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 30
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 55
rs 9.44

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Backpack\CRUD\Tests\Unit\CrudPanel;
4
5
use Backpack\CRUD\Tests\config\Models\Article;
6
use Backpack\CRUD\Tests\config\Models\Bang;
7
use Backpack\CRUD\Tests\config\Models\Comet;
8
use Backpack\CRUD\Tests\config\Models\Planet;
9
use Backpack\CRUD\Tests\config\Models\PlanetNonNullable;
10
use Backpack\CRUD\Tests\config\Models\Star;
11
use Backpack\CRUD\Tests\config\Models\Universe;
12
use Backpack\CRUD\Tests\config\Models\User;
13
use Faker\Factory;
14
use Illuminate\Database\Eloquent\Relations\Relation;
15
use Illuminate\Support\Arr;
16
use Illuminate\Support\Facades\Hash;
17
18
/**
19
 * @covers Backpack\CRUD\app\Library\CrudPanel\Traits\Create
20
 * @covers Backpack\CRUD\app\Library\CrudPanel\Traits\Relationships
21
 * @covers Backpack\CRUD\app\Library\CrudPanel\Traits\FieldsProtectedMethods
22
 * @covers Backpack\CRUD\app\Library\CrudPanel\Traits\Update
23
 * @covers Backpack\CRUD\app\Library\CrudPanel\Traits\Input
24
 * @covers Backpack\CRUD\app\Library\CrudPanel\Traits\MorphRelationships
25
 */
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);
0 ignored issues
show
Bug introduced by
The property nickname does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property nickname does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
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);
0 ignored issues
show
Bug introduced by
The property nickname does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property nickname does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
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);
0 ignored issues
show
Unused Code introduced by
The assignment to $userEntry is dead and can be removed.
Loading history...
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);
0 ignored issues
show
Unused Code introduced by
The assignment to $userEntry is dead and can be removed.
Loading history...
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);
0 ignored issues
show
Unused Code introduced by
The assignment to $updateFields is dead and can be removed.
Loading history...
350
        $account_details = $entry->accountDetails()->first();
351
352
        $this->assertEquals($account_details->nickname, 'i_have_has_one');
0 ignored issues
show
Bug introduced by
The property nickname does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The property nickname does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
353
    }
354
355
    public function testGetRelationFieldsNoRelations()
356
    {
357
        $this->crudPanel->addField($this->nonRelationshipField);
358
359
        $relationFields = $this->crudPanel->getRelationFields();
360
361
        $this->assertEmpty($relationFields);
362
    }
363
364
    public function testGetRelationFieldsNoFields()
365
    {
366
        $relationFields = $this->crudPanel->getRelationFields();
367
368
        $this->assertEmpty($relationFields);
369
    }
370
371
    public function testGetRelationFieldsWithPivot()
372
    {
373
        $this->crudPanel->setModel(User::class);
374
        $this->crudPanel->setOperation('create');
375
        $this->crudPanel->addFields($this->userInputFieldsDotNotation);
376
377
        $relationFields = $this->crudPanel->getRelationFieldsWithPivot();
378
        $this->assertEquals($this->crudPanel->get('create.fields')['roles'], Arr::first($relationFields));
379
    }
380
381
    public function testGetRelationFieldsWithPivotNoRelations()
382
    {
383
        $this->crudPanel->setModel(User::class);
384
        $this->crudPanel->setOperation('create');
385
        $this->crudPanel->addFields($this->nonRelationshipField);
386
387
        $relationFields = $this->crudPanel->getRelationFieldsWithPivot();
388
389
        $this->assertEmpty($relationFields);
390
    }
391
392
    public function testMorphToManySelectableRelationship()
393
    {
394
        $this->crudPanel->setModel(User::class);
395
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both');
0 ignored issues
show
Unused Code introduced by
The call to Backpack\CRUD\app\Librar...\CrudPanel::addFields() has too many arguments starting with 'both'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

395
        $this->crudPanel->/** @scrutinizer ignore-call */ 
396
                          addFields($this->userInputFieldsNoRelationships, 'both');

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
396
        $this->crudPanel->addField(['name' => 'bills'], 'both');
0 ignored issues
show
Unused Code introduced by
The call to Backpack\CRUD\app\Librar...l\CrudPanel::addField() has too many arguments starting with 'both'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

396
        $this->crudPanel->/** @scrutinizer ignore-call */ 
397
                          addField(['name' => 'bills'], 'both');

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
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);
0 ignored issues
show
Unused Code introduced by
The assignment to $updateFields is dead and can be removed.
Loading history...
410
411
        $this->assertCount(1, $entry->bills);
412
413
        $this->assertEquals(1, $entry->bills()->first()->id);
0 ignored issues
show
Bug introduced by
The property id does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The property id does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
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());
0 ignored issues
show
Bug introduced by
The method pluck() does not exist on Countable. It seems like you code against a sub-type of Countable such as RectorPrefix202405\Nette\Utils\Html or RectorPrefix202405\Nette\Utils\ArrayList or Illuminate\Pagination\LengthAwarePaginator or Illuminate\Http\Resources\Json\ResourceCollection or Nette\Utils\Html or Illuminate\Pagination\CursorPaginator or Carbon\CarbonPeriod or Illuminate\Pagination\Paginator or Nette\Iterators\CachingIterator or Nette\Utils\ArrayList or Illuminate\Support\ViewErrorBag or RectorPrefix202405\Nette\Iterators\CachingIterator or Illuminate\Support\Enumerable or Prologue\Alerts\AlertsMessageBag or Nette\Iterators\CachingIterator or RectorPrefix202405\Nette\Iterators\CachingIterator. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

421
        $this->assertEquals([1, 2], $entry->fresh()->bills->/** @scrutinizer ignore-call */ pluck('id')->toArray());
Loading history...
422
    }
423
424
    public function testMorphToManyCreatableRelationship()
425
    {
426
        $this->crudPanel->setModel(User::class);
427
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both');
0 ignored issues
show
Unused Code introduced by
The call to Backpack\CRUD\app\Librar...\CrudPanel::addFields() has too many arguments starting with 'both'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

427
        $this->crudPanel->/** @scrutinizer ignore-call */ 
428
                          addFields($this->userInputFieldsNoRelationships, 'both');

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
428
        $this->crudPanel->addField(['name' => 'recommends', 'subfields' => [
429
            [
430
                'name' => 'text',
431
            ],
432
        ]], 'both');
0 ignored issues
show
Unused Code introduced by
The call to Backpack\CRUD\app\Librar...l\CrudPanel::addField() has too many arguments starting with 'both'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

432
        $this->crudPanel->/** @scrutinizer ignore-call */ 
433
                          addField(['name' => 'recommends', 'subfields' => [

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
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);
0 ignored issues
show
Unused Code introduced by
The assignment to $updateFields is dead and can be removed.
Loading history...
450
451
        $this->assertCount(1, $entry->recommends);
452
453
        $this->assertEquals(1, $entry->recommends()->first()->id);
0 ignored issues
show
Bug introduced by
The property id does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The property id does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
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);
0 ignored issues
show
Bug introduced by
The method first() does not exist on Countable. It seems like you code against a sub-type of Countable such as RectorPrefix202405\Nette\Utils\Html or RectorPrefix202405\Nette\Utils\ArrayList or Illuminate\Pagination\LengthAwarePaginator or Illuminate\Http\Resources\Json\ResourceCollection or Nette\Utils\Html or League\CommonMark\Util\ArrayCollection or Illuminate\Pagination\CursorPaginator or RectorPrefix202405\Illum...acts\Support\MessageBag or Illuminate\Contracts\Support\MessageBag or Carbon\CarbonPeriod or Illuminate\Pagination\Paginator or Nette\Iterators\CachingIterator or Nette\Utils\ArrayList or Illuminate\Support\ViewErrorBag or RectorPrefix202405\Nette\Iterators\CachingIterator or Illuminate\Support\Enumerable or Ramsey\Collection\CollectionInterface or Ramsey\Collection\AbstractCollection or Ds\Map or Ds\Set or Ds\Sequence or Nette\Iterators\CachingIterator or RectorPrefix202405\Nette\Iterators\CachingIterator. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

468
        $this->assertEquals('I changed the recommend and the pivot text', $entry->fresh()->recommends->/** @scrutinizer ignore-call */ first()->pivot->text);
Loading history...
469
    }
470
471
    public function testBelongsToManyWithPivotDataRelationship()
472
    {
473
        $this->crudPanel->setModel(User::class);
474
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships);
475
        $this->crudPanel->addField([
476
            'name' => 'superArticles',
477
            'subfields' => [
478
                [
479
                    'name' => 'notes',
480
                ],
481
            ],
482
        ]);
483
484
        $faker = Factory::create();
485
        $articleData = [
486
            'content' => $faker->text(),
487
            'tags' => $faker->words(3, true),
488
            'user_id' => 1,
489
        ];
490
491
        $article = Article::create($articleData);
492
493
        $inputData = [
494
            'name' => $faker->name,
495
            'email' => $faker->safeEmail,
496
            'password' => Hash::make($faker->password()),
497
            'remember_token' => null,
498
            'superArticles' => [
499
                [
500
                    'superArticles' => $article->id,
0 ignored issues
show
Bug introduced by
The property id does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
501
                    'notes' => 'my first article note',
502
                ],
503
            ],
504
        ];
505
506
        $entry = $this->crudPanel->create($inputData);
507
        $updateFields = $this->crudPanel->getUpdateFields($entry->id);
0 ignored issues
show
Unused Code introduced by
The assignment to $updateFields is dead and can be removed.
Loading history...
508
509
        $this->assertCount(1, $entry->fresh()->superArticles);
510
        $this->assertEquals('my first article note', $entry->fresh()->superArticles->first()->pivot->notes);
511
    }
512
513
    public function testCreateHasOneWithNestedRelationsRepeatableInterface()
514
    {
515
        $this->crudPanel->setModel(User::class);
516
        $this->crudPanel->setOperation('create');
517
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships);
518
        $this->crudPanel->addField(
519
            [
520
                'name' => 'accountDetails',
521
                'subfields' => [
522
                    [
523
                        'name' => 'nickname',
524
                    ],
525
                    [
526
                        'name' => 'profile_picture',
527
                    ],
528
                    [
529
                        'name' => 'article',
530
                    ],
531
                    [
532
                        'name' => 'addresses',
533
                        'subfields' => [
534
                            [
535
                                'name' => 'bang',
536
                            ],
537
                            [
538
                                'name' => 'street',
539
                            ],
540
                            [
541
                                'name' => 'number',
542
                            ],
543
                        ],
544
                    ],
545
                    [
546
                        'name' => 'bangs',
547
                    ],
548
                    [
549
                        'name' => 'bangsPivot',
550
                        'subfields' => [
551
                            [
552
                                'name' => 'pivot_field',
553
                            ],
554
                        ],
555
                    ],
556
                ],
557
            ]);
558
559
        $faker = Factory::create();
560
561
        $inputData = [
562
            'name' => $faker->name,
563
            'email' => $faker->safeEmail,
564
            'password' => Hash::make($faker->password()),
565
            'remember_token' => null,
566
            'roles' => [1, 2],
567
            'accountDetails' => [
568
                [
569
                    'nickname' => 'i_have_has_one',
570
                    'profile_picture' => 'ohh my picture 1.jpg',
571
                    'article' => 1,
572
                    'addresses' => [
573
                        [
574
                            'bang' => 1,
575
                            'street' => 'test',
576
                            'number' => 1,
577
                        ],
578
                        [
579
                            'bang' => 1,
580
                            'street' => 'test2',
581
                            'number' => 2,
582
                        ],
583
                    ],
584
                    'bangs' => [1, 2],
585
                    'bangsPivot' => [
586
                        ['bangsPivot' => 1, 'pivot_field' => 'test1'],
587
                        ['bangsPivot' => 2, 'pivot_field' => 'test2'],
588
                    ],
589
                ],
590
            ],
591
        ];
592
593
        $entry = $this->crudPanel->create($inputData);
594
        $updateFields = $this->crudPanel->getUpdateFields($entry->id);
0 ignored issues
show
Unused Code introduced by
The assignment to $updateFields is dead and can be removed.
Loading history...
595
        $account_details = $entry->accountDetails()->first();
596
597
        $this->assertEquals($account_details->article, Article::find(1));
0 ignored issues
show
Bug introduced by
The property article does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property article does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
598
        $this->assertEquals($account_details->addresses->count(), 2);
0 ignored issues
show
Bug introduced by
The method count() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

598
        $this->assertEquals($account_details->addresses->/** @scrutinizer ignore-call */ count(), 2);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The property addresses does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property addresses does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The method count() does not exist on UnitEnum. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

598
        $this->assertEquals($account_details->addresses->/** @scrutinizer ignore-call */ count(), 2);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method count() does not exist on BackedEnum. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

598
        $this->assertEquals($account_details->addresses->/** @scrutinizer ignore-call */ count(), 2);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
599
        $this->assertEquals($account_details->addresses->first()->city, 1);
0 ignored issues
show
Bug introduced by
The method first() does not exist on BackedEnum. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

599
        $this->assertEquals($account_details->addresses->/** @scrutinizer ignore-call */ first()->city, 1);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The property city does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property city does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The method first() does not exist on UnitEnum. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

599
        $this->assertEquals($account_details->addresses->/** @scrutinizer ignore-call */ first()->city, 1);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
600
        $this->assertEquals($account_details->addresses->first()->street, 'test');
0 ignored issues
show
Bug introduced by
The property street does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The property street does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
601
        $this->assertEquals($account_details->addresses->first()->number, 1);
0 ignored issues
show
Bug introduced by
The property number does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property number does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
602
        $this->assertEquals($account_details->bangs->first()->name, Bang::find(1)->name);
0 ignored issues
show
Bug introduced by
The property name does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
Bug introduced by
The property name does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The method first() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

602
        $this->assertEquals($account_details->bangs->/** @scrutinizer ignore-call */ first()->name, Bang::find(1)->name);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The property bangs does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property bangs does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The property name does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
603
        $this->assertEquals($account_details->bangsPivot->count(), 2);
0 ignored issues
show
Bug introduced by
The property bangsPivot does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The method count() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

603
        $this->assertEquals($account_details->bangsPivot->/** @scrutinizer ignore-call */ count(), 2);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The property bangsPivot does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
604
        $this->assertEquals($account_details->bangsPivot->first()->pivot->pivot_field, 'test1');
0 ignored issues
show
Bug introduced by
The property pivot does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property pivot_field does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The property pivot_field does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property pivot does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The property pivot_field does not seem to exist on Illuminate\Database\Eloquent\Relations\Relation.
Loading history...
Bug introduced by
The property pivot_field does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
605
    }
606
607
    public function testCreateBelongsToFake()
608
    {
609
        $belongsToField = [   // select_grouped
610
            'label' => 'Select_grouped',
611
            'type' => 'select_grouped', //https://github.com/Laravel-Backpack/CRUD/issues/502
612
            'name' => 'bang_relation_field',
613
            'fake' => true,
614
            'entity' => 'bang',
615
            'model' => 'Backpack\CRUD\Tests\config\Models\Bang',
616
            'attribute' => 'title',
617
            'group_by' => 'category', // the relationship to entity you want to use for grouping
618
            'group_by_attribute' => 'name', // the attribute on related model, that you want shown
619
            'group_by_relationship_back' => 'articles', // relationship from related model back to this model
620
            'tab' => 'Selects',
621
            'wrapperAttributes' => ['class' => 'form-group col-md-6'],
622
        ];
623
624
        $this->crudPanel->setModel(User::class);
625
        $this->crudPanel->setOperation('create');
626
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships);
627
        $this->crudPanel->addField($belongsToField);
628
629
        $faker = Factory::create();
630
631
        $inputData = [
632
            'name' => $faker->name,
633
            'email' => $faker->safeEmail,
634
            'password' => Hash::make($faker->password()),
635
            'remember_token' => null,
636
            'bang_relation_field' => 1,
637
        ];
638
639
        $entry = $this->crudPanel->create($inputData);
640
        $updateFields = $this->crudPanel->getUpdateFields($entry->id);
0 ignored issues
show
Unused Code introduced by
The assignment to $updateFields is dead and can be removed.
Loading history...
641
        $this->crudPanel->entry = $entry->withFakes();
642
        $this->assertEquals($entry->bang_relation_field, 1);
643
    }
644
645
    public function testCreateHasOneWithNestedRelations()
646
    {
647
        $this->crudPanel->setModel(User::class);
648
        $this->crudPanel->setOperation('create');
649
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships);
650
        $this->crudPanel->addFields([
651
            [
652
                'name' => 'accountDetails.nickname',
653
            ],
654
            [
655
                'name' => 'accountDetails.profile_picture',
656
            ],
657
            [
658
                'name' => 'accountDetails.article',
659
            ],
660
            [
661
                'name' => 'accountDetails.addresses',
662
                'subfields' => [
663
                    [
664
                        'name' => 'city',
665
                        'entity' => 'bang',
666
                    ],
667
                    [
668
                        'name' => 'street',
669
                    ],
670
                    [
671
                        'name' => 'number',
672
                    ],
673
                ],
674
            ],
675
            [
676
                'name' => 'accountDetails.bangs',
677
            ],
678
            [
679
                'name' => 'accountDetails.bangsPivot',
680
                'subfields' => [
681
                    [
682
                        'name' => 'pivot_field',
683
                    ],
684
                ],
685
            ],
686
        ]);
687
688
        $faker = Factory::create();
689
690
        $inputData = [
691
            'name' => $faker->name,
692
            'email' => $faker->safeEmail,
693
            'password' => Hash::make($faker->password()),
694
            'remember_token' => null,
695
            'roles' => [1, 2],
696
            'accountDetails' => [
697
                'nickname' => 'i_have_has_one',
698
                'profile_picture' => 'ohh my picture 1.jpg',
699
                'article' => 1,
700
                'addresses' => [
701
                    [
702
                        'city' => 1,
703
                        'street' => 'test',
704
                        'number' => 1,
705
                    ],
706
                    [
707
                        'city' => 2,
708
                        'street' => 'test2',
709
                        'number' => 2,
710
                    ],
711
                ],
712
                'bangs' => [1, 2],
713
                'bangsPivot' => [
714
                    ['bangsPivot' => 1, 'pivot_field' => 'test1'],
715
                    ['bangsPivot' => 2, 'pivot_field' => 'test2'],
716
                ],
717
            ],
718
        ];
719
720
        $entry = $this->crudPanel->create($inputData);
721
        $updateFields = $this->crudPanel->getUpdateFields($entry->id);
0 ignored issues
show
Unused Code introduced by
The assignment to $updateFields is dead and can be removed.
Loading history...
722
        $account_details = $entry->accountDetails()->first();
723
724
        $this->assertEquals($account_details->article, Article::find(1));
0 ignored issues
show
Bug introduced by
The property article does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property article does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
725
        $this->assertEquals($account_details->addresses->count(), 2);
0 ignored issues
show
Bug introduced by
The property addresses does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property addresses does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
726
        $this->assertEquals($account_details->addresses->first()->bang->id, 1);
0 ignored issues
show
Bug introduced by
The property bang does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property id does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The property id does not seem to exist on Illuminate\Database\Eloquent\Relations\Relation.
Loading history...
Bug introduced by
The property bang does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The property id does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
Bug introduced by
The property id does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
727
        $this->assertEquals($account_details->addresses->first()->street, 'test');
0 ignored issues
show
Bug introduced by
The property street does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property street does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
728
        $this->assertEquals($account_details->addresses->first()->number, 1);
0 ignored issues
show
Bug introduced by
The property number does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property number does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
729
        $this->assertEquals($account_details->bangs->first()->name, Bang::find(1)->name);
0 ignored issues
show
Bug introduced by
The property bangs does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The property bangs does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property name does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
Bug introduced by
The property name does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property name does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
730
        $this->assertEquals($account_details->bangsPivot->count(), 2);
0 ignored issues
show
Bug introduced by
The property bangsPivot does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property bangsPivot does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
731
        $this->assertEquals($account_details->bangsPivot->first()->pivot->pivot_field, 'test1');
0 ignored issues
show
Bug introduced by
The property pivot_field does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The property pivot_field does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property pivot does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The property pivot_field does not seem to exist on Illuminate\Database\Eloquent\Relations\Relation.
Loading history...
Bug introduced by
The property pivot_field does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
Bug introduced by
The property pivot does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
732
733
        // Now test the remove process
734
735
        $inputData = [
736
            'name' => $faker->name,
737
            'email' => $faker->safeEmail,
738
            'password' => Hash::make($faker->password()),
739
            'remember_token' => null,
740
            'roles' => [1, 2],
741
            'accountDetails' => [
742
                'nickname' => 'i_have_has_one',
743
                'profile_picture' => 'ohh my picture 1.jpg',
744
                'article' => 1,
745
                'addresses' => [ // HasOne is tested in other test function
746
                    [
747
                        'city' => 2,
748
                        'street' => 'test',
749
                        'number' => 1,
750
                    ],
751
                    [
752
                        'city' => 1,
753
                        'street' => 'test2',
754
                        'number' => 2,
755
                    ],
756
                ],
757
                'bangs' => [],
758
                'bangsPivot' => [],
759
            ],
760
        ];
761
762
        $entry = $this->crudPanel->update($entry->id, $inputData);
763
        $account_details = $entry->accountDetails()->first();
764
        $this->assertEquals($account_details->addresses->count(), 2);
765
        $this->assertEquals($account_details->addresses->first()->bang->id, 2);
766
        $this->assertEquals($account_details->bangs->count(), 0);
767
        $this->assertEquals($account_details->bangsPivot->count(), 0);
768
    }
769
770
    public function testMorphOneRelationship()
771
    {
772
        $this->crudPanel->setModel(User::class);
773
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both');
0 ignored issues
show
Unused Code introduced by
The call to Backpack\CRUD\app\Librar...\CrudPanel::addFields() has too many arguments starting with 'both'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

773
        $this->crudPanel->/** @scrutinizer ignore-call */ 
774
                          addFields($this->userInputFieldsNoRelationships, 'both');

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
774
        $this->crudPanel->addField([
775
            'name' => 'comment.text',
776
        ], 'both');
0 ignored issues
show
Unused Code introduced by
The call to Backpack\CRUD\app\Librar...l\CrudPanel::addField() has too many arguments starting with 'both'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

776
        $this->crudPanel->/** @scrutinizer ignore-call */ 
777
                          addField([

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
777
778
        $faker = Factory::create();
779
        $inputData = [
780
            'name' => $faker->name,
781
            'email' => $faker->safeEmail,
782
            'password' => Hash::make($faker->password()),
783
            'remember_token' => null,
784
            'comment' => [
785
                'text' => 'some test comment text',
786
            ],
787
        ];
788
789
        $entry = $this->crudPanel->create($inputData);
790
        $updateFields = $this->crudPanel->getUpdateFields($entry->id);
0 ignored issues
show
Unused Code introduced by
The assignment to $updateFields is dead and can be removed.
Loading history...
791
792
        $this->assertEquals($inputData['comment']['text'], $entry->comment->text);
0 ignored issues
show
Bug introduced by
The property text does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property text does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
Bug introduced by
The property text does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The property text does not seem to exist on Illuminate\Database\Eloquent\Relations\Relation.
Loading history...
793
794
        $inputData['comment']['text'] = 'updated comment text';
795
796
        $this->crudPanel->update($entry->id, $inputData);
797
798
        $this->assertEquals($inputData['comment']['text'], $entry->fresh()->comment->text);
799
    }
800
801
    public function testMorphManyCreatableRelationship()
802
    {
803
        $this->crudPanel->setModel(User::class);
804
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both');
0 ignored issues
show
Unused Code introduced by
The call to Backpack\CRUD\app\Librar...\CrudPanel::addFields() has too many arguments starting with 'both'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

804
        $this->crudPanel->/** @scrutinizer ignore-call */ 
805
                          addFields($this->userInputFieldsNoRelationships, 'both');

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
805
        $this->crudPanel->addField([
806
            'name' => 'stars',
807
            'subfields' => [
808
                [
809
                    'name' => 'title',
810
                ],
811
            ],
812
        ], 'both');
0 ignored issues
show
Unused Code introduced by
The call to Backpack\CRUD\app\Librar...l\CrudPanel::addField() has too many arguments starting with 'both'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

812
        $this->crudPanel->/** @scrutinizer ignore-call */ 
813
                          addField([

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
813
814
        $faker = Factory::create();
815
        $inputData = [
816
            'name' => $faker->name,
817
            'email' => $faker->safeEmail,
818
            'password' => Hash::make($faker->password()),
819
            'remember_token' => null,
820
            'stars' => [
821
                [
822
                    'id' => null,
823
                    'title' => 'this is the star 1 title',
824
                ],
825
                [
826
                    'id' => null,
827
                    'title' => 'this is the star 2 title',
828
                ],
829
            ],
830
        ];
831
832
        $entry = $this->crudPanel->create($inputData);
833
        $updateFields = $this->crudPanel->getUpdateFields($entry->id);
0 ignored issues
show
Unused Code introduced by
The assignment to $updateFields is dead and can be removed.
Loading history...
834
835
        $this->assertCount(2, $entry->stars);
836
837
        $this->assertEquals($inputData['stars'][0]['title'], $entry->stars()->first()->title);
0 ignored issues
show
Bug introduced by
The property title does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The property title does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
838
839
        $inputData['stars'] = [
840
            [
841
                'id' => 1,
842
                'title' => 'only one star with changed title',
843
            ],
844
        ];
845
846
        $this->crudPanel->update($entry->id, $inputData);
847
848
        $this->assertCount(1, $entry->fresh()->stars);
849
850
        $this->assertEquals($inputData['stars'][0]['title'], $entry->fresh()->stars->first()->title);
851
        $this->assertEquals($inputData['stars'][0]['id'], $entry->fresh()->stars->first()->id);
852
    }
853
854
    public function testHasManyCreatableRelationship()
855
    {
856
        $this->crudPanel->setModel(User::class);
857
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both');
0 ignored issues
show
Unused Code introduced by
The call to Backpack\CRUD\app\Librar...\CrudPanel::addFields() has too many arguments starting with 'both'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

857
        $this->crudPanel->/** @scrutinizer ignore-call */ 
858
                          addFields($this->userInputFieldsNoRelationships, 'both');

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
858
        $this->crudPanel->addField([
859
            'name' => 'universes',
860
            'subfields' => [
861
                [
862
                    'name' => 'title',
863
                ],
864
            ],
865
        ], 'both');
0 ignored issues
show
Unused Code introduced by
The call to Backpack\CRUD\app\Librar...l\CrudPanel::addField() has too many arguments starting with 'both'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

865
        $this->crudPanel->/** @scrutinizer ignore-call */ 
866
                          addField([

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
866
867
        $faker = Factory::create();
868
        $inputData = [
869
            'name' => $faker->name,
870
            'email' => $faker->safeEmail,
871
            'password' => Hash::make($faker->password()),
872
            'remember_token' => null,
873
            'universes' => [
874
                [
875
                    'id' => null,
876
                    'title' => 'this is the star 1 title',
877
                ],
878
                [
879
                    'title' => 'this is the star 2 title',
880
                ],
881
            ],
882
        ];
883
884
        $entry = $this->crudPanel->create($inputData);
885
        $updateFields = $this->crudPanel->getUpdateFields($entry->id);
0 ignored issues
show
Unused Code introduced by
The assignment to $updateFields is dead and can be removed.
Loading history...
886
887
        $this->assertCount(2, $entry->universes);
888
889
        $this->assertEquals($inputData['universes'][0]['title'], $entry->universes()->first()->title);
0 ignored issues
show
Bug introduced by
The property title does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property title does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
890
891
        $inputData['universes'] = [
892
            [
893
                'id' => 1,
894
                'title' => 'star 1 with changed title',
895
            ],
896
            [
897
                'id' => 2,
898
                'title' => 'star 2 with changed title',
899
            ],
900
        ];
901
902
        $this->crudPanel->update($entry->id, $inputData);
903
904
        $universes = $entry->fresh()->universes;
905
        $this->assertCount(2, $universes);
906
        $this->assertEquals([1, 2], $universes->pluck('id')->toArray());
907
908
        $inputData['universes'] = [
909
            [
910
                'id' => 1,
911
                'title' => 'only one star with changed title',
912
            ],
913
        ];
914
915
        $this->crudPanel->update($entry->id, $inputData);
916
917
        $this->assertEquals($inputData['universes'][0]['title'], $entry->fresh()->universes->first()->title);
0 ignored issues
show
Bug introduced by
The method first() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

917
        $this->assertEquals($inputData['universes'][0]['title'], $entry->fresh()->universes->/** @scrutinizer ignore-call */ first()->title);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
918
        $this->assertEquals($inputData['universes'][0]['id'], $entry->fresh()->universes->first()->id);
0 ignored issues
show
Bug introduced by
The property id does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property id does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
919
        $this->assertEquals(1, Universe::all()->count());
920
921
        $inputData['universes'] = [
922
            [
923
                'id' => null,
924
                'title' => 'new star 3',
925
            ],
926
        ];
927
928
        $this->crudPanel->update($entry->id, $inputData);
929
930
        $this->assertEquals($inputData['universes'][0]['title'], $entry->fresh()->universes->first()->title);
931
        $this->assertEquals(3, $entry->fresh()->universes->first()->id);
932
        $this->assertEquals(1, Universe::all()->count());
933
934
        $inputData['universes'] = null;
935
936
        $this->crudPanel->update($entry->id, $inputData);
937
938
        $this->assertEquals(0, count($entry->fresh()->universes));
939
        $this->assertEquals(0, Universe::all()->count());
940
    }
941
942
    public function testHasManySelectableRelationshipWithoutForceDelete()
943
    {
944
        $this->crudPanel->setModel(User::class);
945
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both');
0 ignored issues
show
Unused Code introduced by
The call to Backpack\CRUD\app\Librar...\CrudPanel::addFields() has too many arguments starting with 'both'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

945
        $this->crudPanel->/** @scrutinizer ignore-call */ 
946
                          addFields($this->userInputFieldsNoRelationships, 'both');

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
946
        $this->crudPanel->addField([
947
            'name' => 'planets',
948
            'force_delete' => false,
949
            'fallback_id' => false,
950
        ], 'both');
0 ignored issues
show
Unused Code introduced by
The call to Backpack\CRUD\app\Librar...l\CrudPanel::addField() has too many arguments starting with 'both'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

950
        $this->crudPanel->/** @scrutinizer ignore-call */ 
951
                          addField([

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
951
952
        $faker = Factory::create();
953
        $inputData = [
954
            'name' => $faker->name,
955
            'email' => $faker->safeEmail,
956
            'password' => Hash::make($faker->password()),
957
            'remember_token' => null,
958
            'planets' => [1, 2],
959
        ];
960
961
        $entry = $this->crudPanel->create($inputData);
962
        $updateFields = $this->crudPanel->getUpdateFields($entry->id);
0 ignored issues
show
Unused Code introduced by
The assignment to $updateFields is dead and can be removed.
Loading history...
963
964
        $this->assertCount(2, $entry->planets);
965
966
        $inputData['planets'] = [1];
967
968
        $this->crudPanel->update($entry->id, $inputData);
969
970
        $this->assertCount(1, $entry->fresh()->planets);
971
972
        $planets = Planet::all();
973
974
        $this->assertCount(2, $planets);
975
    }
976
977
    public function testHasManySelectableRelationshipRemoveAllRelations()
978
    {
979
        $this->crudPanel->setModel(User::class);
980
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both');
0 ignored issues
show
Unused Code introduced by
The call to Backpack\CRUD\app\Librar...\CrudPanel::addFields() has too many arguments starting with 'both'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

980
        $this->crudPanel->/** @scrutinizer ignore-call */ 
981
                          addFields($this->userInputFieldsNoRelationships, 'both');

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
981
        $this->crudPanel->addField([
982
            'name' => 'planets',
983
            'force_delete' => false,
984
            'fallback_id' => false,
985
        ], 'both');
0 ignored issues
show
Unused Code introduced by
The call to Backpack\CRUD\app\Librar...l\CrudPanel::addField() has too many arguments starting with 'both'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

985
        $this->crudPanel->/** @scrutinizer ignore-call */ 
986
                          addField([

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
986
987
        $faker = Factory::create();
988
        $inputData = [
989
            'name' => $faker->name,
990
            'email' => $faker->safeEmail,
991
            'password' => Hash::make($faker->password()),
992
            'remember_token' => null,
993
            'planets' => [1, 2],
994
        ];
995
996
        $entry = $this->crudPanel->create($inputData);
997
998
        $this->assertCount(2, $entry->planets);
999
1000
        $inputData['planets'] = [];
1001
1002
        $this->crudPanel->update($entry->id, $inputData);
1003
1004
        $this->assertCount(0, $entry->fresh()->planets);
1005
1006
        $planets = Planet::all();
1007
1008
        $this->assertCount(2, $planets);
1009
    }
1010
1011
    public function testHasManyWithRelationScoped()
1012
    {
1013
        $this->crudPanel->setModel(User::class);
1014
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both');
0 ignored issues
show
Unused Code introduced by
The call to Backpack\CRUD\app\Librar...\CrudPanel::addFields() has too many arguments starting with 'both'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1014
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1015
                          addFields($this->userInputFieldsNoRelationships, 'both');

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
1015
        $this->crudPanel->addField([
1016
            'name' => 'incomes',
1017
            'subfields' => [
1018
                [
1019
                    'name' => 'label',
1020
                    'type' => 'text',
1021
                ],
1022
                [
1023
                    'name' => 'type',
1024
                    'type' => 'hidden',
1025
                    'value' => 'income',
1026
                ],
1027
                [
1028
                    'name' => 'amount',
1029
                    'type' => 'number',
1030
                ],
1031
            ],
1032
        ], 'both');
0 ignored issues
show
Unused Code introduced by
The call to Backpack\CRUD\app\Librar...l\CrudPanel::addField() has too many arguments starting with 'both'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1032
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1033
                          addField([

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
1033
        $this->crudPanel->addField([
1034
            'name' => 'expenses',
1035
            'subfields' => [
1036
                [
1037
                    'name' => 'label',
1038
                    'type' => 'text',
1039
                ],
1040
                [
1041
                    'name' => 'type',
1042
                    'type' => 'hidden',
1043
                    'value' => 'expense',
1044
                ],
1045
                [
1046
                    'name' => 'amount',
1047
                    'type' => 'number',
1048
                ],
1049
            ],
1050
        ], 'both');
1051
1052
        $faker = Factory::create();
1053
        $inputData = [
1054
            'name' => $faker->name,
1055
            'email' => $faker->safeEmail,
1056
            'password' => Hash::make($faker->password()),
1057
            'remember_token' => null,
1058
            'incomes' => [
1059
                [
1060
                    'label' => $faker->name,
1061
                    'amount' => 33,
1062
                    'type' => 'income',
1063
                ],
1064
                [
1065
                    'label' => $faker->name,
1066
                    'amount' => 22,
1067
                    'type' => 'income',
1068
                ],
1069
            ],
1070
            'expenses' => [
1071
                [
1072
                    'label' => $faker->name,
1073
                    'amount' => 44,
1074
                    'type' => 'expense',
1075
                ],
1076
                [
1077
                    'label' => $faker->name,
1078
                    'amount' => 10,
1079
                    'type' => 'expense',
1080
                ],
1081
            ],
1082
        ];
1083
        $entry = $this->crudPanel->create($inputData);
1084
1085
        $firstExpense = $entry->expenses->first();
0 ignored issues
show
Bug introduced by
The method first() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1085
        /** @scrutinizer ignore-call */ 
1086
        $firstExpense = $entry->expenses->first();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Unused Code introduced by
The assignment to $firstExpense is dead and can be removed.
Loading history...
1086
        $firstIncome = $entry->incomes->first();
0 ignored issues
show
Bug introduced by
The method first() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1086
        /** @scrutinizer ignore-call */ 
1087
        $firstIncome = $entry->incomes->first();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Unused Code introduced by
The assignment to $firstIncome is dead and can be removed.
Loading history...
1087
        $this->assertCount(2, $entry->expenses);
1088
        $this->assertCount(2, $entry->incomes);
1089
        $this->assertEquals(44, $entry->expenses->first()->amount);
1090
        $this->assertEquals(33, $entry->incomes->first()->amount);
1091
1092
        $inputData['incomes'] = [
1093
            [
1094
                'id' => 2,
1095
                'label' => $faker->name,
1096
                'amount' => 222,
1097
                'type' => 'income',
1098
            ],
1099
        ];
1100
        $inputData['expenses'] = [
1101
            [
1102
                'id' => 3,
1103
                'label' => $faker->name,
1104
                'amount' => 44,
1105
                'type' => 'expense',
1106
            ],
1107
            [
1108
                'id' => 4,
1109
                'label' => $faker->name,
1110
                'amount' => 10,
1111
                'type' => 'expense',
1112
            ],
1113
        ];
1114
        $this->crudPanel->update($entry->id, $inputData);
1115
1116
        $freshIncomes = $entry->fresh()->incomes;
1117
        $freshExpenses = $entry->fresh()->expenses;
1118
        $this->assertCount(2, $freshExpenses);
1119
        $this->assertCount(1, $freshIncomes);
1120
        $this->assertEquals(2, $freshIncomes->first()->id);
1121
1122
        $inputData['expenses'] = [];
1123
        $this->crudPanel->update($entry->id, $inputData);
1124
1125
        $freshIncomes = $entry->fresh()->incomes;
1126
        $freshExpenses = $entry->fresh()->expenses;
1127
        $this->assertCount(0, $freshExpenses);
1128
        $this->assertCount(1, $freshIncomes);
1129
    }
1130
1131
    public function testHasManySelectableRelationshipWithFallbackId()
1132
    {
1133
        $this->crudPanel->setModel(User::class);
1134
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both');
0 ignored issues
show
Unused Code introduced by
The call to Backpack\CRUD\app\Librar...\CrudPanel::addFields() has too many arguments starting with 'both'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1134
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1135
                          addFields($this->userInputFieldsNoRelationships, 'both');

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
1135
        $this->crudPanel->addField([
1136
            'name' => 'planets',
1137
            'fallback_id' => 0,
1138
            'force_delete' => false,
1139
        ], 'both');
0 ignored issues
show
Unused Code introduced by
The call to Backpack\CRUD\app\Librar...l\CrudPanel::addField() has too many arguments starting with 'both'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1139
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1140
                          addField([

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
1140
1141
        $faker = Factory::create();
1142
        $inputData = [
1143
            'name' => $faker->name,
1144
            'email' => $faker->safeEmail,
1145
            'password' => Hash::make($faker->password()),
1146
            'remember_token' => null,
1147
            'planets' => [1, 2],
1148
        ];
1149
1150
        $entry = $this->crudPanel->create($inputData);
1151
1152
        $this->assertCount(2, $entry->planets);
1153
1154
        $inputData['planets'] = [2];
1155
1156
        $this->crudPanel->update($entry->id, $inputData);
1157
1158
        $this->assertCount(1, $entry->fresh()->planets);
1159
1160
        $planets = Planet::all();
1161
        $this->assertCount(2, $planets);
1162
        $this->assertEquals(0, $planets->first()->user_id);
1163
    }
1164
1165
    public function testHasManySelectableRelationshipWithForceDelete()
1166
    {
1167
        $this->crudPanel->setModel(User::class);
1168
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both');
0 ignored issues
show
Unused Code introduced by
The call to Backpack\CRUD\app\Librar...\CrudPanel::addFields() has too many arguments starting with 'both'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1168
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1169
                          addFields($this->userInputFieldsNoRelationships, 'both');

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
1169
        $this->crudPanel->addField([
1170
            'name' => 'planets',
1171
            'force_delete' => true,
1172
            'fallback_id' => false,
1173
        ], 'both');
0 ignored issues
show
Unused Code introduced by
The call to Backpack\CRUD\app\Librar...l\CrudPanel::addField() has too many arguments starting with 'both'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1173
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1174
                          addField([

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
1174
1175
        $faker = Factory::create();
1176
        $inputData = [
1177
            'name' => $faker->name,
1178
            'email' => $faker->safeEmail,
1179
            'password' => Hash::make($faker->password()),
1180
            'remember_token' => null,
1181
            'planets' => [1, 2],
1182
        ];
1183
1184
        $entry = $this->crudPanel->create($inputData);
1185
1186
        $this->assertCount(2, $entry->planets);
1187
1188
        $inputData['planets'] = [2];
1189
1190
        $this->crudPanel->update($entry->id, $inputData);
1191
1192
        $this->assertCount(1, $entry->fresh()->planets);
1193
1194
        $planets = Planet::all();
1195
        $this->assertCount(1, $planets);
1196
    }
1197
1198
    public function testHasManySelectableRelationshipNonNullableForeignKeyAndDefaultInDatabase()
1199
    {
1200
        $this->crudPanel->setModel(User::class);
1201
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both');
0 ignored issues
show
Unused Code introduced by
The call to Backpack\CRUD\app\Librar...\CrudPanel::addFields() has too many arguments starting with 'both'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1201
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1202
                          addFields($this->userInputFieldsNoRelationships, 'both');

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
1202
        $this->crudPanel->addField([
1203
            'name' => 'comets',
1204
            'force_delete' => false,
1205
            'fallback_id' => false,
1206
        ], 'both');
0 ignored issues
show
Unused Code introduced by
The call to Backpack\CRUD\app\Librar...l\CrudPanel::addField() has too many arguments starting with 'both'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1206
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1207
                          addField([

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
1207
1208
        $faker = Factory::create();
1209
        $inputData = [
1210
            'name' => $faker->name,
1211
            'email' => $faker->safeEmail,
1212
            'password' => Hash::make($faker->password()),
1213
            'remember_token' => null,
1214
            'comets' => [1, 2],
1215
        ];
1216
1217
        $entry = $this->crudPanel->create($inputData);
1218
1219
        $this->assertCount(2, $entry->comets);
1220
1221
        $inputData['comets'] = [2];
1222
1223
        $this->crudPanel->update($entry->id, $inputData);
1224
1225
        $this->assertCount(1, $entry->fresh()->comets);
1226
1227
        $comets = Comet::all();
1228
        $this->assertCount(2, $comets);
1229
        $this->assertEquals(0, $comets->first()->user_id);
1230
    }
1231
1232
    public function testHasManySelectableRelationshipNonNullable()
1233
    {
1234
        $this->crudPanel->setModel(User::class);
1235
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both');
0 ignored issues
show
Unused Code introduced by
The call to Backpack\CRUD\app\Librar...\CrudPanel::addFields() has too many arguments starting with 'both'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1235
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1236
                          addFields($this->userInputFieldsNoRelationships, 'both');

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
1236
        $this->crudPanel->addField([
1237
            'name' => 'planetsNonNullable',
1238
            'force_delete' => false,
1239
            'fallback_id' => false,
1240
        ], 'both');
0 ignored issues
show
Unused Code introduced by
The call to Backpack\CRUD\app\Librar...l\CrudPanel::addField() has too many arguments starting with 'both'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1240
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1241
                          addField([

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
1241
1242
        $faker = Factory::create();
1243
        $inputData = [
1244
            'name' => $faker->name,
1245
            'email' => $faker->safeEmail,
1246
            'password' => Hash::make($faker->password()),
1247
            'remember_token' => null,
1248
            'planetsNonNullable' => [1, 2],
1249
        ];
1250
1251
        $entry = $this->crudPanel->create($inputData);
1252
1253
        $this->assertCount(2, $entry->planetsNonNullable);
1254
1255
        $inputData['planetsNonNullable'] = null;
1256
1257
        $this->crudPanel->update($entry->id, $inputData);
1258
1259
        $this->assertCount(0, $entry->fresh()->planetsNonNullable);
1260
1261
        $planets = PlanetNonNullable::all();
1262
        $this->assertCount(0, $planets);
1263
    }
1264
1265
    public function testCreateHasManyRelationWithDelimitedNameSubfields()
1266
    {
1267
        $this->crudPanel->setModel(User::class);
1268
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both');
0 ignored issues
show
Unused Code introduced by
The call to Backpack\CRUD\app\Librar...\CrudPanel::addFields() has too many arguments starting with 'both'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1268
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1269
                          addFields($this->userInputFieldsNoRelationships, 'both');

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
1269
        $this->crudPanel->addField([
1270
            'name' => 'universes',
1271
            'subfields' => [
1272
                [
1273
                    'name' => 'title',
1274
                ],
1275
                [
1276
                    'name' => 'start_date,end_date',
1277
                    'type' => 'date_range',
1278
                ],
1279
            ],
1280
        ], 'both');
0 ignored issues
show
Unused Code introduced by
The call to Backpack\CRUD\app\Librar...l\CrudPanel::addField() has too many arguments starting with 'both'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1280
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1281
                          addField([

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
1281
1282
        $faker = Factory::create();
1283
        $inputData = [
1284
            'name' => $faker->name,
1285
            'email' => $faker->safeEmail,
1286
            'password' => Hash::make($faker->password()),
1287
            'remember_token' => null,
1288
            'universes' => [
1289
                [
1290
                    'id' => null,
1291
                    'title' => 'this is the star 1 title',
1292
                    'start_date' => '2021-02-26',
1293
                    'end_date' => '2091-01-26',
1294
                ],
1295
                [
1296
                    'title' => 'this is the star 2 title',
1297
                    'end_date' => '2021-02-26',
1298
                    'start_date' => '2091-01-26',
1299
                ],
1300
            ],
1301
        ];
1302
1303
        $entry = $this->crudPanel->create($inputData);
1304
1305
        $this->assertCount(2, $entry->universes);
1306
1307
        $this->assertEquals($inputData['universes'][0]['start_date'], $entry->universes()->first()->start_date);
0 ignored issues
show
Bug introduced by
The property start_date does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property start_date does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
1308
        $this->assertEquals($inputData['universes'][0]['end_date'], $entry->universes()->first()->end_date);
0 ignored issues
show
Bug introduced by
The property end_date does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property end_date does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
1309
        $this->assertEquals($inputData['universes'][1]['end_date'], $entry->universes()->find(2)->end_date);
1310
        $this->assertEquals($inputData['universes'][1]['start_date'], $entry->universes()->find(2)->start_date);
1311
    }
1312
1313
    public function testCreateHasOneRelationWithDelimitedNameSubfields()
1314
    {
1315
        $this->crudPanel->setModel(User::class);
1316
        $this->crudPanel->setOperation('create');
1317
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships);
1318
        $this->crudPanel->addField(
1319
            [
1320
                'name' => 'accountDetails',
1321
                'subfields' => [
1322
                    [
1323
                        'name' => 'nickname',
1324
                    ],
1325
                    [
1326
                        'name' => 'start_date,end_date',
1327
                    ],
1328
                    [
1329
                        'name' => 'profile_picture',
1330
                    ],
1331
                ],
1332
            ]);
1333
1334
        $faker = Factory::create();
1335
1336
        $inputData = [
1337
            'name' => $faker->name,
1338
            'email' => $faker->safeEmail,
1339
            'password' => Hash::make($faker->password()),
1340
            'remember_token' => null,
1341
            'roles' => [1, 2],
1342
            'accountDetails' => [
1343
                [
1344
                    'nickname' => 'i_have_has_one',
1345
                    'profile_picture' => 'ohh my picture 1.jpg',
1346
                    'start_date' => '2021-02-26',
1347
                    'end_date' => '2091-01-26',
1348
                ],
1349
            ],
1350
        ];
1351
1352
        $entry = $this->crudPanel->create($inputData);
1353
        $account_details = $entry->accountDetails()->first();
1354
1355
        $this->assertEquals($account_details->start_date, '2021-02-26');
0 ignored issues
show
Bug introduced by
The property start_date does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property start_date does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
1356
        $this->assertEquals($account_details->end_date, '2091-01-26');
0 ignored issues
show
Bug introduced by
The property end_date does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property end_date does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
1357
    }
1358
1359
    public function testBelongsToManyWithDelimitedNameSubfields()
1360
    {
1361
        $this->crudPanel->setModel(User::class);
1362
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships);
1363
        $this->crudPanel->addField([
1364
            'name' => 'superArticles',
1365
            'subfields' => [
1366
                [
1367
                    'name' => 'notes',
1368
                ],
1369
                [
1370
                    'name' => 'start_date,end_date',
1371
                ],
1372
            ],
1373
        ]);
1374
1375
        $faker = Factory::create();
1376
        $articleData = [
1377
            'content' => $faker->text(),
1378
            'tags' => $faker->words(3, true),
1379
            'user_id' => 1,
1380
        ];
1381
1382
        $article = Article::create($articleData);
1383
1384
        $inputData = [
1385
            'name' => $faker->name,
1386
            'email' => $faker->safeEmail,
1387
            'password' => Hash::make($faker->password()),
1388
            'remember_token' => null,
1389
            'superArticles' => [
1390
                [
1391
                    'superArticles' => $article->id,
0 ignored issues
show
Bug introduced by
The property id does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
1392
                    'notes' => 'my first article note',
1393
                    'start_date' => '2021-02-26',
1394
                    'end_date' => '2091-01-26',
1395
                ],
1396
            ],
1397
        ];
1398
1399
        $entry = $this->crudPanel->create($inputData);
1400
1401
        $this->assertCount(1, $entry->fresh()->superArticles);
1402
        $superArticle = $entry->fresh()->superArticles->first();
1403
        $this->assertEquals($superArticle->pivot->start_date, '2021-02-26');
1404
        $this->assertEquals($superArticle->pivot->end_date, '2091-01-26');
1405
1406
        $this->crudPanel->getUpdateFields($superArticle->id);
1407
    }
1408
1409
    public function testItCanCreateMorphToFieldsStructure()
1410
    {
1411
        $this->crudPanel->setModel(Star::class);
1412
        $this->crudPanel->addField([
1413
            'name' => 'starable',
1414
            'morphOptions' => [
1415
                ['Backpack\CRUD\Tests\config\Models\User', 'User'],
1416
            ],
1417
        ]);
1418
1419
        $this->assertCount(2, $this->crudPanel->fields()['starable']['subfields']);
1420
1421
        [$morphTypeField, $morphIdField] = $this->crudPanel->fields()['starable']['subfields'];
1422
1423
        $this->assertTrue($morphTypeField['name'] === 'starable_type');
1424
        $this->assertTrue($morphIdField['name'] === 'starable_id');
1425
    }
1426
1427
    public function testIPreventsAddingRepeateadMorphOptions()
1428
    {
1429
        $this->crudPanel->setModel(Star::class);
1430
        $this->expectException(\Exception::class);
1431
1432
        $this->crudPanel->addField([
1433
            'name' => 'starable',
1434
            'morphOptions' => [
1435
                ['Backpack\CRUD\Tests\config\Models\User', 'User'],
1436
                ['Backpack\CRUD\Tests\config\Models\User', 'User'],
1437
            ],
1438
        ]);
1439
    }
1440
1441
    public function testItThrowsErrorIfStringIsNotOnMorphMap()
1442
    {
1443
        $this->crudPanel->setModel(Star::class);
1444
        $this->expectException(\Exception::class);
1445
1446
        $this->crudPanel->addField([
1447
            'name' => 'starable',
1448
            'morphOptions' => [
1449
                ['somethingThatDoesNotExist'],
1450
            ],
1451
        ]);
1452
    }
1453
1454
    public function testItCanAddTheOptionsFromTheMorphMap()
1455
    {
1456
        $this->crudPanel->setModel(Star::class);
1457
1458
        Relation::morphMap([
1459
            'user' => 'Backpack\CRUD\Tests\config\Models\User',
1460
        ]);
1461
1462
        $this->crudPanel->addField([
1463
            'name' => 'starable',
1464
            'morphOptions' => [
1465
                ['user'],
1466
            ],
1467
        ]);
1468
1469
        [$morphTypeField, $morphIdField] = $this->crudPanel->fields()['starable']['subfields'];
1470
        $this->assertEquals(['user' => 'Backpack\CRUD\Tests\config\Models\User'], $morphTypeField['morphMap']);
1471
    }
1472
1473
    public function testItThrowsErrorIfDuplicateMorphMapName()
1474
    {
1475
        $this->crudPanel->setModel(Star::class);
1476
        $this->expectException(\Exception::class);
1477
1478
        Relation::morphMap([
1479
            'user' => 'Backpack\CRUD\Tests\config\Models\User',
1480
        ]);
1481
1482
        $this->crudPanel->addField([
1483
            'name' => 'starable',
1484
            'morphOptions' => [
1485
                ['user'],
1486
                ['user'],
1487
            ],
1488
        ]);
1489
    }
1490
}
1491