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