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 — master (#3410)
by
unknown
11:29
created

CrudPanelCreateTest::testCreateHasOneRelations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

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