3 namespace MediaWiki\Tests\Block
;
5 use MediaWiki\Block\BlockRestriction
;
6 use MediaWiki\Block\Restriction\NamespaceRestriction
;
7 use MediaWiki\Block\Restriction\PageRestriction
;
8 use MediaWiki\Block\Restriction\Restriction
;
13 * @coversDefaultClass \MediaWiki\Block\BlockRestriction
15 class BlockRestrictionTest
extends \MediaWikiLangTestCase
{
17 public function tearDown() {
23 * @covers ::loadByBlockId
24 * @covers ::resultToRestrictions
25 * @covers ::rowToRestriction
27 public function testLoadMultipleRestrictions() {
28 $block = $this->insertBlock();
30 $pageFoo = $this->getExistingTestPage( 'Foo' );
31 $pageBar = $this->getExistingTestPage( 'Bar' );
33 BlockRestriction
::insert( [
34 new PageRestriction( $block->getId(), $pageFoo->getId() ),
35 new PageRestriction( $block->getId(), $pageBar->getId() ),
36 new NamespaceRestriction( $block->getId(), NS_USER
),
39 $restrictions = BlockRestriction
::loadByBlockId( $block->getId() );
41 $this->assertCount( 3, $restrictions );
45 * @covers ::loadByBlockId
46 * @covers ::resultToRestrictions
47 * @covers ::rowToRestriction
49 public function testWithNoRestrictions() {
50 $block = $this->insertBlock();
51 $restrictions = BlockRestriction
::loadByBlockId( $block->getId() );
52 $this->assertEmpty( $restrictions );
56 * @covers ::loadByBlockId
57 * @covers ::resultToRestrictions
58 * @covers ::rowToRestriction
60 public function testWithEmptyParam() {
61 $restrictions = BlockRestriction
::loadByBlockId( [] );
63 $this->assertEmpty( $restrictions );
67 * @covers ::loadByBlockId
68 * @covers ::resultToRestrictions
69 * @covers ::rowToRestriction
71 public function testIgnoreNotSupportedTypes() {
72 $block = $this->insertBlock();
74 $pageFoo = $this->getExistingTestPage( 'Foo' );
75 $pageBar = $this->getExistingTestPage( 'Bar' );
78 $this->insertRestriction( $block->getId(), PageRestriction
::TYPE_ID
, $pageFoo->getId() );
79 $this->insertRestriction( $block->getId(), NamespaceRestriction
::TYPE_ID
, NS_USER
);
82 $this->insertRestriction( $block->getId(), 9, $pageBar->getId() );
83 $this->insertRestriction( $block->getId(), 10, NS_FILE
);
85 $restrictions = BlockRestriction
::loadByBlockId( $block->getId() );
86 $this->assertCount( 2, $restrictions );
90 * @covers ::loadByBlockId
91 * @covers ::resultToRestrictions
92 * @covers ::rowToRestriction
94 public function testMappingPageRestrictionObject() {
95 $block = $this->insertBlock();
96 $title = 'Lady Macbeth';
97 $page = $this->getExistingTestPage( $title );
99 // Test Page Restrictions.
100 BlockRestriction
::insert( [
101 new PageRestriction( $block->getId(), $page->getId() ),
104 $restrictions = BlockRestriction
::loadByBlockId( $block->getId() );
106 list( $pageRestriction ) = $restrictions;
107 $this->assertInstanceOf( PageRestriction
::class, $pageRestriction );
108 $this->assertEquals( $block->getId(), $pageRestriction->getBlockId() );
109 $this->assertEquals( $page->getId(), $pageRestriction->getValue() );
110 $this->assertEquals( $pageRestriction->getType(), PageRestriction
::TYPE
);
111 $this->assertEquals( $pageRestriction->getTitle()->getText(), $title );
115 * @covers ::loadByBlockId
116 * @covers ::resultToRestrictions
117 * @covers ::rowToRestriction
119 public function testMappingNamespaceRestrictionObject() {
120 $block = $this->insertBlock();
122 BlockRestriction
::insert( [
123 new NamespaceRestriction( $block->getId(), NS_USER
),
126 $restrictions = BlockRestriction
::loadByBlockId( $block->getId() );
128 list( $namespaceRestriction ) = $restrictions;
129 $this->assertInstanceOf( NamespaceRestriction
::class, $namespaceRestriction );
130 $this->assertEquals( $block->getId(), $namespaceRestriction->getBlockId() );
131 $this->assertSame( NS_USER
, $namespaceRestriction->getValue() );
132 $this->assertEquals( $namespaceRestriction->getType(), NamespaceRestriction
::TYPE
);
138 public function testInsert() {
139 $block = $this->insertBlock();
141 $pageFoo = $this->getExistingTestPage( 'Foo' );
142 $pageBar = $this->getExistingTestPage( 'Bar' );
146 new PageRestriction( $block->getId(), $pageFoo->getId() ),
147 new PageRestriction( $block->getId(), $pageBar->getId() ),
148 new NamespaceRestriction( $block->getId(), NS_USER
)
151 $result = BlockRestriction
::insert( $restrictions );
152 $this->assertTrue( $result );
158 $result = BlockRestriction
::insert( $restrictions );
159 $this->assertFalse( $result );
161 $result = BlockRestriction
::insert( [] );
162 $this->assertFalse( $result );
168 public function testInsertTypes() {
169 $block = $this->insertBlock();
171 $pageFoo = $this->getExistingTestPage( 'Foo' );
172 $pageBar = $this->getExistingTestPage( 'Bar' );
174 $invalid = $this->createMock( Restriction
::class );
175 $invalid->method( 'toRow' )
177 'ir_ipb_id' => $block->getId(),
184 new PageRestriction( $block->getId(), $pageFoo->getId() ),
185 new PageRestriction( $block->getId(), $pageBar->getId() ),
186 new NamespaceRestriction( $block->getId(), NS_USER
),
190 $result = BlockRestriction
::insert( $restrictions );
191 $this->assertTrue( $result );
193 $restrictions = BlockRestriction
::loadByBlockId( $block->getId() );
194 $this->assertCount( 3, $restrictions );
199 * @covers ::restrictionsByBlockId
200 * @covers ::restrictionsToRemove
202 public function testUpdateInsert() {
203 $block = $this->insertBlock();
204 $pageFoo = $this->getExistingTestPage( 'Foo' );
205 $pageBar = $this->getExistingTestPage( 'Bar' );
206 BlockRestriction
::insert( [
207 new PageRestriction( $block->getId(), $pageFoo->getId() ),
210 BlockRestriction
::update( [
212 new PageRestriction( $block->getId(), $pageBar->getId() ),
213 new NamespaceRestriction( $block->getId(), NS_USER
),
216 $db = wfGetDb( DB_REPLICA
);
217 $result = $db->select(
218 [ 'ipblocks_restrictions' ],
220 [ 'ir_ipb_id' => $block->getId() ]
223 $this->assertEquals( 2, $result->numRows() );
224 $row = $result->fetchObject();
225 $this->assertEquals( $block->getId(), $row->ir_ipb_id
);
226 $this->assertEquals( $pageBar->getId(), $row->ir_value
);
231 * @covers ::restrictionsByBlockId
232 * @covers ::restrictionsToRemove
234 public function testUpdateChange() {
235 $block = $this->insertBlock();
236 $page = $this->getExistingTestPage( 'Foo' );
238 BlockRestriction
::update( [
239 new PageRestriction( $block->getId(), $page->getId() ),
242 $db = wfGetDb( DB_REPLICA
);
243 $result = $db->select(
244 [ 'ipblocks_restrictions' ],
246 [ 'ir_ipb_id' => $block->getId() ]
249 $this->assertEquals( 1, $result->numRows() );
250 $row = $result->fetchObject();
251 $this->assertEquals( $block->getId(), $row->ir_ipb_id
);
252 $this->assertEquals( $page->getId(), $row->ir_value
);
257 * @covers ::restrictionsByBlockId
258 * @covers ::restrictionsToRemove
260 public function testUpdateNoRestrictions() {
261 $block = $this->insertBlock();
263 BlockRestriction
::update( [] );
265 $db = wfGetDb( DB_REPLICA
);
266 $result = $db->select(
267 [ 'ipblocks_restrictions' ],
269 [ 'ir_ipb_id' => $block->getId() ]
272 $this->assertEquals( 0, $result->numRows() );
277 * @covers ::restrictionsByBlockId
278 * @covers ::restrictionsToRemove
280 public function testUpdateSame() {
281 $block = $this->insertBlock();
282 $page = $this->getExistingTestPage( 'Foo' );
283 BlockRestriction
::insert( [
284 new PageRestriction( $block->getId(), $page->getId() ),
287 BlockRestriction
::update( [
288 new PageRestriction( $block->getId(), $page->getId() ),
291 $db = wfGetDb( DB_REPLICA
);
292 $result = $db->select(
293 [ 'ipblocks_restrictions' ],
295 [ 'ir_ipb_id' => $block->getId() ]
298 $this->assertEquals( 1, $result->numRows() );
299 $row = $result->fetchObject();
300 $this->assertEquals( $block->getId(), $row->ir_ipb_id
);
301 $this->assertEquals( $page->getId(), $row->ir_value
);
305 * @covers ::updateByParentBlockId
307 public function testDeleteAllUpdateByParentBlockId() {
308 // Create a block and an autoblock (a child block)
309 $block = $this->insertBlock();
310 $pageFoo = $this->getExistingTestPage( 'Foo' );
311 $pageBar = $this->getExistingTestPage( 'Bar' );
312 BlockRestriction
::insert( [
313 new PageRestriction( $block->getId(), $pageFoo->getId() ),
315 $autoblockId = $block->doAutoblock( '127.0.0.1' );
317 // Ensure that the restrictions on the block have not changed.
318 $restrictions = BlockRestriction
::loadByBlockId( $block->getId() );
319 $this->assertCount( 1, $restrictions );
320 $this->assertEquals( $pageFoo->getId(), $restrictions[0]->getValue() );
322 // Ensure that the restrictions on the autoblock are the same as the block.
323 $restrictions = BlockRestriction
::loadByBlockId( $autoblockId );
324 $this->assertCount( 1, $restrictions );
325 $this->assertEquals( $pageFoo->getId(), $restrictions[0]->getValue() );
327 // Update the restrictions on the autoblock (but leave the block unchanged)
328 BlockRestriction
::updateByParentBlockId( $block->getId(), [
329 new PageRestriction( $block->getId(), $pageBar->getId() ),
332 // Ensure that the restrictions on the block have not changed.
333 $restrictions = BlockRestriction
::loadByBlockId( $block->getId() );
334 $this->assertCount( 1, $restrictions );
335 $this->assertEquals( $pageFoo->getId(), $restrictions[0]->getValue() );
337 // Ensure that the restrictions on the autoblock have been updated.
338 $restrictions = BlockRestriction
::loadByBlockId( $autoblockId );
339 $this->assertCount( 1, $restrictions );
340 $this->assertEquals( $pageBar->getId(), $restrictions[0]->getValue() );
344 * @covers ::updateByParentBlockId
346 public function testUpdateByParentBlockId() {
347 // Create a block and an autoblock (a child block)
348 $block = $this->insertBlock();
349 $page = $this->getExistingTestPage( 'Foo' );
350 BlockRestriction
::insert( [
351 new PageRestriction( $block->getId(), $page->getId() ),
353 $autoblockId = $block->doAutoblock( '127.0.0.1' );
355 // Ensure that the restrictions on the block have not changed.
356 $restrictions = BlockRestriction
::loadByBlockId( $block->getId() );
357 $this->assertCount( 1, $restrictions );
359 // Ensure that the restrictions on the autoblock have not changed.
360 $restrictions = BlockRestriction
::loadByBlockId( $autoblockId );
361 $this->assertCount( 1, $restrictions );
363 // Remove the restrictions on the autoblock (but leave the block unchanged)
364 BlockRestriction
::updateByParentBlockId( $block->getId(), [] );
366 // Ensure that the restrictions on the block have not changed.
367 $restrictions = BlockRestriction
::loadByBlockId( $block->getId() );
368 $this->assertCount( 1, $restrictions );
370 // Ensure that the restrictions on the autoblock have been updated.
371 $restrictions = BlockRestriction
::loadByBlockId( $autoblockId );
372 $this->assertCount( 0, $restrictions );
376 * @covers ::updateByParentBlockId
378 public function testNoAutoblocksUpdateByParentBlockId() {
379 // Create a block with no autoblock.
380 $block = $this->insertBlock();
381 $page = $this->getExistingTestPage( 'Foo' );
382 BlockRestriction
::insert( [
383 new PageRestriction( $block->getId(), $page->getId() ),
386 // Ensure that the restrictions on the block have not changed.
387 $restrictions = BlockRestriction
::loadByBlockId( $block->getId() );
388 $this->assertCount( 1, $restrictions );
390 // Update the restrictions on any autoblocks (there are none).
391 BlockRestriction
::updateByParentBlockId( $block->getId(), $restrictions );
393 // Ensure that the restrictions on the block have not changed.
394 $restrictions = BlockRestriction
::loadByBlockId( $block->getId() );
395 $this->assertCount( 1, $restrictions );
401 public function testDelete() {
402 $block = $this->insertBlock();
403 $page = $this->getExistingTestPage( 'Foo' );
404 BlockRestriction
::insert( [
405 new PageRestriction( $block->getId(), $page->getId() ),
408 $restrictions = BlockRestriction
::loadByBlockId( $block->getId() );
409 $this->assertCount( 1, $restrictions );
411 $result = BlockRestriction
::delete( array_merge( $restrictions, [ new \
stdClass() ] ) );
412 $this->assertTrue( $result );
414 $restrictions = BlockRestriction
::loadByBlockId( $block->getId() );
415 $this->assertCount( 0, $restrictions );
419 * @covers ::deleteByBlockId
421 public function testDeleteByBlockId() {
422 $block = $this->insertBlock();
423 $page = $this->getExistingTestPage( 'Foo' );
424 BlockRestriction
::insert( [
425 new PageRestriction( $block->getId(), $page->getId() ),
428 $restrictions = BlockRestriction
::loadByBlockId( $block->getId() );
429 $this->assertCount( 1, $restrictions );
431 $result = BlockRestriction
::deleteByBlockId( $block->getId() );
432 $this->assertNotFalse( $result );
434 $restrictions = BlockRestriction
::loadByBlockId( $block->getId() );
435 $this->assertCount( 0, $restrictions );
439 * @covers ::deleteByParentBlockId
441 public function testDeleteByParentBlockId() {
442 // Create a block with no autoblock.
443 $block = $this->insertBlock();
444 $page = $this->getExistingTestPage( 'Foo' );
445 BlockRestriction
::insert( [
446 new PageRestriction( $block->getId(), $page->getId() ),
448 $autoblockId = $block->doAutoblock( '127.0.0.1' );
450 // Ensure that the restrictions on the block have not changed.
451 $restrictions = BlockRestriction
::loadByBlockId( $block->getId() );
452 $this->assertCount( 1, $restrictions );
454 // Ensure that the restrictions on the autoblock are the same as the block.
455 $restrictions = BlockRestriction
::loadByBlockId( $autoblockId );
456 $this->assertCount( 1, $restrictions );
458 // Remove all of the restrictions on the autoblock (but leave the block unchanged).
459 $result = BlockRestriction
::deleteByParentBlockId( $block->getId() );
460 // NOTE: commented out until https://gerrit.wikimedia.org/r/c/mediawiki/core/+/469324 is merged
461 //$this->assertTrue( $result );
463 // Ensure that the restrictions on the block have not changed.
464 $restrictions = BlockRestriction
::loadByBlockId( $block->getId() );
465 $this->assertCount( 1, $restrictions );
467 // Ensure that the restrictions on the autoblock have been removed.
468 $restrictions = BlockRestriction
::loadByBlockId( $autoblockId );
469 $this->assertCount( 0, $restrictions );
474 * @dataProvider equalsDataProvider
478 * @param bool $expected
480 public function testEquals( array $a, array $b, $expected ) {
481 $this->assertSame( $expected, BlockRestriction
::equals( $a, $b ) );
484 public function equalsDataProvider() {
489 new PageRestriction( 1, 1 ),
493 new PageRestriction( 1, 2 )
499 new PageRestriction( 1, 1 ),
502 new PageRestriction( 1, 1 ),
503 new PageRestriction( 1, 2 )
514 new PageRestriction( 1, 1 ),
515 new PageRestriction( 1, 2 ),
516 new PageRestriction( 2, 3 ),
519 new PageRestriction( 2, 3 ),
520 new PageRestriction( 1, 2 ),
521 new PageRestriction( 1, 1 ),
527 new NamespaceRestriction( 1, NS_USER
),
530 new NamespaceRestriction( 1, NS_USER
),
536 new NamespaceRestriction( 1, NS_USER
),
539 new NamespaceRestriction( 1, NS_TALK
),
547 * @covers ::setBlockId
549 public function testSetBlockId() {
552 new PageRestriction( 1, 1 ),
553 new PageRestriction( 1, 2 ),
554 new NamespaceRestriction( 1, NS_USER
),
557 $this->assertSame( 1, $restrictions[1]->getBlockId() );
558 $this->assertSame( 1, $restrictions[2]->getBlockId() );
559 $this->assertSame( 1, $restrictions[3]->getBlockId() );
561 $result = BlockRestriction
::setBlockId( 2, $restrictions );
563 foreach ( $result as $restriction ) {
564 $this->assertSame( 2, $restriction->getBlockId() );
568 protected function insertBlock() {
569 $badActor = $this->getTestUser()->getUser();
570 $sysop = $this->getTestSysop()->getUser();
572 $block = new \
Block( [
573 'address' => $badActor->getName(),
574 'user' => $badActor->getId(),
575 'by' => $sysop->getId(),
576 'expiry' => 'infinity',
578 'enableAutoblock' => true,
586 protected function insertRestriction( $blockId, $type, $value ) {
587 $this->db
->insert( 'ipblocks_restrictions', [
588 'ir_ipb_id' => $blockId,
590 'ir_value' => $value,
594 protected function resetTables() {
595 $this->db
->delete( 'ipblocks', '*', __METHOD__
);
596 $this->db
->delete( 'ipblocks_restrictions', '*', __METHOD__
);