1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
|
<?php
declare(strict_types=1);
namespace PhpMyAdmin\Tests\Controllers\Server\Privileges;
use PhpMyAdmin\Controllers\Server\Privileges\AccountLockController;
use PhpMyAdmin\DatabaseInterface;
use PhpMyAdmin\Http\ServerRequest;
use PhpMyAdmin\Message;
use PhpMyAdmin\Server\Privileges\AccountLocking;
use PhpMyAdmin\Template;
use PhpMyAdmin\Tests\AbstractTestCase;
use PhpMyAdmin\Tests\Stubs\ResponseRenderer;
use PHPUnit\Framework\MockObject\Stub;
/**
* @covers \PhpMyAdmin\Controllers\Server\Privileges\AccountLockController
*/
class AccountLockControllerTest extends AbstractTestCase
{
/** @var DatabaseInterface&Stub */
private $dbiStub;
/** @var ServerRequest&Stub */
private $requestStub;
/** @var ResponseRenderer */
private $responseRendererStub;
/** @var AccountLockController */
private $controller;
protected function setUp(): void
{
parent::setUp();
$GLOBALS['server'] = 1;
$GLOBALS['text_dir'] = 'ltr';
$GLOBALS['PMA_PHP_SELF'] = 'index.php';
$this->dbiStub = $this->createStub(DatabaseInterface::class);
$this->dbiStub->method('isMariaDB')->willReturn(true);
$this->dbiStub->method('escapeString')->willReturnArgument(0);
$this->requestStub = $this->createStub(ServerRequest::class);
$this->requestStub->method('getParsedBodyParam')->willReturnOnConsecutiveCalls('test.user', 'test.host');
$this->responseRendererStub = new ResponseRenderer();
$this->responseRendererStub->setAjax(false);
$this->controller = new AccountLockController(
$this->responseRendererStub,
new Template(),
new AccountLocking($this->dbiStub)
);
}
public function testWithValidAccount(): void
{
$this->dbiStub->method('getVersion')->willReturn(100402);
$this->dbiStub->method('tryQuery')->willReturn(true);
($this->controller)($this->requestStub);
$message = Message::success('The account test.user@test.host has been successfully locked.');
$this->assertTrue($this->responseRendererStub->isAjax());
$this->assertEquals(200, $this->responseRendererStub->getHttpResponseCode());
$this->assertTrue($this->responseRendererStub->hasSuccessState());
$this->assertEquals(['message' => $message->getDisplay()], $this->responseRendererStub->getJSONResult());
}
public function testWithInvalidAccount(): void
{
$this->dbiStub->method('getVersion')->willReturn(100402);
$this->dbiStub->method('tryQuery')->willReturn(false);
$this->dbiStub->method('getError')->willReturn('Invalid account.');
($this->controller)($this->requestStub);
$message = Message::error('Invalid account.');
$this->assertTrue($this->responseRendererStub->isAjax());
$this->assertEquals(400, $this->responseRendererStub->getHttpResponseCode());
$this->assertFalse($this->responseRendererStub->hasSuccessState());
$this->assertEquals(['message' => $message->getDisplay()], $this->responseRendererStub->getJSONResult());
}
public function testWithUnsupportedServer(): void
{
$this->dbiStub->method('getVersion')->willReturn(100401);
($this->controller)($this->requestStub);
$message = Message::error('Account locking is not supported.');
$this->assertTrue($this->responseRendererStub->isAjax());
$this->assertEquals(400, $this->responseRendererStub->getHttpResponseCode());
$this->assertFalse($this->responseRendererStub->hasSuccessState());
$this->assertEquals(['message' => $message->getDisplay()], $this->responseRendererStub->getJSONResult());
}
}
|