Merge "EditPage: Migrate Title::userCan() calls to PermissionManager"
[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 narrow IPv4 range and IP' => [
179 [ '0.0.0.0/31', '0.0.0.0' ],
180 '0.0.0.0',
181 '0.0.0.0'
182 ],
183 'Blocks to wide IPv6 range and IP' => [
184 [ '0:0:0:0:0:0:0:0/19', '0:0:0:0:0:0:0:0' ],
185 '0:0:0:0:0:0:0:0',
186 '0:0:0:0: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 'Blocks to wide IPv6 range and IP, large numbers' => [
194 [ '2000:DEAD:BEEF:A:0:0:0:0/19', '2000:DEAD:BEEF:A:0:0:0:0' ],
195 '2000:DEAD:BEEF:A:0:0:0:0',
196 '2000:DEAD:BEEF:A:0:0:0:0'
197 ],
198 'Blocks to narrow IPv6 range and IP, large numbers' => [
199 [ '2000:DEAD:BEEF:A:0:0:0:0/127', '2000:DEAD:BEEF:A:0:0:0:0' ],
200 '2000:DEAD:BEEF:A:0:0:0:0',
201 '2000:DEAD:BEEF:A:0:0:0:0'
202 ],
203 ];
204 }
205
206 /**
207 * @covers Block::appliesToRight
208 */
209 public function testBlockedUserCanNotCreateAccount() {
210 $username = 'BlockedUserToCreateAccountWith';
211 $u = User::newFromName( $username );
212 $u->addToDatabase();
213 $userId = $u->getId();
214 $this->assertNotEquals( 0, $userId, 'sanity' );
215 TestUser::setPasswordForUser( $u, 'NotRandomPass' );
216 unset( $u );
217
218 // Sanity check
219 $this->assertNull(
220 Block::newFromTarget( $username ),
221 "$username should not be blocked"
222 );
223
224 // Reload user
225 $u = User::newFromName( $username );
226 $this->assertFalse(
227 $u->isBlockedFromCreateAccount(),
228 "Our sandbox user should be able to create account before being blocked"
229 );
230
231 // Foreign perspective (blockee not on current wiki)...
232 $blockOptions = [
233 'address' => $username,
234 'user' => $userId,
235 'reason' => 'crosswiki block...',
236 'timestamp' => wfTimestampNow(),
237 'expiry' => $this->db->getInfinity(),
238 'createAccount' => true,
239 'enableAutoblock' => true,
240 'hideName' => true,
241 'blockEmail' => true,
242 'byText' => 'm>MetaWikiUser',
243 ];
244 $block = new Block( $blockOptions );
245 $block->insert();
246
247 // Reload block from DB
248 $userBlock = Block::newFromTarget( $username );
249 $this->assertTrue(
250 (bool)$block->appliesToRight( 'createaccount' ),
251 "Block object in DB should block right 'createaccount'"
252 );
253
254 $this->assertInstanceOf(
255 Block::class,
256 $userBlock,
257 "'$username' block block object should be existent"
258 );
259
260 // Reload user
261 $u = User::newFromName( $username );
262 $this->assertTrue(
263 (bool)$u->isBlockedFromCreateAccount(),
264 "Our sandbox user '$username' should NOT be able to create account"
265 );
266 }
267
268 /**
269 * @covers Block::insert
270 */
271 public function testCrappyCrossWikiBlocks() {
272 // Delete the last round's block if it's still there
273 $oldBlock = Block::newFromTarget( 'UserOnForeignWiki' );
274 if ( $oldBlock ) {
275 // An old block will prevent our new one from saving.
276 $oldBlock->delete();
277 }
278
279 // Local perspective (blockee on current wiki)...
280 $user = User::newFromName( 'UserOnForeignWiki' );
281 $user->addToDatabase();
282 $userId = $user->getId();
283 $this->assertNotEquals( 0, $userId, 'sanity' );
284
285 // Foreign perspective (blockee not on current wiki)...
286 $blockOptions = [
287 'address' => 'UserOnForeignWiki',
288 'user' => $user->getId(),
289 'reason' => 'crosswiki block...',
290 'timestamp' => wfTimestampNow(),
291 'expiry' => $this->db->getInfinity(),
292 'createAccount' => true,
293 'enableAutoblock' => true,
294 'hideName' => true,
295 'blockEmail' => true,
296 'byText' => 'Meta>MetaWikiUser',
297 ];
298 $block = new Block( $blockOptions );
299
300 $res = $block->insert( $this->db );
301 $this->assertTrue( (bool)$res['id'], 'Block succeeded' );
302
303 $user = null; // clear
304
305 $block = Block::newFromID( $res['id'] );
306 $this->assertEquals(
307 'UserOnForeignWiki',
308 $block->getTarget()->getName(),
309 'Correct blockee name'
310 );
311 $this->assertEquals( $userId, $block->getTarget()->getId(), 'Correct blockee id' );
312 $this->assertEquals( 'Meta>MetaWikiUser', $block->getBlocker()->getName(),
313 'Correct blocker name' );
314 $this->assertEquals( 'Meta>MetaWikiUser', $block->getByName(), 'Correct blocker name' );
315 $this->assertEquals( 0, $block->getBy(), 'Correct blocker id' );
316 }
317
318 protected function addXffBlocks() {
319 static $inited = false;
320
321 if ( $inited ) {
322 return;
323 }
324
325 $inited = true;
326
327 $blockList = [
328 [ 'target' => '70.2.0.0/16',
329 'type' => Block::TYPE_RANGE,
330 'desc' => 'Range Hardblock',
331 'ACDisable' => false,
332 'isHardblock' => true,
333 'isAutoBlocking' => false,
334 ],
335 [ 'target' => '2001:4860:4001::/48',
336 'type' => Block::TYPE_RANGE,
337 'desc' => 'Range6 Hardblock',
338 'ACDisable' => false,
339 'isHardblock' => true,
340 'isAutoBlocking' => false,
341 ],
342 [ 'target' => '60.2.0.0/16',
343 'type' => Block::TYPE_RANGE,
344 'desc' => 'Range Softblock with AC Disabled',
345 'ACDisable' => true,
346 'isHardblock' => false,
347 'isAutoBlocking' => false,
348 ],
349 [ 'target' => '50.2.0.0/16',
350 'type' => Block::TYPE_RANGE,
351 'desc' => 'Range Softblock',
352 'ACDisable' => false,
353 'isHardblock' => false,
354 'isAutoBlocking' => false,
355 ],
356 [ 'target' => '50.1.1.1',
357 'type' => Block::TYPE_IP,
358 'desc' => 'Exact Softblock',
359 'ACDisable' => false,
360 'isHardblock' => false,
361 'isAutoBlocking' => false,
362 ],
363 ];
364
365 $blocker = $this->getTestUser()->getUser();
366 foreach ( $blockList as $insBlock ) {
367 $target = $insBlock['target'];
368
369 if ( $insBlock['type'] === Block::TYPE_IP ) {
370 $target = User::newFromName( IP::sanitizeIP( $target ), false )->getName();
371 } elseif ( $insBlock['type'] === Block::TYPE_RANGE ) {
372 $target = IP::sanitizeRange( $target );
373 }
374
375 $block = new Block();
376 $block->setTarget( $target );
377 $block->setBlocker( $blocker );
378 $block->setReason( $insBlock['desc'] );
379 $block->setExpiry( 'infinity' );
380 $block->isCreateAccountBlocked( $insBlock['ACDisable'] );
381 $block->isHardblock( $insBlock['isHardblock'] );
382 $block->isAutoblocking( $insBlock['isAutoBlocking'] );
383 $block->insert();
384 }
385 }
386
387 public static function providerXff() {
388 return [
389 [ 'xff' => '1.2.3.4, 70.2.1.1, 60.2.1.1, 2.3.4.5',
390 'count' => 2,
391 'result' => 'Range Hardblock'
392 ],
393 [ 'xff' => '1.2.3.4, 50.2.1.1, 60.2.1.1, 2.3.4.5',
394 'count' => 2,
395 'result' => 'Range Softblock with AC Disabled'
396 ],
397 [ 'xff' => '1.2.3.4, 70.2.1.1, 50.1.1.1, 2.3.4.5',
398 'count' => 2,
399 'result' => 'Exact Softblock'
400 ],
401 [ 'xff' => '1.2.3.4, 70.2.1.1, 50.2.1.1, 50.1.1.1, 2.3.4.5',
402 'count' => 3,
403 'result' => 'Exact Softblock'
404 ],
405 [ 'xff' => '1.2.3.4, 70.2.1.1, 50.2.1.1, 2.3.4.5',
406 'count' => 2,
407 'result' => 'Range Hardblock'
408 ],
409 [ 'xff' => '1.2.3.4, 70.2.1.1, 60.2.1.1, 2.3.4.5',
410 'count' => 2,
411 'result' => 'Range Hardblock'
412 ],
413 [ 'xff' => '50.2.1.1, 60.2.1.1, 2.3.4.5',
414 'count' => 2,
415 'result' => 'Range Softblock with AC Disabled'
416 ],
417 [ 'xff' => '1.2.3.4, 50.1.1.1, 60.2.1.1, 2.3.4.5',
418 'count' => 2,
419 'result' => 'Exact Softblock'
420 ],
421 [ 'xff' => '1.2.3.4, <$A_BUNCH-OF{INVALID}TEXT\>, 60.2.1.1, 2.3.4.5',
422 'count' => 1,
423 'result' => 'Range Softblock with AC Disabled'
424 ],
425 [ 'xff' => '1.2.3.4, 50.2.1.1, 2001:4860:4001:802::1003, 2.3.4.5',
426 'count' => 2,
427 'result' => 'Range6 Hardblock'
428 ],
429 ];
430 }
431
432 /**
433 * @dataProvider providerXff
434 * @covers Block::getBlocksForIPList
435 * @covers Block::chooseBlock
436 */
437 public function testBlocksOnXff( $xff, $exCount, $exResult ) {
438 $user = $this->getUserForBlocking();
439 $this->addBlockForUser( $user );
440
441 $list = array_map( 'trim', explode( ',', $xff ) );
442 $xffblocks = Block::getBlocksForIPList( $list, true );
443 $this->assertEquals( $exCount, count( $xffblocks ), 'Number of blocks for ' . $xff );
444 $block = Block::chooseBlock( $xffblocks, $list );
445 $this->assertEquals(
446 $exResult, $block->getReason(), 'Correct block type for XFF header ' . $xff
447 );
448 }
449
450 /**
451 * @covers Block::newFromRow
452 */
453 public function testNewFromRow() {
454 $badActor = $this->getTestUser()->getUser();
455 $sysop = $this->getTestSysop()->getUser();
456
457 $block = new Block( [
458 'address' => $badActor->getName(),
459 'user' => $badActor->getId(),
460 'by' => $sysop->getId(),
461 'expiry' => 'infinity',
462 ] );
463 $block->insert();
464
465 $blockQuery = Block::getQueryInfo();
466 $row = $this->db->select(
467 $blockQuery['tables'],
468 $blockQuery['fields'],
469 [
470 'ipb_id' => $block->getId(),
471 ],
472 __METHOD__,
473 [],
474 $blockQuery['joins']
475 )->fetchObject();
476
477 $block = Block::newFromRow( $row );
478 $this->assertInstanceOf( Block::class, $block );
479 $this->assertEquals( $block->getBy(), $sysop->getId() );
480 $this->assertEquals( $block->getTarget()->getName(), $badActor->getName() );
481 $block->delete();
482 }
483
484 /**
485 * @covers Block::equals
486 */
487 public function testEquals() {
488 $block = new Block();
489
490 $this->assertTrue( $block->equals( $block ) );
491
492 $partial = new Block( [
493 'sitewide' => false,
494 ] );
495 $this->assertFalse( $block->equals( $partial ) );
496 }
497
498 /**
499 * @covers Block::isSitewide
500 */
501 public function testIsSitewide() {
502 $block = new Block();
503 $this->assertTrue( $block->isSitewide() );
504
505 $block = new Block( [
506 'sitewide' => true,
507 ] );
508 $this->assertTrue( $block->isSitewide() );
509
510 $block = new Block( [
511 'sitewide' => false,
512 ] );
513 $this->assertFalse( $block->isSitewide() );
514
515 $block = new Block( [
516 'sitewide' => false,
517 ] );
518 $block->isSitewide( true );
519 $this->assertTrue( $block->isSitewide() );
520 }
521
522 /**
523 * @covers Block::getRestrictions
524 * @covers Block::setRestrictions
525 */
526 public function testRestrictions() {
527 $block = new Block();
528 $restrictions = [
529 new PageRestriction( 0, 1 )
530 ];
531 $block->setRestrictions( $restrictions );
532
533 $this->assertSame( $restrictions, $block->getRestrictions() );
534 }
535
536 /**
537 * @covers Block::getRestrictions
538 * @covers Block::insert
539 */
540 public function testRestrictionsFromDatabase() {
541 $badActor = $this->getTestUser()->getUser();
542 $sysop = $this->getTestSysop()->getUser();
543
544 $block = new Block( [
545 'address' => $badActor->getName(),
546 'user' => $badActor->getId(),
547 'by' => $sysop->getId(),
548 'expiry' => 'infinity',
549 ] );
550 $page = $this->getExistingTestPage( 'Foo' );
551 $restriction = new PageRestriction( 0, $page->getId() );
552 $block->setRestrictions( [ $restriction ] );
553 $block->insert();
554
555 // Refresh the block from the database.
556 $block = Block::newFromID( $block->getId() );
557 $restrictions = $block->getRestrictions();
558 $this->assertCount( 1, $restrictions );
559 $this->assertTrue( $restriction->equals( $restrictions[0] ) );
560 $block->delete();
561 }
562
563 /**
564 * @covers Block::insert
565 */
566 public function testInsertExistingBlock() {
567 $badActor = $this->getTestUser()->getUser();
568 $sysop = $this->getTestSysop()->getUser();
569
570 $block = new Block( [
571 'address' => $badActor->getName(),
572 'user' => $badActor->getId(),
573 'by' => $sysop->getId(),
574 'expiry' => 'infinity',
575 ] );
576 $page = $this->getExistingTestPage( 'Foo' );
577 $restriction = new PageRestriction( 0, $page->getId() );
578 $block->setRestrictions( [ $restriction ] );
579 $block->insert();
580
581 // Insert the block again, which should result in a failur
582 $result = $block->insert();
583
584 $this->assertFalse( $result );
585
586 // Ensure that there are no restrictions where the blockId is 0.
587 $count = $this->db->selectRowCount(
588 'ipblocks_restrictions',
589 '*',
590 [ 'ir_ipb_id' => 0 ],
591 __METHOD__
592 );
593 $this->assertSame( 0, $count );
594
595 $block->delete();
596 }
597
598 /**
599 * @covers Block::appliesToTitle
600 */
601 public function testAppliesToTitleReturnsTrueOnSitewideBlock() {
602 $this->setMwGlobals( [
603 'wgBlockDisablesLogin' => false,
604 ] );
605 $user = $this->getTestUser()->getUser();
606 $block = new Block( [
607 'expiry' => wfTimestamp( TS_MW, wfTimestamp() + ( 40 * 60 * 60 ) ),
608 'allowUsertalk' => true,
609 'sitewide' => true
610 ] );
611
612 $block->setTarget( $user );
613 $block->setBlocker( $this->getTestSysop()->getUser() );
614 $block->insert();
615
616 $title = $this->getExistingTestPage( 'Foo' )->getTitle();
617
618 $this->assertTrue( $block->appliesToTitle( $title ) );
619
620 // appliesToTitle() ignores allowUsertalk
621 $title = $user->getTalkPage();
622 $this->assertTrue( $block->appliesToTitle( $title ) );
623
624 $block->delete();
625 }
626
627 /**
628 * @covers Block::appliesToTitle
629 */
630 public function testAppliesToTitleOnPartialBlock() {
631 $this->setMwGlobals( [
632 'wgBlockDisablesLogin' => false,
633 ] );
634 $user = $this->getTestUser()->getUser();
635 $block = new Block( [
636 'expiry' => wfTimestamp( TS_MW, wfTimestamp() + ( 40 * 60 * 60 ) ),
637 'allowUsertalk' => true,
638 'sitewide' => false
639 ] );
640
641 $block->setTarget( $user );
642 $block->setBlocker( $this->getTestSysop()->getUser() );
643 $block->insert();
644
645 $pageFoo = $this->getExistingTestPage( 'Foo' );
646 $pageBar = $this->getExistingTestPage( 'Bar' );
647 $pageJohn = $this->getExistingTestPage( 'User:John' );
648
649 $pageRestriction = new PageRestriction( $block->getId(), $pageFoo->getId() );
650 $namespaceRestriction = new NamespaceRestriction( $block->getId(), NS_USER );
651 $this->getBlockRestrictionStore()->insert( [ $pageRestriction, $namespaceRestriction ] );
652
653 $this->assertTrue( $block->appliesToTitle( $pageFoo->getTitle() ) );
654 $this->assertFalse( $block->appliesToTitle( $pageBar->getTitle() ) );
655 $this->assertTrue( $block->appliesToTitle( $pageJohn->getTitle() ) );
656
657 $block->delete();
658 }
659
660 /**
661 * @covers Block::appliesToNamespace
662 * @covers Block::appliesToPage
663 */
664 public function testAppliesToReturnsTrueOnSitewideBlock() {
665 $this->setMwGlobals( [
666 'wgBlockDisablesLogin' => false,
667 ] );
668 $user = $this->getTestUser()->getUser();
669 $block = new Block( [
670 'expiry' => wfTimestamp( TS_MW, wfTimestamp() + ( 40 * 60 * 60 ) ),
671 'allowUsertalk' => true,
672 'sitewide' => true
673 ] );
674
675 $block->setTarget( $user );
676 $block->setBlocker( $this->getTestSysop()->getUser() );
677 $block->insert();
678
679 $title = $this->getExistingTestPage()->getTitle();
680
681 $this->assertTrue( $block->appliesToPage( $title->getArticleID() ) );
682 $this->assertTrue( $block->appliesToNamespace( NS_MAIN ) );
683 $this->assertTrue( $block->appliesToNamespace( NS_USER_TALK ) );
684
685 $block->delete();
686 }
687
688 /**
689 * @covers Block::appliesToPage
690 */
691 public function testAppliesToPageOnPartialPageBlock() {
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' => false
700 ] );
701
702 $block->setTarget( $user );
703 $block->setBlocker( $this->getTestSysop()->getUser() );
704 $block->insert();
705
706 $title = $this->getExistingTestPage()->getTitle();
707
708 $pageRestriction = new PageRestriction(
709 $block->getId(),
710 $title->getArticleID()
711 );
712 $this->getBlockRestrictionStore()->insert( [ $pageRestriction ] );
713
714 $this->assertTrue( $block->appliesToPage( $title->getArticleID() ) );
715
716 $block->delete();
717 }
718
719 /**
720 * @covers Block::appliesToNamespace
721 */
722 public function testAppliesToNamespaceOnPartialNamespaceBlock() {
723 $this->setMwGlobals( [
724 'wgBlockDisablesLogin' => false,
725 ] );
726 $user = $this->getTestUser()->getUser();
727 $block = new Block( [
728 'expiry' => wfTimestamp( TS_MW, wfTimestamp() + ( 40 * 60 * 60 ) ),
729 'allowUsertalk' => true,
730 'sitewide' => false
731 ] );
732
733 $block->setTarget( $user );
734 $block->setBlocker( $this->getTestSysop()->getUser() );
735 $block->insert();
736
737 $namespaceRestriction = new NamespaceRestriction( $block->getId(), NS_MAIN );
738 $this->getBlockRestrictionStore()->insert( [ $namespaceRestriction ] );
739
740 $this->assertTrue( $block->appliesToNamespace( NS_MAIN ) );
741 $this->assertFalse( $block->appliesToNamespace( NS_USER ) );
742
743 $block->delete();
744 }
745
746 /**
747 * @covers Block::appliesToRight
748 */
749 public function testBlockAllowsPurge() {
750 $this->setMwGlobals( [
751 'wgBlockDisablesLogin' => false,
752 ] );
753 $block = new Block();
754 $this->assertFalse( $block->appliesToRight( 'purge' ) );
755 }
756
757 /**
758 * Get an instance of BlockRestrictionStore
759 *
760 * @return BlockRestrictionStore
761 */
762 protected function getBlockRestrictionStore() : BlockRestrictionStore {
763 return MediaWikiServices::getInstance()->getBlockRestrictionStore();
764 }
765 }