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
16:43
created

CrudPanelCreateTest::testCreate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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