2019-09-20 15:03:48 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
use MediaWiki\Block\CompositeBlock;
|
|
|
|
|
use MediaWiki\Block\DatabaseBlock;
|
|
|
|
|
use MediaWiki\Block\SystemBlock;
|
2022-10-28 10:04:25 +00:00
|
|
|
use MediaWiki\Request\FauxRequest;
|
2019-09-20 15:03:48 +00:00
|
|
|
|
|
|
|
|
/**
|
2020-10-12 20:41:01 +00:00
|
|
|
* @todo Can this be converted to unit tests?
|
|
|
|
|
*
|
2019-09-20 15:03:48 +00:00
|
|
|
* @group Blocking
|
|
|
|
|
* @coversDefaultClass \MediaWiki\Block\BlockErrorFormatter
|
|
|
|
|
*/
|
2020-06-30 15:09:24 +00:00
|
|
|
class BlockErrorFormatterTest extends MediaWikiIntegrationTestCase {
|
2019-09-20 15:03:48 +00:00
|
|
|
/**
|
|
|
|
|
* @dataProvider provideTestGetMessage
|
|
|
|
|
* @covers ::getMessage
|
|
|
|
|
* @covers ::getBlockErrorMessageParams
|
|
|
|
|
* @covers ::getBlockErrorInfo
|
|
|
|
|
* @covers ::getFormattedBlockErrorInfo
|
|
|
|
|
* @covers ::getBlockErrorMessageKey
|
|
|
|
|
*/
|
|
|
|
|
public function testGetMessage( $block, $expectedKey, $expectedParams ) {
|
|
|
|
|
$context = new DerivativeContext( RequestContext::getMain() );
|
|
|
|
|
$request = $this->getMockBuilder( FauxRequest::class )
|
2021-03-20 15:18:58 +00:00
|
|
|
->onlyMethods( [ 'getIP' ] )
|
2019-09-20 15:03:48 +00:00
|
|
|
->getMock();
|
|
|
|
|
$request->method( 'getIP' )
|
|
|
|
|
->willReturn( '1.2.3.4' );
|
|
|
|
|
$context->setRequest( $request );
|
|
|
|
|
|
2022-01-12 20:13:39 +00:00
|
|
|
$formatter = $this->getServiceContainer()->getBlockErrorFormatter();
|
2019-09-20 15:03:48 +00:00
|
|
|
$message = $formatter->getMessage(
|
|
|
|
|
$block,
|
|
|
|
|
$context->getUser(),
|
2022-01-12 20:13:39 +00:00
|
|
|
$this->getServiceContainer()->getLanguageFactory()->getLanguage( 'qqx' ),
|
2019-09-20 15:03:48 +00:00
|
|
|
$context->getRequest()->getIP()
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->assertSame( $expectedKey, $message->getKey() );
|
|
|
|
|
$this->assertSame( $expectedParams, $message->getParams() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function provideTestGetMessage() {
|
|
|
|
|
$timestamp = '20000101000000';
|
|
|
|
|
$expiry = '20010101000000';
|
|
|
|
|
|
|
|
|
|
$databaseBlock = new DatabaseBlock( [
|
|
|
|
|
'timestamp' => $timestamp,
|
|
|
|
|
'expiry' => $expiry,
|
2019-10-20 00:04:00 +00:00
|
|
|
'reason' => 'Test reason.',
|
2019-09-20 15:03:48 +00:00
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$systemBlock = new SystemBlock( [
|
|
|
|
|
'timestamp' => $timestamp,
|
|
|
|
|
'systemBlock' => 'test',
|
2019-10-20 00:04:00 +00:00
|
|
|
'reason' => new Message( 'proxyblockreason' ),
|
2019-09-20 15:03:48 +00:00
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$compositeBlock = new CompositeBlock( [
|
|
|
|
|
'timestamp' => $timestamp,
|
|
|
|
|
'originalBlocks' => [
|
|
|
|
|
$databaseBlock,
|
|
|
|
|
$systemBlock
|
|
|
|
|
]
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
'Database block' => [
|
|
|
|
|
$databaseBlock,
|
|
|
|
|
'blockedtext',
|
|
|
|
|
[
|
|
|
|
|
'',
|
2019-10-20 00:04:00 +00:00
|
|
|
'Test reason.',
|
2019-09-20 15:03:48 +00:00
|
|
|
'1.2.3.4',
|
|
|
|
|
'',
|
|
|
|
|
null, // Block not inserted
|
2019-10-20 00:04:00 +00:00
|
|
|
'00:00, 1 (january) 2001',
|
2019-09-20 15:03:48 +00:00
|
|
|
'',
|
2019-10-20 00:04:00 +00:00
|
|
|
'00:00, 1 (january) 2000',
|
2019-09-20 15:03:48 +00:00
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
'Database block (autoblock)' => [
|
|
|
|
|
new DatabaseBlock( [
|
|
|
|
|
'timestamp' => $timestamp,
|
|
|
|
|
'expiry' => $expiry,
|
|
|
|
|
'auto' => true,
|
|
|
|
|
] ),
|
|
|
|
|
'autoblockedtext',
|
|
|
|
|
[
|
|
|
|
|
'',
|
2019-10-20 00:04:00 +00:00
|
|
|
'(blockednoreason)',
|
2019-09-20 15:03:48 +00:00
|
|
|
'1.2.3.4',
|
|
|
|
|
'',
|
2019-10-20 00:04:00 +00:00
|
|
|
null, // Block not inserted
|
|
|
|
|
'00:00, 1 (january) 2001',
|
2019-09-20 15:03:48 +00:00
|
|
|
'',
|
2019-10-20 00:04:00 +00:00
|
|
|
'00:00, 1 (january) 2000',
|
2019-09-20 15:03:48 +00:00
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
'Database block (partial block)' => [
|
|
|
|
|
new DatabaseBlock( [
|
|
|
|
|
'timestamp' => $timestamp,
|
|
|
|
|
'expiry' => $expiry,
|
|
|
|
|
'sitewide' => false,
|
|
|
|
|
] ),
|
|
|
|
|
'blockedtext-partial',
|
|
|
|
|
[
|
|
|
|
|
'',
|
2019-10-20 00:04:00 +00:00
|
|
|
'(blockednoreason)',
|
2019-09-20 15:03:48 +00:00
|
|
|
'1.2.3.4',
|
|
|
|
|
'',
|
2019-10-20 00:04:00 +00:00
|
|
|
null, // Block not inserted
|
|
|
|
|
'00:00, 1 (january) 2001',
|
2019-09-20 15:03:48 +00:00
|
|
|
'',
|
2019-10-20 00:04:00 +00:00
|
|
|
'00:00, 1 (january) 2000',
|
2019-09-20 15:03:48 +00:00
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
'System block (type \'test\')' => [
|
|
|
|
|
$systemBlock,
|
|
|
|
|
'systemblockedtext',
|
|
|
|
|
[
|
|
|
|
|
'',
|
2019-10-20 00:04:00 +00:00
|
|
|
'(proxyblockreason)',
|
|
|
|
|
'1.2.3.4',
|
|
|
|
|
'',
|
|
|
|
|
'test',
|
|
|
|
|
'(infiniteblock)',
|
|
|
|
|
'',
|
|
|
|
|
'00:00, 1 (january) 2000',
|
|
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
'System block (type \'test\') with reason parameters' => [
|
|
|
|
|
new SystemBlock( [
|
|
|
|
|
'timestamp' => $timestamp,
|
|
|
|
|
'systemBlock' => 'test',
|
|
|
|
|
'reason' => new Message( 'softblockrangesreason', [ '1.2.3.4' ] ),
|
|
|
|
|
] ),
|
|
|
|
|
'systemblockedtext',
|
|
|
|
|
[
|
|
|
|
|
'',
|
|
|
|
|
'(softblockrangesreason: 1.2.3.4)',
|
2019-09-20 15:03:48 +00:00
|
|
|
'1.2.3.4',
|
|
|
|
|
'',
|
|
|
|
|
'test',
|
2019-10-20 00:04:00 +00:00
|
|
|
'(infiniteblock)',
|
2019-09-20 15:03:48 +00:00
|
|
|
'',
|
2019-10-20 00:04:00 +00:00
|
|
|
'00:00, 1 (january) 2000',
|
2019-09-20 15:03:48 +00:00
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
'Composite block (original blocks not inserted)' => [
|
|
|
|
|
$compositeBlock,
|
|
|
|
|
'blockedtext-composite',
|
|
|
|
|
[
|
|
|
|
|
'',
|
2019-10-20 00:04:00 +00:00
|
|
|
'(blockednoreason)',
|
2019-09-20 15:03:48 +00:00
|
|
|
'1.2.3.4',
|
|
|
|
|
'',
|
2019-10-20 00:04:00 +00:00
|
|
|
'(blockedtext-composite-no-ids)',
|
|
|
|
|
'(infiniteblock)',
|
2019-09-20 15:03:48 +00:00
|
|
|
'',
|
2019-10-20 00:04:00 +00:00
|
|
|
'00:00, 1 (january) 2000',
|
2019-09-20 15:03:48 +00:00
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideTestGetMessageCompositeBlocks
|
|
|
|
|
* @covers ::getMessage
|
|
|
|
|
* @covers ::getBlockErrorMessageParams
|
|
|
|
|
*/
|
|
|
|
|
public function testGetMessageCompositeBlocks( $ids, $expected ) {
|
|
|
|
|
$block = $this->getMockBuilder( CompositeBlock::class )
|
2021-03-20 15:18:58 +00:00
|
|
|
->onlyMethods( [ 'getIdentifier' ] )
|
2019-09-20 15:03:48 +00:00
|
|
|
->getMock();
|
|
|
|
|
$block->method( 'getIdentifier' )
|
|
|
|
|
->willReturn( $ids );
|
|
|
|
|
|
|
|
|
|
$context = RequestContext::getMain();
|
|
|
|
|
|
2022-01-12 20:13:39 +00:00
|
|
|
$formatter = $this->getServiceContainer()->getBlockErrorFormatter();
|
2019-09-20 15:03:48 +00:00
|
|
|
$this->assertContains(
|
|
|
|
|
$expected,
|
|
|
|
|
$formatter->getMessage(
|
|
|
|
|
$block,
|
|
|
|
|
$context->getUser(),
|
|
|
|
|
$context->getLanguage(),
|
|
|
|
|
$context->getRequest()->getIP()
|
|
|
|
|
)->getParams()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function provideTestGetMessageCompositeBlocks() {
|
|
|
|
|
return [
|
|
|
|
|
'All original blocks are system blocks' => [
|
|
|
|
|
[ 'test', 'test' ],
|
2020-10-02 15:25:31 +00:00
|
|
|
'Your IP address appears in multiple blocklists',
|
2019-09-20 15:03:48 +00:00
|
|
|
],
|
|
|
|
|
'One original block is a database block' => [
|
|
|
|
|
[ 100, 'test' ],
|
2020-10-02 15:25:31 +00:00
|
|
|
'Relevant block IDs: #100 (your IP address may also appear in a blocklist)',
|
2019-09-20 15:03:48 +00:00
|
|
|
],
|
|
|
|
|
'Several original blocks are database blocks' => [
|
|
|
|
|
[ 100, 101, 102 ],
|
2020-10-02 15:25:31 +00:00
|
|
|
'Relevant block IDs: #100, #101, #102 (your IP address may also appear in a blocklist)',
|
2019-09-20 15:03:48 +00:00
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
}
|