wiki.techinc.nl/tests/phpunit/includes/block/BlockErrorFormatterTest.php
Thalia df20197250 Introduce a formatter service for block errors
The main reasons for adding this service layer are:
* It allows error messages to be more consistent, by defining
  a set of reportable information that can describe any block
  type and is consistently formatted.
* It decouples formatting from the block classes, removing
  their dependency on language, for the most part.

The service provides one public method, getMessage, which
returns a Message object whose key and parameters are
determined by the type of block. This should be used instead
of the deprecated AbstractBlock::getPermissionsError and
AbstractBlock::getBlockErrorParams.

Calls to AbstractBlock::getPermissionsError are replaced in
this patch.

Bug: T227174
Change-Id: I8caae7e30a46ef7120a86a4e5e6f30ae00855063
2019-10-08 12:29:23 +01:00

188 lines
4.3 KiB
PHP

<?php
use MediaWiki\Block\CompositeBlock;
use MediaWiki\Block\DatabaseBlock;
use MediaWiki\Block\SystemBlock;
use MediaWiki\MediaWikiServices;
/**
* @group Blocking
* @coversDefaultClass \MediaWiki\Block\BlockErrorFormatter
*/
class BlockErrorFormatterTest extends MediaWikiLangTestCase {
/**
* @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 )
->setMethods( [ 'getIP' ] )
->getMock();
$request->method( 'getIP' )
->willReturn( '1.2.3.4' );
$context->setRequest( $request );
$formatter = MediaWikiServices::getInstance()->getBlockErrorFormatter();
$message = $formatter->getMessage(
$block,
$context->getUser(),
$context->getLanguage(),
$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,
] );
$systemBlock = new SystemBlock( [
'timestamp' => $timestamp,
'systemBlock' => 'test',
] );
$compositeBlock = new CompositeBlock( [
'timestamp' => $timestamp,
'originalBlocks' => [
$databaseBlock,
$systemBlock
]
] );
return [
'Database block' => [
$databaseBlock,
'blockedtext',
[
'',
'no reason given',
'1.2.3.4',
'',
null, // Block not inserted
'00:00, 1 January 2001',
'',
'00:00, 1 January 2000',
],
],
'Database block (autoblock)' => [
new DatabaseBlock( [
'timestamp' => $timestamp,
'expiry' => $expiry,
'auto' => true,
] ),
'autoblockedtext',
[
'',
'no reason given',
'1.2.3.4',
'',
null,
'00:00, 1 January 2001',
'',
'00:00, 1 January 2000',
],
],
'Database block (partial block)' => [
new DatabaseBlock( [
'timestamp' => $timestamp,
'expiry' => $expiry,
'sitewide' => false,
] ),
'blockedtext-partial',
[
'',
'no reason given',
'1.2.3.4',
'',
null,
'00:00, 1 January 2001',
'',
'00:00, 1 January 2000',
],
],
'System block (type \'test\')' => [
$systemBlock,
'systemblockedtext',
[
'',
'no reason given',
'1.2.3.4',
'',
'test',
'infinite',
'',
'00:00, 1 January 2000',
],
],
'Composite block (original blocks not inserted)' => [
$compositeBlock,
'blockedtext-composite',
[
'',
'no reason given',
'1.2.3.4',
'',
'Your IP address appears in multiple blacklists',
'infinite',
'',
'00:00, 1 January 2000',
],
],
];
}
/**
* @dataProvider provideTestGetMessageCompositeBlocks
* @covers ::getMessage
* @covers ::getBlockErrorMessageParams
*/
public function testGetMessageCompositeBlocks( $ids, $expected ) {
$block = $this->getMockBuilder( CompositeBlock::class )
->setMethods( [ 'getIdentifier', 'getBlockErrorParams' ] )
->getMock();
$block->method( 'getIdentifier' )
->willReturn( $ids );
$context = RequestContext::getMain();
$formatter = MediaWikiServices::getInstance()->getBlockErrorFormatter();
$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' ],
'Your IP address appears in multiple blacklists',
],
'One original block is a database block' => [
[ 100, 'test' ],
'Relevant block IDs: #100 (your IP address may also be blacklisted)',
],
'Several original blocks are database blocks' => [
[ 100, 101, 102 ],
'Relevant block IDs: #100, #101, #102 (your IP address may also be blacklisted)',
],
];
}
}