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 (#5538)
by Pedro
16:57 queued 02:10
created

testHasManySelectableRelationshipWithoutForceDelete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 33
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 22
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 33
rs 9.568
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
508
        $this->assertCount(1, $entry->fresh()->superArticles);
509
        $this->assertEquals('my first article note', $entry->fresh()->superArticles->first()->pivot->notes);
510
    }
511
512
    public function testBelongsToManyWithMultipleSameRelationIdAndPivotDataRelationship()
513
    {
514
        $inputData = $this->getPivotInputData(['superArticlesDuplicates' => [
515
            [
516
                'superArticlesDuplicates' => 1,
517
                'notes' => 'my first article note',
518
                'id' => null,
519
            ],
520
            [
521
                'superArticlesDuplicates' => 1,
522
                'notes' => 'my second article note',
523
                'id' => null,
524
            ],
525
            [
526
                'superArticlesDuplicates' => 2,
527
                'notes' => 'my first article2 note',
528
                'id' => null,
529
            ],
530
        ],
531
        ], true, true);
532
533
        $entry = $this->crudPanel->create($inputData);
534
        $relationField = $this->crudPanel->getUpdateFields($entry->id)['superArticlesDuplicates'];
535
536
        $this->assertCount(3, $relationField['value']);
537
538
        $entry = $entry->fresh();
539
540
        $this->assertCount(3, $entry->superArticlesDuplicates);
541
        $this->assertEquals('my first article note', $entry->superArticles->first()->pivot->notes);
0 ignored issues
show
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

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

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

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

Loading history...
Bug introduced by
The 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

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

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

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

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

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

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

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

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

Loading history...
Bug introduced by
The property pivot does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property notes does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The property notes does not seem to exist on Illuminate\Database\Eloquent\Relations\Relation.
Loading history...
Bug introduced by
The property notes does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property notes does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
542
        $this->assertEquals('my second article note', $entry->superArticles[1]->pivot->notes);
543
        $this->assertEquals('my first article2 note', $entry->superArticles[2]->pivot->notes);
544
545
        $inputData = $this->getPivotInputData(['superArticlesDuplicates' => [
546
            [
547
                'superArticlesDuplicates' => 1,
548
                'notes' => 'my first article note updated',
549
                'id' => 1,
550
            ],
551
            [
552
                'superArticlesDuplicates' => 1,
553
                'notes' => 'my second article note updated',
554
                'id' => 2,
555
            ],
556
            [
557
                'superArticlesDuplicates' => 2,
558
                'notes' => 'my first article2 note updated',
559
                'id' => 3,
560
            ],
561
        ],
562
        ], false, true);
563
564
        $entry = $this->crudPanel->update($entry->id, $inputData);
565
        $relationField = $this->crudPanel->getUpdateFields($entry->id)['superArticlesDuplicates'];
566
        $this->assertCount(3, $relationField['value']);
567
568
        $entry = $entry->fresh();
569
570
        $this->assertCount(3, $entry->superArticlesDuplicates);
571
        $this->assertEquals('my first article note updated', $entry->superArticles[0]->pivot->notes);
572
        $this->assertEquals('my second article note updated', $entry->superArticles[1]->pivot->notes);
573
        $this->assertEquals('my first article2 note updated', $entry->superArticles[2]->pivot->notes);
574
    }
575
576
    public function testBelongsToManyAlwaysSaveSinglePivotWhenMultipleNotAllowed()
577
    {
578
        $inputData = $this->getPivotInputData(['superArticlesDuplicates' => [
579
            [
580
                'superArticlesDuplicates' => 1,
581
                'notes' => 'my first article note',
582
                'id' => null,
583
            ],
584
            [
585
                'superArticlesDuplicates' => 1,
586
                'notes' => 'my second article note',
587
                'id' => null,
588
            ],
589
            [
590
                'superArticlesDuplicates' => 2,
591
                'notes' => 'my first article2 note',
592
                'id' => null,
593
            ],
594
        ],
595
        ]);
596
597
        $entry = $this->crudPanel->create($inputData);
598
        $relationField = $this->crudPanel->getUpdateFields($entry->id)['superArticlesDuplicates'];
599
600
        $this->assertCount(2, $relationField['value']);
601
602
        $entry = $entry->fresh();
603
604
        $this->assertCount(2, $entry->superArticlesDuplicates);
605
        $this->assertEquals('my second article note', $entry->superArticles[0]->pivot->notes);
606
        $this->assertEquals('my first article2 note', $entry->superArticles[1]->pivot->notes);
607
    }
608
609
    public function testBelongsToManyDeletesPivotData()
610
    {
611
        $inputData = $this->getPivotInputData(['superArticlesDuplicates' => [
612
            [
613
                'superArticlesDuplicates' => 1,
614
                'notes' => 'my first article note',
615
                'id' => null,
616
            ],
617
            [
618
                'superArticlesDuplicates' => 1,
619
                'notes' => 'my second article note',
620
                'id' => null,
621
            ],
622
            [
623
                'superArticlesDuplicates' => 2,
624
                'notes' => 'my first article2 note',
625
                'id' => null,
626
            ],
627
        ],
628
        ], true, true);
629
630
        $entry = $this->crudPanel->create($inputData);
631
        $relationField = $this->crudPanel->getUpdateFields($entry->id)['superArticlesDuplicates'];
632
633
        $this->assertCount(3, $relationField['value']);
634
635
        $inputData = $this->getPivotInputData(['superArticlesDuplicates' => [
636
            [
637
                'superArticlesDuplicates' => 1,
638
                'notes' => 'new first article note',
639
                'id' => null,
640
            ],
641
            [
642
                'superArticlesDuplicates' => 1,
643
                'notes' => 'my second article note updated',
644
                'id' => 2,
645
            ],
646
            [
647
                'superArticlesDuplicates' => 3,
648
                'notes' => 'my first article2 note updated',
649
                'id' => 3,
650
            ],
651
        ],
652
        ], false, true);
653
654
        $entry = $this->crudPanel->update($entry->id, $inputData);
655
        $relationField = $this->crudPanel->getUpdateFields($entry->id)['superArticlesDuplicates'];
656
        $this->assertCount(3, $relationField['value']);
657
658
        $entry = $entry->fresh();
659
660
        $this->assertCount(3, $entry->superArticlesDuplicates);
661
        $this->assertEquals('new first article note', $entry->superArticles[2]->pivot->notes);
662
        $this->assertEquals('my second article note updated', $entry->superArticles[0]->pivot->notes);
663
        $this->assertEquals('my first article2 note updated', $entry->superArticles[1]->pivot->notes);
664
    }
665
666
    public function testCreateHasOneWithNestedRelationsRepeatableInterface()
667
    {
668
        $this->crudPanel->setModel(User::class);
669
        $this->crudPanel->setOperation('create');
670
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships);
671
        $this->crudPanel->addField(
672
            [
673
                'name' => 'accountDetails',
674
                'subfields' => [
675
                    [
676
                        'name' => 'nickname',
677
                    ],
678
                    [
679
                        'name' => 'profile_picture',
680
                    ],
681
                    [
682
                        'name' => 'article',
683
                    ],
684
                    [
685
                        'name' => 'addresses',
686
                        'subfields' => [
687
                            [
688
                                'name' => 'bang',
689
                            ],
690
                            [
691
                                'name' => 'street',
692
                            ],
693
                            [
694
                                'name' => 'number',
695
                            ],
696
                        ],
697
                    ],
698
                    [
699
                        'name' => 'bangs',
700
                    ],
701
                    [
702
                        'name' => 'bangsPivot',
703
                        'subfields' => [
704
                            [
705
                                'name' => 'pivot_field',
706
                            ],
707
                        ],
708
                    ],
709
                ],
710
            ]);
711
712
        $faker = Factory::create();
713
714
        $inputData = [
715
            'name' => $faker->name,
716
            'email' => $faker->safeEmail,
717
            'password' => Hash::make($faker->password()),
718
            'remember_token' => null,
719
            'roles' => [1, 2],
720
            'accountDetails' => [
721
                [
722
                    'nickname' => 'i_have_has_one',
723
                    'profile_picture' => 'ohh my picture 1.jpg',
724
                    'article' => 1,
725
                    'addresses' => [
726
                        [
727
                            'bang' => 1,
728
                            'street' => 'test',
729
                            'number' => 1,
730
                        ],
731
                        [
732
                            'bang' => 1,
733
                            'street' => 'test2',
734
                            'number' => 2,
735
                        ],
736
                    ],
737
                    'bangs' => [1, 2],
738
                    'bangsPivot' => [
739
                        ['bangsPivot' => 1, 'pivot_field' => 'test1'],
740
                        ['bangsPivot' => 2, 'pivot_field' => 'test2'],
741
                    ],
742
                ],
743
            ],
744
        ];
745
746
        $entry = $this->crudPanel->create($inputData);
747
        $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...
748
        $account_details = $entry->accountDetails()->first();
749
750
        $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...
751
        $this->assertEquals($account_details->addresses->count(), 2);
0 ignored issues
show
Bug introduced by
The property addresses does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property addresses does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The method count() does not exist on null. ( Ignorable by Annotation )

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

751
        $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

751
        $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

751
        $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...
752
        $this->assertEquals($account_details->addresses->first()->city, 1);
0 ignored issues
show
Bug introduced by
The property city does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The property city does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
753
        $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...
754
        $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...
755
        $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...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property name does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The method first() does not exist on null. ( Ignorable by Annotation )

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

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

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

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

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

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

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

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

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

Loading history...
Bug introduced by
The property bangsPivot does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
757
        $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 does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property pivot_field does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property pivot_field does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
Bug introduced by
The property pivot_field does not seem to exist on Illuminate\Database\Eloquent\Relations\Relation.
Loading history...
Bug introduced by
The property pivot does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
758
    }
759
760
    public function testCreateBelongsToFake()
761
    {
762
        $belongsToField = [   // select_grouped
763
            'label' => 'Select_grouped',
764
            'type' => 'select_grouped', //https://github.com/Laravel-Backpack/CRUD/issues/502
765
            'name' => 'bang_relation_field',
766
            'fake' => true,
767
            'entity' => 'bang',
768
            'model' => 'Backpack\CRUD\Tests\config\Models\Bang',
769
            'attribute' => 'title',
770
            'group_by' => 'category', // the relationship to entity you want to use for grouping
771
            'group_by_attribute' => 'name', // the attribute on related model, that you want shown
772
            'group_by_relationship_back' => 'articles', // relationship from related model back to this model
773
            'tab' => 'Selects',
774
            'wrapperAttributes' => ['class' => 'form-group col-md-6'],
775
        ];
776
777
        $this->crudPanel->setModel(User::class);
778
        $this->crudPanel->setOperation('create');
779
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships);
780
        $this->crudPanel->addField($belongsToField);
781
782
        $faker = Factory::create();
783
784
        $inputData = [
785
            'name' => $faker->name,
786
            'email' => $faker->safeEmail,
787
            'password' => Hash::make($faker->password()),
788
            'remember_token' => null,
789
            'bang_relation_field' => 1,
790
        ];
791
792
        $entry = $this->crudPanel->create($inputData);
793
        $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...
794
        $this->crudPanel->entry = $entry->withFakes();
795
        $this->assertEquals($entry->bang_relation_field, 1);
796
    }
797
798
    public function testCreateHasOneWithNestedRelations()
799
    {
800
        $this->crudPanel->setModel(User::class);
801
        $this->crudPanel->setOperation('create');
802
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships);
803
        $this->crudPanel->addFields([
804
            [
805
                'name' => 'accountDetails.nickname',
806
            ],
807
            [
808
                'name' => 'accountDetails.profile_picture',
809
            ],
810
            [
811
                'name' => 'accountDetails.article',
812
            ],
813
            [
814
                'name' => 'accountDetails.addresses',
815
                'subfields' => [
816
                    [
817
                        'name' => 'city',
818
                        'entity' => 'bang',
819
                    ],
820
                    [
821
                        'name' => 'street',
822
                    ],
823
                    [
824
                        'name' => 'number',
825
                    ],
826
                ],
827
            ],
828
            [
829
                'name' => 'accountDetails.bangs',
830
            ],
831
            [
832
                'name' => 'accountDetails.bangsPivot',
833
                'subfields' => [
834
                    [
835
                        'name' => 'pivot_field',
836
                    ],
837
                ],
838
            ],
839
        ]);
840
841
        $faker = Factory::create();
842
843
        $inputData = [
844
            'name' => $faker->name,
845
            'email' => $faker->safeEmail,
846
            'password' => Hash::make($faker->password()),
847
            'remember_token' => null,
848
            'roles' => [1, 2],
849
            'accountDetails' => [
850
                'nickname' => 'i_have_has_one',
851
                'profile_picture' => 'ohh my picture 1.jpg',
852
                'article' => 1,
853
                'addresses' => [
854
                    [
855
                        'city' => 1,
856
                        'street' => 'test',
857
                        'number' => 1,
858
                    ],
859
                    [
860
                        'city' => 2,
861
                        'street' => 'test2',
862
                        'number' => 2,
863
                    ],
864
                ],
865
                'bangs' => [1, 2],
866
                'bangsPivot' => [
867
                    ['bangsPivot' => 1, 'pivot_field' => 'test1'],
868
                    ['bangsPivot' => 2, 'pivot_field' => 'test2'],
869
                ],
870
            ],
871
        ];
872
873
        $entry = $this->crudPanel->create($inputData);
874
        $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...
875
        $account_details = $entry->accountDetails()->first();
876
877
        $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...
878
        $this->assertEquals($account_details->addresses->count(), 2);
0 ignored issues
show
Bug introduced by
The property addresses does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The property addresses does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
879
        $this->assertEquals($account_details->addresses->first()->bang->id, 1);
0 ignored issues
show
Bug introduced by
The property id does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The property id does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property id does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
Bug introduced by
The property bang does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The property bang does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property id does not seem to exist on Illuminate\Database\Eloquent\Relations\Relation.
Loading history...
880
        $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...
881
        $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...
882
        $this->assertEquals($account_details->bangs->first()->name, Bang::find(1)->name);
0 ignored issues
show
Bug introduced by
The property bangs does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property name does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The property name does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property name does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
Bug introduced by
The property bangs does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
883
        $this->assertEquals($account_details->bangsPivot->count(), 2);
0 ignored issues
show
Bug introduced by
The property bangsPivot does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The property bangsPivot does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
884
        $this->assertEquals($account_details->bangsPivot->first()->pivot->pivot_field, 'test1');
0 ignored issues
show
Bug introduced by
The property pivot_field does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property pivot_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\Eloquent\Relations\Relation.
Loading history...
Bug introduced by
The property pivot_field does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
Bug introduced by
The property pivot does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property pivot does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
885
886
        // Now test the remove process
887
888
        $inputData = [
889
            'name' => $faker->name,
890
            'email' => $faker->safeEmail,
891
            'password' => Hash::make($faker->password()),
892
            'remember_token' => null,
893
            'roles' => [1, 2],
894
            'accountDetails' => [
895
                'nickname' => 'i_have_has_one',
896
                'profile_picture' => 'ohh my picture 1.jpg',
897
                'article' => 1,
898
                'addresses' => [ // HasOne is tested in other test function
899
                    [
900
                        'city' => 2,
901
                        'street' => 'test',
902
                        'number' => 1,
903
                    ],
904
                    [
905
                        'city' => 1,
906
                        'street' => 'test2',
907
                        'number' => 2,
908
                    ],
909
                ],
910
                'bangs' => [],
911
                'bangsPivot' => [],
912
            ],
913
        ];
914
915
        $entry = $this->crudPanel->update($entry->id, $inputData);
916
        $account_details = $entry->accountDetails()->first();
917
        $this->assertEquals($account_details->addresses->count(), 2);
918
        $this->assertEquals($account_details->addresses->first()->bang->id, 2);
919
        $this->assertEquals($account_details->bangs->count(), 0);
920
        $this->assertEquals($account_details->bangsPivot->count(), 0);
921
    }
922
923
    public function testMorphOneRelationship()
924
    {
925
        $this->crudPanel->setModel(User::class);
926
        $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

926
        $this->crudPanel->/** @scrutinizer ignore-call */ 
927
                          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...
927
        $this->crudPanel->addField([
928
            'name' => 'comment.text',
929
        ], '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

929
        $this->crudPanel->/** @scrutinizer ignore-call */ 
930
                          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...
930
931
        $faker = Factory::create();
932
        $inputData = [
933
            'name' => $faker->name,
934
            'email' => $faker->safeEmail,
935
            'password' => Hash::make($faker->password()),
936
            'remember_token' => null,
937
            'comment' => [
938
                'text' => 'some test comment text',
939
            ],
940
        ];
941
942
        $entry = $this->crudPanel->create($inputData);
943
        $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...
944
945
        $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...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...
Bug introduced by
The property text does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
946
947
        $inputData['comment']['text'] = 'updated comment text';
948
949
        $this->crudPanel->update($entry->id, $inputData);
950
951
        $this->assertEquals($inputData['comment']['text'], $entry->fresh()->comment->text);
952
    }
953
954
    public function testMorphManyCreatableRelationship()
955
    {
956
        $this->crudPanel->setModel(User::class);
957
        $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

957
        $this->crudPanel->/** @scrutinizer ignore-call */ 
958
                          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...
958
        $this->crudPanel->addField([
959
            'name' => 'stars',
960
            'subfields' => [
961
                [
962
                    'name' => 'title',
963
                ],
964
            ],
965
        ], '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

965
        $this->crudPanel->/** @scrutinizer ignore-call */ 
966
                          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...
966
967
        $faker = Factory::create();
968
        $inputData = [
969
            'name' => $faker->name,
970
            'email' => $faker->safeEmail,
971
            'password' => Hash::make($faker->password()),
972
            'remember_token' => null,
973
            'stars' => [
974
                [
975
                    'id' => null,
976
                    'title' => 'this is the star 1 title',
977
                ],
978
                [
979
                    'id' => null,
980
                    'title' => 'this is the star 2 title',
981
                ],
982
            ],
983
        ];
984
985
        $entry = $this->crudPanel->create($inputData);
986
        $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...
987
988
        $this->assertCount(2, $entry->stars);
989
990
        $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...
991
992
        $inputData['stars'] = [
993
            [
994
                'id' => 1,
995
                'title' => 'only one star with changed title',
996
            ],
997
        ];
998
999
        $this->crudPanel->update($entry->id, $inputData);
1000
1001
        $this->assertCount(1, $entry->fresh()->stars);
1002
1003
        $this->assertEquals($inputData['stars'][0]['title'], $entry->fresh()->stars->first()->title);
1004
        $this->assertEquals($inputData['stars'][0]['id'], $entry->fresh()->stars->first()->id);
1005
    }
1006
1007
    public function testHasManyCreatableRelationship()
1008
    {
1009
        $this->crudPanel->setModel(User::class);
1010
        $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

1010
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1011
                          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...
1011
        $this->crudPanel->addField([
1012
            'name' => 'universes',
1013
            'subfields' => [
1014
                [
1015
                    'name' => 'title',
1016
                ],
1017
            ],
1018
        ], '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

1018
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1019
                          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...
1019
1020
        $faker = Factory::create();
1021
        $inputData = [
1022
            'name' => $faker->name,
1023
            'email' => $faker->safeEmail,
1024
            'password' => Hash::make($faker->password()),
1025
            'remember_token' => null,
1026
            'universes' => [
1027
                [
1028
                    'id' => null,
1029
                    'title' => 'this is the star 1 title',
1030
                ],
1031
                [
1032
                    'title' => 'this is the star 2 title',
1033
                ],
1034
            ],
1035
        ];
1036
1037
        $entry = $this->crudPanel->create($inputData);
1038
        $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...
1039
1040
        $this->assertCount(2, $entry->universes);
1041
1042
        $this->assertEquals($inputData['universes'][0]['title'], $entry->universes()->first()->title);
0 ignored issues
show
Bug introduced by
The property title does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The property title does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
1043
1044
        $inputData['universes'] = [
1045
            [
1046
                'id' => 1,
1047
                'title' => 'star 1 with changed title',
1048
            ],
1049
            [
1050
                'id' => 2,
1051
                'title' => 'star 2 with changed title',
1052
            ],
1053
        ];
1054
1055
        $this->crudPanel->update($entry->id, $inputData);
1056
1057
        $universes = $entry->fresh()->universes;
1058
        $this->assertCount(2, $universes);
1059
        $this->assertEquals([1, 2], $universes->pluck('id')->toArray());
1060
1061
        $inputData['universes'] = [
1062
            [
1063
                'id' => 1,
1064
                'title' => 'only one star with changed title',
1065
            ],
1066
        ];
1067
1068
        $this->crudPanel->update($entry->id, $inputData);
1069
1070
        $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

1070
        $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...
1071
        $this->assertEquals($inputData['universes'][0]['id'], $entry->fresh()->universes->first()->id);
0 ignored issues
show
Bug introduced by
The property id does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property id does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
1072
        $this->assertEquals(1, Universe::all()->count());
1073
1074
        $inputData['universes'] = [
1075
            [
1076
                'id' => null,
1077
                'title' => 'new star 3',
1078
            ],
1079
        ];
1080
1081
        $this->crudPanel->update($entry->id, $inputData);
1082
1083
        $this->assertEquals($inputData['universes'][0]['title'], $entry->fresh()->universes->first()->title);
1084
        $this->assertEquals(3, $entry->fresh()->universes->first()->id);
1085
        $this->assertEquals(1, Universe::all()->count());
1086
1087
        $inputData['universes'] = null;
1088
1089
        $this->crudPanel->update($entry->id, $inputData);
1090
1091
        $this->assertEquals(0, count($entry->fresh()->universes));
1092
        $this->assertEquals(0, Universe::all()->count());
1093
    }
1094
1095
    public function testHasManySelectableRelationshipWithoutForceDelete()
1096
    {
1097
        $this->crudPanel->setModel(User::class);
1098
        $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

1098
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1099
                          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...
1099
        $this->crudPanel->addField([
1100
            'name' => 'planets',
1101
            'force_delete' => false,
1102
            'fallback_id' => false,
1103
        ], '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

1103
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1104
                          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...
1104
1105
        $faker = Factory::create();
1106
        $inputData = [
1107
            'name' => $faker->name,
1108
            'email' => $faker->safeEmail,
1109
            'password' => Hash::make($faker->password()),
1110
            'remember_token' => null,
1111
            'planets' => [1, 2],
1112
        ];
1113
1114
        $entry = $this->crudPanel->create($inputData);
1115
        $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...
1116
1117
        $this->assertCount(2, $entry->planets);
1118
1119
        $inputData['planets'] = [1];
1120
1121
        $this->crudPanel->update($entry->id, $inputData);
1122
1123
        $this->assertCount(1, $entry->fresh()->planets);
1124
1125
        $planets = Planet::all();
1126
1127
        $this->assertCount(2, $planets);
1128
    }
1129
1130
    public function testHasManySelectableRelationshipRemoveAllRelations()
1131
    {
1132
        $this->crudPanel->setModel(User::class);
1133
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both');
0 ignored issues
show
Unused Code introduced by
The call to Backpack\CRUD\app\Librar...\CrudPanel::addFields() has too many arguments starting with 'both'. ( Ignorable by Annotation )

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Loading history...
1168
        $this->crudPanel->addField([
1169
            'name' => 'incomes',
1170
            'subfields' => [
1171
                [
1172
                    'name' => 'label',
1173
                    'type' => 'text',
1174
                ],
1175
                [
1176
                    'name' => 'type',
1177
                    'type' => 'hidden',
1178
                    'value' => 'income',
1179
                ],
1180
                [
1181
                    'name' => 'amount',
1182
                    'type' => 'number',
1183
                ],
1184
            ],
1185
        ], '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

1185
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1186
                          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...
1186
        $this->crudPanel->addField([
1187
            'name' => 'expenses',
1188
            'subfields' => [
1189
                [
1190
                    'name' => 'label',
1191
                    'type' => 'text',
1192
                ],
1193
                [
1194
                    'name' => 'type',
1195
                    'type' => 'hidden',
1196
                    'value' => 'expense',
1197
                ],
1198
                [
1199
                    'name' => 'amount',
1200
                    'type' => 'number',
1201
                ],
1202
            ],
1203
        ], 'both');
1204
1205
        $faker = Factory::create();
1206
        $inputData = [
1207
            'name' => $faker->name,
1208
            'email' => $faker->safeEmail,
1209
            'password' => Hash::make($faker->password()),
1210
            'remember_token' => null,
1211
            'incomes' => [
1212
                [
1213
                    'label' => $faker->name,
1214
                    'amount' => 33,
1215
                    'type' => 'income',
1216
                ],
1217
                [
1218
                    'label' => $faker->name,
1219
                    'amount' => 22,
1220
                    'type' => 'income',
1221
                ],
1222
            ],
1223
            'expenses' => [
1224
                [
1225
                    'label' => $faker->name,
1226
                    'amount' => 44,
1227
                    'type' => 'expense',
1228
                ],
1229
                [
1230
                    'label' => $faker->name,
1231
                    'amount' => 10,
1232
                    'type' => 'expense',
1233
                ],
1234
            ],
1235
        ];
1236
        $entry = $this->crudPanel->create($inputData);
1237
1238
        $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

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

1239
        /** @scrutinizer ignore-call */ 
1240
        $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...
1240
        $this->assertCount(2, $entry->expenses);
1241
        $this->assertCount(2, $entry->incomes);
1242
        $this->assertEquals(44, $entry->expenses->first()->amount);
1243
        $this->assertEquals(33, $entry->incomes->first()->amount);
1244
1245
        $inputData['incomes'] = [
1246
            [
1247
                'id' => 2,
1248
                'label' => $faker->name,
1249
                'amount' => 222,
1250
                'type' => 'income',
1251
            ],
1252
        ];
1253
        $inputData['expenses'] = [
1254
            [
1255
                'id' => 3,
1256
                'label' => $faker->name,
1257
                'amount' => 44,
1258
                'type' => 'expense',
1259
            ],
1260
            [
1261
                'id' => 4,
1262
                'label' => $faker->name,
1263
                'amount' => 10,
1264
                'type' => 'expense',
1265
            ],
1266
        ];
1267
        $this->crudPanel->update($entry->id, $inputData);
1268
1269
        $freshIncomes = $entry->fresh()->incomes;
1270
        $freshExpenses = $entry->fresh()->expenses;
1271
        $this->assertCount(2, $freshExpenses);
1272
        $this->assertCount(1, $freshIncomes);
1273
        $this->assertEquals(2, $freshIncomes->first()->id);
1274
1275
        $inputData['expenses'] = [];
1276
        $this->crudPanel->update($entry->id, $inputData);
1277
1278
        $freshIncomes = $entry->fresh()->incomes;
1279
        $freshExpenses = $entry->fresh()->expenses;
1280
        $this->assertCount(0, $freshExpenses);
1281
        $this->assertCount(1, $freshIncomes);
1282
    }
1283
1284
    public function testHasManySelectableRelationshipWithFallbackId()
1285
    {
1286
        $this->crudPanel->setModel(User::class);
1287
        $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

1287
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1288
                          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...
1288
        $this->crudPanel->addField([
1289
            'name' => 'planets',
1290
            'fallback_id' => 0,
1291
            'force_delete' => false,
1292
        ], '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

1292
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1293
                          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...
1293
1294
        $faker = Factory::create();
1295
        $inputData = [
1296
            'name' => $faker->name,
1297
            'email' => $faker->safeEmail,
1298
            'password' => Hash::make($faker->password()),
1299
            'remember_token' => null,
1300
            'planets' => [1, 2],
1301
        ];
1302
1303
        $entry = $this->crudPanel->create($inputData);
1304
1305
        $this->assertCount(2, $entry->planets);
1306
1307
        $inputData['planets'] = [2];
1308
1309
        $this->crudPanel->update($entry->id, $inputData);
1310
1311
        $this->assertCount(1, $entry->fresh()->planets);
1312
1313
        $planets = Planet::all();
1314
        $this->assertCount(2, $planets);
1315
        $this->assertEquals(0, $planets->first()->user_id);
1316
    }
1317
1318
    public function testHasManySelectableRelationshipWithForceDelete()
1319
    {
1320
        $this->crudPanel->setModel(User::class);
1321
        $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

1321
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1322
                          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...
1322
        $this->crudPanel->addField([
1323
            'name' => 'planets',
1324
            'force_delete' => true,
1325
            'fallback_id' => false,
1326
        ], '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

1326
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1327
                          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...
1327
1328
        $faker = Factory::create();
1329
        $inputData = [
1330
            'name' => $faker->name,
1331
            'email' => $faker->safeEmail,
1332
            'password' => Hash::make($faker->password()),
1333
            'remember_token' => null,
1334
            'planets' => [1, 2],
1335
        ];
1336
1337
        $entry = $this->crudPanel->create($inputData);
1338
1339
        $this->assertCount(2, $entry->planets);
1340
1341
        $inputData['planets'] = [2];
1342
1343
        $this->crudPanel->update($entry->id, $inputData);
1344
1345
        $this->assertCount(1, $entry->fresh()->planets);
1346
1347
        $planets = Planet::all();
1348
        $this->assertCount(1, $planets);
1349
    }
1350
1351
    public function testHasManySelectableRelationshipNonNullableForeignKeyAndDefaultInDatabase()
1352
    {
1353
        $this->crudPanel->setModel(User::class);
1354
        $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

1354
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1355
                          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...
1355
        $this->crudPanel->addField([
1356
            'name' => 'comets',
1357
            'force_delete' => false,
1358
            'fallback_id' => false,
1359
        ], '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

1359
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1360
                          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...
1360
1361
        $faker = Factory::create();
1362
        $inputData = [
1363
            'name' => $faker->name,
1364
            'email' => $faker->safeEmail,
1365
            'password' => Hash::make($faker->password()),
1366
            'remember_token' => null,
1367
            'comets' => [1, 2],
1368
        ];
1369
1370
        $entry = $this->crudPanel->create($inputData);
1371
1372
        $this->assertCount(2, $entry->comets);
1373
1374
        $inputData['comets'] = [2];
1375
1376
        $this->crudPanel->update($entry->id, $inputData);
1377
1378
        $this->assertCount(1, $entry->fresh()->comets);
1379
1380
        $comets = Comet::all();
1381
        $this->assertCount(2, $comets);
1382
        $this->assertEquals(0, $comets->first()->user_id);
1383
    }
1384
1385
    public function testHasManySelectableRelationshipNonNullable()
1386
    {
1387
        $this->crudPanel->setModel(User::class);
1388
        $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

1388
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1389
                          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...
1389
        $this->crudPanel->addField([
1390
            'name' => 'planetsNonNullable',
1391
            'force_delete' => false,
1392
            'fallback_id' => false,
1393
        ], '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

1393
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1394
                          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...
1394
1395
        $faker = Factory::create();
1396
        $inputData = [
1397
            'name' => $faker->name,
1398
            'email' => $faker->safeEmail,
1399
            'password' => Hash::make($faker->password()),
1400
            'remember_token' => null,
1401
            'planetsNonNullable' => [1, 2],
1402
        ];
1403
1404
        $entry = $this->crudPanel->create($inputData);
1405
1406
        $this->assertCount(2, $entry->planetsNonNullable);
1407
1408
        $inputData['planetsNonNullable'] = null;
1409
1410
        $this->crudPanel->update($entry->id, $inputData);
1411
1412
        $this->assertCount(0, $entry->fresh()->planetsNonNullable);
1413
1414
        $planets = PlanetNonNullable::all();
1415
        $this->assertCount(0, $planets);
1416
    }
1417
1418
    public function testCreateHasManyRelationWithDelimitedNameSubfields()
1419
    {
1420
        $this->crudPanel->setModel(User::class);
1421
        $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

1421
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1422
                          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...
1422
        $this->crudPanel->addField([
1423
            'name' => 'universes',
1424
            'subfields' => [
1425
                [
1426
                    'name' => 'title',
1427
                ],
1428
                [
1429
                    'name' => 'start_date,end_date',
1430
                    'type' => 'date_range',
1431
                ],
1432
            ],
1433
        ], '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

1433
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1434
                          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...
1434
1435
        $faker = Factory::create();
1436
        $inputData = [
1437
            'name' => $faker->name,
1438
            'email' => $faker->safeEmail,
1439
            'password' => Hash::make($faker->password()),
1440
            'remember_token' => null,
1441
            'universes' => [
1442
                [
1443
                    'id' => null,
1444
                    'title' => 'this is the star 1 title',
1445
                    'start_date' => '2021-02-26',
1446
                    'end_date' => '2091-01-26',
1447
                ],
1448
                [
1449
                    'title' => 'this is the star 2 title',
1450
                    'end_date' => '2021-02-26',
1451
                    'start_date' => '2091-01-26',
1452
                ],
1453
            ],
1454
        ];
1455
1456
        $entry = $this->crudPanel->create($inputData);
1457
1458
        $this->assertCount(2, $entry->universes);
1459
1460
        $this->assertEquals($inputData['universes'][0]['start_date'], $entry->universes()->first()->start_date);
0 ignored issues
show
Bug introduced by
The property start_date does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The property start_date does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
1461
        $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...
1462
        $this->assertEquals($inputData['universes'][1]['end_date'], $entry->universes()->find(2)->end_date);
1463
        $this->assertEquals($inputData['universes'][1]['start_date'], $entry->universes()->find(2)->start_date);
1464
    }
1465
1466
    public function testCreateHasOneRelationWithDelimitedNameSubfields()
1467
    {
1468
        $this->crudPanel->setModel(User::class);
1469
        $this->crudPanel->setOperation('create');
1470
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships);
1471
        $this->crudPanel->addField(
1472
            [
1473
                'name' => 'accountDetails',
1474
                'subfields' => [
1475
                    [
1476
                        'name' => 'nickname',
1477
                    ],
1478
                    [
1479
                        'name' => 'start_date,end_date',
1480
                    ],
1481
                    [
1482
                        'name' => 'profile_picture',
1483
                    ],
1484
                ],
1485
            ]);
1486
1487
        $faker = Factory::create();
1488
1489
        $inputData = [
1490
            'name' => $faker->name,
1491
            'email' => $faker->safeEmail,
1492
            'password' => Hash::make($faker->password()),
1493
            'remember_token' => null,
1494
            'roles' => [1, 2],
1495
            'accountDetails' => [
1496
                [
1497
                    'nickname' => 'i_have_has_one',
1498
                    'profile_picture' => 'ohh my picture 1.jpg',
1499
                    'start_date' => '2021-02-26',
1500
                    'end_date' => '2091-01-26',
1501
                ],
1502
            ],
1503
        ];
1504
1505
        $entry = $this->crudPanel->create($inputData);
1506
        $account_details = $entry->accountDetails()->first();
1507
1508
        $this->assertEquals($account_details->start_date, '2021-02-26');
0 ignored issues
show
Bug introduced by
The property start_date does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property start_date does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
1509
        $this->assertEquals($account_details->end_date, '2091-01-26');
0 ignored issues
show
Bug introduced by
The property end_date does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property end_date does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
1510
    }
1511
1512
    public function testBelongsToManyWithDelimitedNameSubfields()
1513
    {
1514
        $this->crudPanel->setModel(User::class);
1515
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships);
1516
        $this->crudPanel->addField([
1517
            'name' => 'superArticles',
1518
            'subfields' => [
1519
                [
1520
                    'name' => 'notes',
1521
                ],
1522
                [
1523
                    'name' => 'start_date,end_date',
1524
                ],
1525
            ],
1526
        ]);
1527
1528
        $faker = Factory::create();
1529
        $articleData = [
1530
            'content' => $faker->text(),
1531
            'tags' => $faker->words(3, true),
1532
            'user_id' => 1,
1533
        ];
1534
1535
        $article = Article::create($articleData);
1536
1537
        $inputData = [
1538
            'name' => $faker->name,
1539
            'email' => $faker->safeEmail,
1540
            'password' => Hash::make($faker->password()),
1541
            'remember_token' => null,
1542
            'superArticles' => [
1543
                [
1544
                    '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...
1545
                    'notes' => 'my first article note',
1546
                    'start_date' => '2021-02-26',
1547
                    'end_date' => '2091-01-26',
1548
                ],
1549
            ],
1550
        ];
1551
1552
        $entry = $this->crudPanel->create($inputData);
1553
1554
        $this->assertCount(1, $entry->fresh()->superArticles);
1555
        $superArticle = $entry->fresh()->superArticles->first();
1556
        $this->assertEquals($superArticle->pivot->start_date, '2021-02-26');
1557
        $this->assertEquals($superArticle->pivot->end_date, '2091-01-26');
1558
1559
        $this->crudPanel->getUpdateFields($superArticle->id);
1560
    }
1561
1562
    public function testItCanCreateMorphToFieldsStructure()
1563
    {
1564
        $this->crudPanel->setModel(Star::class);
1565
        $this->crudPanel->addField([
1566
            'name' => 'starable',
1567
            'morphOptions' => [
1568
                ['Backpack\CRUD\Tests\config\Models\User', 'User'],
1569
            ],
1570
        ]);
1571
1572
        $this->assertCount(2, $this->crudPanel->fields()['starable']['subfields']);
1573
1574
        [$morphTypeField, $morphIdField] = $this->crudPanel->fields()['starable']['subfields'];
1575
1576
        $this->assertTrue($morphTypeField['name'] === 'starable_type');
1577
        $this->assertTrue($morphIdField['name'] === 'starable_id');
1578
    }
1579
1580
    public function testIPreventsAddingRepeateadMorphOptions()
1581
    {
1582
        $this->crudPanel->setModel(Star::class);
1583
        $this->expectException(\Exception::class);
1584
1585
        $this->crudPanel->addField([
1586
            'name' => 'starable',
1587
            'morphOptions' => [
1588
                ['Backpack\CRUD\Tests\config\Models\User', 'User'],
1589
                ['Backpack\CRUD\Tests\config\Models\User', 'User'],
1590
            ],
1591
        ]);
1592
    }
1593
1594
    public function testItThrowsErrorIfStringIsNotOnMorphMap()
1595
    {
1596
        $this->crudPanel->setModel(Star::class);
1597
        $this->expectException(\Exception::class);
1598
1599
        $this->crudPanel->addField([
1600
            'name' => 'starable',
1601
            'morphOptions' => [
1602
                ['somethingThatDoesNotExist'],
1603
            ],
1604
        ]);
1605
    }
1606
1607
    public function testItCanAddTheOptionsFromTheMorphMap()
1608
    {
1609
        $this->crudPanel->setModel(Star::class);
1610
1611
        Relation::morphMap([
1612
            'user' => 'Backpack\CRUD\Tests\config\Models\User',
1613
        ]);
1614
1615
        $this->crudPanel->addField([
1616
            'name' => 'starable',
1617
            'morphOptions' => [
1618
                ['user'],
1619
            ],
1620
        ]);
1621
1622
        [$morphTypeField, $morphIdField] = $this->crudPanel->fields()['starable']['subfields'];
1623
        $this->assertEquals(['user' => 'Backpack\CRUD\Tests\config\Models\User'], $morphTypeField['morphMap']);
1624
    }
1625
1626
    public function testItThrowsErrorIfDuplicateMorphMapName()
1627
    {
1628
        $this->crudPanel->setModel(Star::class);
1629
        $this->expectException(\Exception::class);
1630
1631
        Relation::morphMap([
1632
            'user' => 'Backpack\CRUD\Tests\config\Models\User',
1633
        ]);
1634
1635
        $this->crudPanel->addField([
1636
            'name' => 'starable',
1637
            'morphOptions' => [
1638
                ['user'],
1639
                ['user'],
1640
            ],
1641
        ]);
1642
    }
1643
1644
    private function getPivotInputData(array $pivotRelationData, bool $initCrud = true, bool $allowDuplicates = false)
1645
    {
1646
        $faker = Factory::create();
1647
1648
        if ($initCrud) {
1649
            $this->crudPanel->setModel(User::class);
1650
            $this->crudPanel->addFields($this->userInputFieldsNoRelationships);
1651
            $this->crudPanel->addField([
1652
                'name' => 'superArticlesDuplicates',
1653
                'allow_duplicate_pivots' => $allowDuplicates,
1654
                'pivot_key_name' => 'id',
1655
                'subfields' => [
1656
                    [
1657
                        'name' => 'notes',
1658
                    ],
1659
1660
                ],
1661
            ]);
1662
1663
            $article = Article::create([
0 ignored issues
show
Unused Code introduced by
The assignment to $article is dead and can be removed.
Loading history...
1664
                'content' => $faker->text(),
1665
                'tags' => $faker->words(3, true),
1666
                'user_id' => 1,
1667
            ]);
1668
            $article2 = Article::create([
0 ignored issues
show
Unused Code introduced by
The assignment to $article2 is dead and can be removed.
Loading history...
1669
                'content' => $faker->text(),
1670
                'tags' => $faker->words(3, true),
1671
                'user_id' => 1,
1672
            ]);
1673
        }
1674
1675
        $inputData = [
1676
            'name' => $faker->name,
1677
            'email' => $faker->safeEmail,
1678
            'password' => Hash::make($faker->password()),
1679
            'remember_token' => null,
1680
        ];
1681
1682
        return array_merge($inputData, $pivotRelationData);
1683
    }
1684
}
1685