Skip to content

Commit

Permalink
Add unit tests for UrlRedirector
Browse files Browse the repository at this point in the history
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
  • Loading branch information
MauricioFauth committed Feb 25, 2024
1 parent ecafb16 commit cbff3f3
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
5 changes: 5 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14972,6 +14972,11 @@
<code><![CDATA[providerGetUniqueConditionForGroupFlag]]></code>
</PossiblyUnusedMethod>
</file>
<file src="tests/unit/UrlRedirectorTest.php">
<DeprecatedMethod>
<code><![CDATA[Config::getInstance()]]></code>
</DeprecatedMethod>
</file>
<file src="tests/unit/UrlTest.php">
<DeprecatedMethod>
<code><![CDATA[Config::getInstance()]]></code>
Expand Down
48 changes: 48 additions & 0 deletions tests/unit/UrlRedirectorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace PhpMyAdmin\Tests;

use Fig\Http\Message\StatusCodeInterface;
use PhpMyAdmin\Config;
use PhpMyAdmin\ResponseRenderer;
use PhpMyAdmin\UrlRedirector;
use PHPUnit\Framework\Attributes\CoversClass;
use ReflectionProperty;

#[CoversClass(UrlRedirector::class)]
final class UrlRedirectorTest extends AbstractTestCase
{
public function testRedirectWithDisallowedUrl(): void
{
(new ReflectionProperty(ResponseRenderer::class, 'instance'))->setValue(null, null);
$GLOBALS['lang'] = 'en';
$config = Config::getInstance();
$config->settings['PmaAbsoluteUri'] = 'http://localhost/phpmyadmin';

$response = UrlRedirector::redirect('https://user:pass@example.com/');
self::assertSame('/phpmyadmin/', $response->getHeaderLine('Location'));
self::assertSame(StatusCodeInterface::STATUS_FOUND, $response->getStatusCode());
}

public function testRedirectWithAllowedUrl(): void
{
(new ReflectionProperty(ResponseRenderer::class, 'instance'))->setValue(null, null);
$GLOBALS['lang'] = 'en';
$_SERVER['SERVER_NAME'] = 'localhost';

UrlRedirector::redirect('https://phpmyadmin.net/');
$output = self::getActualOutputForAssertion();
$expected = <<<'HTML'
<script>
window.onload = function () {
window.location = 'https\u003A\/\/phpmyadmin.net\/';
};
</script>
Taking you to the target site.
HTML;

self::assertSame($expected, $output);
}
}

0 comments on commit cbff3f3

Please sign in to comment.