AdminUserLoginLogMysqlTest.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace Tests\Integration;
  3. use Illuminate\Support\Facades\DB;
  4. use PDO;
  5. /** Runs the login log scenarios on a disposable MySQL server without loading .env. */
  6. class AdminUserLoginLogMysqlTest extends AdminUserLoginLogTest
  7. {
  8. private ?PDO $control = null;
  9. private string $database;
  10. protected function setUp(): void
  11. {
  12. $socket = getenv('LOGIN_LOG_TEST_MYSQL_SOCKET');
  13. if (!$socket) {
  14. $this->markTestSkipped('Set LOGIN_LOG_TEST_MYSQL_SOCKET to a disposable MySQL server.');
  15. }
  16. $this->control = new PDO('mysql:unix_socket=' . $socket, 'root', '', [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
  17. $this->database = 'bot28_login_log_test_' . bin2hex(random_bytes(8));
  18. $this->control->exec('CREATE DATABASE `' . $this->database . '` CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci');
  19. parent::setUp();
  20. // Reproduce the deployed schema: accounts and user IDs can have different collations.
  21. DB::statement('ALTER TABLE bot_users
  22. MODIFY user_id VARCHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL,
  23. MODIFY account VARCHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL');
  24. }
  25. protected function tearDown(): void
  26. {
  27. if ($this->control !== null) {
  28. parent::tearDown();
  29. $this->control->exec('DROP DATABASE `' . $this->database . '`');
  30. $this->control = null;
  31. }
  32. }
  33. protected function connectionConfig(): array
  34. {
  35. return [
  36. 'driver' => 'mysql', 'unix_socket' => getenv('LOGIN_LOG_TEST_MYSQL_SOCKET'),
  37. 'database' => $this->database, 'username' => 'root', 'password' => '',
  38. 'prefix' => 'bot_', 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_general_ci',
  39. ];
  40. }
  41. public function test_account_matching_keeps_case_insensitive_and_unicode_semantics(): void
  42. {
  43. DB::table('users')->insert([
  44. 'user_id' => '9004', 'member_id' => 'support-unicode', 'account' => '客服É', 'from' => '0',
  45. ]);
  46. $this->log('', 'KEFU001', 0);
  47. $this->log('', '客服é', 0);
  48. $expected = [$this->log('', '其他账号', 0), $this->log('1001', 'ALICE', 0)];
  49. $data = $this->logs(['status' => 0]);
  50. $this->assertSame(2, $data['total']);
  51. $this->assertSame(array_reverse($expected), array_column($data['data'], 'id'));
  52. }
  53. }