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

testCreateWithOneToManyDynamicRelationship()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 42
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 28
nc 1
nop 0
dl 0
loc 42
rs 9.472
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 Illuminate\Pagination\LengthAwarePaginator or Illuminate\Http\Resources\Json\ResourceCollection or Nette\Utils\Html or Illuminate\Pagination\CursorPaginator or Carbon\CarbonPeriod or Illuminate\Pagination\Paginator or Nette\Iterators\CachingIterator or Nette\Utils\ArrayList or Illuminate\Support\ViewErrorBag or Illuminate\Support\Enumerable or Prologue\Alerts\AlertsMessageBag or Nette\Iterators\CachingIterator. ( Ignorable by Annotation )

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

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 Illuminate\Pagination\LengthAwarePaginator or Illuminate\Http\Resources\Json\ResourceCollection or Nette\Utils\Html or League\CommonMark\Util\ArrayCollection or Illuminate\Pagination\CursorPaginator or Illuminate\Contracts\Support\MessageBag or Carbon\CarbonPeriod or Illuminate\Pagination\Paginator or Nette\Iterators\CachingIterator or Nette\Utils\ArrayList or Illuminate\Support\ViewErrorBag or Illuminate\Support\Enumerable or Ramsey\Collection\CollectionInterface or Ramsey\Collection\AbstractCollection or Ds\Map or Ds\Set or Ds\Sequence or Nette\Iterators\CachingIterator. ( Ignorable by Annotation )

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

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 testBelongsToManyWithPivotDataDynamicRelationship()
687
    {
688
        User::resolveRelationUsing('dynamicRelation', function ($user) {
689
            return $user->belongsToMany(\Backpack\CRUD\Tests\config\Models\Article::class, 'articles_user')->withPivot(['notes', 'start_date', 'end_date']);
690
        });
691
692
        $this->crudPanel->setModel(User::class);
693
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships);
694
        $this->crudPanel->addField([
695
            'name' => 'dynamicRelation',
696
            'subfields' => [
697
                [
698
                    'name' => 'notes',
699
                ],
700
            ],
701
        ]);
702
703
        $faker = Factory::create();
704
        $articleData = [
705
            'content' => $faker->text(),
706
            'tags' => $faker->words(3, true),
707
            'user_id' => 1,
708
        ];
709
710
        $article = Article::create($articleData);
711
712
        $inputData = [
713
            'name' => $faker->name,
714
            'email' => $faker->safeEmail,
715
            'password' => Hash::make($faker->password()),
716
            'remember_token' => null,
717
            'dynamicRelation' => [
718
                [
719
                    'dynamicRelation' => $article->id,
0 ignored issues
show
Bug introduced by
The property id does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
720
                    'notes' => 'my first article note',
721
                ],
722
            ],
723
        ];
724
725
        $entry = $this->crudPanel->create($inputData);
726
727
        $this->assertCount(1, $entry->fresh()->dynamicRelation);
728
        $this->assertEquals('my first article note', $entry->fresh()->dynamicRelation->first()->pivot->notes);
729
    }
730
731
    public function testBelongsToManyWithMultipleSameRelationIdAndPivotDataRelationship()
732
    {
733
        $inputData = $this->getPivotInputData(['superArticlesDuplicates' => [
734
            [
735
                'superArticlesDuplicates' => 1,
736
                'notes' => 'my first article note',
737
                'id' => null,
738
            ],
739
            [
740
                'superArticlesDuplicates' => 1,
741
                'notes' => 'my second article note',
742
                'id' => null,
743
            ],
744
            [
745
                'superArticlesDuplicates' => 2,
746
                'notes' => 'my first article2 note',
747
                'id' => null,
748
            ],
749
        ],
750
        ], true, true);
751
752
        $entry = $this->crudPanel->create($inputData);
753
        $relationField = $this->crudPanel->getUpdateFields($entry->id)['superArticlesDuplicates'];
754
755
        $this->assertCount(3, $relationField['value']);
756
757
        $entry = $entry->fresh();
758
759
        $this->assertCount(3, $entry->superArticlesDuplicates);
760
        $this->assertEquals('my first article note', $entry->superArticlesDuplicates->first()->pivot->notes);
761
        $this->assertEquals('my second article note', $entry->superArticlesDuplicates[1]->pivot->notes);
762
        $this->assertEquals('my first article2 note', $entry->superArticlesDuplicates[2]->pivot->notes);
763
764
        $inputData = $this->getPivotInputData(['superArticlesDuplicates' => [
765
            [
766
                'superArticlesDuplicates' => 1,
767
                'notes' => 'my first article note updated',
768
                'id' => 1,
769
            ],
770
            [
771
                'superArticlesDuplicates' => 1,
772
                'notes' => 'my second article note updated',
773
                'id' => 2,
774
            ],
775
            [
776
                'superArticlesDuplicates' => 2,
777
                'notes' => 'my first article2 note updated',
778
                'id' => 3,
779
            ],
780
        ],
781
        ], false, true);
782
783
        $entry = $this->crudPanel->update($entry->id, $inputData);
784
        $relationField = $this->crudPanel->getUpdateFields($entry->id)['superArticlesDuplicates'];
785
        $this->assertCount(3, $relationField['value']);
786
787
        $entry = $entry->fresh();
788
789
        $this->assertCount(3, $entry->superArticlesDuplicates);
790
        $this->assertEquals('my first article note updated', $entry->superArticlesDuplicates[0]->pivot->notes);
791
        $this->assertEquals('my second article note updated', $entry->superArticlesDuplicates[1]->pivot->notes);
792
        $this->assertEquals('my first article2 note updated', $entry->superArticlesDuplicates[2]->pivot->notes);
793
    }
794
795
    public function testBelongsToManyWithMultipleSameRelationIdAndPivotDataDynamicRelationship()
796
    {
797
        User::resolveRelationUsing('dynamicRelation', function ($user) {
798
            return $user->belongsToMany(\Backpack\CRUD\Tests\config\Models\Article::class, 'articles_user')->withPivot(['notes', 'start_date', 'end_date', 'id'])->using('Backpack\CRUD\Tests\config\Models\SuperArticlePivot');
799
        });
800
801
        $inputData = $this->getPivotInputData(['dynamicRelation' => [
802
            [
803
                'dynamicRelation' => 1,
804
                'notes' => 'my first article note',
805
                'id' => null,
806
            ],
807
            [
808
                'dynamicRelation' => 1,
809
                'notes' => 'my second article note',
810
                'id' => null,
811
            ],
812
            [
813
                'dynamicRelation' => 2,
814
                'notes' => 'my first article2 note',
815
                'id' => null,
816
            ],
817
        ],
818
        ], true, true);
819
820
        $entry = $this->crudPanel->create($inputData);
821
        $relationField = $this->crudPanel->getUpdateFields($entry->id)['dynamicRelation'];
822
823
        $this->assertCount(3, $relationField['value']);
824
825
        $entry = $entry->fresh();
826
827
        $this->assertCount(3, $entry->dynamicRelation);
828
        $this->assertEquals('my first article note', $entry->dynamicRelation->first()->pivot->notes);
829
        $this->assertEquals('my second article note', $entry->dynamicRelation[1]->pivot->notes);
830
        $this->assertEquals('my first article2 note', $entry->dynamicRelation[2]->pivot->notes);
831
832
        $inputData = $this->getPivotInputData(['dynamicRelation' => [
833
            [
834
                'dynamicRelation' => 1,
835
                'notes' => 'my first article note updated',
836
                'id' => 1,
837
            ],
838
            [
839
                'dynamicRelation' => 1,
840
                'notes' => 'my second article note updated',
841
                'id' => 2,
842
            ],
843
            [
844
                'dynamicRelation' => 2,
845
                'notes' => 'my first article2 note updated',
846
                'id' => 3,
847
            ],
848
        ],
849
        ], false, true);
850
851
        $entry = $this->crudPanel->update($entry->id, $inputData);
852
        $relationField = $this->crudPanel->getUpdateFields($entry->id)['dynamicRelation'];
853
        $this->assertCount(3, $relationField['value']);
854
855
        $entry = $entry->fresh();
856
857
        $this->assertCount(3, $entry->dynamicRelation);
858
        $this->assertEquals('my first article note updated', $entry->dynamicRelation[0]->pivot->notes);
859
        $this->assertEquals('my second article note updated', $entry->dynamicRelation[1]->pivot->notes);
860
        $this->assertEquals('my first article2 note updated', $entry->dynamicRelation[2]->pivot->notes);
861
    }
862
863
    public function testBelongsToManyAlwaysSaveSinglePivotWhenMultipleNotAllowed()
864
    {
865
        $inputData = $this->getPivotInputData(['superArticlesDuplicates' => [
866
            [
867
                'superArticlesDuplicates' => 1,
868
                'notes' => 'my first article note',
869
                'id' => null,
870
            ],
871
            [
872
                'superArticlesDuplicates' => 1,
873
                'notes' => 'my second article note',
874
                'id' => null,
875
            ],
876
            [
877
                'superArticlesDuplicates' => 2,
878
                'notes' => 'my first article2 note',
879
                'id' => null,
880
            ],
881
        ],
882
        ]);
883
884
        $entry = $this->crudPanel->create($inputData);
885
        $relationField = $this->crudPanel->getUpdateFields($entry->id)['superArticlesDuplicates'];
886
887
        $this->assertCount(2, $relationField['value']);
888
889
        $entry = $entry->fresh();
890
891
        $this->assertCount(2, $entry->superArticlesDuplicates);
892
        $this->assertEquals('my second article note', $entry->superArticles[0]->pivot->notes);
893
        $this->assertEquals('my first article2 note', $entry->superArticles[1]->pivot->notes);
894
    }
895
896
    public function testBelongsToManyDeletesPivotData()
897
    {
898
        $inputData = $this->getPivotInputData(['superArticlesDuplicates' => [
899
            [
900
                'superArticlesDuplicates' => 1,
901
                'notes' => 'my first article note',
902
                'id' => null,
903
            ],
904
            [
905
                'superArticlesDuplicates' => 1,
906
                'notes' => 'my second article note',
907
                'id' => null,
908
            ],
909
            [
910
                'superArticlesDuplicates' => 2,
911
                'notes' => 'my first article2 note',
912
                'id' => null,
913
            ],
914
        ],
915
        ], true, true);
916
917
        $entry = $this->crudPanel->create($inputData);
918
        $relationField = $this->crudPanel->getUpdateFields($entry->id)['superArticlesDuplicates'];
919
920
        $this->assertCount(3, $relationField['value']);
921
922
        $inputData = $this->getPivotInputData(['superArticlesDuplicates' => [
923
            [
924
                'superArticlesDuplicates' => 1,
925
                'notes' => 'new first article note',
926
                'id' => null,
927
            ],
928
            [
929
                'superArticlesDuplicates' => 1,
930
                'notes' => 'my second article note updated',
931
                'id' => 2,
932
            ],
933
            [
934
                'superArticlesDuplicates' => 3,
935
                'notes' => 'my first article2 note updated',
936
                'id' => 3,
937
            ],
938
        ],
939
        ], false, true);
940
941
        $entry = $this->crudPanel->update($entry->id, $inputData);
942
        $relationField = $this->crudPanel->getUpdateFields($entry->id)['superArticlesDuplicates'];
943
        $this->assertCount(3, $relationField['value']);
944
945
        $entry = $entry->fresh();
946
947
        $this->assertCount(3, $entry->superArticlesDuplicates);
948
        $this->assertEquals('new first article note', $entry->superArticles[2]->pivot->notes);
949
        $this->assertEquals('my second article note updated', $entry->superArticles[0]->pivot->notes);
950
        $this->assertEquals('my first article2 note updated', $entry->superArticles[1]->pivot->notes);
951
    }
952
953
    public function testCreateHasOneWithNestedRelationsRepeatableInterface()
954
    {
955
        $this->crudPanel->setModel(User::class);
956
        $this->crudPanel->setOperation('create');
957
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships);
958
        $this->crudPanel->addField(
959
            [
960
                'name' => 'accountDetails',
961
                'subfields' => [
962
                    [
963
                        'name' => 'nickname',
964
                    ],
965
                    [
966
                        'name' => 'profile_picture',
967
                    ],
968
                    [
969
                        'name' => 'article',
970
                    ],
971
                    [
972
                        'name' => 'addresses',
973
                        'subfields' => [
974
                            [
975
                                'name' => 'bang',
976
                            ],
977
                            [
978
                                'name' => 'street',
979
                            ],
980
                            [
981
                                'name' => 'number',
982
                            ],
983
                        ],
984
                    ],
985
                    [
986
                        'name' => 'bangs',
987
                    ],
988
                    [
989
                        'name' => 'bangsPivot',
990
                        'subfields' => [
991
                            [
992
                                'name' => 'pivot_field',
993
                            ],
994
                        ],
995
                    ],
996
                ],
997
            ]);
998
999
        $faker = Factory::create();
1000
1001
        $inputData = [
1002
            'name' => $faker->name,
1003
            'email' => $faker->safeEmail,
1004
            'password' => Hash::make($faker->password()),
1005
            'remember_token' => null,
1006
            'roles' => [1, 2],
1007
            'accountDetails' => [
1008
                [
1009
                    'nickname' => 'i_have_has_one',
1010
                    'profile_picture' => 'ohh my picture 1.jpg',
1011
                    'article' => 1,
1012
                    'addresses' => [
1013
                        [
1014
                            'bang' => 1,
1015
                            'street' => 'test',
1016
                            'number' => 1,
1017
                        ],
1018
                        [
1019
                            'bang' => 1,
1020
                            'street' => 'test2',
1021
                            'number' => 2,
1022
                        ],
1023
                    ],
1024
                    'bangs' => [1, 2],
1025
                    'bangsPivot' => [
1026
                        ['bangsPivot' => 1, 'pivot_field' => 'test1'],
1027
                        ['bangsPivot' => 2, 'pivot_field' => 'test2'],
1028
                    ],
1029
                ],
1030
            ],
1031
        ];
1032
1033
        $entry = $this->crudPanel->create($inputData);
1034
        $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...
1035
        $account_details = $entry->accountDetails()->first();
1036
1037
        $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...
1038
        $this->assertEquals($account_details->addresses->count(), 2);
0 ignored issues
show
Bug introduced by
The property addresses does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property addresses does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
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

1038
        $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...
1039
        $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...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property city does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
1040
        $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...
1041
        $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...
1042
        $this->assertEquals($account_details->bangs->first()->name, Bang::find(1)->name);
0 ignored issues
show
Bug introduced by
The method first() does not exist on null. ( Ignorable by Annotation )

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

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

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

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

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

1043
        $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...
1044
        $this->assertEquals($account_details->bangsPivot->first()->pivot->pivot_field, 'test1');
0 ignored issues
show
Bug introduced by
The property pivot_field does not seem to exist on Illuminate\Database\Eloquent\Relations\Relation.
Loading history...
Bug introduced by
The property pivot does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The property pivot_field does not seem to exist on Illuminate\Database\Eloq...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\Eloq...elations\HasManyThrough.
Loading history...
1045
    }
1046
1047
    public function testCreateBelongsToFake()
1048
    {
1049
        $belongsToField = [   // select_grouped
1050
            'label' => 'Select_grouped',
1051
            'type' => 'select_grouped', //https://github.com/Laravel-Backpack/CRUD/issues/502
1052
            'name' => 'bang_relation_field',
1053
            'fake' => true,
1054
            'entity' => 'bang',
1055
            'model' => 'Backpack\CRUD\Tests\config\Models\Bang',
1056
            'attribute' => 'title',
1057
            'group_by' => 'category', // the relationship to entity you want to use for grouping
1058
            'group_by_attribute' => 'name', // the attribute on related model, that you want shown
1059
            'group_by_relationship_back' => 'articles', // relationship from related model back to this model
1060
            'tab' => 'Selects',
1061
            'wrapperAttributes' => ['class' => 'form-group col-md-6'],
1062
        ];
1063
1064
        $this->crudPanel->setModel(User::class);
1065
        $this->crudPanel->setOperation('create');
1066
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships);
1067
        $this->crudPanel->addField($belongsToField);
1068
1069
        $faker = Factory::create();
1070
1071
        $inputData = [
1072
            'name' => $faker->name,
1073
            'email' => $faker->safeEmail,
1074
            'password' => Hash::make($faker->password()),
1075
            'remember_token' => null,
1076
            'bang_relation_field' => 1,
1077
        ];
1078
1079
        $entry = $this->crudPanel->create($inputData);
1080
        $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...
1081
        $this->crudPanel->entry = $entry->withFakes();
1082
        $this->assertEquals($entry->bang_relation_field, 1);
1083
    }
1084
1085
    public function testCreateHasOneWithNestedRelations()
1086
    {
1087
        $this->crudPanel->setModel(User::class);
1088
        $this->crudPanel->setOperation('create');
1089
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships);
1090
        $this->crudPanel->addFields([
1091
            [
1092
                'name' => 'accountDetails.nickname',
1093
            ],
1094
            [
1095
                'name' => 'accountDetails.profile_picture',
1096
            ],
1097
            [
1098
                'name' => 'accountDetails.article',
1099
            ],
1100
            [
1101
                'name' => 'accountDetails.addresses',
1102
                'subfields' => [
1103
                    [
1104
                        'name' => 'city',
1105
                        'entity' => 'bang',
1106
                    ],
1107
                    [
1108
                        'name' => 'street',
1109
                    ],
1110
                    [
1111
                        'name' => 'number',
1112
                    ],
1113
                ],
1114
            ],
1115
            [
1116
                'name' => 'accountDetails.bangs',
1117
            ],
1118
            [
1119
                'name' => 'accountDetails.bangsPivot',
1120
                'subfields' => [
1121
                    [
1122
                        'name' => 'pivot_field',
1123
                    ],
1124
                ],
1125
            ],
1126
        ]);
1127
1128
        $faker = Factory::create();
1129
1130
        $inputData = [
1131
            'name' => $faker->name,
1132
            'email' => $faker->safeEmail,
1133
            'password' => Hash::make($faker->password()),
1134
            'remember_token' => null,
1135
            'roles' => [1, 2],
1136
            'accountDetails' => [
1137
                'nickname' => 'i_have_has_one',
1138
                'profile_picture' => 'ohh my picture 1.jpg',
1139
                'article' => 1,
1140
                'addresses' => [
1141
                    [
1142
                        'city' => 1,
1143
                        'street' => 'test',
1144
                        'number' => 1,
1145
                    ],
1146
                    [
1147
                        'city' => 2,
1148
                        'street' => 'test2',
1149
                        'number' => 2,
1150
                    ],
1151
                ],
1152
                'bangs' => [1, 2],
1153
                'bangsPivot' => [
1154
                    ['bangsPivot' => 1, 'pivot_field' => 'test1'],
1155
                    ['bangsPivot' => 2, 'pivot_field' => 'test2'],
1156
                ],
1157
            ],
1158
        ];
1159
1160
        $entry = $this->crudPanel->create($inputData);
1161
        $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...
1162
        $account_details = $entry->accountDetails()->first();
1163
1164
        $this->assertEquals($account_details->article, Article::find(1));
0 ignored issues
show
Bug introduced by
The property article does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property article does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
1165
        $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...
1166
        $this->assertEquals($account_details->addresses->first()->bang->id, 1);
0 ignored issues
show
Bug introduced by
The property bang does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The property id does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The property bang does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property id does not seem to exist on Illuminate\Database\Eloquent\Relations\Relation.
Loading history...
Bug introduced by
The property id does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
Bug introduced by
The property id does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
1167
        $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...
1168
        $this->assertEquals($account_details->addresses->first()->number, 1);
0 ignored issues
show
Bug introduced by
The property number does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property number does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
1169
        $this->assertEquals($account_details->bangs->first()->name, Bang::find(1)->name);
0 ignored issues
show
Bug introduced by
The property name does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The property bangs does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The property bangs does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property name does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
Bug introduced by
The property name does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
1170
        $this->assertEquals($account_details->bangsPivot->count(), 2);
0 ignored issues
show
Bug introduced by
The property bangsPivot does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The property bangsPivot does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
1171
        $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...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_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 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...
1172
1173
        // Now test the remove process
1174
1175
        $inputData = [
1176
            'name' => $faker->name,
1177
            'email' => $faker->safeEmail,
1178
            'password' => Hash::make($faker->password()),
1179
            'remember_token' => null,
1180
            'roles' => [1, 2],
1181
            'accountDetails' => [
1182
                'nickname' => 'i_have_has_one',
1183
                'profile_picture' => 'ohh my picture 1.jpg',
1184
                'article' => 1,
1185
                'addresses' => [ // HasOne is tested in other test function
1186
                    [
1187
                        'city' => 2,
1188
                        'street' => 'test',
1189
                        'number' => 1,
1190
                    ],
1191
                    [
1192
                        'city' => 1,
1193
                        'street' => 'test2',
1194
                        'number' => 2,
1195
                    ],
1196
                ],
1197
                'bangs' => [],
1198
                'bangsPivot' => [],
1199
            ],
1200
        ];
1201
1202
        $entry = $this->crudPanel->update($entry->id, $inputData);
1203
        $account_details = $entry->accountDetails()->first();
1204
        $this->assertEquals($account_details->addresses->count(), 2);
1205
        $this->assertEquals($account_details->addresses->first()->bang->id, 2);
1206
        $this->assertEquals($account_details->bangs->count(), 0);
1207
        $this->assertEquals($account_details->bangsPivot->count(), 0);
1208
    }
1209
1210
    public function testCreateHasOneWithNestedRelationAsTheFirstField()
1211
    {
1212
        $this->crudPanel->setModel(User::class);
1213
        $this->crudPanel->setOperation('create');
1214
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships);
1215
        $this->crudPanel->addFields([
1216
            [
1217
                'name' => 'accountDetails.article',
1218
            ],
1219
            [
1220
                'name' => 'accountDetails.nickname',
1221
            ],
1222
            [
1223
                'name' => 'accountDetails.profile_picture',
1224
            ],
1225
        ]);
1226
1227
        $faker = Factory::create();
1228
1229
        $inputData = [
1230
            'name' => $faker->name,
1231
            'email' => $faker->safeEmail,
1232
            'password' => Hash::make($faker->password()),
1233
            'remember_token' => null,
1234
            'roles' => [1, 2],
1235
            'accountDetails' => [
1236
                'article' => 1,
1237
                'nickname' => 'i_have_has_one',
1238
                'profile_picture' => 'ohh my picture 1.jpg',
1239
            ],
1240
        ];
1241
1242
        $entry = $this->crudPanel->create($inputData);
1243
        $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...
1244
        $account_details = $entry->accountDetails()->first();
1245
1246
        $this->assertEquals($account_details->article, Article::find(1));
0 ignored issues
show
Bug introduced by
The property article does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property article does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
1247
    }
1248
1249
    public function testMorphOneRelationship()
1250
    {
1251
        $this->crudPanel->setModel(User::class);
1252
        $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

1252
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1253
                          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...
1253
        $this->crudPanel->addField([
1254
            'name' => 'comment.text',
1255
        ], '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

1255
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1256
                          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...
1256
1257
        $faker = Factory::create();
1258
        $inputData = [
1259
            'name' => $faker->name,
1260
            'email' => $faker->safeEmail,
1261
            'password' => Hash::make($faker->password()),
1262
            'remember_token' => null,
1263
            'comment' => [
1264
                'text' => 'some test comment text',
1265
            ],
1266
        ];
1267
1268
        $entry = $this->crudPanel->create($inputData);
1269
        $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...
1270
1271
        $this->assertEquals($inputData['comment']['text'], $entry->comment->text);
0 ignored issues
show
Bug introduced by
The property text does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property text does not seem to exist on Illuminate\Database\Eloquent\Relations\Relation.
Loading history...
Bug introduced by
The property text does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
Bug introduced by
The property text does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
1272
1273
        $inputData['comment']['text'] = 'updated comment text';
1274
1275
        $this->crudPanel->update($entry->id, $inputData);
1276
1277
        $this->assertEquals($inputData['comment']['text'], $entry->fresh()->comment->text);
1278
    }
1279
1280
    public function testMorphManyCreatableRelationship()
1281
    {
1282
        $this->crudPanel->setModel(User::class);
1283
        $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

1283
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1284
                          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...
1284
        $this->crudPanel->addField([
1285
            'name' => 'stars',
1286
            'subfields' => [
1287
                [
1288
                    'name' => 'title',
1289
                ],
1290
            ],
1291
        ], '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

1291
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1292
                          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...
1292
1293
        $faker = Factory::create();
1294
        $inputData = [
1295
            'name' => $faker->name,
1296
            'email' => $faker->safeEmail,
1297
            'password' => Hash::make($faker->password()),
1298
            'remember_token' => null,
1299
            'stars' => [
1300
                [
1301
                    'id' => null,
1302
                    'title' => 'this is the star 1 title',
1303
                ],
1304
                [
1305
                    'id' => null,
1306
                    'title' => 'this is the star 2 title',
1307
                ],
1308
            ],
1309
        ];
1310
1311
        $entry = $this->crudPanel->create($inputData);
1312
        $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...
1313
1314
        $this->assertCount(2, $entry->stars);
1315
1316
        $this->assertEquals($inputData['stars'][0]['title'], $entry->stars()->first()->title);
0 ignored issues
show
Bug introduced by
The property title does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The property title does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
1317
1318
        $inputData['stars'] = [
1319
            [
1320
                'id' => 1,
1321
                'title' => 'only one star with changed title',
1322
            ],
1323
        ];
1324
1325
        $this->crudPanel->update($entry->id, $inputData);
1326
1327
        $this->assertCount(1, $entry->fresh()->stars);
1328
1329
        $this->assertEquals($inputData['stars'][0]['title'], $entry->fresh()->stars->first()->title);
1330
        $this->assertEquals($inputData['stars'][0]['id'], $entry->fresh()->stars->first()->id);
1331
    }
1332
1333
    public function testHasManyCreatableRelationship()
1334
    {
1335
        $this->crudPanel->setModel(User::class);
1336
        $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

1336
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1337
                          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...
1337
        $this->crudPanel->addField([
1338
            'name' => 'universes',
1339
            'subfields' => [
1340
                [
1341
                    'name' => 'title',
1342
                ],
1343
            ],
1344
        ], '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

1344
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1345
                          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...
1345
1346
        $faker = Factory::create();
1347
        $inputData = [
1348
            'name' => $faker->name,
1349
            'email' => $faker->safeEmail,
1350
            'password' => Hash::make($faker->password()),
1351
            'remember_token' => null,
1352
            'universes' => [
1353
                [
1354
                    'id' => null,
1355
                    'title' => 'this is the star 1 title',
1356
                ],
1357
                [
1358
                    'title' => 'this is the star 2 title',
1359
                ],
1360
            ],
1361
        ];
1362
1363
        $entry = $this->crudPanel->create($inputData);
1364
        $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...
1365
1366
        $this->assertCount(2, $entry->universes);
1367
1368
        $this->assertEquals($inputData['universes'][0]['title'], $entry->universes()->first()->title);
0 ignored issues
show
Bug introduced by
The property title does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property title does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
1369
1370
        $inputData['universes'] = [
1371
            [
1372
                'id' => 1,
1373
                'title' => 'star 1 with changed title',
1374
            ],
1375
            [
1376
                'id' => 2,
1377
                'title' => 'star 2 with changed title',
1378
            ],
1379
        ];
1380
1381
        $this->crudPanel->update($entry->id, $inputData);
1382
1383
        $universes = $entry->fresh()->universes;
1384
        $this->assertCount(2, $universes);
1385
        $this->assertEquals([1, 2], $universes->pluck('id')->toArray());
1386
1387
        $inputData['universes'] = [
1388
            [
1389
                'id' => 1,
1390
                'title' => 'only one star with changed title',
1391
            ],
1392
        ];
1393
1394
        $this->crudPanel->update($entry->id, $inputData);
1395
1396
        $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

1396
        $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...
1397
        $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...
1398
        $this->assertEquals(1, Universe::all()->count());
1399
1400
        $inputData['universes'] = [
1401
            [
1402
                'id' => null,
1403
                'title' => 'new star 3',
1404
            ],
1405
        ];
1406
1407
        $this->crudPanel->update($entry->id, $inputData);
1408
1409
        $this->assertEquals($inputData['universes'][0]['title'], $entry->fresh()->universes->first()->title);
1410
        $this->assertEquals(3, $entry->fresh()->universes->first()->id);
1411
        $this->assertEquals(1, Universe::all()->count());
1412
1413
        $inputData['universes'] = null;
1414
1415
        $this->crudPanel->update($entry->id, $inputData);
1416
1417
        $this->assertEquals(0, count($entry->fresh()->universes));
1418
        $this->assertEquals(0, Universe::all()->count());
1419
    }
1420
1421
    public function testHasManySelectableRelationshipWithoutForceDelete()
1422
    {
1423
        $this->crudPanel->setModel(User::class);
1424
        $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

1424
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1425
                          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...
1425
        $this->crudPanel->addField([
1426
            'name' => 'planets',
1427
            'force_delete' => false,
1428
            'fallback_id' => false,
1429
        ], '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

1429
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1430
                          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...
1430
1431
        $faker = Factory::create();
1432
        $inputData = [
1433
            'name' => $faker->name,
1434
            'email' => $faker->safeEmail,
1435
            'password' => Hash::make($faker->password()),
1436
            'remember_token' => null,
1437
            'planets' => [1, 2],
1438
        ];
1439
1440
        $entry = $this->crudPanel->create($inputData);
1441
        $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...
1442
1443
        $this->assertCount(2, $entry->planets);
1444
1445
        $inputData['planets'] = [1];
1446
1447
        $this->crudPanel->update($entry->id, $inputData);
1448
1449
        $this->assertCount(1, $entry->fresh()->planets);
1450
1451
        $planets = Planet::all();
1452
1453
        $this->assertCount(2, $planets);
1454
    }
1455
1456
    public function testHasManySelectableRelationshipRemoveAllRelations()
1457
    {
1458
        $this->crudPanel->setModel(User::class);
1459
        $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

1459
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1460
                          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...
1460
        $this->crudPanel->addField([
1461
            'name' => 'planets',
1462
            'force_delete' => false,
1463
            'fallback_id' => false,
1464
        ], '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

1464
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1465
                          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...
1465
1466
        $faker = Factory::create();
1467
        $inputData = [
1468
            'name' => $faker->name,
1469
            'email' => $faker->safeEmail,
1470
            'password' => Hash::make($faker->password()),
1471
            'remember_token' => null,
1472
            'planets' => [1, 2],
1473
        ];
1474
1475
        $entry = $this->crudPanel->create($inputData);
1476
1477
        $this->assertCount(2, $entry->planets);
1478
1479
        $inputData['planets'] = [];
1480
1481
        $this->crudPanel->update($entry->id, $inputData);
1482
1483
        $this->assertCount(0, $entry->fresh()->planets);
1484
1485
        $planets = Planet::all();
1486
1487
        $this->assertCount(2, $planets);
1488
    }
1489
1490
    public function testHasManyWithRelationScoped()
1491
    {
1492
        $this->crudPanel->setModel(User::class);
1493
        $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

1493
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1494
                          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...
1494
        $this->crudPanel->addField([
1495
            'name' => 'incomes',
1496
            'subfields' => [
1497
                [
1498
                    'name' => 'label',
1499
                    'type' => 'text',
1500
                ],
1501
                [
1502
                    'name' => 'type',
1503
                    'type' => 'hidden',
1504
                    'value' => 'income',
1505
                ],
1506
                [
1507
                    'name' => 'amount',
1508
                    'type' => 'number',
1509
                ],
1510
            ],
1511
        ], '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

1511
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1512
                          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...
1512
        $this->crudPanel->addField([
1513
            'name' => 'expenses',
1514
            'subfields' => [
1515
                [
1516
                    'name' => 'label',
1517
                    'type' => 'text',
1518
                ],
1519
                [
1520
                    'name' => 'type',
1521
                    'type' => 'hidden',
1522
                    'value' => 'expense',
1523
                ],
1524
                [
1525
                    'name' => 'amount',
1526
                    'type' => 'number',
1527
                ],
1528
            ],
1529
        ], 'both');
1530
1531
        $faker = Factory::create();
1532
        $inputData = [
1533
            'name' => $faker->name,
1534
            'email' => $faker->safeEmail,
1535
            'password' => Hash::make($faker->password()),
1536
            'remember_token' => null,
1537
            'incomes' => [
1538
                [
1539
                    'label' => $faker->name,
1540
                    'amount' => 33,
1541
                    'type' => 'income',
1542
                ],
1543
                [
1544
                    'label' => $faker->name,
1545
                    'amount' => 22,
1546
                    'type' => 'income',
1547
                ],
1548
            ],
1549
            'expenses' => [
1550
                [
1551
                    'label' => $faker->name,
1552
                    'amount' => 44,
1553
                    'type' => 'expense',
1554
                ],
1555
                [
1556
                    'label' => $faker->name,
1557
                    'amount' => 10,
1558
                    'type' => 'expense',
1559
                ],
1560
            ],
1561
        ];
1562
        $entry = $this->crudPanel->create($inputData);
1563
1564
        $firstExpense = $entry->expenses->first();
0 ignored issues
show
Bug introduced by
The method first() does not exist on null. ( Ignorable by Annotation )

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

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

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

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

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

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

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

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

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

Loading history...
Unused Code introduced by
The assignment to $firstIncome is dead and can be removed.
Loading history...
1566
        $this->assertCount(2, $entry->expenses);
1567
        $this->assertCount(2, $entry->incomes);
1568
        $this->assertEquals(44, $entry->expenses->first()->amount);
1569
        $this->assertEquals(33, $entry->incomes->first()->amount);
1570
1571
        $inputData['incomes'] = [
1572
            [
1573
                'id' => 2,
1574
                'label' => $faker->name,
1575
                'amount' => 222,
1576
                'type' => 'income',
1577
            ],
1578
        ];
1579
        $inputData['expenses'] = [
1580
            [
1581
                'id' => 3,
1582
                'label' => $faker->name,
1583
                'amount' => 44,
1584
                'type' => 'expense',
1585
            ],
1586
            [
1587
                'id' => 4,
1588
                'label' => $faker->name,
1589
                'amount' => 10,
1590
                'type' => 'expense',
1591
            ],
1592
        ];
1593
        $this->crudPanel->update($entry->id, $inputData);
1594
1595
        $freshIncomes = $entry->fresh()->incomes;
1596
        $freshExpenses = $entry->fresh()->expenses;
1597
        $this->assertCount(2, $freshExpenses);
1598
        $this->assertCount(1, $freshIncomes);
1599
        $this->assertEquals(2, $freshIncomes->first()->id);
1600
1601
        $inputData['expenses'] = [];
1602
        $this->crudPanel->update($entry->id, $inputData);
1603
1604
        $freshIncomes = $entry->fresh()->incomes;
1605
        $freshExpenses = $entry->fresh()->expenses;
1606
        $this->assertCount(0, $freshExpenses);
1607
        $this->assertCount(1, $freshIncomes);
1608
    }
1609
1610
    public function testHasManySelectableRelationshipWithFallbackId()
1611
    {
1612
        $this->crudPanel->setModel(User::class);
1613
        $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

1613
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1614
                          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...
1614
        $this->crudPanel->addField([
1615
            'name' => 'planets',
1616
            'fallback_id' => 0,
1617
            'force_delete' => false,
1618
        ], '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

1618
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1619
                          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...
1619
1620
        $faker = Factory::create();
1621
        $inputData = [
1622
            'name' => $faker->name,
1623
            'email' => $faker->safeEmail,
1624
            'password' => Hash::make($faker->password()),
1625
            'remember_token' => null,
1626
            'planets' => [1, 2],
1627
        ];
1628
1629
        $entry = $this->crudPanel->create($inputData);
1630
1631
        $this->assertCount(2, $entry->planets);
1632
1633
        $inputData['planets'] = [2];
1634
1635
        $this->crudPanel->update($entry->id, $inputData);
1636
1637
        $this->assertCount(1, $entry->fresh()->planets);
1638
1639
        $planets = Planet::all();
1640
        $this->assertCount(2, $planets);
1641
        $this->assertEquals(0, $planets->first()->user_id);
1642
    }
1643
1644
    public function testHasManySelectableRelationshipWithForceDelete()
1645
    {
1646
        $this->crudPanel->setModel(User::class);
1647
        $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

1647
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1648
                          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...
1648
        $this->crudPanel->addField([
1649
            'name' => 'planets',
1650
            'force_delete' => true,
1651
            'fallback_id' => false,
1652
        ], '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

1652
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1653
                          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...
1653
1654
        $faker = Factory::create();
1655
        $inputData = [
1656
            'name' => $faker->name,
1657
            'email' => $faker->safeEmail,
1658
            'password' => Hash::make($faker->password()),
1659
            'remember_token' => null,
1660
            'planets' => [1, 2],
1661
        ];
1662
1663
        $entry = $this->crudPanel->create($inputData);
1664
1665
        $this->assertCount(2, $entry->planets);
1666
1667
        $inputData['planets'] = [2];
1668
1669
        $this->crudPanel->update($entry->id, $inputData);
1670
1671
        $this->assertCount(1, $entry->fresh()->planets);
1672
1673
        $planets = Planet::all();
1674
        $this->assertCount(1, $planets);
1675
    }
1676
1677
    public function testHasManySelectableRelationshipNonNullableForeignKeyAndDefaultInDatabase()
1678
    {
1679
        $this->crudPanel->setModel(User::class);
1680
        $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

1680
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1681
                          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...
1681
        $this->crudPanel->addField([
1682
            'name' => 'comets',
1683
            'force_delete' => false,
1684
            'fallback_id' => false,
1685
        ], '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

1685
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1686
                          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...
1686
1687
        $faker = Factory::create();
1688
        $inputData = [
1689
            'name' => $faker->name,
1690
            'email' => $faker->safeEmail,
1691
            'password' => Hash::make($faker->password()),
1692
            'remember_token' => null,
1693
            'comets' => [1, 2],
1694
        ];
1695
1696
        $entry = $this->crudPanel->create($inputData);
1697
1698
        $this->assertCount(2, $entry->comets);
1699
1700
        $inputData['comets'] = [2];
1701
1702
        $this->crudPanel->update($entry->id, $inputData);
1703
1704
        $this->assertCount(1, $entry->fresh()->comets);
1705
1706
        $comets = Comet::all();
1707
        $this->assertCount(2, $comets);
1708
        $this->assertEquals(0, $comets->first()->user_id);
1709
    }
1710
1711
    public function testHasManySelectableRelationshipNonNullable()
1712
    {
1713
        $this->crudPanel->setModel(User::class);
1714
        $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

1714
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1715
                          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...
1715
        $this->crudPanel->addField([
1716
            'name' => 'planetsNonNullable',
1717
            'force_delete' => false,
1718
            'fallback_id' => false,
1719
        ], '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

1719
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1720
                          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...
1720
1721
        $faker = Factory::create();
1722
        $inputData = [
1723
            'name' => $faker->name,
1724
            'email' => $faker->safeEmail,
1725
            'password' => Hash::make($faker->password()),
1726
            'remember_token' => null,
1727
            'planetsNonNullable' => [1, 2],
1728
        ];
1729
1730
        $entry = $this->crudPanel->create($inputData);
1731
1732
        $this->assertCount(2, $entry->planetsNonNullable);
1733
1734
        $inputData['planetsNonNullable'] = null;
1735
1736
        $this->crudPanel->update($entry->id, $inputData);
1737
1738
        $this->assertCount(0, $entry->fresh()->planetsNonNullable);
1739
1740
        $planets = PlanetNonNullable::all();
1741
        $this->assertCount(0, $planets);
1742
    }
1743
1744
    public function testCreateHasManyRelationWithDelimitedNameSubfields()
1745
    {
1746
        $this->crudPanel->setModel(User::class);
1747
        $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

1747
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1748
                          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...
1748
        $this->crudPanel->addField([
1749
            'name' => 'universes',
1750
            'subfields' => [
1751
                [
1752
                    'name' => 'title',
1753
                ],
1754
                [
1755
                    'name' => 'start_date,end_date',
1756
                    'type' => 'date_range',
1757
                ],
1758
            ],
1759
        ], '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

1759
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1760
                          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...
1760
1761
        $faker = Factory::create();
1762
        $inputData = [
1763
            'name' => $faker->name,
1764
            'email' => $faker->safeEmail,
1765
            'password' => Hash::make($faker->password()),
1766
            'remember_token' => null,
1767
            'universes' => [
1768
                [
1769
                    'id' => null,
1770
                    'title' => 'this is the star 1 title',
1771
                    'start_date' => '2021-02-26',
1772
                    'end_date' => '2091-01-26',
1773
                ],
1774
                [
1775
                    'title' => 'this is the star 2 title',
1776
                    'end_date' => '2021-02-26',
1777
                    'start_date' => '2091-01-26',
1778
                ],
1779
            ],
1780
        ];
1781
1782
        $entry = $this->crudPanel->create($inputData);
1783
1784
        $this->assertCount(2, $entry->universes);
1785
1786
        $this->assertEquals($inputData['universes'][0]['start_date'], $entry->universes()->first()->start_date);
0 ignored issues
show
Bug introduced by
The property start_date does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The property start_date does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
1787
        $this->assertEquals($inputData['universes'][0]['end_date'], $entry->universes()->first()->end_date);
0 ignored issues
show
Bug introduced by
The property end_date does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property end_date does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
1788
        $this->assertEquals($inputData['universes'][1]['end_date'], $entry->universes()->find(2)->end_date);
1789
        $this->assertEquals($inputData['universes'][1]['start_date'], $entry->universes()->find(2)->start_date);
1790
    }
1791
1792
    public function testCreateHasOneRelationWithDelimitedNameSubfields()
1793
    {
1794
        $this->crudPanel->setModel(User::class);
1795
        $this->crudPanel->setOperation('create');
1796
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships);
1797
        $this->crudPanel->addField(
1798
            [
1799
                'name' => 'accountDetails',
1800
                'subfields' => [
1801
                    [
1802
                        'name' => 'nickname',
1803
                    ],
1804
                    [
1805
                        'name' => 'start_date,end_date',
1806
                    ],
1807
                    [
1808
                        'name' => 'profile_picture',
1809
                    ],
1810
                ],
1811
            ]);
1812
1813
        $faker = Factory::create();
1814
1815
        $inputData = [
1816
            'name' => $faker->name,
1817
            'email' => $faker->safeEmail,
1818
            'password' => Hash::make($faker->password()),
1819
            'remember_token' => null,
1820
            'roles' => [1, 2],
1821
            'accountDetails' => [
1822
                [
1823
                    'nickname' => 'i_have_has_one',
1824
                    'profile_picture' => 'ohh my picture 1.jpg',
1825
                    'start_date' => '2021-02-26',
1826
                    'end_date' => '2091-01-26',
1827
                ],
1828
            ],
1829
        ];
1830
1831
        $entry = $this->crudPanel->create($inputData);
1832
        $account_details = $entry->accountDetails()->first();
1833
1834
        $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...
1835
        $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...
1836
    }
1837
1838
    public function testBelongsToManyWithDelimitedNameSubfields()
1839
    {
1840
        $this->crudPanel->setModel(User::class);
1841
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships);
1842
        $this->crudPanel->addField([
1843
            'name' => 'superArticles',
1844
            'subfields' => [
1845
                [
1846
                    'name' => 'notes',
1847
                ],
1848
                [
1849
                    'name' => 'start_date,end_date',
1850
                ],
1851
            ],
1852
        ]);
1853
1854
        $faker = Factory::create();
1855
        $articleData = [
1856
            'content' => $faker->text(),
1857
            'tags' => $faker->words(3, true),
1858
            'user_id' => 1,
1859
        ];
1860
1861
        $article = Article::create($articleData);
1862
1863
        $inputData = [
1864
            'name' => $faker->name,
1865
            'email' => $faker->safeEmail,
1866
            'password' => Hash::make($faker->password()),
1867
            'remember_token' => null,
1868
            'superArticles' => [
1869
                [
1870
                    '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...
1871
                    'notes' => 'my first article note',
1872
                    'start_date' => '2021-02-26',
1873
                    'end_date' => '2091-01-26',
1874
                ],
1875
            ],
1876
        ];
1877
1878
        $entry = $this->crudPanel->create($inputData);
1879
1880
        $this->assertCount(1, $entry->fresh()->superArticles);
1881
        $superArticle = $entry->fresh()->superArticles->first();
1882
        $this->assertEquals($superArticle->pivot->start_date, '2021-02-26');
1883
        $this->assertEquals($superArticle->pivot->end_date, '2091-01-26');
1884
1885
        $this->crudPanel->getUpdateFields($superArticle->id);
1886
    }
1887
1888
    public function testItCanCreateMorphToFieldsStructure()
1889
    {
1890
        $this->crudPanel->setModel(Star::class);
1891
        $this->crudPanel->addField([
1892
            'name' => 'starable',
1893
            'morphOptions' => [
1894
                ['Backpack\CRUD\Tests\config\Models\User', 'User'],
1895
            ],
1896
        ]);
1897
1898
        $this->assertCount(2, $this->crudPanel->fields()['starable']['subfields']);
1899
1900
        [$morphTypeField, $morphIdField] = $this->crudPanel->fields()['starable']['subfields'];
1901
1902
        $this->assertTrue($morphTypeField['name'] === 'starable_type');
1903
        $this->assertTrue($morphIdField['name'] === 'starable_id');
1904
    }
1905
1906
    public function testIPreventsAddingRepeateadMorphOptions()
1907
    {
1908
        $this->crudPanel->setModel(Star::class);
1909
        $this->expectException(\Exception::class);
1910
1911
        $this->crudPanel->addField([
1912
            'name' => 'starable',
1913
            'morphOptions' => [
1914
                ['Backpack\CRUD\Tests\config\Models\User', 'User'],
1915
                ['Backpack\CRUD\Tests\config\Models\User', 'User'],
1916
            ],
1917
        ]);
1918
    }
1919
1920
    public function testItThrowsErrorIfStringIsNotOnMorphMap()
1921
    {
1922
        $this->crudPanel->setModel(Star::class);
1923
        $this->expectException(\Exception::class);
1924
1925
        $this->crudPanel->addField([
1926
            'name' => 'starable',
1927
            'morphOptions' => [
1928
                ['somethingThatDoesNotExist'],
1929
            ],
1930
        ]);
1931
    }
1932
1933
    public function testItCanAddTheOptionsFromTheMorphMap()
1934
    {
1935
        $this->crudPanel->setModel(Star::class);
1936
1937
        Relation::morphMap([
1938
            'user' => 'Backpack\CRUD\Tests\config\Models\User',
1939
        ]);
1940
1941
        $this->crudPanel->addField([
1942
            'name' => 'starable',
1943
            'morphOptions' => [
1944
                ['user'],
1945
            ],
1946
        ]);
1947
1948
        [$morphTypeField, $morphIdField] = $this->crudPanel->fields()['starable']['subfields'];
1949
        $this->assertEquals(['user' => 'Backpack\CRUD\Tests\config\Models\User'], $morphTypeField['morphMap']);
1950
    }
1951
1952
    public function testItThrowsErrorIfDuplicateMorphMapName()
1953
    {
1954
        $this->crudPanel->setModel(Star::class);
1955
        $this->expectException(\Exception::class);
1956
1957
        Relation::morphMap([
1958
            'user' => 'Backpack\CRUD\Tests\config\Models\User',
1959
        ]);
1960
1961
        $this->crudPanel->addField([
1962
            'name' => 'starable',
1963
            'morphOptions' => [
1964
                ['user'],
1965
                ['user'],
1966
            ],
1967
        ]);
1968
    }
1969
1970
    public function testItCanRegisterModelEventsInTheFields()
1971
    {
1972
        $this->crudPanel->setModel(User::class);
1973
1974
        $this->crudPanel->addField([
1975
            'name' => 'name',
1976
            'events' => [
1977
                '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

1977
                '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...
1978
                },
1979
                'creating' => function ($entry) {
1980
                    $entry->email = '[email protected]';
1981
                    $entry->password = Hash::make('password');
1982
                },
1983
                '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

1983
                '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...
1984
                },
1985
                '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

1985
                '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...
1986
                },
1987
                '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

1987
                '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...
1988
                },
1989
                '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

1989
                '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...
1990
                },
1991
                '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

1991
                '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...
1992
                },
1993
                '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

1993
                '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...
1994
                },
1995
            ],
1996
        ]);
1997
1998
        $this->crudPanel->registerFieldEvents();
1999
2000
        $this->assertNotEmpty($this->crudPanel->getModel()->getEventDispatcher()->getListeners('eloquent.created: Backpack\CRUD\Tests\Config\Models\User'));
2001
        $this->assertNotEmpty($this->crudPanel->getModel()->getEventDispatcher()->getListeners('eloquent.creating: Backpack\CRUD\Tests\Config\Models\User'));
2002
        $this->assertNotEmpty($this->crudPanel->getModel()->getEventDispatcher()->getListeners('eloquent.saving: Backpack\CRUD\Tests\Config\Models\User'));
2003
        $this->assertNotEmpty($this->crudPanel->getModel()->getEventDispatcher()->getListeners('eloquent.saved: Backpack\CRUD\Tests\Config\Models\User'));
2004
        $this->assertNotEmpty($this->crudPanel->getModel()->getEventDispatcher()->getListeners('eloquent.updating: Backpack\CRUD\Tests\Config\Models\User'));
2005
        $this->assertNotEmpty($this->crudPanel->getModel()->getEventDispatcher()->getListeners('eloquent.updated: Backpack\CRUD\Tests\Config\Models\User'));
2006
        $this->assertNotEmpty($this->crudPanel->getModel()->getEventDispatcher()->getListeners('eloquent.deleting: Backpack\CRUD\Tests\Config\Models\User'));
2007
        $this->assertNotEmpty($this->crudPanel->getModel()->getEventDispatcher()->getListeners('eloquent.deleted: Backpack\CRUD\Tests\Config\Models\User'));
2008
2009
        $this->crudPanel->getModel()->create(['name' => 'test']);
2010
2011
        $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...
2012
    }
2013
2014
    public function testItCanCreateDynamicRelationships()
2015
    {
2016
        User::resolveRelationUsing('dynamicRelation', function ($user) {
2017
            return $user->belongsTo(\Backpack\CRUD\Tests\config\Models\Bang::class, 'bang_relation_field');
2018
        });
2019
2020
        $this->crudPanel->setModel(User::class);
2021
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships);
2022
        $this->crudPanel->addField([
2023
            'name' => 'dynamicRelation',
2024
        ]);
2025
2026
        $faker = Factory::create();
2027
2028
        $inputData = [
2029
            'name' => $faker->name,
2030
            'email' => $faker->safeEmail,
2031
            'password' => Hash::make($faker->password()),
2032
            'remember_token' => null,
2033
            'dynamicRelation' => 1,
2034
        ];
2035
2036
        $entry = $this->crudPanel->create($inputData);
2037
2038
        $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...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property name does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
2039
    }
2040
2041
    private function getPivotInputData(array $pivotRelationData, bool $initCrud = true, bool $allowDuplicates = false)
2042
    {
2043
        $faker = Factory::create();
2044
2045
        if ($initCrud) {
2046
            $this->crudPanel->setModel(User::class);
2047
            $this->crudPanel->addFields($this->userInputFieldsNoRelationships);
2048
            $this->crudPanel->addField([
2049
                'name' => array_key_first($pivotRelationData),
2050
                'allow_duplicate_pivots' => $allowDuplicates,
2051
                'pivot_key_name' => 'id',
2052
                'subfields' => [
2053
                    [
2054
                        'name' => 'notes',
2055
                    ],
2056
2057
                ],
2058
            ]);
2059
2060
            $article = Article::create([
0 ignored issues
show
Unused Code introduced by
The assignment to $article is dead and can be removed.
Loading history...
2061
                'content' => $faker->text(),
2062
                'tags' => $faker->words(3, true),
2063
                'user_id' => 1,
2064
            ]);
2065
            $article2 = Article::create([
0 ignored issues
show
Unused Code introduced by
The assignment to $article2 is dead and can be removed.
Loading history...
2066
                'content' => $faker->text(),
2067
                'tags' => $faker->words(3, true),
2068
                'user_id' => 1,
2069
            ]);
2070
        }
2071
2072
        $inputData = [
2073
            'name' => $faker->name,
2074
            'email' => $faker->safeEmail,
2075
            'password' => Hash::make($faker->password()),
2076
            'remember_token' => null,
2077
        ];
2078
2079
        return array_merge($inputData, $pivotRelationData);
2080
    }
2081
}
2082