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