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 (#5637)
by Pedro
41:06 queued 26:08
created

testCreateHasOneWithNestedRelationAsTheFirstField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 37
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

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

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

398
        $this->crudPanel->/** @scrutinizer ignore-call */ 
399
                          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...
399
400
        $faker = Factory::create();
401
        $inputData = [
402
            'name' => $faker->name,
403
            'email' => $faker->safeEmail,
404
            'password' => Hash::make($faker->password()),
405
            'remember_token' => null,
406
            'bills' => [1],
407
        ];
408
409
        $entry = $this->crudPanel->create($inputData);
410
411
        $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...
412
413
        $this->assertCount(1, $entry->bills);
414
415
        $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...
416
417
        $inputData['bills'] = [1, 2];
418
419
        $this->crudPanel->update($entry->id, $inputData);
420
421
        $this->assertCount(2, $entry->fresh()->bills);
422
423
        $this->assertEquals([1, 2], $entry->fresh()->bills->pluck('id')->toArray());
0 ignored issues
show
Bug introduced by
The method pluck() does not exist on Countable. It seems like you code against a sub-type of Countable such as RectorPrefix202408\Nette\Utils\ArrayList or Illuminate\Pagination\LengthAwarePaginator or Illuminate\Http\Resources\Json\ResourceCollection or Nette\Utils\Html or RectorPrefix202408\Nette\Iterators\CachingIterator or Illuminate\Pagination\CursorPaginator or Carbon\CarbonPeriod or Illuminate\Pagination\Paginator or Nette\Iterators\CachingIterator or Nette\Utils\ArrayList or Illuminate\Support\ViewErrorBag or RectorPrefix202408\Nette\Utils\Html or Illuminate\Support\Enumerable or Prologue\Alerts\AlertsMessageBag or RectorPrefix202408\Nette\Iterators\CachingIterator or Nette\Iterators\CachingIterator. ( Ignorable by Annotation )

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

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

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

434
        $this->crudPanel->/** @scrutinizer ignore-call */ 
435
                          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...
435
436
        $faker = Factory::create();
437
        $inputData = [
438
            'name' => $faker->name,
439
            'email' => $faker->safeEmail,
440
            'password' => Hash::make($faker->password()),
441
            'remember_token' => null,
442
            'recommends' => [
443
                [
444
                    'recommends' => 1,
445
                    'text' => 'my pivot recommend field',
446
                ],
447
            ],
448
        ];
449
450
        $entry = $this->crudPanel->create($inputData);
451
        $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...
452
453
        $this->assertCount(1, $entry->recommends);
454
455
        $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...
456
457
        $inputData['recommends'] = [
458
            [
459
                'recommends' => 2,
460
                'text' => 'I changed the recommend and the pivot text',
461
            ],
462
        ];
463
464
        $this->crudPanel->update($entry->id, $inputData);
465
466
        $this->assertCount(1, $entry->fresh()->recommends);
467
468
        $this->assertEquals(2, $entry->recommends()->first()->id);
469
470
        $this->assertEquals('I changed the recommend and the pivot text', $entry->fresh()->recommends->first()->pivot->text);
0 ignored issues
show
Bug introduced by
The method first() does not exist on Countable. It seems like you code against a sub-type of Countable such as RectorPrefix202408\Nette\Utils\ArrayList or RectorPrefix202408\Illum...acts\Support\MessageBag or Illuminate\Pagination\LengthAwarePaginator or Illuminate\Http\Resources\Json\ResourceCollection or Nette\Utils\Html or League\CommonMark\Util\ArrayCollection or RectorPrefix202408\Nette\Iterators\CachingIterator or Illuminate\Pagination\CursorPaginator or Illuminate\Contracts\Support\MessageBag or Carbon\CarbonPeriod or Illuminate\Pagination\Paginator or Nette\Iterators\CachingIterator or Nette\Utils\ArrayList or Illuminate\Support\ViewErrorBag or RectorPrefix202408\Nette\Utils\Html or Illuminate\Support\Enumerable or Ramsey\Collection\CollectionInterface or Ramsey\Collection\AbstractCollection or Ds\Map or Ds\Set or Ds\Sequence or RectorPrefix202408\Nette\Iterators\CachingIterator or Nette\Iterators\CachingIterator. ( Ignorable by Annotation )

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

470
        $this->assertEquals('I changed the recommend and the pivot text', $entry->fresh()->recommends->/** @scrutinizer ignore-call */ first()->pivot->text);
Loading history...
471
    }
472
473
    public function testMorphToManyCreatableRelationshipWithMultiple()
474
    {
475
        $inputData = $this->getPivotInputData(['recommendsDuplicate' => [
476
            [
477
                'recommendsDuplicate' => 1,
478
                'text' => 'my pivot recommend field 1',
479
            ],
480
            [
481
                'recommendsDuplicate' => 2,
482
                'text' => 'my pivot recommend field 2',
483
            ],
484
            [
485
                'recommendsDuplicate' => 1,
486
                'text' => 'my pivot recommend field 1x1',
487
            ],
488
        ],
489
        ], true, true);
490
491
        $entry = $this->crudPanel->create($inputData);
492
493
        $entry = $entry->fresh();
494
495
        $this->assertCount(3, $entry->recommendsDuplicate);
496
497
        $this->assertEquals(1, $entry->recommendsDuplicate[0]->id);
498
        $this->assertEquals(1, $entry->recommendsDuplicate[2]->id);
499
500
        $inputData['recommendsDuplicate'] = [
501
            [
502
                'recommendsDuplicate' => 1,
503
                'text' => 'I changed the recommend and the pivot text',
504
                'id' => 1,
505
            ],
506
            [
507
                'recommendsDuplicate' => 2,
508
                'text' => 'I changed the recommend and the pivot text 2',
509
                'id' => 2,
510
            ],
511
            [
512
                'recommendsDuplicate' => 3,
513
                'text' => 'new recommend and the pivot text 3',
514
                'id' => null,
515
            ],
516
        ];
517
518
        $this->crudPanel->update($entry->id, $inputData);
519
520
        $entry = $entry->fresh();
521
522
        $this->assertCount(3, $entry->recommendsDuplicate);
523
        $this->assertDatabaseCount('recommendables', 3);
524
525
        $this->assertEquals('I changed the recommend and the pivot text', $entry->recommendsDuplicate[0]->pivot->text);
526
        $this->assertEquals('I changed the recommend and the pivot text 2', $entry->recommendsDuplicate[1]->pivot->text);
527
        $this->assertEquals('new recommend and the pivot text 3', $entry->recommendsDuplicate[2]->pivot->text);
528
    }
529
530
    public function testBelongsToManyWithPivotDataRelationship()
531
    {
532
        $this->crudPanel->setModel(User::class);
533
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships);
534
        $this->crudPanel->addField([
535
            'name' => 'superArticles',
536
            'subfields' => [
537
                [
538
                    'name' => 'notes',
539
                ],
540
            ],
541
        ]);
542
543
        $faker = Factory::create();
544
        $articleData = [
545
            'content' => $faker->text(),
546
            'tags' => $faker->words(3, true),
547
            'user_id' => 1,
548
        ];
549
550
        $article = Article::create($articleData);
551
552
        $inputData = [
553
            'name' => $faker->name,
554
            'email' => $faker->safeEmail,
555
            'password' => Hash::make($faker->password()),
556
            'remember_token' => null,
557
            'superArticles' => [
558
                [
559
                    '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...
560
                    'notes' => 'my first article note',
561
                ],
562
            ],
563
        ];
564
565
        $entry = $this->crudPanel->create($inputData);
566
567
        $this->assertCount(1, $entry->fresh()->superArticles);
568
        $this->assertEquals('my first article note', $entry->fresh()->superArticles->first()->pivot->notes);
569
    }
570
571
    public function testBelongsToManyWithMultipleSameRelationIdAndPivotDataRelationship()
572
    {
573
        $inputData = $this->getPivotInputData(['superArticlesDuplicates' => [
574
            [
575
                'superArticlesDuplicates' => 1,
576
                'notes' => 'my first article note',
577
                'id' => null,
578
            ],
579
            [
580
                'superArticlesDuplicates' => 1,
581
                'notes' => 'my second article note',
582
                'id' => null,
583
            ],
584
            [
585
                'superArticlesDuplicates' => 2,
586
                'notes' => 'my first article2 note',
587
                'id' => null,
588
            ],
589
        ],
590
        ], true, true);
591
592
        $entry = $this->crudPanel->create($inputData);
593
        $relationField = $this->crudPanel->getUpdateFields($entry->id)['superArticlesDuplicates'];
594
595
        $this->assertCount(3, $relationField['value']);
596
597
        $entry = $entry->fresh();
598
599
        $this->assertCount(3, $entry->superArticlesDuplicates);
600
        $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 BackedEnum. ( Ignorable by Annotation )

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

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

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

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

Loading history...
Bug introduced by
The property notes does not seem to exist on Illuminate\Database\Eloquent\Relations\Relation.
Loading history...
Bug introduced by
The method first() does not exist on null. ( Ignorable by Annotation )

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

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

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

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

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

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

Loading history...
Bug introduced by
The property notes does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
601
        $this->assertEquals('my second article note', $entry->superArticles[1]->pivot->notes);
602
        $this->assertEquals('my first article2 note', $entry->superArticles[2]->pivot->notes);
603
604
        $inputData = $this->getPivotInputData(['superArticlesDuplicates' => [
605
            [
606
                'superArticlesDuplicates' => 1,
607
                'notes' => 'my first article note updated',
608
                'id' => 1,
609
            ],
610
            [
611
                'superArticlesDuplicates' => 1,
612
                'notes' => 'my second article note updated',
613
                'id' => 2,
614
            ],
615
            [
616
                'superArticlesDuplicates' => 2,
617
                'notes' => 'my first article2 note updated',
618
                'id' => 3,
619
            ],
620
        ],
621
        ], false, true);
622
623
        $entry = $this->crudPanel->update($entry->id, $inputData);
624
        $relationField = $this->crudPanel->getUpdateFields($entry->id)['superArticlesDuplicates'];
625
        $this->assertCount(3, $relationField['value']);
626
627
        $entry = $entry->fresh();
628
629
        $this->assertCount(3, $entry->superArticlesDuplicates);
630
        $this->assertEquals('my first article note updated', $entry->superArticles[0]->pivot->notes);
631
        $this->assertEquals('my second article note updated', $entry->superArticles[1]->pivot->notes);
632
        $this->assertEquals('my first article2 note updated', $entry->superArticles[2]->pivot->notes);
633
    }
634
635
    public function testBelongsToManyAlwaysSaveSinglePivotWhenMultipleNotAllowed()
636
    {
637
        $inputData = $this->getPivotInputData(['superArticlesDuplicates' => [
638
            [
639
                'superArticlesDuplicates' => 1,
640
                'notes' => 'my first article note',
641
                'id' => null,
642
            ],
643
            [
644
                'superArticlesDuplicates' => 1,
645
                'notes' => 'my second article note',
646
                'id' => null,
647
            ],
648
            [
649
                'superArticlesDuplicates' => 2,
650
                'notes' => 'my first article2 note',
651
                'id' => null,
652
            ],
653
        ],
654
        ]);
655
656
        $entry = $this->crudPanel->create($inputData);
657
        $relationField = $this->crudPanel->getUpdateFields($entry->id)['superArticlesDuplicates'];
658
659
        $this->assertCount(2, $relationField['value']);
660
661
        $entry = $entry->fresh();
662
663
        $this->assertCount(2, $entry->superArticlesDuplicates);
664
        $this->assertEquals('my second article note', $entry->superArticles[0]->pivot->notes);
665
        $this->assertEquals('my first article2 note', $entry->superArticles[1]->pivot->notes);
666
    }
667
668
    public function testBelongsToManyDeletesPivotData()
669
    {
670
        $inputData = $this->getPivotInputData(['superArticlesDuplicates' => [
671
            [
672
                'superArticlesDuplicates' => 1,
673
                'notes' => 'my first article note',
674
                'id' => null,
675
            ],
676
            [
677
                'superArticlesDuplicates' => 1,
678
                'notes' => 'my second article note',
679
                'id' => null,
680
            ],
681
            [
682
                'superArticlesDuplicates' => 2,
683
                'notes' => 'my first article2 note',
684
                'id' => null,
685
            ],
686
        ],
687
        ], true, true);
688
689
        $entry = $this->crudPanel->create($inputData);
690
        $relationField = $this->crudPanel->getUpdateFields($entry->id)['superArticlesDuplicates'];
691
692
        $this->assertCount(3, $relationField['value']);
693
694
        $inputData = $this->getPivotInputData(['superArticlesDuplicates' => [
695
            [
696
                'superArticlesDuplicates' => 1,
697
                'notes' => 'new first article note',
698
                'id' => null,
699
            ],
700
            [
701
                'superArticlesDuplicates' => 1,
702
                'notes' => 'my second article note updated',
703
                'id' => 2,
704
            ],
705
            [
706
                'superArticlesDuplicates' => 3,
707
                'notes' => 'my first article2 note updated',
708
                'id' => 3,
709
            ],
710
        ],
711
        ], false, true);
712
713
        $entry = $this->crudPanel->update($entry->id, $inputData);
714
        $relationField = $this->crudPanel->getUpdateFields($entry->id)['superArticlesDuplicates'];
715
        $this->assertCount(3, $relationField['value']);
716
717
        $entry = $entry->fresh();
718
719
        $this->assertCount(3, $entry->superArticlesDuplicates);
720
        $this->assertEquals('new first article note', $entry->superArticles[2]->pivot->notes);
721
        $this->assertEquals('my second article note updated', $entry->superArticles[0]->pivot->notes);
722
        $this->assertEquals('my first article2 note updated', $entry->superArticles[1]->pivot->notes);
723
    }
724
725
    public function testCreateHasOneWithNestedRelationsRepeatableInterface()
726
    {
727
        $this->crudPanel->setModel(User::class);
728
        $this->crudPanel->setOperation('create');
729
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships);
730
        $this->crudPanel->addField(
731
            [
732
                'name' => 'accountDetails',
733
                'subfields' => [
734
                    [
735
                        'name' => 'nickname',
736
                    ],
737
                    [
738
                        'name' => 'profile_picture',
739
                    ],
740
                    [
741
                        'name' => 'article',
742
                    ],
743
                    [
744
                        'name' => 'addresses',
745
                        'subfields' => [
746
                            [
747
                                'name' => 'bang',
748
                            ],
749
                            [
750
                                'name' => 'street',
751
                            ],
752
                            [
753
                                'name' => 'number',
754
                            ],
755
                        ],
756
                    ],
757
                    [
758
                        'name' => 'bangs',
759
                    ],
760
                    [
761
                        'name' => 'bangsPivot',
762
                        'subfields' => [
763
                            [
764
                                'name' => 'pivot_field',
765
                            ],
766
                        ],
767
                    ],
768
                ],
769
            ]);
770
771
        $faker = Factory::create();
772
773
        $inputData = [
774
            'name' => $faker->name,
775
            'email' => $faker->safeEmail,
776
            'password' => Hash::make($faker->password()),
777
            'remember_token' => null,
778
            'roles' => [1, 2],
779
            'accountDetails' => [
780
                [
781
                    'nickname' => 'i_have_has_one',
782
                    'profile_picture' => 'ohh my picture 1.jpg',
783
                    'article' => 1,
784
                    'addresses' => [
785
                        [
786
                            'bang' => 1,
787
                            'street' => 'test',
788
                            'number' => 1,
789
                        ],
790
                        [
791
                            'bang' => 1,
792
                            'street' => 'test2',
793
                            'number' => 2,
794
                        ],
795
                    ],
796
                    'bangs' => [1, 2],
797
                    'bangsPivot' => [
798
                        ['bangsPivot' => 1, 'pivot_field' => 'test1'],
799
                        ['bangsPivot' => 2, 'pivot_field' => 'test2'],
800
                    ],
801
                ],
802
            ],
803
        ];
804
805
        $entry = $this->crudPanel->create($inputData);
806
        $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...
807
        $account_details = $entry->accountDetails()->first();
808
809
        $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...
810
        $this->assertEquals($account_details->addresses->count(), 2);
0 ignored issues
show
Bug introduced by
The method count() does not exist on UnitEnum. ( Ignorable by Annotation )

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

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

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

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

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

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

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

810
        $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...
811
        $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...
812
        $this->assertEquals($account_details->addresses->first()->street, 'test');
0 ignored issues
show
Bug introduced by
The property street does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property street does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
813
        $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...
814
        $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 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 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

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

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

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

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

815
        $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...
816
        $this->assertEquals($account_details->bangsPivot->first()->pivot->pivot_field, 'test1');
0 ignored issues
show
Bug introduced by
The property pivot_field does not seem to exist on Illuminate\Database\Eloquent\Relations\Relation.
Loading history...
Bug introduced by
The property pivot does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property pivot does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The property pivot_field does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The property pivot_field does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
Bug introduced by
The property pivot_field does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
817
    }
818
819
    public function testCreateBelongsToFake()
820
    {
821
        $belongsToField = [   // select_grouped
822
            'label' => 'Select_grouped',
823
            'type' => 'select_grouped', //https://github.com/Laravel-Backpack/CRUD/issues/502
824
            'name' => 'bang_relation_field',
825
            'fake' => true,
826
            'entity' => 'bang',
827
            'model' => 'Backpack\CRUD\Tests\config\Models\Bang',
828
            'attribute' => 'title',
829
            'group_by' => 'category', // the relationship to entity you want to use for grouping
830
            'group_by_attribute' => 'name', // the attribute on related model, that you want shown
831
            'group_by_relationship_back' => 'articles', // relationship from related model back to this model
832
            'tab' => 'Selects',
833
            'wrapperAttributes' => ['class' => 'form-group col-md-6'],
834
        ];
835
836
        $this->crudPanel->setModel(User::class);
837
        $this->crudPanel->setOperation('create');
838
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships);
839
        $this->crudPanel->addField($belongsToField);
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
            'bang_relation_field' => 1,
849
        ];
850
851
        $entry = $this->crudPanel->create($inputData);
852
        $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...
853
        $this->crudPanel->entry = $entry->withFakes();
854
        $this->assertEquals($entry->bang_relation_field, 1);
855
    }
856
857
    public function testCreateHasOneWithNestedRelations()
858
    {
859
        $this->crudPanel->setModel(User::class);
860
        $this->crudPanel->setOperation('create');
861
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships);
862
        $this->crudPanel->addFields([
863
            [
864
                'name' => 'accountDetails.nickname',
865
            ],
866
            [
867
                'name' => 'accountDetails.profile_picture',
868
            ],
869
            [
870
                'name' => 'accountDetails.article',
871
            ],
872
            [
873
                'name' => 'accountDetails.addresses',
874
                'subfields' => [
875
                    [
876
                        'name' => 'city',
877
                        'entity' => 'bang',
878
                    ],
879
                    [
880
                        'name' => 'street',
881
                    ],
882
                    [
883
                        'name' => 'number',
884
                    ],
885
                ],
886
            ],
887
            [
888
                'name' => 'accountDetails.bangs',
889
            ],
890
            [
891
                'name' => 'accountDetails.bangsPivot',
892
                'subfields' => [
893
                    [
894
                        'name' => 'pivot_field',
895
                    ],
896
                ],
897
            ],
898
        ]);
899
900
        $faker = Factory::create();
901
902
        $inputData = [
903
            'name' => $faker->name,
904
            'email' => $faker->safeEmail,
905
            'password' => Hash::make($faker->password()),
906
            'remember_token' => null,
907
            'roles' => [1, 2],
908
            'accountDetails' => [
909
                'nickname' => 'i_have_has_one',
910
                'profile_picture' => 'ohh my picture 1.jpg',
911
                'article' => 1,
912
                'addresses' => [
913
                    [
914
                        'city' => 1,
915
                        'street' => 'test',
916
                        'number' => 1,
917
                    ],
918
                    [
919
                        'city' => 2,
920
                        'street' => 'test2',
921
                        'number' => 2,
922
                    ],
923
                ],
924
                'bangs' => [1, 2],
925
                'bangsPivot' => [
926
                    ['bangsPivot' => 1, 'pivot_field' => 'test1'],
927
                    ['bangsPivot' => 2, 'pivot_field' => 'test2'],
928
                ],
929
            ],
930
        ];
931
932
        $entry = $this->crudPanel->create($inputData);
933
        $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...
934
        $account_details = $entry->accountDetails()->first();
935
936
        $this->assertEquals($account_details->article, Article::find(1));
0 ignored issues
show
Bug introduced by
The property article does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The property article does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
937
        $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...
938
        $this->assertEquals($account_details->addresses->first()->bang->id, 1);
0 ignored issues
show
Bug introduced by
The property id does not seem to exist on Illuminate\Database\Eloquent\Relations\Relation.
Loading history...
Bug introduced by
The property bang does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The property id does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property bang does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property id does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The property id does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
939
        $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...
940
        $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...
941
        $this->assertEquals($account_details->bangs->first()->name, Bang::find(1)->name);
0 ignored issues
show
Bug introduced by
The property name does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The property name does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property bangs does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property bangs does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The property name does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
942
        $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...
943
        $this->assertEquals($account_details->bangsPivot->first()->pivot->pivot_field, 'test1');
0 ignored issues
show
Bug introduced by
The property pivot_field does not seem to exist on Illuminate\Database\Eloquent\Relations\Relation.
Loading history...
Bug introduced by
The property pivot_field does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property pivot does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The property pivot_field does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
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 does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
944
945
        // Now test the remove process
946
947
        $inputData = [
948
            'name' => $faker->name,
949
            'email' => $faker->safeEmail,
950
            'password' => Hash::make($faker->password()),
951
            'remember_token' => null,
952
            'roles' => [1, 2],
953
            'accountDetails' => [
954
                'nickname' => 'i_have_has_one',
955
                'profile_picture' => 'ohh my picture 1.jpg',
956
                'article' => 1,
957
                'addresses' => [ // HasOne is tested in other test function
958
                    [
959
                        'city' => 2,
960
                        'street' => 'test',
961
                        'number' => 1,
962
                    ],
963
                    [
964
                        'city' => 1,
965
                        'street' => 'test2',
966
                        'number' => 2,
967
                    ],
968
                ],
969
                'bangs' => [],
970
                'bangsPivot' => [],
971
            ],
972
        ];
973
974
        $entry = $this->crudPanel->update($entry->id, $inputData);
975
        $account_details = $entry->accountDetails()->first();
976
        $this->assertEquals($account_details->addresses->count(), 2);
977
        $this->assertEquals($account_details->addresses->first()->bang->id, 2);
978
        $this->assertEquals($account_details->bangs->count(), 0);
979
        $this->assertEquals($account_details->bangsPivot->count(), 0);
980
    }
981
982
    public function testCreateHasOneWithNestedRelationAsTheFirstField()
983
    {
984
        $this->crudPanel->setModel(User::class);
985
        $this->crudPanel->setOperation('create');
986
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships);
987
        $this->crudPanel->addFields([
988
            [
989
                'name' => 'accountDetails.article',
990
            ],
991
            [
992
                'name' => 'accountDetails.nickname',
993
            ],
994
            [
995
                'name' => 'accountDetails.profile_picture',
996
            ],
997
        ]);
998
999
        $faker = Factory::create();
1000
1001
        $inputData = [
1002
            'name' => $faker->name,
1003
            'email' => $faker->safeEmail,
1004
            'password' => Hash::make($faker->password()),
1005
            'remember_token' => null,
1006
            'roles' => [1, 2],
1007
            'accountDetails' => [
1008
                'article' => 1,
1009
                'nickname' => 'i_have_has_one',
1010
                'profile_picture' => 'ohh my picture 1.jpg',
1011
            ],
1012
        ];
1013
1014
        $entry = $this->crudPanel->create($inputData);
1015
        $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...
1016
        $account_details = $entry->accountDetails()->first();
1017
1018
        $this->assertEquals($account_details->article, Article::find(1));
0 ignored issues
show
Bug introduced by
The property article does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The property article does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
1019
    }
1020
1021
    public function testMorphOneRelationship()
1022
    {
1023
        $this->crudPanel->setModel(User::class);
1024
        $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

1024
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1025
                          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...
1025
        $this->crudPanel->addField([
1026
            'name' => 'comment.text',
1027
        ], '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

1027
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1028
                          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...
1028
1029
        $faker = Factory::create();
1030
        $inputData = [
1031
            'name' => $faker->name,
1032
            'email' => $faker->safeEmail,
1033
            'password' => Hash::make($faker->password()),
1034
            'remember_token' => null,
1035
            'comment' => [
1036
                'text' => 'some test comment text',
1037
            ],
1038
        ];
1039
1040
        $entry = $this->crudPanel->create($inputData);
1041
        $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...
1042
1043
        $this->assertEquals($inputData['comment']['text'], $entry->comment->text);
0 ignored issues
show
Bug introduced by
The property text does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
Bug introduced by
The property text does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property text does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The property text does not seem to exist on Illuminate\Database\Eloquent\Relations\Relation.
Loading history...
1044
1045
        $inputData['comment']['text'] = 'updated comment text';
1046
1047
        $this->crudPanel->update($entry->id, $inputData);
1048
1049
        $this->assertEquals($inputData['comment']['text'], $entry->fresh()->comment->text);
1050
    }
1051
1052
    public function testMorphManyCreatableRelationship()
1053
    {
1054
        $this->crudPanel->setModel(User::class);
1055
        $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

1055
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1056
                          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...
1056
        $this->crudPanel->addField([
1057
            'name' => 'stars',
1058
            'subfields' => [
1059
                [
1060
                    'name' => 'title',
1061
                ],
1062
            ],
1063
        ], '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

1063
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1064
                          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...
1064
1065
        $faker = Factory::create();
1066
        $inputData = [
1067
            'name' => $faker->name,
1068
            'email' => $faker->safeEmail,
1069
            'password' => Hash::make($faker->password()),
1070
            'remember_token' => null,
1071
            'stars' => [
1072
                [
1073
                    'id' => null,
1074
                    'title' => 'this is the star 1 title',
1075
                ],
1076
                [
1077
                    'id' => null,
1078
                    'title' => 'this is the star 2 title',
1079
                ],
1080
            ],
1081
        ];
1082
1083
        $entry = $this->crudPanel->create($inputData);
1084
        $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...
1085
1086
        $this->assertCount(2, $entry->stars);
1087
1088
        $this->assertEquals($inputData['stars'][0]['title'], $entry->stars()->first()->title);
0 ignored issues
show
Bug introduced by
The property title does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property title does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
1089
1090
        $inputData['stars'] = [
1091
            [
1092
                'id' => 1,
1093
                'title' => 'only one star with changed title',
1094
            ],
1095
        ];
1096
1097
        $this->crudPanel->update($entry->id, $inputData);
1098
1099
        $this->assertCount(1, $entry->fresh()->stars);
1100
1101
        $this->assertEquals($inputData['stars'][0]['title'], $entry->fresh()->stars->first()->title);
1102
        $this->assertEquals($inputData['stars'][0]['id'], $entry->fresh()->stars->first()->id);
1103
    }
1104
1105
    public function testHasManyCreatableRelationship()
1106
    {
1107
        $this->crudPanel->setModel(User::class);
1108
        $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

1108
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1109
                          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...
1109
        $this->crudPanel->addField([
1110
            'name' => 'universes',
1111
            'subfields' => [
1112
                [
1113
                    'name' => 'title',
1114
                ],
1115
            ],
1116
        ], '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

1116
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1117
                          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...
1117
1118
        $faker = Factory::create();
1119
        $inputData = [
1120
            'name' => $faker->name,
1121
            'email' => $faker->safeEmail,
1122
            'password' => Hash::make($faker->password()),
1123
            'remember_token' => null,
1124
            'universes' => [
1125
                [
1126
                    'id' => null,
1127
                    'title' => 'this is the star 1 title',
1128
                ],
1129
                [
1130
                    'title' => 'this is the star 2 title',
1131
                ],
1132
            ],
1133
        ];
1134
1135
        $entry = $this->crudPanel->create($inputData);
1136
        $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...
1137
1138
        $this->assertCount(2, $entry->universes);
1139
1140
        $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...
1141
1142
        $inputData['universes'] = [
1143
            [
1144
                'id' => 1,
1145
                'title' => 'star 1 with changed title',
1146
            ],
1147
            [
1148
                'id' => 2,
1149
                'title' => 'star 2 with changed title',
1150
            ],
1151
        ];
1152
1153
        $this->crudPanel->update($entry->id, $inputData);
1154
1155
        $universes = $entry->fresh()->universes;
1156
        $this->assertCount(2, $universes);
1157
        $this->assertEquals([1, 2], $universes->pluck('id')->toArray());
1158
1159
        $inputData['universes'] = [
1160
            [
1161
                'id' => 1,
1162
                'title' => 'only one star with changed title',
1163
            ],
1164
        ];
1165
1166
        $this->crudPanel->update($entry->id, $inputData);
1167
1168
        $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

1168
        $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...
1169
        $this->assertEquals($inputData['universes'][0]['id'], $entry->fresh()->universes->first()->id);
0 ignored issues
show
Bug introduced by
The property id does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The property id does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
1170
        $this->assertEquals(1, Universe::all()->count());
1171
1172
        $inputData['universes'] = [
1173
            [
1174
                'id' => null,
1175
                'title' => 'new star 3',
1176
            ],
1177
        ];
1178
1179
        $this->crudPanel->update($entry->id, $inputData);
1180
1181
        $this->assertEquals($inputData['universes'][0]['title'], $entry->fresh()->universes->first()->title);
1182
        $this->assertEquals(3, $entry->fresh()->universes->first()->id);
1183
        $this->assertEquals(1, Universe::all()->count());
1184
1185
        $inputData['universes'] = null;
1186
1187
        $this->crudPanel->update($entry->id, $inputData);
1188
1189
        $this->assertEquals(0, count($entry->fresh()->universes));
1190
        $this->assertEquals(0, Universe::all()->count());
1191
    }
1192
1193
    public function testHasManySelectableRelationshipWithoutForceDelete()
1194
    {
1195
        $this->crudPanel->setModel(User::class);
1196
        $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

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

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

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

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

1265
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1266
                          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...
1266
        $this->crudPanel->addField([
1267
            'name' => 'incomes',
1268
            'subfields' => [
1269
                [
1270
                    'name' => 'label',
1271
                    'type' => 'text',
1272
                ],
1273
                [
1274
                    'name' => 'type',
1275
                    'type' => 'hidden',
1276
                    'value' => 'income',
1277
                ],
1278
                [
1279
                    'name' => 'amount',
1280
                    'type' => 'number',
1281
                ],
1282
            ],
1283
        ], '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

1283
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1284
                          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...
1284
        $this->crudPanel->addField([
1285
            'name' => 'expenses',
1286
            'subfields' => [
1287
                [
1288
                    'name' => 'label',
1289
                    'type' => 'text',
1290
                ],
1291
                [
1292
                    'name' => 'type',
1293
                    'type' => 'hidden',
1294
                    'value' => 'expense',
1295
                ],
1296
                [
1297
                    'name' => 'amount',
1298
                    'type' => 'number',
1299
                ],
1300
            ],
1301
        ], 'both');
1302
1303
        $faker = Factory::create();
1304
        $inputData = [
1305
            'name' => $faker->name,
1306
            'email' => $faker->safeEmail,
1307
            'password' => Hash::make($faker->password()),
1308
            'remember_token' => null,
1309
            'incomes' => [
1310
                [
1311
                    'label' => $faker->name,
1312
                    'amount' => 33,
1313
                    'type' => 'income',
1314
                ],
1315
                [
1316
                    'label' => $faker->name,
1317
                    'amount' => 22,
1318
                    'type' => 'income',
1319
                ],
1320
            ],
1321
            'expenses' => [
1322
                [
1323
                    'label' => $faker->name,
1324
                    'amount' => 44,
1325
                    'type' => 'expense',
1326
                ],
1327
                [
1328
                    'label' => $faker->name,
1329
                    'amount' => 10,
1330
                    'type' => 'expense',
1331
                ],
1332
            ],
1333
        ];
1334
        $entry = $this->crudPanel->create($inputData);
1335
1336
        $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

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

1337
        /** @scrutinizer ignore-call */ 
1338
        $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...
1338
        $this->assertCount(2, $entry->expenses);
1339
        $this->assertCount(2, $entry->incomes);
1340
        $this->assertEquals(44, $entry->expenses->first()->amount);
1341
        $this->assertEquals(33, $entry->incomes->first()->amount);
1342
1343
        $inputData['incomes'] = [
1344
            [
1345
                'id' => 2,
1346
                'label' => $faker->name,
1347
                'amount' => 222,
1348
                'type' => 'income',
1349
            ],
1350
        ];
1351
        $inputData['expenses'] = [
1352
            [
1353
                'id' => 3,
1354
                'label' => $faker->name,
1355
                'amount' => 44,
1356
                'type' => 'expense',
1357
            ],
1358
            [
1359
                'id' => 4,
1360
                'label' => $faker->name,
1361
                'amount' => 10,
1362
                'type' => 'expense',
1363
            ],
1364
        ];
1365
        $this->crudPanel->update($entry->id, $inputData);
1366
1367
        $freshIncomes = $entry->fresh()->incomes;
1368
        $freshExpenses = $entry->fresh()->expenses;
1369
        $this->assertCount(2, $freshExpenses);
1370
        $this->assertCount(1, $freshIncomes);
1371
        $this->assertEquals(2, $freshIncomes->first()->id);
1372
1373
        $inputData['expenses'] = [];
1374
        $this->crudPanel->update($entry->id, $inputData);
1375
1376
        $freshIncomes = $entry->fresh()->incomes;
1377
        $freshExpenses = $entry->fresh()->expenses;
1378
        $this->assertCount(0, $freshExpenses);
1379
        $this->assertCount(1, $freshIncomes);
1380
    }
1381
1382
    public function testHasManySelectableRelationshipWithFallbackId()
1383
    {
1384
        $this->crudPanel->setModel(User::class);
1385
        $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

1385
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1386
                          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...
1386
        $this->crudPanel->addField([
1387
            'name' => 'planets',
1388
            'fallback_id' => 0,
1389
            'force_delete' => false,
1390
        ], '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

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

1419
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1420
                          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...
1420
        $this->crudPanel->addField([
1421
            'name' => 'planets',
1422
            'force_delete' => true,
1423
            'fallback_id' => false,
1424
        ], '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

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

1452
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1453
                          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...
1453
        $this->crudPanel->addField([
1454
            'name' => 'comets',
1455
            'force_delete' => false,
1456
            'fallback_id' => false,
1457
        ], '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

1457
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1458
                          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...
1458
1459
        $faker = Factory::create();
1460
        $inputData = [
1461
            'name' => $faker->name,
1462
            'email' => $faker->safeEmail,
1463
            'password' => Hash::make($faker->password()),
1464
            'remember_token' => null,
1465
            'comets' => [1, 2],
1466
        ];
1467
1468
        $entry = $this->crudPanel->create($inputData);
1469
1470
        $this->assertCount(2, $entry->comets);
1471
1472
        $inputData['comets'] = [2];
1473
1474
        $this->crudPanel->update($entry->id, $inputData);
1475
1476
        $this->assertCount(1, $entry->fresh()->comets);
1477
1478
        $comets = Comet::all();
1479
        $this->assertCount(2, $comets);
1480
        $this->assertEquals(0, $comets->first()->user_id);
1481
    }
1482
1483
    public function testHasManySelectableRelationshipNonNullable()
1484
    {
1485
        $this->crudPanel->setModel(User::class);
1486
        $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

1486
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1487
                          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...
1487
        $this->crudPanel->addField([
1488
            'name' => 'planetsNonNullable',
1489
            'force_delete' => false,
1490
            'fallback_id' => false,
1491
        ], '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

1491
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1492
                          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...
1492
1493
        $faker = Factory::create();
1494
        $inputData = [
1495
            'name' => $faker->name,
1496
            'email' => $faker->safeEmail,
1497
            'password' => Hash::make($faker->password()),
1498
            'remember_token' => null,
1499
            'planetsNonNullable' => [1, 2],
1500
        ];
1501
1502
        $entry = $this->crudPanel->create($inputData);
1503
1504
        $this->assertCount(2, $entry->planetsNonNullable);
1505
1506
        $inputData['planetsNonNullable'] = null;
1507
1508
        $this->crudPanel->update($entry->id, $inputData);
1509
1510
        $this->assertCount(0, $entry->fresh()->planetsNonNullable);
1511
1512
        $planets = PlanetNonNullable::all();
1513
        $this->assertCount(0, $planets);
1514
    }
1515
1516
    public function testCreateHasManyRelationWithDelimitedNameSubfields()
1517
    {
1518
        $this->crudPanel->setModel(User::class);
1519
        $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

1519
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1520
                          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...
1520
        $this->crudPanel->addField([
1521
            'name' => 'universes',
1522
            'subfields' => [
1523
                [
1524
                    'name' => 'title',
1525
                ],
1526
                [
1527
                    'name' => 'start_date,end_date',
1528
                    'type' => 'date_range',
1529
                ],
1530
            ],
1531
        ], '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

1531
        $this->crudPanel->/** @scrutinizer ignore-call */ 
1532
                          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...
1532
1533
        $faker = Factory::create();
1534
        $inputData = [
1535
            'name' => $faker->name,
1536
            'email' => $faker->safeEmail,
1537
            'password' => Hash::make($faker->password()),
1538
            'remember_token' => null,
1539
            'universes' => [
1540
                [
1541
                    'id' => null,
1542
                    'title' => 'this is the star 1 title',
1543
                    'start_date' => '2021-02-26',
1544
                    'end_date' => '2091-01-26',
1545
                ],
1546
                [
1547
                    'title' => 'this is the star 2 title',
1548
                    'end_date' => '2021-02-26',
1549
                    'start_date' => '2091-01-26',
1550
                ],
1551
            ],
1552
        ];
1553
1554
        $entry = $this->crudPanel->create($inputData);
1555
1556
        $this->assertCount(2, $entry->universes);
1557
1558
        $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...
1559
        $this->assertEquals($inputData['universes'][0]['end_date'], $entry->universes()->first()->end_date);
0 ignored issues
show
Bug introduced by
The property end_date does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The property end_date does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
1560
        $this->assertEquals($inputData['universes'][1]['end_date'], $entry->universes()->find(2)->end_date);
1561
        $this->assertEquals($inputData['universes'][1]['start_date'], $entry->universes()->find(2)->start_date);
1562
    }
1563
1564
    public function testCreateHasOneRelationWithDelimitedNameSubfields()
1565
    {
1566
        $this->crudPanel->setModel(User::class);
1567
        $this->crudPanel->setOperation('create');
1568
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships);
1569
        $this->crudPanel->addField(
1570
            [
1571
                'name' => 'accountDetails',
1572
                'subfields' => [
1573
                    [
1574
                        'name' => 'nickname',
1575
                    ],
1576
                    [
1577
                        'name' => 'start_date,end_date',
1578
                    ],
1579
                    [
1580
                        'name' => 'profile_picture',
1581
                    ],
1582
                ],
1583
            ]);
1584
1585
        $faker = Factory::create();
1586
1587
        $inputData = [
1588
            'name' => $faker->name,
1589
            'email' => $faker->safeEmail,
1590
            'password' => Hash::make($faker->password()),
1591
            'remember_token' => null,
1592
            'roles' => [1, 2],
1593
            'accountDetails' => [
1594
                [
1595
                    'nickname' => 'i_have_has_one',
1596
                    'profile_picture' => 'ohh my picture 1.jpg',
1597
                    'start_date' => '2021-02-26',
1598
                    'end_date' => '2091-01-26',
1599
                ],
1600
            ],
1601
        ];
1602
1603
        $entry = $this->crudPanel->create($inputData);
1604
        $account_details = $entry->accountDetails()->first();
1605
1606
        $this->assertEquals($account_details->start_date, '2021-02-26');
0 ignored issues
show
Bug introduced by
The property start_date does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The property start_date does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
1607
        $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...
1608
    }
1609
1610
    public function testBelongsToManyWithDelimitedNameSubfields()
1611
    {
1612
        $this->crudPanel->setModel(User::class);
1613
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships);
1614
        $this->crudPanel->addField([
1615
            'name' => 'superArticles',
1616
            'subfields' => [
1617
                [
1618
                    'name' => 'notes',
1619
                ],
1620
                [
1621
                    'name' => 'start_date,end_date',
1622
                ],
1623
            ],
1624
        ]);
1625
1626
        $faker = Factory::create();
1627
        $articleData = [
1628
            'content' => $faker->text(),
1629
            'tags' => $faker->words(3, true),
1630
            'user_id' => 1,
1631
        ];
1632
1633
        $article = Article::create($articleData);
1634
1635
        $inputData = [
1636
            'name' => $faker->name,
1637
            'email' => $faker->safeEmail,
1638
            'password' => Hash::make($faker->password()),
1639
            'remember_token' => null,
1640
            'superArticles' => [
1641
                [
1642
                    '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...
1643
                    'notes' => 'my first article note',
1644
                    'start_date' => '2021-02-26',
1645
                    'end_date' => '2091-01-26',
1646
                ],
1647
            ],
1648
        ];
1649
1650
        $entry = $this->crudPanel->create($inputData);
1651
1652
        $this->assertCount(1, $entry->fresh()->superArticles);
1653
        $superArticle = $entry->fresh()->superArticles->first();
1654
        $this->assertEquals($superArticle->pivot->start_date, '2021-02-26');
1655
        $this->assertEquals($superArticle->pivot->end_date, '2091-01-26');
1656
1657
        $this->crudPanel->getUpdateFields($superArticle->id);
1658
    }
1659
1660
    public function testItCanCreateMorphToFieldsStructure()
1661
    {
1662
        $this->crudPanel->setModel(Star::class);
1663
        $this->crudPanel->addField([
1664
            'name' => 'starable',
1665
            'morphOptions' => [
1666
                ['Backpack\CRUD\Tests\config\Models\User', 'User'],
1667
            ],
1668
        ]);
1669
1670
        $this->assertCount(2, $this->crudPanel->fields()['starable']['subfields']);
1671
1672
        [$morphTypeField, $morphIdField] = $this->crudPanel->fields()['starable']['subfields'];
1673
1674
        $this->assertTrue($morphTypeField['name'] === 'starable_type');
1675
        $this->assertTrue($morphIdField['name'] === 'starable_id');
1676
    }
1677
1678
    public function testIPreventsAddingRepeateadMorphOptions()
1679
    {
1680
        $this->crudPanel->setModel(Star::class);
1681
        $this->expectException(\Exception::class);
1682
1683
        $this->crudPanel->addField([
1684
            'name' => 'starable',
1685
            'morphOptions' => [
1686
                ['Backpack\CRUD\Tests\config\Models\User', 'User'],
1687
                ['Backpack\CRUD\Tests\config\Models\User', 'User'],
1688
            ],
1689
        ]);
1690
    }
1691
1692
    public function testItThrowsErrorIfStringIsNotOnMorphMap()
1693
    {
1694
        $this->crudPanel->setModel(Star::class);
1695
        $this->expectException(\Exception::class);
1696
1697
        $this->crudPanel->addField([
1698
            'name' => 'starable',
1699
            'morphOptions' => [
1700
                ['somethingThatDoesNotExist'],
1701
            ],
1702
        ]);
1703
    }
1704
1705
    public function testItCanAddTheOptionsFromTheMorphMap()
1706
    {
1707
        $this->crudPanel->setModel(Star::class);
1708
1709
        Relation::morphMap([
1710
            'user' => 'Backpack\CRUD\Tests\config\Models\User',
1711
        ]);
1712
1713
        $this->crudPanel->addField([
1714
            'name' => 'starable',
1715
            'morphOptions' => [
1716
                ['user'],
1717
            ],
1718
        ]);
1719
1720
        [$morphTypeField, $morphIdField] = $this->crudPanel->fields()['starable']['subfields'];
1721
        $this->assertEquals(['user' => 'Backpack\CRUD\Tests\config\Models\User'], $morphTypeField['morphMap']);
1722
    }
1723
1724
    public function testItThrowsErrorIfDuplicateMorphMapName()
1725
    {
1726
        $this->crudPanel->setModel(Star::class);
1727
        $this->expectException(\Exception::class);
1728
1729
        Relation::morphMap([
1730
            'user' => 'Backpack\CRUD\Tests\config\Models\User',
1731
        ]);
1732
1733
        $this->crudPanel->addField([
1734
            'name' => 'starable',
1735
            'morphOptions' => [
1736
                ['user'],
1737
                ['user'],
1738
            ],
1739
        ]);
1740
    }
1741
1742
    public function testItCanRegisterModelEventsInTheFields()
1743
    {
1744
        $this->crudPanel->setModel(User::class);
1745
1746
        $this->crudPanel->addField([
1747
            'name' => 'name',
1748
            'events' => [
1749
                'created' => function ($entry) {
0 ignored issues
show
Unused Code introduced by
The parameter $entry is not used and could be removed. ( Ignorable by Annotation )

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

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

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

Loading history...
1750
                },
1751
                'creating' => function ($entry) {
1752
                    $entry->email = '[email protected]';
1753
                    $entry->password = Hash::make('password');
1754
                },
1755
                'saving' => function ($entry) {
0 ignored issues
show
Unused Code introduced by
The parameter $entry is not used and could be removed. ( Ignorable by Annotation )

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Loading history...
1766
                },
1767
            ],
1768
        ]);
1769
1770
        $this->crudPanel->registerFieldEvents();
1771
1772
        $this->assertNotEmpty($this->crudPanel->getModel()->getEventDispatcher()->getListeners('eloquent.created: Backpack\CRUD\Tests\Config\Models\User'));
1773
        $this->assertNotEmpty($this->crudPanel->getModel()->getEventDispatcher()->getListeners('eloquent.creating: Backpack\CRUD\Tests\Config\Models\User'));
1774
        $this->assertNotEmpty($this->crudPanel->getModel()->getEventDispatcher()->getListeners('eloquent.saving: Backpack\CRUD\Tests\Config\Models\User'));
1775
        $this->assertNotEmpty($this->crudPanel->getModel()->getEventDispatcher()->getListeners('eloquent.saved: Backpack\CRUD\Tests\Config\Models\User'));
1776
        $this->assertNotEmpty($this->crudPanel->getModel()->getEventDispatcher()->getListeners('eloquent.updating: Backpack\CRUD\Tests\Config\Models\User'));
1777
        $this->assertNotEmpty($this->crudPanel->getModel()->getEventDispatcher()->getListeners('eloquent.updated: Backpack\CRUD\Tests\Config\Models\User'));
1778
        $this->assertNotEmpty($this->crudPanel->getModel()->getEventDispatcher()->getListeners('eloquent.deleting: Backpack\CRUD\Tests\Config\Models\User'));
1779
        $this->assertNotEmpty($this->crudPanel->getModel()->getEventDispatcher()->getListeners('eloquent.deleted: Backpack\CRUD\Tests\Config\Models\User'));
1780
1781
        $this->crudPanel->getModel()->create(['name' => 'test']);
1782
1783
        $this->assertEquals('[email protected]', User::latest('id')->first()->email);
0 ignored issues
show
Bug introduced by
The property email does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property email does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
1784
    }
1785
1786
    private function getPivotInputData(array $pivotRelationData, bool $initCrud = true, bool $allowDuplicates = false)
1787
    {
1788
        $faker = Factory::create();
1789
1790
        if ($initCrud) {
1791
            $this->crudPanel->setModel(User::class);
1792
            $this->crudPanel->addFields($this->userInputFieldsNoRelationships);
1793
            $this->crudPanel->addField([
1794
                'name' => array_key_first($pivotRelationData),
1795
                'allow_duplicate_pivots' => $allowDuplicates,
1796
                'pivot_key_name' => 'id',
1797
                'subfields' => [
1798
                    [
1799
                        'name' => 'notes',
1800
                    ],
1801
1802
                ],
1803
            ]);
1804
1805
            $article = Article::create([
0 ignored issues
show
Unused Code introduced by
The assignment to $article is dead and can be removed.
Loading history...
1806
                'content' => $faker->text(),
1807
                'tags' => $faker->words(3, true),
1808
                'user_id' => 1,
1809
            ]);
1810
            $article2 = Article::create([
0 ignored issues
show
Unused Code introduced by
The assignment to $article2 is dead and can be removed.
Loading history...
1811
                'content' => $faker->text(),
1812
                'tags' => $faker->words(3, true),
1813
                'user_id' => 1,
1814
            ]);
1815
        }
1816
1817
        $inputData = [
1818
            'name' => $faker->name,
1819
            'email' => $faker->safeEmail,
1820
            'password' => Hash::make($faker->password()),
1821
            'remember_token' => null,
1822
        ];
1823
1824
        return array_merge($inputData, $pivotRelationData);
1825
    }
1826
}
1827