From 39b2fb5f5c7749d5007bd06ecbdc9daab5dc04d3 Mon Sep 17 00:00:00 2001 From: Perside Rosalie Date: Thu, 22 Jan 2026 04:28:52 +0100 Subject: [PATCH 1/2] Add case insensitiviti to email search + test cases Bug: T415017 --- app/Console/Commands/User/CheckUserEmailExist.php | 2 +- app/Services/WikiUserEmailChecker.php | 2 +- app/User.php | 4 ++++ tests/Commands/User/CheckUserEmailExistTest.php | 9 +++++++++ tests/Services/WikiUserEmailCheckerTest.php | 8 ++++++++ 5 files changed, 23 insertions(+), 2 deletions(-) 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..b6ba660c 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(user_email) = 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..a0197d1b 100644 --- a/tests/Commands/User/CheckUserEmailExistTest.php +++ b/tests/Commands/User/CheckUserEmailExistTest.php @@ -51,4 +51,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..d56d0401 100644 --- a/tests/Services/WikiUserEmailCheckerTest.php +++ b/tests/Services/WikiUserEmailCheckerTest.php @@ -62,4 +62,12 @@ public function testEmailFoundInMultipleDatabases(): void { $checker->findEmail('user1@email.localhost') ); } + + public function testWikiUserEmailCheckerIsCaseInsensitive(): void { + $checker = new WikiUserEmailChecker($this->db); + $this->assertEquals( + ['mwdb_1.db_1_user'], + $checker->findEmail('uSer2@eMAil.localhost') + ); + } } From 838b17feb34f8e89c1bbef73a01918d65dc56566 Mon Sep 17 00:00:00 2001 From: Perside Rosalie Date: Thu, 22 Jan 2026 17:21:22 +0100 Subject: [PATCH 2/2] Ensure correct case-insensitive email matching on MW VARBINARY columns --- app/Services/WikiUserEmailChecker.php | 2 +- tests/Commands/User/CheckUserEmailExistTest.php | 6 +++++- tests/Services/WikiUserEmailCheckerTest.php | 5 +++-- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/app/Services/WikiUserEmailChecker.php b/app/Services/WikiUserEmailChecker.php index b6ba660c..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 LOWER(user_email) = LOWER(:email) + WHERE LOWER(CONVERT(user_email USING utf8mb4)) = LOWER(:email) LIMIT 1 "); diff --git a/tests/Commands/User/CheckUserEmailExistTest.php b/tests/Commands/User/CheckUserEmailExistTest.php index a0197d1b..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', @@ -56,7 +60,7 @@ public function testCaseInsensitive() { User::factory()->create([ 'email' => 'Test@Example.com', ]); - $exists = User::whereEmailInsensitive('test@example.com')->exists(); + $exists = User::whereEmailInsensitive('tEsT@eXaMpLe.CoM')->exists(); $this->assertTrue($exists); } diff --git a/tests/Services/WikiUserEmailCheckerTest.php b/tests/Services/WikiUserEmailCheckerTest.php index d56d0401..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 { @@ -66,8 +67,8 @@ public function testEmailFoundInMultipleDatabases(): void { public function testWikiUserEmailCheckerIsCaseInsensitive(): void { $checker = new WikiUserEmailChecker($this->db); $this->assertEquals( - ['mwdb_1.db_1_user'], - $checker->findEmail('uSer2@eMAil.localhost') + ['mwdb_4.db_4_user'], + $checker->findEmail('uSer4@eMAil.localhost') ); } }