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
Pull Request — main (#5640)
by Pedro
24:59 queued 10:03
created

testItCanCreateDynamicRelationships()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 15
nc 1
nop 0
dl 0
loc 25
rs 9.7666
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
use Illuminate\Support\Facades\Hash;
17
18
/**
19
 * @covers Backpack\CRUD\app\Library\CrudPanel\Traits\Create
20
 * @covers Backpack\CRUD\app\Library\CrudPanel\Traits\Relationships
21
 * @covers Backpack\CRUD\app\Library\CrudPanel\Traits\FieldsProtectedMethods
22
 * @covers Backpack\CRUD\app\Library\CrudPanel\Traits\Update
23
 * @covers Backpack\CRUD\app\Library\CrudPanel\Traits\Input
24
 * @covers Backpack\CRUD\app\Library\CrudPanel\Traits\MorphRelationships
25
 * @covers Backpack\CRUD\app\Library\CrudPanel\Traits\Fields
26
 * @covers Backpack\CRUD\app\Library\CrudPanel\CrudField
27
 */
28
class CrudPanelCreateTest extends \Backpack\CRUD\Tests\config\CrudPanel\BaseDBCrudPanel
29
{
30
    private $nonRelationshipField = [
31
        'name' => 'field1',
32
        'label' => 'Field1',
33
    ];
34
35
    private $userInputFieldsNoRelationships = [
36
        [
37
            'name' => 'id',
38
            'type' => 'hidden',
39
        ], [
40
            'name' => 'name',
41
        ], [
42
            'name' => 'email',
43
            'type' => 'email',
44
        ], [
45
            'name' => 'password',
46
            'type' => 'password',
47
        ],
48
    ];
49
50
    private $articleInputFieldsOneToMany = [
51
        [
52
            'name' => 'id',
53
            'type' => 'hidden',
54
        ], [
55
            'name' => 'content',
56
        ], [
57
            'name' => 'tags',
58
        ], [
59
            'label' => 'Author',
60
            'type' => 'select',
61
            'name' => 'user_id',
62
            'entity' => 'user',
63
            'attribute' => 'name',
64
        ],
65
    ];
66
67
    private $userInputFieldsManyToMany = [
68
        [
69
            'name' => 'id',
70
            'type' => 'hidden',
71
        ], [
72
            'name' => 'name',
73
        ], [
74
            'name' => 'email',
75
            'type' => 'email',
76
        ], [
77
            'name' => 'password',
78
            'type' => 'password',
79
        ], [
80
            'label' => 'Roles',
81
            'type' => 'select_multiple',
82
            'name' => 'roles',
83
            'entity' => 'roles',
84
            'attribute' => 'name',
85
            'pivot' => true,
86
        ],
87
    ];
88
89
    private $userInputFieldsDotNotation = [
90
        [
91
            'name' => 'id',
92
            'type' => 'hidden',
93
        ], [
94
            'name' => 'name',
95
        ], [
96
            'name' => 'email',
97
            'type' => 'email',
98
        ], [
99
            'name' => 'password',
100
            'type' => 'password',
101
        ], [
102
            'label' => 'Roles',
103
            'type' => 'relationship',
104
            'name' => 'roles',
105
            'entity' => 'roles',
106
            'attribute' => 'name',
107
        ], [
108
            'label' => 'Street',
109
            'name' => 'street',
110
            'entity' => 'accountDetails.addresses',
111
            'attribute' => 'street',
112
        ],
113
    ];
114
115
    private $userInputHasOneRelation = [
116
        [
117
            'name' => 'accountDetails.nickname',
118
        ],
119
        [
120
            'name' => 'accountDetails.profile_picture',
121
        ],
122
    ];
123
124
    private $articleInputBelongsToRelationName = [
125
        [
126
            'name' => 'user',
127
        ],
128
    ];
129
130
    public function testCreate()
131
    {
132
        $this->crudPanel->setModel(User::class);
133
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships);
134
        $faker = Factory::create();
135
        $inputData = [
136
            'name' => $faker->name,
137
            'email' => $faker->safeEmail,
138
            'password' => Hash::make($faker->password()),
139
        ];
140
141
        $entry = $this->crudPanel->create($inputData);
142
143
        $this->assertInstanceOf(User::class, $entry);
144
        $this->assertEntryEquals($inputData, $entry);
145
        $this->assertEmpty($entry->articles);
146
    }
147
148
    public function testCreateWithOneToOneRelationship()
149
    {
150
        $this->crudPanel->setModel(User::class);
151
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships);
152
        $this->crudPanel->addFields($this->userInputHasOneRelation);
153
        $faker = Factory::create();
154
        $account_details_nickname = $faker->name;
155
        $inputData = [
156
            'name' => $faker->name,
157
            'email' => $faker->safeEmail,
158
            'password' => Hash::make($faker->password()),
159
            'accountDetails' => [
160
                'nickname' => $account_details_nickname,
161
                'profile_picture' => 'test.jpg',
162
            ],
163
        ];
164
        $entry = $this->crudPanel->create($inputData);
165
        $account_details = $entry->accountDetails()->first();
166
167
        $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...
168
    }
169
170
    public function testCreateWithOneToOneDynamicRelationship()
171
    {
172
        User::resolveRelationUsing('dynamicRelation', function ($user) {
173
            return $user->hasOne(\Backpack\CRUD\Tests\config\Models\AccountDetails::class);
174
        });
175
        $this->crudPanel->setModel(User::class);
176
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships);
177
        $this->crudPanel->addFields([
178
            [
179
                'name' => 'dynamicRelation.nickname',
180
            ],
181
            [
182
                'name' => 'dynamicRelation.profile_picture',
183
            ],
184
        ]);
185
        $faker = Factory::create();
186
        $account_details_nickname = $faker->name;
187
        $inputData = [
188
            'name' => $faker->name,
189
            'email' => $faker->safeEmail,
190
            'password' => Hash::make($faker->password()),
191
            'dynamicRelation' => [
192
                'nickname' => $account_details_nickname,
193
                'profile_picture' => 'test.jpg',
194
            ],
195
        ];
196
        $entry = $this->crudPanel->create($inputData);
197
        $account_details = $entry->accountDetails()->first();
198
199
        $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...
200
    }
201
202
    public function testCreateWithOneToOneRelationshipUsingRepeatableInterface()
203
    {
204
        $this->crudPanel->setModel(User::class);
205
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships);
206
        $this->crudPanel->addField([
207
            'name' => 'accountDetails',
208
            'fields' => [
209
                [
210
                    'name' => 'nickname',
211
                ],
212
                [
213
                    'name' => 'profile_picture',
214
                ],
215
            ],
216
        ]);
217
        $faker = Factory::create();
218
        $account_details_nickname = $faker->name;
219
        $inputData = [
220
            'name' => $faker->name,
221
            'email' => $faker->safeEmail,
222
            'password' => Hash::make($faker->password()),
223
            'accountDetails' => [
224
                ['nickname' => $account_details_nickname, 'profile_picture' => 'test.jpg'],
225
            ],
226
        ];
227
        $entry = $this->crudPanel->create($inputData);
228
        $account_details = $entry->accountDetails()->first();
229
230
        $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...
231
    }
232
233
    public function testCreateBelongsToWithRelationName()
234
    {
235
        $this->crudPanel->setModel(Article::class);
236
        $this->crudPanel->addFields($this->articleInputFieldsOneToMany);
237
        $this->crudPanel->removeField('user_id');
238
        $this->crudPanel->addFields($this->articleInputBelongsToRelationName);
239
        $faker = Factory::create();
240
        $inputData = [
241
            'content' => $faker->text(),
242
            'tags' => $faker->words(3, true),
243
            'user' => 1,
244
            'metas' => null,
245
            'extras' => null,
246
            'cast_metas' => null,
247
            'cast_tags' => null,
248
            'cast_extras' => null,
249
        ];
250
        $entry = $this->crudPanel->create($inputData);
251
        $userEntry = User::find(1);
0 ignored issues
show
Unused Code introduced by
The assignment to $userEntry is dead and can be removed.
Loading history...
252
        $article = Article::where('user_id', 1)->with('user')->get()->last();
253
        $this->assertEquals($article->user_id, $entry->user_id);
254
        $this->assertEquals($article->id, $entry->id);
255
    }
256
257
    public function testCreateWithOneToManyRelationship()
258
    {
259
        $this->crudPanel->setModel(Article::class);
260
        $this->crudPanel->addFields($this->articleInputFieldsOneToMany);
261
        $faker = Factory::create();
262
        $inputData = [
263
            'content' => $faker->text(),
264
            'tags' => $faker->words(3, true),
265
            'user_id' => 1,
266
            'metas' => null,
267
            'extras' => null,
268
            'cast_metas' => null,
269
            'cast_tags' => null,
270
            'cast_extras' => null,
271
        ];
272
273
        $entry = $this->crudPanel->create($inputData);
274
        $userEntry = User::find(1);
0 ignored issues
show
Unused Code introduced by
The assignment to $userEntry is dead and can be removed.
Loading history...
275
        $article = Article::where('user_id', 1)->with('user')->get()->last();
276
        $this->assertEntryEquals($inputData, $entry);
277
        $this->assertEquals($article->user_id, $entry->user_id);
278
        $this->assertEquals($article->id, $entry->id);
279
    }
280
281
    public function testCreateWithOneToManyDynamicRelationship()
282
    {
283
        Article::resolveRelationUsing('dynamicRelation', function ($article) {
284
            return $article->belongsTo(\Backpack\CRUD\Tests\config\Models\User::class, 'user_id');
285
        });
286
287
        $this->crudPanel->setModel(Article::class);
288
        $this->crudPanel->addFields([
289
            [
290
                'name' => 'id',
291
                'type' => 'hidden',
292
            ], [
293
                'name' => 'content',
294
            ], [
295
                'name' => 'tags',
296
            ], [
297
                'name' => 'dynamicRelation',
298
            ],
299
        ]);
300
        $faker = Factory::create();
301
        $inputData = [
302
            'content' => $faker->text(),
303
            'tags' => $faker->words(3, true),
304
            'dynamicRelation' => 1,
305
            'metas' => null,
306
            'extras' => null,
307
            'cast_metas' => null,
308
            'cast_tags' => null,
309
            'cast_extras' => null,
310
        ];
311
312
        $entry = $this->crudPanel->create($inputData);
313
314
        unset($inputData['dynamicRelation']);
315
        $inputData['user_id'] = 1;
316
317
        $userEntry = User::find(1);
0 ignored issues
show
Unused Code introduced by
The assignment to $userEntry is dead and can be removed.
Loading history...
318
        $article = Article::where('user_id', 1)->with('dynamicRelation')->get()->last();
319
        $this->assertEntryEquals($inputData, $entry);
320
        $this->assertEquals($article->user_id, $entry->user_id);
321
        $this->assertEquals($article->id, $entry->id);
322
        $this->assertEquals($article->user_id, $entry->dynamicRelation->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...
Bug introduced by
The property id does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
Bug introduced by
The property id does not seem to exist on Illuminate\Database\Eloquent\Relations\Relation.
Loading history...
323
    }
324
325
    public function testCreateWithManyToManyRelationship()
326
    {
327
        $this->crudPanel->setModel(User::class);
328
        $this->crudPanel->addFields($this->userInputFieldsManyToMany);
329
        $faker = Factory::create();
330
        $inputData = [
331
            'name' => $faker->name,
332
            'email' => $faker->safeEmail,
333
            'password' => Hash::make($faker->password()),
334
            'remember_token' => null,
335
            'roles' => [1, 2],
336
        ];
337
338
        $entry = $this->crudPanel->create($inputData);
339
340
        $this->assertInstanceOf(User::class, $entry);
341
        $this->assertEntryEquals($inputData, $entry);
342
    }
343
344
    public function testCreateWithManyToManyDynamicRelationship()
345
    {
346
        User::resolveRelationUsing('dynamicRelation', function ($user) {
347
            return $user->belongsToMany(\Backpack\CRUD\Tests\config\Models\Role::class, 'user_role');
348
        });
349
350
        $this->crudPanel->setModel(User::class);
351
        $this->crudPanel->addFields([
352
            [
353
                'name' => 'id',
354
                'type' => 'hidden',
355
            ], [
356
                'name' => 'name',
357
            ], [
358
                'name' => 'email',
359
                'type' => 'email',
360
            ], [
361
                'name' => 'password',
362
                'type' => 'password',
363
            ], [
364
                'name' => 'dynamicRelation',
365
            ],
366
        ]);
367
        $faker = Factory::create();
368
        $inputData = [
369
            'name' => $faker->name,
370
            'email' => $faker->safeEmail,
371
            'password' => Hash::make($faker->password()),
372
            'remember_token' => null,
373
            'dynamicRelation' => [1, 2],
374
        ];
375
376
        $entry = $this->crudPanel->create($inputData);
377
378
        $this->assertInstanceOf(User::class, $entry);
379
        $this->assertEntryEquals($inputData, $entry);
380
        $this->assertCount(2, $entry->dynamicRelation);
381
    }
382
383
    public function testGetRelationFields()
384
    {
385
        $this->markTestIncomplete('Not correctly implemented');
386
387
        $this->crudPanel->setModel(User::class);
388
        $this->crudPanel->addFields($this->userInputFieldsManyToMany, 'create');
389
390
        // TODO: fix method and documentation. when 'both' is passed as the $form value, the getRelationFields searches
391
        //       for relationship fields in the update fields.
392
        $relationFields = $this->crudPanel->getRelationFields('both');
393
394
        $this->assertEquals($this->crudPanel->create_fields['roles'], Arr::last($relationFields));
395
    }
396
397
    public function testGetRelationFieldsCreateForm()
398
    {
399
        $this->crudPanel->setModel(User::class);
400
        $this->crudPanel->setOperation('create');
401
        $this->crudPanel->addFields($this->userInputFieldsManyToMany);
402
403
        $relationFields = $this->crudPanel->getRelationFields();
404
405
        $this->assertEquals($this->crudPanel->get('create.fields')['roles'], Arr::last($relationFields));
406
    }
407
408
    public function testGetRelationFieldsUpdateForm()
409
    {
410
        $this->crudPanel->setModel(User::class);
411
        $this->crudPanel->setOperation('update');
412
        $this->crudPanel->addFields($this->userInputFieldsManyToMany);
413
414
        $relationFields = $this->crudPanel->getRelationFields();
415
416
        $this->assertEquals($this->crudPanel->get('update.fields')['roles'], Arr::last($relationFields));
417
    }
418
419
    public function testGetRelationFieldsUnknownForm()
420
    {
421
        $this->markTestIncomplete('Not correctly implemented');
422
423
        $this->expectException(\InvalidArgumentException::class);
424
425
        $this->crudPanel->setModel(User::class);
426
        $this->crudPanel->addFields($this->userInputFieldsManyToMany);
427
428
        // TODO: this should throw an invalid argument exception but instead it searches for relationship fields in the
429
        //       update fields.
430
        $this->crudPanel->getRelationFields('unknownForm');
431
    }
432
433
    public function testGetRelationFieldsDotNotation()
434
    {
435
        $this->crudPanel->setModel(User::class);
436
        $this->crudPanel->setOperation('create');
437
438
        $this->crudPanel->addFields($this->userInputFieldsDotNotation);
439
440
        //get all fields with a relation
441
        $relationFields = $this->crudPanel->getRelationFields();
442
443
        $this->assertEquals($this->crudPanel->get('create.fields')['street'], Arr::last($relationFields));
444
    }
445
446
    public function testCreateHasOneRelations()
447
    {
448
        $this->crudPanel->setModel(User::class);
449
        $this->crudPanel->setOperation('create');
450
451
        $this->crudPanel->addFields($this->userInputHasOneRelation);
452
        $faker = Factory::create();
453
454
        $inputData = [
455
            'name' => $faker->name,
456
            'email' => $faker->safeEmail,
457
            'password' => Hash::make($faker->password()),
458
            'remember_token' => null,
459
            'roles' => [1, 2],
460
            'accountDetails' => [
461
                'nickname' => 'i_have_has_one',
462
                'profile_picture' => 'simple_picture.jpg',
463
            ],
464
        ];
465
        $entry = $this->crudPanel->create($inputData);
466
        $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...
467
        $account_details = $entry->accountDetails()->first();
468
469
        $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...
470
    }
471
472
    public function testGetRelationFieldsNoRelations()
473
    {
474
        $this->crudPanel->addField($this->nonRelationshipField);
475
476
        $relationFields = $this->crudPanel->getRelationFields();
477
478
        $this->assertEmpty($relationFields);
479
    }
480
481
    public function testGetRelationFieldsNoFields()
482
    {
483
        $relationFields = $this->crudPanel->getRelationFields();
484
485
        $this->assertEmpty($relationFields);
486
    }
487
488
    public function testGetRelationFieldsWithPivot()
489
    {
490
        $this->crudPanel->setModel(User::class);
491
        $this->crudPanel->setOperation('create');
492
        $this->crudPanel->addFields($this->userInputFieldsDotNotation);
493
494
        $relationFields = $this->crudPanel->getRelationFieldsWithPivot();
495
        $this->assertEquals($this->crudPanel->get('create.fields')['roles'], Arr::first($relationFields));
496
    }
497
498
    public function testGetRelationFieldsWithPivotNoRelations()
499
    {
500
        $this->crudPanel->setModel(User::class);
501
        $this->crudPanel->setOperation('create');
502
        $this->crudPanel->addFields($this->nonRelationshipField);
503
504
        $relationFields = $this->crudPanel->getRelationFieldsWithPivot();
505
506
        $this->assertEmpty($relationFields);
507
    }
508
509
    public function testMorphToManySelectableRelationship()
510
    {
511
        $this->crudPanel->setModel(User::class);
512
        $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

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

513
        $this->crudPanel->/** @scrutinizer ignore-call */ 
514
                          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...
514
515
        $faker = Factory::create();
516
        $inputData = [
517
            'name' => $faker->name,
518
            'email' => $faker->safeEmail,
519
            'password' => Hash::make($faker->password()),
520
            'remember_token' => null,
521
            'bills' => [1],
522
        ];
523
524
        $entry = $this->crudPanel->create($inputData);
525
526
        $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...
527
528
        $this->assertCount(1, $entry->bills);
529
530
        $this->assertEquals(1, $entry->bills()->first()->id);
0 ignored issues
show
Bug introduced by
The property id does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The property id does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
531
532
        $inputData['bills'] = [1, 2];
533
534
        $this->crudPanel->update($entry->id, $inputData);
535
536
        $this->assertCount(2, $entry->fresh()->bills);
537
538
        $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 RectorPrefix202408\Nette\Utils\ArrayList or Illuminate\Pagination\LengthAwarePaginator or Illuminate\Http\Resources\Json\ResourceCollection or Nette\Utils\Html or RectorPrefix202408\Nette\Iterators\CachingIterator or Illuminate\Pagination\CursorPaginator or Carbon\CarbonPeriod or Illuminate\Pagination\Paginator or Nette\Iterators\CachingIterator or Nette\Utils\ArrayList or Illuminate\Support\ViewErrorBag or RectorPrefix202408\Nette\Utils\Html or Illuminate\Support\Enumerable or Prologue\Alerts\AlertsMessageBag or RectorPrefix202408\Nette\Iterators\CachingIterator 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

538
        $this->assertEquals([1, 2], $entry->fresh()->bills->/** @scrutinizer ignore-call */ pluck('id')->toArray());
Loading history...
539
    }
540
541
    public function testMorphToManyCreatableRelationship()
542
    {
543
        $this->crudPanel->setModel(User::class);
544
        $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

544
        $this->crudPanel->/** @scrutinizer ignore-call */ 
545
                          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...
545
        $this->crudPanel->addField(['name' => 'recommends', 'subfields' => [
546
            [
547
                'name' => 'text',
548
            ],
549
        ]], '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

549
        $this->crudPanel->/** @scrutinizer ignore-call */ 
550
                          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...
550
551
        $faker = Factory::create();
552
        $inputData = [
553
            'name' => $faker->name,
554
            'email' => $faker->safeEmail,
555
            'password' => Hash::make($faker->password()),
556
            'remember_token' => null,
557
            'recommends' => [
558
                [
559
                    'recommends' => 1,
560
                    'text' => 'my pivot recommend field',
561
                ],
562
            ],
563
        ];
564
565
        $entry = $this->crudPanel->create($inputData);
566
        $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...
567
568
        $this->assertCount(1, $entry->recommends);
569
570
        $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...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property id does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
571
572
        $inputData['recommends'] = [
573
            [
574
                'recommends' => 2,
575
                'text' => 'I changed the recommend and the pivot text',
576
            ],
577
        ];
578
579
        $this->crudPanel->update($entry->id, $inputData);
580
581
        $this->assertCount(1, $entry->fresh()->recommends);
582
583
        $this->assertEquals(2, $entry->recommends()->first()->id);
584
585
        $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 RectorPrefix202408\Nette\Utils\ArrayList or RectorPrefix202408\Illum...acts\Support\MessageBag or Illuminate\Pagination\LengthAwarePaginator or Illuminate\Http\Resources\Json\ResourceCollection or Nette\Utils\Html or League\CommonMark\Util\ArrayCollection or RectorPrefix202408\Nette\Iterators\CachingIterator 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 RectorPrefix202408\Nette\Utils\Html or Illuminate\Support\Enumerable or Ramsey\Collection\CollectionInterface or Ramsey\Collection\AbstractCollection or Ds\Map or Ds\Set or Ds\Sequence or RectorPrefix202408\Nette\Iterators\CachingIterator 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

585
        $this->assertEquals('I changed the recommend and the pivot text', $entry->fresh()->recommends->/** @scrutinizer ignore-call */ first()->pivot->text);
Loading history...
586
    }
587
588
    public function testMorphToManyCreatableRelationshipWithMultiple()
589
    {
590
        $inputData = $this->getPivotInputData(['recommendsDuplicate' => [
591
            [
592
                'recommendsDuplicate' => 1,
593
                'text' => 'my pivot recommend field 1',
594
            ],
595
            [
596
                'recommendsDuplicate' => 2,
597
                'text' => 'my pivot recommend field 2',
598
            ],
599
            [
600
                'recommendsDuplicate' => 1,
601
                'text' => 'my pivot recommend field 1x1',
602
            ],
603
        ],
604
        ], true, true);
605
606
        $entry = $this->crudPanel->create($inputData);
607
608
        $entry = $entry->fresh();
609
610
        $this->assertCount(3, $entry->recommendsDuplicate);
611
612
        $this->assertEquals(1, $entry->recommendsDuplicate[0]->id);
613
        $this->assertEquals(1, $entry->recommendsDuplicate[2]->id);
614
615
        $inputData['recommendsDuplicate'] = [
616
            [
617
                'recommendsDuplicate' => 1,
618
                'text' => 'I changed the recommend and the pivot text',
619
                'id' => 1,
620
            ],
621
            [
622
                'recommendsDuplicate' => 2,
623
                'text' => 'I changed the recommend and the pivot text 2',
624
                'id' => 2,
625
            ],
626
            [
627
                'recommendsDuplicate' => 3,
628
                'text' => 'new recommend and the pivot text 3',
629
                'id' => null,
630
            ],
631
        ];
632
633
        $this->crudPanel->update($entry->id, $inputData);
634
635
        $entry = $entry->fresh();
636
637
        $this->assertCount(3, $entry->recommendsDuplicate);
638
        $this->assertDatabaseCount('recommendables', 3);
639
640
        $this->assertEquals('I changed the recommend and the pivot text', $entry->recommendsDuplicate[0]->pivot->text);
641
        $this->assertEquals('I changed the recommend and the pivot text 2', $entry->recommendsDuplicate[1]->pivot->text);
642
        $this->assertEquals('new recommend and the pivot text 3', $entry->recommendsDuplicate[2]->pivot->text);
643
    }
644
645
    public function testBelongsToManyWithPivotDataRelationship()
646
    {
647
        $this->crudPanel->setModel(User::class);
648
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships);
649
        $this->crudPanel->addField([
650
            'name' => 'superArticles',
651
            'subfields' => [
652
                [
653
                    'name' => 'notes',
654
                ],
655
            ],
656
        ]);
657
658
        $faker = Factory::create();
659
        $articleData = [
660
            'content' => $faker->text(),
661
            'tags' => $faker->words(3, true),
662
            'user_id' => 1,
663
        ];
664
665
        $article = Article::create($articleData);
666
667
        $inputData = [
668
            'name' => $faker->name,
669
            'email' => $faker->safeEmail,
670
            'password' => Hash::make($faker->password()),
671
            'remember_token' => null,
672
            'superArticles' => [
673
                [
674
                    '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...
675
                    'notes' => 'my first article note',
676
                ],
677
            ],
678
        ];
679
680
        $entry = $this->crudPanel->create($inputData);
681
682
        $this->assertCount(1, $entry->fresh()->superArticles);
683
        $this->assertEquals('my first article note', $entry->fresh()->superArticles->first()->pivot->notes);
684
    }
685
686
    public function testBelongsToManyWithMultipleSameRelationIdAndPivotDataRelationship()
687
    {
688
        $inputData = $this->getPivotInputData(['superArticlesDuplicates' => [
689
            [
690
                'superArticlesDuplicates' => 1,
691
                'notes' => 'my first article note',
692
                'id' => null,
693
            ],
694
            [
695
                'superArticlesDuplicates' => 1,
696
                'notes' => 'my second article note',
697
                'id' => null,
698
            ],
699
            [
700
                'superArticlesDuplicates' => 2,
701
                'notes' => 'my first article2 note',
702
                'id' => null,
703
            ],
704
        ],
705
        ], true, true);
706
707
        $entry = $this->crudPanel->create($inputData);
708
        $relationField = $this->crudPanel->getUpdateFields($entry->id)['superArticlesDuplicates'];
709
710
        $this->assertCount(3, $relationField['value']);
711
712
        $entry = $entry->fresh();
713
714
        $this->assertCount(3, $entry->superArticlesDuplicates);
715
        $this->assertEquals('my first article note', $entry->superArticles->first()->pivot->notes);
0 ignored issues
show
Bug introduced by
The property notes does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The method first() does not exist on UnitEnum. ( Ignorable by Annotation )

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

715
        $this->assertEquals('my first article note', $entry->superArticles->/** @scrutinizer ignore-call */ first()->pivot->notes);

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 notes does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property pivot does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The property pivot does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The method first() does not exist on BackedEnum. ( Ignorable by Annotation )

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

715
        $this->assertEquals('my first article note', $entry->superArticles->/** @scrutinizer ignore-call */ first()->pivot->notes);

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 notes does not seem to exist on Illuminate\Database\Eloquent\Relations\Relation.
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

715
        $this->assertEquals('my first article note', $entry->superArticles->/** @scrutinizer ignore-call */ first()->pivot->notes);

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 notes does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
716
        $this->assertEquals('my second article note', $entry->superArticles[1]->pivot->notes);
717
        $this->assertEquals('my first article2 note', $entry->superArticles[2]->pivot->notes);
718
719
        $inputData = $this->getPivotInputData(['superArticlesDuplicates' => [
720
            [
721
                'superArticlesDuplicates' => 1,
722
                'notes' => 'my first article note updated',
723
                'id' => 1,
724
            ],
725
            [
726
                'superArticlesDuplicates' => 1,
727
                'notes' => 'my second article note updated',
728
                'id' => 2,
729
            ],
730
            [
731
                'superArticlesDuplicates' => 2,
732
                'notes' => 'my first article2 note updated',
733
                'id' => 3,
734
            ],
735
        ],
736
        ], false, true);
737
738
        $entry = $this->crudPanel->update($entry->id, $inputData);
739
        $relationField = $this->crudPanel->getUpdateFields($entry->id)['superArticlesDuplicates'];
740
        $this->assertCount(3, $relationField['value']);
741
742
        $entry = $entry->fresh();
743
744
        $this->assertCount(3, $entry->superArticlesDuplicates);
745
        $this->assertEquals('my first article note updated', $entry->superArticles[0]->pivot->notes);
746
        $this->assertEquals('my second article note updated', $entry->superArticles[1]->pivot->notes);
747
        $this->assertEquals('my first article2 note updated', $entry->superArticles[2]->pivot->notes);
748
    }
749
750
    public function testBelongsToManyAlwaysSaveSinglePivotWhenMultipleNotAllowed()
751
    {
752
        $inputData = $this->getPivotInputData(['superArticlesDuplicates' => [
753
            [
754
                'superArticlesDuplicates' => 1,
755
                'notes' => 'my first article note',
756
                'id' => null,
757
            ],
758
            [
759
                'superArticlesDuplicates' => 1,
760
                'notes' => 'my second article note',
761
                'id' => null,
762
            ],
763
            [
764
                'superArticlesDuplicates' => 2,
765
                'notes' => 'my first article2 note',
766
                'id' => null,
767
            ],
768
        ],
769
        ]);
770
771
        $entry = $this->crudPanel->create($inputData);
772
        $relationField = $this->crudPanel->getUpdateFields($entry->id)['superArticlesDuplicates'];
773
774
        $this->assertCount(2, $relationField['value']);
775
776
        $entry = $entry->fresh();
777
778
        $this->assertCount(2, $entry->superArticlesDuplicates);
779
        $this->assertEquals('my second article note', $entry->superArticles[0]->pivot->notes);
780
        $this->assertEquals('my first article2 note', $entry->superArticles[1]->pivot->notes);
781
    }
782
783
    public function testBelongsToManyDeletesPivotData()
784
    {
785
        $inputData = $this->getPivotInputData(['superArticlesDuplicates' => [
786
            [
787
                'superArticlesDuplicates' => 1,
788
                'notes' => 'my first article note',
789
                'id' => null,
790
            ],
791
            [
792
                'superArticlesDuplicates' => 1,
793
                'notes' => 'my second article note',
794
                'id' => null,
795
            ],
796
            [
797
                'superArticlesDuplicates' => 2,
798
                'notes' => 'my first article2 note',
799
                'id' => null,
800
            ],
801
        ],
802
        ], true, true);
803
804
        $entry = $this->crudPanel->create($inputData);
805
        $relationField = $this->crudPanel->getUpdateFields($entry->id)['superArticlesDuplicates'];
806
807
        $this->assertCount(3, $relationField['value']);
808
809
        $inputData = $this->getPivotInputData(['superArticlesDuplicates' => [
810
            [
811
                'superArticlesDuplicates' => 1,
812
                'notes' => 'new first article note',
813
                'id' => null,
814
            ],
815
            [
816
                'superArticlesDuplicates' => 1,
817
                'notes' => 'my second article note updated',
818
                'id' => 2,
819
            ],
820
            [
821
                'superArticlesDuplicates' => 3,
822
                'notes' => 'my first article2 note updated',
823
                'id' => 3,
824
            ],
825
        ],
826
        ], false, true);
827
828
        $entry = $this->crudPanel->update($entry->id, $inputData);
829
        $relationField = $this->crudPanel->getUpdateFields($entry->id)['superArticlesDuplicates'];
830
        $this->assertCount(3, $relationField['value']);
831
832
        $entry = $entry->fresh();
833
834
        $this->assertCount(3, $entry->superArticlesDuplicates);
835
        $this->assertEquals('new first article note', $entry->superArticles[2]->pivot->notes);
836
        $this->assertEquals('my second article note updated', $entry->superArticles[0]->pivot->notes);
837
        $this->assertEquals('my first article2 note updated', $entry->superArticles[1]->pivot->notes);
838
    }
839
840
    public function testCreateHasOneWithNestedRelationsRepeatableInterface()
841
    {
842
        $this->crudPanel->setModel(User::class);
843
        $this->crudPanel->setOperation('create');
844
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships);
845
        $this->crudPanel->addField(
846
            [
847
                'name' => 'accountDetails',
848
                'subfields' => [
849
                    [
850
                        'name' => 'nickname',
851
                    ],
852
                    [
853
                        'name' => 'profile_picture',
854
                    ],
855
                    [
856
                        'name' => 'article',
857
                    ],
858
                    [
859
                        'name' => 'addresses',
860
                        'subfields' => [
861
                            [
862
                                'name' => 'bang',
863
                            ],
864
                            [
865
                                'name' => 'street',
866
                            ],
867
                            [
868
                                'name' => 'number',
869
                            ],
870
                        ],
871
                    ],
872
                    [
873
                        'name' => 'bangs',
874
                    ],
875
                    [
876
                        'name' => 'bangsPivot',
877
                        'subfields' => [
878
                            [
879
                                'name' => 'pivot_field',
880
                            ],
881
                        ],
882
                    ],
883
                ],
884
            ]);
885
886
        $faker = Factory::create();
887
888
        $inputData = [
889
            'name' => $faker->name,
890
            'email' => $faker->safeEmail,
891
            'password' => Hash::make($faker->password()),
892
            'remember_token' => null,
893
            'roles' => [1, 2],
894
            'accountDetails' => [
895
                [
896
                    'nickname' => 'i_have_has_one',
897
                    'profile_picture' => 'ohh my picture 1.jpg',
898
                    'article' => 1,
899
                    'addresses' => [
900
                        [
901
                            'bang' => 1,
902
                            'street' => 'test',
903
                            'number' => 1,
904
                        ],
905
                        [
906
                            'bang' => 1,
907
                            'street' => 'test2',
908
                            'number' => 2,
909
                        ],
910
                    ],
911
                    'bangs' => [1, 2],
912
                    'bangsPivot' => [
913
                        ['bangsPivot' => 1, 'pivot_field' => 'test1'],
914
                        ['bangsPivot' => 2, 'pivot_field' => 'test2'],
915
                    ],
916
                ],
917
            ],
918
        ];
919
920
        $entry = $this->crudPanel->create($inputData);
921
        $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...
922
        $account_details = $entry->accountDetails()->first();
923
924
        $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...elations\HasManyThrough.
Loading history...
Bug introduced by
The property article does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
925
        $this->assertEquals($account_details->addresses->count(), 2);
0 ignored issues
show
Bug introduced by
The method count() does not exist on UnitEnum. ( Ignorable by Annotation )

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

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

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

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

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

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

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

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

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

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

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

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

929
        $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 name does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
930
        $this->assertEquals($account_details->bangsPivot->count(), 2);
0 ignored issues
show
Bug introduced by
The property bangsPivot does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The 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

930
        $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...
931
        $this->assertEquals($account_details->bangsPivot->first()->pivot->pivot_field, 'test1');
0 ignored issues
show
Bug introduced by
The property pivot_field does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
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...gHasThroughRelationship.
Loading history...
Bug introduced by
The property pivot_field does not seem to exist on Illuminate\Database\Eloquent\Relations\Relation.
Loading history...
Bug introduced by
The property pivot 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...elations\HasManyThrough.
Loading history...
932
    }
933
934
    public function testCreateBelongsToFake()
935
    {
936
        $belongsToField = [   // select_grouped
937
            'label' => 'Select_grouped',
938
            'type' => 'select_grouped', //https://github.com/Laravel-Backpack/CRUD/issues/502
939
            'name' => 'bang_relation_field',
940
            'fake' => true,
941
            'entity' => 'bang',
942
            'model' => 'Backpack\CRUD\Tests\config\Models\Bang',
943
            'attribute' => 'title',
944
            'group_by' => 'category', // the relationship to entity you want to use for grouping
945
            'group_by_attribute' => 'name', // the attribute on related model, that you want shown
946
            'group_by_relationship_back' => 'articles', // relationship from related model back to this model
947
            'tab' => 'Selects',
948
            'wrapperAttributes' => ['class' => 'form-group col-md-6'],
949
        ];
950
951
        $this->crudPanel->setModel(User::class);
952
        $this->crudPanel->setOperation('create');
953
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships);
954
        $this->crudPanel->addField($belongsToField);
955
956
        $faker = Factory::create();
957
958
        $inputData = [
959
            'name' => $faker->name,
960
            'email' => $faker->safeEmail,
961
            'password' => Hash::make($faker->password()),
962
            'remember_token' => null,
963
            'bang_relation_field' => 1,
964
        ];
965
966
        $entry = $this->crudPanel->create($inputData);
967
        $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...
968
        $this->crudPanel->entry = $entry->withFakes();
969
        $this->assertEquals($entry->bang_relation_field, 1);
970
    }
971
972
    public function testCreateHasOneWithNestedRelations()
973
    {
974
        $this->crudPanel->setModel(User::class);
975
        $this->crudPanel->setOperation('create');
976
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships);
977
        $this->crudPanel->addFields([
978
            [
979
                'name' => 'accountDetails.nickname',
980
            ],
981
            [
982
                'name' => 'accountDetails.profile_picture',
983
            ],
984
            [
985
                'name' => 'accountDetails.article',
986
            ],
987
            [
988
                'name' => 'accountDetails.addresses',
989
                'subfields' => [
990
                    [
991
                        'name' => 'city',
992
                        'entity' => 'bang',
993
                    ],
994
                    [
995
                        'name' => 'street',
996
                    ],
997
                    [
998
                        'name' => 'number',
999
                    ],
1000
                ],
1001
            ],
1002
            [
1003
                'name' => 'accountDetails.bangs',
1004
            ],
1005
            [
1006
                'name' => 'accountDetails.bangsPivot',
1007
                'subfields' => [
1008
                    [
1009
                        'name' => 'pivot_field',
1010
                    ],
1011
                ],
1012
            ],
1013
        ]);
1014
1015
        $faker = Factory::create();
1016
1017
        $inputData = [
1018
            'name' => $faker->name,
1019
            'email' => $faker->safeEmail,
1020
            'password' => Hash::make($faker->password()),
1021
            'remember_token' => null,
1022
            'roles' => [1, 2],
1023
            'accountDetails' => [
1024
                'nickname' => 'i_have_has_one',
1025
                'profile_picture' => 'ohh my picture 1.jpg',
1026
                'article' => 1,
1027
                'addresses' => [
1028
                    [
1029
                        'city' => 1,
1030
                        'street' => 'test',
1031
                        'number' => 1,
1032
                    ],
1033
                    [
1034
                        'city' => 2,
1035
                        'street' => 'test2',
1036
                        'number' => 2,
1037
                    ],
1038
                ],
1039
                'bangs' => [1, 2],
1040
                'bangsPivot' => [
1041
                    ['bangsPivot' => 1, 'pivot_field' => 'test1'],
1042
                    ['bangsPivot' => 2, 'pivot_field' => 'test2'],
1043
                ],
1044
            ],
1045
        ];
1046
1047
        $entry = $this->crudPanel->create($inputData);
1048
        $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...
1049
        $account_details = $entry->accountDetails()->first();
1050
1051
        $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...elations\HasManyThrough.
Loading history...
Bug introduced by
The property article does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
1052
        $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...
1053
        $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\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property id does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The property bang does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The property id does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
Bug introduced by
The property 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\Eloquent\Relations\Relation.
Loading history...
1054
        $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...
1055
        $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...
1056
        $this->assertEquals($account_details->bangs->first()->name, Bang::find(1)->name);
0 ignored issues
show
Bug introduced by
The property bangs does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
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...gHasThroughRelationship.
Loading history...
Bug introduced by
The property name does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The property bangs does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
1057
        $this->assertEquals($account_details->bangsPivot->count(), 2);
0 ignored issues
show
Bug introduced by
The property bangsPivot does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property bangsPivot does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
1058
        $this->assertEquals($account_details->bangsPivot->first()->pivot->pivot_field, 'test1');
0 ignored issues
show
Bug introduced by
The property pivot_field does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The property pivot_field does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
Bug introduced by
The property pivot does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The property pivot_field does not seem to exist on Illuminate\Database\Eloquent\Relations\Relation.
Loading history...
Bug introduced by
The property pivot_field does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property pivot does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
1059
1060
        // Now test the remove process
1061
1062
        $inputData = [
1063
            'name' => $faker->name,
1064
            'email' => $faker->safeEmail,
1065
            'password' => Hash::make($faker->password()),
1066
            'remember_token' => null,
1067
            'roles' => [1, 2],
1068
            'accountDetails' => [
1069
                'nickname' => 'i_have_has_one',
1070
                'profile_picture' => 'ohh my picture 1.jpg',
1071
                'article' => 1,
1072
                'addresses' => [ // HasOne is tested in other test function
1073
                    [
1074
                        'city' => 2,
1075
                        'street' => 'test',
1076
                        'number' => 1,
1077
                    ],
1078
                    [
1079
                        'city' => 1,
1080
                        'street' => 'test2',
1081
                        'number' => 2,
1082
                    ],
1083
                ],
1084
                'bangs' => [],
1085
                'bangsPivot' => [],
1086
            ],
1087
        ];
1088
1089
        $entry = $this->crudPanel->update($entry->id, $inputData);
1090
        $account_details = $entry->accountDetails()->first();
1091
        $this->assertEquals($account_details->addresses->count(), 2);
1092
        $this->assertEquals($account_details->addresses->first()->bang->id, 2);
1093
        $this->assertEquals($account_details->bangs->count(), 0);
1094
        $this->assertEquals($account_details->bangsPivot->count(), 0);
1095
    }
1096
1097
    public function testCreateHasOneWithNestedRelationAsTheFirstField()
1098
    {
1099
        $this->crudPanel->setModel(User::class);
1100
        $this->crudPanel->setOperation('create');
1101
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships);
1102
        $this->crudPanel->addFields([
1103
            [
1104
                'name' => 'accountDetails.article',
1105
            ],
1106
            [
1107
                'name' => 'accountDetails.nickname',
1108
            ],
1109
            [
1110
                'name' => 'accountDetails.profile_picture',
1111
            ],
1112
        ]);
1113
1114
        $faker = Factory::create();
1115
1116
        $inputData = [
1117
            'name' => $faker->name,
1118
            'email' => $faker->safeEmail,
1119
            'password' => Hash::make($faker->password()),
1120
            'remember_token' => null,
1121
            'roles' => [1, 2],
1122
            'accountDetails' => [
1123
                'article' => 1,
1124
                'nickname' => 'i_have_has_one',
1125
                'profile_picture' => 'ohh my picture 1.jpg',
1126
            ],
1127
        ];
1128
1129
        $entry = $this->crudPanel->create($inputData);
1130
        $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...
1131
        $account_details = $entry->accountDetails()->first();
1132
1133
        $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...elations\HasManyThrough.
Loading history...
Bug introduced by
The property article does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
1134
    }
1135
1136
    public function testMorphOneRelationship()
1137
    {
1138
        $this->crudPanel->setModel(User::class);
1139
        $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

1139
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1140
                          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...
1140
        $this->crudPanel->addField([
1141
            'name' => 'comment.text',
1142
        ], '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

1142
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1143
                          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...
1143
1144
        $faker = Factory::create();
1145
        $inputData = [
1146
            'name' => $faker->name,
1147
            'email' => $faker->safeEmail,
1148
            'password' => Hash::make($faker->password()),
1149
            'remember_token' => null,
1150
            'comment' => [
1151
                'text' => 'some test comment text',
1152
            ],
1153
        ];
1154
1155
        $entry = $this->crudPanel->create($inputData);
1156
        $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...
1157
1158
        $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...elations\HasManyThrough.
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...
1159
1160
        $inputData['comment']['text'] = 'updated comment text';
1161
1162
        $this->crudPanel->update($entry->id, $inputData);
1163
1164
        $this->assertEquals($inputData['comment']['text'], $entry->fresh()->comment->text);
1165
    }
1166
1167
    public function testMorphManyCreatableRelationship()
1168
    {
1169
        $this->crudPanel->setModel(User::class);
1170
        $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

1170
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1171
                          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...
1171
        $this->crudPanel->addField([
1172
            'name' => 'stars',
1173
            'subfields' => [
1174
                [
1175
                    'name' => 'title',
1176
                ],
1177
            ],
1178
        ], '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

1178
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1179
                          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...
1179
1180
        $faker = Factory::create();
1181
        $inputData = [
1182
            'name' => $faker->name,
1183
            'email' => $faker->safeEmail,
1184
            'password' => Hash::make($faker->password()),
1185
            'remember_token' => null,
1186
            'stars' => [
1187
                [
1188
                    'id' => null,
1189
                    'title' => 'this is the star 1 title',
1190
                ],
1191
                [
1192
                    'id' => null,
1193
                    'title' => 'this is the star 2 title',
1194
                ],
1195
            ],
1196
        ];
1197
1198
        $entry = $this->crudPanel->create($inputData);
1199
        $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...
1200
1201
        $this->assertCount(2, $entry->stars);
1202
1203
        $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...
1204
1205
        $inputData['stars'] = [
1206
            [
1207
                'id' => 1,
1208
                'title' => 'only one star with changed title',
1209
            ],
1210
        ];
1211
1212
        $this->crudPanel->update($entry->id, $inputData);
1213
1214
        $this->assertCount(1, $entry->fresh()->stars);
1215
1216
        $this->assertEquals($inputData['stars'][0]['title'], $entry->fresh()->stars->first()->title);
1217
        $this->assertEquals($inputData['stars'][0]['id'], $entry->fresh()->stars->first()->id);
1218
    }
1219
1220
    public function testHasManyCreatableRelationship()
1221
    {
1222
        $this->crudPanel->setModel(User::class);
1223
        $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

1223
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1224
                          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...
1224
        $this->crudPanel->addField([
1225
            'name' => 'universes',
1226
            'subfields' => [
1227
                [
1228
                    'name' => 'title',
1229
                ],
1230
            ],
1231
        ], '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

1231
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1232
                          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...
1232
1233
        $faker = Factory::create();
1234
        $inputData = [
1235
            'name' => $faker->name,
1236
            'email' => $faker->safeEmail,
1237
            'password' => Hash::make($faker->password()),
1238
            'remember_token' => null,
1239
            'universes' => [
1240
                [
1241
                    'id' => null,
1242
                    'title' => 'this is the star 1 title',
1243
                ],
1244
                [
1245
                    'title' => 'this is the star 2 title',
1246
                ],
1247
            ],
1248
        ];
1249
1250
        $entry = $this->crudPanel->create($inputData);
1251
        $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...
1252
1253
        $this->assertCount(2, $entry->universes);
1254
1255
        $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...
1256
1257
        $inputData['universes'] = [
1258
            [
1259
                'id' => 1,
1260
                'title' => 'star 1 with changed title',
1261
            ],
1262
            [
1263
                'id' => 2,
1264
                'title' => 'star 2 with changed title',
1265
            ],
1266
        ];
1267
1268
        $this->crudPanel->update($entry->id, $inputData);
1269
1270
        $universes = $entry->fresh()->universes;
1271
        $this->assertCount(2, $universes);
1272
        $this->assertEquals([1, 2], $universes->pluck('id')->toArray());
1273
1274
        $inputData['universes'] = [
1275
            [
1276
                'id' => 1,
1277
                'title' => 'only one star with changed title',
1278
            ],
1279
        ];
1280
1281
        $this->crudPanel->update($entry->id, $inputData);
1282
1283
        $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

1283
        $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...
1284
        $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...
1285
        $this->assertEquals(1, Universe::all()->count());
1286
1287
        $inputData['universes'] = [
1288
            [
1289
                'id' => null,
1290
                'title' => 'new star 3',
1291
            ],
1292
        ];
1293
1294
        $this->crudPanel->update($entry->id, $inputData);
1295
1296
        $this->assertEquals($inputData['universes'][0]['title'], $entry->fresh()->universes->first()->title);
1297
        $this->assertEquals(3, $entry->fresh()->universes->first()->id);
1298
        $this->assertEquals(1, Universe::all()->count());
1299
1300
        $inputData['universes'] = null;
1301
1302
        $this->crudPanel->update($entry->id, $inputData);
1303
1304
        $this->assertEquals(0, count($entry->fresh()->universes));
1305
        $this->assertEquals(0, Universe::all()->count());
1306
    }
1307
1308
    public function testHasManySelectableRelationshipWithoutForceDelete()
1309
    {
1310
        $this->crudPanel->setModel(User::class);
1311
        $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

1311
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1312
                          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...
1312
        $this->crudPanel->addField([
1313
            'name' => 'planets',
1314
            'force_delete' => false,
1315
            'fallback_id' => false,
1316
        ], '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

1316
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1317
                          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...
1317
1318
        $faker = Factory::create();
1319
        $inputData = [
1320
            'name' => $faker->name,
1321
            'email' => $faker->safeEmail,
1322
            'password' => Hash::make($faker->password()),
1323
            'remember_token' => null,
1324
            'planets' => [1, 2],
1325
        ];
1326
1327
        $entry = $this->crudPanel->create($inputData);
1328
        $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...
1329
1330
        $this->assertCount(2, $entry->planets);
1331
1332
        $inputData['planets'] = [1];
1333
1334
        $this->crudPanel->update($entry->id, $inputData);
1335
1336
        $this->assertCount(1, $entry->fresh()->planets);
1337
1338
        $planets = Planet::all();
1339
1340
        $this->assertCount(2, $planets);
1341
    }
1342
1343
    public function testHasManySelectableRelationshipRemoveAllRelations()
1344
    {
1345
        $this->crudPanel->setModel(User::class);
1346
        $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

1346
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1347
                          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...
1347
        $this->crudPanel->addField([
1348
            'name' => 'planets',
1349
            'force_delete' => false,
1350
            'fallback_id' => false,
1351
        ], '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

1351
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1352
                          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...
1352
1353
        $faker = Factory::create();
1354
        $inputData = [
1355
            'name' => $faker->name,
1356
            'email' => $faker->safeEmail,
1357
            'password' => Hash::make($faker->password()),
1358
            'remember_token' => null,
1359
            'planets' => [1, 2],
1360
        ];
1361
1362
        $entry = $this->crudPanel->create($inputData);
1363
1364
        $this->assertCount(2, $entry->planets);
1365
1366
        $inputData['planets'] = [];
1367
1368
        $this->crudPanel->update($entry->id, $inputData);
1369
1370
        $this->assertCount(0, $entry->fresh()->planets);
1371
1372
        $planets = Planet::all();
1373
1374
        $this->assertCount(2, $planets);
1375
    }
1376
1377
    public function testHasManyWithRelationScoped()
1378
    {
1379
        $this->crudPanel->setModel(User::class);
1380
        $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

1380
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1381
                          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...
1381
        $this->crudPanel->addField([
1382
            'name' => 'incomes',
1383
            'subfields' => [
1384
                [
1385
                    'name' => 'label',
1386
                    'type' => 'text',
1387
                ],
1388
                [
1389
                    'name' => 'type',
1390
                    'type' => 'hidden',
1391
                    'value' => 'income',
1392
                ],
1393
                [
1394
                    'name' => 'amount',
1395
                    'type' => 'number',
1396
                ],
1397
            ],
1398
        ], '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

1398
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1399
                          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...
1399
        $this->crudPanel->addField([
1400
            'name' => 'expenses',
1401
            'subfields' => [
1402
                [
1403
                    'name' => 'label',
1404
                    'type' => 'text',
1405
                ],
1406
                [
1407
                    'name' => 'type',
1408
                    'type' => 'hidden',
1409
                    'value' => 'expense',
1410
                ],
1411
                [
1412
                    'name' => 'amount',
1413
                    'type' => 'number',
1414
                ],
1415
            ],
1416
        ], 'both');
1417
1418
        $faker = Factory::create();
1419
        $inputData = [
1420
            'name' => $faker->name,
1421
            'email' => $faker->safeEmail,
1422
            'password' => Hash::make($faker->password()),
1423
            'remember_token' => null,
1424
            'incomes' => [
1425
                [
1426
                    'label' => $faker->name,
1427
                    'amount' => 33,
1428
                    'type' => 'income',
1429
                ],
1430
                [
1431
                    'label' => $faker->name,
1432
                    'amount' => 22,
1433
                    'type' => 'income',
1434
                ],
1435
            ],
1436
            'expenses' => [
1437
                [
1438
                    'label' => $faker->name,
1439
                    'amount' => 44,
1440
                    'type' => 'expense',
1441
                ],
1442
                [
1443
                    'label' => $faker->name,
1444
                    'amount' => 10,
1445
                    'type' => 'expense',
1446
                ],
1447
            ],
1448
        ];
1449
        $entry = $this->crudPanel->create($inputData);
1450
1451
        $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

1451
        /** @scrutinizer ignore-call */ 
1452
        $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...
1452
        $firstIncome = $entry->incomes->first();
0 ignored issues
show
Unused Code introduced by
The assignment to $firstIncome 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

1452
        /** @scrutinizer ignore-call */ 
1453
        $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...
1453
        $this->assertCount(2, $entry->expenses);
1454
        $this->assertCount(2, $entry->incomes);
1455
        $this->assertEquals(44, $entry->expenses->first()->amount);
1456
        $this->assertEquals(33, $entry->incomes->first()->amount);
1457
1458
        $inputData['incomes'] = [
1459
            [
1460
                'id' => 2,
1461
                'label' => $faker->name,
1462
                'amount' => 222,
1463
                'type' => 'income',
1464
            ],
1465
        ];
1466
        $inputData['expenses'] = [
1467
            [
1468
                'id' => 3,
1469
                'label' => $faker->name,
1470
                'amount' => 44,
1471
                'type' => 'expense',
1472
            ],
1473
            [
1474
                'id' => 4,
1475
                'label' => $faker->name,
1476
                'amount' => 10,
1477
                'type' => 'expense',
1478
            ],
1479
        ];
1480
        $this->crudPanel->update($entry->id, $inputData);
1481
1482
        $freshIncomes = $entry->fresh()->incomes;
1483
        $freshExpenses = $entry->fresh()->expenses;
1484
        $this->assertCount(2, $freshExpenses);
1485
        $this->assertCount(1, $freshIncomes);
1486
        $this->assertEquals(2, $freshIncomes->first()->id);
1487
1488
        $inputData['expenses'] = [];
1489
        $this->crudPanel->update($entry->id, $inputData);
1490
1491
        $freshIncomes = $entry->fresh()->incomes;
1492
        $freshExpenses = $entry->fresh()->expenses;
1493
        $this->assertCount(0, $freshExpenses);
1494
        $this->assertCount(1, $freshIncomes);
1495
    }
1496
1497
    public function testHasManySelectableRelationshipWithFallbackId()
1498
    {
1499
        $this->crudPanel->setModel(User::class);
1500
        $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

1500
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1501
                          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...
1501
        $this->crudPanel->addField([
1502
            'name' => 'planets',
1503
            'fallback_id' => 0,
1504
            'force_delete' => false,
1505
        ], '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

1505
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1506
                          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...
1506
1507
        $faker = Factory::create();
1508
        $inputData = [
1509
            'name' => $faker->name,
1510
            'email' => $faker->safeEmail,
1511
            'password' => Hash::make($faker->password()),
1512
            'remember_token' => null,
1513
            'planets' => [1, 2],
1514
        ];
1515
1516
        $entry = $this->crudPanel->create($inputData);
1517
1518
        $this->assertCount(2, $entry->planets);
1519
1520
        $inputData['planets'] = [2];
1521
1522
        $this->crudPanel->update($entry->id, $inputData);
1523
1524
        $this->assertCount(1, $entry->fresh()->planets);
1525
1526
        $planets = Planet::all();
1527
        $this->assertCount(2, $planets);
1528
        $this->assertEquals(0, $planets->first()->user_id);
1529
    }
1530
1531
    public function testHasManySelectableRelationshipWithForceDelete()
1532
    {
1533
        $this->crudPanel->setModel(User::class);
1534
        $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

1534
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1535
                          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...
1535
        $this->crudPanel->addField([
1536
            'name' => 'planets',
1537
            'force_delete' => true,
1538
            'fallback_id' => false,
1539
        ], '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

1539
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1540
                          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...
1540
1541
        $faker = Factory::create();
1542
        $inputData = [
1543
            'name' => $faker->name,
1544
            'email' => $faker->safeEmail,
1545
            'password' => Hash::make($faker->password()),
1546
            'remember_token' => null,
1547
            'planets' => [1, 2],
1548
        ];
1549
1550
        $entry = $this->crudPanel->create($inputData);
1551
1552
        $this->assertCount(2, $entry->planets);
1553
1554
        $inputData['planets'] = [2];
1555
1556
        $this->crudPanel->update($entry->id, $inputData);
1557
1558
        $this->assertCount(1, $entry->fresh()->planets);
1559
1560
        $planets = Planet::all();
1561
        $this->assertCount(1, $planets);
1562
    }
1563
1564
    public function testHasManySelectableRelationshipNonNullableForeignKeyAndDefaultInDatabase()
1565
    {
1566
        $this->crudPanel->setModel(User::class);
1567
        $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

1567
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1568
                          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...
1568
        $this->crudPanel->addField([
1569
            'name' => 'comets',
1570
            'force_delete' => false,
1571
            'fallback_id' => false,
1572
        ], '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

1572
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1573
                          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...
1573
1574
        $faker = Factory::create();
1575
        $inputData = [
1576
            'name' => $faker->name,
1577
            'email' => $faker->safeEmail,
1578
            'password' => Hash::make($faker->password()),
1579
            'remember_token' => null,
1580
            'comets' => [1, 2],
1581
        ];
1582
1583
        $entry = $this->crudPanel->create($inputData);
1584
1585
        $this->assertCount(2, $entry->comets);
1586
1587
        $inputData['comets'] = [2];
1588
1589
        $this->crudPanel->update($entry->id, $inputData);
1590
1591
        $this->assertCount(1, $entry->fresh()->comets);
1592
1593
        $comets = Comet::all();
1594
        $this->assertCount(2, $comets);
1595
        $this->assertEquals(0, $comets->first()->user_id);
1596
    }
1597
1598
    public function testHasManySelectableRelationshipNonNullable()
1599
    {
1600
        $this->crudPanel->setModel(User::class);
1601
        $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

1601
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1602
                          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...
1602
        $this->crudPanel->addField([
1603
            'name' => 'planetsNonNullable',
1604
            'force_delete' => false,
1605
            'fallback_id' => false,
1606
        ], '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

1606
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1607
                          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...
1607
1608
        $faker = Factory::create();
1609
        $inputData = [
1610
            'name' => $faker->name,
1611
            'email' => $faker->safeEmail,
1612
            'password' => Hash::make($faker->password()),
1613
            'remember_token' => null,
1614
            'planetsNonNullable' => [1, 2],
1615
        ];
1616
1617
        $entry = $this->crudPanel->create($inputData);
1618
1619
        $this->assertCount(2, $entry->planetsNonNullable);
1620
1621
        $inputData['planetsNonNullable'] = null;
1622
1623
        $this->crudPanel->update($entry->id, $inputData);
1624
1625
        $this->assertCount(0, $entry->fresh()->planetsNonNullable);
1626
1627
        $planets = PlanetNonNullable::all();
1628
        $this->assertCount(0, $planets);
1629
    }
1630
1631
    public function testCreateHasManyRelationWithDelimitedNameSubfields()
1632
    {
1633
        $this->crudPanel->setModel(User::class);
1634
        $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

1634
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1635
                          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...
1635
        $this->crudPanel->addField([
1636
            'name' => 'universes',
1637
            'subfields' => [
1638
                [
1639
                    'name' => 'title',
1640
                ],
1641
                [
1642
                    'name' => 'start_date,end_date',
1643
                    'type' => 'date_range',
1644
                ],
1645
            ],
1646
        ], '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

1646
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1647
                          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...
1647
1648
        $faker = Factory::create();
1649
        $inputData = [
1650
            'name' => $faker->name,
1651
            'email' => $faker->safeEmail,
1652
            'password' => Hash::make($faker->password()),
1653
            'remember_token' => null,
1654
            'universes' => [
1655
                [
1656
                    'id' => null,
1657
                    'title' => 'this is the star 1 title',
1658
                    'start_date' => '2021-02-26',
1659
                    'end_date' => '2091-01-26',
1660
                ],
1661
                [
1662
                    'title' => 'this is the star 2 title',
1663
                    'end_date' => '2021-02-26',
1664
                    'start_date' => '2091-01-26',
1665
                ],
1666
            ],
1667
        ];
1668
1669
        $entry = $this->crudPanel->create($inputData);
1670
1671
        $this->assertCount(2, $entry->universes);
1672
1673
        $this->assertEquals($inputData['universes'][0]['start_date'], $entry->universes()->first()->start_date);
0 ignored issues
show
Bug introduced by
The property start_date does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property start_date does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
1674
        $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...
1675
        $this->assertEquals($inputData['universes'][1]['end_date'], $entry->universes()->find(2)->end_date);
1676
        $this->assertEquals($inputData['universes'][1]['start_date'], $entry->universes()->find(2)->start_date);
1677
    }
1678
1679
    public function testCreateHasOneRelationWithDelimitedNameSubfields()
1680
    {
1681
        $this->crudPanel->setModel(User::class);
1682
        $this->crudPanel->setOperation('create');
1683
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships);
1684
        $this->crudPanel->addField(
1685
            [
1686
                'name' => 'accountDetails',
1687
                'subfields' => [
1688
                    [
1689
                        'name' => 'nickname',
1690
                    ],
1691
                    [
1692
                        'name' => 'start_date,end_date',
1693
                    ],
1694
                    [
1695
                        'name' => 'profile_picture',
1696
                    ],
1697
                ],
1698
            ]);
1699
1700
        $faker = Factory::create();
1701
1702
        $inputData = [
1703
            'name' => $faker->name,
1704
            'email' => $faker->safeEmail,
1705
            'password' => Hash::make($faker->password()),
1706
            'remember_token' => null,
1707
            'roles' => [1, 2],
1708
            'accountDetails' => [
1709
                [
1710
                    'nickname' => 'i_have_has_one',
1711
                    'profile_picture' => 'ohh my picture 1.jpg',
1712
                    'start_date' => '2021-02-26',
1713
                    'end_date' => '2091-01-26',
1714
                ],
1715
            ],
1716
        ];
1717
1718
        $entry = $this->crudPanel->create($inputData);
1719
        $account_details = $entry->accountDetails()->first();
1720
1721
        $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...elations\HasManyThrough.
Loading history...
Bug introduced by
The property start_date does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
1722
        $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...
1723
    }
1724
1725
    public function testBelongsToManyWithDelimitedNameSubfields()
1726
    {
1727
        $this->crudPanel->setModel(User::class);
1728
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships);
1729
        $this->crudPanel->addField([
1730
            'name' => 'superArticles',
1731
            'subfields' => [
1732
                [
1733
                    'name' => 'notes',
1734
                ],
1735
                [
1736
                    'name' => 'start_date,end_date',
1737
                ],
1738
            ],
1739
        ]);
1740
1741
        $faker = Factory::create();
1742
        $articleData = [
1743
            'content' => $faker->text(),
1744
            'tags' => $faker->words(3, true),
1745
            'user_id' => 1,
1746
        ];
1747
1748
        $article = Article::create($articleData);
1749
1750
        $inputData = [
1751
            'name' => $faker->name,
1752
            'email' => $faker->safeEmail,
1753
            'password' => Hash::make($faker->password()),
1754
            'remember_token' => null,
1755
            'superArticles' => [
1756
                [
1757
                    '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...
1758
                    'notes' => 'my first article note',
1759
                    'start_date' => '2021-02-26',
1760
                    'end_date' => '2091-01-26',
1761
                ],
1762
            ],
1763
        ];
1764
1765
        $entry = $this->crudPanel->create($inputData);
1766
1767
        $this->assertCount(1, $entry->fresh()->superArticles);
1768
        $superArticle = $entry->fresh()->superArticles->first();
1769
        $this->assertEquals($superArticle->pivot->start_date, '2021-02-26');
1770
        $this->assertEquals($superArticle->pivot->end_date, '2091-01-26');
1771
1772
        $this->crudPanel->getUpdateFields($superArticle->id);
1773
    }
1774
1775
    public function testItCanCreateMorphToFieldsStructure()
1776
    {
1777
        $this->crudPanel->setModel(Star::class);
1778
        $this->crudPanel->addField([
1779
            'name' => 'starable',
1780
            'morphOptions' => [
1781
                ['Backpack\CRUD\Tests\config\Models\User', 'User'],
1782
            ],
1783
        ]);
1784
1785
        $this->assertCount(2, $this->crudPanel->fields()['starable']['subfields']);
1786
1787
        [$morphTypeField, $morphIdField] = $this->crudPanel->fields()['starable']['subfields'];
1788
1789
        $this->assertTrue($morphTypeField['name'] === 'starable_type');
1790
        $this->assertTrue($morphIdField['name'] === 'starable_id');
1791
    }
1792
1793
    public function testIPreventsAddingRepeateadMorphOptions()
1794
    {
1795
        $this->crudPanel->setModel(Star::class);
1796
        $this->expectException(\Exception::class);
1797
1798
        $this->crudPanel->addField([
1799
            'name' => 'starable',
1800
            'morphOptions' => [
1801
                ['Backpack\CRUD\Tests\config\Models\User', 'User'],
1802
                ['Backpack\CRUD\Tests\config\Models\User', 'User'],
1803
            ],
1804
        ]);
1805
    }
1806
1807
    public function testItThrowsErrorIfStringIsNotOnMorphMap()
1808
    {
1809
        $this->crudPanel->setModel(Star::class);
1810
        $this->expectException(\Exception::class);
1811
1812
        $this->crudPanel->addField([
1813
            'name' => 'starable',
1814
            'morphOptions' => [
1815
                ['somethingThatDoesNotExist'],
1816
            ],
1817
        ]);
1818
    }
1819
1820
    public function testItCanAddTheOptionsFromTheMorphMap()
1821
    {
1822
        $this->crudPanel->setModel(Star::class);
1823
1824
        Relation::morphMap([
1825
            'user' => 'Backpack\CRUD\Tests\config\Models\User',
1826
        ]);
1827
1828
        $this->crudPanel->addField([
1829
            'name' => 'starable',
1830
            'morphOptions' => [
1831
                ['user'],
1832
            ],
1833
        ]);
1834
1835
        [$morphTypeField, $morphIdField] = $this->crudPanel->fields()['starable']['subfields'];
1836
        $this->assertEquals(['user' => 'Backpack\CRUD\Tests\config\Models\User'], $morphTypeField['morphMap']);
1837
    }
1838
1839
    public function testItThrowsErrorIfDuplicateMorphMapName()
1840
    {
1841
        $this->crudPanel->setModel(Star::class);
1842
        $this->expectException(\Exception::class);
1843
1844
        Relation::morphMap([
1845
            'user' => 'Backpack\CRUD\Tests\config\Models\User',
1846
        ]);
1847
1848
        $this->crudPanel->addField([
1849
            'name' => 'starable',
1850
            'morphOptions' => [
1851
                ['user'],
1852
                ['user'],
1853
            ],
1854
        ]);
1855
    }
1856
1857
    public function testItCanRegisterModelEventsInTheFields()
1858
    {
1859
        $this->crudPanel->setModel(User::class);
1860
1861
        $this->crudPanel->addField([
1862
            'name' => 'name',
1863
            'events' => [
1864
                'created' => function ($entry) {
0 ignored issues
show
Unused Code introduced by
The parameter $entry is not used and could be removed. ( Ignorable by Annotation )

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

1864
                'created' => function (/** @scrutinizer ignore-unused */ $entry) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1865
                },
1866
                'creating' => function ($entry) {
1867
                    $entry->email = '[email protected]';
1868
                    $entry->password = Hash::make('password');
1869
                },
1870
                'saving' => function ($entry) {
0 ignored issues
show
Unused Code introduced by
The parameter $entry is not used and could be removed. ( Ignorable by Annotation )

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

1870
                'saving' => function (/** @scrutinizer ignore-unused */ $entry) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1871
                },
1872
                'saved' => function ($entry) {
0 ignored issues
show
Unused Code introduced by
The parameter $entry is not used and could be removed. ( Ignorable by Annotation )

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

1872
                'saved' => function (/** @scrutinizer ignore-unused */ $entry) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1873
                },
1874
                'updating' => function ($entry) {
0 ignored issues
show
Unused Code introduced by
The parameter $entry is not used and could be removed. ( Ignorable by Annotation )

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

1874
                'updating' => function (/** @scrutinizer ignore-unused */ $entry) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1875
                },
1876
                'updated' => function ($entry) {
0 ignored issues
show
Unused Code introduced by
The parameter $entry is not used and could be removed. ( Ignorable by Annotation )

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

1876
                'updated' => function (/** @scrutinizer ignore-unused */ $entry) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1877
                },
1878
                'deleting' => function ($entry) {
0 ignored issues
show
Unused Code introduced by
The parameter $entry is not used and could be removed. ( Ignorable by Annotation )

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

1878
                'deleting' => function (/** @scrutinizer ignore-unused */ $entry) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1879
                },
1880
                'deleted' => function ($entry) {
0 ignored issues
show
Unused Code introduced by
The parameter $entry is not used and could be removed. ( Ignorable by Annotation )

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

1880
                'deleted' => function (/** @scrutinizer ignore-unused */ $entry) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1881
                },
1882
            ],
1883
        ]);
1884
1885
        $this->crudPanel->registerFieldEvents();
1886
1887
        $this->assertNotEmpty($this->crudPanel->getModel()->getEventDispatcher()->getListeners('eloquent.created: Backpack\CRUD\Tests\Config\Models\User'));
1888
        $this->assertNotEmpty($this->crudPanel->getModel()->getEventDispatcher()->getListeners('eloquent.creating: Backpack\CRUD\Tests\Config\Models\User'));
1889
        $this->assertNotEmpty($this->crudPanel->getModel()->getEventDispatcher()->getListeners('eloquent.saving: Backpack\CRUD\Tests\Config\Models\User'));
1890
        $this->assertNotEmpty($this->crudPanel->getModel()->getEventDispatcher()->getListeners('eloquent.saved: Backpack\CRUD\Tests\Config\Models\User'));
1891
        $this->assertNotEmpty($this->crudPanel->getModel()->getEventDispatcher()->getListeners('eloquent.updating: Backpack\CRUD\Tests\Config\Models\User'));
1892
        $this->assertNotEmpty($this->crudPanel->getModel()->getEventDispatcher()->getListeners('eloquent.updated: Backpack\CRUD\Tests\Config\Models\User'));
1893
        $this->assertNotEmpty($this->crudPanel->getModel()->getEventDispatcher()->getListeners('eloquent.deleting: Backpack\CRUD\Tests\Config\Models\User'));
1894
        $this->assertNotEmpty($this->crudPanel->getModel()->getEventDispatcher()->getListeners('eloquent.deleted: Backpack\CRUD\Tests\Config\Models\User'));
1895
1896
        $this->crudPanel->getModel()->create(['name' => 'test']);
1897
1898
        $this->assertEquals('[email protected]', User::latest('id')->first()->email);
0 ignored issues
show
Bug introduced by
The property email does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property email does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
1899
    }
1900
1901
    public function testItCanCreateDynamicRelationships()
1902
    {
1903
        User::resolveRelationUsing('dynamicRelation', function ($user) {
1904
            return $user->belongsTo(\Backpack\CRUD\Tests\config\Models\Bang::class, 'bang_relation_field');
1905
        });
1906
1907
        $this->crudPanel->setModel(User::class);
1908
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships);
1909
        $this->crudPanel->addField([
1910
            'name' => 'dynamicRelation',
1911
        ]);
1912
1913
        $faker = Factory::create();
1914
1915
        $inputData = [
1916
            'name' => $faker->name,
1917
            'email' => $faker->safeEmail,
1918
            'password' => Hash::make($faker->password()),
1919
            'remember_token' => null,
1920
            'dynamicRelation' => 1,
1921
        ];
1922
1923
        $entry = $this->crudPanel->create($inputData);
1924
1925
        $this->assertEquals($entry->dynamicRelation()->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...gHasThroughRelationship.
Loading history...
Bug introduced by
The property name does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
1926
    }
1927
1928
    private function getPivotInputData(array $pivotRelationData, bool $initCrud = true, bool $allowDuplicates = false)
1929
    {
1930
        $faker = Factory::create();
1931
1932
        if ($initCrud) {
1933
            $this->crudPanel->setModel(User::class);
1934
            $this->crudPanel->addFields($this->userInputFieldsNoRelationships);
1935
            $this->crudPanel->addField([
1936
                'name' => array_key_first($pivotRelationData),
1937
                'allow_duplicate_pivots' => $allowDuplicates,
1938
                'pivot_key_name' => 'id',
1939
                'subfields' => [
1940
                    [
1941
                        'name' => 'notes',
1942
                    ],
1943
1944
                ],
1945
            ]);
1946
1947
            $article = Article::create([
0 ignored issues
show
Unused Code introduced by
The assignment to $article is dead and can be removed.
Loading history...
1948
                'content' => $faker->text(),
1949
                'tags' => $faker->words(3, true),
1950
                'user_id' => 1,
1951
            ]);
1952
            $article2 = Article::create([
0 ignored issues
show
Unused Code introduced by
The assignment to $article2 is dead and can be removed.
Loading history...
1953
                'content' => $faker->text(),
1954
                'tags' => $faker->words(3, true),
1955
                'user_id' => 1,
1956
            ]);
1957
        }
1958
1959
        $inputData = [
1960
            'name' => $faker->name,
1961
            'email' => $faker->safeEmail,
1962
            'password' => Hash::make($faker->password()),
1963
            'remember_token' => null,
1964
        ];
1965
1966
        return array_merge($inputData, $pivotRelationData);
1967
    }
1968
}
1969