Skip to content

Commit 8868006

Browse files
committed
feat: when you pass an array user data to insert()/update(), Model events do not save Email Identity
1 parent af13b4c commit 8868006

File tree

2 files changed

+67
-19
lines changed

2 files changed

+67
-19
lines changed

src/Models/UserModel.php

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -196,17 +196,18 @@ public function activate(User $user): void
196196
}
197197

198198
/**
199-
* @param User $data
199+
* Override the BaseModel's `insert()` method.
200+
* If you pass User object, also inserts Email Identity.
201+
*
202+
* @param array|User $data
200203
*
201204
* @throws ValidationException
202205
*
203206
* @retrun true|int|string Insert ID if $returnID is true
204207
*/
205208
public function insert($data = null, bool $returnID = true)
206209
{
207-
assert($data instanceof User);
208-
209-
$this->tempUser = $data;
210+
$this->tempUser = $data instanceof User ? $data : null;
210211

211212
$result = parent::insert($data, $returnID);
212213

@@ -216,16 +217,17 @@ public function insert($data = null, bool $returnID = true)
216217
}
217218

218219
/**
220+
* Override the BaseModel's `update()` method.
221+
* If you pass User object, also updates Email Identity.
222+
*
219223
* @param array|int|string|null $id
220-
* @param User $data
224+
* @param array|User $data
221225
*
222226
* @throws ValidationException
223227
*/
224228
public function update($id = null, $data = null): bool
225229
{
226-
assert($data instanceof User);
227-
228-
$this->tempUser = $data;
230+
$this->tempUser = $data instanceof User ? $data : null;
229231

230232
try {
231233
/** @throws DataException */
@@ -250,18 +252,15 @@ public function update($id = null, $data = null): bool
250252
}
251253

252254
/**
253-
* Override the BaseModel's `save()` method to allow
254-
* updating of user email, password, or password_hash fields
255-
* if they've been modified.
255+
* Override the BaseModel's `save()` method.
256+
* If you pass User object, also updates Email Identity.
256257
*
257-
* @param User $data
258+
* @param array|User $data
258259
*
259260
* @throws ValidationException
260261
*/
261262
public function save($data): bool
262263
{
263-
assert($data instanceof User);
264-
265264
$result = parent::save($data);
266265

267266
$this->checkQueryReturn($result);
@@ -276,6 +275,11 @@ public function save($data): bool
276275
*/
277276
protected function saveEmailIdentity(array $data): array
278277
{
278+
// If insert()/update() gets an array data, do nothing.
279+
if ($this->tempUser === null) {
280+
return $data;
281+
}
282+
279283
// Insert
280284
if ($this->tempUser->id === null) {
281285
/** @var User $user */

tests/Unit/UserModelTest.php

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function testSaveInsertUser(): void
4141
]);
4242
}
4343

44-
public function testInsertUser(): void
44+
public function testInsertUserObject(): void
4545
{
4646
$users = $this->createUserModel();
4747

@@ -60,6 +60,25 @@ public function testInsertUser(): void
6060
]);
6161
}
6262

63+
public function testInsertUserArray(): void
64+
{
65+
$users = $this->createUserModel();
66+
67+
$user = $this->createNewUser();
68+
69+
$userArray = $user->toArray();
70+
$id = $users->insert($userArray);
71+
72+
$this->dontSeeInDatabase('auth_identities', [
73+
'user_id' => $id,
74+
'secret' => 'foo@bar.com',
75+
]);
76+
$this->seeInDatabase('users', [
77+
'id' => $id,
78+
'active' => 0,
79+
]);
80+
}
81+
6382
private function createNewUser(): User
6483
{
6584
$user = new User();
@@ -71,7 +90,7 @@ private function createNewUser(): User
7190
return $user;
7291
}
7392

74-
public function testSaveUpdateUserWithUserDataToUpdate(): void
93+
public function testSaveUpdateUserObjectWithUserDataToUpdate(): void
7594
{
7695
$users = $this->createUserModel();
7796
$user = $this->createNewUser();
@@ -95,7 +114,7 @@ public function testSaveUpdateUserWithUserDataToUpdate(): void
95114
]);
96115
}
97116

98-
public function testUpdateUserWithUserDataToUpdate(): void
117+
public function testUpdateUserObjectWithUserDataToUpdate(): void
99118
{
100119
$users = $this->createUserModel();
101120
$user = $this->createNewUser();
@@ -119,7 +138,32 @@ public function testUpdateUserWithUserDataToUpdate(): void
119138
]);
120139
}
121140

122-
public function testSaveUpdateUserWithNoUserDataToUpdate(): void
141+
public function testUpdateUserArrayWithUserDataToUpdate(): void
142+
{
143+
$users = $this->createUserModel();
144+
$user = $this->createNewUser();
145+
$users->save($user);
146+
147+
$user = $users->findByCredentials(['email' => 'foo@bar.com']);
148+
149+
$user->username = 'bar';
150+
$user->email = 'bar@bar.com';
151+
$user->active = 1;
152+
153+
$userArray = $user->toArray();
154+
$users->update(null, $userArray);
155+
156+
$this->dontSeeInDatabase('auth_identities', [
157+
'user_id' => $user->id,
158+
'secret' => 'bar@bar.com',
159+
]);
160+
$this->seeInDatabase('users', [
161+
'id' => $user->id,
162+
'active' => 1,
163+
]);
164+
}
165+
166+
public function testSaveUpdateUserObjectWithoutUserDataToUpdate(): void
123167
{
124168
$users = $this->createUserModel();
125169
$user = $this->createNewUser();
@@ -137,7 +181,7 @@ public function testSaveUpdateUserWithNoUserDataToUpdate(): void
137181
]);
138182
}
139183

140-
public function testUpdateUserWithNoUserDataToUpdate(): void
184+
public function testUpdateUserObjectWithoutUserDataToUpdate(): void
141185
{
142186
$users = $this->createUserModel();
143187
$user = $this->createNewUser();

0 commit comments

Comments
 (0)