1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\Tests\Unit\CrudPanel; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
use Illuminate\Support\Facades\DB; |
7
|
|
|
use Backpack\CRUD\Tests\Unit\Models\User; |
8
|
|
|
use Backpack\CRUD\Tests\Unit\Models\Article; |
9
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException; |
10
|
|
|
|
11
|
|
|
class CrudPanelReadTest extends BaseDBCrudPanelTest |
12
|
|
|
{ |
13
|
|
|
private $relationshipColumn = [ |
14
|
|
|
'name' => 'user_id', |
15
|
|
|
'type' => 'select', |
16
|
|
|
'entity' => 'user', |
17
|
|
|
'attribute' => 'name', |
18
|
|
|
]; |
19
|
|
|
|
20
|
|
|
private $nonRelationshipColumn = [ |
21
|
|
|
'name' => 'field1', |
22
|
|
|
'label' => 'Field1', |
23
|
|
|
]; |
24
|
|
|
|
25
|
|
|
private $articleFieldsArray = [ |
26
|
|
|
[ |
27
|
|
|
'name' => 'content', |
28
|
|
|
'label' => 'The Content', |
29
|
|
|
'type' => 'text', |
30
|
|
|
], |
31
|
|
|
[ |
32
|
|
|
'name' => 'metas', |
33
|
|
|
'label' => 'Metas', |
34
|
|
|
], |
35
|
|
|
[ |
36
|
|
|
'name' => 'tags', |
37
|
|
|
], |
38
|
|
|
[ |
39
|
|
|
'name' => 'extras', |
40
|
|
|
], |
41
|
|
|
]; |
42
|
|
|
|
43
|
|
|
private $expectedCreateFormArticleFieldsArray = [ |
44
|
|
|
'content' => [ |
45
|
|
|
'name' => 'content', |
46
|
|
|
'label' => 'The Content', |
47
|
|
|
'type' => 'text', |
48
|
|
|
], |
49
|
|
|
'metas' => [ |
50
|
|
|
'name' => 'metas', |
51
|
|
|
'label' => 'Metas', |
52
|
|
|
'type' => 'text', |
53
|
|
|
], |
54
|
|
|
'tags' => [ |
55
|
|
|
'name' => 'tags', |
56
|
|
|
'label' => 'Tags', |
57
|
|
|
'type' => 'text', |
58
|
|
|
], |
59
|
|
|
'extras' => [ |
60
|
|
|
'name' => 'extras', |
61
|
|
|
'label' => 'Extras', |
62
|
|
|
'type' => 'text', |
63
|
|
|
], |
64
|
|
|
]; |
65
|
|
|
|
66
|
|
|
private $expectedUpdateFormArticleFieldsArray = [ |
67
|
|
|
'content' => [ |
68
|
|
|
'name' => 'content', |
69
|
|
|
'label' => 'The Content', |
70
|
|
|
'type' => 'text', |
71
|
|
|
'value' => 'Some Content', |
72
|
|
|
], |
73
|
|
|
'metas' => [ |
74
|
|
|
'name' => 'metas', |
75
|
|
|
'label' => 'Metas', |
76
|
|
|
'type' => 'text', |
77
|
|
|
'value' => '{"meta_title":"Meta Title Value","meta_description":"Meta Description Value"}', |
78
|
|
|
], |
79
|
|
|
'tags' => [ |
80
|
|
|
'name' => 'tags', |
81
|
|
|
'label' => 'Tags', |
82
|
|
|
'type' => 'text', |
83
|
|
|
'value' => '{"tags":["tag1","tag2","tag3"]}', |
84
|
|
|
], |
85
|
|
|
'extras' => [ |
86
|
|
|
'name' => 'extras', |
87
|
|
|
'label' => 'Extras', |
88
|
|
|
'type' => 'text', |
89
|
|
|
'value' => '{"extra_details":["detail1","detail2","detail3"]}', |
90
|
|
|
], |
91
|
|
|
'id' => [ |
92
|
|
|
'name' => 'id', |
93
|
|
|
'type' => 'hidden', |
94
|
|
|
'value' => 1, |
95
|
|
|
], |
96
|
|
|
]; |
97
|
|
|
|
98
|
|
|
private $uploadField = [ |
99
|
|
|
'name' => 'image', |
100
|
|
|
'label' => 'Image', |
101
|
|
|
'type' => 'upload', |
102
|
|
|
'upload' => true, |
103
|
|
|
]; |
104
|
|
|
|
105
|
|
|
private $multipleUploadField = [ |
106
|
|
|
'name' => 'photos', |
107
|
|
|
'label' => 'Photos', |
108
|
|
|
'type' => 'upload_multiple', |
109
|
|
|
'upload' => true, |
110
|
|
|
]; |
111
|
|
|
|
112
|
1 |
View Code Duplication |
public function testGetEntry() |
|
|
|
|
113
|
|
|
{ |
114
|
1 |
|
$this->crudPanel->setModel(User::class); |
115
|
1 |
|
$user = User::find(1); |
116
|
|
|
|
117
|
1 |
|
$entry = $this->crudPanel->getEntry($user->id); |
118
|
|
|
|
119
|
1 |
|
$this->assertEquals($user, $entry); |
120
|
1 |
|
} |
121
|
|
|
|
122
|
|
View Code Duplication |
public function testGetEntryWithFakes() |
|
|
|
|
123
|
|
|
{ |
124
|
|
|
$this->markTestIncomplete('Not correctly implemented'); |
125
|
|
|
|
126
|
|
|
$this->crudPanel->setModel(Article::class); |
127
|
|
|
$article = Article::find(1); |
128
|
|
|
|
129
|
|
|
$entry = $this->crudPanel->getEntry($article->id); |
130
|
|
|
|
131
|
|
|
// TODO: the withFakes call is needed for this to work. the state of the model should not be changed by the |
132
|
|
|
// getEntry method. the transformation of the fakes columns should be kept in a different crud panel |
133
|
|
|
// attribute or, at most, the getEntry method should be renamed. |
134
|
|
|
$article->withFakes(); |
135
|
|
|
|
136
|
|
|
$this->assertEquals($article, $entry); |
137
|
|
|
} |
138
|
|
|
|
139
|
1 |
|
public function testGetEntryExists() |
140
|
|
|
{ |
141
|
1 |
|
$this->crudPanel->setModel(User::class); |
142
|
1 |
|
$userEntry = $this->crudPanel->getEntry(1); |
143
|
|
|
|
144
|
1 |
|
$this->assertInstanceOf(User::class, $userEntry); |
145
|
|
|
|
146
|
1 |
|
$this->crudPanel->setModel(Article::class); |
147
|
1 |
|
$articleEntry = $this->crudPanel->getEntry(1); |
148
|
|
|
|
149
|
1 |
|
$this->assertInstanceOf(Article::class, $articleEntry); |
150
|
1 |
|
} |
151
|
|
|
|
152
|
1 |
View Code Duplication |
public function testGetEntryUnknownId() |
|
|
|
|
153
|
|
|
{ |
154
|
1 |
|
$this->setExpectedException(ModelNotFoundException::class); |
155
|
|
|
|
156
|
1 |
|
$this->crudPanel->setModel(User::class); |
157
|
|
|
|
158
|
1 |
|
$unknownId = DB::getPdo()->lastInsertId() + 1; |
159
|
1 |
|
$this->crudPanel->getEntry($unknownId); |
160
|
|
|
} |
161
|
|
|
|
162
|
1 |
|
public function testAutoEagerLoadRelationshipColumns() |
163
|
|
|
{ |
164
|
1 |
|
$this->crudPanel->setModel(Article::class); |
165
|
1 |
|
$this->crudPanel->addColumn($this->relationshipColumn); |
166
|
|
|
|
167
|
1 |
|
$this->crudPanel->autoEagerLoadRelationshipColumns(); |
168
|
|
|
|
169
|
1 |
|
$this->assertArrayHasKey('user', $this->crudPanel->query->getEagerLoads()); |
170
|
1 |
|
} |
171
|
|
|
|
172
|
1 |
|
public function testAutoEagerLoadRelationshipColumnsNoRelationships() |
173
|
|
|
{ |
174
|
1 |
|
$this->crudPanel->setModel(Article::class); |
175
|
1 |
|
$this->crudPanel->addColumn($this->nonRelationshipColumn); |
176
|
|
|
|
177
|
1 |
|
$this->crudPanel->autoEagerLoadRelationshipColumns(); |
178
|
|
|
|
179
|
1 |
|
$this->assertEmpty($this->crudPanel->query->getEagerLoads()); |
180
|
1 |
|
} |
181
|
|
|
|
182
|
1 |
View Code Duplication |
public function testGetEntries() |
|
|
|
|
183
|
|
|
{ |
184
|
1 |
|
$this->crudPanel->setModel(User::class); |
185
|
|
|
|
186
|
1 |
|
$entries = $this->crudPanel->getEntries(); |
187
|
|
|
|
188
|
1 |
|
$this->assertInstanceOf(Collection::class, $entries); |
189
|
1 |
|
$this->assertEquals(1, $entries->count()); |
190
|
1 |
|
$this->assertEquals(User::find(1), $entries->first()); |
191
|
1 |
|
} |
192
|
|
|
|
193
|
|
View Code Duplication |
public function testGetEntriesWithFakes() |
|
|
|
|
194
|
|
|
{ |
195
|
|
|
$this->markTestIncomplete('Not correctly implemented'); |
196
|
|
|
|
197
|
|
|
$this->crudPanel->setModel(Article::class); |
198
|
|
|
|
199
|
|
|
$entries = $this->crudPanel->getEntries(); |
200
|
|
|
|
201
|
|
|
// TODO: the getEntries method automatically adds fakes. the state of the models should not be changed by the |
202
|
|
|
// getEntries method. at most, the getEntries method should be renamed. |
203
|
|
|
$this->assertInstanceOf(Collection::class, $entries); |
204
|
|
|
$this->assertEquals(1, $entries->count()); |
205
|
|
|
$this->assertEquals(Article::find(1), $entries->first()); |
206
|
|
|
} |
207
|
|
|
|
208
|
1 |
|
public function testGetFieldsCreateForm() |
209
|
|
|
{ |
210
|
1 |
|
$this->crudPanel->addFields($this->articleFieldsArray); |
211
|
|
|
|
212
|
|
|
// TODO: update method documentation. the $form parameter does not default to 'both'. |
213
|
1 |
|
$fields = $this->crudPanel->getFields('create'); |
214
|
|
|
|
215
|
1 |
|
$this->assertEquals($this->expectedCreateFormArticleFieldsArray, $fields); |
216
|
1 |
|
} |
217
|
|
|
|
218
|
1 |
|
public function testGetFieldsUpdateForm() |
219
|
|
|
{ |
220
|
1 |
|
$this->crudPanel->setModel(Article::class); |
221
|
|
|
|
222
|
1 |
|
$this->crudPanel->addFields($this->articleFieldsArray); |
223
|
|
|
|
224
|
|
|
// TODO: update method documentation. the $form parameter does not default to 'both'. |
225
|
1 |
|
$fields = $this->crudPanel->getFields('update', 1); |
|
|
|
|
226
|
|
|
|
227
|
1 |
|
$this->assertEquals($this->expectedUpdateFormArticleFieldsArray, $fields); |
228
|
1 |
|
} |
229
|
|
|
|
230
|
1 |
|
public function testGetFieldsUpdateFormUnknownId() |
231
|
|
|
{ |
232
|
1 |
|
$this->setExpectedException(ModelNotFoundException::class); |
233
|
|
|
|
234
|
1 |
|
$this->crudPanel->setModel(Article::class); |
235
|
|
|
|
236
|
1 |
|
$this->crudPanel->addFields($this->articleFieldsArray); |
237
|
|
|
|
238
|
|
|
// TODO: update method documentation. the $form parameter does not default to 'both'. |
239
|
1 |
|
$unknownId = DB::getPdo()->lastInsertId() + 1; |
240
|
1 |
|
$this->crudPanel->getFields('update', $unknownId); |
|
|
|
|
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
public function testGetFieldsUnknownForm() |
244
|
|
|
{ |
245
|
|
|
$this->markTestIncomplete('Not correctly implemented'); |
246
|
|
|
|
247
|
|
|
$this->setExpectedException(\InvalidArgumentException::class); |
248
|
|
|
|
249
|
|
|
$this->crudPanel->addFields($this->articleFieldsArray); |
250
|
|
|
|
251
|
|
|
// TODO: this should throw an invalid argument exception but doesn't because the getFields method returns the |
252
|
|
|
// create fields in case of an unknown form type. |
253
|
|
|
$this->crudPanel->getFields('unknownForm'); |
254
|
|
|
} |
255
|
|
|
|
256
|
1 |
|
public function testHasUploadFieldsCreateForm() |
257
|
|
|
{ |
258
|
1 |
|
$this->crudPanel->addField($this->uploadField, 'create'); |
259
|
|
|
|
260
|
|
|
// TODO: update method documentation. the $form parameter does not default to 'both'. |
261
|
1 |
|
$hasUploadFields = $this->crudPanel->hasUploadFields('create'); |
262
|
|
|
|
263
|
1 |
|
$this->assertTrue($hasUploadFields); |
264
|
1 |
|
} |
265
|
|
|
|
266
|
1 |
|
public function testHasMultipleUploadFieldsCreateForm() |
267
|
|
|
{ |
268
|
1 |
|
$this->crudPanel->addField($this->multipleUploadField, 'create'); |
269
|
|
|
|
270
|
|
|
// TODO: update method documentation. the $form parameter does not default to 'both'. |
271
|
1 |
|
$hasMultipleUploadFields = $this->crudPanel->hasUploadFields('create'); |
272
|
|
|
|
273
|
1 |
|
$this->assertTrue($hasMultipleUploadFields); |
274
|
1 |
|
} |
275
|
|
|
|
276
|
1 |
View Code Duplication |
public function testHasUploadFieldsUpdateForm() |
|
|
|
|
277
|
|
|
{ |
278
|
1 |
|
$this->crudPanel->setModel(Article::class); |
279
|
1 |
|
$this->crudPanel->addField($this->uploadField, 'update'); |
280
|
|
|
|
281
|
|
|
// TODO: update method documentation. the $form parameter does not default to 'both'. |
282
|
1 |
|
$hasUploadFields = $this->crudPanel->hasUploadFields('update', 1); |
|
|
|
|
283
|
|
|
|
284
|
1 |
|
$this->assertTrue($hasUploadFields); |
285
|
1 |
|
} |
286
|
|
|
|
287
|
1 |
View Code Duplication |
public function testHasUploadFieldsUpdateFormUnknownId() |
|
|
|
|
288
|
|
|
{ |
289
|
1 |
|
$this->setExpectedException(ModelNotFoundException::class); |
290
|
|
|
|
291
|
1 |
|
$this->crudPanel->setModel(Article::class); |
292
|
1 |
|
$this->crudPanel->addField($this->uploadField, 'update'); |
293
|
|
|
|
294
|
1 |
|
$unknownId = DB::getPdo()->lastInsertId() + 1; |
295
|
1 |
|
$this->crudPanel->hasUploadFields('update', $unknownId); |
|
|
|
|
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
public function testEnableDetailsRow() |
299
|
|
|
{ |
300
|
|
|
$this->crudPanel->enableDetailsRow(); |
301
|
|
|
|
302
|
|
|
$this->assertTrue($this->crudPanel->details_row); |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
public function testDisableDetailsRow() |
306
|
|
|
{ |
307
|
|
|
$this->crudPanel->disableDetailsRow(); |
308
|
|
|
|
309
|
|
|
$this->assertFalse($this->crudPanel->details_row); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
public function testSetDefaultPageLength() |
313
|
|
|
{ |
314
|
|
|
$pageLength = 20; |
315
|
|
|
$this->crudPanel->setDefaultPageLength($pageLength); |
316
|
|
|
|
317
|
|
|
$this->assertEquals($pageLength, $this->crudPanel->getDefaultPageLength()); |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
public function testGetDefaultPageLength() |
321
|
|
|
{ |
322
|
|
|
$defaultPageLength = $this->crudPanel->getDefaultPageLength(); |
323
|
|
|
|
324
|
|
|
$this->assertEquals(25, $defaultPageLength); |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
public function testEnableAjaxTable() |
328
|
|
|
{ |
329
|
|
|
$this->crudPanel->enableAjaxTable(); |
330
|
|
|
|
331
|
|
|
$this->assertTrue($this->crudPanel->ajaxTable()); |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
public function testGetAjaxTable() |
335
|
|
|
{ |
336
|
|
|
$ajaxTable = $this->crudPanel->ajaxTable(); |
337
|
|
|
|
338
|
|
|
$this->assertFalse($ajaxTable); |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
public function testEnableExportButtons() |
342
|
|
|
{ |
343
|
|
|
$this->crudPanel->enableExportButtons(); |
344
|
|
|
|
345
|
|
|
$this->assertTrue($this->crudPanel->exportButtons()); |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
public function testGetExportButtons() |
349
|
|
|
{ |
350
|
|
|
$exportButtons = $this->crudPanel->exportButtons(); |
351
|
|
|
|
352
|
|
|
$this->assertFalse($exportButtons); |
353
|
|
|
} |
354
|
|
|
} |
355
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.