|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PhpMyAdmin\Tests\Plugins; |
| 6 | + |
| 7 | +use PhpMyAdmin\DatabaseInterface; |
| 8 | +use PhpMyAdmin\Exceptions\ExitException; |
| 9 | +use PhpMyAdmin\Http\Factory\ServerRequestFactory; |
| 10 | +use PhpMyAdmin\Plugins\AuthenticationPlugin; |
| 11 | +use PhpMyAdmin\ResponseRenderer; |
| 12 | +use PhpMyAdmin\Tests\AbstractTestCase; |
| 13 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 14 | +use ReflectionProperty; |
| 15 | + |
| 16 | +#[CoversClass(AuthenticationPlugin::class)] |
| 17 | +final class AuthenticationPluginTest extends AbstractTestCase |
| 18 | +{ |
| 19 | + public function testCheckTwoFactor(): void |
| 20 | + { |
| 21 | + $GLOBALS['lang'] = 'en'; |
| 22 | + $dbiDummy = $this->createDbiDummy(); |
| 23 | + $dbiDummy->addResult('SHOW TABLES FROM `phpmyadmin`;', [['pma__userconfig'], ['Tables_in_phpmyadmin']]); |
| 24 | + $dbiDummy->addSelectDb('phpmyadmin'); |
| 25 | + $dbi = $this->createDatabaseInterface($dbiDummy); |
| 26 | + DatabaseInterface::$instance = $dbi; |
| 27 | + |
| 28 | + $object = new class extends AuthenticationPlugin { |
| 29 | + public function showLoginForm(): void |
| 30 | + { |
| 31 | + } |
| 32 | + |
| 33 | + public function readCredentials(): bool |
| 34 | + { |
| 35 | + return false; |
| 36 | + } |
| 37 | + }; |
| 38 | + |
| 39 | + $_SESSION['two_factor_check'] = false; |
| 40 | + |
| 41 | + (new ReflectionProperty(ResponseRenderer::class, 'instance'))->setValue(null, null); |
| 42 | + $responseRenderer = ResponseRenderer::getInstance(); |
| 43 | + $responseRenderer->setAjax(false); |
| 44 | + |
| 45 | + $request = ServerRequestFactory::create()->createServerRequest('GET', 'http://example.com/'); |
| 46 | + |
| 47 | + $object->user = 'test_user'; |
| 48 | + try { |
| 49 | + $object->checkTwoFactor($request); |
| 50 | + } catch (ExitException) { |
| 51 | + } |
| 52 | + |
| 53 | + $response = $responseRenderer->response(); |
| 54 | + self::assertStringContainsString( |
| 55 | + 'You have enabled two factor authentication, please confirm your login.', |
| 56 | + (string) $response->getBody(), |
| 57 | + ); |
| 58 | + } |
| 59 | +} |
0 commit comments