|
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
|
|
|
'name' => 'articles', |
|
111
|
|
|
'type' => 'select2_many', |
|
112
|
|
|
'entity' => 'articles', |
|
113
|
|
|
'attribute' => 'content', |
|
114
|
|
|
], |
|
115
|
|
|
]; |
|
116
|
|
|
|
|
117
|
|
|
public function testCreate() |
|
118
|
|
|
{ |
|
119
|
|
|
$this->crudPanel->setModel(User::class); |
|
120
|
|
|
$this->crudPanel->addFields($this->userInputFieldsNoRelationships); |
|
121
|
|
|
$faker = Factory::create(); |
|
122
|
|
|
$inputData = [ |
|
123
|
|
|
'name' => $faker->name, |
|
124
|
|
|
'email' => $faker->safeEmail, |
|
125
|
|
|
'password' => bcrypt($faker->password()), |
|
126
|
|
|
]; |
|
127
|
|
|
|
|
128
|
|
|
$entry = $this->crudPanel->create($inputData); |
|
129
|
|
|
|
|
130
|
|
|
$this->assertInstanceOf(User::class, $entry); |
|
131
|
|
|
$this->assertEntryEquals($inputData, $entry); |
|
132
|
|
|
$this->assertEmpty($entry->articles); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
public function testCreateWithOneToOneRelationship() |
|
136
|
|
|
{ |
|
137
|
|
|
$this->markTestIncomplete('Not yet implemented'); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
public function testCreateWithOneToManyRelationship() |
|
141
|
|
|
{ |
|
142
|
|
|
$this->crudPanel->setModel(Article::class); |
|
143
|
|
|
$this->crudPanel->addFields($this->articleInputFieldsOneToMany); |
|
144
|
|
|
$faker = Factory::create(); |
|
145
|
|
|
$inputData = [ |
|
146
|
|
|
'content' => $faker->text(), |
|
147
|
|
|
'tags' => $faker->words(3, true), |
|
148
|
|
|
'user_id' => 1, |
|
149
|
|
|
'metas' => null, |
|
150
|
|
|
'extras' => null, |
|
151
|
|
|
'cast_metas' => null, |
|
152
|
|
|
'cast_tags' => null, |
|
153
|
|
|
'cast_extras' => null, |
|
154
|
|
|
]; |
|
155
|
|
|
|
|
156
|
|
|
$entry = $this->crudPanel->create($inputData); |
|
157
|
|
|
$userEntry = User::find(1); |
|
|
|
|
|
|
158
|
|
|
$article = Article::where('user_id', 1)->with('user')->get()->last(); |
|
159
|
|
|
|
|
160
|
|
|
$this->assertEntryEquals($inputData, $entry); |
|
161
|
|
|
$this->assertEquals($article->toArray(), $entry->toArray()); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
public function testCreateWithManyToOneRelationship() |
|
165
|
|
|
{ |
|
166
|
|
|
$this->crudPanel->setModel(User::class); |
|
167
|
|
|
$this->crudPanel->addFields($this->userInputFieldsManyToOne); |
|
168
|
|
|
$faker = Factory::create(); |
|
169
|
|
|
$inputData = [ |
|
170
|
|
|
'name' => $faker->name, |
|
171
|
|
|
'email' => $faker->safeEmail, |
|
172
|
|
|
'password' => bcrypt($faker->password()), |
|
173
|
|
|
'remember_token' => null, |
|
174
|
|
|
'roles' => [1, 2], |
|
175
|
|
|
'articles' => [1, 2], |
|
176
|
|
|
]; |
|
177
|
|
|
|
|
178
|
|
|
$entry = $this->crudPanel->create($inputData); |
|
179
|
|
|
|
|
180
|
|
|
$this->assertInstanceOf(User::class, $entry); |
|
181
|
|
|
$this->assertEntryEquals($inputData, $entry); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
public function testCreateWithManyToManyRelationship() |
|
185
|
|
|
{ |
|
186
|
|
|
$this->crudPanel->setModel(User::class); |
|
187
|
|
|
$this->crudPanel->addFields($this->userInputFieldsManyToMany); |
|
188
|
|
|
$faker = Factory::create(); |
|
189
|
|
|
$inputData = [ |
|
190
|
|
|
'name' => $faker->name, |
|
191
|
|
|
'email' => $faker->safeEmail, |
|
192
|
|
|
'password' => bcrypt($faker->password()), |
|
193
|
|
|
'remember_token' => null, |
|
194
|
|
|
'roles' => [1, 2], |
|
195
|
|
|
]; |
|
196
|
|
|
|
|
197
|
|
|
$entry = $this->crudPanel->create($inputData); |
|
198
|
|
|
|
|
199
|
|
|
$this->assertInstanceOf(User::class, $entry); |
|
200
|
|
|
$this->assertEntryEquals($inputData, $entry); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
View Code Duplication |
public function testGetRelationFields() |
|
|
|
|
|
|
204
|
|
|
{ |
|
205
|
|
|
$this->markTestIncomplete('Not correctly implemented'); |
|
206
|
|
|
|
|
207
|
|
|
$this->crudPanel->setModel(User::class); |
|
208
|
|
|
$this->crudPanel->addFields($this->userInputFieldsManyToMany, 'create'); |
|
209
|
|
|
|
|
210
|
|
|
// TODO: fix method and documentation. when 'both' is passed as the $form value, the getRelationFields searches |
|
211
|
|
|
// for relationship fields in the update fields. |
|
212
|
|
|
$relationFields = $this->crudPanel->getRelationFields('both'); |
|
213
|
|
|
|
|
214
|
|
|
$this->assertEquals($this->crudPanel->create_fields['roles'], array_last($relationFields)); |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
View Code Duplication |
public function testGetRelationFieldsCreateForm() |
|
|
|
|
|
|
218
|
|
|
{ |
|
219
|
|
|
$this->crudPanel->setModel(User::class); |
|
220
|
|
|
$this->crudPanel->addFields($this->userInputFieldsManyToMany, 'create'); |
|
221
|
|
|
|
|
222
|
|
|
$relationFields = $this->crudPanel->getRelationFields('create'); |
|
223
|
|
|
|
|
224
|
|
|
$this->assertEquals($this->crudPanel->create_fields['roles'], array_last($relationFields)); |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
View Code Duplication |
public function testGetRelationFieldsUpdateForm() |
|
|
|
|
|
|
228
|
|
|
{ |
|
229
|
|
|
$this->crudPanel->setModel(User::class); |
|
230
|
|
|
$this->crudPanel->addFields($this->userInputFieldsManyToMany, 'update'); |
|
231
|
|
|
|
|
232
|
|
|
$relationFields = $this->crudPanel->getRelationFields('update'); |
|
233
|
|
|
|
|
234
|
|
|
$this->assertEquals($this->crudPanel->update_fields['roles'], array_last($relationFields)); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
public function testGetRelationFieldsUnknownForm() |
|
238
|
|
|
{ |
|
239
|
|
|
$this->markTestIncomplete('Not correctly implemented'); |
|
240
|
|
|
|
|
241
|
|
|
$this->expectException(\InvalidArgumentException::class); |
|
242
|
|
|
|
|
243
|
|
|
$this->crudPanel->setModel(User::class); |
|
244
|
|
|
$this->crudPanel->addFields($this->userInputFieldsManyToMany); |
|
245
|
|
|
|
|
246
|
|
|
// TODO: this should throw an invalid argument exception but instead it searches for relationship fields in the |
|
247
|
|
|
// update fields. |
|
248
|
|
|
$this->crudPanel->getRelationFields('unknownForm'); |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
View Code Duplication |
public function testGetRelationFieldsDotNotation() |
|
|
|
|
|
|
252
|
|
|
{ |
|
253
|
|
|
$this->crudPanel->setModel(User::class); |
|
254
|
|
|
$this->crudPanel->addFields($this->userInputFieldsDotNotation, 'create'); |
|
255
|
|
|
|
|
256
|
|
|
$relationFields = $this->crudPanel->getRelationFields('create'); |
|
257
|
|
|
|
|
258
|
|
|
$this->assertEquals($this->crudPanel->create_fields['street'], array_last($relationFields)); |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
public function testGetRelationFieldsNoRelations() |
|
262
|
|
|
{ |
|
263
|
|
|
$this->crudPanel->addField($this->nonRelationshipField); |
|
264
|
|
|
|
|
265
|
|
|
$relationFields = $this->crudPanel->getRelationFields(); |
|
266
|
|
|
|
|
267
|
|
|
$this->assertEmpty($relationFields); |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
public function testGetRelationFieldsNoFields() |
|
271
|
|
|
{ |
|
272
|
|
|
$relationFields = $this->crudPanel->getRelationFields(); |
|
273
|
|
|
|
|
274
|
|
|
$this->assertEmpty($relationFields); |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
View Code Duplication |
public function testGetRelationFieldsWithPivot() |
|
|
|
|
|
|
278
|
|
|
{ |
|
279
|
|
|
$this->crudPanel->setModel(User::class); |
|
280
|
|
|
$this->crudPanel->addFields($this->userInputFieldsDotNotation, 'create'); |
|
281
|
|
|
|
|
282
|
|
|
$relationFields = $this->crudPanel->getRelationFieldsWithPivot('create'); |
|
283
|
|
|
|
|
284
|
|
|
$this->assertEquals($this->crudPanel->create_fields['roles'], array_last($relationFields)); |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
public function testGetRelationFieldsWithPivotNoRelations() |
|
288
|
|
|
{ |
|
289
|
|
|
$this->crudPanel->setModel(User::class); |
|
290
|
|
|
$this->crudPanel->addFields($this->nonRelationshipField, 'create'); |
|
291
|
|
|
|
|
292
|
|
|
$relationFields = $this->crudPanel->getRelationFieldsWithPivot('create'); |
|
293
|
|
|
|
|
294
|
|
|
$this->assertEmpty($relationFields); |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
View Code Duplication |
public function testSyncPivot() |
|
|
|
|
|
|
298
|
|
|
{ |
|
299
|
|
|
$this->crudPanel->setModel(User::class); |
|
300
|
|
|
$this->crudPanel->addFields($this->userInputFieldsManyToMany); |
|
301
|
|
|
$faker = Factory::create(); |
|
302
|
|
|
$inputData = [ |
|
303
|
|
|
'name' => $faker->name, |
|
304
|
|
|
'email' => $faker->safeEmail, |
|
305
|
|
|
'password' => bcrypt($faker->password()), |
|
306
|
|
|
'remember_token' => null, |
|
307
|
|
|
'roles' => [1, 2], |
|
308
|
|
|
]; |
|
309
|
|
|
|
|
310
|
|
|
$entry = User::find(1); |
|
311
|
|
|
$this->crudPanel->syncPivot($entry, $inputData); |
|
312
|
|
|
|
|
313
|
|
|
$this->assertEquals($inputData['roles'], $entry->roles->pluck('id')->toArray()); |
|
314
|
|
|
} |
|
315
|
|
|
|
|
316
|
|
View Code Duplication |
public function testSyncPivotUnknownData() |
|
|
|
|
|
|
317
|
|
|
{ |
|
318
|
|
|
$this->crudPanel->setModel(User::class); |
|
319
|
|
|
$this->crudPanel->addFields($this->nonRelationshipField); |
|
320
|
|
|
$faker = Factory::create(); |
|
321
|
|
|
$inputData = [ |
|
322
|
|
|
'name' => $faker->name, |
|
323
|
|
|
'email' => $faker->safeEmail, |
|
324
|
|
|
'password' => bcrypt($faker->password()), |
|
325
|
|
|
'remember_token' => null, |
|
326
|
|
|
'roles' => [1, 2], |
|
327
|
|
|
]; |
|
328
|
|
|
|
|
329
|
|
|
$entry = User::find(1); |
|
330
|
|
|
$this->crudPanel->syncPivot($entry, $inputData); |
|
331
|
|
|
|
|
332
|
|
|
$this->assertEquals(1, $entry->roles->count()); |
|
333
|
|
|
} |
|
334
|
|
|
|
|
335
|
|
View Code Duplication |
public function testSyncPivotUnknownModel() |
|
|
|
|
|
|
336
|
|
|
{ |
|
337
|
|
|
$this->expectException(\BadMethodCallException::class); |
|
338
|
|
|
|
|
339
|
|
|
$this->crudPanel->setModel(User::class); |
|
340
|
|
|
$this->crudPanel->addFields($this->userInputFieldsManyToMany); |
|
341
|
|
|
$faker = Factory::create(); |
|
342
|
|
|
$inputData = [ |
|
343
|
|
|
'name' => $faker->name, |
|
344
|
|
|
'email' => $faker->safeEmail, |
|
345
|
|
|
'password' => bcrypt($faker->password()), |
|
346
|
|
|
'remember_token' => null, |
|
347
|
|
|
'roles' => [1, 2], |
|
348
|
|
|
]; |
|
349
|
|
|
|
|
350
|
|
|
$entry = Article::find(1); |
|
351
|
|
|
$this->crudPanel->syncPivot($entry, $inputData); |
|
352
|
|
|
} |
|
353
|
|
|
} |
|
354
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.