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 — v6-livewire-widget ( bb8951...53543e )
by Pedro
14:58
created

testCreateHasOneRelationWithArrayedNameSubfields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 44
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 25
nc 1
nop 0
dl 0
loc 44
rs 9.52
c 0
b 0
f 0
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
17
/**
18
 * @covers Backpack\CRUD\app\Library\CrudPanel\Traits\Create
19
 * @covers Backpack\CRUD\app\Library\CrudPanel\Traits\Relationships
20
 * @covers Backpack\CRUD\app\Library\CrudPanel\Traits\FieldsProtectedMethods
21
 * @covers Backpack\CRUD\app\Library\CrudPanel\Traits\Update
22
 * @covers Backpack\CRUD\app\Library\CrudPanel\Traits\Input
23
 * @covers Backpack\CRUD\app\Library\CrudPanel\Traits\MorphRelationships
24
 */
25
class CrudPanelCreateTest extends \Backpack\CRUD\Tests\config\CrudPanel\BaseDBCrudPanel
26
{
27
    private $nonRelationshipField = [
28
        'name'  => 'field1',
29
        'label' => 'Field1',
30
    ];
31
32
    private $userInputFieldsNoRelationships = [
33
        [
34
            'name' => 'id',
35
            'type' => 'hidden',
36
        ], [
37
            'name' => 'name',
38
        ], [
39
            'name' => 'email',
40
            'type' => 'email',
41
        ], [
42
            'name' => 'password',
43
            'type' => 'password',
44
        ],
45
    ];
46
47
    private $articleInputFieldsOneToMany = [
48
        [
49
            'name' => 'id',
50
            'type' => 'hidden',
51
        ], [
52
            'name' => 'content',
53
        ], [
54
            'name' => 'tags',
55
        ], [
56
            'label'     => 'Author',
57
            'type'      => 'select',
58
            'name'      => 'user_id',
59
            'entity'    => 'user',
60
            'attribute' => 'name',
61
        ],
62
    ];
63
64
    private $userInputFieldsManyToMany = [
65
        [
66
            'name' => 'id',
67
            'type' => 'hidden',
68
        ], [
69
            'name' => 'name',
70
        ], [
71
            'name' => 'email',
72
            'type' => 'email',
73
        ], [
74
            'name' => 'password',
75
            'type' => 'password',
76
        ], [
77
            'label'     => 'Roles',
78
            'type'      => 'select_multiple',
79
            'name'      => 'roles',
80
            'entity'    => 'roles',
81
            'attribute' => 'name',
82
            'pivot'     => true,
83
        ],
84
    ];
85
86
    private $userInputFieldsDotNotation = [
87
        [
88
            'name' => 'id',
89
            'type' => 'hidden',
90
        ], [
91
            'name' => 'name',
92
        ], [
93
            'name' => 'email',
94
            'type' => 'email',
95
        ], [
96
            'name' => 'password',
97
            'type' => 'password',
98
        ], [
99
            'label'     => 'Roles',
100
            'type'      => 'relationship',
101
            'name'      => 'roles',
102
            'entity'    => 'roles',
103
            'attribute' => 'name',
104
        ], [
105
            'label'     => 'Street',
106
            'name'      => 'street',
107
            'entity'    => 'accountDetails.addresses',
108
            'attribute' => 'street',
109
        ],
110
    ];
111
112
    private $userInputHasOneRelation = [
113
        [
114
            'name' => 'accountDetails.nickname',
115
        ],
116
        [
117
            'name' => 'accountDetails.profile_picture',
118
        ],
119
    ];
120
121
    private $articleInputBelongsToRelationName = [
122
        [
123
            'name' => 'user',
124
        ],
125
    ];
126
127
    public function testCreate()
128
    {
129
        $this->crudPanel->setModel(User::class);
130
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships);
131
        $faker = Factory::create();
132
        $inputData = [
133
            'name'     => $faker->name,
134
            'email'    => $faker->safeEmail,
135
            'password' => bcrypt($faker->password()),
136
        ];
137
138
        $entry = $this->crudPanel->create($inputData);
139
140
        $this->assertInstanceOf(User::class, $entry);
141
        $this->assertEntryEquals($inputData, $entry);
142
        $this->assertEmpty($entry->articles);
143
    }
144
145
    public function testCreateWithOneToOneRelationship()
146
    {
147
        $this->crudPanel->setModel(User::class);
148
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships);
149
        $this->crudPanel->addFields($this->userInputHasOneRelation);
150
        $faker = Factory::create();
151
        $account_details_nickname = $faker->name;
152
        $inputData = [
153
            'name'           => $faker->name,
154
            'email'          => $faker->safeEmail,
155
            'password'       => bcrypt($faker->password()),
156
            'accountDetails' => [
157
                'nickname'        => $account_details_nickname,
158
                'profile_picture' => 'test.jpg',
159
            ],
160
        ];
161
        $entry = $this->crudPanel->create($inputData);
162
        $account_details = $entry->accountDetails()->first();
163
164
        $this->assertEquals($account_details->nickname, $account_details_nickname);
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...
165
    }
166
167
    public function testCreateWithOneToOneRelationshipUsingRepeatableInterface()
168
    {
169
        $this->crudPanel->setModel(User::class);
170
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships);
171
        $this->crudPanel->addField([
172
            'name'   => 'accountDetails',
173
            'fields' => [
174
                [
175
                    'name' => 'nickname',
176
                ],
177
                [
178
                    'name' => 'profile_picture',
179
                ],
180
            ],
181
        ]);
182
        $faker = Factory::create();
183
        $account_details_nickname = $faker->name;
184
        $inputData = [
185
            'name'           => $faker->name,
186
            'email'          => $faker->safeEmail,
187
            'password'       => bcrypt($faker->password()),
188
            'accountDetails' => [
189
                ['nickname' => $account_details_nickname, 'profile_picture' => 'test.jpg'],
190
            ],
191
        ];
192
        $entry = $this->crudPanel->create($inputData);
193
        $account_details = $entry->accountDetails()->first();
194
195
        $this->assertEquals($account_details->nickname, $account_details_nickname);
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...
196
    }
197
198
    public function testCreateBelongsToWithRelationName()
199
    {
200
        $this->crudPanel->setModel(Article::class);
201
        $this->crudPanel->addFields($this->articleInputFieldsOneToMany);
202
        $this->crudPanel->removeField('user_id');
203
        $this->crudPanel->addFields($this->articleInputBelongsToRelationName);
204
        $faker = Factory::create();
205
        $inputData = [
206
            'content'     => $faker->text(),
207
            'tags'        => $faker->words(3, true),
208
            'user'        => 1,
209
            'metas'       => null,
210
            'extras'      => null,
211
            'cast_metas'  => null,
212
            'cast_tags'   => null,
213
            'cast_extras' => null,
214
        ];
215
        $entry = $this->crudPanel->create($inputData);
216
        $userEntry = User::find(1);
0 ignored issues
show
Unused Code introduced by
The assignment to $userEntry is dead and can be removed.
Loading history...
217
        $article = Article::where('user_id', 1)->with('user')->get()->last();
218
        $this->assertEquals($article->user_id, $entry->user_id);
219
        $this->assertEquals($article->id, $entry->id);
220
    }
221
222
    public function testCreateWithOneToManyRelationship()
223
    {
224
        $this->crudPanel->setModel(Article::class);
225
        $this->crudPanel->addFields($this->articleInputFieldsOneToMany);
226
        $faker = Factory::create();
227
        $inputData = [
228
            'content'     => $faker->text(),
229
            'tags'        => $faker->words(3, true),
230
            'user_id'     => 1,
231
            'metas'       => null,
232
            'extras'      => null,
233
            'cast_metas'  => null,
234
            'cast_tags'   => null,
235
            'cast_extras' => null,
236
        ];
237
238
        $entry = $this->crudPanel->create($inputData);
239
        $userEntry = User::find(1);
0 ignored issues
show
Unused Code introduced by
The assignment to $userEntry is dead and can be removed.
Loading history...
240
        $article = Article::where('user_id', 1)->with('user')->get()->last();
241
        $this->assertEntryEquals($inputData, $entry);
242
        $this->assertEquals($article->user_id, $entry->user_id);
243
        $this->assertEquals($article->id, $entry->id);
244
    }
245
246
    public function testCreateWithManyToManyRelationship()
247
    {
248
        $this->crudPanel->setModel(User::class);
249
        $this->crudPanel->addFields($this->userInputFieldsManyToMany);
250
        $faker = Factory::create();
251
        $inputData = [
252
            'name'           => $faker->name,
253
            'email'          => $faker->safeEmail,
254
            'password'       => bcrypt($faker->password()),
255
            'remember_token' => null,
256
            'roles'          => [1, 2],
257
        ];
258
259
        $entry = $this->crudPanel->create($inputData);
260
261
        $this->assertInstanceOf(User::class, $entry);
262
        $this->assertEntryEquals($inputData, $entry);
263
    }
264
265
    public function testGetRelationFields()
266
    {
267
        $this->markTestIncomplete('Not correctly implemented');
268
269
        $this->crudPanel->setModel(User::class);
270
        $this->crudPanel->addFields($this->userInputFieldsManyToMany, 'create');
271
272
        // TODO: fix method and documentation. when 'both' is passed as the $form value, the getRelationFields searches
273
        //       for relationship fields in the update fields.
274
        $relationFields = $this->crudPanel->getRelationFields('both');
275
276
        $this->assertEquals($this->crudPanel->create_fields['roles'], Arr::last($relationFields));
277
    }
278
279
    public function testGetRelationFieldsCreateForm()
280
    {
281
        $this->crudPanel->setModel(User::class);
282
        $this->crudPanel->setOperation('create');
283
        $this->crudPanel->addFields($this->userInputFieldsManyToMany);
284
285
        $relationFields = $this->crudPanel->getRelationFields();
286
287
        $this->assertEquals($this->crudPanel->get('create.fields')['roles'], Arr::last($relationFields));
288
    }
289
290
    public function testGetRelationFieldsUpdateForm()
291
    {
292
        $this->crudPanel->setModel(User::class);
293
        $this->crudPanel->setOperation('update');
294
        $this->crudPanel->addFields($this->userInputFieldsManyToMany);
295
296
        $relationFields = $this->crudPanel->getRelationFields();
297
298
        $this->assertEquals($this->crudPanel->get('update.fields')['roles'], Arr::last($relationFields));
299
    }
300
301
    public function testGetRelationFieldsUnknownForm()
302
    {
303
        $this->markTestIncomplete('Not correctly implemented');
304
305
        $this->expectException(\InvalidArgumentException::class);
306
307
        $this->crudPanel->setModel(User::class);
308
        $this->crudPanel->addFields($this->userInputFieldsManyToMany);
309
310
        // TODO: this should throw an invalid argument exception but instead it searches for relationship fields in the
311
        //       update fields.
312
        $this->crudPanel->getRelationFields('unknownForm');
313
    }
314
315
    public function testGetRelationFieldsDotNotation()
316
    {
317
        $this->crudPanel->setModel(User::class);
318
        $this->crudPanel->setOperation('create');
319
320
        $this->crudPanel->addFields($this->userInputFieldsDotNotation);
321
322
        //get all fields with a relation
323
        $relationFields = $this->crudPanel->getRelationFields();
324
325
        $this->assertEquals($this->crudPanel->get('create.fields')['street'], Arr::last($relationFields));
326
    }
327
328
    public function testCreateHasOneRelations()
329
    {
330
        $this->crudPanel->setModel(User::class);
331
        $this->crudPanel->setOperation('create');
332
333
        $this->crudPanel->addFields($this->userInputHasOneRelation);
334
        $faker = Factory::create();
335
336
        $inputData = [
337
            'name'           => $faker->name,
338
            'email'          => $faker->safeEmail,
339
            'password'       => bcrypt($faker->password()),
340
            'remember_token' => null,
341
            'roles'          => [1, 2],
342
            'accountDetails' => [
343
                'nickname'        => 'i_have_has_one',
344
                'profile_picture' => 'simple_picture.jpg',
345
            ],
346
        ];
347
        $entry = $this->crudPanel->create($inputData);
348
        $updateFields = $this->crudPanel->getUpdateFields($entry->id);
0 ignored issues
show
Unused Code introduced by
The assignment to $updateFields is dead and can be removed.
Loading history...
349
        $account_details = $entry->accountDetails()->first();
350
351
        $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...
352
    }
353
354
    public function testGetRelationFieldsNoRelations()
355
    {
356
        $this->crudPanel->addField($this->nonRelationshipField);
357
358
        $relationFields = $this->crudPanel->getRelationFields();
359
360
        $this->assertEmpty($relationFields);
361
    }
362
363
    public function testGetRelationFieldsNoFields()
364
    {
365
        $relationFields = $this->crudPanel->getRelationFields();
366
367
        $this->assertEmpty($relationFields);
368
    }
369
370
    public function testGetRelationFieldsWithPivot()
371
    {
372
        $this->crudPanel->setModel(User::class);
373
        $this->crudPanel->setOperation('create');
374
        $this->crudPanel->addFields($this->userInputFieldsDotNotation);
375
376
        $relationFields = $this->crudPanel->getRelationFieldsWithPivot();
377
        $this->assertEquals($this->crudPanel->get('create.fields')['roles'], Arr::first($relationFields));
378
    }
379
380
    public function testGetRelationFieldsWithPivotNoRelations()
381
    {
382
        $this->crudPanel->setModel(User::class);
383
        $this->crudPanel->setOperation('create');
384
        $this->crudPanel->addFields($this->nonRelationshipField);
385
386
        $relationFields = $this->crudPanel->getRelationFieldsWithPivot();
387
388
        $this->assertEmpty($relationFields);
389
    }
390
391
    public function testMorphToManySelectableRelationship()
392
    {
393
        $this->crudPanel->setModel(User::class);
394
        $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

394
        $this->crudPanel->/** @scrutinizer ignore-call */ 
395
                          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...
395
        $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

395
        $this->crudPanel->/** @scrutinizer ignore-call */ 
396
                          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...
396
397
        $faker = Factory::create();
398
        $inputData = [
399
            'name'           => $faker->name,
400
            'email'          => $faker->safeEmail,
401
            'password'       => bcrypt($faker->password()),
402
            'remember_token' => null,
403
            'bills'          => [1],
404
        ];
405
406
        $entry = $this->crudPanel->create($inputData);
407
408
        $updateFields = $this->crudPanel->getUpdateFields($entry->id);
0 ignored issues
show
Unused Code introduced by
The assignment to $updateFields is dead and can be removed.
Loading history...
409
410
        $this->assertCount(1, $entry->bills);
411
412
        $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...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property id does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
413
414
        $inputData['bills'] = [1, 2];
415
416
        $this->crudPanel->update($entry->id, $inputData);
417
418
        $this->assertCount(2, $entry->fresh()->bills);
419
420
        $this->assertEquals([1, 2], $entry->fresh()->bills->pluck('id')->toArray());
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 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 Illuminate\Support\Enumerable or Prologue\Alerts\AlertsMessageBag or 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

420
        $this->assertEquals([1, 2], $entry->fresh()->bills->/** @scrutinizer ignore-call */ pluck('id')->toArray());
Loading history...
421
    }
422
423
    public function testMorphToManyCreatableRelationship()
424
    {
425
        $this->crudPanel->setModel(User::class);
426
        $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

426
        $this->crudPanel->/** @scrutinizer ignore-call */ 
427
                          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...
427
        $this->crudPanel->addField(['name' => 'recommends', 'subfields' => [
428
            [
429
                'name' => 'text',
430
            ],
431
        ]], '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

431
        $this->crudPanel->/** @scrutinizer ignore-call */ 
432
                          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...
432
433
        $faker = Factory::create();
434
        $inputData = [
435
            'name'                => $faker->name,
436
            'email'               => $faker->safeEmail,
437
            'password'            => bcrypt($faker->password()),
438
            'remember_token'      => null,
439
            'recommends'          => [
440
                [
441
                    'recommends' => 1,
442
                    'text'       => 'my pivot recommend field',
443
                ],
444
            ],
445
        ];
446
447
        $entry = $this->crudPanel->create($inputData);
448
        $updateFields = $this->crudPanel->getUpdateFields($entry->id);
0 ignored issues
show
Unused Code introduced by
The assignment to $updateFields is dead and can be removed.
Loading history...
449
450
        $this->assertCount(1, $entry->recommends);
451
452
        $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...
453
454
        $inputData['recommends'] = [
455
            [
456
                'recommends' => 2,
457
                'text'       => 'I changed the recommend and the pivot text',
458
            ],
459
        ];
460
461
        $this->crudPanel->update($entry->id, $inputData);
462
463
        $this->assertCount(1, $entry->fresh()->recommends);
464
465
        $this->assertEquals(2, $entry->recommends()->first()->id);
466
467
        $this->assertEquals('I changed the recommend and the pivot text', $entry->fresh()->recommends->first()->pivot->text);
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 Illuminate\Pagination\LengthAwarePaginator or Illuminate\Http\Resources\Json\ResourceCollection or Nette\Utils\Html or League\CommonMark\Util\ArrayCollection or Illuminate\Pagination\CursorPaginator 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 Illuminate\Support\Enumerable or Ramsey\Collection\CollectionInterface or Ramsey\Collection\AbstractCollection or Ds\Map or Ds\Set or Ds\Sequence or 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

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

597
        $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...elations\HasManyThrough.
Loading history...
Bug introduced by
The property addresses does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
598
        $this->assertEquals($account_details->addresses->first()->city, 1);
0 ignored issues
show
Bug introduced by
The property city does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The property city does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
599
        $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...
600
        $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...
601
        $this->assertEquals($account_details->bangs->first()->name, Bang::find(1)->name);
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

601
        $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...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...
602
        $this->assertEquals($account_details->bangsPivot->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

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

772
        $this->crudPanel->/** @scrutinizer ignore-call */ 
773
                          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...
773
        $this->crudPanel->addField([
774
            'name' => 'comment.text',
775
        ], '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

775
        $this->crudPanel->/** @scrutinizer ignore-call */ 
776
                          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...
776
777
        $faker = Factory::create();
778
        $inputData = [
779
            'name'             => $faker->name,
780
            'email'            => $faker->safeEmail,
781
            'password'         => bcrypt($faker->password()),
782
            'remember_token'   => null,
783
            'comment'          => [
784
                'text' => 'some test comment text',
785
            ],
786
        ];
787
788
        $entry = $this->crudPanel->create($inputData);
789
        $updateFields = $this->crudPanel->getUpdateFields($entry->id);
0 ignored issues
show
Unused Code introduced by
The assignment to $updateFields is dead and can be removed.
Loading history...
790
791
        $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\Eloquent\Relations\Relation.
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...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property text does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
792
793
        $inputData['comment']['text'] = 'updated comment text';
794
795
        $this->crudPanel->update($entry->id, $inputData);
796
797
        $this->assertEquals($inputData['comment']['text'], $entry->fresh()->comment->text);
798
    }
799
800
    public function testMorphManyCreatableRelationship()
801
    {
802
        $this->crudPanel->setModel(User::class);
803
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both');
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

803
        $this->crudPanel->/** @scrutinizer ignore-call */ 
804
                          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...
804
        $this->crudPanel->addField([
805
            'name'      => 'stars',
806
            'subfields' => [
807
                [
808
                    'name' => 'title',
809
                ],
810
            ],
811
        ], '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

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

856
        $this->crudPanel->/** @scrutinizer ignore-call */ 
857
                          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...
857
        $this->crudPanel->addField([
858
            'name'      => 'universes',
859
            'subfields' => [
860
                [
861
                    'name' => 'title',
862
                ],
863
            ],
864
        ], '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

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

916
        $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...
917
        $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...
918
        $this->assertEquals(1, Universe::all()->count());
919
920
        $inputData['universes'] = [
921
            [
922
                'id'    => null,
923
                'title' => 'new star 3',
924
            ],
925
        ];
926
927
        $this->crudPanel->update($entry->id, $inputData);
928
929
        $this->assertEquals($inputData['universes'][0]['title'], $entry->fresh()->universes->first()->title);
930
        $this->assertEquals(3, $entry->fresh()->universes->first()->id);
931
        $this->assertEquals(1, Universe::all()->count());
932
933
        $inputData['universes'] = null;
934
935
        $this->crudPanel->update($entry->id, $inputData);
936
937
        $this->assertEquals(0, count($entry->fresh()->universes));
938
        $this->assertEquals(0, Universe::all()->count());
939
    }
940
941
    public function testHasManySelectableRelationshipWithoutForceDelete()
942
    {
943
        $this->crudPanel->setModel(User::class);
944
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both');
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

944
        $this->crudPanel->/** @scrutinizer ignore-call */ 
945
                          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...
945
        $this->crudPanel->addField([
946
            'name'         => 'planets',
947
            'force_delete' => false,
948
            'fallback_id'  => false,
949
        ], '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

949
        $this->crudPanel->/** @scrutinizer ignore-call */ 
950
                          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...
950
951
        $faker = Factory::create();
952
        $inputData = [
953
            'name'             => $faker->name,
954
            'email'            => $faker->safeEmail,
955
            'password'         => bcrypt($faker->password()),
956
            'remember_token'   => null,
957
            'planets'          => [1, 2],
958
        ];
959
960
        $entry = $this->crudPanel->create($inputData);
961
        $updateFields = $this->crudPanel->getUpdateFields($entry->id);
0 ignored issues
show
Unused Code introduced by
The assignment to $updateFields is dead and can be removed.
Loading history...
962
963
        $this->assertCount(2, $entry->planets);
964
965
        $inputData['planets'] = [1];
966
967
        $this->crudPanel->update($entry->id, $inputData);
968
969
        $this->assertCount(1, $entry->fresh()->planets);
970
971
        $planets = Planet::all();
972
973
        $this->assertCount(2, $planets);
974
    }
975
976
    public function testHasManySelectableRelationshipRemoveAllRelations()
977
    {
978
        $this->crudPanel->setModel(User::class);
979
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both');
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

979
        $this->crudPanel->/** @scrutinizer ignore-call */ 
980
                          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...
980
        $this->crudPanel->addField([
981
            'name'         => 'planets',
982
            'force_delete' => false,
983
            'fallback_id'  => false,
984
        ], '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

984
        $this->crudPanel->/** @scrutinizer ignore-call */ 
985
                          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...
985
986
        $faker = Factory::create();
987
        $inputData = [
988
            'name'             => $faker->name,
989
            'email'            => $faker->safeEmail,
990
            'password'         => bcrypt($faker->password()),
991
            'remember_token'   => null,
992
            'planets'          => [1, 2],
993
        ];
994
995
        $entry = $this->crudPanel->create($inputData);
996
997
        $this->assertCount(2, $entry->planets);
998
999
        $inputData['planets'] = [];
1000
1001
        $this->crudPanel->update($entry->id, $inputData);
1002
1003
        $this->assertCount(0, $entry->fresh()->planets);
1004
1005
        $planets = Planet::all();
1006
1007
        $this->assertCount(2, $planets);
1008
    }
1009
1010
    public function testHasManyWithRelationScoped()
1011
    {
1012
        $this->crudPanel->setModel(User::class);
1013
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both');
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

1013
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1014
                          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...
1014
        $this->crudPanel->addField([
1015
            'name'          => 'incomes',
1016
            'subfields'     => [
1017
                [
1018
                    'name' => 'label',
1019
                    'type' => 'text',
1020
                ],
1021
                [
1022
                    'name'  => 'type',
1023
                    'type'  => 'hidden',
1024
                    'value' => 'income',
1025
                ],
1026
                [
1027
                    'name' => 'amount',
1028
                    'type' => 'number',
1029
                ],
1030
            ],
1031
        ], 'both');
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

1031
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1032
                          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...
1032
        $this->crudPanel->addField([
1033
            'name'          => 'expenses',
1034
            'subfields'     => [
1035
                [
1036
                    'name' => 'label',
1037
                    'type' => 'text',
1038
                ],
1039
                [
1040
                    'name'  => 'type',
1041
                    'type'  => 'hidden',
1042
                    'value' => 'expense',
1043
                ],
1044
                [
1045
                    'name' => 'amount',
1046
                    'type' => 'number',
1047
                ],
1048
            ],
1049
        ], 'both');
1050
1051
        $faker = Factory::create();
1052
        $inputData = [
1053
            'name'           => $faker->name,
1054
            'email'          => $faker->safeEmail,
1055
            'password'       => bcrypt($faker->password()),
1056
            'remember_token' => null,
1057
            'incomes'        => [
1058
                [
1059
                    'label'  => $faker->name,
1060
                    'amount' => 33,
1061
                    'type'   => 'income',
1062
                ],
1063
                [
1064
                    'label'  => $faker->name,
1065
                    'amount' => 22,
1066
                    'type'   => 'income',
1067
                ],
1068
            ],
1069
            'expenses' => [
1070
                [
1071
                    'label'  => $faker->name,
1072
                    'amount' => 44,
1073
                    'type'   => 'expense',
1074
                ],
1075
                [
1076
                    'label'  => $faker->name,
1077
                    'amount' => 10,
1078
                    'type'   => 'expense',
1079
                ],
1080
            ],
1081
        ];
1082
        $entry = $this->crudPanel->create($inputData);
1083
1084
        $firstExpense = $entry->expenses->first();
0 ignored issues
show
Unused Code introduced by
The assignment to $firstExpense is dead and can be removed.
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

1084
        /** @scrutinizer ignore-call */ 
1085
        $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...
1085
        $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

1085
        /** @scrutinizer ignore-call */ 
1086
        $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...
1086
        $this->assertCount(2, $entry->expenses);
1087
        $this->assertCount(2, $entry->incomes);
1088
        $this->assertEquals(44, $entry->expenses->first()->amount);
1089
        $this->assertEquals(33, $entry->incomes->first()->amount);
1090
1091
        $inputData['incomes'] = [
1092
            [
1093
                'id'     => 2,
1094
                'label'  => $faker->name,
1095
                'amount' => 222,
1096
                'type'   => 'income',
1097
            ],
1098
        ];
1099
        $inputData['expenses'] = [
1100
            [
1101
                'id'     => 3,
1102
                'label'  => $faker->name,
1103
                'amount' => 44,
1104
                'type'   => 'expense',
1105
            ],
1106
            [
1107
                'id'     => 4,
1108
                'label'  => $faker->name,
1109
                'amount' => 10,
1110
                'type'   => 'expense',
1111
            ],
1112
        ];
1113
        $this->crudPanel->update($entry->id, $inputData);
1114
1115
        $freshIncomes = $entry->fresh()->incomes;
1116
        $freshExpenses = $entry->fresh()->expenses;
1117
        $this->assertCount(2, $freshExpenses);
1118
        $this->assertCount(1, $freshIncomes);
1119
        $this->assertEquals(2, $freshIncomes->first()->id);
1120
1121
        $inputData['expenses'] = [];
1122
        $this->crudPanel->update($entry->id, $inputData);
1123
1124
        $freshIncomes = $entry->fresh()->incomes;
1125
        $freshExpenses = $entry->fresh()->expenses;
1126
        $this->assertCount(0, $freshExpenses);
1127
        $this->assertCount(1, $freshIncomes);
1128
    }
1129
1130
    public function testHasManySelectableRelationshipWithFallbackId()
1131
    {
1132
        $this->crudPanel->setModel(User::class);
1133
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both');
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

1133
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1134
                          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...
1134
        $this->crudPanel->addField([
1135
            'name'         => 'planets',
1136
            'fallback_id'  => 0,
1137
            'force_delete' => false,
1138
        ], '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

1138
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1139
                          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...
1139
1140
        $faker = Factory::create();
1141
        $inputData = [
1142
            'name'             => $faker->name,
1143
            'email'            => $faker->safeEmail,
1144
            'password'         => bcrypt($faker->password()),
1145
            'remember_token'   => null,
1146
            'planets'          => [1, 2],
1147
        ];
1148
1149
        $entry = $this->crudPanel->create($inputData);
1150
1151
        $this->assertCount(2, $entry->planets);
1152
1153
        $inputData['planets'] = [2];
1154
1155
        $this->crudPanel->update($entry->id, $inputData);
1156
1157
        $this->assertCount(1, $entry->fresh()->planets);
1158
1159
        $planets = Planet::all();
1160
        $this->assertCount(2, $planets);
1161
        $this->assertEquals(0, $planets->first()->user_id);
1162
    }
1163
1164
    public function testHasManySelectableRelationshipWithForceDelete()
1165
    {
1166
        $this->crudPanel->setModel(User::class);
1167
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both');
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

1167
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1168
                          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...
1168
        $this->crudPanel->addField([
1169
            'name'         => 'planets',
1170
            'force_delete' => true,
1171
            'fallback_id'  => false,
1172
        ], '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

1172
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1173
                          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...
1173
1174
        $faker = Factory::create();
1175
        $inputData = [
1176
            'name'             => $faker->name,
1177
            'email'            => $faker->safeEmail,
1178
            'password'         => bcrypt($faker->password()),
1179
            'remember_token'   => null,
1180
            'planets'          => [1, 2],
1181
        ];
1182
1183
        $entry = $this->crudPanel->create($inputData);
1184
1185
        $this->assertCount(2, $entry->planets);
1186
1187
        $inputData['planets'] = [2];
1188
1189
        $this->crudPanel->update($entry->id, $inputData);
1190
1191
        $this->assertCount(1, $entry->fresh()->planets);
1192
1193
        $planets = Planet::all();
1194
        $this->assertCount(1, $planets);
1195
    }
1196
1197
    public function testHasManySelectableRelationshipNonNullableForeignKeyAndDefaultInDatabase()
1198
    {
1199
        $this->crudPanel->setModel(User::class);
1200
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both');
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

1200
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1201
                          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...
1201
        $this->crudPanel->addField([
1202
            'name'         => 'comets',
1203
            'force_delete' => false,
1204
            'fallback_id'  => false,
1205
        ], '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

1205
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1206
                          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...
1206
1207
        $faker = Factory::create();
1208
        $inputData = [
1209
            'name'            => $faker->name,
1210
            'email'           => $faker->safeEmail,
1211
            'password'        => bcrypt($faker->password()),
1212
            'remember_token'  => null,
1213
            'comets'          => [1, 2],
1214
        ];
1215
1216
        $entry = $this->crudPanel->create($inputData);
1217
1218
        $this->assertCount(2, $entry->comets);
1219
1220
        $inputData['comets'] = [2];
1221
1222
        $this->crudPanel->update($entry->id, $inputData);
1223
1224
        $this->assertCount(1, $entry->fresh()->comets);
1225
1226
        $comets = Comet::all();
1227
        $this->assertCount(2, $comets);
1228
        $this->assertEquals(0, $comets->first()->user_id);
1229
    }
1230
1231
    public function testHasManySelectableRelationshipNonNullable()
1232
    {
1233
        $this->crudPanel->setModel(User::class);
1234
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both');
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

1234
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1235
                          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...
1235
        $this->crudPanel->addField([
1236
            'name'         => 'planetsNonNullable',
1237
            'force_delete' => false,
1238
            'fallback_id'  => false,
1239
        ], '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

1239
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1240
                          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...
1240
1241
        $faker = Factory::create();
1242
        $inputData = [
1243
            'name'                        => $faker->name,
1244
            'email'                       => $faker->safeEmail,
1245
            'password'                    => bcrypt($faker->password()),
1246
            'remember_token'              => null,
1247
            'planetsNonNullable'          => [1, 2],
1248
        ];
1249
1250
        $entry = $this->crudPanel->create($inputData);
1251
1252
        $this->assertCount(2, $entry->planetsNonNullable);
1253
1254
        $inputData['planetsNonNullable'] = null;
1255
1256
        $this->crudPanel->update($entry->id, $inputData);
1257
1258
        $this->assertCount(0, $entry->fresh()->planetsNonNullable);
1259
1260
        $planets = PlanetNonNullable::all();
1261
        $this->assertCount(0, $planets);
1262
    }
1263
1264
    public function testCreateHasManyRelationWithDelimitedNameSubfields()
1265
    {
1266
        $this->crudPanel->setModel(User::class);
1267
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both');
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

1267
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1268
                          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...
1268
        $this->crudPanel->addField([
1269
            'name'      => 'universes',
1270
            'subfields' => [
1271
                [
1272
                    'name' => 'title',
1273
                ],
1274
                [
1275
                    'name' => 'start_date,end_date',
1276
                    'type' => 'date_range',
1277
                ],
1278
            ],
1279
        ], 'both');
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

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