From 88680067809b55378dda3867fa4223fdca115ef9 Mon Sep 17 00:00:00 2001 From: kenjis Date: Thu, 14 Jul 2022 09:52:41 +0900 Subject: [PATCH] feat: when you pass an array user data to insert()/update(), Model events do not save Email Identity --- src/Models/UserModel.php | 32 +++++++++++---------- tests/Unit/UserModelTest.php | 54 ++++++++++++++++++++++++++++++++---- 2 files changed, 67 insertions(+), 19 deletions(-) diff --git a/src/Models/UserModel.php b/src/Models/UserModel.php index 07d2b80e6..1663ccc7d 100644 --- a/src/Models/UserModel.php +++ b/src/Models/UserModel.php @@ -196,7 +196,10 @@ public function activate(User $user): void } /** - * @param User $data + * Override the BaseModel's `insert()` method. + * If you pass User object, also inserts Email Identity. + * + * @param array|User $data * * @throws ValidationException * @@ -204,9 +207,7 @@ public function activate(User $user): void */ public function insert($data = null, bool $returnID = true) { - assert($data instanceof User); - - $this->tempUser = $data; + $this->tempUser = $data instanceof User ? $data : null; $result = parent::insert($data, $returnID); @@ -216,16 +217,17 @@ public function insert($data = null, bool $returnID = true) } /** + * Override the BaseModel's `update()` method. + * If you pass User object, also updates Email Identity. + * * @param array|int|string|null $id - * @param User $data + * @param array|User $data * * @throws ValidationException */ public function update($id = null, $data = null): bool { - assert($data instanceof User); - - $this->tempUser = $data; + $this->tempUser = $data instanceof User ? $data : null; try { /** @throws DataException */ @@ -250,18 +252,15 @@ public function update($id = null, $data = null): bool } /** - * Override the BaseModel's `save()` method to allow - * updating of user email, password, or password_hash fields - * if they've been modified. + * Override the BaseModel's `save()` method. + * If you pass User object, also updates Email Identity. * - * @param User $data + * @param array|User $data * * @throws ValidationException */ public function save($data): bool { - assert($data instanceof User); - $result = parent::save($data); $this->checkQueryReturn($result); @@ -276,6 +275,11 @@ public function save($data): bool */ protected function saveEmailIdentity(array $data): array { + // If insert()/update() gets an array data, do nothing. + if ($this->tempUser === null) { + return $data; + } + // Insert if ($this->tempUser->id === null) { /** @var User $user */ diff --git a/tests/Unit/UserModelTest.php b/tests/Unit/UserModelTest.php index ebf0c15b2..e411b6b49 100644 --- a/tests/Unit/UserModelTest.php +++ b/tests/Unit/UserModelTest.php @@ -41,7 +41,7 @@ public function testSaveInsertUser(): void ]); } - public function testInsertUser(): void + public function testInsertUserObject(): void { $users = $this->createUserModel(); @@ -60,6 +60,25 @@ public function testInsertUser(): void ]); } + public function testInsertUserArray(): void + { + $users = $this->createUserModel(); + + $user = $this->createNewUser(); + + $userArray = $user->toArray(); + $id = $users->insert($userArray); + + $this->dontSeeInDatabase('auth_identities', [ + 'user_id' => $id, + 'secret' => 'foo@bar.com', + ]); + $this->seeInDatabase('users', [ + 'id' => $id, + 'active' => 0, + ]); + } + private function createNewUser(): User { $user = new User(); @@ -71,7 +90,7 @@ private function createNewUser(): User return $user; } - public function testSaveUpdateUserWithUserDataToUpdate(): void + public function testSaveUpdateUserObjectWithUserDataToUpdate(): void { $users = $this->createUserModel(); $user = $this->createNewUser(); @@ -95,7 +114,7 @@ public function testSaveUpdateUserWithUserDataToUpdate(): void ]); } - public function testUpdateUserWithUserDataToUpdate(): void + public function testUpdateUserObjectWithUserDataToUpdate(): void { $users = $this->createUserModel(); $user = $this->createNewUser(); @@ -119,7 +138,32 @@ public function testUpdateUserWithUserDataToUpdate(): void ]); } - public function testSaveUpdateUserWithNoUserDataToUpdate(): void + public function testUpdateUserArrayWithUserDataToUpdate(): void + { + $users = $this->createUserModel(); + $user = $this->createNewUser(); + $users->save($user); + + $user = $users->findByCredentials(['email' => 'foo@bar.com']); + + $user->username = 'bar'; + $user->email = 'bar@bar.com'; + $user->active = 1; + + $userArray = $user->toArray(); + $users->update(null, $userArray); + + $this->dontSeeInDatabase('auth_identities', [ + 'user_id' => $user->id, + 'secret' => 'bar@bar.com', + ]); + $this->seeInDatabase('users', [ + 'id' => $user->id, + 'active' => 1, + ]); + } + + public function testSaveUpdateUserObjectWithoutUserDataToUpdate(): void { $users = $this->createUserModel(); $user = $this->createNewUser(); @@ -137,7 +181,7 @@ public function testSaveUpdateUserWithNoUserDataToUpdate(): void ]); } - public function testUpdateUserWithNoUserDataToUpdate(): void + public function testUpdateUserObjectWithoutUserDataToUpdate(): void { $users = $this->createUserModel(); $user = $this->createNewUser();