|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\Tests\Unit\CrudPanel; |
|
4
|
|
|
|
|
5
|
|
|
use Backpack\CRUD\Tests\Unit\Models\AccountDetails; |
|
6
|
|
|
use Backpack\CRUD\Tests\Unit\Models\Article; |
|
7
|
|
|
use Backpack\CRUD\Tests\Unit\Models\User; |
|
8
|
|
|
use Faker\Factory; |
|
9
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException; |
|
10
|
|
|
use Illuminate\Support\Facades\DB; |
|
11
|
|
|
|
|
12
|
|
|
class CrudPanelUpdateTest extends BaseDBCrudPanelTest |
|
13
|
|
|
{ |
|
14
|
|
|
private $userInputFields = [ |
|
15
|
|
|
[ |
|
16
|
|
|
'name' => 'id', |
|
17
|
|
|
'type' => 'hidden', |
|
18
|
|
|
], [ |
|
19
|
|
|
'name' => 'name', |
|
20
|
|
|
], [ |
|
21
|
|
|
'name' => 'email', |
|
22
|
|
|
'type' => 'email', |
|
23
|
|
|
], [ |
|
24
|
|
|
'name' => 'password', |
|
25
|
|
|
'type' => 'password', |
|
26
|
|
|
], |
|
27
|
|
|
]; |
|
28
|
|
|
|
|
29
|
|
|
private $expectedUpdatedFields = [ |
|
30
|
|
|
'id' => [ |
|
31
|
|
|
'name' => 'id', |
|
32
|
|
|
'type' => 'hidden', |
|
33
|
|
|
'label' => 'Id', |
|
34
|
|
|
], |
|
35
|
|
|
'name' => [ |
|
36
|
|
|
'name' => 'name', |
|
37
|
|
|
'label' => 'Name', |
|
38
|
|
|
'type' => 'text', |
|
39
|
|
|
], |
|
40
|
|
|
'email' => [ |
|
41
|
|
|
'name' => 'email', |
|
42
|
|
|
'type' => 'email', |
|
43
|
|
|
'label' => 'Email', |
|
44
|
|
|
], |
|
45
|
|
|
'password' => [ |
|
46
|
|
|
'name' => 'password', |
|
47
|
|
|
'type' => 'password', |
|
48
|
|
|
'label' => 'Password', |
|
49
|
|
|
], |
|
50
|
|
|
]; |
|
51
|
|
|
|
|
52
|
|
|
private $userInputHasOneRelation = [ |
|
53
|
|
|
[ |
|
54
|
|
|
'name' => 'accountDetails.nickname', |
|
55
|
|
|
], |
|
56
|
|
|
[ |
|
57
|
|
|
'name' => 'accountDetails.profile_picture', |
|
58
|
|
|
], |
|
59
|
|
|
]; |
|
60
|
|
|
|
|
61
|
|
|
private $userInputHasOneWithBelongsToRelation = [ |
|
62
|
|
|
[ |
|
63
|
|
|
'name' => 'accountDetails.nickname', |
|
64
|
|
|
], |
|
65
|
|
|
[ |
|
66
|
|
|
'name' => 'accountDetails.profile_picture', |
|
67
|
|
|
], |
|
68
|
|
|
[ |
|
69
|
|
|
'name' => 'accountDetails.article', |
|
70
|
|
|
'type' => 'relationship', |
|
71
|
|
|
], |
|
72
|
|
|
|
|
73
|
|
|
]; |
|
74
|
|
|
|
|
75
|
|
|
public function testUpdate() |
|
76
|
|
|
{ |
|
77
|
|
|
$this->crudPanel->setModel(User::class); |
|
78
|
|
|
$this->crudPanel->addFields($this->userInputFields); |
|
79
|
|
|
$faker = Factory::create(); |
|
80
|
|
|
$inputData = [ |
|
81
|
|
|
'name' => $faker->name, |
|
82
|
|
|
'email' => $faker->safeEmail, |
|
83
|
|
|
'password' => bcrypt($faker->password()), |
|
84
|
|
|
]; |
|
85
|
|
|
|
|
86
|
|
|
$entry = $this->crudPanel->update(1, $inputData); |
|
87
|
|
|
|
|
88
|
|
|
$this->assertInstanceOf(User::class, $entry); |
|
89
|
|
|
$this->assertEntryEquals($inputData, $entry); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Undocumented function. |
|
94
|
|
|
*/ |
|
95
|
|
|
public function testUpdateExistingOneToOneRelationship() |
|
96
|
|
|
{ |
|
97
|
|
|
$this->crudPanel->setModel(User::class); |
|
98
|
|
|
$this->crudPanel->addFields($this->userInputFields); |
|
99
|
|
|
$this->crudPanel->addFields($this->userInputHasOneRelation); |
|
100
|
|
|
$user = User::find(1)->load('accountDetails'); |
|
101
|
|
|
$this->assertInstanceOf(AccountDetails::class, $user->accountDetails); |
|
102
|
|
|
|
|
103
|
|
|
$inputData = [ |
|
104
|
|
|
'name' => $user->name, |
|
105
|
|
|
'email' => $user->email, |
|
106
|
|
|
'accountDetails' => [ |
|
107
|
|
|
'profile_picture' => 'test_updated.jpg', |
|
108
|
|
|
'nickname' => 'i_have_has_one', |
|
109
|
|
|
], |
|
110
|
|
|
]; |
|
111
|
|
|
|
|
112
|
|
|
$entry = $this->crudPanel->update(1, $inputData); |
|
113
|
|
|
|
|
114
|
|
|
$entry->load('accountDetails'); |
|
115
|
|
|
|
|
116
|
|
|
$this->assertInstanceOf(AccountDetails::class, $entry->accountDetails); |
|
117
|
|
|
$this->assertEquals('test_updated.jpg', $entry->accountDetails->profile_picture); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Undocumented function. |
|
122
|
|
|
*/ |
|
123
|
|
|
public function testClearBelongsToRelationInOneToOneRelationship() |
|
124
|
|
|
{ |
|
125
|
|
|
$this->crudPanel->setModel(User::class); |
|
126
|
|
|
$this->crudPanel->addFields($this->userInputFields); |
|
127
|
|
|
$this->crudPanel->addFields($this->userInputHasOneWithBelongsToRelation); |
|
128
|
|
|
$article = Article::first(); |
|
129
|
|
|
$faker = Factory::create(); |
|
130
|
|
|
$inputData = [ |
|
131
|
|
|
'name' => $faker->name, |
|
132
|
|
|
'email' => $faker->safeEmail, |
|
133
|
|
|
'password' => bcrypt($faker->password()), |
|
134
|
|
|
'accountDetails' => [ |
|
135
|
|
|
'nickname' => $faker->name, |
|
136
|
|
|
'profile_picture' => 'test.jpg', |
|
137
|
|
|
'article' => $article->id, |
|
138
|
|
|
], |
|
139
|
|
|
]; |
|
140
|
|
|
|
|
141
|
|
|
$entry = $this->crudPanel->create($inputData); |
|
142
|
|
|
|
|
143
|
|
|
$entry->load('accountDetails'); |
|
144
|
|
|
|
|
145
|
|
|
$this->assertInstanceOf(AccountDetails::class, $entry->accountDetails); |
|
146
|
|
|
$this->assertInstanceOf(Article::class, $entry->accountDetails->article); |
|
147
|
|
|
|
|
148
|
|
|
$inputData = [ |
|
149
|
|
|
'name' => $faker->name, |
|
150
|
|
|
'email' => $faker->safeEmail, |
|
151
|
|
|
'password' => bcrypt($faker->password()), |
|
152
|
|
|
'accountDetails' => [ |
|
153
|
|
|
'nickname' => $faker->name, |
|
154
|
|
|
'profile_picture' => 'test.jpg', |
|
155
|
|
|
'article' => null, |
|
156
|
|
|
], |
|
157
|
|
|
]; |
|
158
|
|
|
|
|
159
|
|
|
$this->crudPanel->update($entry->id, $inputData); |
|
160
|
|
|
|
|
161
|
|
|
$entry->load('accountDetails'); |
|
162
|
|
|
|
|
163
|
|
|
$this->assertInstanceOf(AccountDetails::class, $entry->accountDetails); |
|
164
|
|
|
$this->assertNull($entry->accountDetails->article); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
public function testUpdateUnknownId() |
|
168
|
|
|
{ |
|
169
|
|
|
$this->expectException(ModelNotFoundException::class); |
|
170
|
|
|
|
|
171
|
|
|
$this->crudPanel->setModel(User::class); |
|
172
|
|
|
$this->crudPanel->addFields($this->userInputFields); |
|
173
|
|
|
$faker = Factory::create(); |
|
174
|
|
|
$inputData = [ |
|
175
|
|
|
'name' => $faker->name, |
|
176
|
|
|
'email' => $faker->safeEmail, |
|
177
|
|
|
'password' => bcrypt($faker->password()), |
|
178
|
|
|
]; |
|
179
|
|
|
|
|
180
|
|
|
$unknownId = DB::getPdo()->lastInsertId() + 2; |
|
181
|
|
|
$this->crudPanel->update($unknownId, $inputData); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
public function testGetUpdateFields() |
|
185
|
|
|
{ |
|
186
|
|
|
$this->crudPanel->setModel(User::class); |
|
187
|
|
|
$this->crudPanel->addFields($this->userInputFields); |
|
188
|
|
|
$faker = Factory::create(); |
|
189
|
|
|
$inputData = [ |
|
190
|
|
|
'name' => $faker->name, |
|
191
|
|
|
'email' => $faker->safeEmail, |
|
192
|
|
|
'password' => bcrypt($faker->password()), |
|
193
|
|
|
]; |
|
194
|
|
|
$entry = $this->crudPanel->create($inputData); |
|
195
|
|
|
$this->addValuesToExpectedFields($entry->id, $inputData); |
|
196
|
|
|
|
|
197
|
|
|
$updateFields = $this->crudPanel->getUpdateFields($entry->id); |
|
198
|
|
|
|
|
199
|
|
|
$this->assertEquals($this->expectedUpdatedFields, $updateFields); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
public function testGetUpdateFieldsUnknownId() |
|
203
|
|
|
{ |
|
204
|
|
|
$this->expectException(ModelNotFoundException::class); |
|
205
|
|
|
|
|
206
|
|
|
$this->crudPanel->setModel(User::class); |
|
207
|
|
|
$this->crudPanel->addFields($this->userInputFields); |
|
208
|
|
|
|
|
209
|
|
|
$unknownId = DB::getPdo()->lastInsertId() + 2; |
|
210
|
|
|
$this->crudPanel->getUpdateFields($unknownId); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
private function addValuesToExpectedFields($id, $inputData) |
|
214
|
|
|
{ |
|
215
|
|
|
foreach ($inputData as $key => $value) { |
|
216
|
|
|
$this->expectedUpdatedFields[$key]['value'] = $value; |
|
217
|
|
|
} |
|
218
|
|
|
$this->expectedUpdatedFields['id']['value'] = $id; |
|
219
|
|
|
} |
|
220
|
|
|
} |
|
221
|
|
|
|