aboutsummaryrefslogtreecommitdiffstats
path: root/tests/phpunit/includes/exception/UserBlockedErrorTest.php
blob: d388dd32aea8418e5ec483c18924a4cc35adab0e (plain) (blame)
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
<?php

use MediaWiki\Block\AbstractBlock;
use MediaWiki\Block\BlockErrorFormatter;
use MediaWiki\Context\RequestContext;
use MediaWiki\Exception\UserBlockedError;
use MediaWiki\Language\FormatterFactory;
use MediaWiki\Language\Language;
use MediaWiki\Language\RawMessage;
use MediaWiki\User\UserIdentity;

/**
 * @covers \MediaWiki\Exception\UserBlockedError
 */
class UserBlockedErrorTest extends MediaWikiIntegrationTestCase {

	private function setUpMockBlockFormatter(
		$expectedBlock, $expectedUser, $expectedLanguage, $expectedIp, $returnMessages
	) {
		$mockBlockErrorFormatter = $this->createMock( BlockErrorFormatter::class );
		$mockBlockErrorFormatter->expects( $this->once() )
			->method( 'getMessages' )
			->with( $expectedBlock, $expectedUser, $expectedIp )
			->willReturn( $returnMessages );

		$formatterFactory = $this->createNoOpMock( FormatterFactory::class, [ 'getBlockErrorFormatter' ] );
		$formatterFactory->method( 'getBlockErrorFormatter' )->willReturn( $mockBlockErrorFormatter );

		$this->setService( 'FormatterFactory', $formatterFactory );
	}

	public function testConstructionProvidedOnlyBlockParameter() {
		$context = RequestContext::getMain();
		$block = $this->createMock( AbstractBlock::class );
		$this->setUpMockBlockFormatter(
			$block, $context->getUser(), $context->getLanguage(), $context->getRequest()->getIP(),
			[ new RawMessage( 'testing' ) ]
		);
		$e = new UserBlockedError( $block );
		$this->assertSame(
			( new RawMessage( 'testing' ) )->text(),
			$e->getMessageObject()->text(),
			'UserBlockedError did not use the expected message.'
		);
	}

	public function testConstructionProvidedAllParametersWithMultipleBlockMessages() {
		$mockUser = $this->createMock( UserIdentity::class );
		$mockLanguage = $this->createMock( Language::class );
		$block = $this->createMock( AbstractBlock::class );
		$this->setUpMockBlockFormatter(
			$block, $mockUser, $mockLanguage, '1.2.3.4',
			[ new RawMessage( 'testing' ), new RawMessage( 'testing2' ) ]
		);
		$e = new UserBlockedError( $block, $mockUser, $mockLanguage, '1.2.3.4' );
		$this->assertSame(
			"* testing\n* testing2",
			$e->getMessageObject()->text(),
			'UserBlockedError did not use the expected message.'
		);
	}
}