| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Block\AbstractBlock; |
| 4 |
use MediaWiki\Block\BlockErrorFormatter; |
| 5 |
use MediaWiki\Context\RequestContext; |
| 6 |
use MediaWiki\Language\FormatterFactory; |
| 7 |
use MediaWiki\Language\Language; |
| 8 |
use MediaWiki\Language\RawMessage; |
| 9 |
use MediaWiki\User\UserIdentity; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
class UserBlockedErrorTest extends MediaWikiIntegrationTestCase { |
| 15 |
|
| 16 |
private function setUpMockBlockFormatter( |
| 17 |
$expectedBlock, $expectedUser, $expectedLanguage, $expectedIp, $returnMessages |
| 18 |
) { |
| 19 |
$mockBlockErrorFormatter = $this->createMock( BlockErrorFormatter::class ); |
| 20 |
$mockBlockErrorFormatter->expects( $this->once() ) |
| 21 |
->method( 'getMessages' ) |
| 22 |
->with( $expectedBlock, $expectedUser, $expectedIp ) |
| 23 |
->willReturn( $returnMessages ); |
| 24 |
|
| 25 |
$formatterFactory = $this->createNoOpMock( FormatterFactory::class, [ 'getBlockErrorFormatter' ] ); |
| 26 |
$formatterFactory->method( 'getBlockErrorFormatter' )->willReturn( $mockBlockErrorFormatter ); |
| 27 |
|
| 28 |
$this->setService( 'FormatterFactory', $formatterFactory ); |
| 29 |
} |
| 30 |
|
| 31 |
public function testConstructionProvidedOnlyBlockParameter() { |
| 32 |
$context = RequestContext::getMain(); |
| 33 |
$block = $this->createMock( AbstractBlock::class ); |
| 34 |
$this->setUpMockBlockFormatter( |
| 35 |
$block, $context->getUser(), $context->getLanguage(), $context->getRequest()->getIP(), |
| 36 |
[ new RawMessage( 'testing' ) ] |
| 37 |
); |
| 38 |
$e = new UserBlockedError( $block ); |
| 39 |
$this->assertSame( |
| 40 |
( new RawMessage( 'testing' ) )->text(), |
| 41 |
$e->getMessageObject()->text(), |
| 42 |
'UserBlockedError did not use the expected message.' |
| 43 |
); |
| 44 |
} |
| 45 |
|
| 46 |
public function testConstructionProvidedAllParametersWithMultipleBlockMessages() { |
| 47 |
$mockUser = $this->createMock( UserIdentity::class ); |
| 48 |
$mockLanguage = $this->createMock( Language::class ); |
| 49 |
$block = $this->createMock( AbstractBlock::class ); |
| 50 |
$this->setUpMockBlockFormatter( |
| 51 |
$block, $mockUser, $mockLanguage, '1.2.3.4', |
| 52 |
[ new RawMessage( 'testing' ), new RawMessage( 'testing2' ) ] |
| 53 |
); |
| 54 |
$e = new UserBlockedError( $block, $mockUser, $mockLanguage, '1.2.3.4' ); |
| 55 |
$this->assertSame( |
| 56 |
"* testing\n* testing2", |
| 57 |
$e->getMessageObject()->text(), |
| 58 |
'UserBlockedError did not use the expected message.' |
| 59 |
); |
| 60 |
} |
| 61 |
} |