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
Push — master ( 9087f0...ba6c2c )
by Cristian
12:30 queued 05:45
created

testGetRelatedEntriesAttributesFromBelongsToManyWithAcessor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 7
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\Article;
6
use Backpack\CRUD\Tests\Unit\Models\Role;
7
use Backpack\CRUD\Tests\Unit\Models\User;
8
use Illuminate\Database\Eloquent\ModelNotFoundException;
9
use Illuminate\Support\Collection;
10
use Illuminate\Support\Facades\DB;
11
12
class CrudPanelReadTest extends BaseDBCrudPanelTest
13
{
14
    private $relationshipColumn = [
15
        'name'      => 'user_id',
16
        'type'      => 'select',
17
        'entity'    => 'user',
18
        'attribute' => 'name',
19
    ];
20
21
    private $relationshipMultipleColumn = [
0 ignored issues
show
introduced by
The private property $relationshipMultipleColumn is not used, and could be removed.
Loading history...
22
        'name'      => 'roles',
23
        'type'      => 'select',
24
        'entity'    => 'roles',
25
        'attribute' => 'name',
26
        'model' => Role::class,
27
    ];
28
29
    private $nonRelationshipColumn = [
30
        'name'  => 'field1',
31
        'label' => 'Field1',
32
    ];
33
34
    private $articleFieldsArray = [
35
        [
36
            'name'  => 'content',
37
            'label' => 'The Content',
38
            'type'  => 'text',
39
        ],
40
        [
41
            'name'  => 'metas',
42
            'label' => 'Metas',
43
        ],
44
        [
45
            'name' => 'tags',
46
        ],
47
        [
48
            'name' => 'extras',
49
        ],
50
    ];
51
52
    private $expectedCreateFormArticleFieldsArray = [
53
        'content' => [
54
            'name'  => 'content',
55
            'label' => 'The Content',
56
            'type'  => 'text',
57
        ],
58
        'metas' => [
59
            'name'  => 'metas',
60
            'label' => 'Metas',
61
            'type'  => 'text',
62
        ],
63
        'tags' => [
64
            'name'  => 'tags',
65
            'label' => 'Tags',
66
            'type'  => 'text',
67
        ],
68
        'extras' => [
69
            'name'  => 'extras',
70
            'label' => 'Extras',
71
            'type'  => 'text',
72
        ],
73
    ];
74
75
    private $expectedUpdateFormArticleFieldsArray = [
76
        'content' => [
77
            'name'  => 'content',
78
            'label' => 'The Content',
79
            'type'  => 'text',
80
            'value' => 'Some Content',
81
        ],
82
        'metas' => [
83
            'name'  => 'metas',
84
            'label' => 'Metas',
85
            'type'  => 'text',
86
            'value' => '{"meta_title":"Meta Title Value","meta_description":"Meta Description Value"}',
87
        ],
88
        'tags' => [
89
            'name'  => 'tags',
90
            'label' => 'Tags',
91
            'type'  => 'text',
92
            'value' => '{"tags":["tag1","tag2","tag3"]}',
93
        ],
94
        'extras' => [
95
            'name'  => 'extras',
96
            'label' => 'Extras',
97
            'type'  => 'text',
98
            'value' => '{"extra_details":["detail1","detail2","detail3"]}',
99
        ],
100
        'id' => [
101
            'name'  => 'id',
102
            'type'  => 'hidden',
103
            'value' => 1,
104
        ],
105
    ];
106
107
    private $uploadField = [
108
        'name'   => 'image',
109
        'label'  => 'Image',
110
        'type'   => 'upload',
111
        'upload' => true,
112
    ];
113
114
    private $multipleUploadField = [
115
        'name'   => 'photos',
116
        'label'  => 'Photos',
117
        'type'   => 'upload_multiple',
118
        'upload' => true,
119
    ];
120
121
    public function testGetEntry()
122
    {
123
        $this->crudPanel->setModel(User::class);
124
        $user = User::find(1);
125
126
        $entry = $this->crudPanel->getEntry($user->id);
127
128
        $this->assertEquals($user, $entry);
129
    }
130
131
    public function testGetEntryWithFakes()
132
    {
133
        $this->markTestIncomplete('Not correctly implemented');
134
135
        $this->crudPanel->setModel(Article::class);
136
        $article = Article::find(1);
137
138
        $entry = $this->crudPanel->getEntry($article->id);
139
140
        // TODO: the withFakes call is needed for this to work. the state of the model should not be changed by the
141
        //       getEntry method. the transformation of the fakes columns should be kept in a different crud panel
142
        //       attribute or, at most, the getEntry method should be renamed.
143
        $article->withFakes();
144
145
        $this->assertEquals($article, $entry);
146
    }
147
148
    public function testGetEntryExists()
149
    {
150
        $this->crudPanel->setModel(User::class);
151
        $userEntry = $this->crudPanel->getEntry(1);
152
153
        $this->assertInstanceOf(User::class, $userEntry);
154
155
        $this->crudPanel->setModel(Article::class);
156
        $articleEntry = $this->crudPanel->getEntry(1);
157
158
        $this->assertInstanceOf(Article::class, $articleEntry);
159
    }
160
161
    public function testGetEntryUnknownId()
162
    {
163
        $this->expectException(ModelNotFoundException::class);
164
165
        $this->crudPanel->setModel(User::class);
166
167
        $unknownId = DB::getPdo()->lastInsertId() + 2;
168
        $this->crudPanel->getEntry($unknownId);
169
    }
170
171
    public function testAutoEagerLoadRelationshipColumns()
172
    {
173
        $this->crudPanel->setModel(Article::class);
174
        $this->crudPanel->setOperation('list');
175
        $this->crudPanel->addColumn($this->relationshipColumn);
176
177
        $this->crudPanel->autoEagerLoadRelationshipColumns();
178
179
        $this->assertArrayHasKey('user', $this->crudPanel->query->getEagerLoads());
180
    }
181
182
    public function testAutoEagerLoadRelationshipColumnsNoRelationships()
183
    {
184
        $this->crudPanel->setModel(Article::class);
185
        $this->crudPanel->addColumn($this->nonRelationshipColumn);
186
187
        $this->crudPanel->autoEagerLoadRelationshipColumns();
188
189
        $this->assertEmpty($this->crudPanel->query->getEagerLoads());
190
    }
191
192
    public function testGetEntries()
193
    {
194
        $this->crudPanel->setModel(User::class);
195
196
        $entries = $this->crudPanel->getEntries();
197
198
        $this->assertInstanceOf(Collection::class, $entries);
199
        $this->assertEquals(2, $entries->count());
200
        $this->assertEquals(User::find(1), $entries->first());
201
    }
202
203
    public function testGetEntriesWithFakes()
204
    {
205
        $this->markTestIncomplete('Not correctly implemented');
206
207
        $this->crudPanel->setModel(Article::class);
208
209
        $entries = $this->crudPanel->getEntries();
210
211
        // TODO: the getEntries method automatically adds fakes. the state of the models should not be changed by the
212
        //       getEntries method. at most, the getEntries method should be renamed.
213
        $this->assertInstanceOf(Collection::class, $entries);
214
        $this->assertEquals(1, $entries->count());
215
        $this->assertEquals(Article::find(1), $entries->first());
216
    }
217
218
    public function testGetFieldsCreateForm()
219
    {
220
        $this->crudPanel->addFields($this->articleFieldsArray);
221
222
        // TODO: update method documentation. the $form parameter does not default to 'both'.
223
        $fields = $this->crudPanel->getFields('create');
0 ignored issues
show
Unused Code introduced by
The call to Backpack\CRUD\app\Librar...\CrudPanel::getFields() has too many arguments starting with 'create'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

223
        /** @scrutinizer ignore-call */ 
224
        $fields = $this->crudPanel->getFields('create');

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
224
225
        $this->assertEquals($this->expectedCreateFormArticleFieldsArray, $fields);
226
    }
227
228
    public function testGetFieldsUpdateForm()
229
    {
230
        $this->crudPanel->setModel(Article::class);
231
232
        $this->crudPanel->setOperation('update');
233
        $this->crudPanel->addFields($this->articleFieldsArray);
234
235
        // TODO: update method documentation. the $form parameter does not default to 'both'.
236
        $fields = $this->crudPanel->getUpdateFields(1);
237
238
        $this->assertEquals($this->expectedUpdateFormArticleFieldsArray, $fields);
239
    }
240
241
    public function testHasUploadFieldsCreateForm()
242
    {
243
        $this->crudPanel->addField($this->uploadField, 'create');
0 ignored issues
show
Unused Code introduced by
The call to Backpack\CRUD\app\Librar...l\CrudPanel::addField() has too many arguments starting with 'create'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

243
        $this->crudPanel->/** @scrutinizer ignore-call */ 
244
                          addField($this->uploadField, 'create');

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
244
245
        // TODO: update method documentation. the $form parameter does not default to 'both'.
246
        $hasUploadFields = $this->crudPanel->hasUploadFields('create');
0 ignored issues
show
Unused Code introduced by
The call to Backpack\CRUD\app\Librar...anel::hasUploadFields() has too many arguments starting with 'create'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

246
        /** @scrutinizer ignore-call */ 
247
        $hasUploadFields = $this->crudPanel->hasUploadFields('create');

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
247
248
        $this->assertTrue($hasUploadFields);
249
    }
250
251
    public function testHasMultipleUploadFieldsCreateForm()
252
    {
253
        $this->crudPanel->addField($this->multipleUploadField, 'create');
0 ignored issues
show
Unused Code introduced by
The call to Backpack\CRUD\app\Librar...l\CrudPanel::addField() has too many arguments starting with 'create'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

253
        $this->crudPanel->/** @scrutinizer ignore-call */ 
254
                          addField($this->multipleUploadField, 'create');

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
254
255
        // TODO: update method documentation. the $form parameter does not default to 'both'.
256
        $hasMultipleUploadFields = $this->crudPanel->hasUploadFields('create');
0 ignored issues
show
Unused Code introduced by
The call to Backpack\CRUD\app\Librar...anel::hasUploadFields() has too many arguments starting with 'create'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

256
        /** @scrutinizer ignore-call */ 
257
        $hasMultipleUploadFields = $this->crudPanel->hasUploadFields('create');

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
257
258
        $this->assertTrue($hasMultipleUploadFields);
259
    }
260
261
    public function testHasUploadFieldsUpdateForm()
262
    {
263
        $this->crudPanel->setModel(Article::class);
264
        $this->crudPanel->addField($this->uploadField, 'update');
0 ignored issues
show
Unused Code introduced by
The call to Backpack\CRUD\app\Librar...l\CrudPanel::addField() has too many arguments starting with 'update'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

264
        $this->crudPanel->/** @scrutinizer ignore-call */ 
265
                          addField($this->uploadField, 'update');

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
265
266
        // TODO: update method documentation. the $form parameter does not default to 'both'.
267
        $hasUploadFields = $this->crudPanel->hasUploadFields('update', 1);
0 ignored issues
show
Unused Code introduced by
The call to Backpack\CRUD\app\Librar...anel::hasUploadFields() has too many arguments starting with 'update'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

267
        /** @scrutinizer ignore-call */ 
268
        $hasUploadFields = $this->crudPanel->hasUploadFields('update', 1);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
268
269
        $this->assertTrue($hasUploadFields);
270
    }
271
272
    public function testEnableDetailsRow()
273
    {
274
        $this->crudPanel->setOperation('create');
275
        $this->crudPanel->enableDetailsRow();
276
277
        $this->assertTrue($this->crudPanel->getOperationSetting('detailsRow'));
278
    }
279
280
    public function testDisableDetailsRow()
281
    {
282
        $this->crudPanel->setOperation('list');
283
        $this->crudPanel->disableDetailsRow();
284
285
        $this->assertFalse($this->crudPanel->get('list.detailsRow'));
286
    }
287
288
    public function testSetDefaultPageLength()
289
    {
290
        $pageLength = 20;
291
        $this->crudPanel->setDefaultPageLength($pageLength);
292
293
        $this->assertEquals($pageLength, $this->crudPanel->getDefaultPageLength());
294
    }
295
296
    public function testGetDefaultPageLength()
297
    {
298
        $defaultPageLength = $this->crudPanel->getDefaultPageLength();
299
300
        $this->assertEquals(25, $defaultPageLength);
301
    }
302
303
    public function testEnableExportButtons()
304
    {
305
        $this->crudPanel->enableExportButtons();
306
307
        $this->assertTrue($this->crudPanel->exportButtons());
308
    }
309
310
    public function testGetExportButtons()
311
    {
312
        $exportButtons = $this->crudPanel->exportButtons();
313
314
        $this->assertFalse($exportButtons);
315
    }
316
317
    public function testGetRelatedEntriesAttributesFromBelongsToMany()
318
    {
319
        $this->crudPanel->setModel(User::class);
320
        $this->crudPanel->setOperation('list');
321
        $user = $this->crudPanel->getModel()->where('id', 2)->first();
322
        $entries = $this->crudPanel->getRelatedEntriesAttributes($user, 'roles', 'name');
323
        $this->assertEquals([1 => 'admin', 2 => 'user'], $entries);
324
    }
325
326
    public function testGetRelatedEntriesAttributesFromBelongsToManyWithAcessor()
327
    {
328
        $this->crudPanel->setModel(User::class);
329
        $this->crudPanel->setOperation('list');
330
        $user = $this->crudPanel->getModel()->where('id', 2)->first();
331
        $entries = $this->crudPanel->getRelatedEntriesAttributes($user, 'roles', 'role_name');
332
        $this->assertEquals([1 => 'admin++', 2 => 'user++'], $entries);
333
    }
334
335
    public function testGetRelatedEntriesAttributesFromHasMany()
336
    {
337
        $this->crudPanel->setModel(User::class);
338
        $this->crudPanel->setOperation('list');
339
        $user = $this->crudPanel->getModel()->first();
340
        $entries = $this->crudPanel->getRelatedEntriesAttributes($user, 'articles', 'content');
341
        $this->assertCount(1, $entries);
342
    }
343
344
    public function testGetRelatedEntriesAttributesFromHasManyWithAcessor()
345
    {
346
        $this->crudPanel->setModel(User::class);
347
        $this->crudPanel->setOperation('list');
348
        $user = $this->crudPanel->getModel()->first();
349
        $entries = $this->crudPanel->getRelatedEntriesAttributes($user, 'articles', 'content_composed');
350
        $this->assertCount(1, $entries);
351
    }
352
353
    public function testGetRelatedEntriesAttributesFromBelongsTo()
354
    {
355
        $this->crudPanel->setModel(Article::class);
356
        $this->crudPanel->setOperation('list');
357
        $article = $this->crudPanel->getModel()->first();
358
        $entries = $this->crudPanel->getRelatedEntriesAttributes($article, 'user', 'name');
359
        $this->assertCount(1, $entries);
360
    }
361
362
    public function testGetRelatedEntriesAttributesFromBelongsToWithAcessor()
363
    {
364
        $this->crudPanel->setModel(Article::class);
365
        $this->crudPanel->setOperation('list');
366
        $article = $this->crudPanel->getModel()->first();
367
        $entries = $this->crudPanel->getRelatedEntriesAttributes($article, 'user', 'name_composed');
368
        $this->assertCount(1, $entries);
369
    }
370
371
    public function testGetRelatedEntriesAttributesFromHasOne()
372
    {
373
        $this->crudPanel->setModel(User::class);
374
        $this->crudPanel->setOperation('list');
375
        $user = $this->crudPanel->getModel()->first();
376
        $entries = $this->crudPanel->getRelatedEntriesAttributes($user, 'accountDetails', 'nickname');
377
        $this->assertCount(1, $entries);
378
    }
379
380
    public function testGetRelatedEntriesAttributesFromHasOneWithAcessor()
381
    {
382
        $this->crudPanel->setModel(User::class);
383
        $this->crudPanel->setOperation('list');
384
        $user = $this->crudPanel->getModel()->first();
385
        $entries = $this->crudPanel->getRelatedEntriesAttributes($user, 'accountDetails', 'nickname_composed');
386
        $this->assertCount(1, $entries);
387
    }
388
}
389