Skip to content

Commit bc7b647

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

File tree

2 files changed

+68
-19
lines changed

2 files changed

+68
-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: 50 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,26 @@ 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+
$user = $users->findByCredentials(['email' => '[email protected]']);
73+
$this->dontSeeInDatabase('auth_identities', [
74+
'user_id' => $id,
75+
'secret' => '[email protected]',
76+
]);
77+
$this->seeInDatabase('users', [
78+
'id' => $id,
79+
'active' => 0,
80+
]);
81+
}
82+
6383
private function createNewUser(): User
6484
{
6585
$user = new User();
@@ -71,7 +91,7 @@ private function createNewUser(): User
7191
return $user;
7292
}
7393

74-
public function testSaveUpdateUserWithUserDataToUpdate(): void
94+
public function testSaveUpdateUserObjectWithUserDataToUpdate(): void
7595
{
7696
$users = $this->createUserModel();
7797
$user = $this->createNewUser();
@@ -95,7 +115,7 @@ public function testSaveUpdateUserWithUserDataToUpdate(): void
95115
]);
96116
}
97117

98-
public function testUpdateUserWithUserDataToUpdate(): void
118+
public function testUpdateUserObjectWithUserDataToUpdate(): void
99119
{
100120
$users = $this->createUserModel();
101121
$user = $this->createNewUser();
@@ -119,7 +139,32 @@ public function testUpdateUserWithUserDataToUpdate(): void
119139
]);
120140
}
121141

122-
public function testSaveUpdateUserWithNoUserDataToUpdate(): void
142+
public function testUpdateUserArrayWithUserDataToUpdate(): void
143+
{
144+
$users = $this->createUserModel();
145+
$user = $this->createNewUser();
146+
$users->save($user);
147+
148+
$user = $users->findByCredentials(['email' => '[email protected]']);
149+
150+
$user->username = 'bar';
151+
$user->email = '[email protected]';
152+
$user->active = 1;
153+
154+
$userArray = $user->toArray();
155+
$users->update(null, $userArray);
156+
157+
$this->dontSeeInDatabase('auth_identities', [
158+
'user_id' => $user->id,
159+
'secret' => '[email protected]',
160+
]);
161+
$this->seeInDatabase('users', [
162+
'id' => $user->id,
163+
'active' => 1,
164+
]);
165+
}
166+
167+
public function testSaveUpdateUserObjectWithoutUserDataToUpdate(): void
123168
{
124169
$users = $this->createUserModel();
125170
$user = $this->createNewUser();
@@ -137,7 +182,7 @@ public function testSaveUpdateUserWithNoUserDataToUpdate(): void
137182
]);
138183
}
139184

140-
public function testUpdateUserWithNoUserDataToUpdate(): void
185+
public function testUpdateUserObjectWithoutUserDataToUpdate(): void
141186
{
142187
$users = $this->createUserModel();
143188
$user = $this->createNewUser();

0 commit comments

Comments
 (0)