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 (#3383)
by Cristian
17:18
created

testCreateBelongsToWithRelationName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 0
dl 0
loc 15
rs 9.9332
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
     * @group failing
208
     */
209
    public function testCreateWithOneToOneRelationship()
210
    {
211
        $this->crudPanel->setModel(User::class);
212
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships);
213
        $this->crudPanel->addFields($this->userInputHasOneRelation);
214
        $faker = Factory::create();
215
        $inputData = [
216
            'name'     => $faker->name,
217
            'email'    => $faker->safeEmail,
218
            'password' => bcrypt($faker->password()),
219
            'accountDetails' => [
220
                'nickname' => $faker->name,
221
                'profile_picture' => 'test.jpg',
222
            ],
223
        ];
224
        $entry = $this->crudPanel->create($inputData);
225
226
        $entry->load('accountDetails');
227
228
        $this->assertInstanceOf(AccountDetails::class, $entry->accountDetails);
229
        $this->assertEquals('test.jpg', $entry->accountDetails->profile_picture);
0 ignored issues
show
Bug introduced by
The property profile_picture does not seem to exist on Illuminate\Database\Eloquent\Relations\Relation.
Loading history...
230
    }
231
232
    /**
233
     * Undocumented function.
234
     *
235
     * @group failing
236
     */
237
    public function testCreateBelongsToRelationInOneToOneRelationship()
238
    {
239
        $this->crudPanel->setModel(User::class);
240
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships);
241
        $this->crudPanel->addFields($this->userInputHasOneWithBelongsToRelation);
242
        $article = Article::first();
243
        $faker = Factory::create();
244
        $inputData = [
245
            'name'     => $faker->name,
246
            'email'    => $faker->safeEmail,
247
            'password' => bcrypt($faker->password()),
248
            'accountDetails' => [
249
                'nickname' => $faker->name,
250
                'profile_picture' => 'test.jpg',
251
                'article' => $article->id,
252
            ],
253
        ];
254
255
        $entry = $this->crudPanel->create($inputData);
256
257
        $entry->load('accountDetails');
258
259
        $this->assertInstanceOf(AccountDetails::class, $entry->accountDetails);
260
        $this->assertInstanceOf(Article::class, $entry->accountDetails->article);
0 ignored issues
show
Bug introduced by
The property article does not seem to exist on Illuminate\Database\Eloquent\Relations\Relation.
Loading history...
261
    }
262
263
    public function testCreateWithOneToManyRelationship()
264
    {
265
        $this->crudPanel->setModel(Article::class);
266
        $this->crudPanel->addFields($this->articleInputFieldsOneToMany);
267
        $faker = Factory::create();
268
        $inputData = [
269
            'content'     => $faker->text(),
270
            'tags'        => $faker->words(3, true),
271
            'user_id'     => 1,
272
            'metas'       => null,
273
            'extras'      => null,
274
            'cast_metas'  => null,
275
            'cast_tags'   => null,
276
            'cast_extras' => null,
277
        ];
278
279
        $entry = $this->crudPanel->create($inputData);
280
        $userEntry = User::find(1);
0 ignored issues
show
Unused Code introduced by
The assignment to $userEntry is dead and can be removed.
Loading history...
281
        $article = Article::where('user_id', 1)->with('user')->get()->last();
282
        $this->assertEntryEquals($inputData, $entry);
283
        $this->assertEquals($article->user_id, $entry->user_id);
284
        $this->assertEquals($article->id, $entry->id);
285
    }
286
287
    public function testCreateWithManyToManyRelationship()
288
    {
289
        $this->crudPanel->setModel(User::class);
290
        $this->crudPanel->addFields($this->userInputFieldsManyToMany);
291
        $faker = Factory::create();
292
        $inputData = [
293
            'name'           => $faker->name,
294
            'email'          => $faker->safeEmail,
295
            'password'       => bcrypt($faker->password()),
296
            'remember_token' => null,
297
            'roles'          => [1, 2],
298
        ];
299
300
        $entry = $this->crudPanel->create($inputData);
301
302
        $this->assertInstanceOf(User::class, $entry);
303
        $this->assertEntryEquals($inputData, $entry);
304
    }
305
306
    public function testGetRelationFields()
307
    {
308
        $this->markTestIncomplete('Not correctly implemented');
309
310
        $this->crudPanel->setModel(User::class);
311
        $this->crudPanel->addFields($this->userInputFieldsManyToMany, 'create');
312
313
        // TODO: fix method and documentation. when 'both' is passed as the $form value, the getRelationFields searches
314
        //       for relationship fields in the update fields.
315
        $relationFields = $this->crudPanel->getRelationFields('both');
316
317
        $this->assertEquals($this->crudPanel->create_fields['roles'], Arr::last($relationFields));
318
    }
319
320
    public function testGetRelationFieldsCreateForm()
321
    {
322
        $this->crudPanel->setModel(User::class);
323
        $this->crudPanel->setOperation('create');
324
        $this->crudPanel->addFields($this->userInputFieldsManyToMany);
325
326
        $relationFields = $this->crudPanel->getRelationFields();
327
328
        $this->assertEquals($this->crudPanel->get('create.fields')['roles'], Arr::last($relationFields));
329
    }
330
331
    public function testGetRelationFieldsUpdateForm()
332
    {
333
        $this->crudPanel->setModel(User::class);
334
        $this->crudPanel->setOperation('update');
335
        $this->crudPanel->addFields($this->userInputFieldsManyToMany);
336
337
        $relationFields = $this->crudPanel->getRelationFields();
338
339
        $this->assertEquals($this->crudPanel->get('update.fields')['roles'], Arr::last($relationFields));
340
    }
341
342
    public function testGetRelationFieldsUnknownForm()
343
    {
344
        $this->markTestIncomplete('Not correctly implemented');
345
346
        $this->expectException(\InvalidArgumentException::class);
347
348
        $this->crudPanel->setModel(User::class);
349
        $this->crudPanel->addFields($this->userInputFieldsManyToMany);
350
351
        // TODO: this should throw an invalid argument exception but instead it searches for relationship fields in the
352
        //       update fields.
353
        $this->crudPanel->getRelationFields('unknownForm');
354
    }
355
356
    public function testGetRelationFieldsDotNotation()
357
    {
358
        $this->crudPanel->setModel(User::class);
359
        $this->crudPanel->setOperation('create');
360
361
        $this->crudPanel->addFields($this->userInputFieldsDotNotation);
362
363
        //get all fields with a relation
364
        $relationFields = $this->crudPanel->getRelationFields();
365
366
        $this->assertEquals($this->crudPanel->get('create.fields')['street'], Arr::last($relationFields));
367
    }
368
369
    public function testCreateHasOneRelations()
370
    {
371
        $this->crudPanel->setModel(User::class);
372
        $this->crudPanel->setOperation('create');
373
374
        $this->crudPanel->addFields($this->userInputHasOneRelation);
375
        $faker = Factory::create();
376
377
        $inputData = [
378
            'name'           => $faker->name,
379
            'email'          => $faker->safeEmail,
380
            'password'       => bcrypt($faker->password()),
381
            'remember_token' => null,
382
            'roles'          => [1, 2],
383
            'accountDetails' => [
384
                'nickname' => 'i_have_has_one',
385
                'profile_picture' => 'simple_picture.jpg',
386
            ],
387
        ];
388
        $entry = $this->crudPanel->create($inputData);
389
        $account_details = $entry->accountDetails()->first();
390
391
        $this->assertEquals($account_details->nickname, 'i_have_has_one');
392
    }
393
394
    public function testCreateBelongsToInHasOneRelations()
395
    {
396
        $this->crudPanel->setModel(User::class);
397
        $this->crudPanel->setOperation('create');
398
399
        $this->crudPanel->addFields($this->userInputHasOneWithBelongsToRelation);
400
        $faker = Factory::create();
401
402
        $inputData = [
403
            'name'           => $faker->name,
404
            'email'          => $faker->safeEmail,
405
            'password'       => bcrypt($faker->password()),
406
            'remember_token' => null,
407
            'roles'          => [1, 2],
408
            'accountDetails' => [
409
                'nickname' => 'i_have_has_one',
410
                'profile_picture' => 'simple_picture.jpg',
411
            ],
412
        ];
413
        $entry = $this->crudPanel->create($inputData);
414
        $account_details = $entry->accountDetails()->first();
415
416
        $this->assertEquals($account_details->nickname, 'i_have_has_one');
417
    }
418
419
    public function testGetRelationFieldsNoRelations()
420
    {
421
        $this->crudPanel->addField($this->nonRelationshipField);
422
423
        $relationFields = $this->crudPanel->getRelationFields();
424
425
        $this->assertEmpty($relationFields);
426
    }
427
428
    public function testGetRelationFieldsNoFields()
429
    {
430
        $relationFields = $this->crudPanel->getRelationFields();
431
432
        $this->assertEmpty($relationFields);
433
    }
434
435
    public function testGetRelationFieldsWithPivot()
436
    {
437
        $this->crudPanel->setModel(User::class);
438
        $this->crudPanel->setOperation('create');
439
        $this->crudPanel->addFields($this->userInputFieldsDotNotation);
440
441
        $relationFields = $this->crudPanel->getRelationFieldsWithPivot();
442
        $this->assertEquals($this->crudPanel->get('create.fields')['roles'], Arr::first($relationFields));
443
    }
444
445
    public function testGetRelationFieldsWithPivotNoRelations()
446
    {
447
        $this->crudPanel->setModel(User::class);
448
        $this->crudPanel->setOperation('create');
449
        $this->crudPanel->addFields($this->nonRelationshipField);
450
451
        $relationFields = $this->crudPanel->getRelationFieldsWithPivot();
452
453
        $this->assertEmpty($relationFields);
454
    }
455
456
    public function testSyncPivot()
457
    {
458
        $this->crudPanel->setModel(User::class);
459
        $this->crudPanel->addFields($this->userInputFieldsManyToMany);
460
        $faker = Factory::create();
461
        $inputData = [
462
            'name'           => $faker->name,
463
            'email'          => $faker->safeEmail,
464
            'password'       => bcrypt($faker->password()),
465
            'remember_token' => null,
466
            'roles'          => [1, 2],
467
        ];
468
469
        $entry = User::find(1);
470
        $this->crudPanel->syncPivot($entry, $inputData);
471
472
        $this->assertEquals($inputData['roles'], $entry->roles->pluck('id')->toArray());
473
    }
474
475
    public function testSyncPivotUnknownData()
476
    {
477
        $this->crudPanel->setModel(User::class);
478
        $this->crudPanel->addFields($this->nonRelationshipField);
479
        $faker = Factory::create();
480
        $inputData = [
481
            'name'           => $faker->name,
482
            'email'          => $faker->safeEmail,
483
            'password'       => bcrypt($faker->password()),
484
            'remember_token' => null,
485
            'roles'          => [1, 2],
486
        ];
487
488
        $entry = User::find(1);
489
        $this->crudPanel->syncPivot($entry, $inputData);
490
491
        $this->assertEquals(1, $entry->roles->count());
492
    }
493
494
    public function testSyncPivotUnknownModel()
495
    {
496
        $this->expectException(\BadMethodCallException::class);
497
498
        $this->crudPanel->setModel(User::class);
499
        $this->crudPanel->addFields($this->userInputFieldsManyToMany);
500
        $faker = Factory::create();
501
        $inputData = [
502
            'name'           => $faker->name,
503
            'email'          => $faker->safeEmail,
504
            'password'       => bcrypt($faker->password()),
505
            'remember_token' => null,
506
            'roles'          => [1, 2],
507
        ];
508
509
        $entry = Article::find(1);
510
        $this->crudPanel->syncPivot($entry, $inputData);
511
    }
512
}
513