Skip to content

Commit

Permalink
feat: when you pass an array user data to insert()/update(), Model ev…
Browse files Browse the repository at this point in the history
…ents do not save Email Identity
  • Loading branch information
kenjis committed Jul 15, 2022
1 parent af13b4c commit 8868006
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 19 deletions.
32 changes: 18 additions & 14 deletions src/Models/UserModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,17 +196,18 @@ 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
*
* @retrun true|int|string Insert ID if $returnID is true
*/
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);

Expand All @@ -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 */
Expand All @@ -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);
Expand All @@ -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 */
Expand Down
54 changes: 49 additions & 5 deletions tests/Unit/UserModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function testSaveInsertUser(): void
]);
}

public function testInsertUser(): void
public function testInsertUserObject(): void
{
$users = $this->createUserModel();

Expand All @@ -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();
Expand All @@ -71,7 +90,7 @@ private function createNewUser(): User
return $user;
}

public function testSaveUpdateUserWithUserDataToUpdate(): void
public function testSaveUpdateUserObjectWithUserDataToUpdate(): void
{
$users = $this->createUserModel();
$user = $this->createNewUser();
Expand All @@ -95,7 +114,7 @@ public function testSaveUpdateUserWithUserDataToUpdate(): void
]);
}

public function testUpdateUserWithUserDataToUpdate(): void
public function testUpdateUserObjectWithUserDataToUpdate(): void
{
$users = $this->createUserModel();
$user = $this->createNewUser();
Expand All @@ -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();
Expand All @@ -137,7 +181,7 @@ public function testSaveUpdateUserWithNoUserDataToUpdate(): void
]);
}

public function testUpdateUserWithNoUserDataToUpdate(): void
public function testUpdateUserObjectWithoutUserDataToUpdate(): void
{
$users = $this->createUserModel();
$user = $this->createNewUser();
Expand Down

0 comments on commit 8868006

Please sign in to comment.