Merge "Fix Block::newLoad for IPv6 range blocks"
[lhc/web/wiklou.git] / tests / phpunit / includes / BlockTest.php
1 <?php
2
3 use MediaWiki\Block\BlockRestrictionStore;
4 use MediaWiki\Block\Restriction\PageRestriction;
5 use MediaWiki\Block\Restriction\NamespaceRestriction;
6 use MediaWiki\MediaWikiServices;
7
8 /**
9 * @group Database
10 * @group Blocking
11 */
12 class BlockTest extends MediaWikiLangTestCase {
13
14 /**
15 * @return User
16 */
17 private function getUserForBlocking() {
18 $testUser = $this->getMutableTestUser();
19 $user = $testUser->getUser();
20 $user->addToDatabase();
21 TestUser::setPasswordForUser( $user, 'UTBlockeePassword' );
22 $user->saveSettings();
23 return $user;
24 }
25
26 /**
27 * @param User $user
28 *
29 * @return Block
30 * @throws MWException
31 */
32 private function addBlockForUser( User $user ) {
33 // Delete the last round's block if it's still there
34 $oldBlock = Block::newFromTarget( $user->getName() );
35 if ( $oldBlock ) {
36 // An old block will prevent our new one from saving.
37 $oldBlock->delete();
38 }
39
40 $blockOptions = [
41 'address' => $user->getName(),
42 'user' => $user->getId(),
43 'by' => $this->getTestSysop()->getUser()->getId(),
44 'reason' => 'Parce que',
45 'expiry' => time() + 100500,
46 ];
47 $block = new Block( $blockOptions );
48
49 $block->insert();
50 // save up ID for use in assertion. Since ID is an autoincrement,
51 // its value might change depending on the order the tests are run.
52 // ApiBlockTest insert its own blocks!
53 if ( !$block->getId() ) {
54 throw new MWException( "Failed to insert block for BlockTest; old leftover block remaining?" );
55 }
56
57 $this->addXffBlocks();
58
59 return $block;
60 }
61
62 /**
63 * @covers Block::newFromTarget
64 */
65 public function testINewFromTargetReturnsCorrectBlock() {
66 $user = $this->getUserForBlocking();
67 $block = $this->addBlockForUser( $user );
68
69 $this->assertTrue(
70 $block->equals( Block::newFromTarget( $user->getName() ) ),
71 "newFromTarget() returns the same block as the one that was made"
72 );
73 }
74
75 /**
76 * @covers Block::newFromID
77 */
78 public function testINewFromIDReturnsCorrectBlock() {
79 $user = $this->getUserForBlocking();
80 $block = $this->addBlockForUser( $user );
81
82 $this->assertTrue(
83 $block->equals( Block::newFromID( $block->getId() ) ),
84 "newFromID() returns the same block as the one that was made"
85 );
86 }
87
88 /**
89 * per T28425
90 * @covers Block::__construct
91 */
92 public function testT28425BlockTimestampDefaultsToTime() {
93 $user = $this->getUserForBlocking();
94 $block = $this->addBlockForUser( $user );
95 $madeAt = wfTimestamp( TS_MW );
96
97 // delta to stop one-off errors when things happen to go over a second mark.
98 $delta = abs( $madeAt - $block->getTimestamp() );
99 $this->assertLessThan(
100 2,
101 $delta,
102 "If no timestamp is specified, the block is recorded as time()"
103 );
104 }
105
106 /**
107 * CheckUser since being changed to use Block::newFromTarget started failing
108 * because the new function didn't accept empty strings like Block::load()
109 * had. Regression T31116.
110 *
111 * @dataProvider provideT31116Data
112 * @covers Block::newFromTarget
113 */
114 public function testT31116NewFromTargetWithEmptyIp( $vagueTarget ) {
115 $user = $this->getUserForBlocking();
116 $initialBlock = $this->addBlockForUser( $user );
117 $block = Block::newFromTarget( $user->getName(), $vagueTarget );
118
119 $this->assertTrue(
120 $initialBlock->equals( $block ),
121 "newFromTarget() returns the same block as the one that was made when "
122 . "given empty vagueTarget param " . var_export( $vagueTarget, true )
123 );
124 }
125
126 public static function provideT31116Data() {
127 return [
128 [ null ],
129 [ '' ],
130 [ false ]
131 ];
132 }
133
134 /**
135 * @dataProvider provideNewFromTargetRangeBlocks
136 * @covers Block::newFromTarget
137 */
138 public function testNewFromTargetRangeBlocks( $targets, $ip, $expectedTarget ) {
139 $blocker = $this->getTestSysop()->getUser();
140
141 foreach ( $targets as $target ) {
142 $block = new Block();
143 $block->setTarget( $target );
144 $block->setBlocker( $blocker );
145 $block->insert();
146 }
147
148 // Should find the block with the narrowest range
149 $blockTarget = Block::newFromTarget( $this->getTestUser()->getUser(), $ip )->getTarget();
150 $this->assertSame(
151 $blockTarget instanceof User ? $blockTarget->getName() : $blockTarget,
152 $expectedTarget
153 );
154
155 foreach ( $targets as $target ) {
156 $block = Block::newFromTarget( $target );
157 $block->delete();
158 }
159 }
160
161 function provideNewFromTargetRangeBlocks() {
162 return [
163 'Blocks to IPv4 ranges' => [
164 [ '0.0.0.0/20', '0.0.0.0/30', '0.0.0.0/25' ],
165 '0.0.0.0',
166 '0.0.0.0/30'
167 ],
168 'Blocks to IPv6 ranges' => [
169 [ '0:0:0:0:0:0:0:0/20', '0:0:0:0:0:0:0:0/30', '0:0:0:0:0:0:0:0/25' ],
170 '0:0:0:0:0:0:0:0',
171 '0:0:0:0:0:0:0:0/30'
172 ],
173 'Blocks to wide IPv4 range and IP' => [
174 [ '0.0.0.0/16', '0.0.0.0' ],
175 '0.0.0.0',
176 '0.0.0.0'
177 ],
178 'Blocks to wide IPv6 range and IP' => [
179 [ '0:0:0:0:0:0:0:0/19', '0:0:0:0:0:0:0:0' ],
180 '0:0:0:0:0:0:0:0',
181 '0:0:0:0:0:0:0:0'
182 ],
183 'Blocks to narrow IPv4 range and IP' => [
184 [ '0.0.0.0/31', '0.0.0.0' ],
185 '0.0.0.0',
186 '0.0.0.0'
187 ],
188 'Blocks to narrow IPv6 range and IP' => [
189 [ '0:0:0:0:0:0:0:0/127', '0:0:0:0:0:0:0:0' ],
190 '0:0:0:0:0:0:0:0',
191 '0:0:0:0:0:0:0:0'
192 ],
193 ];
194 }
195
196 /**
197 * @covers Block::appliesToRight
198 */
199 public function testBlockedUserCanNotCreateAccount() {
200 $username = 'BlockedUserToCreateAccountWith';
201 $u = User::newFromName( $username );
202 $u->addToDatabase();
203 $userId = $u->getId();
204 $this->assertNotEquals( 0, $userId, 'sanity' );
205 TestUser::setPasswordForUser( $u, 'NotRandomPass' );
206 unset( $u );
207
208 // Sanity check
209 $this->assertNull(
210 Block::newFromTarget( $username ),
211 "$username should not be blocked"
212 );
213
214 // Reload user
215 $u = User::newFromName( $username );
216 $this->assertFalse(
217 $u->isBlockedFromCreateAccount(),
218 "Our sandbox user should be able to create account before being blocked"
219 );
220
221 // Foreign perspective (blockee not on current wiki)...
222 $blockOptions = [
223 'address' => $username,
224 'user' => $userId,
225 'reason' => 'crosswiki block...',
226 'timestamp' => wfTimestampNow(),
227 'expiry' => $this->db->getInfinity(),
228 'createAccount' => true,
229 'enableAutoblock' => true,
230 'hideName' => true,
231 'blockEmail' => true,
232 'byText' => 'm>MetaWikiUser',
233 ];
234 $block = new Block( $blockOptions );
235 $block->insert();
236
237 // Reload block from DB
238 $userBlock = Block::newFromTarget( $username );
239 $this->assertTrue(
240 (bool)$block->appliesToRight( 'createaccount' ),
241 "Block object in DB should block right 'createaccount'"
242 );
243
244 $this->assertInstanceOf(
245 Block::class,
246 $userBlock,
247 "'$username' block block object should be existent"
248 );
249
250 // Reload user
251 $u = User::newFromName( $username );
252 $this->assertTrue(
253 (bool)$u->isBlockedFromCreateAccount(),
254 "Our sandbox user '$username' should NOT be able to create account"
255 );
256 }
257
258 /**
259 * @covers Block::insert
260 */
261 public function testCrappyCrossWikiBlocks() {
262 // Delete the last round's block if it's still there
263 $oldBlock = Block::newFromTarget( 'UserOnForeignWiki' );
264 if ( $oldBlock ) {
265 // An old block will prevent our new one from saving.
266 $oldBlock->delete();
267 }
268
269 // Local perspective (blockee on current wiki)...
270 $user = User::newFromName( 'UserOnForeignWiki' );
271 $user->addToDatabase();
272 $userId = $user->getId();
273 $this->assertNotEquals( 0, $userId, 'sanity' );
274
275 // Foreign perspective (blockee not on current wiki)...
276 $blockOptions = [
277 'address' => 'UserOnForeignWiki',
278 'user' => $user->getId(),
279 'reason' => 'crosswiki block...',
280 'timestamp' => wfTimestampNow(),
281 'expiry' => $this->db->getInfinity(),
282 'createAccount' => true,
283 'enableAutoblock' => true,
284 'hideName' => true,
285 'blockEmail' => true,
286 'byText' => 'Meta>MetaWikiUser',
287 ];
288 $block = new Block( $blockOptions );
289
290 $res = $block->insert( $this->db );
291 $this->assertTrue( (bool)$res['id'], 'Block succeeded' );
292
293 $user = null; // clear
294
295 $block = Block::newFromID( $res['id'] );
296 $this->assertEquals(
297 'UserOnForeignWiki',
298 $block->getTarget()->getName(),
299 'Correct blockee name'
300 );
301 $this->assertEquals( $userId, $block->getTarget()->getId(), 'Correct blockee id' );
302 $this->assertEquals( 'Meta>MetaWikiUser', $block->getBlocker()->getName(),
303 'Correct blocker name' );
304 $this->assertEquals( 'Meta>MetaWikiUser', $block->getByName(), 'Correct blocker name' );
305 $this->assertEquals( 0, $block->getBy(), 'Correct blocker id' );
306 }
307
308 protected function addXffBlocks() {
309 static $inited = false;
310
311 if ( $inited ) {
312 return;
313 }
314
315 $inited = true;
316
317 $blockList = [
318 [ 'target' => '70.2.0.0/16',
319 'type' => Block::TYPE_RANGE,
320 'desc' => 'Range Hardblock',
321 'ACDisable' => false,
322 'isHardblock' => true,
323 'isAutoBlocking' => false,
324 ],
325 [ 'target' => '2001:4860:4001::/48',
326 'type' => Block::TYPE_RANGE,
327 'desc' => 'Range6 Hardblock',
328 'ACDisable' => false,
329 'isHardblock' => true,
330 'isAutoBlocking' => false,
331 ],
332 [ 'target' => '60.2.0.0/16',
333 'type' => Block::TYPE_RANGE,
334 'desc' => 'Range Softblock with AC Disabled',
335 'ACDisable' => true,
336 'isHardblock' => false,
337 'isAutoBlocking' => false,
338 ],
339 [ 'target' => '50.2.0.0/16',
340 'type' => Block::TYPE_RANGE,
341 'desc' => 'Range Softblock',
342 'ACDisable' => false,
343 'isHardblock' => false,
344 'isAutoBlocking' => false,
345 ],
346 [ 'target' => '50.1.1.1',
347 'type' => Block::TYPE_IP,
348 'desc' => 'Exact Softblock',
349 'ACDisable' => false,
350 'isHardblock' => false,
351 'isAutoBlocking' => false,
352 ],
353 ];
354
355 $blocker = $this->getTestUser()->getUser();
356 foreach ( $blockList as $insBlock ) {
357 $target = $insBlock['target'];
358
359 if ( $insBlock['type'] === Block::TYPE_IP ) {
360 $target = User::newFromName( IP::sanitizeIP( $target ), false )->getName();
361 } elseif ( $insBlock['type'] === Block::TYPE_RANGE ) {
362 $target = IP::sanitizeRange( $target );
363 }
364
365 $block = new Block();
366 $block->setTarget( $target );
367 $block->setBlocker( $blocker );
368 $block->setReason( $insBlock['desc'] );
369 $block->setExpiry( 'infinity' );
370 $block->isCreateAccountBlocked( $insBlock['ACDisable'] );
371 $block->isHardblock( $insBlock['isHardblock'] );
372 $block->isAutoblocking( $insBlock['isAutoBlocking'] );
373 $block->insert();
374 }
375 }
376
377 public static function providerXff() {
378 return [
379 [ 'xff' => '1.2.3.4, 70.2.1.1, 60.2.1.1, 2.3.4.5',
380 'count' => 2,
381 'result' => 'Range Hardblock'
382 ],
383 [ 'xff' => '1.2.3.4, 50.2.1.1, 60.2.1.1, 2.3.4.5',
384 'count' => 2,
385 'result' => 'Range Softblock with AC Disabled'
386 ],
387 [ 'xff' => '1.2.3.4, 70.2.1.1, 50.1.1.1, 2.3.4.5',
388 'count' => 2,
389 'result' => 'Exact Softblock'
390 ],
391 [ 'xff' => '1.2.3.4, 70.2.1.1, 50.2.1.1, 50.1.1.1, 2.3.4.5',
392 'count' => 3,
393 'result' => 'Exact Softblock'
394 ],
395 [ 'xff' => '1.2.3.4, 70.2.1.1, 50.2.1.1, 2.3.4.5',
396 'count' => 2,
397 'result' => 'Range Hardblock'
398 ],
399 [ 'xff' => '1.2.3.4, 70.2.1.1, 60.2.1.1, 2.3.4.5',
400 'count' => 2,
401 'result' => 'Range Hardblock'
402 ],
403 [ 'xff' => '50.2.1.1, 60.2.1.1, 2.3.4.5',
404 'count' => 2,
405 'result' => 'Range Softblock with AC Disabled'
406 ],
407 [ 'xff' => '1.2.3.4, 50.1.1.1, 60.2.1.1, 2.3.4.5',
408 'count' => 2,
409 'result' => 'Exact Softblock'
410 ],
411 [ 'xff' => '1.2.3.4, <$A_BUNCH-OF{INVALID}TEXT\>, 60.2.1.1, 2.3.4.5',
412 'count' => 1,
413 'result' => 'Range Softblock with AC Disabled'
414 ],
415 [ 'xff' => '1.2.3.4, 50.2.1.1, 2001:4860:4001:802::1003, 2.3.4.5',
416 'count' => 2,
417 'result' => 'Range6 Hardblock'
418 ],
419 ];
420 }
421
422 /**
423 * @dataProvider providerXff
424 * @covers Block::getBlocksForIPList
425 * @covers Block::chooseBlock
426 */
427 public function testBlocksOnXff( $xff, $exCount, $exResult ) {
428 $user = $this->getUserForBlocking();
429 $this->addBlockForUser( $user );
430
431 $list = array_map( 'trim', explode( ',', $xff ) );
432 $xffblocks = Block::getBlocksForIPList( $list, true );
433 $this->assertEquals( $exCount, count( $xffblocks ), 'Number of blocks for ' . $xff );
434 $block = Block::chooseBlock( $xffblocks, $list );
435 $this->assertEquals(
436 $exResult, $block->getReason(), 'Correct block type for XFF header ' . $xff
437 );
438 }
439
440 /**
441 * @covers Block::getSystemBlockType
442 * @covers Block::insert
443 * @covers Block::doAutoblock
444 */
445 public function testSystemBlocks() {
446 $user = $this->getUserForBlocking();
447 $this->addBlockForUser( $user );
448
449 $blockOptions = [
450 'address' => $user->getName(),
451 'reason' => 'test system block',
452 'timestamp' => wfTimestampNow(),
453 'expiry' => $this->db->getInfinity(),
454 'byText' => 'MediaWiki default',
455 'systemBlock' => 'test',
456 'enableAutoblock' => true,
457 ];
458 $block = new Block( $blockOptions );
459
460 $this->assertSame( 'test', $block->getSystemBlockType() );
461
462 try {
463 $block->insert();
464 $this->fail( 'Expected exception not thrown' );
465 } catch ( MWException $ex ) {
466 $this->assertSame( 'Cannot insert a system block into the database', $ex->getMessage() );
467 }
468
469 try {
470 $block->doAutoblock( '192.0.2.2' );
471 $this->fail( 'Expected exception not thrown' );
472 } catch ( MWException $ex ) {
473 $this->assertSame( 'Cannot autoblock from a system block', $ex->getMessage() );
474 }
475 }
476
477 /**
478 * @covers Block::newFromRow
479 */
480 public function testNewFromRow() {
481 $badActor = $this->getTestUser()->getUser();
482 $sysop = $this->getTestSysop()->getUser();
483
484 $block = new Block( [
485 'address' => $badActor->getName(),
486 'user' => $badActor->getId(),
487 'by' => $sysop->getId(),
488 'expiry' => 'infinity',
489 ] );
490 $block->insert();
491
492 $blockQuery = Block::getQueryInfo();
493 $row = $this->db->select(
494 $blockQuery['tables'],
495 $blockQuery['fields'],
496 [
497 'ipb_id' => $block->getId(),
498 ],
499 __METHOD__,
500 [],
501 $blockQuery['joins']
502 )->fetchObject();
503
504 $block = Block::newFromRow( $row );
505 $this->assertInstanceOf( Block::class, $block );
506 $this->assertEquals( $block->getBy(), $sysop->getId() );
507 $this->assertEquals( $block->getTarget()->getName(), $badActor->getName() );
508 $block->delete();
509 }
510
511 /**
512 * @covers Block::equals
513 */
514 public function testEquals() {
515 $block = new Block();
516
517 $this->assertTrue( $block->equals( $block ) );
518
519 $partial = new Block( [
520 'sitewide' => false,
521 ] );
522 $this->assertFalse( $block->equals( $partial ) );
523 }
524
525 /**
526 * @covers Block::isSitewide
527 */
528 public function testIsSitewide() {
529 $block = new Block();
530 $this->assertTrue( $block->isSitewide() );
531
532 $block = new Block( [
533 'sitewide' => true,
534 ] );
535 $this->assertTrue( $block->isSitewide() );
536
537 $block = new Block( [
538 'sitewide' => false,
539 ] );
540 $this->assertFalse( $block->isSitewide() );
541
542 $block = new Block( [
543 'sitewide' => false,
544 ] );
545 $block->isSitewide( true );
546 $this->assertTrue( $block->isSitewide() );
547 }
548
549 /**
550 * @covers Block::getRestrictions
551 * @covers Block::setRestrictions
552 */
553 public function testRestrictions() {
554 $block = new Block();
555 $restrictions = [
556 new PageRestriction( 0, 1 )
557 ];
558 $block->setRestrictions( $restrictions );
559
560 $this->assertSame( $restrictions, $block->getRestrictions() );
561 }
562
563 /**
564 * @covers Block::getRestrictions
565 * @covers Block::insert
566 */
567 public function testRestrictionsFromDatabase() {
568 $badActor = $this->getTestUser()->getUser();
569 $sysop = $this->getTestSysop()->getUser();
570
571 $block = new Block( [
572 'address' => $badActor->getName(),
573 'user' => $badActor->getId(),
574 'by' => $sysop->getId(),
575 'expiry' => 'infinity',
576 ] );
577 $page = $this->getExistingTestPage( 'Foo' );
578 $restriction = new PageRestriction( 0, $page->getId() );
579 $block->setRestrictions( [ $restriction ] );
580 $block->insert();
581
582 // Refresh the block from the database.
583 $block = Block::newFromID( $block->getId() );
584 $restrictions = $block->getRestrictions();
585 $this->assertCount( 1, $restrictions );
586 $this->assertTrue( $restriction->equals( $restrictions[0] ) );
587 $block->delete();
588 }
589
590 /**
591 * @covers Block::insert
592 */
593 public function testInsertExistingBlock() {
594 $badActor = $this->getTestUser()->getUser();
595 $sysop = $this->getTestSysop()->getUser();
596
597 $block = new Block( [
598 'address' => $badActor->getName(),
599 'user' => $badActor->getId(),
600 'by' => $sysop->getId(),
601 'expiry' => 'infinity',
602 ] );
603 $page = $this->getExistingTestPage( 'Foo' );
604 $restriction = new PageRestriction( 0, $page->getId() );
605 $block->setRestrictions( [ $restriction ] );
606 $block->insert();
607
608 // Insert the block again, which should result in a failur
609 $result = $block->insert();
610
611 $this->assertFalse( $result );
612
613 // Ensure that there are no restrictions where the blockId is 0.
614 $count = $this->db->selectRowCount(
615 'ipblocks_restrictions',
616 '*',
617 [ 'ir_ipb_id' => 0 ],
618 __METHOD__
619 );
620 $this->assertSame( 0, $count );
621
622 $block->delete();
623 }
624
625 /**
626 * @covers Block::appliesToTitle
627 */
628 public function testAppliesToTitleReturnsTrueOnSitewideBlock() {
629 $this->setMwGlobals( [
630 'wgBlockDisablesLogin' => false,
631 ] );
632 $user = $this->getTestUser()->getUser();
633 $block = new Block( [
634 'expiry' => wfTimestamp( TS_MW, wfTimestamp() + ( 40 * 60 * 60 ) ),
635 'allowUsertalk' => true,
636 'sitewide' => true
637 ] );
638
639 $block->setTarget( $user );
640 $block->setBlocker( $this->getTestSysop()->getUser() );
641 $block->insert();
642
643 $title = $this->getExistingTestPage( 'Foo' )->getTitle();
644
645 $this->assertTrue( $block->appliesToTitle( $title ) );
646
647 // appliesToTitle() ignores allowUsertalk
648 $title = $user->getTalkPage();
649 $this->assertTrue( $block->appliesToTitle( $title ) );
650
651 $block->delete();
652 }
653
654 /**
655 * @covers Block::appliesToTitle
656 */
657 public function testAppliesToTitleOnPartialBlock() {
658 $this->setMwGlobals( [
659 'wgBlockDisablesLogin' => false,
660 ] );
661 $user = $this->getTestUser()->getUser();
662 $block = new Block( [
663 'expiry' => wfTimestamp( TS_MW, wfTimestamp() + ( 40 * 60 * 60 ) ),
664 'allowUsertalk' => true,
665 'sitewide' => false
666 ] );
667
668 $block->setTarget( $user );
669 $block->setBlocker( $this->getTestSysop()->getUser() );
670 $block->insert();
671
672 $pageFoo = $this->getExistingTestPage( 'Foo' );
673 $pageBar = $this->getExistingTestPage( 'Bar' );
674 $pageJohn = $this->getExistingTestPage( 'User:John' );
675
676 $pageRestriction = new PageRestriction( $block->getId(), $pageFoo->getId() );
677 $namespaceRestriction = new NamespaceRestriction( $block->getId(), NS_USER );
678 $this->getBlockRestrictionStore()->insert( [ $pageRestriction, $namespaceRestriction ] );
679
680 $this->assertTrue( $block->appliesToTitle( $pageFoo->getTitle() ) );
681 $this->assertFalse( $block->appliesToTitle( $pageBar->getTitle() ) );
682 $this->assertTrue( $block->appliesToTitle( $pageJohn->getTitle() ) );
683
684 $block->delete();
685 }
686
687 /**
688 * @covers Block::appliesToNamespace
689 * @covers Block::appliesToPage
690 */
691 public function testAppliesToReturnsTrueOnSitewideBlock() {
692 $this->setMwGlobals( [
693 'wgBlockDisablesLogin' => false,
694 ] );
695 $user = $this->getTestUser()->getUser();
696 $block = new Block( [
697 'expiry' => wfTimestamp( TS_MW, wfTimestamp() + ( 40 * 60 * 60 ) ),
698 'allowUsertalk' => true,
699 'sitewide' => true
700 ] );
701
702 $block->setTarget( $user );
703 $block->setBlocker( $this->getTestSysop()->getUser() );
704 $block->insert();
705
706 $title = $this->getExistingTestPage()->getTitle();
707
708 $this->assertTrue( $block->appliesToPage( $title->getArticleID() ) );
709 $this->assertTrue( $block->appliesToNamespace( NS_MAIN ) );
710 $this->assertTrue( $block->appliesToNamespace( NS_USER_TALK ) );
711
712 $block->delete();
713 }
714
715 /**
716 * @covers Block::appliesToPage
717 */
718 public function testAppliesToPageOnPartialPageBlock() {
719 $this->setMwGlobals( [
720 'wgBlockDisablesLogin' => false,
721 ] );
722 $user = $this->getTestUser()->getUser();
723 $block = new Block( [
724 'expiry' => wfTimestamp( TS_MW, wfTimestamp() + ( 40 * 60 * 60 ) ),
725 'allowUsertalk' => true,
726 'sitewide' => false
727 ] );
728
729 $block->setTarget( $user );
730 $block->setBlocker( $this->getTestSysop()->getUser() );
731 $block->insert();
732
733 $title = $this->getExistingTestPage()->getTitle();
734
735 $pageRestriction = new PageRestriction(
736 $block->getId(),
737 $title->getArticleID()
738 );
739 $this->getBlockRestrictionStore()->insert( [ $pageRestriction ] );
740
741 $this->assertTrue( $block->appliesToPage( $title->getArticleID() ) );
742
743 $block->delete();
744 }
745
746 /**
747 * @covers Block::appliesToNamespace
748 */
749 public function testAppliesToNamespaceOnPartialNamespaceBlock() {
750 $this->setMwGlobals( [
751 'wgBlockDisablesLogin' => false,
752 ] );
753 $user = $this->getTestUser()->getUser();
754 $block = new Block( [
755 'expiry' => wfTimestamp( TS_MW, wfTimestamp() + ( 40 * 60 * 60 ) ),
756 'allowUsertalk' => true,
757 'sitewide' => false
758 ] );
759
760 $block->setTarget( $user );
761 $block->setBlocker( $this->getTestSysop()->getUser() );
762 $block->insert();
763
764 $namespaceRestriction = new NamespaceRestriction( $block->getId(), NS_MAIN );
765 $this->getBlockRestrictionStore()->insert( [ $namespaceRestriction ] );
766
767 $this->assertTrue( $block->appliesToNamespace( NS_MAIN ) );
768 $this->assertFalse( $block->appliesToNamespace( NS_USER ) );
769
770 $block->delete();
771 }
772
773 /**
774 * @covers Block::appliesToRight
775 */
776 public function testBlockAllowsPurge() {
777 $this->setMwGlobals( [
778 'wgBlockDisablesLogin' => false,
779 ] );
780 $block = new Block();
781 $this->assertFalse( $block->appliesToRight( 'purge' ) );
782 }
783
784 /**
785 * Get an instance of BlockRestrictionStore
786 *
787 * @return BlockRestrictionStore
788 */
789 protected function getBlockRestrictionStore() : BlockRestrictionStore {
790 return MediaWikiServices::getInstance()->getBlockRestrictionStore();
791 }
792 }