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\Bang; |
7
|
|
|
use Backpack\CRUD\Tests\Unit\Models\Comet; |
8
|
|
|
use Backpack\CRUD\Tests\Unit\Models\Planet; |
9
|
|
|
use Backpack\CRUD\Tests\Unit\Models\Universe; |
10
|
|
|
use Backpack\CRUD\Tests\Unit\Models\User; |
11
|
|
|
use Faker\Factory; |
12
|
|
|
use Illuminate\Support\Arr; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @covers Backpack\CRUD\app\Library\CrudPanel\Traits\Create |
16
|
|
|
* @covers Backpack\CRUD\app\Library\CrudPanel\Traits\Relationships |
17
|
|
|
*/ |
18
|
|
|
class CrudPanelCreateTest extends BaseDBCrudPanelTest |
19
|
|
|
{ |
20
|
|
|
private $nonRelationshipField = [ |
21
|
|
|
'name' => 'field1', |
22
|
|
|
'label' => 'Field1', |
23
|
|
|
]; |
24
|
|
|
|
25
|
|
|
private $userInputFieldsNoRelationships = [ |
26
|
|
|
[ |
27
|
|
|
'name' => 'id', |
28
|
|
|
'type' => 'hidden', |
29
|
|
|
], [ |
30
|
|
|
'name' => 'name', |
31
|
|
|
], [ |
32
|
|
|
'name' => 'email', |
33
|
|
|
'type' => 'email', |
34
|
|
|
], [ |
35
|
|
|
'name' => 'password', |
36
|
|
|
'type' => 'password', |
37
|
|
|
], |
38
|
|
|
]; |
39
|
|
|
|
40
|
|
|
private $articleInputFieldsOneToMany = [ |
41
|
|
|
[ |
42
|
|
|
'name' => 'id', |
43
|
|
|
'type' => 'hidden', |
44
|
|
|
], [ |
45
|
|
|
'name' => 'content', |
46
|
|
|
], [ |
47
|
|
|
'name' => 'tags', |
48
|
|
|
], [ |
49
|
|
|
'label' => 'Author', |
50
|
|
|
'type' => 'select', |
51
|
|
|
'name' => 'user_id', |
52
|
|
|
'entity' => 'user', |
53
|
|
|
'attribute' => 'name', |
54
|
|
|
], |
55
|
|
|
]; |
56
|
|
|
|
57
|
|
|
private $userInputFieldsManyToMany = [ |
58
|
|
|
[ |
59
|
|
|
'name' => 'id', |
60
|
|
|
'type' => 'hidden', |
61
|
|
|
], [ |
62
|
|
|
'name' => 'name', |
63
|
|
|
], [ |
64
|
|
|
'name' => 'email', |
65
|
|
|
'type' => 'email', |
66
|
|
|
], [ |
67
|
|
|
'name' => 'password', |
68
|
|
|
'type' => 'password', |
69
|
|
|
], [ |
70
|
|
|
'label' => 'Roles', |
71
|
|
|
'type' => 'select_multiple', |
72
|
|
|
'name' => 'roles', |
73
|
|
|
'entity' => 'roles', |
74
|
|
|
'attribute' => 'name', |
75
|
|
|
'pivot' => true, |
76
|
|
|
], |
77
|
|
|
]; |
78
|
|
|
|
79
|
|
|
private $userInputFieldsDotNotation = [ |
80
|
|
|
[ |
81
|
|
|
'name' => 'id', |
82
|
|
|
'type' => 'hidden', |
83
|
|
|
], [ |
84
|
|
|
'name' => 'name', |
85
|
|
|
], [ |
86
|
|
|
'name' => 'email', |
87
|
|
|
'type' => 'email', |
88
|
|
|
], [ |
89
|
|
|
'name' => 'password', |
90
|
|
|
'type' => 'password', |
91
|
|
|
], [ |
92
|
|
|
'label' => 'Roles', |
93
|
|
|
'type' => 'relationship', |
94
|
|
|
'name' => 'roles', |
95
|
|
|
'entity' => 'roles', |
96
|
|
|
'attribute' => 'name', |
97
|
|
|
], [ |
98
|
|
|
'label' => 'Street', |
99
|
|
|
'name' => 'street', |
100
|
|
|
'entity' => 'accountDetails.addresses', |
101
|
|
|
'attribute' => 'street', |
102
|
|
|
], |
103
|
|
|
]; |
104
|
|
|
|
105
|
|
|
private $userInputHasOneRelation = [ |
106
|
|
|
[ |
107
|
|
|
'name' => 'accountDetails.nickname', |
108
|
|
|
], |
109
|
|
|
[ |
110
|
|
|
'name' => 'accountDetails.profile_picture', |
111
|
|
|
], |
112
|
|
|
]; |
113
|
|
|
|
114
|
|
|
private $articleInputBelongsToRelationName = [ |
115
|
|
|
[ |
116
|
|
|
'name' => 'user', |
117
|
|
|
], |
118
|
|
|
]; |
119
|
|
|
|
120
|
|
|
public function testCreate() |
121
|
|
|
{ |
122
|
|
|
$this->crudPanel->setModel(User::class); |
123
|
|
|
$this->crudPanel->addFields($this->userInputFieldsNoRelationships); |
124
|
|
|
$faker = Factory::create(); |
125
|
|
|
$inputData = [ |
126
|
|
|
'name' => $faker->name, |
127
|
|
|
'email' => $faker->safeEmail, |
128
|
|
|
'password' => bcrypt($faker->password()), |
129
|
|
|
]; |
130
|
|
|
|
131
|
|
|
$entry = $this->crudPanel->create($inputData); |
132
|
|
|
|
133
|
|
|
$this->assertInstanceOf(User::class, $entry); |
134
|
|
|
$this->assertEntryEquals($inputData, $entry); |
135
|
|
|
$this->assertEmpty($entry->articles); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function testCreateWithOneToOneRelationship() |
139
|
|
|
{ |
140
|
|
|
$this->crudPanel->setModel(User::class); |
141
|
|
|
$this->crudPanel->addFields($this->userInputFieldsNoRelationships); |
142
|
|
|
$this->crudPanel->addFields($this->userInputHasOneRelation); |
143
|
|
|
$faker = Factory::create(); |
144
|
|
|
$account_details_nickname = $faker->name; |
145
|
|
|
$inputData = [ |
146
|
|
|
'name' => $faker->name, |
147
|
|
|
'email' => $faker->safeEmail, |
148
|
|
|
'password' => bcrypt($faker->password()), |
149
|
|
|
'accountDetails' => [ |
150
|
|
|
'nickname' => $account_details_nickname, |
151
|
|
|
'profile_picture' => 'test.jpg', |
152
|
|
|
], |
153
|
|
|
]; |
154
|
|
|
$entry = $this->crudPanel->create($inputData); |
155
|
|
|
$account_details = $entry->accountDetails()->first(); |
156
|
|
|
|
157
|
|
|
$this->assertEquals($account_details->nickname, $account_details_nickname); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public function testCreateWithOneToOneRelationshipUsingRepeatableInterface() |
161
|
|
|
{ |
162
|
|
|
$this->crudPanel->setModel(User::class); |
163
|
|
|
$this->crudPanel->addFields($this->userInputFieldsNoRelationships); |
164
|
|
|
$this->crudPanel->addField([ |
165
|
|
|
'name' => 'accountDetails', |
166
|
|
|
'fields' => [ |
167
|
|
|
[ |
168
|
|
|
'name' => 'nickname', |
169
|
|
|
], |
170
|
|
|
[ |
171
|
|
|
'name' => 'profile_picture', |
172
|
|
|
], |
173
|
|
|
], |
174
|
|
|
]); |
175
|
|
|
$faker = Factory::create(); |
176
|
|
|
$account_details_nickname = $faker->name; |
177
|
|
|
$inputData = [ |
178
|
|
|
'name' => $faker->name, |
179
|
|
|
'email' => $faker->safeEmail, |
180
|
|
|
'password' => bcrypt($faker->password()), |
181
|
|
|
'accountDetails' => [ |
182
|
|
|
['nickname' => $account_details_nickname, 'profile_picture' => 'test.jpg'], |
183
|
|
|
], |
184
|
|
|
]; |
185
|
|
|
$entry = $this->crudPanel->create($inputData); |
186
|
|
|
$account_details = $entry->accountDetails()->first(); |
187
|
|
|
|
188
|
|
|
$this->assertEquals($account_details->nickname, $account_details_nickname); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function testCreateBelongsToWithRelationName() |
192
|
|
|
{ |
193
|
|
|
$this->crudPanel->setModel(Article::class); |
194
|
|
|
$this->crudPanel->addFields($this->articleInputFieldsOneToMany); |
195
|
|
|
$this->crudPanel->removeField('user_id'); |
196
|
|
|
$this->crudPanel->addFields($this->articleInputBelongsToRelationName); |
197
|
|
|
$faker = Factory::create(); |
198
|
|
|
$inputData = [ |
199
|
|
|
'content' => $faker->text(), |
200
|
|
|
'tags' => $faker->words(3, true), |
201
|
|
|
'user' => 1, |
202
|
|
|
'metas' => null, |
203
|
|
|
'extras' => null, |
204
|
|
|
'cast_metas' => null, |
205
|
|
|
'cast_tags' => null, |
206
|
|
|
'cast_extras' => null, |
207
|
|
|
]; |
208
|
|
|
$entry = $this->crudPanel->create($inputData); |
209
|
|
|
$userEntry = User::find(1); |
|
|
|
|
210
|
|
|
$article = Article::where('user_id', 1)->with('user')->get()->last(); |
211
|
|
|
$this->assertEquals($article->user_id, $entry->user_id); |
212
|
|
|
$this->assertEquals($article->id, $entry->id); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
public function testCreateWithOneToManyRelationship() |
216
|
|
|
{ |
217
|
|
|
$this->crudPanel->setModel(Article::class); |
218
|
|
|
$this->crudPanel->addFields($this->articleInputFieldsOneToMany); |
219
|
|
|
$faker = Factory::create(); |
220
|
|
|
$inputData = [ |
221
|
|
|
'content' => $faker->text(), |
222
|
|
|
'tags' => $faker->words(3, true), |
223
|
|
|
'user_id' => 1, |
224
|
|
|
'metas' => null, |
225
|
|
|
'extras' => null, |
226
|
|
|
'cast_metas' => null, |
227
|
|
|
'cast_tags' => null, |
228
|
|
|
'cast_extras' => null, |
229
|
|
|
]; |
230
|
|
|
|
231
|
|
|
$entry = $this->crudPanel->create($inputData); |
232
|
|
|
$userEntry = User::find(1); |
|
|
|
|
233
|
|
|
$article = Article::where('user_id', 1)->with('user')->get()->last(); |
234
|
|
|
$this->assertEntryEquals($inputData, $entry); |
235
|
|
|
$this->assertEquals($article->user_id, $entry->user_id); |
236
|
|
|
$this->assertEquals($article->id, $entry->id); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
public function testCreateWithManyToManyRelationship() |
240
|
|
|
{ |
241
|
|
|
$this->crudPanel->setModel(User::class); |
242
|
|
|
$this->crudPanel->addFields($this->userInputFieldsManyToMany); |
243
|
|
|
$faker = Factory::create(); |
244
|
|
|
$inputData = [ |
245
|
|
|
'name' => $faker->name, |
246
|
|
|
'email' => $faker->safeEmail, |
247
|
|
|
'password' => bcrypt($faker->password()), |
248
|
|
|
'remember_token' => null, |
249
|
|
|
'roles' => [1, 2], |
250
|
|
|
]; |
251
|
|
|
|
252
|
|
|
$entry = $this->crudPanel->create($inputData); |
253
|
|
|
|
254
|
|
|
$this->assertInstanceOf(User::class, $entry); |
255
|
|
|
$this->assertEntryEquals($inputData, $entry); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
public function testGetRelationFields() |
259
|
|
|
{ |
260
|
|
|
$this->markTestIncomplete('Not correctly implemented'); |
261
|
|
|
|
262
|
|
|
$this->crudPanel->setModel(User::class); |
263
|
|
|
$this->crudPanel->addFields($this->userInputFieldsManyToMany, 'create'); |
264
|
|
|
|
265
|
|
|
// TODO: fix method and documentation. when 'both' is passed as the $form value, the getRelationFields searches |
266
|
|
|
// for relationship fields in the update fields. |
267
|
|
|
$relationFields = $this->crudPanel->getRelationFields('both'); |
268
|
|
|
|
269
|
|
|
$this->assertEquals($this->crudPanel->create_fields['roles'], Arr::last($relationFields)); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
public function testGetRelationFieldsCreateForm() |
273
|
|
|
{ |
274
|
|
|
$this->crudPanel->setModel(User::class); |
275
|
|
|
$this->crudPanel->setOperation('create'); |
276
|
|
|
$this->crudPanel->addFields($this->userInputFieldsManyToMany); |
277
|
|
|
|
278
|
|
|
$relationFields = $this->crudPanel->getRelationFields(); |
279
|
|
|
|
280
|
|
|
$this->assertEquals($this->crudPanel->get('create.fields')['roles'], Arr::last($relationFields)); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
public function testGetRelationFieldsUpdateForm() |
284
|
|
|
{ |
285
|
|
|
$this->crudPanel->setModel(User::class); |
286
|
|
|
$this->crudPanel->setOperation('update'); |
287
|
|
|
$this->crudPanel->addFields($this->userInputFieldsManyToMany); |
288
|
|
|
|
289
|
|
|
$relationFields = $this->crudPanel->getRelationFields(); |
290
|
|
|
|
291
|
|
|
$this->assertEquals($this->crudPanel->get('update.fields')['roles'], Arr::last($relationFields)); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
public function testGetRelationFieldsUnknownForm() |
295
|
|
|
{ |
296
|
|
|
$this->markTestIncomplete('Not correctly implemented'); |
297
|
|
|
|
298
|
|
|
$this->expectException(\InvalidArgumentException::class); |
299
|
|
|
|
300
|
|
|
$this->crudPanel->setModel(User::class); |
301
|
|
|
$this->crudPanel->addFields($this->userInputFieldsManyToMany); |
302
|
|
|
|
303
|
|
|
// TODO: this should throw an invalid argument exception but instead it searches for relationship fields in the |
304
|
|
|
// update fields. |
305
|
|
|
$this->crudPanel->getRelationFields('unknownForm'); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
public function testGetRelationFieldsDotNotation() |
309
|
|
|
{ |
310
|
|
|
$this->crudPanel->setModel(User::class); |
311
|
|
|
$this->crudPanel->setOperation('create'); |
312
|
|
|
|
313
|
|
|
$this->crudPanel->addFields($this->userInputFieldsDotNotation); |
314
|
|
|
|
315
|
|
|
//get all fields with a relation |
316
|
|
|
$relationFields = $this->crudPanel->getRelationFields(); |
317
|
|
|
//var_dump($this->crudPanel->get('create.fields')['street']); |
318
|
|
|
|
319
|
|
|
$this->assertEquals($this->crudPanel->get('create.fields')['street'], Arr::last($relationFields)); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
public function testCreateHasOneRelations() |
323
|
|
|
{ |
324
|
|
|
$this->crudPanel->setModel(User::class); |
325
|
|
|
$this->crudPanel->setOperation('create'); |
326
|
|
|
|
327
|
|
|
$this->crudPanel->addFields($this->userInputHasOneRelation); |
328
|
|
|
$faker = Factory::create(); |
329
|
|
|
|
330
|
|
|
$inputData = [ |
331
|
|
|
'name' => $faker->name, |
332
|
|
|
'email' => $faker->safeEmail, |
333
|
|
|
'password' => bcrypt($faker->password()), |
334
|
|
|
'remember_token' => null, |
335
|
|
|
'roles' => [1, 2], |
336
|
|
|
'accountDetails' => [ |
337
|
|
|
'nickname' => 'i_have_has_one', |
338
|
|
|
'profile_picture' => 'simple_picture.jpg', |
339
|
|
|
], |
340
|
|
|
]; |
341
|
|
|
$entry = $this->crudPanel->create($inputData); |
342
|
|
|
$account_details = $entry->accountDetails()->first(); |
343
|
|
|
|
344
|
|
|
$this->assertEquals($account_details->nickname, 'i_have_has_one'); |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
public function testGetRelationFieldsNoRelations() |
348
|
|
|
{ |
349
|
|
|
$this->crudPanel->addField($this->nonRelationshipField); |
350
|
|
|
|
351
|
|
|
$relationFields = $this->crudPanel->getRelationFields(); |
352
|
|
|
|
353
|
|
|
$this->assertEmpty($relationFields); |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
public function testGetRelationFieldsNoFields() |
357
|
|
|
{ |
358
|
|
|
$relationFields = $this->crudPanel->getRelationFields(); |
359
|
|
|
|
360
|
|
|
$this->assertEmpty($relationFields); |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
public function testGetRelationFieldsWithPivot() |
364
|
|
|
{ |
365
|
|
|
$this->crudPanel->setModel(User::class); |
366
|
|
|
$this->crudPanel->setOperation('create'); |
367
|
|
|
$this->crudPanel->addFields($this->userInputFieldsDotNotation); |
368
|
|
|
|
369
|
|
|
$relationFields = $this->crudPanel->getRelationFieldsWithPivot(); |
370
|
|
|
$this->assertEquals($this->crudPanel->get('create.fields')['roles'], Arr::first($relationFields)); |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
public function testGetRelationFieldsWithPivotNoRelations() |
374
|
|
|
{ |
375
|
|
|
$this->crudPanel->setModel(User::class); |
376
|
|
|
$this->crudPanel->setOperation('create'); |
377
|
|
|
$this->crudPanel->addFields($this->nonRelationshipField); |
378
|
|
|
|
379
|
|
|
$relationFields = $this->crudPanel->getRelationFieldsWithPivot(); |
380
|
|
|
|
381
|
|
|
$this->assertEmpty($relationFields); |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
public function testMorphToManySelectableRelationship() |
385
|
|
|
{ |
386
|
|
|
$this->crudPanel->setModel(User::class); |
387
|
|
|
$this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both'); |
|
|
|
|
388
|
|
|
$this->crudPanel->addField(['name' => 'bills'], 'both'); |
|
|
|
|
389
|
|
|
|
390
|
|
|
$faker = Factory::create(); |
391
|
|
|
$inputData = [ |
392
|
|
|
'name' => $faker->name, |
393
|
|
|
'email' => $faker->safeEmail, |
394
|
|
|
'password' => bcrypt($faker->password()), |
395
|
|
|
'remember_token' => null, |
396
|
|
|
'bills' => [1], |
397
|
|
|
]; |
398
|
|
|
|
399
|
|
|
$entry = $this->crudPanel->create($inputData); |
400
|
|
|
|
401
|
|
|
$this->assertCount(1, $entry->bills); |
402
|
|
|
|
403
|
|
|
$this->assertEquals(1, $entry->bills()->first()->id); |
404
|
|
|
|
405
|
|
|
$inputData['bills'] = [1, 2]; |
406
|
|
|
|
407
|
|
|
$this->crudPanel->update($entry->id, $inputData); |
408
|
|
|
|
409
|
|
|
$this->assertCount(2, $entry->fresh()->bills); |
410
|
|
|
|
411
|
|
|
$this->assertEquals([1, 2], $entry->fresh()->bills->pluck('id')->toArray()); |
412
|
|
|
} |
413
|
|
|
|
414
|
|
|
public function testMorphToManyCreatableRelationship() |
415
|
|
|
{ |
416
|
|
|
$this->crudPanel->setModel(User::class); |
417
|
|
|
$this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both'); |
|
|
|
|
418
|
|
|
$this->crudPanel->addField(['name' => 'recommends', 'subfields' => [ |
419
|
|
|
[ |
420
|
|
|
'name' => 'text', |
421
|
|
|
], |
422
|
|
|
]], 'both'); |
|
|
|
|
423
|
|
|
|
424
|
|
|
$faker = Factory::create(); |
425
|
|
|
$inputData = [ |
426
|
|
|
'name' => $faker->name, |
427
|
|
|
'email' => $faker->safeEmail, |
428
|
|
|
'password' => bcrypt($faker->password()), |
429
|
|
|
'remember_token' => null, |
430
|
|
|
'recommends' => [ |
431
|
|
|
[ |
432
|
|
|
'recommends' => 1, |
433
|
|
|
'text' => 'my pivot recommend field', |
434
|
|
|
], |
435
|
|
|
], |
436
|
|
|
]; |
437
|
|
|
|
438
|
|
|
$entry = $this->crudPanel->create($inputData); |
439
|
|
|
|
440
|
|
|
$this->assertCount(1, $entry->recommends); |
441
|
|
|
|
442
|
|
|
$this->assertEquals(1, $entry->recommends()->first()->id); |
443
|
|
|
|
444
|
|
|
$inputData['recommends'] = [ |
445
|
|
|
[ |
446
|
|
|
'recommends' => 2, |
447
|
|
|
'text' => 'I changed the recommend and the pivot text', |
448
|
|
|
], |
449
|
|
|
]; |
450
|
|
|
|
451
|
|
|
$this->crudPanel->update($entry->id, $inputData); |
452
|
|
|
|
453
|
|
|
$this->assertCount(1, $entry->fresh()->recommends); |
454
|
|
|
|
455
|
|
|
$this->assertEquals(2, $entry->recommends()->first()->id); |
456
|
|
|
|
457
|
|
|
$this->assertEquals('I changed the recommend and the pivot text', $entry->fresh()->recommends->first()->pivot->text); |
458
|
|
|
} |
459
|
|
|
|
460
|
|
|
public function testBelongsToManyWithPivotDataRelationship() |
461
|
|
|
{ |
462
|
|
|
$this->crudPanel->setModel(User::class); |
463
|
|
|
$this->crudPanel->addFields($this->userInputFieldsNoRelationships); |
464
|
|
|
$this->crudPanel->addField([ |
465
|
|
|
'name' => 'superArticles', |
466
|
|
|
'subfields' => [ |
467
|
|
|
[ |
468
|
|
|
'name' => 'notes', |
469
|
|
|
], |
470
|
|
|
], |
471
|
|
|
]); |
472
|
|
|
|
473
|
|
|
$faker = Factory::create(); |
474
|
|
|
$articleData = [ |
475
|
|
|
'content' => $faker->text(), |
476
|
|
|
'tags' => $faker->words(3, true), |
477
|
|
|
'user_id' => 1, |
478
|
|
|
]; |
479
|
|
|
|
480
|
|
|
$article = Article::create($articleData); |
481
|
|
|
|
482
|
|
|
$inputData = [ |
483
|
|
|
'name' => $faker->name, |
484
|
|
|
'email' => $faker->safeEmail, |
485
|
|
|
'password' => bcrypt($faker->password()), |
486
|
|
|
'remember_token' => null, |
487
|
|
|
'superArticles' => [ |
488
|
|
|
[ |
489
|
|
|
'superArticles' => $article->id, |
490
|
|
|
'notes' => 'my first article note', |
491
|
|
|
], |
492
|
|
|
], |
493
|
|
|
]; |
494
|
|
|
|
495
|
|
|
$entry = $this->crudPanel->create($inputData); |
496
|
|
|
|
497
|
|
|
$this->assertCount(1, $entry->fresh()->superArticles); |
498
|
|
|
$this->assertEquals('my first article note', $entry->fresh()->superArticles->first()->pivot->notes); |
499
|
|
|
} |
500
|
|
|
|
501
|
|
|
public function testCreateHasOneWithNestedRelationsRepeatableInterface() |
502
|
|
|
{ |
503
|
|
|
$this->crudPanel->setModel(User::class); |
504
|
|
|
$this->crudPanel->setOperation('create'); |
505
|
|
|
$this->crudPanel->addFields($this->userInputFieldsNoRelationships); |
506
|
|
|
$this->crudPanel->addField( |
507
|
|
|
[ |
508
|
|
|
'name' => 'accountDetails', |
509
|
|
|
'subfields' => [ |
510
|
|
|
[ |
511
|
|
|
'name' => 'nickname', |
512
|
|
|
], |
513
|
|
|
[ |
514
|
|
|
'name' => 'profile_picture', |
515
|
|
|
], |
516
|
|
|
[ |
517
|
|
|
'name' => 'article', |
518
|
|
|
], |
519
|
|
|
[ |
520
|
|
|
'name' => 'addresses', |
521
|
|
|
'subfields' => [ |
522
|
|
|
[ |
523
|
|
|
'name' => 'bang', |
524
|
|
|
], |
525
|
|
|
[ |
526
|
|
|
'name' => 'street', |
527
|
|
|
], |
528
|
|
|
[ |
529
|
|
|
'name' => 'number', |
530
|
|
|
], |
531
|
|
|
], |
532
|
|
|
], |
533
|
|
|
[ |
534
|
|
|
'name' => 'bangs', |
535
|
|
|
], |
536
|
|
|
[ |
537
|
|
|
'name' => 'bangsPivot', |
538
|
|
|
'subfields' => [ |
539
|
|
|
[ |
540
|
|
|
'name' => 'pivot_field', |
541
|
|
|
], |
542
|
|
|
], |
543
|
|
|
], |
544
|
|
|
], |
545
|
|
|
]); |
546
|
|
|
|
547
|
|
|
$faker = Factory::create(); |
548
|
|
|
|
549
|
|
|
$inputData = [ |
550
|
|
|
'name' => $faker->name, |
551
|
|
|
'email' => $faker->safeEmail, |
552
|
|
|
'password' => bcrypt($faker->password()), |
553
|
|
|
'remember_token' => null, |
554
|
|
|
'roles' => [1, 2], |
555
|
|
|
'accountDetails' => [ |
556
|
|
|
[ |
557
|
|
|
'nickname' => 'i_have_has_one', |
558
|
|
|
'profile_picture' => 'ohh my picture 1.jpg', |
559
|
|
|
'article' => 1, |
560
|
|
|
'addresses' => [ |
561
|
|
|
[ |
562
|
|
|
'bang' => 1, |
563
|
|
|
'street' => 'test', |
564
|
|
|
'number' => 1, |
565
|
|
|
], |
566
|
|
|
[ |
567
|
|
|
'bang' => 1, |
568
|
|
|
'street' => 'test2', |
569
|
|
|
'number' => 2, |
570
|
|
|
], |
571
|
|
|
], |
572
|
|
|
'bangs' => [1, 2], |
573
|
|
|
'bangsPivot' => [ |
574
|
|
|
['bangsPivot' => 1, 'pivot_field' => 'test1'], |
575
|
|
|
['bangsPivot' => 2, 'pivot_field' => 'test2'], |
576
|
|
|
], |
577
|
|
|
], |
578
|
|
|
], |
579
|
|
|
]; |
580
|
|
|
|
581
|
|
|
$entry = $this->crudPanel->create($inputData); |
582
|
|
|
$account_details = $entry->accountDetails()->first(); |
583
|
|
|
|
584
|
|
|
$this->assertEquals($account_details->article, Article::find(1)); |
585
|
|
|
$this->assertEquals($account_details->addresses->count(), 2); |
|
|
|
|
586
|
|
|
$this->assertEquals($account_details->addresses->first()->city, 1); |
|
|
|
|
587
|
|
|
$this->assertEquals($account_details->addresses->first()->street, 'test'); |
|
|
|
|
588
|
|
|
$this->assertEquals($account_details->addresses->first()->number, 1); |
|
|
|
|
589
|
|
|
$this->assertEquals($account_details->bangs->first()->name, Bang::find(1)->name); |
|
|
|
|
590
|
|
|
$this->assertEquals($account_details->bangsPivot->count(), 2); |
|
|
|
|
591
|
|
|
$this->assertEquals($account_details->bangsPivot->first()->pivot->pivot_field, 'test1'); |
|
|
|
|
592
|
|
|
} |
593
|
|
|
|
594
|
|
|
public function testCreateBelongsToFake() |
595
|
|
|
{ |
596
|
|
|
$belongsToField = [ // select_grouped |
597
|
|
|
'label' => 'Select_grouped', |
598
|
|
|
'type' => 'select_grouped', //https://github.com/Laravel-Backpack/CRUD/issues/502 |
599
|
|
|
'name' => 'bang_relation_field', |
600
|
|
|
'fake' => true, |
601
|
|
|
'entity' => 'bang', |
602
|
|
|
'model' => 'Backpack\CRUD\Tests\Unit\Models\Bang', |
603
|
|
|
'attribute' => 'title', |
604
|
|
|
'group_by' => 'category', // the relationship to entity you want to use for grouping |
605
|
|
|
'group_by_attribute' => 'name', // the attribute on related model, that you want shown |
606
|
|
|
'group_by_relationship_back' => 'articles', // relationship from related model back to this model |
607
|
|
|
'tab' => 'Selects', |
608
|
|
|
'wrapperAttributes' => ['class' => 'form-group col-md-6'], |
609
|
|
|
]; |
610
|
|
|
|
611
|
|
|
$this->crudPanel->setModel(User::class); |
612
|
|
|
$this->crudPanel->setOperation('create'); |
613
|
|
|
$this->crudPanel->addFields($this->userInputFieldsNoRelationships); |
614
|
|
|
$this->crudPanel->addField($belongsToField); |
615
|
|
|
|
616
|
|
|
$faker = Factory::create(); |
617
|
|
|
|
618
|
|
|
$inputData = [ |
619
|
|
|
'name' => $faker->name, |
620
|
|
|
'email' => $faker->safeEmail, |
621
|
|
|
'password' => bcrypt($faker->password()), |
622
|
|
|
'remember_token' => null, |
623
|
|
|
'bang_relation_field' => 1, |
624
|
|
|
]; |
625
|
|
|
|
626
|
|
|
$entry = $this->crudPanel->create($inputData); |
627
|
|
|
$this->crudPanel->entry = $entry->withFakes(); |
628
|
|
|
$this->assertEquals($entry->bang_relation_field, 1); |
629
|
|
|
} |
630
|
|
|
|
631
|
|
|
public function testCreateHasOneWithNestedRelations() |
632
|
|
|
{ |
633
|
|
|
$this->crudPanel->setModel(User::class); |
634
|
|
|
$this->crudPanel->setOperation('create'); |
635
|
|
|
$this->crudPanel->addFields($this->userInputFieldsNoRelationships); |
636
|
|
|
$this->crudPanel->addFields([ |
637
|
|
|
[ |
638
|
|
|
'name' => 'accountDetails.nickname', |
639
|
|
|
], |
640
|
|
|
[ |
641
|
|
|
'name' => 'accountDetails.profile_picture', |
642
|
|
|
], |
643
|
|
|
[ |
644
|
|
|
'name' => 'accountDetails.article', |
645
|
|
|
], |
646
|
|
|
[ |
647
|
|
|
'name' => 'accountDetails.addresses', |
648
|
|
|
'subfields' => [ |
649
|
|
|
[ |
650
|
|
|
'name' => 'city', |
651
|
|
|
'entity' => 'bang', |
652
|
|
|
], |
653
|
|
|
[ |
654
|
|
|
'name' => 'street', |
655
|
|
|
], |
656
|
|
|
[ |
657
|
|
|
'name' => 'number', |
658
|
|
|
], |
659
|
|
|
], |
660
|
|
|
], |
661
|
|
|
[ |
662
|
|
|
'name' => 'accountDetails.bangs', |
663
|
|
|
], |
664
|
|
|
[ |
665
|
|
|
'name' => 'accountDetails.bangsPivot', |
666
|
|
|
'subfields' => [ |
667
|
|
|
[ |
668
|
|
|
'name' => 'pivot_field', |
669
|
|
|
], |
670
|
|
|
], |
671
|
|
|
], |
672
|
|
|
]); |
673
|
|
|
|
674
|
|
|
$faker = Factory::create(); |
675
|
|
|
|
676
|
|
|
$inputData = [ |
677
|
|
|
'name' => $faker->name, |
678
|
|
|
'email' => $faker->safeEmail, |
679
|
|
|
'password' => bcrypt($faker->password()), |
680
|
|
|
'remember_token' => null, |
681
|
|
|
'roles' => [1, 2], |
682
|
|
|
'accountDetails' => [ |
683
|
|
|
'nickname' => 'i_have_has_one', |
684
|
|
|
'profile_picture' => 'ohh my picture 1.jpg', |
685
|
|
|
'article' => 1, |
686
|
|
|
'addresses' => [ |
687
|
|
|
[ |
688
|
|
|
'city' => 1, |
689
|
|
|
'street' => 'test', |
690
|
|
|
'number' => 1, |
691
|
|
|
], |
692
|
|
|
[ |
693
|
|
|
'city' => 2, |
694
|
|
|
'street' => 'test2', |
695
|
|
|
'number' => 2, |
696
|
|
|
], |
697
|
|
|
], |
698
|
|
|
'bangs' => [1, 2], |
699
|
|
|
'bangsPivot' => [ |
700
|
|
|
['bangsPivot' => 1, 'pivot_field' => 'test1'], |
701
|
|
|
['bangsPivot' => 2, 'pivot_field' => 'test2'], |
702
|
|
|
], |
703
|
|
|
], |
704
|
|
|
]; |
705
|
|
|
|
706
|
|
|
$entry = $this->crudPanel->create($inputData); |
707
|
|
|
$account_details = $entry->accountDetails()->first(); |
708
|
|
|
|
709
|
|
|
$this->assertEquals($account_details->article, Article::find(1)); |
710
|
|
|
$this->assertEquals($account_details->addresses->count(), 2); |
711
|
|
|
$this->assertEquals($account_details->addresses->first()->bang->id, 1); |
|
|
|
|
712
|
|
|
$this->assertEquals($account_details->addresses->first()->street, 'test'); |
|
|
|
|
713
|
|
|
$this->assertEquals($account_details->addresses->first()->number, 1); |
|
|
|
|
714
|
|
|
$this->assertEquals($account_details->bangs->first()->name, Bang::find(1)->name); |
|
|
|
|
715
|
|
|
$this->assertEquals($account_details->bangsPivot->count(), 2); |
716
|
|
|
$this->assertEquals($account_details->bangsPivot->first()->pivot->pivot_field, 'test1'); |
|
|
|
|
717
|
|
|
|
718
|
|
|
// Now test the remove process |
719
|
|
|
|
720
|
|
|
$inputData = [ |
721
|
|
|
'name' => $faker->name, |
722
|
|
|
'email' => $faker->safeEmail, |
723
|
|
|
'password' => bcrypt($faker->password()), |
724
|
|
|
'remember_token' => null, |
725
|
|
|
'roles' => [1, 2], |
726
|
|
|
'accountDetails' => [ |
727
|
|
|
'nickname' => 'i_have_has_one', |
728
|
|
|
'profile_picture' => 'ohh my picture 1.jpg', |
729
|
|
|
'article' => 1, |
730
|
|
|
'addresses' => [ // HasOne is tested in other test function |
731
|
|
|
[ |
732
|
|
|
'city' => 2, |
733
|
|
|
'street' => 'test', |
734
|
|
|
'number' => 1, |
735
|
|
|
], |
736
|
|
|
[ |
737
|
|
|
'city' => 1, |
738
|
|
|
'street' => 'test2', |
739
|
|
|
'number' => 2, |
740
|
|
|
], |
741
|
|
|
], |
742
|
|
|
'bangs' => [], |
743
|
|
|
'bangsPivot' => [], |
744
|
|
|
], |
745
|
|
|
]; |
746
|
|
|
|
747
|
|
|
$entry = $this->crudPanel->update($entry->id, $inputData); |
748
|
|
|
$account_details = $entry->accountDetails()->first(); |
749
|
|
|
$this->assertEquals($account_details->addresses->count(), 2); |
750
|
|
|
$this->assertEquals($account_details->addresses->first()->bang->id, 2); |
751
|
|
|
$this->assertEquals($account_details->bangs->count(), 0); |
752
|
|
|
$this->assertEquals($account_details->bangsPivot->count(), 0); |
753
|
|
|
} |
754
|
|
|
|
755
|
|
|
public function testMorphOneRelationship() |
756
|
|
|
{ |
757
|
|
|
$this->crudPanel->setModel(User::class); |
758
|
|
|
$this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both'); |
|
|
|
|
759
|
|
|
$this->crudPanel->addField([ |
760
|
|
|
'name' => 'comment.text', |
761
|
|
|
], 'both'); |
|
|
|
|
762
|
|
|
|
763
|
|
|
$faker = Factory::create(); |
764
|
|
|
$inputData = [ |
765
|
|
|
'name' => $faker->name, |
766
|
|
|
'email' => $faker->safeEmail, |
767
|
|
|
'password' => bcrypt($faker->password()), |
768
|
|
|
'remember_token' => null, |
769
|
|
|
'comment' => [ |
770
|
|
|
'text' => 'some test comment text', |
771
|
|
|
], |
772
|
|
|
]; |
773
|
|
|
|
774
|
|
|
$entry = $this->crudPanel->create($inputData); |
775
|
|
|
|
776
|
|
|
$this->assertEquals($inputData['comment']['text'], $entry->comment->text); |
|
|
|
|
777
|
|
|
|
778
|
|
|
$inputData['comment']['text'] = 'updated comment text'; |
779
|
|
|
|
780
|
|
|
$this->crudPanel->update($entry->id, $inputData); |
781
|
|
|
|
782
|
|
|
$this->assertEquals($inputData['comment']['text'], $entry->fresh()->comment->text); |
783
|
|
|
} |
784
|
|
|
|
785
|
|
|
public function testMorphManyCreatableRelationship() |
786
|
|
|
{ |
787
|
|
|
$this->crudPanel->setModel(User::class); |
788
|
|
|
$this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both'); |
|
|
|
|
789
|
|
|
$this->crudPanel->addField([ |
790
|
|
|
'name' => 'stars', |
791
|
|
|
'subfields' => [ |
792
|
|
|
[ |
793
|
|
|
'name' => 'title', |
794
|
|
|
], |
795
|
|
|
], |
796
|
|
|
], 'both'); |
|
|
|
|
797
|
|
|
|
798
|
|
|
$faker = Factory::create(); |
799
|
|
|
$inputData = [ |
800
|
|
|
'name' => $faker->name, |
801
|
|
|
'email' => $faker->safeEmail, |
802
|
|
|
'password' => bcrypt($faker->password()), |
803
|
|
|
'remember_token' => null, |
804
|
|
|
'stars' => [ |
805
|
|
|
[ |
806
|
|
|
'id' => null, |
807
|
|
|
'title' => 'this is the star 1 title', |
808
|
|
|
], |
809
|
|
|
[ |
810
|
|
|
'id' => null, |
811
|
|
|
'title' => 'this is the star 2 title', |
812
|
|
|
], |
813
|
|
|
], |
814
|
|
|
]; |
815
|
|
|
|
816
|
|
|
$entry = $this->crudPanel->create($inputData); |
817
|
|
|
|
818
|
|
|
$this->assertCount(2, $entry->stars); |
819
|
|
|
|
820
|
|
|
$this->assertEquals($inputData['stars'][0]['title'], $entry->stars()->first()->title); |
821
|
|
|
|
822
|
|
|
$inputData['stars'] = [ |
823
|
|
|
[ |
824
|
|
|
'id' => 1, |
825
|
|
|
'title' => 'only one star with changed title', |
826
|
|
|
], |
827
|
|
|
]; |
828
|
|
|
|
829
|
|
|
$this->crudPanel->update($entry->id, $inputData); |
830
|
|
|
|
831
|
|
|
$this->assertCount(1, $entry->fresh()->stars); |
832
|
|
|
|
833
|
|
|
$this->assertEquals($inputData['stars'][0]['title'], $entry->fresh()->stars->first()->title); |
834
|
|
|
$this->assertEquals($inputData['stars'][0]['id'], $entry->fresh()->stars->first()->id); |
835
|
|
|
} |
836
|
|
|
|
837
|
|
|
public function testHasManyCreatableRelationship() |
838
|
|
|
{ |
839
|
|
|
$this->crudPanel->setModel(User::class); |
840
|
|
|
$this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both'); |
|
|
|
|
841
|
|
|
$this->crudPanel->addField([ |
842
|
|
|
'name' => 'universes', |
843
|
|
|
'subfields' => [ |
844
|
|
|
[ |
845
|
|
|
'name' => 'title', |
846
|
|
|
], |
847
|
|
|
], |
848
|
|
|
], 'both'); |
|
|
|
|
849
|
|
|
|
850
|
|
|
$faker = Factory::create(); |
851
|
|
|
$inputData = [ |
852
|
|
|
'name' => $faker->name, |
853
|
|
|
'email' => $faker->safeEmail, |
854
|
|
|
'password' => bcrypt($faker->password()), |
855
|
|
|
'remember_token' => null, |
856
|
|
|
'universes' => [ |
857
|
|
|
[ |
858
|
|
|
'title' => 'this is the star 1 title', |
859
|
|
|
], |
860
|
|
|
[ |
861
|
|
|
'title' => 'this is the star 2 title', |
862
|
|
|
], |
863
|
|
|
], |
864
|
|
|
]; |
865
|
|
|
|
866
|
|
|
$entry = $this->crudPanel->create($inputData); |
867
|
|
|
|
868
|
|
|
$this->assertCount(2, $entry->universes); |
869
|
|
|
|
870
|
|
|
$this->assertEquals($inputData['universes'][0]['title'], $entry->universes()->first()->title); |
871
|
|
|
|
872
|
|
|
$inputData['universes'] = [ |
873
|
|
|
[ |
874
|
|
|
'id' => 1, |
875
|
|
|
'title' => 'only one star with changed title', |
876
|
|
|
], |
877
|
|
|
]; |
878
|
|
|
|
879
|
|
|
$this->crudPanel->update($entry->id, $inputData); |
880
|
|
|
|
881
|
|
|
$this->assertCount(1, $entry->fresh()->universes); |
882
|
|
|
|
883
|
|
|
$this->assertEquals($inputData['universes'][0]['title'], $entry->fresh()->universes->first()->title); |
884
|
|
|
$this->assertEquals($inputData['universes'][0]['id'], $entry->fresh()->universes->first()->id); |
885
|
|
|
$this->assertEquals(1, Universe::all()->count()); |
886
|
|
|
} |
887
|
|
|
|
888
|
|
|
public function testHasManySelectableRelationshipWithoutForceDelete() |
889
|
|
|
{ |
890
|
|
|
$this->crudPanel->setModel(User::class); |
891
|
|
|
$this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both'); |
|
|
|
|
892
|
|
|
$this->crudPanel->addField([ |
893
|
|
|
'name' => 'planets', |
894
|
|
|
'force_delete' => false, |
895
|
|
|
'fallback_id' => false, |
896
|
|
|
], 'both'); |
|
|
|
|
897
|
|
|
|
898
|
|
|
$faker = Factory::create(); |
899
|
|
|
$inputData = [ |
900
|
|
|
'name' => $faker->name, |
901
|
|
|
'email' => $faker->safeEmail, |
902
|
|
|
'password' => bcrypt($faker->password()), |
903
|
|
|
'remember_token' => null, |
904
|
|
|
'planets' => [1, 2], |
905
|
|
|
]; |
906
|
|
|
|
907
|
|
|
$entry = $this->crudPanel->create($inputData); |
908
|
|
|
|
909
|
|
|
$this->assertCount(2, $entry->planets); |
910
|
|
|
|
911
|
|
|
$inputData['planets'] = [1]; |
912
|
|
|
|
913
|
|
|
$this->crudPanel->update($entry->id, $inputData); |
914
|
|
|
|
915
|
|
|
$this->assertCount(1, $entry->fresh()->planets); |
916
|
|
|
|
917
|
|
|
$planets = Planet::all(); |
918
|
|
|
|
919
|
|
|
$this->assertCount(2, $planets); |
920
|
|
|
} |
921
|
|
|
|
922
|
|
|
public function testHasManySelectableRelationshipRemoveAllRelations() |
923
|
|
|
{ |
924
|
|
|
$this->crudPanel->setModel(User::class); |
925
|
|
|
$this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both'); |
|
|
|
|
926
|
|
|
$this->crudPanel->addField([ |
927
|
|
|
'name' => 'planets', |
928
|
|
|
'force_delete' => false, |
929
|
|
|
'fallback_id' => false, |
930
|
|
|
], 'both'); |
|
|
|
|
931
|
|
|
|
932
|
|
|
$faker = Factory::create(); |
933
|
|
|
$inputData = [ |
934
|
|
|
'name' => $faker->name, |
935
|
|
|
'email' => $faker->safeEmail, |
936
|
|
|
'password' => bcrypt($faker->password()), |
937
|
|
|
'remember_token' => null, |
938
|
|
|
'planets' => [1, 2], |
939
|
|
|
]; |
940
|
|
|
|
941
|
|
|
$entry = $this->crudPanel->create($inputData); |
942
|
|
|
|
943
|
|
|
$this->assertCount(2, $entry->planets); |
944
|
|
|
|
945
|
|
|
$inputData['planets'] = []; |
946
|
|
|
|
947
|
|
|
$this->crudPanel->update($entry->id, $inputData); |
948
|
|
|
|
949
|
|
|
$this->assertCount(0, $entry->fresh()->planets); |
950
|
|
|
|
951
|
|
|
$planets = Planet::all(); |
952
|
|
|
|
953
|
|
|
$this->assertCount(2, $planets); |
954
|
|
|
} |
955
|
|
|
|
956
|
|
|
public function testHasManySelectableRelationshipWithFallbackId() |
957
|
|
|
{ |
958
|
|
|
$this->crudPanel->setModel(User::class); |
959
|
|
|
$this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both'); |
|
|
|
|
960
|
|
|
$this->crudPanel->addField([ |
961
|
|
|
'name' => 'planets', |
962
|
|
|
'fallback_id' => 0, |
963
|
|
|
'force_delete' => false, |
964
|
|
|
], 'both'); |
|
|
|
|
965
|
|
|
|
966
|
|
|
$faker = Factory::create(); |
967
|
|
|
$inputData = [ |
968
|
|
|
'name' => $faker->name, |
969
|
|
|
'email' => $faker->safeEmail, |
970
|
|
|
'password' => bcrypt($faker->password()), |
971
|
|
|
'remember_token' => null, |
972
|
|
|
'planets' => [1, 2], |
973
|
|
|
]; |
974
|
|
|
|
975
|
|
|
$entry = $this->crudPanel->create($inputData); |
976
|
|
|
|
977
|
|
|
$this->assertCount(2, $entry->planets); |
978
|
|
|
|
979
|
|
|
$inputData['planets'] = [2]; |
980
|
|
|
|
981
|
|
|
$this->crudPanel->update($entry->id, $inputData); |
982
|
|
|
|
983
|
|
|
$this->assertCount(1, $entry->fresh()->planets); |
984
|
|
|
|
985
|
|
|
$planets = Planet::all(); |
986
|
|
|
$this->assertCount(2, $planets); |
987
|
|
|
$this->assertEquals(0, $planets->first()->user_id); |
988
|
|
|
} |
989
|
|
|
|
990
|
|
|
public function testHasManySelectableRelationshipWithForceDelete() |
991
|
|
|
{ |
992
|
|
|
$this->crudPanel->setModel(User::class); |
993
|
|
|
$this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both'); |
|
|
|
|
994
|
|
|
$this->crudPanel->addField([ |
995
|
|
|
'name' => 'planets', |
996
|
|
|
'force_delete' => true, |
997
|
|
|
'fallback_id' => false, |
998
|
|
|
], 'both'); |
|
|
|
|
999
|
|
|
|
1000
|
|
|
$faker = Factory::create(); |
1001
|
|
|
$inputData = [ |
1002
|
|
|
'name' => $faker->name, |
1003
|
|
|
'email' => $faker->safeEmail, |
1004
|
|
|
'password' => bcrypt($faker->password()), |
1005
|
|
|
'remember_token' => null, |
1006
|
|
|
'planets' => [1, 2], |
1007
|
|
|
]; |
1008
|
|
|
|
1009
|
|
|
$entry = $this->crudPanel->create($inputData); |
1010
|
|
|
|
1011
|
|
|
$this->assertCount(2, $entry->planets); |
1012
|
|
|
|
1013
|
|
|
$inputData['planets'] = [2]; |
1014
|
|
|
|
1015
|
|
|
$this->crudPanel->update($entry->id, $inputData); |
1016
|
|
|
|
1017
|
|
|
$this->assertCount(1, $entry->fresh()->planets); |
1018
|
|
|
|
1019
|
|
|
$planets = Planet::all(); |
1020
|
|
|
$this->assertCount(1, $planets); |
1021
|
|
|
} |
1022
|
|
|
|
1023
|
|
|
public function testHasManySelectableRelationshipNonNullableForeignKeyAndDefaultInDatabase() |
1024
|
|
|
{ |
1025
|
|
|
$this->crudPanel->setModel(User::class); |
1026
|
|
|
$this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both'); |
|
|
|
|
1027
|
|
|
$this->crudPanel->addField([ |
1028
|
|
|
'name' => 'comets', |
1029
|
|
|
'force_delete' => false, |
1030
|
|
|
'fallback_id' => false, |
1031
|
|
|
], 'both'); |
|
|
|
|
1032
|
|
|
|
1033
|
|
|
$faker = Factory::create(); |
1034
|
|
|
$inputData = [ |
1035
|
|
|
'name' => $faker->name, |
1036
|
|
|
'email' => $faker->safeEmail, |
1037
|
|
|
'password' => bcrypt($faker->password()), |
1038
|
|
|
'remember_token' => null, |
1039
|
|
|
'comets' => [1, 2], |
1040
|
|
|
]; |
1041
|
|
|
|
1042
|
|
|
$entry = $this->crudPanel->create($inputData); |
1043
|
|
|
|
1044
|
|
|
$this->assertCount(2, $entry->comets); |
1045
|
|
|
|
1046
|
|
|
$inputData['comets'] = [2]; |
1047
|
|
|
|
1048
|
|
|
$this->crudPanel->update($entry->id, $inputData); |
1049
|
|
|
|
1050
|
|
|
$this->assertCount(1, $entry->fresh()->comets); |
1051
|
|
|
|
1052
|
|
|
$comets = Comet::all(); |
1053
|
|
|
$this->assertCount(2, $comets); |
1054
|
|
|
$this->assertEquals(0, $comets->first()->user_id); |
1055
|
|
|
} |
1056
|
|
|
} |
1057
|
|
|
|