Merge pull request #19552 from kamil-tekiela/Fix-default-values
[phpmyadmin.git] / tests / unit / Plugins / Auth / AuthenticationConfigTest.php
blob475448d4131f3a53949e06cede7bdc3d17437fc6
1 <?php
3 declare(strict_types=1);
5 namespace PhpMyAdmin\Tests\Plugins\Auth;
7 use PhpMyAdmin\Config;
8 use PhpMyAdmin\Current;
9 use PhpMyAdmin\Dbal\DatabaseInterface;
10 use PhpMyAdmin\Exceptions\AuthenticationFailure;
11 use PhpMyAdmin\Plugins\Auth\AuthenticationConfig;
12 use PhpMyAdmin\ResponseRenderer;
13 use PhpMyAdmin\Tests\AbstractTestCase;
14 use PHPUnit\Framework\Attributes\CoversClass;
15 use PHPUnit\Framework\Attributes\Medium;
16 use ReflectionProperty;
18 use function json_decode;
20 #[CoversClass(AuthenticationConfig::class)]
21 #[Medium]
22 class AuthenticationConfigTest extends AbstractTestCase
24 protected AuthenticationConfig $object;
26 /**
27 * Configures global environment.
29 protected function setUp(): void
31 parent::setUp();
33 $this->setLanguage();
35 $this->setGlobalConfig();
37 DatabaseInterface::$instance = $this->createDatabaseInterface();
38 Current::$server = 2;
39 Current::$database = 'db';
40 Current::$table = 'table';
41 $this->object = new AuthenticationConfig();
44 /**
45 * tearDown for test cases
47 protected function tearDown(): void
49 parent::tearDown();
51 unset($this->object);
54 public function testShowLoginFormWithoutAjax(): void
56 (new ReflectionProperty(ResponseRenderer::class, 'instance'))->setValue(null, null);
57 ResponseRenderer::getInstance()->setAjax(false);
58 self::assertNull($this->object->showLoginForm());
61 public function testShowLoginFormWithAjax(): void
63 (new ReflectionProperty(ResponseRenderer::class, 'instance'))->setValue(null, null);
64 ResponseRenderer::getInstance()->setAjax(true);
65 $response = $this->object->showLoginForm();
66 self::assertNotNull($response);
67 $body = (string) $response->getBody();
68 self::assertJson($body);
69 $json = json_decode($body, true);
70 self::assertIsArray($json);
71 self::assertArrayHasKey('reload_flag', $json);
72 self::assertSame('1', $json['reload_flag']);
75 public function testAuthCheck(): void
77 Config::getInstance()->selectedServer = ['user' => 'username', 'password' => 'password'];
78 self::assertTrue(
79 $this->object->readCredentials(),
83 public function testAuthSetUser(): void
85 self::assertTrue(
86 $this->object->storeCredentials(),
90 public function testAuthFails(): void
92 Config::getInstance()->settings['Servers'] = [1];
94 $dbi = $this->getMockBuilder(DatabaseInterface::class)
95 ->disableOriginalConstructor()
96 ->getMock();
97 DatabaseInterface::$instance = $dbi;
99 (new ReflectionProperty(ResponseRenderer::class, 'instance'))->setValue(null, null);
101 $response = $this->object->showFailure(AuthenticationFailure::deniedByDatabaseServer());
103 $html = (string) $response->getBody();
105 self::assertStringContainsString(
106 'You probably did not create a configuration file. You might want ' .
107 'to use the <a href="setup/">setup script</a> to create one.',
108 $html,
111 self::assertStringContainsString(
112 '<strong>MySQL said: </strong><a href="index.php?route=/url&url=https%3A%2F%2F' .
113 'dev.mysql.com%2Fdoc%2Frefman%2F5.5%2Fen%2Fserver-error-reference.html"' .
114 ' target="mysql_doc">' .
115 '<img src="themes/dot.gif" title="Documentation" alt="Documentation" ' .
116 'class="icon ic_b_help"></a>',
117 $html,
120 self::assertStringContainsString('Cannot connect: invalid settings.', $html);
122 self::assertStringContainsString(
123 '<a href="index.php?route=/&server=2&lang=en" '
124 . 'class="btn btn-primary mt-1 mb-1 disableAjax">Retry to connect</a>',
125 $html,