Debian Codegraph @

tests/phpunit/includes/block/DatabaseBlockTest.php

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