Translated using Weblate (Portuguese)
[phpmyadmin.git] / tests / unit / Plugins / AuthenticationPluginTest.php
blob392dfbabe92be4a909853b6544c371d85d3f562b
1 <?php
3 declare(strict_types=1);
5 namespace PhpMyAdmin\Tests\Plugins;
7 use PhpMyAdmin\Config;
8 use PhpMyAdmin\ConfigStorage\Relation;
9 use PhpMyAdmin\ConfigStorage\RelationParameters;
10 use PhpMyAdmin\Current;
11 use PhpMyAdmin\Dbal\DatabaseInterface;
12 use PhpMyAdmin\Exceptions\AuthenticationFailure;
13 use PhpMyAdmin\Exceptions\ExitException;
14 use PhpMyAdmin\Http\Factory\ServerRequestFactory;
15 use PhpMyAdmin\Http\Response;
16 use PhpMyAdmin\Plugins\AuthenticationPlugin;
17 use PhpMyAdmin\ResponseRenderer;
18 use PhpMyAdmin\Tests\AbstractTestCase;
19 use PHPUnit\Framework\Attributes\CoversClass;
20 use ReflectionProperty;
22 #[CoversClass(AuthenticationPlugin::class)]
23 final class AuthenticationPluginTest extends AbstractTestCase
25 public function testCheckTwoFactor(): void
27 /** @psalm-suppress DeprecatedMethod */
28 $config = Config::getInstance();
29 /** @psalm-suppress InaccessibleProperty */
30 $config->config->debug->simple2fa = true;
32 Current::$lang = 'en';
33 $dbiDummy = $this->createDbiDummy();
34 $dbiDummy->addResult(
35 // phpcs:ignore Generic.Files.LineLength.TooLong
36 "SELECT `config_data`, UNIX_TIMESTAMP(`timevalue`) ts FROM `db_pma`.`pma__userconfig` WHERE `username` = 'test_user'",
37 [['{"2fa":{"backend":"simple","settings":[]}}', '1724620722']],
38 ['config_data', 'ts'],
40 $dbiDummy->addResult('SELECT CURRENT_USER();', [['test_user@localhost']]);
41 DatabaseInterface::$instance = $this->createDatabaseInterface($dbiDummy);
43 $object = new class extends AuthenticationPlugin {
44 public function showLoginForm(): Response|null
46 return null;
49 public function readCredentials(): bool
51 return false;
54 public function showFailure(AuthenticationFailure $failure): Response
56 throw new ExitException();
60 $_SESSION['two_factor_check'] = false;
62 $relationParameters = RelationParameters::fromArray([
63 'user' => 'test_user',
64 'db' => 'db_pma',
65 'userconfigwork' => true,
66 'userconfig' => 'pma__userconfig',
67 ]);
68 (new ReflectionProperty(Relation::class, 'cache'))->setValue(null, $relationParameters);
69 (new ReflectionProperty(ResponseRenderer::class, 'instance'))->setValue(null, null);
70 $responseRenderer = ResponseRenderer::getInstance();
71 $responseRenderer->setAjax(false);
73 $request = ServerRequestFactory::create()->createServerRequest('GET', 'http://example.com/');
75 $object->user = 'test_user';
76 $response = $object->checkTwoFactor($request);
78 self::assertNotNull($response);
79 self::assertStringContainsString(
80 'You have enabled two factor authentication, please confirm your login.',
81 (string) $response->getBody(),
84 $dbiDummy->assertAllQueriesConsumed();
85 $dbiDummy->assertAllSelectsConsumed();
88 public function testCheckTwoFactorConfirmation(): void
90 /** @psalm-suppress DeprecatedMethod */
91 $config = Config::getInstance();
92 /** @psalm-suppress InaccessibleProperty */
93 $config->config->debug->simple2fa = true;
95 Current::$lang = 'en';
96 $dbiDummy = $this->createDbiDummy();
97 $dbiDummy->addResult(
98 // phpcs:ignore Generic.Files.LineLength.TooLong
99 "SELECT `config_data`, UNIX_TIMESTAMP(`timevalue`) ts FROM `db_pma`.`pma__userconfig` WHERE `username` = 'test_user'",
100 [['{"2fa":{"backend":"simple","settings":[]}}', '1724620722']],
101 ['config_data', 'ts'],
103 DatabaseInterface::$instance = $this->createDatabaseInterface($dbiDummy);
105 $object = new class extends AuthenticationPlugin {
106 public function showLoginForm(): Response|null
108 return null;
111 public function readCredentials(): bool
113 return false;
116 public function showFailure(AuthenticationFailure $failure): Response
118 throw new ExitException();
122 $_SESSION['two_factor_check'] = false;
124 $relationParameters = RelationParameters::fromArray([
125 'user' => 'test_user',
126 'db' => 'db_pma',
127 'userconfigwork' => true,
128 'userconfig' => 'pma__userconfig',
130 (new ReflectionProperty(Relation::class, 'cache'))->setValue(null, $relationParameters);
131 (new ReflectionProperty(ResponseRenderer::class, 'instance'))->setValue(null, null);
132 $responseRenderer = ResponseRenderer::getInstance();
133 $responseRenderer->setAjax(false);
135 $request = ServerRequestFactory::create()->createServerRequest('POST', 'http://example.com/')
136 ->withParsedBody(['2fa_confirm' => '1']);
138 $object->user = 'test_user';
139 $response = $object->checkTwoFactor($request);
141 self::assertNull($response);
143 $dbiDummy->assertAllQueriesConsumed();
144 $dbiDummy->assertAllSelectsConsumed();