1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\Tests\Unit\CrudPanel; |
4
|
|
|
|
5
|
|
|
use Backpack\CRUD\Tests\config\Models\Article; |
6
|
|
|
use Backpack\CRUD\Tests\config\Models\Bang; |
7
|
|
|
use Backpack\CRUD\Tests\config\Models\Comet; |
8
|
|
|
use Backpack\CRUD\Tests\config\Models\Planet; |
9
|
|
|
use Backpack\CRUD\Tests\config\Models\PlanetNonNullable; |
10
|
|
|
use Backpack\CRUD\Tests\config\Models\Star; |
11
|
|
|
use Backpack\CRUD\Tests\config\Models\Universe; |
12
|
|
|
use Backpack\CRUD\Tests\config\Models\User; |
13
|
|
|
use Faker\Factory; |
14
|
|
|
use Illuminate\Database\Eloquent\Relations\Relation; |
15
|
|
|
use Illuminate\Support\Arr; |
16
|
|
|
use Illuminate\Support\Facades\Hash; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @covers Backpack\CRUD\app\Library\CrudPanel\Traits\Create |
20
|
|
|
* @covers Backpack\CRUD\app\Library\CrudPanel\Traits\Relationships |
21
|
|
|
* @covers Backpack\CRUD\app\Library\CrudPanel\Traits\FieldsProtectedMethods |
22
|
|
|
* @covers Backpack\CRUD\app\Library\CrudPanel\Traits\Update |
23
|
|
|
* @covers Backpack\CRUD\app\Library\CrudPanel\Traits\Input |
24
|
|
|
* @covers Backpack\CRUD\app\Library\CrudPanel\Traits\MorphRelationships |
25
|
|
|
*/ |
26
|
|
|
class CrudPanelCreateTest extends \Backpack\CRUD\Tests\config\CrudPanel\BaseDBCrudPanel |
27
|
|
|
{ |
28
|
|
|
private $nonRelationshipField = [ |
29
|
|
|
'name' => 'field1', |
30
|
|
|
'label' => 'Field1', |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
private $userInputFieldsNoRelationships = [ |
34
|
|
|
[ |
35
|
|
|
'name' => 'id', |
36
|
|
|
'type' => 'hidden', |
37
|
|
|
], [ |
38
|
|
|
'name' => 'name', |
39
|
|
|
], [ |
40
|
|
|
'name' => 'email', |
41
|
|
|
'type' => 'email', |
42
|
|
|
], [ |
43
|
|
|
'name' => 'password', |
44
|
|
|
'type' => 'password', |
45
|
|
|
], |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
|
private $articleInputFieldsOneToMany = [ |
49
|
|
|
[ |
50
|
|
|
'name' => 'id', |
51
|
|
|
'type' => 'hidden', |
52
|
|
|
], [ |
53
|
|
|
'name' => 'content', |
54
|
|
|
], [ |
55
|
|
|
'name' => 'tags', |
56
|
|
|
], [ |
57
|
|
|
'label' => 'Author', |
58
|
|
|
'type' => 'select', |
59
|
|
|
'name' => 'user_id', |
60
|
|
|
'entity' => 'user', |
61
|
|
|
'attribute' => 'name', |
62
|
|
|
], |
63
|
|
|
]; |
64
|
|
|
|
65
|
|
|
private $userInputFieldsManyToMany = [ |
66
|
|
|
[ |
67
|
|
|
'name' => 'id', |
68
|
|
|
'type' => 'hidden', |
69
|
|
|
], [ |
70
|
|
|
'name' => 'name', |
71
|
|
|
], [ |
72
|
|
|
'name' => 'email', |
73
|
|
|
'type' => 'email', |
74
|
|
|
], [ |
75
|
|
|
'name' => 'password', |
76
|
|
|
'type' => 'password', |
77
|
|
|
], [ |
78
|
|
|
'label' => 'Roles', |
79
|
|
|
'type' => 'select_multiple', |
80
|
|
|
'name' => 'roles', |
81
|
|
|
'entity' => 'roles', |
82
|
|
|
'attribute' => 'name', |
83
|
|
|
'pivot' => true, |
84
|
|
|
], |
85
|
|
|
]; |
86
|
|
|
|
87
|
|
|
private $userInputFieldsDotNotation = [ |
88
|
|
|
[ |
89
|
|
|
'name' => 'id', |
90
|
|
|
'type' => 'hidden', |
91
|
|
|
], [ |
92
|
|
|
'name' => 'name', |
93
|
|
|
], [ |
94
|
|
|
'name' => 'email', |
95
|
|
|
'type' => 'email', |
96
|
|
|
], [ |
97
|
|
|
'name' => 'password', |
98
|
|
|
'type' => 'password', |
99
|
|
|
], [ |
100
|
|
|
'label' => 'Roles', |
101
|
|
|
'type' => 'relationship', |
102
|
|
|
'name' => 'roles', |
103
|
|
|
'entity' => 'roles', |
104
|
|
|
'attribute' => 'name', |
105
|
|
|
], [ |
106
|
|
|
'label' => 'Street', |
107
|
|
|
'name' => 'street', |
108
|
|
|
'entity' => 'accountDetails.addresses', |
109
|
|
|
'attribute' => 'street', |
110
|
|
|
], |
111
|
|
|
]; |
112
|
|
|
|
113
|
|
|
private $userInputHasOneRelation = [ |
114
|
|
|
[ |
115
|
|
|
'name' => 'accountDetails.nickname', |
116
|
|
|
], |
117
|
|
|
[ |
118
|
|
|
'name' => 'accountDetails.profile_picture', |
119
|
|
|
], |
120
|
|
|
]; |
121
|
|
|
|
122
|
|
|
private $articleInputBelongsToRelationName = [ |
123
|
|
|
[ |
124
|
|
|
'name' => 'user', |
125
|
|
|
], |
126
|
|
|
]; |
127
|
|
|
|
128
|
|
|
public function testCreate() |
129
|
|
|
{ |
130
|
|
|
$this->crudPanel->setModel(User::class); |
131
|
|
|
$this->crudPanel->addFields($this->userInputFieldsNoRelationships); |
132
|
|
|
$faker = Factory::create(); |
133
|
|
|
$inputData = [ |
134
|
|
|
'name' => $faker->name, |
135
|
|
|
'email' => $faker->safeEmail, |
136
|
|
|
'password' => Hash::make($faker->password()), |
137
|
|
|
]; |
138
|
|
|
|
139
|
|
|
$entry = $this->crudPanel->create($inputData); |
140
|
|
|
|
141
|
|
|
$this->assertInstanceOf(User::class, $entry); |
142
|
|
|
$this->assertEntryEquals($inputData, $entry); |
143
|
|
|
$this->assertEmpty($entry->articles); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function testCreateWithOneToOneRelationship() |
147
|
|
|
{ |
148
|
|
|
$this->crudPanel->setModel(User::class); |
149
|
|
|
$this->crudPanel->addFields($this->userInputFieldsNoRelationships); |
150
|
|
|
$this->crudPanel->addFields($this->userInputHasOneRelation); |
151
|
|
|
$faker = Factory::create(); |
152
|
|
|
$account_details_nickname = $faker->name; |
153
|
|
|
$inputData = [ |
154
|
|
|
'name' => $faker->name, |
155
|
|
|
'email' => $faker->safeEmail, |
156
|
|
|
'password' => Hash::make($faker->password()), |
157
|
|
|
'accountDetails' => [ |
158
|
|
|
'nickname' => $account_details_nickname, |
159
|
|
|
'profile_picture' => 'test.jpg', |
160
|
|
|
], |
161
|
|
|
]; |
162
|
|
|
$entry = $this->crudPanel->create($inputData); |
163
|
|
|
$account_details = $entry->accountDetails()->first(); |
164
|
|
|
|
165
|
|
|
$this->assertEquals($account_details->nickname, $account_details_nickname); |
|
|
|
|
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function testCreateWithOneToOneRelationshipUsingRepeatableInterface() |
169
|
|
|
{ |
170
|
|
|
$this->crudPanel->setModel(User::class); |
171
|
|
|
$this->crudPanel->addFields($this->userInputFieldsNoRelationships); |
172
|
|
|
$this->crudPanel->addField([ |
173
|
|
|
'name' => 'accountDetails', |
174
|
|
|
'fields' => [ |
175
|
|
|
[ |
176
|
|
|
'name' => 'nickname', |
177
|
|
|
], |
178
|
|
|
[ |
179
|
|
|
'name' => 'profile_picture', |
180
|
|
|
], |
181
|
|
|
], |
182
|
|
|
]); |
183
|
|
|
$faker = Factory::create(); |
184
|
|
|
$account_details_nickname = $faker->name; |
185
|
|
|
$inputData = [ |
186
|
|
|
'name' => $faker->name, |
187
|
|
|
'email' => $faker->safeEmail, |
188
|
|
|
'password' => Hash::make($faker->password()), |
189
|
|
|
'accountDetails' => [ |
190
|
|
|
['nickname' => $account_details_nickname, 'profile_picture' => 'test.jpg'], |
191
|
|
|
], |
192
|
|
|
]; |
193
|
|
|
$entry = $this->crudPanel->create($inputData); |
194
|
|
|
$account_details = $entry->accountDetails()->first(); |
195
|
|
|
|
196
|
|
|
$this->assertEquals($account_details->nickname, $account_details_nickname); |
|
|
|
|
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
public function testCreateBelongsToWithRelationName() |
200
|
|
|
{ |
201
|
|
|
$this->crudPanel->setModel(Article::class); |
202
|
|
|
$this->crudPanel->addFields($this->articleInputFieldsOneToMany); |
203
|
|
|
$this->crudPanel->removeField('user_id'); |
204
|
|
|
$this->crudPanel->addFields($this->articleInputBelongsToRelationName); |
205
|
|
|
$faker = Factory::create(); |
206
|
|
|
$inputData = [ |
207
|
|
|
'content' => $faker->text(), |
208
|
|
|
'tags' => $faker->words(3, true), |
209
|
|
|
'user' => 1, |
210
|
|
|
'metas' => null, |
211
|
|
|
'extras' => null, |
212
|
|
|
'cast_metas' => null, |
213
|
|
|
'cast_tags' => null, |
214
|
|
|
'cast_extras' => null, |
215
|
|
|
]; |
216
|
|
|
$entry = $this->crudPanel->create($inputData); |
217
|
|
|
$userEntry = User::find(1); |
|
|
|
|
218
|
|
|
$article = Article::where('user_id', 1)->with('user')->get()->last(); |
219
|
|
|
$this->assertEquals($article->user_id, $entry->user_id); |
220
|
|
|
$this->assertEquals($article->id, $entry->id); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
public function testCreateWithOneToManyRelationship() |
224
|
|
|
{ |
225
|
|
|
$this->crudPanel->setModel(Article::class); |
226
|
|
|
$this->crudPanel->addFields($this->articleInputFieldsOneToMany); |
227
|
|
|
$faker = Factory::create(); |
228
|
|
|
$inputData = [ |
229
|
|
|
'content' => $faker->text(), |
230
|
|
|
'tags' => $faker->words(3, true), |
231
|
|
|
'user_id' => 1, |
232
|
|
|
'metas' => null, |
233
|
|
|
'extras' => null, |
234
|
|
|
'cast_metas' => null, |
235
|
|
|
'cast_tags' => null, |
236
|
|
|
'cast_extras' => null, |
237
|
|
|
]; |
238
|
|
|
|
239
|
|
|
$entry = $this->crudPanel->create($inputData); |
240
|
|
|
$userEntry = User::find(1); |
|
|
|
|
241
|
|
|
$article = Article::where('user_id', 1)->with('user')->get()->last(); |
242
|
|
|
$this->assertEntryEquals($inputData, $entry); |
243
|
|
|
$this->assertEquals($article->user_id, $entry->user_id); |
244
|
|
|
$this->assertEquals($article->id, $entry->id); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
public function testCreateWithManyToManyRelationship() |
248
|
|
|
{ |
249
|
|
|
$this->crudPanel->setModel(User::class); |
250
|
|
|
$this->crudPanel->addFields($this->userInputFieldsManyToMany); |
251
|
|
|
$faker = Factory::create(); |
252
|
|
|
$inputData = [ |
253
|
|
|
'name' => $faker->name, |
254
|
|
|
'email' => $faker->safeEmail, |
255
|
|
|
'password' => Hash::make($faker->password()), |
256
|
|
|
'remember_token' => null, |
257
|
|
|
'roles' => [1, 2], |
258
|
|
|
]; |
259
|
|
|
|
260
|
|
|
$entry = $this->crudPanel->create($inputData); |
261
|
|
|
|
262
|
|
|
$this->assertInstanceOf(User::class, $entry); |
263
|
|
|
$this->assertEntryEquals($inputData, $entry); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
public function testGetRelationFields() |
267
|
|
|
{ |
268
|
|
|
$this->markTestIncomplete('Not correctly implemented'); |
269
|
|
|
|
270
|
|
|
$this->crudPanel->setModel(User::class); |
271
|
|
|
$this->crudPanel->addFields($this->userInputFieldsManyToMany, 'create'); |
272
|
|
|
|
273
|
|
|
// TODO: fix method and documentation. when 'both' is passed as the $form value, the getRelationFields searches |
274
|
|
|
// for relationship fields in the update fields. |
275
|
|
|
$relationFields = $this->crudPanel->getRelationFields('both'); |
276
|
|
|
|
277
|
|
|
$this->assertEquals($this->crudPanel->create_fields['roles'], Arr::last($relationFields)); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
public function testGetRelationFieldsCreateForm() |
281
|
|
|
{ |
282
|
|
|
$this->crudPanel->setModel(User::class); |
283
|
|
|
$this->crudPanel->setOperation('create'); |
284
|
|
|
$this->crudPanel->addFields($this->userInputFieldsManyToMany); |
285
|
|
|
|
286
|
|
|
$relationFields = $this->crudPanel->getRelationFields(); |
287
|
|
|
|
288
|
|
|
$this->assertEquals($this->crudPanel->get('create.fields')['roles'], Arr::last($relationFields)); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
public function testGetRelationFieldsUpdateForm() |
292
|
|
|
{ |
293
|
|
|
$this->crudPanel->setModel(User::class); |
294
|
|
|
$this->crudPanel->setOperation('update'); |
295
|
|
|
$this->crudPanel->addFields($this->userInputFieldsManyToMany); |
296
|
|
|
|
297
|
|
|
$relationFields = $this->crudPanel->getRelationFields(); |
298
|
|
|
|
299
|
|
|
$this->assertEquals($this->crudPanel->get('update.fields')['roles'], Arr::last($relationFields)); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
public function testGetRelationFieldsUnknownForm() |
303
|
|
|
{ |
304
|
|
|
$this->markTestIncomplete('Not correctly implemented'); |
305
|
|
|
|
306
|
|
|
$this->expectException(\InvalidArgumentException::class); |
307
|
|
|
|
308
|
|
|
$this->crudPanel->setModel(User::class); |
309
|
|
|
$this->crudPanel->addFields($this->userInputFieldsManyToMany); |
310
|
|
|
|
311
|
|
|
// TODO: this should throw an invalid argument exception but instead it searches for relationship fields in the |
312
|
|
|
// update fields. |
313
|
|
|
$this->crudPanel->getRelationFields('unknownForm'); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
public function testGetRelationFieldsDotNotation() |
317
|
|
|
{ |
318
|
|
|
$this->crudPanel->setModel(User::class); |
319
|
|
|
$this->crudPanel->setOperation('create'); |
320
|
|
|
|
321
|
|
|
$this->crudPanel->addFields($this->userInputFieldsDotNotation); |
322
|
|
|
|
323
|
|
|
//get all fields with a relation |
324
|
|
|
$relationFields = $this->crudPanel->getRelationFields(); |
325
|
|
|
|
326
|
|
|
$this->assertEquals($this->crudPanel->get('create.fields')['street'], Arr::last($relationFields)); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
public function testCreateHasOneRelations() |
330
|
|
|
{ |
331
|
|
|
$this->crudPanel->setModel(User::class); |
332
|
|
|
$this->crudPanel->setOperation('create'); |
333
|
|
|
|
334
|
|
|
$this->crudPanel->addFields($this->userInputHasOneRelation); |
335
|
|
|
$faker = Factory::create(); |
336
|
|
|
|
337
|
|
|
$inputData = [ |
338
|
|
|
'name' => $faker->name, |
339
|
|
|
'email' => $faker->safeEmail, |
340
|
|
|
'password' => Hash::make($faker->password()), |
341
|
|
|
'remember_token' => null, |
342
|
|
|
'roles' => [1, 2], |
343
|
|
|
'accountDetails' => [ |
344
|
|
|
'nickname' => 'i_have_has_one', |
345
|
|
|
'profile_picture' => 'simple_picture.jpg', |
346
|
|
|
], |
347
|
|
|
]; |
348
|
|
|
$entry = $this->crudPanel->create($inputData); |
349
|
|
|
$updateFields = $this->crudPanel->getUpdateFields($entry->id); |
|
|
|
|
350
|
|
|
$account_details = $entry->accountDetails()->first(); |
351
|
|
|
|
352
|
|
|
$this->assertEquals($account_details->nickname, 'i_have_has_one'); |
|
|
|
|
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
public function testGetRelationFieldsNoRelations() |
356
|
|
|
{ |
357
|
|
|
$this->crudPanel->addField($this->nonRelationshipField); |
358
|
|
|
|
359
|
|
|
$relationFields = $this->crudPanel->getRelationFields(); |
360
|
|
|
|
361
|
|
|
$this->assertEmpty($relationFields); |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
public function testGetRelationFieldsNoFields() |
365
|
|
|
{ |
366
|
|
|
$relationFields = $this->crudPanel->getRelationFields(); |
367
|
|
|
|
368
|
|
|
$this->assertEmpty($relationFields); |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
public function testGetRelationFieldsWithPivot() |
372
|
|
|
{ |
373
|
|
|
$this->crudPanel->setModel(User::class); |
374
|
|
|
$this->crudPanel->setOperation('create'); |
375
|
|
|
$this->crudPanel->addFields($this->userInputFieldsDotNotation); |
376
|
|
|
|
377
|
|
|
$relationFields = $this->crudPanel->getRelationFieldsWithPivot(); |
378
|
|
|
$this->assertEquals($this->crudPanel->get('create.fields')['roles'], Arr::first($relationFields)); |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
public function testGetRelationFieldsWithPivotNoRelations() |
382
|
|
|
{ |
383
|
|
|
$this->crudPanel->setModel(User::class); |
384
|
|
|
$this->crudPanel->setOperation('create'); |
385
|
|
|
$this->crudPanel->addFields($this->nonRelationshipField); |
386
|
|
|
|
387
|
|
|
$relationFields = $this->crudPanel->getRelationFieldsWithPivot(); |
388
|
|
|
|
389
|
|
|
$this->assertEmpty($relationFields); |
390
|
|
|
} |
391
|
|
|
|
392
|
|
|
public function testMorphToManySelectableRelationship() |
393
|
|
|
{ |
394
|
|
|
$this->crudPanel->setModel(User::class); |
395
|
|
|
$this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both'); |
|
|
|
|
396
|
|
|
$this->crudPanel->addField(['name' => 'bills'], 'both'); |
|
|
|
|
397
|
|
|
|
398
|
|
|
$faker = Factory::create(); |
399
|
|
|
$inputData = [ |
400
|
|
|
'name' => $faker->name, |
401
|
|
|
'email' => $faker->safeEmail, |
402
|
|
|
'password' => Hash::make($faker->password()), |
403
|
|
|
'remember_token' => null, |
404
|
|
|
'bills' => [1], |
405
|
|
|
]; |
406
|
|
|
|
407
|
|
|
$entry = $this->crudPanel->create($inputData); |
408
|
|
|
|
409
|
|
|
$updateFields = $this->crudPanel->getUpdateFields($entry->id); |
|
|
|
|
410
|
|
|
|
411
|
|
|
$this->assertCount(1, $entry->bills); |
412
|
|
|
|
413
|
|
|
$this->assertEquals(1, $entry->bills()->first()->id); |
|
|
|
|
414
|
|
|
|
415
|
|
|
$inputData['bills'] = [1, 2]; |
416
|
|
|
|
417
|
|
|
$this->crudPanel->update($entry->id, $inputData); |
418
|
|
|
|
419
|
|
|
$this->assertCount(2, $entry->fresh()->bills); |
420
|
|
|
|
421
|
|
|
$this->assertEquals([1, 2], $entry->fresh()->bills->pluck('id')->toArray()); |
|
|
|
|
422
|
|
|
} |
423
|
|
|
|
424
|
|
|
public function testMorphToManyCreatableRelationship() |
425
|
|
|
{ |
426
|
|
|
$this->crudPanel->setModel(User::class); |
427
|
|
|
$this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both'); |
|
|
|
|
428
|
|
|
$this->crudPanel->addField(['name' => 'recommends', 'subfields' => [ |
429
|
|
|
[ |
430
|
|
|
'name' => 'text', |
431
|
|
|
], |
432
|
|
|
]], 'both'); |
|
|
|
|
433
|
|
|
|
434
|
|
|
$faker = Factory::create(); |
435
|
|
|
$inputData = [ |
436
|
|
|
'name' => $faker->name, |
437
|
|
|
'email' => $faker->safeEmail, |
438
|
|
|
'password' => Hash::make($faker->password()), |
439
|
|
|
'remember_token' => null, |
440
|
|
|
'recommends' => [ |
441
|
|
|
[ |
442
|
|
|
'recommends' => 1, |
443
|
|
|
'text' => 'my pivot recommend field', |
444
|
|
|
], |
445
|
|
|
], |
446
|
|
|
]; |
447
|
|
|
|
448
|
|
|
$entry = $this->crudPanel->create($inputData); |
449
|
|
|
$updateFields = $this->crudPanel->getUpdateFields($entry->id); |
|
|
|
|
450
|
|
|
|
451
|
|
|
$this->assertCount(1, $entry->recommends); |
452
|
|
|
|
453
|
|
|
$this->assertEquals(1, $entry->recommends()->first()->id); |
|
|
|
|
454
|
|
|
|
455
|
|
|
$inputData['recommends'] = [ |
456
|
|
|
[ |
457
|
|
|
'recommends' => 2, |
458
|
|
|
'text' => 'I changed the recommend and the pivot text', |
459
|
|
|
], |
460
|
|
|
]; |
461
|
|
|
|
462
|
|
|
$this->crudPanel->update($entry->id, $inputData); |
463
|
|
|
|
464
|
|
|
$this->assertCount(1, $entry->fresh()->recommends); |
465
|
|
|
|
466
|
|
|
$this->assertEquals(2, $entry->recommends()->first()->id); |
467
|
|
|
|
468
|
|
|
$this->assertEquals('I changed the recommend and the pivot text', $entry->fresh()->recommends->first()->pivot->text); |
|
|
|
|
469
|
|
|
} |
470
|
|
|
|
471
|
|
|
public function testMorphToManyCreatableRelationshipWithMultiple() |
472
|
|
|
{ |
473
|
|
|
$inputData = $this->getPivotInputData(['recommendsDuplicate' => [ |
474
|
|
|
[ |
475
|
|
|
'recommendsDuplicate' => 1, |
476
|
|
|
'text' => 'my pivot recommend field 1', |
477
|
|
|
], |
478
|
|
|
[ |
479
|
|
|
'recommendsDuplicate' => 2, |
480
|
|
|
'text' => 'my pivot recommend field 2', |
481
|
|
|
], |
482
|
|
|
[ |
483
|
|
|
'recommendsDuplicate' => 1, |
484
|
|
|
'text' => 'my pivot recommend field 1x1', |
485
|
|
|
], |
486
|
|
|
], |
487
|
|
|
], true, true); |
488
|
|
|
|
489
|
|
|
$entry = $this->crudPanel->create($inputData); |
490
|
|
|
|
491
|
|
|
$entry = $entry->fresh(); |
492
|
|
|
|
493
|
|
|
$this->assertCount(3, $entry->recommendsDuplicate); |
494
|
|
|
|
495
|
|
|
$this->assertEquals(1, $entry->recommendsDuplicate[0]->id); |
496
|
|
|
$this->assertEquals(1, $entry->recommendsDuplicate[2]->id); |
497
|
|
|
|
498
|
|
|
$inputData['recommendsDuplicate'] = [ |
499
|
|
|
[ |
500
|
|
|
'recommendsDuplicate' => 1, |
501
|
|
|
'text' => 'I changed the recommend and the pivot text', |
502
|
|
|
'id' => 1, |
503
|
|
|
], |
504
|
|
|
[ |
505
|
|
|
'recommendsDuplicate' => 2, |
506
|
|
|
'text' => 'I changed the recommend and the pivot text 2', |
507
|
|
|
'id' => 2, |
508
|
|
|
], |
509
|
|
|
[ |
510
|
|
|
'recommendsDuplicate' => 3, |
511
|
|
|
'text' => 'new recommend and the pivot text 3', |
512
|
|
|
'id' => null, |
513
|
|
|
], |
514
|
|
|
]; |
515
|
|
|
|
516
|
|
|
$this->crudPanel->update($entry->id, $inputData); |
517
|
|
|
|
518
|
|
|
$entry = $entry->fresh(); |
519
|
|
|
|
520
|
|
|
$this->assertCount(3, $entry->recommendsDuplicate); |
521
|
|
|
$this->assertDatabaseCount('recommendables', 3); |
522
|
|
|
|
523
|
|
|
$this->assertEquals('I changed the recommend and the pivot text', $entry->recommendsDuplicate[0]->pivot->text); |
524
|
|
|
$this->assertEquals('I changed the recommend and the pivot text 2', $entry->recommendsDuplicate[1]->pivot->text); |
525
|
|
|
$this->assertEquals('new recommend and the pivot text 3', $entry->recommendsDuplicate[2]->pivot->text); |
526
|
|
|
} |
527
|
|
|
|
528
|
|
|
public function testBelongsToManyWithPivotDataRelationship() |
529
|
|
|
{ |
530
|
|
|
$this->crudPanel->setModel(User::class); |
531
|
|
|
$this->crudPanel->addFields($this->userInputFieldsNoRelationships); |
532
|
|
|
$this->crudPanel->addField([ |
533
|
|
|
'name' => 'superArticles', |
534
|
|
|
'subfields' => [ |
535
|
|
|
[ |
536
|
|
|
'name' => 'notes', |
537
|
|
|
], |
538
|
|
|
], |
539
|
|
|
]); |
540
|
|
|
|
541
|
|
|
$faker = Factory::create(); |
542
|
|
|
$articleData = [ |
543
|
|
|
'content' => $faker->text(), |
544
|
|
|
'tags' => $faker->words(3, true), |
545
|
|
|
'user_id' => 1, |
546
|
|
|
]; |
547
|
|
|
|
548
|
|
|
$article = Article::create($articleData); |
549
|
|
|
|
550
|
|
|
$inputData = [ |
551
|
|
|
'name' => $faker->name, |
552
|
|
|
'email' => $faker->safeEmail, |
553
|
|
|
'password' => Hash::make($faker->password()), |
554
|
|
|
'remember_token' => null, |
555
|
|
|
'superArticles' => [ |
556
|
|
|
[ |
557
|
|
|
'superArticles' => $article->id, |
|
|
|
|
558
|
|
|
'notes' => 'my first article note', |
559
|
|
|
], |
560
|
|
|
], |
561
|
|
|
]; |
562
|
|
|
|
563
|
|
|
$entry = $this->crudPanel->create($inputData); |
564
|
|
|
|
565
|
|
|
$this->assertCount(1, $entry->fresh()->superArticles); |
566
|
|
|
$this->assertEquals('my first article note', $entry->fresh()->superArticles->first()->pivot->notes); |
567
|
|
|
} |
568
|
|
|
|
569
|
|
|
public function testBelongsToManyWithMultipleSameRelationIdAndPivotDataRelationship() |
570
|
|
|
{ |
571
|
|
|
$inputData = $this->getPivotInputData(['superArticlesDuplicates' => [ |
572
|
|
|
[ |
573
|
|
|
'superArticlesDuplicates' => 1, |
574
|
|
|
'notes' => 'my first article note', |
575
|
|
|
'id' => null, |
576
|
|
|
], |
577
|
|
|
[ |
578
|
|
|
'superArticlesDuplicates' => 1, |
579
|
|
|
'notes' => 'my second article note', |
580
|
|
|
'id' => null, |
581
|
|
|
], |
582
|
|
|
[ |
583
|
|
|
'superArticlesDuplicates' => 2, |
584
|
|
|
'notes' => 'my first article2 note', |
585
|
|
|
'id' => null, |
586
|
|
|
], |
587
|
|
|
], |
588
|
|
|
], true, true); |
589
|
|
|
|
590
|
|
|
$entry = $this->crudPanel->create($inputData); |
591
|
|
|
$relationField = $this->crudPanel->getUpdateFields($entry->id)['superArticlesDuplicates']; |
592
|
|
|
|
593
|
|
|
$this->assertCount(3, $relationField['value']); |
594
|
|
|
|
595
|
|
|
$entry = $entry->fresh(); |
596
|
|
|
|
597
|
|
|
$this->assertCount(3, $entry->superArticlesDuplicates); |
598
|
|
|
$this->assertEquals('my first article note', $entry->superArticles->first()->pivot->notes); |
|
|
|
|
599
|
|
|
$this->assertEquals('my second article note', $entry->superArticles[1]->pivot->notes); |
600
|
|
|
$this->assertEquals('my first article2 note', $entry->superArticles[2]->pivot->notes); |
601
|
|
|
|
602
|
|
|
$inputData = $this->getPivotInputData(['superArticlesDuplicates' => [ |
603
|
|
|
[ |
604
|
|
|
'superArticlesDuplicates' => 1, |
605
|
|
|
'notes' => 'my first article note updated', |
606
|
|
|
'id' => 1, |
607
|
|
|
], |
608
|
|
|
[ |
609
|
|
|
'superArticlesDuplicates' => 1, |
610
|
|
|
'notes' => 'my second article note updated', |
611
|
|
|
'id' => 2, |
612
|
|
|
], |
613
|
|
|
[ |
614
|
|
|
'superArticlesDuplicates' => 2, |
615
|
|
|
'notes' => 'my first article2 note updated', |
616
|
|
|
'id' => 3, |
617
|
|
|
], |
618
|
|
|
], |
619
|
|
|
], false, true); |
620
|
|
|
|
621
|
|
|
$entry = $this->crudPanel->update($entry->id, $inputData); |
622
|
|
|
$relationField = $this->crudPanel->getUpdateFields($entry->id)['superArticlesDuplicates']; |
623
|
|
|
$this->assertCount(3, $relationField['value']); |
624
|
|
|
|
625
|
|
|
$entry = $entry->fresh(); |
626
|
|
|
|
627
|
|
|
$this->assertCount(3, $entry->superArticlesDuplicates); |
628
|
|
|
$this->assertEquals('my first article note updated', $entry->superArticles[0]->pivot->notes); |
629
|
|
|
$this->assertEquals('my second article note updated', $entry->superArticles[1]->pivot->notes); |
630
|
|
|
$this->assertEquals('my first article2 note updated', $entry->superArticles[2]->pivot->notes); |
631
|
|
|
} |
632
|
|
|
|
633
|
|
|
public function testBelongsToManyAlwaysSaveSinglePivotWhenMultipleNotAllowed() |
634
|
|
|
{ |
635
|
|
|
$inputData = $this->getPivotInputData(['superArticlesDuplicates' => [ |
636
|
|
|
[ |
637
|
|
|
'superArticlesDuplicates' => 1, |
638
|
|
|
'notes' => 'my first article note', |
639
|
|
|
'id' => null, |
640
|
|
|
], |
641
|
|
|
[ |
642
|
|
|
'superArticlesDuplicates' => 1, |
643
|
|
|
'notes' => 'my second article note', |
644
|
|
|
'id' => null, |
645
|
|
|
], |
646
|
|
|
[ |
647
|
|
|
'superArticlesDuplicates' => 2, |
648
|
|
|
'notes' => 'my first article2 note', |
649
|
|
|
'id' => null, |
650
|
|
|
], |
651
|
|
|
], |
652
|
|
|
]); |
653
|
|
|
|
654
|
|
|
$entry = $this->crudPanel->create($inputData); |
655
|
|
|
$relationField = $this->crudPanel->getUpdateFields($entry->id)['superArticlesDuplicates']; |
656
|
|
|
|
657
|
|
|
$this->assertCount(2, $relationField['value']); |
658
|
|
|
|
659
|
|
|
$entry = $entry->fresh(); |
660
|
|
|
|
661
|
|
|
$this->assertCount(2, $entry->superArticlesDuplicates); |
662
|
|
|
$this->assertEquals('my second article note', $entry->superArticles[0]->pivot->notes); |
663
|
|
|
$this->assertEquals('my first article2 note', $entry->superArticles[1]->pivot->notes); |
664
|
|
|
} |
665
|
|
|
|
666
|
|
|
public function testBelongsToManyDeletesPivotData() |
667
|
|
|
{ |
668
|
|
|
$inputData = $this->getPivotInputData(['superArticlesDuplicates' => [ |
669
|
|
|
[ |
670
|
|
|
'superArticlesDuplicates' => 1, |
671
|
|
|
'notes' => 'my first article note', |
672
|
|
|
'id' => null, |
673
|
|
|
], |
674
|
|
|
[ |
675
|
|
|
'superArticlesDuplicates' => 1, |
676
|
|
|
'notes' => 'my second article note', |
677
|
|
|
'id' => null, |
678
|
|
|
], |
679
|
|
|
[ |
680
|
|
|
'superArticlesDuplicates' => 2, |
681
|
|
|
'notes' => 'my first article2 note', |
682
|
|
|
'id' => null, |
683
|
|
|
], |
684
|
|
|
], |
685
|
|
|
], true, true); |
686
|
|
|
|
687
|
|
|
$entry = $this->crudPanel->create($inputData); |
688
|
|
|
$relationField = $this->crudPanel->getUpdateFields($entry->id)['superArticlesDuplicates']; |
689
|
|
|
|
690
|
|
|
$this->assertCount(3, $relationField['value']); |
691
|
|
|
|
692
|
|
|
$inputData = $this->getPivotInputData(['superArticlesDuplicates' => [ |
693
|
|
|
[ |
694
|
|
|
'superArticlesDuplicates' => 1, |
695
|
|
|
'notes' => 'new first article note', |
696
|
|
|
'id' => null, |
697
|
|
|
], |
698
|
|
|
[ |
699
|
|
|
'superArticlesDuplicates' => 1, |
700
|
|
|
'notes' => 'my second article note updated', |
701
|
|
|
'id' => 2, |
702
|
|
|
], |
703
|
|
|
[ |
704
|
|
|
'superArticlesDuplicates' => 3, |
705
|
|
|
'notes' => 'my first article2 note updated', |
706
|
|
|
'id' => 3, |
707
|
|
|
], |
708
|
|
|
], |
709
|
|
|
], false, true); |
710
|
|
|
|
711
|
|
|
$entry = $this->crudPanel->update($entry->id, $inputData); |
712
|
|
|
$relationField = $this->crudPanel->getUpdateFields($entry->id)['superArticlesDuplicates']; |
713
|
|
|
$this->assertCount(3, $relationField['value']); |
714
|
|
|
|
715
|
|
|
$entry = $entry->fresh(); |
716
|
|
|
|
717
|
|
|
$this->assertCount(3, $entry->superArticlesDuplicates); |
718
|
|
|
$this->assertEquals('new first article note', $entry->superArticles[2]->pivot->notes); |
719
|
|
|
$this->assertEquals('my second article note updated', $entry->superArticles[0]->pivot->notes); |
720
|
|
|
$this->assertEquals('my first article2 note updated', $entry->superArticles[1]->pivot->notes); |
721
|
|
|
} |
722
|
|
|
|
723
|
|
|
public function testCreateHasOneWithNestedRelationsRepeatableInterface() |
724
|
|
|
{ |
725
|
|
|
$this->crudPanel->setModel(User::class); |
726
|
|
|
$this->crudPanel->setOperation('create'); |
727
|
|
|
$this->crudPanel->addFields($this->userInputFieldsNoRelationships); |
728
|
|
|
$this->crudPanel->addField( |
729
|
|
|
[ |
730
|
|
|
'name' => 'accountDetails', |
731
|
|
|
'subfields' => [ |
732
|
|
|
[ |
733
|
|
|
'name' => 'nickname', |
734
|
|
|
], |
735
|
|
|
[ |
736
|
|
|
'name' => 'profile_picture', |
737
|
|
|
], |
738
|
|
|
[ |
739
|
|
|
'name' => 'article', |
740
|
|
|
], |
741
|
|
|
[ |
742
|
|
|
'name' => 'addresses', |
743
|
|
|
'subfields' => [ |
744
|
|
|
[ |
745
|
|
|
'name' => 'bang', |
746
|
|
|
], |
747
|
|
|
[ |
748
|
|
|
'name' => 'street', |
749
|
|
|
], |
750
|
|
|
[ |
751
|
|
|
'name' => 'number', |
752
|
|
|
], |
753
|
|
|
], |
754
|
|
|
], |
755
|
|
|
[ |
756
|
|
|
'name' => 'bangs', |
757
|
|
|
], |
758
|
|
|
[ |
759
|
|
|
'name' => 'bangsPivot', |
760
|
|
|
'subfields' => [ |
761
|
|
|
[ |
762
|
|
|
'name' => 'pivot_field', |
763
|
|
|
], |
764
|
|
|
], |
765
|
|
|
], |
766
|
|
|
], |
767
|
|
|
]); |
768
|
|
|
|
769
|
|
|
$faker = Factory::create(); |
770
|
|
|
|
771
|
|
|
$inputData = [ |
772
|
|
|
'name' => $faker->name, |
773
|
|
|
'email' => $faker->safeEmail, |
774
|
|
|
'password' => Hash::make($faker->password()), |
775
|
|
|
'remember_token' => null, |
776
|
|
|
'roles' => [1, 2], |
777
|
|
|
'accountDetails' => [ |
778
|
|
|
[ |
779
|
|
|
'nickname' => 'i_have_has_one', |
780
|
|
|
'profile_picture' => 'ohh my picture 1.jpg', |
781
|
|
|
'article' => 1, |
782
|
|
|
'addresses' => [ |
783
|
|
|
[ |
784
|
|
|
'bang' => 1, |
785
|
|
|
'street' => 'test', |
786
|
|
|
'number' => 1, |
787
|
|
|
], |
788
|
|
|
[ |
789
|
|
|
'bang' => 1, |
790
|
|
|
'street' => 'test2', |
791
|
|
|
'number' => 2, |
792
|
|
|
], |
793
|
|
|
], |
794
|
|
|
'bangs' => [1, 2], |
795
|
|
|
'bangsPivot' => [ |
796
|
|
|
['bangsPivot' => 1, 'pivot_field' => 'test1'], |
797
|
|
|
['bangsPivot' => 2, 'pivot_field' => 'test2'], |
798
|
|
|
], |
799
|
|
|
], |
800
|
|
|
], |
801
|
|
|
]; |
802
|
|
|
|
803
|
|
|
$entry = $this->crudPanel->create($inputData); |
804
|
|
|
$updateFields = $this->crudPanel->getUpdateFields($entry->id); |
|
|
|
|
805
|
|
|
$account_details = $entry->accountDetails()->first(); |
806
|
|
|
|
807
|
|
|
$this->assertEquals($account_details->article, Article::find(1)); |
|
|
|
|
808
|
|
|
$this->assertEquals($account_details->addresses->count(), 2); |
|
|
|
|
809
|
|
|
$this->assertEquals($account_details->addresses->first()->city, 1); |
|
|
|
|
810
|
|
|
$this->assertEquals($account_details->addresses->first()->street, 'test'); |
|
|
|
|
811
|
|
|
$this->assertEquals($account_details->addresses->first()->number, 1); |
|
|
|
|
812
|
|
|
$this->assertEquals($account_details->bangs->first()->name, Bang::find(1)->name); |
|
|
|
|
813
|
|
|
$this->assertEquals($account_details->bangsPivot->count(), 2); |
|
|
|
|
814
|
|
|
$this->assertEquals($account_details->bangsPivot->first()->pivot->pivot_field, 'test1'); |
|
|
|
|
815
|
|
|
} |
816
|
|
|
|
817
|
|
|
public function testCreateBelongsToFake() |
818
|
|
|
{ |
819
|
|
|
$belongsToField = [ // select_grouped |
820
|
|
|
'label' => 'Select_grouped', |
821
|
|
|
'type' => 'select_grouped', //https://github.com/Laravel-Backpack/CRUD/issues/502 |
822
|
|
|
'name' => 'bang_relation_field', |
823
|
|
|
'fake' => true, |
824
|
|
|
'entity' => 'bang', |
825
|
|
|
'model' => 'Backpack\CRUD\Tests\config\Models\Bang', |
826
|
|
|
'attribute' => 'title', |
827
|
|
|
'group_by' => 'category', // the relationship to entity you want to use for grouping |
828
|
|
|
'group_by_attribute' => 'name', // the attribute on related model, that you want shown |
829
|
|
|
'group_by_relationship_back' => 'articles', // relationship from related model back to this model |
830
|
|
|
'tab' => 'Selects', |
831
|
|
|
'wrapperAttributes' => ['class' => 'form-group col-md-6'], |
832
|
|
|
]; |
833
|
|
|
|
834
|
|
|
$this->crudPanel->setModel(User::class); |
835
|
|
|
$this->crudPanel->setOperation('create'); |
836
|
|
|
$this->crudPanel->addFields($this->userInputFieldsNoRelationships); |
837
|
|
|
$this->crudPanel->addField($belongsToField); |
838
|
|
|
|
839
|
|
|
$faker = Factory::create(); |
840
|
|
|
|
841
|
|
|
$inputData = [ |
842
|
|
|
'name' => $faker->name, |
843
|
|
|
'email' => $faker->safeEmail, |
844
|
|
|
'password' => Hash::make($faker->password()), |
845
|
|
|
'remember_token' => null, |
846
|
|
|
'bang_relation_field' => 1, |
847
|
|
|
]; |
848
|
|
|
|
849
|
|
|
$entry = $this->crudPanel->create($inputData); |
850
|
|
|
$updateFields = $this->crudPanel->getUpdateFields($entry->id); |
|
|
|
|
851
|
|
|
$this->crudPanel->entry = $entry->withFakes(); |
852
|
|
|
$this->assertEquals($entry->bang_relation_field, 1); |
853
|
|
|
} |
854
|
|
|
|
855
|
|
|
public function testCreateHasOneWithNestedRelations() |
856
|
|
|
{ |
857
|
|
|
$this->crudPanel->setModel(User::class); |
858
|
|
|
$this->crudPanel->setOperation('create'); |
859
|
|
|
$this->crudPanel->addFields($this->userInputFieldsNoRelationships); |
860
|
|
|
$this->crudPanel->addFields([ |
861
|
|
|
[ |
862
|
|
|
'name' => 'accountDetails.nickname', |
863
|
|
|
], |
864
|
|
|
[ |
865
|
|
|
'name' => 'accountDetails.profile_picture', |
866
|
|
|
], |
867
|
|
|
[ |
868
|
|
|
'name' => 'accountDetails.article', |
869
|
|
|
], |
870
|
|
|
[ |
871
|
|
|
'name' => 'accountDetails.addresses', |
872
|
|
|
'subfields' => [ |
873
|
|
|
[ |
874
|
|
|
'name' => 'city', |
875
|
|
|
'entity' => 'bang', |
876
|
|
|
], |
877
|
|
|
[ |
878
|
|
|
'name' => 'street', |
879
|
|
|
], |
880
|
|
|
[ |
881
|
|
|
'name' => 'number', |
882
|
|
|
], |
883
|
|
|
], |
884
|
|
|
], |
885
|
|
|
[ |
886
|
|
|
'name' => 'accountDetails.bangs', |
887
|
|
|
], |
888
|
|
|
[ |
889
|
|
|
'name' => 'accountDetails.bangsPivot', |
890
|
|
|
'subfields' => [ |
891
|
|
|
[ |
892
|
|
|
'name' => 'pivot_field', |
893
|
|
|
], |
894
|
|
|
], |
895
|
|
|
], |
896
|
|
|
]); |
897
|
|
|
|
898
|
|
|
$faker = Factory::create(); |
899
|
|
|
|
900
|
|
|
$inputData = [ |
901
|
|
|
'name' => $faker->name, |
902
|
|
|
'email' => $faker->safeEmail, |
903
|
|
|
'password' => Hash::make($faker->password()), |
904
|
|
|
'remember_token' => null, |
905
|
|
|
'roles' => [1, 2], |
906
|
|
|
'accountDetails' => [ |
907
|
|
|
'nickname' => 'i_have_has_one', |
908
|
|
|
'profile_picture' => 'ohh my picture 1.jpg', |
909
|
|
|
'article' => 1, |
910
|
|
|
'addresses' => [ |
911
|
|
|
[ |
912
|
|
|
'city' => 1, |
913
|
|
|
'street' => 'test', |
914
|
|
|
'number' => 1, |
915
|
|
|
], |
916
|
|
|
[ |
917
|
|
|
'city' => 2, |
918
|
|
|
'street' => 'test2', |
919
|
|
|
'number' => 2, |
920
|
|
|
], |
921
|
|
|
], |
922
|
|
|
'bangs' => [1, 2], |
923
|
|
|
'bangsPivot' => [ |
924
|
|
|
['bangsPivot' => 1, 'pivot_field' => 'test1'], |
925
|
|
|
['bangsPivot' => 2, 'pivot_field' => 'test2'], |
926
|
|
|
], |
927
|
|
|
], |
928
|
|
|
]; |
929
|
|
|
|
930
|
|
|
$entry = $this->crudPanel->create($inputData); |
931
|
|
|
$updateFields = $this->crudPanel->getUpdateFields($entry->id); |
|
|
|
|
932
|
|
|
$account_details = $entry->accountDetails()->first(); |
933
|
|
|
|
934
|
|
|
$this->assertEquals($account_details->article, Article::find(1)); |
|
|
|
|
935
|
|
|
$this->assertEquals($account_details->addresses->count(), 2); |
|
|
|
|
936
|
|
|
$this->assertEquals($account_details->addresses->first()->bang->id, 1); |
|
|
|
|
937
|
|
|
$this->assertEquals($account_details->addresses->first()->street, 'test'); |
|
|
|
|
938
|
|
|
$this->assertEquals($account_details->addresses->first()->number, 1); |
|
|
|
|
939
|
|
|
$this->assertEquals($account_details->bangs->first()->name, Bang::find(1)->name); |
|
|
|
|
940
|
|
|
$this->assertEquals($account_details->bangsPivot->count(), 2); |
|
|
|
|
941
|
|
|
$this->assertEquals($account_details->bangsPivot->first()->pivot->pivot_field, 'test1'); |
|
|
|
|
942
|
|
|
|
943
|
|
|
// Now test the remove process |
944
|
|
|
|
945
|
|
|
$inputData = [ |
946
|
|
|
'name' => $faker->name, |
947
|
|
|
'email' => $faker->safeEmail, |
948
|
|
|
'password' => Hash::make($faker->password()), |
949
|
|
|
'remember_token' => null, |
950
|
|
|
'roles' => [1, 2], |
951
|
|
|
'accountDetails' => [ |
952
|
|
|
'nickname' => 'i_have_has_one', |
953
|
|
|
'profile_picture' => 'ohh my picture 1.jpg', |
954
|
|
|
'article' => 1, |
955
|
|
|
'addresses' => [ // HasOne is tested in other test function |
956
|
|
|
[ |
957
|
|
|
'city' => 2, |
958
|
|
|
'street' => 'test', |
959
|
|
|
'number' => 1, |
960
|
|
|
], |
961
|
|
|
[ |
962
|
|
|
'city' => 1, |
963
|
|
|
'street' => 'test2', |
964
|
|
|
'number' => 2, |
965
|
|
|
], |
966
|
|
|
], |
967
|
|
|
'bangs' => [], |
968
|
|
|
'bangsPivot' => [], |
969
|
|
|
], |
970
|
|
|
]; |
971
|
|
|
|
972
|
|
|
$entry = $this->crudPanel->update($entry->id, $inputData); |
973
|
|
|
$account_details = $entry->accountDetails()->first(); |
974
|
|
|
$this->assertEquals($account_details->addresses->count(), 2); |
975
|
|
|
$this->assertEquals($account_details->addresses->first()->bang->id, 2); |
976
|
|
|
$this->assertEquals($account_details->bangs->count(), 0); |
977
|
|
|
$this->assertEquals($account_details->bangsPivot->count(), 0); |
978
|
|
|
} |
979
|
|
|
|
980
|
|
|
public function testMorphOneRelationship() |
981
|
|
|
{ |
982
|
|
|
$this->crudPanel->setModel(User::class); |
983
|
|
|
$this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both'); |
|
|
|
|
984
|
|
|
$this->crudPanel->addField([ |
985
|
|
|
'name' => 'comment.text', |
986
|
|
|
], 'both'); |
|
|
|
|
987
|
|
|
|
988
|
|
|
$faker = Factory::create(); |
989
|
|
|
$inputData = [ |
990
|
|
|
'name' => $faker->name, |
991
|
|
|
'email' => $faker->safeEmail, |
992
|
|
|
'password' => Hash::make($faker->password()), |
993
|
|
|
'remember_token' => null, |
994
|
|
|
'comment' => [ |
995
|
|
|
'text' => 'some test comment text', |
996
|
|
|
], |
997
|
|
|
]; |
998
|
|
|
|
999
|
|
|
$entry = $this->crudPanel->create($inputData); |
1000
|
|
|
$updateFields = $this->crudPanel->getUpdateFields($entry->id); |
|
|
|
|
1001
|
|
|
|
1002
|
|
|
$this->assertEquals($inputData['comment']['text'], $entry->comment->text); |
|
|
|
|
1003
|
|
|
|
1004
|
|
|
$inputData['comment']['text'] = 'updated comment text'; |
1005
|
|
|
|
1006
|
|
|
$this->crudPanel->update($entry->id, $inputData); |
1007
|
|
|
|
1008
|
|
|
$this->assertEquals($inputData['comment']['text'], $entry->fresh()->comment->text); |
1009
|
|
|
} |
1010
|
|
|
|
1011
|
|
|
public function testMorphManyCreatableRelationship() |
1012
|
|
|
{ |
1013
|
|
|
$this->crudPanel->setModel(User::class); |
1014
|
|
|
$this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both'); |
|
|
|
|
1015
|
|
|
$this->crudPanel->addField([ |
1016
|
|
|
'name' => 'stars', |
1017
|
|
|
'subfields' => [ |
1018
|
|
|
[ |
1019
|
|
|
'name' => 'title', |
1020
|
|
|
], |
1021
|
|
|
], |
1022
|
|
|
], 'both'); |
|
|
|
|
1023
|
|
|
|
1024
|
|
|
$faker = Factory::create(); |
1025
|
|
|
$inputData = [ |
1026
|
|
|
'name' => $faker->name, |
1027
|
|
|
'email' => $faker->safeEmail, |
1028
|
|
|
'password' => Hash::make($faker->password()), |
1029
|
|
|
'remember_token' => null, |
1030
|
|
|
'stars' => [ |
1031
|
|
|
[ |
1032
|
|
|
'id' => null, |
1033
|
|
|
'title' => 'this is the star 1 title', |
1034
|
|
|
], |
1035
|
|
|
[ |
1036
|
|
|
'id' => null, |
1037
|
|
|
'title' => 'this is the star 2 title', |
1038
|
|
|
], |
1039
|
|
|
], |
1040
|
|
|
]; |
1041
|
|
|
|
1042
|
|
|
$entry = $this->crudPanel->create($inputData); |
1043
|
|
|
$updateFields = $this->crudPanel->getUpdateFields($entry->id); |
|
|
|
|
1044
|
|
|
|
1045
|
|
|
$this->assertCount(2, $entry->stars); |
1046
|
|
|
|
1047
|
|
|
$this->assertEquals($inputData['stars'][0]['title'], $entry->stars()->first()->title); |
|
|
|
|
1048
|
|
|
|
1049
|
|
|
$inputData['stars'] = [ |
1050
|
|
|
[ |
1051
|
|
|
'id' => 1, |
1052
|
|
|
'title' => 'only one star with changed title', |
1053
|
|
|
], |
1054
|
|
|
]; |
1055
|
|
|
|
1056
|
|
|
$this->crudPanel->update($entry->id, $inputData); |
1057
|
|
|
|
1058
|
|
|
$this->assertCount(1, $entry->fresh()->stars); |
1059
|
|
|
|
1060
|
|
|
$this->assertEquals($inputData['stars'][0]['title'], $entry->fresh()->stars->first()->title); |
1061
|
|
|
$this->assertEquals($inputData['stars'][0]['id'], $entry->fresh()->stars->first()->id); |
1062
|
|
|
} |
1063
|
|
|
|
1064
|
|
|
public function testHasManyCreatableRelationship() |
1065
|
|
|
{ |
1066
|
|
|
$this->crudPanel->setModel(User::class); |
1067
|
|
|
$this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both'); |
|
|
|
|
1068
|
|
|
$this->crudPanel->addField([ |
1069
|
|
|
'name' => 'universes', |
1070
|
|
|
'subfields' => [ |
1071
|
|
|
[ |
1072
|
|
|
'name' => 'title', |
1073
|
|
|
], |
1074
|
|
|
], |
1075
|
|
|
], 'both'); |
|
|
|
|
1076
|
|
|
|
1077
|
|
|
$faker = Factory::create(); |
1078
|
|
|
$inputData = [ |
1079
|
|
|
'name' => $faker->name, |
1080
|
|
|
'email' => $faker->safeEmail, |
1081
|
|
|
'password' => Hash::make($faker->password()), |
1082
|
|
|
'remember_token' => null, |
1083
|
|
|
'universes' => [ |
1084
|
|
|
[ |
1085
|
|
|
'id' => null, |
1086
|
|
|
'title' => 'this is the star 1 title', |
1087
|
|
|
], |
1088
|
|
|
[ |
1089
|
|
|
'title' => 'this is the star 2 title', |
1090
|
|
|
], |
1091
|
|
|
], |
1092
|
|
|
]; |
1093
|
|
|
|
1094
|
|
|
$entry = $this->crudPanel->create($inputData); |
1095
|
|
|
$updateFields = $this->crudPanel->getUpdateFields($entry->id); |
|
|
|
|
1096
|
|
|
|
1097
|
|
|
$this->assertCount(2, $entry->universes); |
1098
|
|
|
|
1099
|
|
|
$this->assertEquals($inputData['universes'][0]['title'], $entry->universes()->first()->title); |
|
|
|
|
1100
|
|
|
|
1101
|
|
|
$inputData['universes'] = [ |
1102
|
|
|
[ |
1103
|
|
|
'id' => 1, |
1104
|
|
|
'title' => 'star 1 with changed title', |
1105
|
|
|
], |
1106
|
|
|
[ |
1107
|
|
|
'id' => 2, |
1108
|
|
|
'title' => 'star 2 with changed title', |
1109
|
|
|
], |
1110
|
|
|
]; |
1111
|
|
|
|
1112
|
|
|
$this->crudPanel->update($entry->id, $inputData); |
1113
|
|
|
|
1114
|
|
|
$universes = $entry->fresh()->universes; |
1115
|
|
|
$this->assertCount(2, $universes); |
1116
|
|
|
$this->assertEquals([1, 2], $universes->pluck('id')->toArray()); |
1117
|
|
|
|
1118
|
|
|
$inputData['universes'] = [ |
1119
|
|
|
[ |
1120
|
|
|
'id' => 1, |
1121
|
|
|
'title' => 'only one star with changed title', |
1122
|
|
|
], |
1123
|
|
|
]; |
1124
|
|
|
|
1125
|
|
|
$this->crudPanel->update($entry->id, $inputData); |
1126
|
|
|
|
1127
|
|
|
$this->assertEquals($inputData['universes'][0]['title'], $entry->fresh()->universes->first()->title); |
|
|
|
|
1128
|
|
|
$this->assertEquals($inputData['universes'][0]['id'], $entry->fresh()->universes->first()->id); |
|
|
|
|
1129
|
|
|
$this->assertEquals(1, Universe::all()->count()); |
1130
|
|
|
|
1131
|
|
|
$inputData['universes'] = [ |
1132
|
|
|
[ |
1133
|
|
|
'id' => null, |
1134
|
|
|
'title' => 'new star 3', |
1135
|
|
|
], |
1136
|
|
|
]; |
1137
|
|
|
|
1138
|
|
|
$this->crudPanel->update($entry->id, $inputData); |
1139
|
|
|
|
1140
|
|
|
$this->assertEquals($inputData['universes'][0]['title'], $entry->fresh()->universes->first()->title); |
1141
|
|
|
$this->assertEquals(3, $entry->fresh()->universes->first()->id); |
1142
|
|
|
$this->assertEquals(1, Universe::all()->count()); |
1143
|
|
|
|
1144
|
|
|
$inputData['universes'] = null; |
1145
|
|
|
|
1146
|
|
|
$this->crudPanel->update($entry->id, $inputData); |
1147
|
|
|
|
1148
|
|
|
$this->assertEquals(0, count($entry->fresh()->universes)); |
1149
|
|
|
$this->assertEquals(0, Universe::all()->count()); |
1150
|
|
|
} |
1151
|
|
|
|
1152
|
|
|
public function testHasManySelectableRelationshipWithoutForceDelete() |
1153
|
|
|
{ |
1154
|
|
|
$this->crudPanel->setModel(User::class); |
1155
|
|
|
$this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both'); |
|
|
|
|
1156
|
|
|
$this->crudPanel->addField([ |
1157
|
|
|
'name' => 'planets', |
1158
|
|
|
'force_delete' => false, |
1159
|
|
|
'fallback_id' => false, |
1160
|
|
|
], 'both'); |
|
|
|
|
1161
|
|
|
|
1162
|
|
|
$faker = Factory::create(); |
1163
|
|
|
$inputData = [ |
1164
|
|
|
'name' => $faker->name, |
1165
|
|
|
'email' => $faker->safeEmail, |
1166
|
|
|
'password' => Hash::make($faker->password()), |
1167
|
|
|
'remember_token' => null, |
1168
|
|
|
'planets' => [1, 2], |
1169
|
|
|
]; |
1170
|
|
|
|
1171
|
|
|
$entry = $this->crudPanel->create($inputData); |
1172
|
|
|
$updateFields = $this->crudPanel->getUpdateFields($entry->id); |
|
|
|
|
1173
|
|
|
|
1174
|
|
|
$this->assertCount(2, $entry->planets); |
1175
|
|
|
|
1176
|
|
|
$inputData['planets'] = [1]; |
1177
|
|
|
|
1178
|
|
|
$this->crudPanel->update($entry->id, $inputData); |
1179
|
|
|
|
1180
|
|
|
$this->assertCount(1, $entry->fresh()->planets); |
1181
|
|
|
|
1182
|
|
|
$planets = Planet::all(); |
1183
|
|
|
|
1184
|
|
|
$this->assertCount(2, $planets); |
1185
|
|
|
} |
1186
|
|
|
|
1187
|
|
|
public function testHasManySelectableRelationshipRemoveAllRelations() |
1188
|
|
|
{ |
1189
|
|
|
$this->crudPanel->setModel(User::class); |
1190
|
|
|
$this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both'); |
|
|
|
|
1191
|
|
|
$this->crudPanel->addField([ |
1192
|
|
|
'name' => 'planets', |
1193
|
|
|
'force_delete' => false, |
1194
|
|
|
'fallback_id' => false, |
1195
|
|
|
], 'both'); |
|
|
|
|
1196
|
|
|
|
1197
|
|
|
$faker = Factory::create(); |
1198
|
|
|
$inputData = [ |
1199
|
|
|
'name' => $faker->name, |
1200
|
|
|
'email' => $faker->safeEmail, |
1201
|
|
|
'password' => Hash::make($faker->password()), |
1202
|
|
|
'remember_token' => null, |
1203
|
|
|
'planets' => [1, 2], |
1204
|
|
|
]; |
1205
|
|
|
|
1206
|
|
|
$entry = $this->crudPanel->create($inputData); |
1207
|
|
|
|
1208
|
|
|
$this->assertCount(2, $entry->planets); |
1209
|
|
|
|
1210
|
|
|
$inputData['planets'] = []; |
1211
|
|
|
|
1212
|
|
|
$this->crudPanel->update($entry->id, $inputData); |
1213
|
|
|
|
1214
|
|
|
$this->assertCount(0, $entry->fresh()->planets); |
1215
|
|
|
|
1216
|
|
|
$planets = Planet::all(); |
1217
|
|
|
|
1218
|
|
|
$this->assertCount(2, $planets); |
1219
|
|
|
} |
1220
|
|
|
|
1221
|
|
|
public function testHasManyWithRelationScoped() |
1222
|
|
|
{ |
1223
|
|
|
$this->crudPanel->setModel(User::class); |
1224
|
|
|
$this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both'); |
|
|
|
|
1225
|
|
|
$this->crudPanel->addField([ |
1226
|
|
|
'name' => 'incomes', |
1227
|
|
|
'subfields' => [ |
1228
|
|
|
[ |
1229
|
|
|
'name' => 'label', |
1230
|
|
|
'type' => 'text', |
1231
|
|
|
], |
1232
|
|
|
[ |
1233
|
|
|
'name' => 'type', |
1234
|
|
|
'type' => 'hidden', |
1235
|
|
|
'value' => 'income', |
1236
|
|
|
], |
1237
|
|
|
[ |
1238
|
|
|
'name' => 'amount', |
1239
|
|
|
'type' => 'number', |
1240
|
|
|
], |
1241
|
|
|
], |
1242
|
|
|
], 'both'); |
|
|
|
|
1243
|
|
|
$this->crudPanel->addField([ |
1244
|
|
|
'name' => 'expenses', |
1245
|
|
|
'subfields' => [ |
1246
|
|
|
[ |
1247
|
|
|
'name' => 'label', |
1248
|
|
|
'type' => 'text', |
1249
|
|
|
], |
1250
|
|
|
[ |
1251
|
|
|
'name' => 'type', |
1252
|
|
|
'type' => 'hidden', |
1253
|
|
|
'value' => 'expense', |
1254
|
|
|
], |
1255
|
|
|
[ |
1256
|
|
|
'name' => 'amount', |
1257
|
|
|
'type' => 'number', |
1258
|
|
|
], |
1259
|
|
|
], |
1260
|
|
|
], 'both'); |
1261
|
|
|
|
1262
|
|
|
$faker = Factory::create(); |
1263
|
|
|
$inputData = [ |
1264
|
|
|
'name' => $faker->name, |
1265
|
|
|
'email' => $faker->safeEmail, |
1266
|
|
|
'password' => Hash::make($faker->password()), |
1267
|
|
|
'remember_token' => null, |
1268
|
|
|
'incomes' => [ |
1269
|
|
|
[ |
1270
|
|
|
'label' => $faker->name, |
1271
|
|
|
'amount' => 33, |
1272
|
|
|
'type' => 'income', |
1273
|
|
|
], |
1274
|
|
|
[ |
1275
|
|
|
'label' => $faker->name, |
1276
|
|
|
'amount' => 22, |
1277
|
|
|
'type' => 'income', |
1278
|
|
|
], |
1279
|
|
|
], |
1280
|
|
|
'expenses' => [ |
1281
|
|
|
[ |
1282
|
|
|
'label' => $faker->name, |
1283
|
|
|
'amount' => 44, |
1284
|
|
|
'type' => 'expense', |
1285
|
|
|
], |
1286
|
|
|
[ |
1287
|
|
|
'label' => $faker->name, |
1288
|
|
|
'amount' => 10, |
1289
|
|
|
'type' => 'expense', |
1290
|
|
|
], |
1291
|
|
|
], |
1292
|
|
|
]; |
1293
|
|
|
$entry = $this->crudPanel->create($inputData); |
1294
|
|
|
|
1295
|
|
|
$firstExpense = $entry->expenses->first(); |
|
|
|
|
1296
|
|
|
$firstIncome = $entry->incomes->first(); |
|
|
|
|
1297
|
|
|
$this->assertCount(2, $entry->expenses); |
1298
|
|
|
$this->assertCount(2, $entry->incomes); |
1299
|
|
|
$this->assertEquals(44, $entry->expenses->first()->amount); |
1300
|
|
|
$this->assertEquals(33, $entry->incomes->first()->amount); |
1301
|
|
|
|
1302
|
|
|
$inputData['incomes'] = [ |
1303
|
|
|
[ |
1304
|
|
|
'id' => 2, |
1305
|
|
|
'label' => $faker->name, |
1306
|
|
|
'amount' => 222, |
1307
|
|
|
'type' => 'income', |
1308
|
|
|
], |
1309
|
|
|
]; |
1310
|
|
|
$inputData['expenses'] = [ |
1311
|
|
|
[ |
1312
|
|
|
'id' => 3, |
1313
|
|
|
'label' => $faker->name, |
1314
|
|
|
'amount' => 44, |
1315
|
|
|
'type' => 'expense', |
1316
|
|
|
], |
1317
|
|
|
[ |
1318
|
|
|
'id' => 4, |
1319
|
|
|
'label' => $faker->name, |
1320
|
|
|
'amount' => 10, |
1321
|
|
|
'type' => 'expense', |
1322
|
|
|
], |
1323
|
|
|
]; |
1324
|
|
|
$this->crudPanel->update($entry->id, $inputData); |
1325
|
|
|
|
1326
|
|
|
$freshIncomes = $entry->fresh()->incomes; |
1327
|
|
|
$freshExpenses = $entry->fresh()->expenses; |
1328
|
|
|
$this->assertCount(2, $freshExpenses); |
1329
|
|
|
$this->assertCount(1, $freshIncomes); |
1330
|
|
|
$this->assertEquals(2, $freshIncomes->first()->id); |
1331
|
|
|
|
1332
|
|
|
$inputData['expenses'] = []; |
1333
|
|
|
$this->crudPanel->update($entry->id, $inputData); |
1334
|
|
|
|
1335
|
|
|
$freshIncomes = $entry->fresh()->incomes; |
1336
|
|
|
$freshExpenses = $entry->fresh()->expenses; |
1337
|
|
|
$this->assertCount(0, $freshExpenses); |
1338
|
|
|
$this->assertCount(1, $freshIncomes); |
1339
|
|
|
} |
1340
|
|
|
|
1341
|
|
|
public function testHasManySelectableRelationshipWithFallbackId() |
1342
|
|
|
{ |
1343
|
|
|
$this->crudPanel->setModel(User::class); |
1344
|
|
|
$this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both'); |
|
|
|
|
1345
|
|
|
$this->crudPanel->addField([ |
1346
|
|
|
'name' => 'planets', |
1347
|
|
|
'fallback_id' => 0, |
1348
|
|
|
'force_delete' => false, |
1349
|
|
|
], 'both'); |
|
|
|
|
1350
|
|
|
|
1351
|
|
|
$faker = Factory::create(); |
1352
|
|
|
$inputData = [ |
1353
|
|
|
'name' => $faker->name, |
1354
|
|
|
'email' => $faker->safeEmail, |
1355
|
|
|
'password' => Hash::make($faker->password()), |
1356
|
|
|
'remember_token' => null, |
1357
|
|
|
'planets' => [1, 2], |
1358
|
|
|
]; |
1359
|
|
|
|
1360
|
|
|
$entry = $this->crudPanel->create($inputData); |
1361
|
|
|
|
1362
|
|
|
$this->assertCount(2, $entry->planets); |
1363
|
|
|
|
1364
|
|
|
$inputData['planets'] = [2]; |
1365
|
|
|
|
1366
|
|
|
$this->crudPanel->update($entry->id, $inputData); |
1367
|
|
|
|
1368
|
|
|
$this->assertCount(1, $entry->fresh()->planets); |
1369
|
|
|
|
1370
|
|
|
$planets = Planet::all(); |
1371
|
|
|
$this->assertCount(2, $planets); |
1372
|
|
|
$this->assertEquals(0, $planets->first()->user_id); |
1373
|
|
|
} |
1374
|
|
|
|
1375
|
|
|
public function testHasManySelectableRelationshipWithForceDelete() |
1376
|
|
|
{ |
1377
|
|
|
$this->crudPanel->setModel(User::class); |
1378
|
|
|
$this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both'); |
|
|
|
|
1379
|
|
|
$this->crudPanel->addField([ |
1380
|
|
|
'name' => 'planets', |
1381
|
|
|
'force_delete' => true, |
1382
|
|
|
'fallback_id' => false, |
1383
|
|
|
], 'both'); |
|
|
|
|
1384
|
|
|
|
1385
|
|
|
$faker = Factory::create(); |
1386
|
|
|
$inputData = [ |
1387
|
|
|
'name' => $faker->name, |
1388
|
|
|
'email' => $faker->safeEmail, |
1389
|
|
|
'password' => Hash::make($faker->password()), |
1390
|
|
|
'remember_token' => null, |
1391
|
|
|
'planets' => [1, 2], |
1392
|
|
|
]; |
1393
|
|
|
|
1394
|
|
|
$entry = $this->crudPanel->create($inputData); |
1395
|
|
|
|
1396
|
|
|
$this->assertCount(2, $entry->planets); |
1397
|
|
|
|
1398
|
|
|
$inputData['planets'] = [2]; |
1399
|
|
|
|
1400
|
|
|
$this->crudPanel->update($entry->id, $inputData); |
1401
|
|
|
|
1402
|
|
|
$this->assertCount(1, $entry->fresh()->planets); |
1403
|
|
|
|
1404
|
|
|
$planets = Planet::all(); |
1405
|
|
|
$this->assertCount(1, $planets); |
1406
|
|
|
} |
1407
|
|
|
|
1408
|
|
|
public function testHasManySelectableRelationshipNonNullableForeignKeyAndDefaultInDatabase() |
1409
|
|
|
{ |
1410
|
|
|
$this->crudPanel->setModel(User::class); |
1411
|
|
|
$this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both'); |
|
|
|
|
1412
|
|
|
$this->crudPanel->addField([ |
1413
|
|
|
'name' => 'comets', |
1414
|
|
|
'force_delete' => false, |
1415
|
|
|
'fallback_id' => false, |
1416
|
|
|
], 'both'); |
|
|
|
|
1417
|
|
|
|
1418
|
|
|
$faker = Factory::create(); |
1419
|
|
|
$inputData = [ |
1420
|
|
|
'name' => $faker->name, |
1421
|
|
|
'email' => $faker->safeEmail, |
1422
|
|
|
'password' => Hash::make($faker->password()), |
1423
|
|
|
'remember_token' => null, |
1424
|
|
|
'comets' => [1, 2], |
1425
|
|
|
]; |
1426
|
|
|
|
1427
|
|
|
$entry = $this->crudPanel->create($inputData); |
1428
|
|
|
|
1429
|
|
|
$this->assertCount(2, $entry->comets); |
1430
|
|
|
|
1431
|
|
|
$inputData['comets'] = [2]; |
1432
|
|
|
|
1433
|
|
|
$this->crudPanel->update($entry->id, $inputData); |
1434
|
|
|
|
1435
|
|
|
$this->assertCount(1, $entry->fresh()->comets); |
1436
|
|
|
|
1437
|
|
|
$comets = Comet::all(); |
1438
|
|
|
$this->assertCount(2, $comets); |
1439
|
|
|
$this->assertEquals(0, $comets->first()->user_id); |
1440
|
|
|
} |
1441
|
|
|
|
1442
|
|
|
public function testHasManySelectableRelationshipNonNullable() |
1443
|
|
|
{ |
1444
|
|
|
$this->crudPanel->setModel(User::class); |
1445
|
|
|
$this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both'); |
|
|
|
|
1446
|
|
|
$this->crudPanel->addField([ |
1447
|
|
|
'name' => 'planetsNonNullable', |
1448
|
|
|
'force_delete' => false, |
1449
|
|
|
'fallback_id' => false, |
1450
|
|
|
], 'both'); |
|
|
|
|
1451
|
|
|
|
1452
|
|
|
$faker = Factory::create(); |
1453
|
|
|
$inputData = [ |
1454
|
|
|
'name' => $faker->name, |
1455
|
|
|
'email' => $faker->safeEmail, |
1456
|
|
|
'password' => Hash::make($faker->password()), |
1457
|
|
|
'remember_token' => null, |
1458
|
|
|
'planetsNonNullable' => [1, 2], |
1459
|
|
|
]; |
1460
|
|
|
|
1461
|
|
|
$entry = $this->crudPanel->create($inputData); |
1462
|
|
|
|
1463
|
|
|
$this->assertCount(2, $entry->planetsNonNullable); |
1464
|
|
|
|
1465
|
|
|
$inputData['planetsNonNullable'] = null; |
1466
|
|
|
|
1467
|
|
|
$this->crudPanel->update($entry->id, $inputData); |
1468
|
|
|
|
1469
|
|
|
$this->assertCount(0, $entry->fresh()->planetsNonNullable); |
1470
|
|
|
|
1471
|
|
|
$planets = PlanetNonNullable::all(); |
1472
|
|
|
$this->assertCount(0, $planets); |
1473
|
|
|
} |
1474
|
|
|
|
1475
|
|
|
public function testCreateHasManyRelationWithDelimitedNameSubfields() |
1476
|
|
|
{ |
1477
|
|
|
$this->crudPanel->setModel(User::class); |
1478
|
|
|
$this->crudPanel->addFields($this->userInputFieldsNoRelationships, 'both'); |
|
|
|
|
1479
|
|
|
$this->crudPanel->addField([ |
1480
|
|
|
'name' => 'universes', |
1481
|
|
|
'subfields' => [ |
1482
|
|
|
[ |
1483
|
|
|
'name' => 'title', |
1484
|
|
|
], |
1485
|
|
|
[ |
1486
|
|
|
'name' => 'start_date,end_date', |
1487
|
|
|
'type' => 'date_range', |
1488
|
|
|
], |
1489
|
|
|
], |
1490
|
|
|
], 'both'); |
|
|
|
|
1491
|
|
|
|
1492
|
|
|
$faker = Factory::create(); |
1493
|
|
|
$inputData = [ |
1494
|
|
|
'name' => $faker->name, |
1495
|
|
|
'email' => $faker->safeEmail, |
1496
|
|
|
'password' => Hash::make($faker->password()), |
1497
|
|
|
'remember_token' => null, |
1498
|
|
|
'universes' => [ |
1499
|
|
|
[ |
1500
|
|
|
'id' => null, |
1501
|
|
|
'title' => 'this is the star 1 title', |
1502
|
|
|
'start_date' => '2021-02-26', |
1503
|
|
|
'end_date' => '2091-01-26', |
1504
|
|
|
], |
1505
|
|
|
[ |
1506
|
|
|
'title' => 'this is the star 2 title', |
1507
|
|
|
'end_date' => '2021-02-26', |
1508
|
|
|
'start_date' => '2091-01-26', |
1509
|
|
|
], |
1510
|
|
|
], |
1511
|
|
|
]; |
1512
|
|
|
|
1513
|
|
|
$entry = $this->crudPanel->create($inputData); |
1514
|
|
|
|
1515
|
|
|
$this->assertCount(2, $entry->universes); |
1516
|
|
|
|
1517
|
|
|
$this->assertEquals($inputData['universes'][0]['start_date'], $entry->universes()->first()->start_date); |
|
|
|
|
1518
|
|
|
$this->assertEquals($inputData['universes'][0]['end_date'], $entry->universes()->first()->end_date); |
|
|
|
|
1519
|
|
|
$this->assertEquals($inputData['universes'][1]['end_date'], $entry->universes()->find(2)->end_date); |
1520
|
|
|
$this->assertEquals($inputData['universes'][1]['start_date'], $entry->universes()->find(2)->start_date); |
1521
|
|
|
} |
1522
|
|
|
|
1523
|
|
|
public function testCreateHasOneRelationWithDelimitedNameSubfields() |
1524
|
|
|
{ |
1525
|
|
|
$this->crudPanel->setModel(User::class); |
1526
|
|
|
$this->crudPanel->setOperation('create'); |
1527
|
|
|
$this->crudPanel->addFields($this->userInputFieldsNoRelationships); |
1528
|
|
|
$this->crudPanel->addField( |
1529
|
|
|
[ |
1530
|
|
|
'name' => 'accountDetails', |
1531
|
|
|
'subfields' => [ |
1532
|
|
|
[ |
1533
|
|
|
'name' => 'nickname', |
1534
|
|
|
], |
1535
|
|
|
[ |
1536
|
|
|
'name' => 'start_date,end_date', |
1537
|
|
|
], |
1538
|
|
|
[ |
1539
|
|
|
'name' => 'profile_picture', |
1540
|
|
|
], |
1541
|
|
|
], |
1542
|
|
|
]); |
1543
|
|
|
|
1544
|
|
|
$faker = Factory::create(); |
1545
|
|
|
|
1546
|
|
|
$inputData = [ |
1547
|
|
|
'name' => $faker->name, |
1548
|
|
|
'email' => $faker->safeEmail, |
1549
|
|
|
'password' => Hash::make($faker->password()), |
1550
|
|
|
'remember_token' => null, |
1551
|
|
|
'roles' => [1, 2], |
1552
|
|
|
'accountDetails' => [ |
1553
|
|
|
[ |
1554
|
|
|
'nickname' => 'i_have_has_one', |
1555
|
|
|
'profile_picture' => 'ohh my picture 1.jpg', |
1556
|
|
|
'start_date' => '2021-02-26', |
1557
|
|
|
'end_date' => '2091-01-26', |
1558
|
|
|
], |
1559
|
|
|
], |
1560
|
|
|
]; |
1561
|
|
|
|
1562
|
|
|
$entry = $this->crudPanel->create($inputData); |
1563
|
|
|
$account_details = $entry->accountDetails()->first(); |
1564
|
|
|
|
1565
|
|
|
$this->assertEquals($account_details->start_date, '2021-02-26'); |
|
|
|
|
1566
|
|
|
$this->assertEquals($account_details->end_date, '2091-01-26'); |
|
|
|
|
1567
|
|
|
} |
1568
|
|
|
|
1569
|
|
|
public function testBelongsToManyWithDelimitedNameSubfields() |
1570
|
|
|
{ |
1571
|
|
|
$this->crudPanel->setModel(User::class); |
1572
|
|
|
$this->crudPanel->addFields($this->userInputFieldsNoRelationships); |
1573
|
|
|
$this->crudPanel->addField([ |
1574
|
|
|
'name' => 'superArticles', |
1575
|
|
|
'subfields' => [ |
1576
|
|
|
[ |
1577
|
|
|
'name' => 'notes', |
1578
|
|
|
], |
1579
|
|
|
[ |
1580
|
|
|
'name' => 'start_date,end_date', |
1581
|
|
|
], |
1582
|
|
|
], |
1583
|
|
|
]); |
1584
|
|
|
|
1585
|
|
|
$faker = Factory::create(); |
1586
|
|
|
$articleData = [ |
1587
|
|
|
'content' => $faker->text(), |
1588
|
|
|
'tags' => $faker->words(3, true), |
1589
|
|
|
'user_id' => 1, |
1590
|
|
|
]; |
1591
|
|
|
|
1592
|
|
|
$article = Article::create($articleData); |
1593
|
|
|
|
1594
|
|
|
$inputData = [ |
1595
|
|
|
'name' => $faker->name, |
1596
|
|
|
'email' => $faker->safeEmail, |
1597
|
|
|
'password' => Hash::make($faker->password()), |
1598
|
|
|
'remember_token' => null, |
1599
|
|
|
'superArticles' => [ |
1600
|
|
|
[ |
1601
|
|
|
'superArticles' => $article->id, |
|
|
|
|
1602
|
|
|
'notes' => 'my first article note', |
1603
|
|
|
'start_date' => '2021-02-26', |
1604
|
|
|
'end_date' => '2091-01-26', |
1605
|
|
|
], |
1606
|
|
|
], |
1607
|
|
|
]; |
1608
|
|
|
|
1609
|
|
|
$entry = $this->crudPanel->create($inputData); |
1610
|
|
|
|
1611
|
|
|
$this->assertCount(1, $entry->fresh()->superArticles); |
1612
|
|
|
$superArticle = $entry->fresh()->superArticles->first(); |
1613
|
|
|
$this->assertEquals($superArticle->pivot->start_date, '2021-02-26'); |
1614
|
|
|
$this->assertEquals($superArticle->pivot->end_date, '2091-01-26'); |
1615
|
|
|
|
1616
|
|
|
$this->crudPanel->getUpdateFields($superArticle->id); |
1617
|
|
|
} |
1618
|
|
|
|
1619
|
|
|
public function testItCanCreateMorphToFieldsStructure() |
1620
|
|
|
{ |
1621
|
|
|
$this->crudPanel->setModel(Star::class); |
1622
|
|
|
$this->crudPanel->addField([ |
1623
|
|
|
'name' => 'starable', |
1624
|
|
|
'morphOptions' => [ |
1625
|
|
|
['Backpack\CRUD\Tests\config\Models\User', 'User'], |
1626
|
|
|
], |
1627
|
|
|
]); |
1628
|
|
|
|
1629
|
|
|
$this->assertCount(2, $this->crudPanel->fields()['starable']['subfields']); |
1630
|
|
|
|
1631
|
|
|
[$morphTypeField, $morphIdField] = $this->crudPanel->fields()['starable']['subfields']; |
1632
|
|
|
|
1633
|
|
|
$this->assertTrue($morphTypeField['name'] === 'starable_type'); |
1634
|
|
|
$this->assertTrue($morphIdField['name'] === 'starable_id'); |
1635
|
|
|
} |
1636
|
|
|
|
1637
|
|
|
public function testIPreventsAddingRepeateadMorphOptions() |
1638
|
|
|
{ |
1639
|
|
|
$this->crudPanel->setModel(Star::class); |
1640
|
|
|
$this->expectException(\Exception::class); |
1641
|
|
|
|
1642
|
|
|
$this->crudPanel->addField([ |
1643
|
|
|
'name' => 'starable', |
1644
|
|
|
'morphOptions' => [ |
1645
|
|
|
['Backpack\CRUD\Tests\config\Models\User', 'User'], |
1646
|
|
|
['Backpack\CRUD\Tests\config\Models\User', 'User'], |
1647
|
|
|
], |
1648
|
|
|
]); |
1649
|
|
|
} |
1650
|
|
|
|
1651
|
|
|
public function testItThrowsErrorIfStringIsNotOnMorphMap() |
1652
|
|
|
{ |
1653
|
|
|
$this->crudPanel->setModel(Star::class); |
1654
|
|
|
$this->expectException(\Exception::class); |
1655
|
|
|
|
1656
|
|
|
$this->crudPanel->addField([ |
1657
|
|
|
'name' => 'starable', |
1658
|
|
|
'morphOptions' => [ |
1659
|
|
|
['somethingThatDoesNotExist'], |
1660
|
|
|
], |
1661
|
|
|
]); |
1662
|
|
|
} |
1663
|
|
|
|
1664
|
|
|
public function testItCanAddTheOptionsFromTheMorphMap() |
1665
|
|
|
{ |
1666
|
|
|
$this->crudPanel->setModel(Star::class); |
1667
|
|
|
|
1668
|
|
|
Relation::morphMap([ |
1669
|
|
|
'user' => 'Backpack\CRUD\Tests\config\Models\User', |
1670
|
|
|
]); |
1671
|
|
|
|
1672
|
|
|
$this->crudPanel->addField([ |
1673
|
|
|
'name' => 'starable', |
1674
|
|
|
'morphOptions' => [ |
1675
|
|
|
['user'], |
1676
|
|
|
], |
1677
|
|
|
]); |
1678
|
|
|
|
1679
|
|
|
[$morphTypeField, $morphIdField] = $this->crudPanel->fields()['starable']['subfields']; |
1680
|
|
|
$this->assertEquals(['user' => 'Backpack\CRUD\Tests\config\Models\User'], $morphTypeField['morphMap']); |
1681
|
|
|
} |
1682
|
|
|
|
1683
|
|
|
public function testItThrowsErrorIfDuplicateMorphMapName() |
1684
|
|
|
{ |
1685
|
|
|
$this->crudPanel->setModel(Star::class); |
1686
|
|
|
$this->expectException(\Exception::class); |
1687
|
|
|
|
1688
|
|
|
Relation::morphMap([ |
1689
|
|
|
'user' => 'Backpack\CRUD\Tests\config\Models\User', |
1690
|
|
|
]); |
1691
|
|
|
|
1692
|
|
|
$this->crudPanel->addField([ |
1693
|
|
|
'name' => 'starable', |
1694
|
|
|
'morphOptions' => [ |
1695
|
|
|
['user'], |
1696
|
|
|
['user'], |
1697
|
|
|
], |
1698
|
|
|
]); |
1699
|
|
|
} |
1700
|
|
|
|
1701
|
|
|
private function getPivotInputData(array $pivotRelationData, bool $initCrud = true, bool $allowDuplicates = false) |
1702
|
|
|
{ |
1703
|
|
|
$faker = Factory::create(); |
1704
|
|
|
|
1705
|
|
|
if ($initCrud) { |
1706
|
|
|
$this->crudPanel->setModel(User::class); |
1707
|
|
|
$this->crudPanel->addFields($this->userInputFieldsNoRelationships); |
1708
|
|
|
$this->crudPanel->addField([ |
1709
|
|
|
'name' => array_key_first($pivotRelationData), |
1710
|
|
|
'allow_duplicate_pivots' => $allowDuplicates, |
1711
|
|
|
'pivot_key_name' => 'id', |
1712
|
|
|
'subfields' => [ |
1713
|
|
|
[ |
1714
|
|
|
'name' => 'notes', |
1715
|
|
|
], |
1716
|
|
|
|
1717
|
|
|
], |
1718
|
|
|
]); |
1719
|
|
|
|
1720
|
|
|
$article = Article::create([ |
|
|
|
|
1721
|
|
|
'content' => $faker->text(), |
1722
|
|
|
'tags' => $faker->words(3, true), |
1723
|
|
|
'user_id' => 1, |
1724
|
|
|
]); |
1725
|
|
|
$article2 = Article::create([ |
|
|
|
|
1726
|
|
|
'content' => $faker->text(), |
1727
|
|
|
'tags' => $faker->words(3, true), |
1728
|
|
|
'user_id' => 1, |
1729
|
|
|
]); |
1730
|
|
|
} |
1731
|
|
|
|
1732
|
|
|
$inputData = [ |
1733
|
|
|
'name' => $faker->name, |
1734
|
|
|
'email' => $faker->safeEmail, |
1735
|
|
|
'password' => Hash::make($faker->password()), |
1736
|
|
|
'remember_token' => null, |
1737
|
|
|
]; |
1738
|
|
|
|
1739
|
|
|
return array_merge($inputData, $pivotRelationData); |
1740
|
|
|
} |
1741
|
|
|
} |
1742
|
|
|
|