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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
<?php
declare(strict_types=1);
namespace PhpMyAdmin\Tests\Server\Privileges;
use PhpMyAdmin\DatabaseInterface;
use PhpMyAdmin\Server\Privileges\AccountLocking;
use PHPUnit\Framework\TestCase;
use Throwable;
/**
* @covers \PhpMyAdmin\Server\Privileges\AccountLocking
*/
#[\PHPUnit\Framework\Attributes\CoversClass(\PhpMyAdmin\Server\Privileges\AccountLocking::class)]
class AccountLockingTest extends TestCase
{
/**
* @requires PHPUnit < 10
*/
#[\PHPUnit\Framework\Attributes\RequiresPhpunit('< 10')]
public function testLockWithValidAccount(): void
{
$dbi = $this->createMock(DatabaseInterface::class);
$dbi->expects($this->once())->method('isMariaDB')->willReturn(true);
$dbi->expects($this->once())->method('getVersion')->willReturn(100402);
$dbi->expects($this->exactly(2))
->method('escapeString')
->willReturnOnConsecutiveCalls('test.user', 'test.host');
$dbi->expects($this->once())
->method('tryQuery')
->with($this->equalTo('ALTER USER \'test.user\'@\'test.host\' ACCOUNT LOCK;'))
->willReturn(true);
$dbi->expects($this->never())->method('getError');
$accountLocking = new AccountLocking($dbi);
$accountLocking->lock('test.user', 'test.host');
}
/**
* @requires PHPUnit < 10
*/
#[\PHPUnit\Framework\Attributes\RequiresPhpunit('< 10')]
public function testLockWithInvalidAccount(): void
{
$dbi = $this->createMock(DatabaseInterface::class);
$dbi->expects($this->once())->method('isMariaDB')->willReturn(true);
$dbi->expects($this->once())->method('getVersion')->willReturn(100402);
$dbi->expects($this->exactly(2))
->method('escapeString')
->willReturnOnConsecutiveCalls('test.user', 'test.host');
$dbi->expects($this->once())
->method('tryQuery')
->with($this->equalTo('ALTER USER \'test.user\'@\'test.host\' ACCOUNT LOCK;'))
->willReturn(false);
$dbi->expects($this->once())->method('getError')->willReturn('Invalid account.');
$accountLocking = new AccountLocking($dbi);
$this->expectException(Throwable::class);
$this->expectExceptionMessage('Invalid account.');
$accountLocking->lock('test.user', 'test.host');
}
public function testLockWithUnsupportedServer(): void
{
$dbi = $this->createMock(DatabaseInterface::class);
$dbi->expects($this->once())->method('isMariaDB')->willReturn(true);
$dbi->expects($this->once())->method('getVersion')->willReturn(100401);
$dbi->expects($this->never())->method('escapeString');
$dbi->expects($this->never())->method('tryQuery');
$dbi->expects($this->never())->method('getError');
$accountLocking = new AccountLocking($dbi);
$this->expectException(Throwable::class);
$this->expectExceptionMessage('Account locking is not supported.');
$accountLocking->lock('test.user', 'test.host');
}
/**
* @requires PHPUnit < 10
*/
#[\PHPUnit\Framework\Attributes\RequiresPhpunit('< 10')]
public function testUnlockWithValidAccount(): void
{
$dbi = $this->createMock(DatabaseInterface::class);
$dbi->expects($this->once())->method('isMariaDB')->willReturn(true);
$dbi->expects($this->once())->method('getVersion')->willReturn(100402);
$dbi->expects($this->exactly(2))
->method('escapeString')
->willReturnOnConsecutiveCalls('test.user', 'test.host');
$dbi->expects($this->once())
->method('tryQuery')
->with($this->equalTo('ALTER USER \'test.user\'@\'test.host\' ACCOUNT UNLOCK;'))
->willReturn(true);
$dbi->expects($this->never())->method('getError');
$accountLocking = new AccountLocking($dbi);
$accountLocking->unlock('test.user', 'test.host');
}
/**
* @requires PHPUnit < 10
*/
#[\PHPUnit\Framework\Attributes\RequiresPhpunit('< 10')]
public function testUnlockWithInvalidAccount(): void
{
$dbi = $this->createMock(DatabaseInterface::class);
$dbi->expects($this->once())->method('isMariaDB')->willReturn(true);
$dbi->expects($this->once())->method('getVersion')->willReturn(100402);
$dbi->expects($this->exactly(2))
->method('escapeString')
->willReturnOnConsecutiveCalls('test.user', 'test.host');
$dbi->expects($this->once())
->method('tryQuery')
->with($this->equalTo('ALTER USER \'test.user\'@\'test.host\' ACCOUNT UNLOCK;'))
->willReturn(false);
$dbi->expects($this->once())->method('getError')->willReturn('Invalid account.');
$accountLocking = new AccountLocking($dbi);
$this->expectException(Throwable::class);
$this->expectExceptionMessage('Invalid account.');
$accountLocking->unlock('test.user', 'test.host');
}
public function testUnlockWithUnsupportedServer(): void
{
$dbi = $this->createMock(DatabaseInterface::class);
$dbi->expects($this->once())->method('isMariaDB')->willReturn(false);
$dbi->expects($this->once())->method('getVersion')->willReturn(50705);
$dbi->expects($this->never())->method('escapeString');
$dbi->expects($this->never())->method('tryQuery');
$dbi->expects($this->never())->method('getError');
$accountLocking = new AccountLocking($dbi);
$this->expectException(Throwable::class);
$this->expectExceptionMessage('Account locking is not supported.');
$accountLocking->unlock('test.user', 'test.host');
}
}
|