Skip to content

Commit de75abb

Browse files
authored
Add case insensitivity to email search + test cases (#1042)
Bug: T415017
1 parent 2150850 commit de75abb

5 files changed

Lines changed: 28 additions & 2 deletions

File tree

app/Console/Commands/User/CheckUserEmailExist.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function handle(WikiUserEmailChecker $emailChecker): int {
1616
foreach ($emails as $email) {
1717
$found = false;
1818

19-
if (User::whereEmail($email)->exists()) {
19+
if (User::whereEmailInsensitive($email)->exists()) {
2020
$this->line("FOUND: {$email} in apidb.users");
2121
$found = true;
2222
}

app/Services/WikiUserEmailChecker.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ private function emailExists(PDO $pdo, string $dbName, string $table, string $em
5151
$stmt = $pdo->prepare("
5252
SELECT 1
5353
FROM {$dbName}.{$table}
54-
WHERE user_email = :email
54+
WHERE LOWER(CONVERT(user_email USING utf8mb4)) = LOWER(:email)
5555
LIMIT 1
5656
");
5757

app/User.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,8 @@ public function sendEmailVerificationNotification() {
121121
public function getEmailForVerification() {
122122
return $this->email;
123123
}
124+
125+
public function scopeWhereEmailInsensitive($query, string $email) {
126+
return $query->whereRaw('LOWER(email) = ?', [strtolower($email)]);
127+
}
124128
}

tests/Commands/User/CheckUserEmailExistTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
use Tests\TestCase;
99

1010
class CheckUserEmailExistTest extends TestCase {
11+
protected function tearDown(): void {
12+
User::query()->delete();
13+
}
14+
1115
public function testItFindsEmailInApiUsersTable() {
1216
User::factory()->create([
1317
'email' => 'user@example.com',
@@ -51,4 +55,13 @@ public function testEmailFoundInWikiDb() {
5155
->expectsOutput('FOUND: test@example.com in mwdb_test.mwdb_test_user')
5256
->assertExitCode(0);
5357
}
58+
59+
public function testCaseInsensitive() {
60+
User::factory()->create([
61+
'email' => 'Test@Example.com',
62+
]);
63+
$exists = User::whereEmailInsensitive('tEsT@eXaMpLe.CoM')->exists();
64+
65+
$this->assertTrue($exists);
66+
}
5467
}

tests/Services/WikiUserEmailCheckerTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class WikiUserEmailCheckerTest extends TestCase {
1616
['prefix' => 'db_1', 'name' => 'mwdb_1', 'emails' => ['user1@email.localhost', 'user2@email.localhost']],
1717
['prefix' => 'db_2', 'name' => 'mwdb_2', 'emails' => ['user1@email.localhost']],
1818
['prefix' => 'db_3', 'name' => 'mwdb_3', 'emails' => []],
19+
['prefix' => 'db_4', 'name' => 'mwdb_4', 'emails' => ['UsEr4@EmAiL.lOcAlHoSt']],
1920
];
2021

2122
protected function setUp(): void {
@@ -62,4 +63,12 @@ public function testEmailFoundInMultipleDatabases(): void {
6263
$checker->findEmail('user1@email.localhost')
6364
);
6465
}
66+
67+
public function testWikiUserEmailCheckerIsCaseInsensitive(): void {
68+
$checker = new WikiUserEmailChecker($this->db);
69+
$this->assertEquals(
70+
['mwdb_4.db_4_user'],
71+
$checker->findEmail('uSer4@eMAil.localhost')
72+
);
73+
}
6574
}

0 commit comments

Comments
 (0)