2018-08-16 04:55:55 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace MediaWiki\Tests\Block;
|
|
|
|
|
|
2019-04-11 19:54:10 +00:00
|
|
|
use MediaWiki\Block\BlockRestrictionStore;
|
2019-05-13 14:18:07 +00:00
|
|
|
use MediaWiki\Block\DatabaseBlock;
|
2018-10-30 18:19:22 +00:00
|
|
|
use MediaWiki\Block\Restriction\NamespaceRestriction;
|
2018-08-16 04:55:55 +00:00
|
|
|
use MediaWiki\Block\Restriction\PageRestriction;
|
|
|
|
|
use MediaWiki\Block\Restriction\Restriction;
|
2022-07-19 10:57:01 +00:00
|
|
|
use MediaWiki\MainConfigNames;
|
2018-08-16 04:55:55 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @group Database
|
|
|
|
|
* @group Blocking
|
2019-04-11 19:54:10 +00:00
|
|
|
* @coversDefaultClass \MediaWiki\Block\BlockRestrictionStore
|
2018-08-16 04:55:55 +00:00
|
|
|
*/
|
2019-04-11 19:54:10 +00:00
|
|
|
class BlockRestrictionStoreTest extends \MediaWikiLangTestCase {
|
|
|
|
|
|
|
|
|
|
/** @var BlockRestrictionStore */
|
|
|
|
|
protected $blockRestrictionStore;
|
|
|
|
|
|
2021-07-22 03:11:47 +00:00
|
|
|
protected function setUp(): void {
|
2019-04-11 19:54:10 +00:00
|
|
|
parent::setUp();
|
|
|
|
|
|
2022-01-12 20:13:39 +00:00
|
|
|
$this->blockRestrictionStore = $this->getServiceContainer()->getBlockRestrictionStore();
|
2019-04-11 19:54:10 +00:00
|
|
|
}
|
2018-08-16 04:55:55 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers ::loadByBlockId
|
|
|
|
|
* @covers ::resultToRestrictions
|
|
|
|
|
* @covers ::rowToRestriction
|
|
|
|
|
*/
|
|
|
|
|
public function testLoadMultipleRestrictions() {
|
2022-07-19 10:57:01 +00:00
|
|
|
$this->overrideConfigValue( MainConfigNames::BlockDisablesLogin, false );
|
2018-08-16 04:55:55 +00:00
|
|
|
$block = $this->insertBlock();
|
|
|
|
|
|
|
|
|
|
$pageFoo = $this->getExistingTestPage( 'Foo' );
|
|
|
|
|
$pageBar = $this->getExistingTestPage( 'Bar' );
|
|
|
|
|
|
2019-04-11 19:54:10 +00:00
|
|
|
$this->blockRestrictionStore->insert( [
|
2018-10-30 18:19:22 +00:00
|
|
|
new PageRestriction( $block->getId(), $pageFoo->getId() ),
|
|
|
|
|
new PageRestriction( $block->getId(), $pageBar->getId() ),
|
|
|
|
|
new NamespaceRestriction( $block->getId(), NS_USER ),
|
2018-08-16 04:55:55 +00:00
|
|
|
] );
|
|
|
|
|
|
2019-04-11 19:54:10 +00:00
|
|
|
$restrictions = $this->blockRestrictionStore->loadByBlockId( $block->getId() );
|
2018-08-16 04:55:55 +00:00
|
|
|
|
2018-10-30 18:19:22 +00:00
|
|
|
$this->assertCount( 3, $restrictions );
|
2018-08-16 04:55:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers ::loadByBlockId
|
|
|
|
|
* @covers ::resultToRestrictions
|
|
|
|
|
* @covers ::rowToRestriction
|
|
|
|
|
*/
|
|
|
|
|
public function testWithNoRestrictions() {
|
|
|
|
|
$block = $this->insertBlock();
|
2019-04-11 19:54:10 +00:00
|
|
|
$restrictions = $this->blockRestrictionStore->loadByBlockId( $block->getId() );
|
2020-02-28 15:45:22 +00:00
|
|
|
$this->assertSame( [], $restrictions );
|
2018-08-16 04:55:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers ::loadByBlockId
|
|
|
|
|
* @covers ::resultToRestrictions
|
|
|
|
|
* @covers ::rowToRestriction
|
|
|
|
|
*/
|
|
|
|
|
public function testWithEmptyParam() {
|
2019-04-11 19:54:10 +00:00
|
|
|
$restrictions = $this->blockRestrictionStore->loadByBlockId( [] );
|
2020-02-28 15:45:22 +00:00
|
|
|
$this->assertSame( [], $restrictions );
|
2018-08-16 04:55:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers ::loadByBlockId
|
|
|
|
|
* @covers ::resultToRestrictions
|
|
|
|
|
* @covers ::rowToRestriction
|
|
|
|
|
*/
|
|
|
|
|
public function testIgnoreNotSupportedTypes() {
|
|
|
|
|
$block = $this->insertBlock();
|
|
|
|
|
|
|
|
|
|
$pageFoo = $this->getExistingTestPage( 'Foo' );
|
|
|
|
|
$pageBar = $this->getExistingTestPage( 'Bar' );
|
|
|
|
|
|
|
|
|
|
// valid type
|
|
|
|
|
$this->insertRestriction( $block->getId(), PageRestriction::TYPE_ID, $pageFoo->getId() );
|
2018-10-30 18:19:22 +00:00
|
|
|
$this->insertRestriction( $block->getId(), NamespaceRestriction::TYPE_ID, NS_USER );
|
2018-08-16 04:55:55 +00:00
|
|
|
|
|
|
|
|
// invalid type
|
|
|
|
|
$this->insertRestriction( $block->getId(), 9, $pageBar->getId() );
|
2018-10-30 18:19:22 +00:00
|
|
|
$this->insertRestriction( $block->getId(), 10, NS_FILE );
|
2018-08-16 04:55:55 +00:00
|
|
|
|
2019-04-11 19:54:10 +00:00
|
|
|
$restrictions = $this->blockRestrictionStore->loadByBlockId( $block->getId() );
|
2018-10-30 18:19:22 +00:00
|
|
|
$this->assertCount( 2, $restrictions );
|
2018-08-16 04:55:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers ::loadByBlockId
|
|
|
|
|
* @covers ::resultToRestrictions
|
|
|
|
|
* @covers ::rowToRestriction
|
|
|
|
|
*/
|
2018-10-30 18:19:22 +00:00
|
|
|
public function testMappingPageRestrictionObject() {
|
2018-08-16 04:55:55 +00:00
|
|
|
$block = $this->insertBlock();
|
|
|
|
|
$title = 'Lady Macbeth';
|
|
|
|
|
$page = $this->getExistingTestPage( $title );
|
|
|
|
|
|
2018-10-30 18:19:22 +00:00
|
|
|
// Test Page Restrictions.
|
2019-04-11 19:54:10 +00:00
|
|
|
$this->blockRestrictionStore->insert( [
|
2018-10-30 18:19:22 +00:00
|
|
|
new PageRestriction( $block->getId(), $page->getId() ),
|
2018-08-16 04:55:55 +00:00
|
|
|
] );
|
|
|
|
|
|
2019-04-11 19:54:10 +00:00
|
|
|
$restrictions = $this->blockRestrictionStore->loadByBlockId( $block->getId() );
|
2018-08-16 04:55:55 +00:00
|
|
|
|
2022-10-21 04:32:38 +00:00
|
|
|
[ $pageRestriction ] = $restrictions;
|
2018-08-16 04:55:55 +00:00
|
|
|
$this->assertInstanceOf( PageRestriction::class, $pageRestriction );
|
|
|
|
|
$this->assertEquals( $block->getId(), $pageRestriction->getBlockId() );
|
|
|
|
|
$this->assertEquals( $page->getId(), $pageRestriction->getValue() );
|
2023-03-11 19:04:01 +00:00
|
|
|
$this->assertEquals( PageRestriction::TYPE, $pageRestriction->getType() );
|
2018-08-16 04:55:55 +00:00
|
|
|
$this->assertEquals( $pageRestriction->getTitle()->getText(), $title );
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-30 18:19:22 +00:00
|
|
|
/**
|
|
|
|
|
* @covers ::loadByBlockId
|
|
|
|
|
* @covers ::resultToRestrictions
|
|
|
|
|
* @covers ::rowToRestriction
|
|
|
|
|
*/
|
|
|
|
|
public function testMappingNamespaceRestrictionObject() {
|
|
|
|
|
$block = $this->insertBlock();
|
|
|
|
|
|
2019-04-11 19:54:10 +00:00
|
|
|
$this->blockRestrictionStore->insert( [
|
2018-10-30 18:19:22 +00:00
|
|
|
new NamespaceRestriction( $block->getId(), NS_USER ),
|
|
|
|
|
] );
|
|
|
|
|
|
2019-04-11 19:54:10 +00:00
|
|
|
$restrictions = $this->blockRestrictionStore->loadByBlockId( $block->getId() );
|
2018-10-30 18:19:22 +00:00
|
|
|
|
2022-10-21 04:32:38 +00:00
|
|
|
[ $namespaceRestriction ] = $restrictions;
|
2018-10-30 18:19:22 +00:00
|
|
|
$this->assertInstanceOf( NamespaceRestriction::class, $namespaceRestriction );
|
|
|
|
|
$this->assertEquals( $block->getId(), $namespaceRestriction->getBlockId() );
|
|
|
|
|
$this->assertSame( NS_USER, $namespaceRestriction->getValue() );
|
2023-03-11 19:04:01 +00:00
|
|
|
$this->assertEquals( NamespaceRestriction::TYPE, $namespaceRestriction->getType() );
|
2018-10-30 18:19:22 +00:00
|
|
|
}
|
|
|
|
|
|
2018-08-16 04:55:55 +00:00
|
|
|
/**
|
|
|
|
|
* @covers ::insert
|
|
|
|
|
*/
|
|
|
|
|
public function testInsert() {
|
|
|
|
|
$block = $this->insertBlock();
|
|
|
|
|
|
|
|
|
|
$pageFoo = $this->getExistingTestPage( 'Foo' );
|
|
|
|
|
$pageBar = $this->getExistingTestPage( 'Bar' );
|
|
|
|
|
|
|
|
|
|
$restrictions = [
|
|
|
|
|
new PageRestriction( $block->getId(), $pageFoo->getId() ),
|
|
|
|
|
new PageRestriction( $block->getId(), $pageBar->getId() ),
|
2018-10-30 18:19:22 +00:00
|
|
|
new NamespaceRestriction( $block->getId(), NS_USER )
|
2018-08-16 04:55:55 +00:00
|
|
|
];
|
|
|
|
|
|
2019-04-11 19:54:10 +00:00
|
|
|
$result = $this->blockRestrictionStore->insert( $restrictions );
|
2018-08-16 04:55:55 +00:00
|
|
|
$this->assertTrue( $result );
|
|
|
|
|
|
2019-04-11 19:54:10 +00:00
|
|
|
$result = $this->blockRestrictionStore->insert( [] );
|
2018-08-16 04:55:55 +00:00
|
|
|
$this->assertFalse( $result );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers ::insert
|
|
|
|
|
*/
|
|
|
|
|
public function testInsertTypes() {
|
|
|
|
|
$block = $this->insertBlock();
|
|
|
|
|
|
|
|
|
|
$pageFoo = $this->getExistingTestPage( 'Foo' );
|
|
|
|
|
$pageBar = $this->getExistingTestPage( 'Bar' );
|
|
|
|
|
|
|
|
|
|
$invalid = $this->createMock( Restriction::class );
|
|
|
|
|
$invalid->method( 'toRow' )
|
|
|
|
|
->willReturn( [
|
|
|
|
|
'ir_ipb_id' => $block->getId(),
|
|
|
|
|
'ir_type' => 9,
|
|
|
|
|
'ir_value' => 42,
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$restrictions = [
|
|
|
|
|
new PageRestriction( $block->getId(), $pageFoo->getId() ),
|
|
|
|
|
new PageRestriction( $block->getId(), $pageBar->getId() ),
|
2018-10-30 18:19:22 +00:00
|
|
|
new NamespaceRestriction( $block->getId(), NS_USER ),
|
2018-08-16 04:55:55 +00:00
|
|
|
$invalid,
|
|
|
|
|
];
|
|
|
|
|
|
2019-04-11 19:54:10 +00:00
|
|
|
$result = $this->blockRestrictionStore->insert( $restrictions );
|
2018-08-16 04:55:55 +00:00
|
|
|
$this->assertTrue( $result );
|
|
|
|
|
|
2019-04-11 19:54:10 +00:00
|
|
|
$restrictions = $this->blockRestrictionStore->loadByBlockId( $block->getId() );
|
2018-10-30 18:19:22 +00:00
|
|
|
$this->assertCount( 3, $restrictions );
|
2018-08-16 04:55:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers ::update
|
|
|
|
|
* @covers ::restrictionsByBlockId
|
|
|
|
|
* @covers ::restrictionsToRemove
|
|
|
|
|
*/
|
|
|
|
|
public function testUpdateInsert() {
|
|
|
|
|
$block = $this->insertBlock();
|
|
|
|
|
$pageFoo = $this->getExistingTestPage( 'Foo' );
|
|
|
|
|
$pageBar = $this->getExistingTestPage( 'Bar' );
|
2019-04-11 19:54:10 +00:00
|
|
|
$this->blockRestrictionStore->insert( [
|
2018-08-16 04:55:55 +00:00
|
|
|
new PageRestriction( $block->getId(), $pageFoo->getId() ),
|
|
|
|
|
] );
|
|
|
|
|
|
2019-04-11 19:54:10 +00:00
|
|
|
$this->blockRestrictionStore->update( [
|
2018-08-16 04:55:55 +00:00
|
|
|
new PageRestriction( $block->getId(), $pageBar->getId() ),
|
2018-10-30 18:19:22 +00:00
|
|
|
new NamespaceRestriction( $block->getId(), NS_USER ),
|
2018-08-16 04:55:55 +00:00
|
|
|
] );
|
|
|
|
|
|
2023-09-25 18:49:16 +00:00
|
|
|
$result = $this->getDb()->newSelectQueryBuilder()
|
2023-07-14 15:19:40 +00:00
|
|
|
->select( [ '*' ] )
|
|
|
|
|
->from( 'ipblocks_restrictions' )
|
|
|
|
|
->where( [ 'ir_ipb_id' => $block->getId() ] )
|
|
|
|
|
->fetchResultSet();
|
2018-08-16 04:55:55 +00:00
|
|
|
|
2018-10-30 18:19:22 +00:00
|
|
|
$this->assertEquals( 2, $result->numRows() );
|
2018-08-16 04:55:55 +00:00
|
|
|
$row = $result->fetchObject();
|
|
|
|
|
$this->assertEquals( $block->getId(), $row->ir_ipb_id );
|
|
|
|
|
$this->assertEquals( $pageBar->getId(), $row->ir_value );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers ::update
|
|
|
|
|
* @covers ::restrictionsByBlockId
|
|
|
|
|
* @covers ::restrictionsToRemove
|
|
|
|
|
*/
|
|
|
|
|
public function testUpdateChange() {
|
|
|
|
|
$block = $this->insertBlock();
|
|
|
|
|
$page = $this->getExistingTestPage( 'Foo' );
|
|
|
|
|
|
2019-04-11 19:54:10 +00:00
|
|
|
$this->blockRestrictionStore->update( [
|
2018-08-16 04:55:55 +00:00
|
|
|
new PageRestriction( $block->getId(), $page->getId() ),
|
|
|
|
|
] );
|
|
|
|
|
|
2023-09-25 18:49:16 +00:00
|
|
|
$result = $this->getDb()->newSelectQueryBuilder()
|
2023-07-14 15:19:40 +00:00
|
|
|
->select( [ '*' ] )
|
|
|
|
|
->from( 'ipblocks_restrictions' )
|
|
|
|
|
->where( [ 'ir_ipb_id' => $block->getId() ] )
|
|
|
|
|
->fetchResultSet();
|
2018-08-16 04:55:55 +00:00
|
|
|
|
2020-05-30 10:36:42 +00:00
|
|
|
$this->assertSame( 1, $result->numRows() );
|
2018-08-16 04:55:55 +00:00
|
|
|
$row = $result->fetchObject();
|
|
|
|
|
$this->assertEquals( $block->getId(), $row->ir_ipb_id );
|
|
|
|
|
$this->assertEquals( $page->getId(), $row->ir_value );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers ::update
|
|
|
|
|
* @covers ::restrictionsByBlockId
|
|
|
|
|
* @covers ::restrictionsToRemove
|
|
|
|
|
*/
|
|
|
|
|
public function testUpdateNoRestrictions() {
|
|
|
|
|
$block = $this->insertBlock();
|
|
|
|
|
|
2019-04-11 19:54:10 +00:00
|
|
|
$this->blockRestrictionStore->update( [] );
|
2018-08-16 04:55:55 +00:00
|
|
|
|
2023-09-25 18:49:16 +00:00
|
|
|
$result = $this->getDb()->newSelectQueryBuilder()
|
2023-07-14 15:19:40 +00:00
|
|
|
->select( [ '*' ] )
|
|
|
|
|
->from( 'ipblocks_restrictions' )
|
|
|
|
|
->where( [ 'ir_ipb_id' => $block->getId() ] )
|
|
|
|
|
->fetchResultSet();
|
2018-08-16 04:55:55 +00:00
|
|
|
|
2019-09-17 14:31:49 +00:00
|
|
|
$this->assertSame( 0, $result->numRows() );
|
2018-08-16 04:55:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers ::update
|
|
|
|
|
* @covers ::restrictionsByBlockId
|
|
|
|
|
* @covers ::restrictionsToRemove
|
|
|
|
|
*/
|
|
|
|
|
public function testUpdateSame() {
|
|
|
|
|
$block = $this->insertBlock();
|
|
|
|
|
$page = $this->getExistingTestPage( 'Foo' );
|
2019-04-11 19:54:10 +00:00
|
|
|
$this->blockRestrictionStore->insert( [
|
2018-10-30 18:19:22 +00:00
|
|
|
new PageRestriction( $block->getId(), $page->getId() ),
|
2018-08-16 04:55:55 +00:00
|
|
|
] );
|
|
|
|
|
|
2019-04-11 19:54:10 +00:00
|
|
|
$this->blockRestrictionStore->update( [
|
2018-08-16 04:55:55 +00:00
|
|
|
new PageRestriction( $block->getId(), $page->getId() ),
|
|
|
|
|
] );
|
|
|
|
|
|
2023-09-25 18:49:16 +00:00
|
|
|
$result = $this->getDb()->newSelectQueryBuilder()
|
2023-07-14 15:19:40 +00:00
|
|
|
->select( [ '*' ] )
|
|
|
|
|
->from( 'ipblocks_restrictions' )
|
|
|
|
|
->where( [ 'ir_ipb_id' => $block->getId() ] )
|
|
|
|
|
->fetchResultSet();
|
2018-08-16 04:55:55 +00:00
|
|
|
|
2020-05-30 10:36:42 +00:00
|
|
|
$this->assertSame( 1, $result->numRows() );
|
2018-08-16 04:55:55 +00:00
|
|
|
$row = $result->fetchObject();
|
|
|
|
|
$this->assertEquals( $block->getId(), $row->ir_ipb_id );
|
|
|
|
|
$this->assertEquals( $page->getId(), $row->ir_value );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers ::updateByParentBlockId
|
|
|
|
|
*/
|
|
|
|
|
public function testDeleteAllUpdateByParentBlockId() {
|
|
|
|
|
// Create a block and an autoblock (a child block)
|
|
|
|
|
$block = $this->insertBlock();
|
|
|
|
|
$pageFoo = $this->getExistingTestPage( 'Foo' );
|
|
|
|
|
$pageBar = $this->getExistingTestPage( 'Bar' );
|
2019-04-11 19:54:10 +00:00
|
|
|
$this->blockRestrictionStore->insert( [
|
2018-08-16 04:55:55 +00:00
|
|
|
new PageRestriction( $block->getId(), $pageFoo->getId() ),
|
|
|
|
|
] );
|
2023-10-31 07:57:33 +00:00
|
|
|
$autoblockId = $this->getServiceContainer()->getDatabaseBlockStore()
|
|
|
|
|
->doAutoblock( $block, '127.0.0.1' );
|
2018-08-16 04:55:55 +00:00
|
|
|
|
|
|
|
|
// Ensure that the restrictions on the block have not changed.
|
2019-04-11 19:54:10 +00:00
|
|
|
$restrictions = $this->blockRestrictionStore->loadByBlockId( $block->getId() );
|
2018-08-16 04:55:55 +00:00
|
|
|
$this->assertCount( 1, $restrictions );
|
|
|
|
|
$this->assertEquals( $pageFoo->getId(), $restrictions[0]->getValue() );
|
|
|
|
|
|
|
|
|
|
// Ensure that the restrictions on the autoblock are the same as the block.
|
2019-04-11 19:54:10 +00:00
|
|
|
$restrictions = $this->blockRestrictionStore->loadByBlockId( $autoblockId );
|
2018-08-16 04:55:55 +00:00
|
|
|
$this->assertCount( 1, $restrictions );
|
|
|
|
|
$this->assertEquals( $pageFoo->getId(), $restrictions[0]->getValue() );
|
|
|
|
|
|
|
|
|
|
// Update the restrictions on the autoblock (but leave the block unchanged)
|
2019-04-11 19:54:10 +00:00
|
|
|
$this->blockRestrictionStore->updateByParentBlockId( $block->getId(), [
|
2018-08-16 04:55:55 +00:00
|
|
|
new PageRestriction( $block->getId(), $pageBar->getId() ),
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
// Ensure that the restrictions on the block have not changed.
|
2019-04-11 19:54:10 +00:00
|
|
|
$restrictions = $this->blockRestrictionStore->loadByBlockId( $block->getId() );
|
2018-08-16 04:55:55 +00:00
|
|
|
$this->assertCount( 1, $restrictions );
|
|
|
|
|
$this->assertEquals( $pageFoo->getId(), $restrictions[0]->getValue() );
|
|
|
|
|
|
|
|
|
|
// Ensure that the restrictions on the autoblock have been updated.
|
2019-04-11 19:54:10 +00:00
|
|
|
$restrictions = $this->blockRestrictionStore->loadByBlockId( $autoblockId );
|
2018-08-16 04:55:55 +00:00
|
|
|
$this->assertCount( 1, $restrictions );
|
|
|
|
|
$this->assertEquals( $pageBar->getId(), $restrictions[0]->getValue() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers ::updateByParentBlockId
|
|
|
|
|
*/
|
|
|
|
|
public function testUpdateByParentBlockId() {
|
|
|
|
|
// Create a block and an autoblock (a child block)
|
|
|
|
|
$block = $this->insertBlock();
|
|
|
|
|
$page = $this->getExistingTestPage( 'Foo' );
|
2019-04-11 19:54:10 +00:00
|
|
|
$this->blockRestrictionStore->insert( [
|
2018-08-16 04:55:55 +00:00
|
|
|
new PageRestriction( $block->getId(), $page->getId() ),
|
|
|
|
|
] );
|
2023-10-31 07:57:33 +00:00
|
|
|
$autoblockId = $this->getServiceContainer()->getDatabaseBlockStore()
|
|
|
|
|
->doAutoblock( $block, '127.0.0.1' );
|
2018-08-16 04:55:55 +00:00
|
|
|
|
|
|
|
|
// Ensure that the restrictions on the block have not changed.
|
2019-04-11 19:54:10 +00:00
|
|
|
$restrictions = $this->blockRestrictionStore->loadByBlockId( $block->getId() );
|
2018-08-16 04:55:55 +00:00
|
|
|
$this->assertCount( 1, $restrictions );
|
|
|
|
|
|
|
|
|
|
// Ensure that the restrictions on the autoblock have not changed.
|
2019-04-11 19:54:10 +00:00
|
|
|
$restrictions = $this->blockRestrictionStore->loadByBlockId( $autoblockId );
|
2018-08-16 04:55:55 +00:00
|
|
|
$this->assertCount( 1, $restrictions );
|
|
|
|
|
|
|
|
|
|
// Remove the restrictions on the autoblock (but leave the block unchanged)
|
2019-04-11 19:54:10 +00:00
|
|
|
$this->blockRestrictionStore->updateByParentBlockId( $block->getId(), [] );
|
2018-08-16 04:55:55 +00:00
|
|
|
|
|
|
|
|
// Ensure that the restrictions on the block have not changed.
|
2019-04-11 19:54:10 +00:00
|
|
|
$restrictions = $this->blockRestrictionStore->loadByBlockId( $block->getId() );
|
2018-08-16 04:55:55 +00:00
|
|
|
$this->assertCount( 1, $restrictions );
|
|
|
|
|
|
|
|
|
|
// Ensure that the restrictions on the autoblock have been updated.
|
2019-04-11 19:54:10 +00:00
|
|
|
$restrictions = $this->blockRestrictionStore->loadByBlockId( $autoblockId );
|
2020-02-28 15:45:22 +00:00
|
|
|
$this->assertSame( [], $restrictions );
|
2018-08-16 04:55:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers ::updateByParentBlockId
|
|
|
|
|
*/
|
|
|
|
|
public function testNoAutoblocksUpdateByParentBlockId() {
|
|
|
|
|
// Create a block with no autoblock.
|
|
|
|
|
$block = $this->insertBlock();
|
|
|
|
|
$page = $this->getExistingTestPage( 'Foo' );
|
2019-04-11 19:54:10 +00:00
|
|
|
$this->blockRestrictionStore->insert( [
|
2018-08-16 04:55:55 +00:00
|
|
|
new PageRestriction( $block->getId(), $page->getId() ),
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
// Ensure that the restrictions on the block have not changed.
|
2019-04-11 19:54:10 +00:00
|
|
|
$restrictions = $this->blockRestrictionStore->loadByBlockId( $block->getId() );
|
2018-08-16 04:55:55 +00:00
|
|
|
$this->assertCount( 1, $restrictions );
|
|
|
|
|
|
|
|
|
|
// Update the restrictions on any autoblocks (there are none).
|
2019-04-11 19:54:10 +00:00
|
|
|
$this->blockRestrictionStore->updateByParentBlockId( $block->getId(), $restrictions );
|
2018-08-16 04:55:55 +00:00
|
|
|
|
|
|
|
|
// Ensure that the restrictions on the block have not changed.
|
2019-04-11 19:54:10 +00:00
|
|
|
$restrictions = $this->blockRestrictionStore->loadByBlockId( $block->getId() );
|
2018-08-16 04:55:55 +00:00
|
|
|
$this->assertCount( 1, $restrictions );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers ::delete
|
|
|
|
|
*/
|
|
|
|
|
public function testDelete() {
|
|
|
|
|
$block = $this->insertBlock();
|
|
|
|
|
$page = $this->getExistingTestPage( 'Foo' );
|
2019-04-11 19:54:10 +00:00
|
|
|
$this->blockRestrictionStore->insert( [
|
2018-08-16 04:55:55 +00:00
|
|
|
new PageRestriction( $block->getId(), $page->getId() ),
|
|
|
|
|
] );
|
|
|
|
|
|
2019-04-11 19:54:10 +00:00
|
|
|
$restrictions = $this->blockRestrictionStore->loadByBlockId( $block->getId() );
|
2018-08-16 04:55:55 +00:00
|
|
|
$this->assertCount( 1, $restrictions );
|
|
|
|
|
|
Blocks cleanup
* Make BlockManager internal methods private, since nothing calls them
anymore.
* In AbstractBlock and DatabaseBlock, remove deprecated public
properties mExpiry, mHideName, mTimestamp, mAuto and mParentBlockId.
* In BlockRestrictionStore, remove all the "instanceof Restriction"
checks. If someone passes in something that's not a Restriction, we
should throw, not ignore it, because we don't know the caller's
intention. Add a type declaration to $hasher in equals() so that it
will throw.
* Remove the "m" prefix from all private and protected properties.
AbstractBlock is not stable to override so this is not a stable
interface break.
* In BlockRestrictionStore::restrictionsToRemove(), use an O(N)
algorithm.
* In BlockRestrictionStore::rowToRestriction(), use a switch instead of
a type map, so that the calls are statically analyzable.
* In BlockUser::__construct(), fix the initialisation order issue by
inlining the relevant logic.
* Rename variable $actionRestriction.
* In Special:Block, fix call to deprecated method getTargetAndType(),
and hard deprecate it. @deprecated has the effect of deprecating a
method for both internal and external callers, there's no such thing
as an external-only deprecation. So it's necessary to rename it if you
want to keep it as a private method.
Bug: T345683
Change-Id: If4a4a18d7b5fec825417de81302266119c215fd3
2023-09-18 01:32:32 +00:00
|
|
|
$result = $this->blockRestrictionStore->delete( $restrictions );
|
2018-08-16 04:55:55 +00:00
|
|
|
$this->assertTrue( $result );
|
|
|
|
|
|
2019-04-11 19:54:10 +00:00
|
|
|
$restrictions = $this->blockRestrictionStore->loadByBlockId( $block->getId() );
|
2020-02-28 15:45:22 +00:00
|
|
|
$this->assertSame( [], $restrictions );
|
2018-08-16 04:55:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers ::deleteByBlockId
|
|
|
|
|
*/
|
|
|
|
|
public function testDeleteByBlockId() {
|
|
|
|
|
$block = $this->insertBlock();
|
|
|
|
|
$page = $this->getExistingTestPage( 'Foo' );
|
2019-04-11 19:54:10 +00:00
|
|
|
$this->blockRestrictionStore->insert( [
|
2018-08-16 04:55:55 +00:00
|
|
|
new PageRestriction( $block->getId(), $page->getId() ),
|
|
|
|
|
] );
|
|
|
|
|
|
2019-04-11 19:54:10 +00:00
|
|
|
$restrictions = $this->blockRestrictionStore->loadByBlockId( $block->getId() );
|
2018-08-16 04:55:55 +00:00
|
|
|
$this->assertCount( 1, $restrictions );
|
|
|
|
|
|
2019-04-11 19:54:10 +00:00
|
|
|
$result = $this->blockRestrictionStore->deleteByBlockId( $block->getId() );
|
2018-08-16 04:55:55 +00:00
|
|
|
$this->assertNotFalse( $result );
|
|
|
|
|
|
2019-04-11 19:54:10 +00:00
|
|
|
$restrictions = $this->blockRestrictionStore->loadByBlockId( $block->getId() );
|
2020-02-28 15:45:22 +00:00
|
|
|
$this->assertSame( [], $restrictions );
|
2018-08-16 04:55:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers ::equals
|
|
|
|
|
* @dataProvider equalsDataProvider
|
|
|
|
|
*
|
|
|
|
|
* @param array $a
|
|
|
|
|
* @param array $b
|
|
|
|
|
* @param bool $expected
|
|
|
|
|
*/
|
|
|
|
|
public function testEquals( array $a, array $b, $expected ) {
|
2019-04-11 19:54:10 +00:00
|
|
|
$this->assertSame( $expected, $this->blockRestrictionStore->equals( $a, $b ) );
|
2018-08-16 04:55:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function equalsDataProvider() {
|
|
|
|
|
return [
|
|
|
|
|
[
|
|
|
|
|
[
|
|
|
|
|
new PageRestriction( 1, 1 ),
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
new PageRestriction( 1, 2 )
|
|
|
|
|
],
|
|
|
|
|
false,
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
[
|
|
|
|
|
new PageRestriction( 1, 1 ),
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
new PageRestriction( 1, 1 ),
|
|
|
|
|
new PageRestriction( 1, 2 )
|
|
|
|
|
],
|
|
|
|
|
false,
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
[],
|
|
|
|
|
[],
|
|
|
|
|
true,
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
[
|
|
|
|
|
new PageRestriction( 1, 1 ),
|
|
|
|
|
new PageRestriction( 1, 2 ),
|
|
|
|
|
new PageRestriction( 2, 3 ),
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
new PageRestriction( 2, 3 ),
|
|
|
|
|
new PageRestriction( 1, 2 ),
|
|
|
|
|
new PageRestriction( 1, 1 ),
|
|
|
|
|
],
|
|
|
|
|
true
|
|
|
|
|
],
|
2018-10-30 18:19:22 +00:00
|
|
|
[
|
|
|
|
|
[
|
|
|
|
|
new NamespaceRestriction( 1, NS_USER ),
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
new NamespaceRestriction( 1, NS_USER ),
|
|
|
|
|
],
|
|
|
|
|
true
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
[
|
|
|
|
|
new NamespaceRestriction( 1, NS_USER ),
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
new NamespaceRestriction( 1, NS_TALK ),
|
|
|
|
|
],
|
|
|
|
|
false
|
|
|
|
|
],
|
2018-08-16 04:55:55 +00:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers ::setBlockId
|
|
|
|
|
*/
|
|
|
|
|
public function testSetBlockId() {
|
|
|
|
|
$restrictions = [
|
|
|
|
|
new PageRestriction( 1, 1 ),
|
|
|
|
|
new PageRestriction( 1, 2 ),
|
2018-10-30 18:19:22 +00:00
|
|
|
new NamespaceRestriction( 1, NS_USER ),
|
2018-08-16 04:55:55 +00:00
|
|
|
];
|
|
|
|
|
|
Blocks cleanup
* Make BlockManager internal methods private, since nothing calls them
anymore.
* In AbstractBlock and DatabaseBlock, remove deprecated public
properties mExpiry, mHideName, mTimestamp, mAuto and mParentBlockId.
* In BlockRestrictionStore, remove all the "instanceof Restriction"
checks. If someone passes in something that's not a Restriction, we
should throw, not ignore it, because we don't know the caller's
intention. Add a type declaration to $hasher in equals() so that it
will throw.
* Remove the "m" prefix from all private and protected properties.
AbstractBlock is not stable to override so this is not a stable
interface break.
* In BlockRestrictionStore::restrictionsToRemove(), use an O(N)
algorithm.
* In BlockRestrictionStore::rowToRestriction(), use a switch instead of
a type map, so that the calls are statically analyzable.
* In BlockUser::__construct(), fix the initialisation order issue by
inlining the relevant logic.
* Rename variable $actionRestriction.
* In Special:Block, fix call to deprecated method getTargetAndType(),
and hard deprecate it. @deprecated has the effect of deprecating a
method for both internal and external callers, there's no such thing
as an external-only deprecation. So it's necessary to rename it if you
want to keep it as a private method.
Bug: T345683
Change-Id: If4a4a18d7b5fec825417de81302266119c215fd3
2023-09-18 01:32:32 +00:00
|
|
|
$this->assertSame( 1, $restrictions[0]->getBlockId() );
|
2018-08-16 04:55:55 +00:00
|
|
|
$this->assertSame( 1, $restrictions[1]->getBlockId() );
|
|
|
|
|
$this->assertSame( 1, $restrictions[2]->getBlockId() );
|
2018-10-30 18:19:22 +00:00
|
|
|
|
2019-04-11 19:54:10 +00:00
|
|
|
$result = $this->blockRestrictionStore->setBlockId( 2, $restrictions );
|
2018-10-30 18:19:22 +00:00
|
|
|
|
|
|
|
|
foreach ( $result as $restriction ) {
|
|
|
|
|
$this->assertSame( 2, $restriction->getBlockId() );
|
|
|
|
|
}
|
2018-08-16 04:55:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function insertBlock() {
|
|
|
|
|
$badActor = $this->getTestUser()->getUser();
|
|
|
|
|
$sysop = $this->getTestSysop()->getUser();
|
|
|
|
|
|
2019-05-13 14:18:07 +00:00
|
|
|
$block = new DatabaseBlock( [
|
2023-11-05 23:53:56 +00:00
|
|
|
'address' => $badActor,
|
2021-06-02 09:44:38 +00:00
|
|
|
'by' => $sysop,
|
2018-08-16 04:55:55 +00:00
|
|
|
'expiry' => 'infinity',
|
|
|
|
|
'sitewide' => 0,
|
|
|
|
|
'enableAutoblock' => true,
|
|
|
|
|
] );
|
|
|
|
|
|
2022-01-12 20:13:39 +00:00
|
|
|
$this->getServiceContainer()->getDatabaseBlockStore()->insertBlock( $block );
|
2018-08-16 04:55:55 +00:00
|
|
|
|
|
|
|
|
return $block;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function insertRestriction( $blockId, $type, $value ) {
|
|
|
|
|
$this->db->insert( 'ipblocks_restrictions', [
|
|
|
|
|
'ir_ipb_id' => $blockId,
|
|
|
|
|
'ir_type' => $type,
|
|
|
|
|
'ir_value' => $value,
|
|
|
|
|
] );
|
|
|
|
|
}
|
|
|
|
|
}
|