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 (#3654)
by
unknown
13:52
created

testCreateBelongsToWithRelationName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

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