Merge ".mailmap: Correct two contributor names"
[mediawiki.git] / tests / phpunit / includes / block / BlockErrorFormatterTest.php
bloba26c9fd8c8b26b09a8db9b921d6a13209f4b14f1
1 <?php
3 use MediaWiki\Block\BlockErrorFormatter;
4 use MediaWiki\Block\CompositeBlock;
5 use MediaWiki\Block\DatabaseBlock;
6 use MediaWiki\Block\SystemBlock;
7 use MediaWiki\Context\DerivativeContext;
8 use MediaWiki\Context\IContextSource;
9 use MediaWiki\Context\RequestContext;
10 use MediaWiki\Message\Message;
11 use Wikimedia\Rdbms\IDatabase;
12 use Wikimedia\Rdbms\LBFactory;
13 use Wikimedia\Rdbms\LoadBalancer;
15 /**
16 * @todo Can this be converted to unit tests?
18 * @group Blocking
19 * @covers \MediaWiki\Block\BlockErrorFormatter
21 class BlockErrorFormatterTest extends MediaWikiIntegrationTestCase {
23 /**
24 * @return DerivativeContext
26 private function getContext(): DerivativeContext {
27 $context = new DerivativeContext( RequestContext::getMain() );
29 $context->setLanguage(
30 $this->getServiceContainer()
31 ->getLanguageFactory()->getLanguage( 'qqx' )
34 return $context;
37 private function getBlockErrorFormatter( IContextSource $context ): BlockErrorFormatter {
38 return $this->getServiceContainer()
39 ->getFormatterFactory()->getBlockErrorFormatter( $context );
42 protected function setUp(): void {
43 parent::setUp();
45 $db = $this->createMock( IDatabase::class );
46 $db->method( 'getInfinity' )->willReturn( 'infinity' );
47 $db->method( 'decodeExpiry' )->willReturnArgument( 0 );
49 $lb = $this->createNoOpMock(
50 LoadBalancer::class,
51 [ 'getConnection' ]
53 $lb->method( 'getConnection' )->willReturn( $db );
55 $lbFactory = $this->createNoOpMock(
56 LBFactory::class,
57 [ 'getReplicaDatabase', 'getPrimaryDatabase', 'getMainLB', ]
59 $lbFactory->method( 'getReplicaDatabase' )->willReturn( $db );
60 $lbFactory->method( 'getPrimaryDatabase' )->willReturn( $db );
61 $lbFactory->method( 'getMainLB' )->willReturn( $lb );
62 $this->setService( 'DBLoadBalancerFactory', $lbFactory );
65 /**
66 * @dataProvider provideTestGetMessage
68 public function testGetMessage( $blockClass, $blockData, $expectedKey, $expectedParams ) {
69 $block = $this->makeBlock(
70 $blockClass,
71 $blockData
73 $context = $this->getContext();
75 $formatter = $this->getBlockErrorFormatter( $context );
76 $message = $formatter->getMessage(
77 $block,
78 $context->getUser(),
79 $context->getLanguage(),
80 '1.2.3.4'
83 $this->assertSame( $expectedKey, $message->getKey() );
84 $this->assertSame( $expectedParams, $message->getParams() );
87 public static function provideTestGetMessage() {
88 $timestamp = '20000101000000';
89 $expiry = '20010101000000';
91 $databaseBlock = [
92 'timestamp' => $timestamp,
93 'expiry' => $expiry,
94 'reason' => 'Test reason.',
97 $systemBlock = [
98 'timestamp' => $timestamp,
99 'systemBlock' => 'test',
100 'reason' => new Message( 'proxyblockreason' ),
103 $compositeBlock = [
104 'timestamp' => $timestamp,
105 'originalBlocks' => [
106 [ DatabaseBlock::class, $databaseBlock ],
107 [ SystemBlock::class, $systemBlock ]
111 return [
112 'Database block' => [
113 DatabaseBlock::class,
114 $databaseBlock,
115 'blockedtext',
118 'Test reason.',
119 '1.2.3.4',
121 null, // Block not inserted
122 '00:00, 1 (january) 2001',
124 '00:00, 1 (january) 2000',
127 'Database block (autoblock)' => [
128 DatabaseBlock::class,
130 'timestamp' => $timestamp,
131 'expiry' => $expiry,
132 'auto' => true,
134 'autoblockedtext',
137 '(blockednoreason)',
138 '1.2.3.4',
140 null, // Block not inserted
141 '00:00, 1 (january) 2001',
143 '00:00, 1 (january) 2000',
146 'Database block (partial block)' => [
147 DatabaseBlock::class,
149 'timestamp' => $timestamp,
150 'expiry' => $expiry,
151 'sitewide' => false,
153 'blockedtext-partial',
156 '(blockednoreason)',
157 '1.2.3.4',
159 null, // Block not inserted
160 '00:00, 1 (january) 2001',
162 '00:00, 1 (january) 2000',
165 'System block (type \'test\')' => [
166 SystemBlock::class,
167 $systemBlock,
168 'systemblockedtext',
171 '(proxyblockreason)',
172 '1.2.3.4',
174 'test',
175 '(infiniteblock)',
177 '00:00, 1 (january) 2000',
180 'System block (type \'test\') with reason parameters' => [
181 SystemBlock::class,
183 'timestamp' => $timestamp,
184 'systemBlock' => 'test',
185 'reason' => new Message( 'softblockrangesreason', [ '1.2.3.4' ] ),
187 'systemblockedtext',
190 '(softblockrangesreason: 1.2.3.4)',
191 '1.2.3.4',
193 'test',
194 '(infiniteblock)',
196 '00:00, 1 (january) 2000',
199 'Composite block (original blocks not inserted)' => [
200 CompositeBlock::class,
201 $compositeBlock,
202 'blockedtext-composite',
205 '(blockednoreason)',
206 '1.2.3.4',
208 '(blockedtext-composite-no-ids)',
209 '(infiniteblock)',
211 '00:00, 1 (january) 2000',
218 * @dataProvider provideTestGetMessageCompositeBlocks
220 public function testGetMessageCompositeBlocks( $ids, $expected ) {
221 $block = $this->getMockBuilder( CompositeBlock::class )
222 ->onlyMethods( [ 'getIdentifier' ] )
223 ->getMock();
224 $block->method( 'getIdentifier' )
225 ->willReturn( $ids );
227 $context = RequestContext::getMain();
229 $formatter = $this->getBlockErrorFormatter( $context );
230 $this->assertContains(
231 $expected,
232 $formatter->getMessage(
233 $block,
234 $context->getUser(),
235 $context->getLanguage(),
236 $context->getRequest()->getIP()
237 )->getParams()
241 public static function provideTestGetMessageCompositeBlocks() {
242 return [
243 'All original blocks are system blocks' => [
244 [ 'test', 'test' ],
245 'Your IP address appears in multiple blocklists',
247 'One original block is a database block' => [
248 [ 100, 'test' ],
249 'Relevant block IDs: #100 (your IP address may also appear in a blocklist)',
251 'Several original blocks are database blocks' => [
252 [ 100, 101, 102 ],
253 'Relevant block IDs: #100, #101, #102 (your IP address may also appear in a blocklist)',
259 * @dataProvider provideTestGetMessages
261 public function testGetMessages( $blockClass, $blockData, $expectedKeys ) {
262 $block = $this->makeBlock(
263 $blockClass,
264 $blockData
267 $context = $this->getContext();
269 $formatter = $this->getBlockErrorFormatter( $context );
270 $messages = $formatter->getMessages(
271 $block,
272 $context->getUser(),
273 '1.2.3.4'
276 $this->assertSame( $expectedKeys, array_map( static function ( $message ) {
277 return $message->getKey();
278 }, $messages ) );
281 public static function provideTestGetMessages() {
282 $timestamp = '20000101000000';
283 $expiry = '20010101000000';
285 $databaseBlock = [
286 'timestamp' => $timestamp,
287 'expiry' => $expiry,
288 'reason' => 'Test reason.',
291 $systemBlock = [
292 'timestamp' => $timestamp,
293 'systemBlock' => 'test',
294 'reason' => new Message( 'proxyblockreason' ),
297 $compositeBlock = [
298 'timestamp' => $timestamp,
299 'originalBlocks' => [
300 [ DatabaseBlock::class, $databaseBlock ],
301 [ SystemBlock::class, $systemBlock ]
305 return [
306 'Database block' => [
307 DatabaseBlock::class,
308 $databaseBlock,
309 [ 'blockedtext' ],
312 'System block (type \'test\')' => [
313 SystemBlock::class,
314 $systemBlock,
315 [ 'systemblockedtext' ],
317 'Composite block (original blocks not inserted)' => [
318 CompositeBlock::class,
319 $compositeBlock,
320 [ 'blockedtext', 'systemblockedtext' ],
326 * @param string $blockClass
327 * @param array $blockData
329 * @return mixed
331 private function makeBlock( $blockClass, $blockData ) {
332 foreach ( $blockData['originalBlocks'] ?? [] as $key => $originalBlock ) {
333 $blockData['originalBlocks'][$key] = $this->makeBlock( ...$originalBlock );
336 return new $blockClass( $blockData );