3 namespace MediaWiki\Tests\Block
;
5 use MediaWiki\Block\BlockRestrictionStore
;
6 use MediaWiki\Block\Restriction\NamespaceRestriction
;
7 use MediaWiki\Block\Restriction\PageRestriction
;
8 use MediaWiki\Block\Restriction\Restriction
;
9 use MediaWiki\MediaWikiServices
;
14 * @coversDefaultClass \MediaWiki\Block\BlockRestrictionStore
16 class BlockRestrictionStoreTest
extends \MediaWikiLangTestCase
{
18 /** @var BlockRestrictionStore */
19 protected $blockRestrictionStore;
21 public function setUp() {
24 $this->blockRestrictionStore
= MediaWikiServices
::getInstance()->getBlockRestrictionStore();
27 public function tearDown() {
33 * @covers ::loadByBlockId
34 * @covers ::resultToRestrictions
35 * @covers ::rowToRestriction
37 public function testLoadMultipleRestrictions() {
38 $this->setMwGlobals( [
39 'wgBlockDisablesLogin' => false,
41 $block = $this->insertBlock();
43 $pageFoo = $this->getExistingTestPage( 'Foo' );
44 $pageBar = $this->getExistingTestPage( 'Bar' );
46 $this->blockRestrictionStore
->insert( [
47 new PageRestriction( $block->getId(), $pageFoo->getId() ),
48 new PageRestriction( $block->getId(), $pageBar->getId() ),
49 new NamespaceRestriction( $block->getId(), NS_USER
),
52 $restrictions = $this->blockRestrictionStore
->loadByBlockId( $block->getId() );
54 $this->assertCount( 3, $restrictions );
58 * @covers ::loadByBlockId
59 * @covers ::resultToRestrictions
60 * @covers ::rowToRestriction
62 public function testWithNoRestrictions() {
63 $block = $this->insertBlock();
64 $restrictions = $this->blockRestrictionStore
->loadByBlockId( $block->getId() );
65 $this->assertEmpty( $restrictions );
69 * @covers ::loadByBlockId
70 * @covers ::resultToRestrictions
71 * @covers ::rowToRestriction
73 public function testWithEmptyParam() {
74 $restrictions = $this->blockRestrictionStore
->loadByBlockId( [] );
75 $this->assertEmpty( $restrictions );
79 * @covers ::loadByBlockId
80 * @covers ::resultToRestrictions
81 * @covers ::rowToRestriction
83 public function testIgnoreNotSupportedTypes() {
84 $block = $this->insertBlock();
86 $pageFoo = $this->getExistingTestPage( 'Foo' );
87 $pageBar = $this->getExistingTestPage( 'Bar' );
90 $this->insertRestriction( $block->getId(), PageRestriction
::TYPE_ID
, $pageFoo->getId() );
91 $this->insertRestriction( $block->getId(), NamespaceRestriction
::TYPE_ID
, NS_USER
);
94 $this->insertRestriction( $block->getId(), 9, $pageBar->getId() );
95 $this->insertRestriction( $block->getId(), 10, NS_FILE
);
97 $restrictions = $this->blockRestrictionStore
->loadByBlockId( $block->getId() );
98 $this->assertCount( 2, $restrictions );
102 * @covers ::loadByBlockId
103 * @covers ::resultToRestrictions
104 * @covers ::rowToRestriction
106 public function testMappingPageRestrictionObject() {
107 $block = $this->insertBlock();
108 $title = 'Lady Macbeth';
109 $page = $this->getExistingTestPage( $title );
111 // Test Page Restrictions.
112 $this->blockRestrictionStore
->insert( [
113 new PageRestriction( $block->getId(), $page->getId() ),
116 $restrictions = $this->blockRestrictionStore
->loadByBlockId( $block->getId() );
118 list( $pageRestriction ) = $restrictions;
119 $this->assertInstanceOf( PageRestriction
::class, $pageRestriction );
120 $this->assertEquals( $block->getId(), $pageRestriction->getBlockId() );
121 $this->assertEquals( $page->getId(), $pageRestriction->getValue() );
122 $this->assertEquals( $pageRestriction->getType(), PageRestriction
::TYPE
);
123 $this->assertEquals( $pageRestriction->getTitle()->getText(), $title );
127 * @covers ::loadByBlockId
128 * @covers ::resultToRestrictions
129 * @covers ::rowToRestriction
131 public function testMappingNamespaceRestrictionObject() {
132 $block = $this->insertBlock();
134 $this->blockRestrictionStore
->insert( [
135 new NamespaceRestriction( $block->getId(), NS_USER
),
138 $restrictions = $this->blockRestrictionStore
->loadByBlockId( $block->getId() );
140 list( $namespaceRestriction ) = $restrictions;
141 $this->assertInstanceOf( NamespaceRestriction
::class, $namespaceRestriction );
142 $this->assertEquals( $block->getId(), $namespaceRestriction->getBlockId() );
143 $this->assertSame( NS_USER
, $namespaceRestriction->getValue() );
144 $this->assertEquals( $namespaceRestriction->getType(), NamespaceRestriction
::TYPE
);
150 public function testInsert() {
151 $block = $this->insertBlock();
153 $pageFoo = $this->getExistingTestPage( 'Foo' );
154 $pageBar = $this->getExistingTestPage( 'Bar' );
158 new PageRestriction( $block->getId(), $pageFoo->getId() ),
159 new PageRestriction( $block->getId(), $pageBar->getId() ),
160 new NamespaceRestriction( $block->getId(), NS_USER
)
163 $result = $this->blockRestrictionStore
->insert( $restrictions );
164 $this->assertTrue( $result );
170 $result = $this->blockRestrictionStore
->insert( $restrictions );
171 $this->assertFalse( $result );
173 $result = $this->blockRestrictionStore
->insert( [] );
174 $this->assertFalse( $result );
180 public function testInsertTypes() {
181 $block = $this->insertBlock();
183 $pageFoo = $this->getExistingTestPage( 'Foo' );
184 $pageBar = $this->getExistingTestPage( 'Bar' );
186 $invalid = $this->createMock( Restriction
::class );
187 $invalid->method( 'toRow' )
189 'ir_ipb_id' => $block->getId(),
196 new PageRestriction( $block->getId(), $pageFoo->getId() ),
197 new PageRestriction( $block->getId(), $pageBar->getId() ),
198 new NamespaceRestriction( $block->getId(), NS_USER
),
202 $result = $this->blockRestrictionStore
->insert( $restrictions );
203 $this->assertTrue( $result );
205 $restrictions = $this->blockRestrictionStore
->loadByBlockId( $block->getId() );
206 $this->assertCount( 3, $restrictions );
211 * @covers ::restrictionsByBlockId
212 * @covers ::restrictionsToRemove
214 public function testUpdateInsert() {
215 $block = $this->insertBlock();
216 $pageFoo = $this->getExistingTestPage( 'Foo' );
217 $pageBar = $this->getExistingTestPage( 'Bar' );
218 $this->blockRestrictionStore
->insert( [
219 new PageRestriction( $block->getId(), $pageFoo->getId() ),
222 $this->blockRestrictionStore
->update( [
224 new PageRestriction( $block->getId(), $pageBar->getId() ),
225 new NamespaceRestriction( $block->getId(), NS_USER
),
228 $db = wfGetDb( DB_REPLICA
);
229 $result = $db->select(
230 [ 'ipblocks_restrictions' ],
232 [ 'ir_ipb_id' => $block->getId() ]
235 $this->assertEquals( 2, $result->numRows() );
236 $row = $result->fetchObject();
237 $this->assertEquals( $block->getId(), $row->ir_ipb_id
);
238 $this->assertEquals( $pageBar->getId(), $row->ir_value
);
243 * @covers ::restrictionsByBlockId
244 * @covers ::restrictionsToRemove
246 public function testUpdateChange() {
247 $block = $this->insertBlock();
248 $page = $this->getExistingTestPage( 'Foo' );
250 $this->blockRestrictionStore
->update( [
251 new PageRestriction( $block->getId(), $page->getId() ),
254 $db = wfGetDb( DB_REPLICA
);
255 $result = $db->select(
256 [ 'ipblocks_restrictions' ],
258 [ 'ir_ipb_id' => $block->getId() ]
261 $this->assertEquals( 1, $result->numRows() );
262 $row = $result->fetchObject();
263 $this->assertEquals( $block->getId(), $row->ir_ipb_id
);
264 $this->assertEquals( $page->getId(), $row->ir_value
);
269 * @covers ::restrictionsByBlockId
270 * @covers ::restrictionsToRemove
272 public function testUpdateNoRestrictions() {
273 $block = $this->insertBlock();
275 $this->blockRestrictionStore
->update( [] );
277 $db = wfGetDb( DB_REPLICA
);
278 $result = $db->select(
279 [ 'ipblocks_restrictions' ],
281 [ 'ir_ipb_id' => $block->getId() ]
284 $this->assertEquals( 0, $result->numRows() );
289 * @covers ::restrictionsByBlockId
290 * @covers ::restrictionsToRemove
292 public function testUpdateSame() {
293 $block = $this->insertBlock();
294 $page = $this->getExistingTestPage( 'Foo' );
295 $this->blockRestrictionStore
->insert( [
296 new PageRestriction( $block->getId(), $page->getId() ),
299 $this->blockRestrictionStore
->update( [
300 new PageRestriction( $block->getId(), $page->getId() ),
303 $db = wfGetDb( DB_REPLICA
);
304 $result = $db->select(
305 [ 'ipblocks_restrictions' ],
307 [ 'ir_ipb_id' => $block->getId() ]
310 $this->assertEquals( 1, $result->numRows() );
311 $row = $result->fetchObject();
312 $this->assertEquals( $block->getId(), $row->ir_ipb_id
);
313 $this->assertEquals( $page->getId(), $row->ir_value
);
317 * @covers ::updateByParentBlockId
319 public function testDeleteAllUpdateByParentBlockId() {
320 // Create a block and an autoblock (a child block)
321 $block = $this->insertBlock();
322 $pageFoo = $this->getExistingTestPage( 'Foo' );
323 $pageBar = $this->getExistingTestPage( 'Bar' );
324 $this->blockRestrictionStore
->insert( [
325 new PageRestriction( $block->getId(), $pageFoo->getId() ),
327 $autoblockId = $block->doAutoblock( '127.0.0.1' );
329 // Ensure that the restrictions on the block have not changed.
330 $restrictions = $this->blockRestrictionStore
->loadByBlockId( $block->getId() );
331 $this->assertCount( 1, $restrictions );
332 $this->assertEquals( $pageFoo->getId(), $restrictions[0]->getValue() );
334 // Ensure that the restrictions on the autoblock are the same as the block.
335 $restrictions = $this->blockRestrictionStore
->loadByBlockId( $autoblockId );
336 $this->assertCount( 1, $restrictions );
337 $this->assertEquals( $pageFoo->getId(), $restrictions[0]->getValue() );
339 // Update the restrictions on the autoblock (but leave the block unchanged)
340 $this->blockRestrictionStore
->updateByParentBlockId( $block->getId(), [
341 new PageRestriction( $block->getId(), $pageBar->getId() ),
344 // Ensure that the restrictions on the block have not changed.
345 $restrictions = $this->blockRestrictionStore
->loadByBlockId( $block->getId() );
346 $this->assertCount( 1, $restrictions );
347 $this->assertEquals( $pageFoo->getId(), $restrictions[0]->getValue() );
349 // Ensure that the restrictions on the autoblock have been updated.
350 $restrictions = $this->blockRestrictionStore
->loadByBlockId( $autoblockId );
351 $this->assertCount( 1, $restrictions );
352 $this->assertEquals( $pageBar->getId(), $restrictions[0]->getValue() );
356 * @covers ::updateByParentBlockId
358 public function testUpdateByParentBlockId() {
359 // Create a block and an autoblock (a child block)
360 $block = $this->insertBlock();
361 $page = $this->getExistingTestPage( 'Foo' );
362 $this->blockRestrictionStore
->insert( [
363 new PageRestriction( $block->getId(), $page->getId() ),
365 $autoblockId = $block->doAutoblock( '127.0.0.1' );
367 // Ensure that the restrictions on the block have not changed.
368 $restrictions = $this->blockRestrictionStore
->loadByBlockId( $block->getId() );
369 $this->assertCount( 1, $restrictions );
371 // Ensure that the restrictions on the autoblock have not changed.
372 $restrictions = $this->blockRestrictionStore
->loadByBlockId( $autoblockId );
373 $this->assertCount( 1, $restrictions );
375 // Remove the restrictions on the autoblock (but leave the block unchanged)
376 $this->blockRestrictionStore
->updateByParentBlockId( $block->getId(), [] );
378 // Ensure that the restrictions on the block have not changed.
379 $restrictions = $this->blockRestrictionStore
->loadByBlockId( $block->getId() );
380 $this->assertCount( 1, $restrictions );
382 // Ensure that the restrictions on the autoblock have been updated.
383 $restrictions = $this->blockRestrictionStore
->loadByBlockId( $autoblockId );
384 $this->assertCount( 0, $restrictions );
388 * @covers ::updateByParentBlockId
390 public function testNoAutoblocksUpdateByParentBlockId() {
391 // Create a block with no autoblock.
392 $block = $this->insertBlock();
393 $page = $this->getExistingTestPage( 'Foo' );
394 $this->blockRestrictionStore
->insert( [
395 new PageRestriction( $block->getId(), $page->getId() ),
398 // Ensure that the restrictions on the block have not changed.
399 $restrictions = $this->blockRestrictionStore
->loadByBlockId( $block->getId() );
400 $this->assertCount( 1, $restrictions );
402 // Update the restrictions on any autoblocks (there are none).
403 $this->blockRestrictionStore
->updateByParentBlockId( $block->getId(), $restrictions );
405 // Ensure that the restrictions on the block have not changed.
406 $restrictions = $this->blockRestrictionStore
->loadByBlockId( $block->getId() );
407 $this->assertCount( 1, $restrictions );
413 public function testDelete() {
414 $block = $this->insertBlock();
415 $page = $this->getExistingTestPage( 'Foo' );
416 $this->blockRestrictionStore
->insert( [
417 new PageRestriction( $block->getId(), $page->getId() ),
420 $restrictions = $this->blockRestrictionStore
->loadByBlockId( $block->getId() );
421 $this->assertCount( 1, $restrictions );
423 $result = $this->blockRestrictionStore
->delete(
424 array_merge( $restrictions, [ new \
stdClass() ] )
426 $this->assertTrue( $result );
428 $restrictions = $this->blockRestrictionStore
->loadByBlockId( $block->getId() );
429 $this->assertCount( 0, $restrictions );
433 * @covers ::deleteByBlockId
435 public function testDeleteByBlockId() {
436 $block = $this->insertBlock();
437 $page = $this->getExistingTestPage( 'Foo' );
438 $this->blockRestrictionStore
->insert( [
439 new PageRestriction( $block->getId(), $page->getId() ),
442 $restrictions = $this->blockRestrictionStore
->loadByBlockId( $block->getId() );
443 $this->assertCount( 1, $restrictions );
445 $result = $this->blockRestrictionStore
->deleteByBlockId( $block->getId() );
446 $this->assertNotFalse( $result );
448 $restrictions = $this->blockRestrictionStore
->loadByBlockId( $block->getId() );
449 $this->assertCount( 0, $restrictions );
453 * @covers ::deleteByParentBlockId
455 public function testDeleteByParentBlockId() {
456 // Create a block with no autoblock.
457 $block = $this->insertBlock();
458 $page = $this->getExistingTestPage( 'Foo' );
459 $this->blockRestrictionStore
->insert( [
460 new PageRestriction( $block->getId(), $page->getId() ),
462 $autoblockId = $block->doAutoblock( '127.0.0.1' );
464 // Ensure that the restrictions on the block have not changed.
465 $restrictions = $this->blockRestrictionStore
->loadByBlockId( $block->getId() );
466 $this->assertCount( 1, $restrictions );
468 // Ensure that the restrictions on the autoblock are the same as the block.
469 $restrictions = $this->blockRestrictionStore
->loadByBlockId( $autoblockId );
470 $this->assertCount( 1, $restrictions );
472 // Remove all of the restrictions on the autoblock (but leave the block unchanged).
473 $result = $this->blockRestrictionStore
->deleteByParentBlockId( $block->getId() );
474 // NOTE: commented out until https://gerrit.wikimedia.org/r/c/mediawiki/core/+/469324 is merged
475 //$this->assertTrue( $result );
477 // Ensure that the restrictions on the block have not changed.
478 $restrictions = $this->blockRestrictionStore
->loadByBlockId( $block->getId() );
479 $this->assertCount( 1, $restrictions );
481 // Ensure that the restrictions on the autoblock have been removed.
482 $restrictions = $this->blockRestrictionStore
->loadByBlockId( $autoblockId );
483 $this->assertCount( 0, $restrictions );
488 * @dataProvider equalsDataProvider
492 * @param bool $expected
494 public function testEquals( array $a, array $b, $expected ) {
495 $this->assertSame( $expected, $this->blockRestrictionStore
->equals( $a, $b ) );
498 public function equalsDataProvider() {
503 new PageRestriction( 1, 1 ),
507 new PageRestriction( 1, 2 )
513 new PageRestriction( 1, 1 ),
516 new PageRestriction( 1, 1 ),
517 new PageRestriction( 1, 2 )
528 new PageRestriction( 1, 1 ),
529 new PageRestriction( 1, 2 ),
530 new PageRestriction( 2, 3 ),
533 new PageRestriction( 2, 3 ),
534 new PageRestriction( 1, 2 ),
535 new PageRestriction( 1, 1 ),
541 new NamespaceRestriction( 1, NS_USER
),
544 new NamespaceRestriction( 1, NS_USER
),
550 new NamespaceRestriction( 1, NS_USER
),
553 new NamespaceRestriction( 1, NS_TALK
),
561 * @covers ::setBlockId
563 public function testSetBlockId() {
566 new PageRestriction( 1, 1 ),
567 new PageRestriction( 1, 2 ),
568 new NamespaceRestriction( 1, NS_USER
),
571 $this->assertSame( 1, $restrictions[1]->getBlockId() );
572 $this->assertSame( 1, $restrictions[2]->getBlockId() );
573 $this->assertSame( 1, $restrictions[3]->getBlockId() );
575 $result = $this->blockRestrictionStore
->setBlockId( 2, $restrictions );
577 foreach ( $result as $restriction ) {
578 $this->assertSame( 2, $restriction->getBlockId() );
582 protected function insertBlock() {
583 $badActor = $this->getTestUser()->getUser();
584 $sysop = $this->getTestSysop()->getUser();
586 $block = new \
Block( [
587 'address' => $badActor->getName(),
588 'user' => $badActor->getId(),
589 'by' => $sysop->getId(),
590 'expiry' => 'infinity',
592 'enableAutoblock' => true,
600 protected function insertRestriction( $blockId, $type, $value ) {
601 $this->db
->insert( 'ipblocks_restrictions', [
602 'ir_ipb_id' => $blockId,
604 'ir_value' => $value,
608 protected function resetTables() {
609 $this->db
->delete( 'ipblocks', '*', __METHOD__
);
610 $this->db
->delete( 'ipblocks_restrictions', '*', __METHOD__
);