Skip to content

Commit

Permalink
User: Make duplication checks optional
Browse files Browse the repository at this point in the history
  • Loading branch information
KreerC committed Mar 19, 2024
1 parent 68dd748 commit b9cfb55
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
10 changes: 6 additions & 4 deletions src/User/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,10 @@ public function getUsername(): string
* Sets the username of the user. Does validation (duplicate check, length check)
*
* @param string $username The new username of the user.
* @param bool $duplicateCheck Whether to check if the username is already in use.
* @return bool True if the username was set successfully, false if a user with the same username already exists.
*/
public function setUsername(string $username): bool
public function setUsername(string $username, bool $duplicateCheck = true): bool
{
if (strlen($username) < 3 || strlen($username) > 20) {
return false;
Expand All @@ -232,7 +233,7 @@ public function setUsername(string $username): bool
return false;
}

if (User::getByUsername($username) !== null) {
if ($duplicateCheck && User::getByUsername($username) !== null) {
return false;
}

Expand All @@ -253,15 +254,16 @@ public function getEmail(): string
* When updating the value, make sure to re-verify the email address and to change the users' status to Unverified.
*
* @param string $email The new email address of the user.
* @param bool $duplicateCheck Whether to check if the email address is already in use.
* @return bool True if the email address was set successfully, false otherwise.
*/
public function setEmail(string $email): bool
public function setEmail(string $email, bool $duplicateCheck = true): bool
{
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
return false;
}

if (User::getByEmail($email) !== null) {
if ($duplicateCheck && User::getByEmail($email) !== null) {
return false;
}

Expand Down
8 changes: 4 additions & 4 deletions tests/User/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ public function testSetEmail(): void
]);

// Test setting a valid email
$this->assertTrue($user->setEmail('[email protected]'));
$this->assertTrue($user->setEmail('[email protected]', false));
$this->assertEquals('[email protected]', $user->getEmail());

// Test setting an invalid email
$this->assertFalse($user->setEmail('invalid-email'));
$this->assertFalse($user->setEmail('invalid-email', false));
$this->assertNotEquals('invalid-email', $user->getEmail());
$this->assertEquals('[email protected]', $user->getEmail());

Expand All @@ -62,11 +62,11 @@ public function testSetUsername(): void
]);

// Test setting a valid username
$this->assertTrue($user->setUsername('john_doe2'));
$this->assertTrue($user->setUsername('john_doe2', false));
$this->assertEquals('john_doe2', $user->getUsername());

// Test setting an invalid username
$this->assertFalse($user->setUsername('invalid-username'));
$this->assertFalse($user->setUsername('invalid-username', false));
$this->assertNotEquals('invalid-username', $user->getUsername());
$this->assertEquals('john_doe2', $user->getUsername());

Expand Down

0 comments on commit b9cfb55

Please sign in to comment.