1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\Tests\Unit\CrudPanel; |
4
|
|
|
|
5
|
|
|
use Faker\Factory; |
6
|
|
|
use Backpack\CRUD\Tests\Unit\Models\User; |
7
|
|
|
use Backpack\CRUD\Tests\Unit\Models\Article; |
8
|
|
|
|
9
|
|
|
class CrudPanelCreateTest extends BaseDBCrudPanelTest |
10
|
|
|
{ |
11
|
|
|
private $nonRelationshipField = [ |
12
|
|
|
'name' => 'field1', |
13
|
|
|
'label' => 'Field1', |
14
|
|
|
]; |
15
|
|
|
|
16
|
|
|
private $userInputFieldsNoRelationships = [ |
17
|
|
|
[ |
18
|
|
|
'name' => 'id', |
19
|
|
|
'type' => 'hidden', |
20
|
|
|
], [ |
21
|
|
|
'name' => 'name', |
22
|
|
|
], [ |
23
|
|
|
'name' => 'email', |
24
|
|
|
'type' => 'email', |
25
|
|
|
], [ |
26
|
|
|
'name' => 'password', |
27
|
|
|
'type' => 'password', |
28
|
|
|
], |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
private $articleInputFieldsOneToMany = [ |
32
|
|
|
[ |
33
|
|
|
'name' => 'id', |
34
|
|
|
'type' => 'hidden', |
35
|
|
|
], [ |
36
|
|
|
'name' => 'content', |
37
|
|
|
], [ |
38
|
|
|
'name' => 'tags', |
39
|
|
|
], [ |
40
|
|
|
'label' => 'Author', |
41
|
|
|
'type' => 'select', |
42
|
|
|
'name' => 'user_id', |
43
|
|
|
'entity' => 'user', |
44
|
|
|
'attribute' => 'name', |
45
|
|
|
], |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
|
private $userInputFieldsManyToMany = [ |
49
|
|
|
[ |
50
|
|
|
'name' => 'id', |
51
|
|
|
'type' => 'hidden', |
52
|
|
|
], [ |
53
|
|
|
'name' => 'name', |
54
|
|
|
], [ |
55
|
|
|
'name' => 'email', |
56
|
|
|
'type' => 'email', |
57
|
|
|
], [ |
58
|
|
|
'name' => 'password', |
59
|
|
|
'type' => 'password', |
60
|
|
|
], [ |
61
|
|
|
'label' => 'Roles', |
62
|
|
|
'type' => 'select_multiple', |
63
|
|
|
'name' => 'roles', |
64
|
|
|
'entity' => 'roles', |
65
|
|
|
'attribute' => 'name', |
66
|
|
|
'pivot' => true, |
67
|
|
|
], |
68
|
|
|
]; |
69
|
|
|
|
70
|
|
|
private $userInputFieldsDotNotation = [ |
71
|
|
|
[ |
72
|
|
|
'name' => 'id', |
73
|
|
|
'type' => 'hidden', |
74
|
|
|
], [ |
75
|
|
|
'name' => 'name', |
76
|
|
|
], [ |
77
|
|
|
'name' => 'email', |
78
|
|
|
'type' => 'email', |
79
|
|
|
], [ |
80
|
|
|
'name' => 'password', |
81
|
|
|
'type' => 'password', |
82
|
|
|
], [ |
83
|
|
|
'label' => 'Roles', |
84
|
|
|
'type' => 'select_multiple', |
85
|
|
|
'name' => 'roles', |
86
|
|
|
'entity' => 'roles', |
87
|
|
|
'attribute' => 'name', |
88
|
|
|
'pivot' => true, |
89
|
|
|
], [ |
90
|
|
|
'label' => 'Street', |
91
|
|
|
'name' => 'street', |
92
|
|
|
'entity' => 'accountDetails.addresses', |
93
|
|
|
'attribute' => 'street', |
94
|
|
|
], |
95
|
|
|
]; |
96
|
|
|
|
97
|
|
|
private $userInputFieldsManyToOne = [ |
98
|
|
|
[ |
99
|
|
|
'name' => 'id', |
100
|
|
|
'type' => 'hidden', |
101
|
|
|
], [ |
102
|
|
|
'name' => 'name', |
103
|
|
|
], [ |
104
|
|
|
'name' => 'email', |
105
|
|
|
'type' => 'email', |
106
|
|
|
], [ |
107
|
|
|
'name' => 'password', |
108
|
|
|
'type' => 'password', |
109
|
|
|
], [ |
110
|
|
|
'label' => 'Roles', |
111
|
|
|
'type' => 'select_multiple', |
112
|
|
|
'name' => 'roles', |
113
|
|
|
'entity' => 'roles', |
114
|
|
|
'attribute' => 'name', |
115
|
|
|
'pivot' => true, |
116
|
|
|
], [ |
117
|
|
|
'name' => 'articles', |
118
|
|
|
'type' => 'select2_many', |
119
|
|
|
'entity' => 'articles', |
120
|
|
|
'model' => Article::class, |
121
|
|
|
'attribute' => 'content', |
122
|
|
|
], |
123
|
|
|
]; |
124
|
|
|
|
125
|
|
|
public function testCreate() |
126
|
|
|
{ |
127
|
|
|
$this->crudPanel->setModel(User::class); |
128
|
|
|
$this->crudPanel->addFields($this->userInputFieldsNoRelationships); |
129
|
|
|
$faker = Factory::create(); |
130
|
|
|
$inputData = [ |
131
|
|
|
'name' => $faker->name, |
132
|
|
|
'email' => $faker->safeEmail, |
133
|
|
|
'password' => bcrypt($faker->password()), |
134
|
|
|
]; |
135
|
|
|
|
136
|
|
|
$entry = $this->crudPanel->create($inputData); |
137
|
|
|
|
138
|
|
|
$this->assertInstanceOf(User::class, $entry); |
139
|
|
|
$this->assertEntryEquals($inputData, $entry); |
140
|
|
|
$this->assertEmpty($entry->articles); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function testCreateWithOneToOneRelationship() |
144
|
|
|
{ |
145
|
|
|
$this->markTestIncomplete('Not yet implemented'); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function testCreateWithOneToManyRelationship() |
149
|
|
|
{ |
150
|
|
|
$this->crudPanel->setModel(Article::class); |
151
|
|
|
$this->crudPanel->addFields($this->articleInputFieldsOneToMany); |
152
|
|
|
$faker = Factory::create(); |
153
|
|
|
$inputData = [ |
154
|
|
|
'content' => $faker->text(), |
155
|
|
|
'tags' => $faker->words(3, true), |
156
|
|
|
'user_id' => 1, |
157
|
|
|
'metas' => null, |
158
|
|
|
'extras' => null, |
159
|
|
|
'cast_metas' => null, |
160
|
|
|
'cast_tags' => null, |
161
|
|
|
'cast_extras' => null, |
162
|
|
|
]; |
163
|
|
|
|
164
|
|
|
$entry = $this->crudPanel->create($inputData); |
165
|
|
|
$userEntry = User::find(1); |
|
|
|
|
166
|
|
|
$article = Article::where('user_id', 1)->with('user')->get()->last(); |
167
|
|
|
|
168
|
|
|
$this->assertEntryEquals($inputData, $entry); |
169
|
|
|
$this->assertEquals($article->toArray(), $entry->toArray()); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function testCreateWithManyToOneRelationship() |
173
|
|
|
{ |
174
|
|
|
$this->crudPanel->setModel(User::class); |
175
|
|
|
$this->crudPanel->addFields($this->userInputFieldsManyToOne); |
176
|
|
|
$faker = Factory::create(); |
177
|
|
|
$inputData = [ |
178
|
|
|
'name' => $faker->name, |
179
|
|
|
'email' => $faker->safeEmail, |
180
|
|
|
'password' => bcrypt($faker->password()), |
181
|
|
|
'remember_token' => null, |
182
|
|
|
'roles' => [1, 2], |
183
|
|
|
'articles' => [1, 2], |
184
|
|
|
]; |
185
|
|
|
|
186
|
|
|
$entry = $this->crudPanel->create($inputData); |
187
|
|
|
|
188
|
|
|
$this->assertInstanceOf(User::class, $entry); |
189
|
|
|
$this->assertEntryEquals($inputData, $entry); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
public function testCreateWithManyToManyRelationship() |
193
|
|
|
{ |
194
|
|
|
$this->crudPanel->setModel(User::class); |
195
|
|
|
$this->crudPanel->addFields($this->userInputFieldsManyToMany); |
196
|
|
|
$faker = Factory::create(); |
197
|
|
|
$inputData = [ |
198
|
|
|
'name' => $faker->name, |
199
|
|
|
'email' => $faker->safeEmail, |
200
|
|
|
'password' => bcrypt($faker->password()), |
201
|
|
|
'remember_token' => null, |
202
|
|
|
'roles' => [1, 2], |
203
|
|
|
]; |
204
|
|
|
|
205
|
|
|
$entry = $this->crudPanel->create($inputData); |
206
|
|
|
|
207
|
|
|
$this->assertInstanceOf(User::class, $entry); |
208
|
|
|
$this->assertEntryEquals($inputData, $entry); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
View Code Duplication |
public function testGetRelationFields() |
|
|
|
|
212
|
|
|
{ |
213
|
|
|
$this->markTestIncomplete('Not correctly implemented'); |
214
|
|
|
|
215
|
|
|
$this->crudPanel->setModel(User::class); |
216
|
|
|
$this->crudPanel->addFields($this->userInputFieldsManyToMany, 'create'); |
217
|
|
|
|
218
|
|
|
// TODO: fix method and documentation. when 'both' is passed as the $form value, the getRelationFields searches |
219
|
|
|
// for relationship fields in the update fields. |
220
|
|
|
$relationFields = $this->crudPanel->getRelationFields('both'); |
221
|
|
|
|
222
|
|
|
$this->assertEquals($this->crudPanel->create_fields['roles'], array_last($relationFields)); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
View Code Duplication |
public function testGetRelationFieldsCreateForm() |
|
|
|
|
226
|
|
|
{ |
227
|
|
|
$this->crudPanel->setModel(User::class); |
228
|
|
|
$this->crudPanel->addFields($this->userInputFieldsManyToMany, 'create'); |
229
|
|
|
|
230
|
|
|
$relationFields = $this->crudPanel->getRelationFields('create'); |
231
|
|
|
|
232
|
|
|
$this->assertEquals($this->crudPanel->create_fields['roles'], array_last($relationFields)); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
View Code Duplication |
public function testGetRelationFieldsUpdateForm() |
|
|
|
|
236
|
|
|
{ |
237
|
|
|
$this->crudPanel->setModel(User::class); |
238
|
|
|
$this->crudPanel->addFields($this->userInputFieldsManyToMany, 'update'); |
239
|
|
|
|
240
|
|
|
$relationFields = $this->crudPanel->getRelationFields('update'); |
241
|
|
|
|
242
|
|
|
$this->assertEquals($this->crudPanel->update_fields['roles'], array_last($relationFields)); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
public function testGetRelationFieldsUnknownForm() |
246
|
|
|
{ |
247
|
|
|
$this->markTestIncomplete('Not correctly implemented'); |
248
|
|
|
|
249
|
|
|
$this->expectException(\InvalidArgumentException::class); |
250
|
|
|
|
251
|
|
|
$this->crudPanel->setModel(User::class); |
252
|
|
|
$this->crudPanel->addFields($this->userInputFieldsManyToMany); |
253
|
|
|
|
254
|
|
|
// TODO: this should throw an invalid argument exception but instead it searches for relationship fields in the |
255
|
|
|
// update fields. |
256
|
|
|
$this->crudPanel->getRelationFields('unknownForm'); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
View Code Duplication |
public function testGetRelationFieldsDotNotation() |
|
|
|
|
260
|
|
|
{ |
261
|
|
|
$this->crudPanel->setModel(User::class); |
262
|
|
|
$this->crudPanel->addFields($this->userInputFieldsDotNotation, 'create'); |
263
|
|
|
|
264
|
|
|
$relationFields = $this->crudPanel->getRelationFields('create'); |
265
|
|
|
|
266
|
|
|
$this->assertEquals($this->crudPanel->create_fields['street'], array_last($relationFields)); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
public function testGetRelationFieldsNoRelations() |
270
|
|
|
{ |
271
|
|
|
$this->crudPanel->addField($this->nonRelationshipField); |
272
|
|
|
|
273
|
|
|
$relationFields = $this->crudPanel->getRelationFields(); |
274
|
|
|
|
275
|
|
|
$this->assertEmpty($relationFields); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
public function testGetRelationFieldsNoFields() |
279
|
|
|
{ |
280
|
|
|
$relationFields = $this->crudPanel->getRelationFields(); |
281
|
|
|
|
282
|
|
|
$this->assertEmpty($relationFields); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
View Code Duplication |
public function testGetRelationFieldsWithPivot() |
|
|
|
|
286
|
|
|
{ |
287
|
|
|
$this->crudPanel->setModel(User::class); |
288
|
|
|
$this->crudPanel->addFields($this->userInputFieldsDotNotation, 'create'); |
289
|
|
|
|
290
|
|
|
$relationFields = $this->crudPanel->getRelationFieldsWithPivot('create'); |
291
|
|
|
|
292
|
|
|
$this->assertEquals($this->crudPanel->create_fields['roles'], array_last($relationFields)); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
public function testGetRelationFieldsWithPivotNoRelations() |
296
|
|
|
{ |
297
|
|
|
$this->crudPanel->setModel(User::class); |
298
|
|
|
$this->crudPanel->addFields($this->nonRelationshipField, 'create'); |
299
|
|
|
|
300
|
|
|
$relationFields = $this->crudPanel->getRelationFieldsWithPivot('create'); |
301
|
|
|
|
302
|
|
|
$this->assertEmpty($relationFields); |
303
|
|
|
} |
304
|
|
|
|
305
|
|
View Code Duplication |
public function testSyncPivot() |
|
|
|
|
306
|
|
|
{ |
307
|
|
|
$this->crudPanel->setModel(User::class); |
308
|
|
|
$this->crudPanel->addFields($this->userInputFieldsManyToMany); |
309
|
|
|
$faker = Factory::create(); |
310
|
|
|
$inputData = [ |
311
|
|
|
'name' => $faker->name, |
312
|
|
|
'email' => $faker->safeEmail, |
313
|
|
|
'password' => bcrypt($faker->password()), |
314
|
|
|
'remember_token' => null, |
315
|
|
|
'roles' => [1, 2], |
316
|
|
|
]; |
317
|
|
|
|
318
|
|
|
$entry = User::find(1); |
319
|
|
|
$this->crudPanel->syncPivot($entry, $inputData); |
320
|
|
|
|
321
|
|
|
$this->assertEquals($inputData['roles'], $entry->roles->pluck('id')->toArray()); |
322
|
|
|
} |
323
|
|
|
|
324
|
|
View Code Duplication |
public function testSyncPivotUnknownData() |
|
|
|
|
325
|
|
|
{ |
326
|
|
|
$this->crudPanel->setModel(User::class); |
327
|
|
|
$this->crudPanel->addFields($this->nonRelationshipField); |
328
|
|
|
$faker = Factory::create(); |
329
|
|
|
$inputData = [ |
330
|
|
|
'name' => $faker->name, |
331
|
|
|
'email' => $faker->safeEmail, |
332
|
|
|
'password' => bcrypt($faker->password()), |
333
|
|
|
'remember_token' => null, |
334
|
|
|
'roles' => [1, 2], |
335
|
|
|
]; |
336
|
|
|
|
337
|
|
|
$entry = User::find(1); |
338
|
|
|
$this->crudPanel->syncPivot($entry, $inputData); |
339
|
|
|
|
340
|
|
|
$this->assertEquals(1, $entry->roles->count()); |
341
|
|
|
} |
342
|
|
|
|
343
|
|
View Code Duplication |
public function testSyncPivotUnknownModel() |
|
|
|
|
344
|
|
|
{ |
345
|
|
|
$this->expectException(\BadMethodCallException::class); |
346
|
|
|
|
347
|
|
|
$this->crudPanel->setModel(User::class); |
348
|
|
|
$this->crudPanel->addFields($this->userInputFieldsManyToMany); |
349
|
|
|
$faker = Factory::create(); |
350
|
|
|
$inputData = [ |
351
|
|
|
'name' => $faker->name, |
352
|
|
|
'email' => $faker->safeEmail, |
353
|
|
|
'password' => bcrypt($faker->password()), |
354
|
|
|
'remember_token' => null, |
355
|
|
|
'roles' => [1, 2], |
356
|
|
|
]; |
357
|
|
|
|
358
|
|
|
$entry = Article::find(1); |
359
|
|
|
$this->crudPanel->syncPivot($entry, $inputData); |
360
|
|
|
} |
361
|
|
|
} |
362
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.