Merge "Remove parameter 'options' from hook 'SkinEditSectionLinks'"
[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::newFromRow
442 */
443 public function testNewFromRow() {
444 $badActor = $this->getTestUser()->getUser();
445 $sysop = $this->getTestSysop()->getUser();
446
447 $block = new Block( [
448 'address' => $badActor->getName(),
449 'user' => $badActor->getId(),
450 'by' => $sysop->getId(),
451 'expiry' => 'infinity',
452 ] );
453 $block->insert();
454
455 $blockQuery = Block::getQueryInfo();
456 $row = $this->db->select(
457 $blockQuery['tables'],
458 $blockQuery['fields'],
459 [
460 'ipb_id' => $block->getId(),
461 ],
462 __METHOD__,
463 [],
464 $blockQuery['joins']
465 )->fetchObject();
466
467 $block = Block::newFromRow( $row );
468 $this->assertInstanceOf( Block::class, $block );
469 $this->assertEquals( $block->getBy(), $sysop->getId() );
470 $this->assertEquals( $block->getTarget()->getName(), $badActor->getName() );
471 $block->delete();
472 }
473
474 /**
475 * @covers Block::equals
476 */
477 public function testEquals() {
478 $block = new Block();
479
480 $this->assertTrue( $block->equals( $block ) );
481
482 $partial = new Block( [
483 'sitewide' => false,
484 ] );
485 $this->assertFalse( $block->equals( $partial ) );
486 }
487
488 /**
489 * @covers Block::isSitewide
490 */
491 public function testIsSitewide() {
492 $block = new Block();
493 $this->assertTrue( $block->isSitewide() );
494
495 $block = new Block( [
496 'sitewide' => true,
497 ] );
498 $this->assertTrue( $block->isSitewide() );
499
500 $block = new Block( [
501 'sitewide' => false,
502 ] );
503 $this->assertFalse( $block->isSitewide() );
504
505 $block = new Block( [
506 'sitewide' => false,
507 ] );
508 $block->isSitewide( true );
509 $this->assertTrue( $block->isSitewide() );
510 }
511
512 /**
513 * @covers Block::getRestrictions
514 * @covers Block::setRestrictions
515 */
516 public function testRestrictions() {
517 $block = new Block();
518 $restrictions = [
519 new PageRestriction( 0, 1 )
520 ];
521 $block->setRestrictions( $restrictions );
522
523 $this->assertSame( $restrictions, $block->getRestrictions() );
524 }
525
526 /**
527 * @covers Block::getRestrictions
528 * @covers Block::insert
529 */
530 public function testRestrictionsFromDatabase() {
531 $badActor = $this->getTestUser()->getUser();
532 $sysop = $this->getTestSysop()->getUser();
533
534 $block = new Block( [
535 'address' => $badActor->getName(),
536 'user' => $badActor->getId(),
537 'by' => $sysop->getId(),
538 'expiry' => 'infinity',
539 ] );
540 $page = $this->getExistingTestPage( 'Foo' );
541 $restriction = new PageRestriction( 0, $page->getId() );
542 $block->setRestrictions( [ $restriction ] );
543 $block->insert();
544
545 // Refresh the block from the database.
546 $block = Block::newFromID( $block->getId() );
547 $restrictions = $block->getRestrictions();
548 $this->assertCount( 1, $restrictions );
549 $this->assertTrue( $restriction->equals( $restrictions[0] ) );
550 $block->delete();
551 }
552
553 /**
554 * @covers Block::insert
555 */
556 public function testInsertExistingBlock() {
557 $badActor = $this->getTestUser()->getUser();
558 $sysop = $this->getTestSysop()->getUser();
559
560 $block = new Block( [
561 'address' => $badActor->getName(),
562 'user' => $badActor->getId(),
563 'by' => $sysop->getId(),
564 'expiry' => 'infinity',
565 ] );
566 $page = $this->getExistingTestPage( 'Foo' );
567 $restriction = new PageRestriction( 0, $page->getId() );
568 $block->setRestrictions( [ $restriction ] );
569 $block->insert();
570
571 // Insert the block again, which should result in a failur
572 $result = $block->insert();
573
574 $this->assertFalse( $result );
575
576 // Ensure that there are no restrictions where the blockId is 0.
577 $count = $this->db->selectRowCount(
578 'ipblocks_restrictions',
579 '*',
580 [ 'ir_ipb_id' => 0 ],
581 __METHOD__
582 );
583 $this->assertSame( 0, $count );
584
585 $block->delete();
586 }
587
588 /**
589 * @covers Block::appliesToTitle
590 */
591 public function testAppliesToTitleReturnsTrueOnSitewideBlock() {
592 $this->setMwGlobals( [
593 'wgBlockDisablesLogin' => false,
594 ] );
595 $user = $this->getTestUser()->getUser();
596 $block = new Block( [
597 'expiry' => wfTimestamp( TS_MW, wfTimestamp() + ( 40 * 60 * 60 ) ),
598 'allowUsertalk' => true,
599 'sitewide' => true
600 ] );
601
602 $block->setTarget( $user );
603 $block->setBlocker( $this->getTestSysop()->getUser() );
604 $block->insert();
605
606 $title = $this->getExistingTestPage( 'Foo' )->getTitle();
607
608 $this->assertTrue( $block->appliesToTitle( $title ) );
609
610 // appliesToTitle() ignores allowUsertalk
611 $title = $user->getTalkPage();
612 $this->assertTrue( $block->appliesToTitle( $title ) );
613
614 $block->delete();
615 }
616
617 /**
618 * @covers Block::appliesToTitle
619 */
620 public function testAppliesToTitleOnPartialBlock() {
621 $this->setMwGlobals( [
622 'wgBlockDisablesLogin' => false,
623 ] );
624 $user = $this->getTestUser()->getUser();
625 $block = new Block( [
626 'expiry' => wfTimestamp( TS_MW, wfTimestamp() + ( 40 * 60 * 60 ) ),
627 'allowUsertalk' => true,
628 'sitewide' => false
629 ] );
630
631 $block->setTarget( $user );
632 $block->setBlocker( $this->getTestSysop()->getUser() );
633 $block->insert();
634
635 $pageFoo = $this->getExistingTestPage( 'Foo' );
636 $pageBar = $this->getExistingTestPage( 'Bar' );
637 $pageJohn = $this->getExistingTestPage( 'User:John' );
638
639 $pageRestriction = new PageRestriction( $block->getId(), $pageFoo->getId() );
640 $namespaceRestriction = new NamespaceRestriction( $block->getId(), NS_USER );
641 $this->getBlockRestrictionStore()->insert( [ $pageRestriction, $namespaceRestriction ] );
642
643 $this->assertTrue( $block->appliesToTitle( $pageFoo->getTitle() ) );
644 $this->assertFalse( $block->appliesToTitle( $pageBar->getTitle() ) );
645 $this->assertTrue( $block->appliesToTitle( $pageJohn->getTitle() ) );
646
647 $block->delete();
648 }
649
650 /**
651 * @covers Block::appliesToNamespace
652 * @covers Block::appliesToPage
653 */
654 public function testAppliesToReturnsTrueOnSitewideBlock() {
655 $this->setMwGlobals( [
656 'wgBlockDisablesLogin' => false,
657 ] );
658 $user = $this->getTestUser()->getUser();
659 $block = new Block( [
660 'expiry' => wfTimestamp( TS_MW, wfTimestamp() + ( 40 * 60 * 60 ) ),
661 'allowUsertalk' => true,
662 'sitewide' => true
663 ] );
664
665 $block->setTarget( $user );
666 $block->setBlocker( $this->getTestSysop()->getUser() );
667 $block->insert();
668
669 $title = $this->getExistingTestPage()->getTitle();
670
671 $this->assertTrue( $block->appliesToPage( $title->getArticleID() ) );
672 $this->assertTrue( $block->appliesToNamespace( NS_MAIN ) );
673 $this->assertTrue( $block->appliesToNamespace( NS_USER_TALK ) );
674
675 $block->delete();
676 }
677
678 /**
679 * @covers Block::appliesToPage
680 */
681 public function testAppliesToPageOnPartialPageBlock() {
682 $this->setMwGlobals( [
683 'wgBlockDisablesLogin' => false,
684 ] );
685 $user = $this->getTestUser()->getUser();
686 $block = new Block( [
687 'expiry' => wfTimestamp( TS_MW, wfTimestamp() + ( 40 * 60 * 60 ) ),
688 'allowUsertalk' => true,
689 'sitewide' => false
690 ] );
691
692 $block->setTarget( $user );
693 $block->setBlocker( $this->getTestSysop()->getUser() );
694 $block->insert();
695
696 $title = $this->getExistingTestPage()->getTitle();
697
698 $pageRestriction = new PageRestriction(
699 $block->getId(),
700 $title->getArticleID()
701 );
702 $this->getBlockRestrictionStore()->insert( [ $pageRestriction ] );
703
704 $this->assertTrue( $block->appliesToPage( $title->getArticleID() ) );
705
706 $block->delete();
707 }
708
709 /**
710 * @covers Block::appliesToNamespace
711 */
712 public function testAppliesToNamespaceOnPartialNamespaceBlock() {
713 $this->setMwGlobals( [
714 'wgBlockDisablesLogin' => false,
715 ] );
716 $user = $this->getTestUser()->getUser();
717 $block = new Block( [
718 'expiry' => wfTimestamp( TS_MW, wfTimestamp() + ( 40 * 60 * 60 ) ),
719 'allowUsertalk' => true,
720 'sitewide' => false
721 ] );
722
723 $block->setTarget( $user );
724 $block->setBlocker( $this->getTestSysop()->getUser() );
725 $block->insert();
726
727 $namespaceRestriction = new NamespaceRestriction( $block->getId(), NS_MAIN );
728 $this->getBlockRestrictionStore()->insert( [ $namespaceRestriction ] );
729
730 $this->assertTrue( $block->appliesToNamespace( NS_MAIN ) );
731 $this->assertFalse( $block->appliesToNamespace( NS_USER ) );
732
733 $block->delete();
734 }
735
736 /**
737 * @covers Block::appliesToRight
738 */
739 public function testBlockAllowsPurge() {
740 $this->setMwGlobals( [
741 'wgBlockDisablesLogin' => false,
742 ] );
743 $block = new Block();
744 $this->assertFalse( $block->appliesToRight( 'purge' ) );
745 }
746
747 /**
748 * Get an instance of BlockRestrictionStore
749 *
750 * @return BlockRestrictionStore
751 */
752 protected function getBlockRestrictionStore() : BlockRestrictionStore {
753 return MediaWikiServices::getInstance()->getBlockRestrictionStore();
754 }
755 }