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
12:56
created

testGetRelationFieldsCreateForm()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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