Scrutinizer GitHub App not installed

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

Install GitHub App

Passed
Pull Request — main (#5535)
by
unknown
19:17 queued 04:18
created

testHasManySelectableRelationshipNonNullable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

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

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

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

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

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

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

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

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

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

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

Loading history...
397
398
        $faker = Factory::create();
399
        $inputData = [
400
            'name' => $faker->name,
401
            'email' => $faker->safeEmail,
402
            'password' => Hash::make($faker->password()),
403
            'remember_token' => null,
404
            'bills' => [1],
405
        ];
406
407
        $entry = $this->crudPanel->create($inputData);
408
409
        $updateFields = $this->crudPanel->getUpdateFields($entry->id);
0 ignored issues
show
Unused Code introduced by
The assignment to $updateFields is dead and can be removed.
Loading history...
410
411
        $this->assertCount(1, $entry->bills);
412
413
        $this->assertEquals(1, $entry->bills()->first()->id);
0 ignored issues
show
Bug introduced by
The property id does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The property id does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
414
415
        $inputData['bills'] = [1, 2];
416
417
        $this->crudPanel->update($entry->id, $inputData);
418
419
        $this->assertCount(2, $entry->fresh()->bills);
420
421
        $this->assertEquals([1, 2], $entry->fresh()->bills->pluck('id')->toArray());
0 ignored issues
show
Bug introduced by
The method pluck() does not exist on Countable. It seems like you code against a sub-type of Countable such as RectorPrefix202405\Nette\Utils\Html or RectorPrefix202405\Nette\Utils\ArrayList or Illuminate\Pagination\LengthAwarePaginator or Illuminate\Http\Resources\Json\ResourceCollection or Nette\Utils\Html or Illuminate\Pagination\CursorPaginator or Carbon\CarbonPeriod or Illuminate\Pagination\Paginator or Nette\Iterators\CachingIterator or Nette\Utils\ArrayList or Illuminate\Support\ViewErrorBag or RectorPrefix202405\Nette\Iterators\CachingIterator or Illuminate\Support\Enumerable or Prologue\Alerts\AlertsMessageBag or Nette\Iterators\CachingIterator or RectorPrefix202405\Nette\Iterators\CachingIterator. ( Ignorable by Annotation )

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

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

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

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

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

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

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

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

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

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

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

Loading history...
433
434
        $faker = Factory::create();
435
        $inputData = [
436
            'name' => $faker->name,
437
            'email' => $faker->safeEmail,
438
            'password' => Hash::make($faker->password()),
439
            'remember_token' => null,
440
            'recommends' => [
441
                [
442
                    'recommends' => 1,
443
                    'text' => 'my pivot recommend field',
444
                ],
445
            ],
446
        ];
447
448
        $entry = $this->crudPanel->create($inputData);
449
        $updateFields = $this->crudPanel->getUpdateFields($entry->id);
0 ignored issues
show
Unused Code introduced by
The assignment to $updateFields is dead and can be removed.
Loading history...
450
451
        $this->assertCount(1, $entry->recommends);
452
453
        $this->assertEquals(1, $entry->recommends()->first()->id);
0 ignored issues
show
Bug introduced by
The property id does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The property id does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
454
455
        $inputData['recommends'] = [
456
            [
457
                'recommends' => 2,
458
                'text' => 'I changed the recommend and the pivot text',
459
            ],
460
        ];
461
462
        $this->crudPanel->update($entry->id, $inputData);
463
464
        $this->assertCount(1, $entry->fresh()->recommends);
465
466
        $this->assertEquals(2, $entry->recommends()->first()->id);
467
468
        $this->assertEquals('I changed the recommend and the pivot text', $entry->fresh()->recommends->first()->pivot->text);
0 ignored issues
show
Bug introduced by
The method first() does not exist on Countable. It seems like you code against a sub-type of Countable such as RectorPrefix202405\Nette\Utils\Html or RectorPrefix202405\Nette\Utils\ArrayList or Illuminate\Pagination\LengthAwarePaginator or Illuminate\Http\Resources\Json\ResourceCollection or Nette\Utils\Html or League\CommonMark\Util\ArrayCollection or Illuminate\Pagination\CursorPaginator or RectorPrefix202405\Illum...acts\Support\MessageBag or Illuminate\Contracts\Support\MessageBag or Carbon\CarbonPeriod or Illuminate\Pagination\Paginator or Nette\Iterators\CachingIterator or Nette\Utils\ArrayList or Illuminate\Support\ViewErrorBag or RectorPrefix202405\Nette\Iterators\CachingIterator or Illuminate\Support\Enumerable or Ramsey\Collection\CollectionInterface or Ramsey\Collection\AbstractCollection or Ds\Map or Ds\Set or Ds\Sequence or Nette\Iterators\CachingIterator or RectorPrefix202405\Nette\Iterators\CachingIterator. ( Ignorable by Annotation )

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

468
        $this->assertEquals('I changed the recommend and the pivot text', $entry->fresh()->recommends->/** @scrutinizer ignore-call */ first()->pivot->text);
Loading history...
469
    }
470
471
    public function testBelongsToManyWithPivotDataRelationship()
472
    {
473
        $this->crudPanel->setModel(User::class);
474
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships);
475
        $this->crudPanel->addField([
476
            'name' => 'superArticles',
477
            'subfields' => [
478
                [
479
                    'name' => 'notes',
480
                ],
481
            ],
482
        ]);
483
484
        $faker = Factory::create();
485
        $articleData = [
486
            'content' => $faker->text(),
487
            'tags' => $faker->words(3, true),
488
            'user_id' => 1,
489
        ];
490
491
        $article = Article::create($articleData);
492
493
        $inputData = [
494
            'name' => $faker->name,
495
            'email' => $faker->safeEmail,
496
            'password' => Hash::make($faker->password()),
497
            'remember_token' => null,
498
            'superArticles' => [
499
                [
500
                    'superArticles' => $article->id,
0 ignored issues
show
Bug introduced by
The property id does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
501
                    'notes' => 'my first article note',
502
                ],
503
            ],
504
        ];
505
506
        $entry = $this->crudPanel->create($inputData);
507
        $updateFields = $this->crudPanel->getUpdateFields($entry->id);
0 ignored issues
show
Unused Code introduced by
The assignment to $updateFields is dead and can be removed.
Loading history...
508
509
        $this->assertCount(1, $entry->fresh()->superArticles);
510
        $this->assertEquals('my first article note', $entry->fresh()->superArticles->first()->pivot->notes);
511
    }
512
513
    public function testBelongsToManyWithMultipleSameRelationIdAndPivotDataRelationship()
514
    {
515
        $this->crudPanel->setModel(User::class);
516
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships);
517
        $this->crudPanel->addField([
518
            'name' => 'superArticles',
519
            'subfields' => [
520
                [
521
                    'name' => 'notes',
522
                ],
523
            ],
524
        ]);
525
526
        $faker = Factory::create();
527
        $articleData = [
0 ignored issues
show
Unused Code introduced by
The assignment to $articleData is dead and can be removed.
Loading history...
528
            'content' => $faker->text(),
529
            'tags' => $faker->words(3, true),
530
            'user_id' => 1,
531
        ];
532
533
        $article = Article::create([
534
            'content' => $faker->text(),
535
            'tags' => $faker->words(3, true),
536
            'user_id' => 1,
537
        ]);
538
        $article2 = Article::create([
539
            'content' => $faker->text(),
540
            'tags' => $faker->words(3, true),
541
            'user_id' => 1,
542
        ]);
543
        $inputData = [
544
            'name' => $faker->name,
545
            'email' => $faker->safeEmail,
546
            'password' => Hash::make($faker->password()),
547
            'remember_token' => null,
548
            'superArticles' => [
549
                [
550
                    '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...
551
                    'notes' => 'my first article note',
552
                ],
553
                [
554
                    'superArticles' => $article->id,
555
                    'notes' => 'my second article note',
556
                ],
557
                [
558
                    'superArticles' => $article2->id,
559
                    'notes' => 'my first article2 note',
560
                ],
561
            ],
562
        ];
563
564
        $entry = $this->crudPanel->create($inputData);
565
        $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...
566
567
        $this->assertCount(3, $entry->fresh()->superArticles);
568
        $this->assertEquals('my first article note', $entry->fresh()->superArticles->first()->pivot->notes);
569
        $this->assertEquals('my second article note', $entry->fresh()->superArticles[1]->pivot->notes);
570
        $this->assertEquals('my first article2 note', $entry->fresh()->superArticles[2]->pivot->notes);
571
    }
572
573
    public function testCreateHasOneWithNestedRelationsRepeatableInterface()
574
    {
575
        $this->crudPanel->setModel(User::class);
576
        $this->crudPanel->setOperation('create');
577
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships);
578
        $this->crudPanel->addField(
579
            [
580
                'name' => 'accountDetails',
581
                'subfields' => [
582
                    [
583
                        'name' => 'nickname',
584
                    ],
585
                    [
586
                        'name' => 'profile_picture',
587
                    ],
588
                    [
589
                        'name' => 'article',
590
                    ],
591
                    [
592
                        'name' => 'addresses',
593
                        'subfields' => [
594
                            [
595
                                'name' => 'bang',
596
                            ],
597
                            [
598
                                'name' => 'street',
599
                            ],
600
                            [
601
                                'name' => 'number',
602
                            ],
603
                        ],
604
                    ],
605
                    [
606
                        'name' => 'bangs',
607
                    ],
608
                    [
609
                        'name' => 'bangsPivot',
610
                        'subfields' => [
611
                            [
612
                                'name' => 'pivot_field',
613
                            ],
614
                        ],
615
                    ],
616
                ],
617
            ]);
618
619
        $faker = Factory::create();
620
621
        $inputData = [
622
            'name' => $faker->name,
623
            'email' => $faker->safeEmail,
624
            'password' => Hash::make($faker->password()),
625
            'remember_token' => null,
626
            'roles' => [1, 2],
627
            'accountDetails' => [
628
                [
629
                    'nickname' => 'i_have_has_one',
630
                    'profile_picture' => 'ohh my picture 1.jpg',
631
                    'article' => 1,
632
                    'addresses' => [
633
                        [
634
                            'bang' => 1,
635
                            'street' => 'test',
636
                            'number' => 1,
637
                        ],
638
                        [
639
                            'bang' => 1,
640
                            'street' => 'test2',
641
                            'number' => 2,
642
                        ],
643
                    ],
644
                    'bangs' => [1, 2],
645
                    'bangsPivot' => [
646
                        ['bangsPivot' => 1, 'pivot_field' => 'test1'],
647
                        ['bangsPivot' => 2, 'pivot_field' => 'test2'],
648
                    ],
649
                ],
650
            ],
651
        ];
652
653
        $entry = $this->crudPanel->create($inputData);
654
        $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...
655
        $account_details = $entry->accountDetails()->first();
656
657
        $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...
658
        $this->assertEquals($account_details->addresses->count(), 2);
0 ignored issues
show
Bug introduced by
The method count() does not exist on null. ( Ignorable by Annotation )

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Loading history...
660
        $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...
661
        $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...
662
        $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

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

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

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

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

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

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

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

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

Loading history...
Bug introduced by
The property bangsPivot does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The property bangsPivot does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
664
        $this->assertEquals($account_details->bangsPivot->first()->pivot->pivot_field, 'test1');
0 ignored issues
show
Bug introduced by
The property pivot does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property pivot_field does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property pivot does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The property pivot_field does not seem to exist on Illuminate\Database\Eloquent\Relations\Relation.
Loading history...
Bug introduced by
The property pivot_field does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The property pivot_field does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
665
    }
666
667
    public function testCreateBelongsToFake()
668
    {
669
        $belongsToField = [   // select_grouped
670
            'label' => 'Select_grouped',
671
            'type' => 'select_grouped', //https://github.com/Laravel-Backpack/CRUD/issues/502
672
            'name' => 'bang_relation_field',
673
            'fake' => true,
674
            'entity' => 'bang',
675
            'model' => 'Backpack\CRUD\Tests\config\Models\Bang',
676
            'attribute' => 'title',
677
            'group_by' => 'category', // the relationship to entity you want to use for grouping
678
            'group_by_attribute' => 'name', // the attribute on related model, that you want shown
679
            'group_by_relationship_back' => 'articles', // relationship from related model back to this model
680
            'tab' => 'Selects',
681
            'wrapperAttributes' => ['class' => 'form-group col-md-6'],
682
        ];
683
684
        $this->crudPanel->setModel(User::class);
685
        $this->crudPanel->setOperation('create');
686
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships);
687
        $this->crudPanel->addField($belongsToField);
688
689
        $faker = Factory::create();
690
691
        $inputData = [
692
            'name' => $faker->name,
693
            'email' => $faker->safeEmail,
694
            'password' => Hash::make($faker->password()),
695
            'remember_token' => null,
696
            'bang_relation_field' => 1,
697
        ];
698
699
        $entry = $this->crudPanel->create($inputData);
700
        $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...
701
        $this->crudPanel->entry = $entry->withFakes();
702
        $this->assertEquals($entry->bang_relation_field, 1);
703
    }
704
705
    public function testCreateHasOneWithNestedRelations()
706
    {
707
        $this->crudPanel->setModel(User::class);
708
        $this->crudPanel->setOperation('create');
709
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships);
710
        $this->crudPanel->addFields([
711
            [
712
                'name' => 'accountDetails.nickname',
713
            ],
714
            [
715
                'name' => 'accountDetails.profile_picture',
716
            ],
717
            [
718
                'name' => 'accountDetails.article',
719
            ],
720
            [
721
                'name' => 'accountDetails.addresses',
722
                'subfields' => [
723
                    [
724
                        'name' => 'city',
725
                        'entity' => 'bang',
726
                    ],
727
                    [
728
                        'name' => 'street',
729
                    ],
730
                    [
731
                        'name' => 'number',
732
                    ],
733
                ],
734
            ],
735
            [
736
                'name' => 'accountDetails.bangs',
737
            ],
738
            [
739
                'name' => 'accountDetails.bangsPivot',
740
                'subfields' => [
741
                    [
742
                        'name' => 'pivot_field',
743
                    ],
744
                ],
745
            ],
746
        ]);
747
748
        $faker = Factory::create();
749
750
        $inputData = [
751
            'name' => $faker->name,
752
            'email' => $faker->safeEmail,
753
            'password' => Hash::make($faker->password()),
754
            'remember_token' => null,
755
            'roles' => [1, 2],
756
            'accountDetails' => [
757
                'nickname' => 'i_have_has_one',
758
                'profile_picture' => 'ohh my picture 1.jpg',
759
                'article' => 1,
760
                'addresses' => [
761
                    [
762
                        'city' => 1,
763
                        'street' => 'test',
764
                        'number' => 1,
765
                    ],
766
                    [
767
                        'city' => 2,
768
                        'street' => 'test2',
769
                        'number' => 2,
770
                    ],
771
                ],
772
                'bangs' => [1, 2],
773
                'bangsPivot' => [
774
                    ['bangsPivot' => 1, 'pivot_field' => 'test1'],
775
                    ['bangsPivot' => 2, 'pivot_field' => 'test2'],
776
                ],
777
            ],
778
        ];
779
780
        $entry = $this->crudPanel->create($inputData);
781
        $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...
782
        $account_details = $entry->accountDetails()->first();
783
784
        $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...
785
        $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...
786
        $this->assertEquals($account_details->addresses->first()->bang->id, 1);
0 ignored issues
show
Bug introduced by
The property bang does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property id does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property bang does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The property id does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
Bug introduced by
The property id does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The property id does not seem to exist on Illuminate\Database\Eloquent\Relations\Relation.
Loading history...
787
        $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...
788
        $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...
789
        $this->assertEquals($account_details->bangs->first()->name, Bang::find(1)->name);
0 ignored issues
show
Bug introduced by
The property name does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
Bug introduced by
The property bangs does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property name does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property 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...
790
        $this->assertEquals($account_details->bangsPivot->count(), 2);
0 ignored issues
show
Bug introduced by
The property bangsPivot does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property bangsPivot does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
791
        $this->assertEquals($account_details->bangsPivot->first()->pivot->pivot_field, 'test1');
0 ignored issues
show
Bug introduced by
The property pivot_field does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The property pivot_field does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property pivot_field does not seem to exist on Illuminate\Database\Eloquent\Relations\Relation.
Loading history...
Bug introduced by
The property pivot does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property pivot does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The property pivot_field does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
792
793
        // Now test the remove process
794
795
        $inputData = [
796
            'name' => $faker->name,
797
            'email' => $faker->safeEmail,
798
            'password' => Hash::make($faker->password()),
799
            'remember_token' => null,
800
            'roles' => [1, 2],
801
            'accountDetails' => [
802
                'nickname' => 'i_have_has_one',
803
                'profile_picture' => 'ohh my picture 1.jpg',
804
                'article' => 1,
805
                'addresses' => [ // HasOne is tested in other test function
806
                    [
807
                        'city' => 2,
808
                        'street' => 'test',
809
                        'number' => 1,
810
                    ],
811
                    [
812
                        'city' => 1,
813
                        'street' => 'test2',
814
                        'number' => 2,
815
                    ],
816
                ],
817
                'bangs' => [],
818
                'bangsPivot' => [],
819
            ],
820
        ];
821
822
        $entry = $this->crudPanel->update($entry->id, $inputData);
823
        $account_details = $entry->accountDetails()->first();
824
        $this->assertEquals($account_details->addresses->count(), 2);
825
        $this->assertEquals($account_details->addresses->first()->bang->id, 2);
826
        $this->assertEquals($account_details->bangs->count(), 0);
827
        $this->assertEquals($account_details->bangsPivot->count(), 0);
828
    }
829
830
    public function testMorphOneRelationship()
831
    {
832
        $this->crudPanel->setModel(User::class);
833
        $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

833
        $this->crudPanel->/** @scrutinizer ignore-call */ 
834
                          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...
834
        $this->crudPanel->addField([
835
            'name' => 'comment.text',
836
        ], '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

836
        $this->crudPanel->/** @scrutinizer ignore-call */ 
837
                          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...
837
838
        $faker = Factory::create();
839
        $inputData = [
840
            'name' => $faker->name,
841
            'email' => $faker->safeEmail,
842
            'password' => Hash::make($faker->password()),
843
            'remember_token' => null,
844
            'comment' => [
845
                'text' => 'some test comment text',
846
            ],
847
        ];
848
849
        $entry = $this->crudPanel->create($inputData);
850
        $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...
851
852
        $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...gHasThroughRelationship.
Loading history...
Bug introduced by
The property text does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The property text does not seem to exist on Illuminate\Database\Eloquent\Relations\Relation.
Loading history...
Bug introduced by
The property text does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
853
854
        $inputData['comment']['text'] = 'updated comment text';
855
856
        $this->crudPanel->update($entry->id, $inputData);
857
858
        $this->assertEquals($inputData['comment']['text'], $entry->fresh()->comment->text);
859
    }
860
861
    public function testMorphManyCreatableRelationship()
862
    {
863
        $this->crudPanel->setModel(User::class);
864
        $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

864
        $this->crudPanel->/** @scrutinizer ignore-call */ 
865
                          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...
865
        $this->crudPanel->addField([
866
            'name' => 'stars',
867
            'subfields' => [
868
                [
869
                    'name' => 'title',
870
                ],
871
            ],
872
        ], '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

872
        $this->crudPanel->/** @scrutinizer ignore-call */ 
873
                          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...
873
874
        $faker = Factory::create();
875
        $inputData = [
876
            'name' => $faker->name,
877
            'email' => $faker->safeEmail,
878
            'password' => Hash::make($faker->password()),
879
            'remember_token' => null,
880
            'stars' => [
881
                [
882
                    'id' => null,
883
                    'title' => 'this is the star 1 title',
884
                ],
885
                [
886
                    'id' => null,
887
                    'title' => 'this is the star 2 title',
888
                ],
889
            ],
890
        ];
891
892
        $entry = $this->crudPanel->create($inputData);
893
        $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...
894
895
        $this->assertCount(2, $entry->stars);
896
897
        $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...
898
899
        $inputData['stars'] = [
900
            [
901
                'id' => 1,
902
                'title' => 'only one star with changed title',
903
            ],
904
        ];
905
906
        $this->crudPanel->update($entry->id, $inputData);
907
908
        $this->assertCount(1, $entry->fresh()->stars);
909
910
        $this->assertEquals($inputData['stars'][0]['title'], $entry->fresh()->stars->first()->title);
911
        $this->assertEquals($inputData['stars'][0]['id'], $entry->fresh()->stars->first()->id);
912
    }
913
914
    public function testHasManyCreatableRelationship()
915
    {
916
        $this->crudPanel->setModel(User::class);
917
        $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

917
        $this->crudPanel->/** @scrutinizer ignore-call */ 
918
                          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...
918
        $this->crudPanel->addField([
919
            'name' => 'universes',
920
            'subfields' => [
921
                [
922
                    'name' => 'title',
923
                ],
924
            ],
925
        ], '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

925
        $this->crudPanel->/** @scrutinizer ignore-call */ 
926
                          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...
926
927
        $faker = Factory::create();
928
        $inputData = [
929
            'name' => $faker->name,
930
            'email' => $faker->safeEmail,
931
            'password' => Hash::make($faker->password()),
932
            'remember_token' => null,
933
            'universes' => [
934
                [
935
                    'id' => null,
936
                    'title' => 'this is the star 1 title',
937
                ],
938
                [
939
                    'title' => 'this is the star 2 title',
940
                ],
941
            ],
942
        ];
943
944
        $entry = $this->crudPanel->create($inputData);
945
        $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...
946
947
        $this->assertCount(2, $entry->universes);
948
949
        $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...
950
951
        $inputData['universes'] = [
952
            [
953
                'id' => 1,
954
                'title' => 'star 1 with changed title',
955
            ],
956
            [
957
                'id' => 2,
958
                'title' => 'star 2 with changed title',
959
            ],
960
        ];
961
962
        $this->crudPanel->update($entry->id, $inputData);
963
964
        $universes = $entry->fresh()->universes;
965
        $this->assertCount(2, $universes);
966
        $this->assertEquals([1, 2], $universes->pluck('id')->toArray());
967
968
        $inputData['universes'] = [
969
            [
970
                'id' => 1,
971
                'title' => 'only one star with changed title',
972
            ],
973
        ];
974
975
        $this->crudPanel->update($entry->id, $inputData);
976
977
        $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

977
        $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...
978
        $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...elations\HasManyThrough.
Loading history...
Bug introduced by
The property id does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
979
        $this->assertEquals(1, Universe::all()->count());
980
981
        $inputData['universes'] = [
982
            [
983
                'id' => null,
984
                'title' => 'new star 3',
985
            ],
986
        ];
987
988
        $this->crudPanel->update($entry->id, $inputData);
989
990
        $this->assertEquals($inputData['universes'][0]['title'], $entry->fresh()->universes->first()->title);
991
        $this->assertEquals(3, $entry->fresh()->universes->first()->id);
992
        $this->assertEquals(1, Universe::all()->count());
993
994
        $inputData['universes'] = null;
995
996
        $this->crudPanel->update($entry->id, $inputData);
997
998
        $this->assertEquals(0, count($entry->fresh()->universes));
999
        $this->assertEquals(0, Universe::all()->count());
1000
    }
1001
1002
    public function testHasManySelectableRelationshipWithoutForceDelete()
1003
    {
1004
        $this->crudPanel->setModel(User::class);
1005
        $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

1005
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1006
                          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...
1006
        $this->crudPanel->addField([
1007
            'name' => 'planets',
1008
            'force_delete' => false,
1009
            'fallback_id' => false,
1010
        ], '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

1010
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1011
                          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...
1011
1012
        $faker = Factory::create();
1013
        $inputData = [
1014
            'name' => $faker->name,
1015
            'email' => $faker->safeEmail,
1016
            'password' => Hash::make($faker->password()),
1017
            'remember_token' => null,
1018
            'planets' => [1, 2],
1019
        ];
1020
1021
        $entry = $this->crudPanel->create($inputData);
1022
        $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...
1023
1024
        $this->assertCount(2, $entry->planets);
1025
1026
        $inputData['planets'] = [1];
1027
1028
        $this->crudPanel->update($entry->id, $inputData);
1029
1030
        $this->assertCount(1, $entry->fresh()->planets);
1031
1032
        $planets = Planet::all();
1033
1034
        $this->assertCount(2, $planets);
1035
    }
1036
1037
    public function testHasManySelectableRelationshipRemoveAllRelations()
1038
    {
1039
        $this->crudPanel->setModel(User::class);
1040
        $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

1040
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1041
                          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...
1041
        $this->crudPanel->addField([
1042
            'name' => 'planets',
1043
            'force_delete' => false,
1044
            'fallback_id' => false,
1045
        ], '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

1045
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1046
                          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...
1046
1047
        $faker = Factory::create();
1048
        $inputData = [
1049
            'name' => $faker->name,
1050
            'email' => $faker->safeEmail,
1051
            'password' => Hash::make($faker->password()),
1052
            'remember_token' => null,
1053
            'planets' => [1, 2],
1054
        ];
1055
1056
        $entry = $this->crudPanel->create($inputData);
1057
1058
        $this->assertCount(2, $entry->planets);
1059
1060
        $inputData['planets'] = [];
1061
1062
        $this->crudPanel->update($entry->id, $inputData);
1063
1064
        $this->assertCount(0, $entry->fresh()->planets);
1065
1066
        $planets = Planet::all();
1067
1068
        $this->assertCount(2, $planets);
1069
    }
1070
1071
    public function testHasManyWithRelationScoped()
1072
    {
1073
        $this->crudPanel->setModel(User::class);
1074
        $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

1074
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1075
                          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...
1075
        $this->crudPanel->addField([
1076
            'name' => 'incomes',
1077
            'subfields' => [
1078
                [
1079
                    'name' => 'label',
1080
                    'type' => 'text',
1081
                ],
1082
                [
1083
                    'name' => 'type',
1084
                    'type' => 'hidden',
1085
                    'value' => 'income',
1086
                ],
1087
                [
1088
                    'name' => 'amount',
1089
                    'type' => 'number',
1090
                ],
1091
            ],
1092
        ], '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

1092
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1093
                          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...
1093
        $this->crudPanel->addField([
1094
            'name' => 'expenses',
1095
            'subfields' => [
1096
                [
1097
                    'name' => 'label',
1098
                    'type' => 'text',
1099
                ],
1100
                [
1101
                    'name' => 'type',
1102
                    'type' => 'hidden',
1103
                    'value' => 'expense',
1104
                ],
1105
                [
1106
                    'name' => 'amount',
1107
                    'type' => 'number',
1108
                ],
1109
            ],
1110
        ], 'both');
1111
1112
        $faker = Factory::create();
1113
        $inputData = [
1114
            'name' => $faker->name,
1115
            'email' => $faker->safeEmail,
1116
            'password' => Hash::make($faker->password()),
1117
            'remember_token' => null,
1118
            'incomes' => [
1119
                [
1120
                    'label' => $faker->name,
1121
                    'amount' => 33,
1122
                    'type' => 'income',
1123
                ],
1124
                [
1125
                    'label' => $faker->name,
1126
                    'amount' => 22,
1127
                    'type' => 'income',
1128
                ],
1129
            ],
1130
            'expenses' => [
1131
                [
1132
                    'label' => $faker->name,
1133
                    'amount' => 44,
1134
                    'type' => 'expense',
1135
                ],
1136
                [
1137
                    'label' => $faker->name,
1138
                    'amount' => 10,
1139
                    'type' => 'expense',
1140
                ],
1141
            ],
1142
        ];
1143
        $entry = $this->crudPanel->create($inputData);
1144
1145
        $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

1145
        /** @scrutinizer ignore-call */ 
1146
        $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...
1146
        $firstIncome = $entry->incomes->first();
0 ignored issues
show
Unused Code introduced by
The assignment to $firstIncome is dead and can be removed.
Loading history...
Bug introduced by
The method first() does not exist on null. ( Ignorable by Annotation )

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

1146
        /** @scrutinizer ignore-call */ 
1147
        $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...
1147
        $this->assertCount(2, $entry->expenses);
1148
        $this->assertCount(2, $entry->incomes);
1149
        $this->assertEquals(44, $entry->expenses->first()->amount);
1150
        $this->assertEquals(33, $entry->incomes->first()->amount);
1151
1152
        $inputData['incomes'] = [
1153
            [
1154
                'id' => 2,
1155
                'label' => $faker->name,
1156
                'amount' => 222,
1157
                'type' => 'income',
1158
            ],
1159
        ];
1160
        $inputData['expenses'] = [
1161
            [
1162
                'id' => 3,
1163
                'label' => $faker->name,
1164
                'amount' => 44,
1165
                'type' => 'expense',
1166
            ],
1167
            [
1168
                'id' => 4,
1169
                'label' => $faker->name,
1170
                'amount' => 10,
1171
                'type' => 'expense',
1172
            ],
1173
        ];
1174
        $this->crudPanel->update($entry->id, $inputData);
1175
1176
        $freshIncomes = $entry->fresh()->incomes;
1177
        $freshExpenses = $entry->fresh()->expenses;
1178
        $this->assertCount(2, $freshExpenses);
1179
        $this->assertCount(1, $freshIncomes);
1180
        $this->assertEquals(2, $freshIncomes->first()->id);
1181
1182
        $inputData['expenses'] = [];
1183
        $this->crudPanel->update($entry->id, $inputData);
1184
1185
        $freshIncomes = $entry->fresh()->incomes;
1186
        $freshExpenses = $entry->fresh()->expenses;
1187
        $this->assertCount(0, $freshExpenses);
1188
        $this->assertCount(1, $freshIncomes);
1189
    }
1190
1191
    public function testHasManySelectableRelationshipWithFallbackId()
1192
    {
1193
        $this->crudPanel->setModel(User::class);
1194
        $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

1194
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1195
                          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...
1195
        $this->crudPanel->addField([
1196
            'name' => 'planets',
1197
            'fallback_id' => 0,
1198
            'force_delete' => false,
1199
        ], '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

1199
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1200
                          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...
1200
1201
        $faker = Factory::create();
1202
        $inputData = [
1203
            'name' => $faker->name,
1204
            'email' => $faker->safeEmail,
1205
            'password' => Hash::make($faker->password()),
1206
            'remember_token' => null,
1207
            'planets' => [1, 2],
1208
        ];
1209
1210
        $entry = $this->crudPanel->create($inputData);
1211
1212
        $this->assertCount(2, $entry->planets);
1213
1214
        $inputData['planets'] = [2];
1215
1216
        $this->crudPanel->update($entry->id, $inputData);
1217
1218
        $this->assertCount(1, $entry->fresh()->planets);
1219
1220
        $planets = Planet::all();
1221
        $this->assertCount(2, $planets);
1222
        $this->assertEquals(0, $planets->first()->user_id);
1223
    }
1224
1225
    public function testHasManySelectableRelationshipWithForceDelete()
1226
    {
1227
        $this->crudPanel->setModel(User::class);
1228
        $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

1228
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1229
                          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...
1229
        $this->crudPanel->addField([
1230
            'name' => 'planets',
1231
            'force_delete' => true,
1232
            'fallback_id' => false,
1233
        ], '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

1233
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1234
                          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...
1234
1235
        $faker = Factory::create();
1236
        $inputData = [
1237
            'name' => $faker->name,
1238
            'email' => $faker->safeEmail,
1239
            'password' => Hash::make($faker->password()),
1240
            'remember_token' => null,
1241
            'planets' => [1, 2],
1242
        ];
1243
1244
        $entry = $this->crudPanel->create($inputData);
1245
1246
        $this->assertCount(2, $entry->planets);
1247
1248
        $inputData['planets'] = [2];
1249
1250
        $this->crudPanel->update($entry->id, $inputData);
1251
1252
        $this->assertCount(1, $entry->fresh()->planets);
1253
1254
        $planets = Planet::all();
1255
        $this->assertCount(1, $planets);
1256
    }
1257
1258
    public function testHasManySelectableRelationshipNonNullableForeignKeyAndDefaultInDatabase()
1259
    {
1260
        $this->crudPanel->setModel(User::class);
1261
        $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

1261
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1262
                          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...
1262
        $this->crudPanel->addField([
1263
            'name' => 'comets',
1264
            'force_delete' => false,
1265
            'fallback_id' => false,
1266
        ], '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

1266
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1267
                          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...
1267
1268
        $faker = Factory::create();
1269
        $inputData = [
1270
            'name' => $faker->name,
1271
            'email' => $faker->safeEmail,
1272
            'password' => Hash::make($faker->password()),
1273
            'remember_token' => null,
1274
            'comets' => [1, 2],
1275
        ];
1276
1277
        $entry = $this->crudPanel->create($inputData);
1278
1279
        $this->assertCount(2, $entry->comets);
1280
1281
        $inputData['comets'] = [2];
1282
1283
        $this->crudPanel->update($entry->id, $inputData);
1284
1285
        $this->assertCount(1, $entry->fresh()->comets);
1286
1287
        $comets = Comet::all();
1288
        $this->assertCount(2, $comets);
1289
        $this->assertEquals(0, $comets->first()->user_id);
1290
    }
1291
1292
    public function testHasManySelectableRelationshipNonNullable()
1293
    {
1294
        $this->crudPanel->setModel(User::class);
1295
        $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

1295
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1296
                          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...
1296
        $this->crudPanel->addField([
1297
            'name' => 'planetsNonNullable',
1298
            'force_delete' => false,
1299
            'fallback_id' => false,
1300
        ], '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

1300
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1301
                          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...
1301
1302
        $faker = Factory::create();
1303
        $inputData = [
1304
            'name' => $faker->name,
1305
            'email' => $faker->safeEmail,
1306
            'password' => Hash::make($faker->password()),
1307
            'remember_token' => null,
1308
            'planetsNonNullable' => [1, 2],
1309
        ];
1310
1311
        $entry = $this->crudPanel->create($inputData);
1312
1313
        $this->assertCount(2, $entry->planetsNonNullable);
1314
1315
        $inputData['planetsNonNullable'] = null;
1316
1317
        $this->crudPanel->update($entry->id, $inputData);
1318
1319
        $this->assertCount(0, $entry->fresh()->planetsNonNullable);
1320
1321
        $planets = PlanetNonNullable::all();
1322
        $this->assertCount(0, $planets);
1323
    }
1324
1325
    public function testCreateHasManyRelationWithDelimitedNameSubfields()
1326
    {
1327
        $this->crudPanel->setModel(User::class);
1328
        $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

1328
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1329
                          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...
1329
        $this->crudPanel->addField([
1330
            'name' => 'universes',
1331
            'subfields' => [
1332
                [
1333
                    'name' => 'title',
1334
                ],
1335
                [
1336
                    'name' => 'start_date,end_date',
1337
                    'type' => 'date_range',
1338
                ],
1339
            ],
1340
        ], '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

1340
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1341
                          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...
1341
1342
        $faker = Factory::create();
1343
        $inputData = [
1344
            'name' => $faker->name,
1345
            'email' => $faker->safeEmail,
1346
            'password' => Hash::make($faker->password()),
1347
            'remember_token' => null,
1348
            'universes' => [
1349
                [
1350
                    'id' => null,
1351
                    'title' => 'this is the star 1 title',
1352
                    'start_date' => '2021-02-26',
1353
                    'end_date' => '2091-01-26',
1354
                ],
1355
                [
1356
                    'title' => 'this is the star 2 title',
1357
                    'end_date' => '2021-02-26',
1358
                    'start_date' => '2091-01-26',
1359
                ],
1360
            ],
1361
        ];
1362
1363
        $entry = $this->crudPanel->create($inputData);
1364
1365
        $this->assertCount(2, $entry->universes);
1366
1367
        $this->assertEquals($inputData['universes'][0]['start_date'], $entry->universes()->first()->start_date);
0 ignored issues
show
Bug introduced by
The property start_date does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property start_date does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
1368
        $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...
1369
        $this->assertEquals($inputData['universes'][1]['end_date'], $entry->universes()->find(2)->end_date);
1370
        $this->assertEquals($inputData['universes'][1]['start_date'], $entry->universes()->find(2)->start_date);
1371
    }
1372
1373
    public function testCreateHasOneRelationWithDelimitedNameSubfields()
1374
    {
1375
        $this->crudPanel->setModel(User::class);
1376
        $this->crudPanel->setOperation('create');
1377
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships);
1378
        $this->crudPanel->addField(
1379
            [
1380
                'name' => 'accountDetails',
1381
                'subfields' => [
1382
                    [
1383
                        'name' => 'nickname',
1384
                    ],
1385
                    [
1386
                        'name' => 'start_date,end_date',
1387
                    ],
1388
                    [
1389
                        'name' => 'profile_picture',
1390
                    ],
1391
                ],
1392
            ]);
1393
1394
        $faker = Factory::create();
1395
1396
        $inputData = [
1397
            'name' => $faker->name,
1398
            'email' => $faker->safeEmail,
1399
            'password' => Hash::make($faker->password()),
1400
            'remember_token' => null,
1401
            'roles' => [1, 2],
1402
            'accountDetails' => [
1403
                [
1404
                    'nickname' => 'i_have_has_one',
1405
                    'profile_picture' => 'ohh my picture 1.jpg',
1406
                    'start_date' => '2021-02-26',
1407
                    'end_date' => '2091-01-26',
1408
                ],
1409
            ],
1410
        ];
1411
1412
        $entry = $this->crudPanel->create($inputData);
1413
        $account_details = $entry->accountDetails()->first();
1414
1415
        $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...
1416
        $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...elations\HasManyThrough.
Loading history...
Bug introduced by
The property end_date does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
1417
    }
1418
1419
    public function testBelongsToManyWithDelimitedNameSubfields()
1420
    {
1421
        $this->crudPanel->setModel(User::class);
1422
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships);
1423
        $this->crudPanel->addField([
1424
            'name' => 'superArticles',
1425
            'subfields' => [
1426
                [
1427
                    'name' => 'notes',
1428
                ],
1429
                [
1430
                    'name' => 'start_date,end_date',
1431
                ],
1432
            ],
1433
        ]);
1434
1435
        $faker = Factory::create();
1436
        $articleData = [
1437
            'content' => $faker->text(),
1438
            'tags' => $faker->words(3, true),
1439
            'user_id' => 1,
1440
        ];
1441
1442
        $article = Article::create($articleData);
1443
1444
        $inputData = [
1445
            'name' => $faker->name,
1446
            'email' => $faker->safeEmail,
1447
            'password' => Hash::make($faker->password()),
1448
            'remember_token' => null,
1449
            'superArticles' => [
1450
                [
1451
                    '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...
1452
                    'notes' => 'my first article note',
1453
                    'start_date' => '2021-02-26',
1454
                    'end_date' => '2091-01-26',
1455
                ],
1456
            ],
1457
        ];
1458
1459
        $entry = $this->crudPanel->create($inputData);
1460
1461
        $this->assertCount(1, $entry->fresh()->superArticles);
1462
        $superArticle = $entry->fresh()->superArticles->first();
1463
        $this->assertEquals($superArticle->pivot->start_date, '2021-02-26');
1464
        $this->assertEquals($superArticle->pivot->end_date, '2091-01-26');
1465
1466
        $this->crudPanel->getUpdateFields($superArticle->id);
1467
    }
1468
1469
    public function testItCanCreateMorphToFieldsStructure()
1470
    {
1471
        $this->crudPanel->setModel(Star::class);
1472
        $this->crudPanel->addField([
1473
            'name' => 'starable',
1474
            'morphOptions' => [
1475
                ['Backpack\CRUD\Tests\config\Models\User', 'User'],
1476
            ],
1477
        ]);
1478
1479
        $this->assertCount(2, $this->crudPanel->fields()['starable']['subfields']);
1480
1481
        [$morphTypeField, $morphIdField] = $this->crudPanel->fields()['starable']['subfields'];
1482
1483
        $this->assertTrue($morphTypeField['name'] === 'starable_type');
1484
        $this->assertTrue($morphIdField['name'] === 'starable_id');
1485
    }
1486
1487
    public function testIPreventsAddingRepeateadMorphOptions()
1488
    {
1489
        $this->crudPanel->setModel(Star::class);
1490
        $this->expectException(\Exception::class);
1491
1492
        $this->crudPanel->addField([
1493
            'name' => 'starable',
1494
            'morphOptions' => [
1495
                ['Backpack\CRUD\Tests\config\Models\User', 'User'],
1496
                ['Backpack\CRUD\Tests\config\Models\User', 'User'],
1497
            ],
1498
        ]);
1499
    }
1500
1501
    public function testItThrowsErrorIfStringIsNotOnMorphMap()
1502
    {
1503
        $this->crudPanel->setModel(Star::class);
1504
        $this->expectException(\Exception::class);
1505
1506
        $this->crudPanel->addField([
1507
            'name' => 'starable',
1508
            'morphOptions' => [
1509
                ['somethingThatDoesNotExist'],
1510
            ],
1511
        ]);
1512
    }
1513
1514
    public function testItCanAddTheOptionsFromTheMorphMap()
1515
    {
1516
        $this->crudPanel->setModel(Star::class);
1517
1518
        Relation::morphMap([
1519
            'user' => 'Backpack\CRUD\Tests\config\Models\User',
1520
        ]);
1521
1522
        $this->crudPanel->addField([
1523
            'name' => 'starable',
1524
            'morphOptions' => [
1525
                ['user'],
1526
            ],
1527
        ]);
1528
1529
        [$morphTypeField, $morphIdField] = $this->crudPanel->fields()['starable']['subfields'];
1530
        $this->assertEquals(['user' => 'Backpack\CRUD\Tests\config\Models\User'], $morphTypeField['morphMap']);
1531
    }
1532
1533
    public function testItThrowsErrorIfDuplicateMorphMapName()
1534
    {
1535
        $this->crudPanel->setModel(Star::class);
1536
        $this->expectException(\Exception::class);
1537
1538
        Relation::morphMap([
1539
            'user' => 'Backpack\CRUD\Tests\config\Models\User',
1540
        ]);
1541
1542
        $this->crudPanel->addField([
1543
            'name' => 'starable',
1544
            'morphOptions' => [
1545
                ['user'],
1546
                ['user'],
1547
            ],
1548
        ]);
1549
    }
1550
}
1551