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

Test Setup Failed
Pull Request — master (#4002)
by
unknown
24:42 queued 09:36
created

testBelongsToManyWithPivotDataRelationship()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 39
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

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

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

414
        $this->crudPanel->/** @scrutinizer ignore-call */ 
415
                          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...
415
416
        $faker = Factory::create();
417
        $inputData = [
418
            'name'           => $faker->name,
419
            'email'          => $faker->safeEmail,
420
            'password'       => bcrypt($faker->password()),
421
            'remember_token' => null,
422
            'bills'          => [1],
423
        ];
424
425
        $entry = $this->crudPanel->create($inputData);
426
427
        $this->assertCount(1, $entry->bills);
428
429
        $this->assertEquals(1, $entry->bills()->first()->id);
430
431
        $inputData['bills'] = [1, 2];
432
433
        $this->crudPanel->update($entry->id, $inputData);
434
435
        $this->assertCount(2, $entry->fresh()->bills);
436
437
        $this->assertEquals([1, 2], $entry->fresh()->bills->pluck('id')->toArray());
438
    }
439
440
    public function testMorphToManyCreatableRelationship()
441
    {
442
        $this->crudPanel->setModel(User::class);
443
        $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

443
        $this->crudPanel->/** @scrutinizer ignore-call */ 
444
                          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...
444
        $this->crudPanel->addField(['name' => 'recommends', 'fields' => [
445
            [
446
                'name' => 'text',
447
            ],
448
        ]], '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

448
        $this->crudPanel->/** @scrutinizer ignore-call */ 
449
                          addField(['name' => 'recommends', 'fields' => [

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...
449
450
        $faker = Factory::create();
451
        $inputData = [
452
            'name'           => $faker->name,
453
            'email'          => $faker->safeEmail,
454
            'password'       => bcrypt($faker->password()),
455
            'remember_token' => null,
456
            'recommends'          => [
457
                [
458
                    'recommends' => 1,
459
                    'text' => 'my pivot recommend field',
460
                ],
461
            ],
462
        ];
463
464
        $entry = $this->crudPanel->create($inputData);
465
466
        $this->assertCount(1, $entry->recommends);
467
468
        $this->assertEquals(1, $entry->recommends()->first()->id);
469
470
        $inputData['recommends'] = [
471
            [
472
                'recommends' => 2,
473
                'text' => 'I changed the recommend and the pivot text',
474
            ],
475
        ];
476
477
        $this->crudPanel->update($entry->id, $inputData);
478
479
        $this->assertCount(1, $entry->fresh()->recommends);
480
481
        $this->assertEquals(2, $entry->recommends()->first()->id);
482
483
        $this->assertEquals('I changed the recommend and the pivot text', $entry->fresh()->recommends->first()->pivot->text);
484
    }
485
486
    public function testBelongsToManyWithPivotDataRelationship()
487
    {
488
        $this->crudPanel->setModel(User::class);
489
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships);
490
        $this->crudPanel->addField([
491
            'name' => 'superArticles',
492
            'fields' => [
493
                [
494
                    'name' => 'notes',
495
                ],
496
            ],
497
        ]);
498
499
        $faker = Factory::create();
500
        $articleData = [
501
            'content'     => $faker->text(),
502
            'tags'        => $faker->words(3, true),
503
            'user_id'     => 1,
504
        ];
505
506
        $article = Article::create($articleData);
507
508
        $inputData = [
509
            'name'           => $faker->name,
510
            'email'          => $faker->safeEmail,
511
            'password'       => bcrypt($faker->password()),
512
            'remember_token' => null,
513
            'superArticles'          => [
514
                [
515
                    'superArticles' => $article->id,
516
                    'notes' => 'my first article note',
517
                ],
518
            ],
519
        ];
520
521
        $entry = $this->crudPanel->create($inputData);
522
523
        $this->assertCount(1, $entry->fresh()->superArticles);
524
        $this->assertEquals('my first article note', $entry->fresh()->superArticles->first()->pivot->notes);
525
    }
526
}
527