wiki.techinc.nl/tests/phpunit/includes/block/BlockErrorFormatterTest.php
Thalia 9335363789
Store block reasons as CommentStoreComments in block classes
AbstractBlock::setReason now accepts a string, Message or
CommentStoreComment. The CommentStoreComment is accessed via
AbstractBlock::getReasonComment.

AbstractBlock::getReason returns the reason as a string, with
the language and format consistent with how block reasons were
built before this commit. This method is deprecated, since it
makes assumptions about the language and format needed. The
deprecated mReason property is no longer public.

Doing this (and T227005) will remove the implicit dependency of
BlockManager::getUserBlock on language, which causes a recursion
error if the block is checked before the user has loaded. It also
provides a mechanism for getting the block reason in a language
specified by the caller. (This does not apply to DatabaseBlock
reasons entered via the Special:Block form, which were not and
are still not translatable.)

This commit also updates authentication classes to return the
translated reason.

Bug: T227007
Change-Id: Iec36876e930dff96a256aebbdc39cbfb331c244e
2019-10-18 17:47:56 -04:00

208 lines
4.8 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 MediaWikiTestCase {
/**
* @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(),
Language::factory( 'qqx' ),
$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,
'reason' => 'Test reason.',
] );
$systemBlock = new SystemBlock( [
'timestamp' => $timestamp,
'systemBlock' => 'test',
'reason' => new Message( 'proxyblockreason' ),
] );
$compositeBlock = new CompositeBlock( [
'timestamp' => $timestamp,
'originalBlocks' => [
$databaseBlock,
$systemBlock
]
] );
return [
'Database block' => [
$databaseBlock,
'blockedtext',
[
'',
'Test reason.',
'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',
[
'',
'(blockednoreason)',
'1.2.3.4',
'',
null, // Block not inserted
'00:00, 1 (january) 2001',
'',
'00:00, 1 (january) 2000',
],
],
'Database block (partial block)' => [
new DatabaseBlock( [
'timestamp' => $timestamp,
'expiry' => $expiry,
'sitewide' => false,
] ),
'blockedtext-partial',
[
'',
'(blockednoreason)',
'1.2.3.4',
'',
null, // Block not inserted
'00:00, 1 (january) 2001',
'',
'00:00, 1 (january) 2000',
],
],
'System block (type \'test\')' => [
$systemBlock,
'systemblockedtext',
[
'',
'(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)',
'1.2.3.4',
'',
'test',
'(infiniteblock)',
'',
'00:00, 1 (january) 2000',
],
],
'Composite block (original blocks not inserted)' => [
$compositeBlock,
'blockedtext-composite',
[
'',
'(blockednoreason)',
'1.2.3.4',
'',
'(blockedtext-composite-no-ids)',
'(infiniteblock)',
'',
'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)',
],
];
}
}