diff --git a/app/Console/Commands/User/CheckUserEmailExist.php b/app/Console/Commands/User/CheckUserEmailExist.php index 5b33f180..859edd77 100644 --- a/app/Console/Commands/User/CheckUserEmailExist.php +++ b/app/Console/Commands/User/CheckUserEmailExist.php @@ -16,7 +16,7 @@ public function handle(WikiUserEmailChecker $emailChecker): int { foreach ($emails as $email) { $found = false; - if (User::whereEmail($email)->exists()) { + if (User::whereEmailInsensitive($email)->exists()) { $this->line("FOUND: {$email} in apidb.users"); $found = true; } diff --git a/app/Services/WikiUserEmailChecker.php b/app/Services/WikiUserEmailChecker.php index fff4c9ed..8e0fdd30 100644 --- a/app/Services/WikiUserEmailChecker.php +++ b/app/Services/WikiUserEmailChecker.php @@ -51,7 +51,7 @@ private function emailExists(PDO $pdo, string $dbName, string $table, string $em $stmt = $pdo->prepare(" SELECT 1 FROM {$dbName}.{$table} - WHERE user_email = :email + WHERE LOWER(CONVERT(user_email USING utf8mb4)) = LOWER(:email) LIMIT 1 "); diff --git a/app/User.php b/app/User.php index 45459e71..e809eed8 100644 --- a/app/User.php +++ b/app/User.php @@ -121,4 +121,8 @@ public function sendEmailVerificationNotification() { public function getEmailForVerification() { return $this->email; } + + public function scopeWhereEmailInsensitive($query, string $email) { + return $query->whereRaw('LOWER(email) = ?', [strtolower($email)]); + } } diff --git a/tests/Commands/User/CheckUserEmailExistTest.php b/tests/Commands/User/CheckUserEmailExistTest.php index 57d74867..d7d3d079 100644 --- a/tests/Commands/User/CheckUserEmailExistTest.php +++ b/tests/Commands/User/CheckUserEmailExistTest.php @@ -8,6 +8,10 @@ use Tests\TestCase; class CheckUserEmailExistTest extends TestCase { + protected function tearDown(): void { + User::query()->delete(); + } + public function testItFindsEmailInApiUsersTable() { User::factory()->create([ 'email' => 'user@example.com', @@ -51,4 +55,13 @@ public function testEmailFoundInWikiDb() { ->expectsOutput('FOUND: test@example.com in mwdb_test.mwdb_test_user') ->assertExitCode(0); } + + public function testCaseInsensitive() { + User::factory()->create([ + 'email' => 'Test@Example.com', + ]); + $exists = User::whereEmailInsensitive('tEsT@eXaMpLe.CoM')->exists(); + + $this->assertTrue($exists); + } } diff --git a/tests/Services/WikiUserEmailCheckerTest.php b/tests/Services/WikiUserEmailCheckerTest.php index 759ca38a..df5bd67c 100644 --- a/tests/Services/WikiUserEmailCheckerTest.php +++ b/tests/Services/WikiUserEmailCheckerTest.php @@ -16,6 +16,7 @@ class WikiUserEmailCheckerTest extends TestCase { ['prefix' => 'db_1', 'name' => 'mwdb_1', 'emails' => ['user1@email.localhost', 'user2@email.localhost']], ['prefix' => 'db_2', 'name' => 'mwdb_2', 'emails' => ['user1@email.localhost']], ['prefix' => 'db_3', 'name' => 'mwdb_3', 'emails' => []], + ['prefix' => 'db_4', 'name' => 'mwdb_4', 'emails' => ['UsEr4@EmAiL.lOcAlHoSt']], ]; protected function setUp(): void { @@ -62,4 +63,12 @@ public function testEmailFoundInMultipleDatabases(): void { $checker->findEmail('user1@email.localhost') ); } + + public function testWikiUserEmailCheckerIsCaseInsensitive(): void { + $checker = new WikiUserEmailChecker($this->db); + $this->assertEquals( + ['mwdb_4.db_4_user'], + $checker->findEmail('uSer4@eMAil.localhost') + ); + } }