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 (#3250)
by
unknown
15:41
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\Article;
6
use Backpack\CRUD\Tests\Unit\Models\User;
7
use Backpack\CRUD\Tests\Unit\Models\Address;
8
use Backpack\CRUD\Tests\Unit\Models\AccountDetails;
9
10
use Faker\Factory;
11
use Illuminate\Support\Arr;
12
13
class CrudPanelCreateTest extends BaseDBCrudPanelTest
14
{
15
    private $nonRelationshipField = [
16
        'name'  => 'field1',
17
        'label' => 'Field1',
18
    ];
19
20
    private $userInputFieldsNoRelationships = [
21
        [
22
            'name' => 'id',
23
            'type' => 'hidden',
24
        ], [
25
            'name' => 'name',
26
        ], [
27
            'name' => 'email',
28
            'type' => 'email',
29
        ], [
30
            'name' => 'password',
31
            'type' => 'password',
32
        ],
33
    ];
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 $articleBelongsToWithRelationName = [
112
        [
113
            'name' => 'user',
114
            'type' => 'relationship'
115
        ],
116
        [
117
            'name' => 'address',
118
            'type' => 'relationship'
119
        ],
120
        [
121
            'name' => 'content',
122
        ],
123
124
    ];
125
126
    private $articleBelongsToWithRelatedKey = [
127
        [
128
            'name' => 'user_id',
129
            'type' => 'relationship'
130
        ],
131
        [
132
            'name' => 'address_id',
133
            'type' => 'relationship'
134
        ],
135
        [
136
            'name' => 'content',
137
        ],
138
139
    ];
140
141
    public function testCreate()
142
    {
143
        $this->crudPanel->setModel(User::class);
144
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships);
145
        $faker = Factory::create();
146
        $inputData = [
147
            'name'     => $faker->name,
148
            'email'    => $faker->safeEmail,
149
            'password' => bcrypt($faker->password()),
150
        ];
151
152
        $entry = $this->crudPanel->create($inputData);
153
154
        $this->assertInstanceOf(User::class, $entry);
155
        $this->assertEntryEquals($inputData, $entry);
156
        $this->assertEmpty($entry->articles);
157
    }
158
159
160
    public function testCreateBelongsToWithRelationName()
161
    {
162
        $this->crudPanel->setModel(Article::class);
163
        $this->crudPanel->addFields($this->articleBelongsToWithRelationName);
164
        $faker = Factory::create();
165
        $inputData = [
166
            'user'     => 1,
167
            'content'    => $faker->text,
168
            'address' => 1
169
        ];
170
171
        $entry = $this->crudPanel->create($inputData);
172
173
        $this->assertInstanceOf(Address::class, $entry->address);
174
        $this->assertInstanceOf(User::class, $entry->user);
175
176
    }
177
178
    public function testCreateBelongsToWithRelationKey()
179
    {
180
        $this->crudPanel->setModel(Article::class);
181
        $this->crudPanel->addFields($this->articleBelongsToWithRelatedKey);
182
        $faker = Factory::create();
183
        $inputData = [
184
            'user_id'     => 1,
185
            'content'    => $faker->text,
186
            'address_id' => 1
187
        ];
188
189
        $entry = $this->crudPanel->create($inputData);
190
191
        $this->assertInstanceOf(Address::class, $entry->address);
192
        $this->assertInstanceOf(User::class, $entry->user);
193
194
    }
195
196
    public function testCreateWithOneToOneRelationship()
197
    {
198
        $this->crudPanel->setModel(User::class);
199
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships);
200
        $this->crudPanel->addFields($this->userInputHasOneRelation);
201
        $faker = Factory::create();
202
        $inputData = [
203
            'name'     => $faker->name,
204
            'email'    => $faker->safeEmail,
205
            'password' => bcrypt($faker->password()),
206
            'accountDetails' => [
207
                'nickname' => $faker->name,
208
                'profile_picture' => 'test.jpg',
209
            ],
210
        ];
211
        $entry = $this->crudPanel->create($inputData);
0 ignored issues
show
Unused Code introduced by
The assignment to $entry is dead and can be removed.
Loading history...
212
        $this->markTestIncomplete('Has one relation is not created in tests.');
213
    }
214
215
    public function testCreateWithOneToManyRelationship()
216
    {
217
        $this->crudPanel->setModel(Article::class);
218
        $this->crudPanel->addFields($this->articleInputFieldsOneToMany);
219
        $faker = Factory::create();
220
        $inputData = [
221
            'content'     => $faker->text(),
222
            'tags'        => $faker->words(3, true),
223
            'user_id'     => 1,
224
            'metas'       => null,
225
            'extras'      => null,
226
            'cast_metas'  => null,
227
            'cast_tags'   => null,
228
            'cast_extras' => null,
229
        ];
230
231
        $entry = $this->crudPanel->create($inputData);
232
        $userEntry = User::find(1);
0 ignored issues
show
Unused Code introduced by
The assignment to $userEntry is dead and can be removed.
Loading history...
233
        $article = Article::where('user_id', 1)->with('user')->get()->last();
234
        $this->assertEntryEquals($inputData, $entry);
235
        $this->assertEquals($article->user_id, $entry->user_id);
236
        $this->assertEquals($article->id, $entry->id);
237
    }
238
239
    public function testCreateWithManyToManyRelationship()
240
    {
241
        $this->crudPanel->setModel(User::class);
242
        $this->crudPanel->addFields($this->userInputFieldsManyToMany);
243
        $faker = Factory::create();
244
        $inputData = [
245
            'name'           => $faker->name,
246
            'email'          => $faker->safeEmail,
247
            'password'       => bcrypt($faker->password()),
248
            'remember_token' => null,
249
            'roles'          => [1, 2],
250
        ];
251
252
        $entry = $this->crudPanel->create($inputData);
253
254
        $this->assertInstanceOf(User::class, $entry);
255
        $this->assertEntryEquals($inputData, $entry);
256
    }
257
258
    public function testGetRelationFields()
259
    {
260
        $this->markTestIncomplete('Not correctly implemented');
261
262
        $this->crudPanel->setModel(User::class);
263
        $this->crudPanel->addFields($this->userInputFieldsManyToMany, 'create');
264
265
        // TODO: fix method and documentation. when 'both' is passed as the $form value, the getRelationFields searches
266
        //       for relationship fields in the update fields.
267
        $relationFields = $this->crudPanel->getRelationFields('both');
268
269
        $this->assertEquals($this->crudPanel->create_fields['roles'], Arr::last($relationFields));
270
    }
271
272
    public function testGetRelationFieldsCreateForm()
273
    {
274
        $this->crudPanel->setModel(User::class);
275
        $this->crudPanel->setOperation('create');
276
        $this->crudPanel->addFields($this->userInputFieldsManyToMany);
277
278
        $relationFields = $this->crudPanel->getRelationFields();
279
280
        $this->assertEquals($this->crudPanel->get('create.fields')['roles'], Arr::last($relationFields));
281
    }
282
283
    public function testGetRelationFieldsUpdateForm()
284
    {
285
        $this->crudPanel->setModel(User::class);
286
        $this->crudPanel->setOperation('update');
287
        $this->crudPanel->addFields($this->userInputFieldsManyToMany);
288
289
        $relationFields = $this->crudPanel->getRelationFields();
290
291
        $this->assertEquals($this->crudPanel->get('update.fields')['roles'], Arr::last($relationFields));
292
    }
293
294
    public function testGetRelationFieldsUnknownForm()
295
    {
296
        $this->markTestIncomplete('Not correctly implemented');
297
298
        $this->expectException(\InvalidArgumentException::class);
299
300
        $this->crudPanel->setModel(User::class);
301
        $this->crudPanel->addFields($this->userInputFieldsManyToMany);
302
303
        // TODO: this should throw an invalid argument exception but instead it searches for relationship fields in the
304
        //       update fields.
305
        $this->crudPanel->getRelationFields('unknownForm');
306
    }
307
308
    public function testGetRelationFieldsDotNotation()
309
    {
310
        $this->crudPanel->setModel(User::class);
311
        $this->crudPanel->setOperation('create');
312
313
        $this->crudPanel->addFields($this->userInputFieldsDotNotation);
314
315
        //get all fields with a relation
316
        $relationFields = $this->crudPanel->getRelationFields();
317
        //var_dump($this->crudPanel->get('create.fields')['street']);
318
319
        $this->assertEquals($this->crudPanel->get('create.fields')['street'], Arr::last($relationFields));
320
    }
321
322
    public function testCreateHasOneRelations()
323
    {
324
        $this->crudPanel->setModel(User::class);
325
        $this->crudPanel->setOperation('create');
326
327
        $this->crudPanel->addFields($this->userInputHasOneRelation);
328
        $faker = Factory::create();
329
330
        $inputData = [
331
            'name'           => $faker->name,
332
            'email'          => $faker->safeEmail,
333
            'password'       => bcrypt($faker->password()),
334
            'remember_token' => null,
335
            'roles'          => [1, 2],
336
            'accountDetails' => [
337
                'nickname' => 'i_have_has_one',
338
                'profile_picture' => 'simple_picture.jpg',
339
            ],
340
        ];
341
        $entry = $this->crudPanel->create($inputData);
342
        $account_details = $entry->accountDetails()->first();
343
344
        $this->assertEquals($account_details->nickname, 'i_have_has_one');
345
    }
346
347
    public function testGetRelationFieldsNoRelations()
348
    {
349
        $this->crudPanel->addField($this->nonRelationshipField);
350
351
        $relationFields = $this->crudPanel->getRelationFields();
352
353
        $this->assertEmpty($relationFields);
354
    }
355
356
    public function testGetRelationFieldsNoFields()
357
    {
358
        $relationFields = $this->crudPanel->getRelationFields();
359
360
        $this->assertEmpty($relationFields);
361
    }
362
363
    public function testGetRelationFieldsWithPivot()
364
    {
365
        $this->crudPanel->setModel(User::class);
366
        $this->crudPanel->setOperation('create');
367
        $this->crudPanel->addFields($this->userInputFieldsDotNotation);
368
369
        $relationFields = $this->crudPanel->getRelationFieldsWithPivot();
370
        $this->assertEquals($this->crudPanel->get('create.fields')['roles'], Arr::first($relationFields));
371
    }
372
373
    public function testGetRelationFieldsWithPivotNoRelations()
374
    {
375
        $this->crudPanel->setModel(User::class);
376
        $this->crudPanel->setOperation('create');
377
        $this->crudPanel->addFields($this->nonRelationshipField);
378
379
        $relationFields = $this->crudPanel->getRelationFieldsWithPivot();
380
381
        $this->assertEmpty($relationFields);
382
    }
383
384
    public function testCreateOneToOneRelationships()
385
    {
386
    }
387
388
    public function testSyncPivot()
389
    {
390
        $this->crudPanel->setModel(User::class);
391
        $this->crudPanel->addFields($this->userInputFieldsManyToMany);
392
        $faker = Factory::create();
393
        $inputData = [
394
            'name'           => $faker->name,
395
            'email'          => $faker->safeEmail,
396
            'password'       => bcrypt($faker->password()),
397
            'remember_token' => null,
398
            'roles'          => [1, 2],
399
        ];
400
401
        $entry = User::find(1);
402
        $this->crudPanel->syncPivot($entry, $inputData);
403
404
        $this->assertEquals($inputData['roles'], $entry->roles->pluck('id')->toArray());
405
    }
406
407
    public function testSyncPivotUnknownData()
408
    {
409
        $this->crudPanel->setModel(User::class);
410
        $this->crudPanel->addFields($this->nonRelationshipField);
411
        $faker = Factory::create();
412
        $inputData = [
413
            'name'           => $faker->name,
414
            'email'          => $faker->safeEmail,
415
            'password'       => bcrypt($faker->password()),
416
            'remember_token' => null,
417
            'roles'          => [1, 2],
418
        ];
419
420
        $entry = User::find(1);
421
        $this->crudPanel->syncPivot($entry, $inputData);
422
423
        $this->assertEquals(1, $entry->roles->count());
424
    }
425
426
    public function testSyncPivotUnknownModel()
427
    {
428
        $this->expectException(\BadMethodCallException::class);
429
430
        $this->crudPanel->setModel(User::class);
431
        $this->crudPanel->addFields($this->userInputFieldsManyToMany);
432
        $faker = Factory::create();
433
        $inputData = [
434
            'name'           => $faker->name,
435
            'email'          => $faker->safeEmail,
436
            'password'       => bcrypt($faker->password()),
437
            'remember_token' => null,
438
            'roles'          => [1, 2],
439
        ];
440
441
        $entry = Article::find(1);
442
        $this->crudPanel->syncPivot($entry, $inputData);
443
    }
444
}
445