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 Failed
Pull Request — master (#3410)
by
unknown
11:02
created

testGetRelationFieldsWithPivot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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