- Extracts the ErrorHandler, Config and Template dependencies from the Application class. - Loads HTTP ServerRequest object after loading the user config. - Requires a Config object for the Template class. - Disables Twig's cache if an exception is thrown before loading the user config. This way a possible different cache directory will not be used. Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
105 lines
3.7 KiB
PHP
105 lines
3.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace PhpMyAdmin\Tests;
|
|
|
|
use PhpMyAdmin\Application;
|
|
use PhpMyAdmin\Config;
|
|
use PhpMyAdmin\ErrorHandler;
|
|
use PhpMyAdmin\Exceptions\ConfigException;
|
|
use PhpMyAdmin\Http\ServerRequest;
|
|
use PhpMyAdmin\Template;
|
|
use PHPUnit\Framework\Attributes\CoversClass;
|
|
use ReflectionProperty;
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
|
|
|
#[CoversClass(Application::class)]
|
|
final class ApplicationTest extends AbstractTestCase
|
|
{
|
|
public function testInit(): void
|
|
{
|
|
$application = new Application(
|
|
$this->createStub(ErrorHandler::class),
|
|
$this->createStub(Config::class),
|
|
$this->createStub(Template::class),
|
|
);
|
|
$container = $this->createMock(ContainerBuilder::class);
|
|
$container->expects($this->once())->method('get')
|
|
->with($this->identicalTo(Application::class))->willReturn($application);
|
|
$GLOBALS['containerBuilder'] = $container;
|
|
$this->assertSame($application, Application::init());
|
|
}
|
|
|
|
public function testRunWithConfigError(): void
|
|
{
|
|
$GLOBALS['errorHandler'] = null;
|
|
$errorHandler = $this->createStub(ErrorHandler::class);
|
|
|
|
$GLOBALS['config'] = null;
|
|
$config = $this->createMock(Config::class);
|
|
$config->expects($this->once())->method('loadAndCheck')
|
|
->willThrowException(new ConfigException('Failed to load phpMyAdmin configuration.'));
|
|
|
|
$request = $this->createStub(ServerRequest::class);
|
|
(new ReflectionProperty(Application::class, 'request'))->setValue($request);
|
|
|
|
$template = new Template($config);
|
|
$expected = $template->render('error/generic', [
|
|
'lang' => 'en',
|
|
'dir' => 'ltr',
|
|
'error_message' => 'Failed to load phpMyAdmin configuration.',
|
|
]);
|
|
|
|
$application = new Application($errorHandler, $config, $template);
|
|
$application->run();
|
|
|
|
$output = $this->getActualOutputForAssertion();
|
|
$this->assertSame($expected, $output);
|
|
$this->assertSame($config, $GLOBALS['config']);
|
|
$this->assertSame($errorHandler, $GLOBALS['errorHandler']);
|
|
|
|
(new ReflectionProperty(Application::class, 'request'))->setValue(null);
|
|
}
|
|
|
|
public function testCheckTokenRequestParam(): void
|
|
{
|
|
$application = new Application(
|
|
$this->createStub(ErrorHandler::class),
|
|
$this->createStub(Config::class),
|
|
$this->createStub(Template::class),
|
|
);
|
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET';
|
|
$application->checkTokenRequestParam();
|
|
$this->assertTrue($GLOBALS['token_mismatch']);
|
|
$this->assertFalse($GLOBALS['token_provided']);
|
|
|
|
$_SERVER['REQUEST_METHOD'] = 'POST';
|
|
$_POST['test'] = 'test';
|
|
$application->checkTokenRequestParam();
|
|
$this->assertTrue($GLOBALS['token_mismatch']);
|
|
$this->assertFalse($GLOBALS['token_provided']);
|
|
$this->assertArrayNotHasKey('test', $_POST);
|
|
|
|
$_SERVER['REQUEST_METHOD'] = 'POST';
|
|
$_POST['token'] = 'token';
|
|
$_POST['test'] = 'test';
|
|
$_SESSION[' PMA_token '] = 'mismatch';
|
|
$application->checkTokenRequestParam();
|
|
$this->assertTrue($GLOBALS['token_mismatch']);
|
|
$this->assertTrue($GLOBALS['token_provided']);
|
|
$this->assertArrayNotHasKey('test', $_POST);
|
|
|
|
$_SERVER['REQUEST_METHOD'] = 'POST';
|
|
$_POST['token'] = 'token';
|
|
$_POST['test'] = 'test';
|
|
$_SESSION[' PMA_token '] = 'token';
|
|
$application->checkTokenRequestParam();
|
|
$this->assertFalse($GLOBALS['token_mismatch']);
|
|
$this->assertTrue($GLOBALS['token_provided']);
|
|
$this->assertArrayHasKey('test', $_POST);
|
|
$this->assertEquals('test', $_POST['test']);
|
|
}
|
|
}
|