Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions app/Console/Commands/User/CheckUserEmailExist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace App\Console\Commands\User;

use App\Services\WikiUserEmailChecker;
use App\User;
use Illuminate\Console\Command;

class CheckUserEmailExist extends Command {
protected $signature = 'wbs-user:check-email {emails*}';

protected $description = 'Check if emails exist in apidb.users or any MediaWiki user table';

public function handle(WikiUserEmailChecker $emailChecker): int {
$emails = $this->argument('emails');
foreach ($emails as $email) {
$found = false;

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

$mwResults = $emailChecker->findEmail($email);

foreach ($mwResults as $location) {
$this->line("FOUND: {$email} in {$location}");
$found = true;
}

if (!$found) {
$this->line("NOT FOUND: {$email}");
}

$this->line('-------------------------------------------------');
}

return 0;
}
}
62 changes: 62 additions & 0 deletions app/Services/WikiUserEmailChecker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace App\Services;

use Illuminate\Database\DatabaseManager;
use PDO;

class WikiUserEmailChecker {
public function __construct(private DatabaseManager $db) {}

public function findEmail(string $email): array {
$this->db->purge('mw');
$pdo = $this->db->connection('mw')->getPdo();

$mwDatabases = $pdo
->query("SHOW DATABASES LIKE 'mwdb_%'")
->fetchAll(PDO::FETCH_COLUMN);

$foundIn = [];

foreach ($mwDatabases as $dbName) {
$userTable = $this->findUserTable($pdo, $dbName);

if (!$userTable) {
continue;
}

if ($this->emailExists($pdo, $dbName, $userTable, $email)) {
$foundIn[] = "{$dbName}.{$userTable}";
}
}

return $foundIn;
}

private function findUserTable(PDO $pdo, string $dbName): ?string {
$stmt = $pdo->prepare("
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = :db
AND TABLE_NAME LIKE '%\_user'
LIMIT 1
");

$stmt->execute(['db' => $dbName]);

return $stmt->fetchColumn() ?: null;
}

private function emailExists(PDO $pdo, string $dbName, string $table, string $email): bool {
$stmt = $pdo->prepare("
SELECT 1
FROM {$dbName}.{$table}
WHERE user_email = :email
LIMIT 1
");

$stmt->execute(['email' => $email]);

return (bool) $stmt->fetch();
}
}
54 changes: 54 additions & 0 deletions tests/Commands/User/CheckUserEmailExistTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Tests\Commands;

use App\Services\WikiUserEmailChecker;
use App\User;
use Mockery;
use Tests\TestCase;

class CheckUserEmailExistTest extends TestCase {
public function testItFindsEmailInApiUsersTable() {
User::factory()->create([
'email' => 'user@example.com',
]);

// Act & Assert
$this->artisan('wbs-user:check-email', ['emails' => ['user@example.com']])
->expectsOutput('FOUND: user@example.com in apidb.users')
->assertExitCode(0);
}

public function testItReturnsNotFoundIfEmailDoesNotExist() {
$this->artisan('wbs-user:check-email', ['emails' => ['nonexistent@example.com']])
->expectsOutput('NOT FOUND: nonexistent@example.com')
->assertExitCode(0);
}

public function testItChecksMultipleEmails() {
User::factory()->create(['email' => 'user1@example.com']);

$emails = ['user1@example.com', 'other@example.com'];

$this->artisan('wbs-user:check-email', ['emails' => $emails])
->expectsOutput('FOUND: user1@example.com in apidb.users')
->expectsOutput('NOT FOUND: other@example.com')
->assertExitCode(0);
}

public function testEmailFoundInWikiDb() {
$checker = Mockery::mock(WikiUserEmailChecker::class);

$checker->shouldReceive('findEmail')
->with('test@example.com')
->andReturn(['mwdb_test.mwdb_test_user']);

$this->app->instance(WikiUserEmailChecker::class, $checker);

$this->artisan('wbs-user:check-email', [
'emails' => ['test@example.com'],
])
->expectsOutput('FOUND: test@example.com in mwdb_test.mwdb_test_user')
->assertExitCode(0);
}
}
65 changes: 65 additions & 0 deletions tests/Services/WikiUserEmailCheckerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace Tests;

use App\Services\WikiUserEmailChecker;
use App\WikiDb;
use Illuminate\Database\DatabaseManager;
use Illuminate\Foundation\Testing\RefreshDatabase;

class WikiUserEmailCheckerTest extends TestCase {
use RefreshDatabase;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is RefreshDatabase actually used? the test creates and delete databases using PDO.


private DatabaseManager $db;

private $databases = [
['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' => []],
];

protected function setUp(): void {
parent::setUp();
$this->db = $this->app->make('db');
$this->deleteDatabases();
$pdo = $this->db->connection('mw')->getPdo();
foreach ($this->databases as $database) {
$pdo->exec("CREATE DATABASE {$database['name']}");
$userTable = "{$database['name']}.{$database['prefix']}_user";
$pdo->exec("CREATE TABLE {$userTable} (user_email TINYBLOB)");
if ($database['emails']) {
$users = implode(',', array_map(fn ($email) => "('$email')", $database['emails']));
$pdo->exec("INSERT INTO {$userTable} VALUES {$users}");
}
}
}

protected function tearDown(): void {
$this->deleteDatabases();
WikiDb::query()->delete();
parent::tearDown();
}

private function deleteDatabases(): void {
$pdo = $this->db->connection('mw')->getPdo();
foreach ($this->databases as $database) {
$pdo->exec("DROP DATABASE IF EXISTS {$database['name']};");
}
}

public function testCorrectDatabaseFound(): void {
$checker = new WikiUserEmailChecker($this->db);
$this->assertEquals(
['mwdb_1.db_1_user'],
$checker->findEmail('user2@email.localhost')
);
}

public function testEmailFoundInMultipleDatabases(): void {
$checker = new WikiUserEmailChecker($this->db);
$this->assertEquals(
['mwdb_1.db_1_user', 'mwdb_2.db_2_user'],
$checker->findEmail('user1@email.localhost')
);
}
}
Loading