2011-01-02 19:58:27 +00:00
|
|
|
<?php
|
|
|
|
|
|
2019-05-13 14:18:07 +00:00
|
|
|
use MediaWiki\Block\DatabaseBlock;
|
2021-04-29 16:21:50 +00:00
|
|
|
use MediaWiki\Block\Restriction\ActionRestriction;
|
2018-12-14 21:08:40 +00:00
|
|
|
use MediaWiki\Block\Restriction\NamespaceRestriction;
|
2020-01-10 00:00:51 +00:00
|
|
|
use MediaWiki\Block\Restriction\PageRestriction;
|
2022-07-06 15:05:30 +00:00
|
|
|
use MediaWiki\MainConfigNames;
|
2023-07-16 18:14:22 +00:00
|
|
|
use MediaWiki\Permissions\Authority;
|
|
|
|
|
use MediaWiki\Permissions\UltimateAuthority;
|
2021-06-01 23:06:18 +00:00
|
|
|
use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait;
|
2023-07-29 09:44:29 +00:00
|
|
|
use MediaWiki\User\UserRigorOptions;
|
2023-08-19 03:35:06 +00:00
|
|
|
use MediaWiki\Utils\MWTimestamp;
|
2018-12-14 21:08:40 +00:00
|
|
|
|
2011-01-02 19:58:27 +00:00
|
|
|
/**
|
2012-04-02 13:33:43 +00:00
|
|
|
* @group API
|
2011-01-02 19:58:27 +00:00
|
|
|
* @group Database
|
2013-01-18 19:02:28 +00:00
|
|
|
* @group medium
|
2013-10-23 16:01:33 +00:00
|
|
|
*
|
|
|
|
|
* @covers ApiBlock
|
2011-01-02 19:58:27 +00:00
|
|
|
*/
|
2011-07-01 16:34:02 +00:00
|
|
|
class ApiBlockTest extends ApiTestCase {
|
2021-06-01 23:06:18 +00:00
|
|
|
use MockAuthorityTrait;
|
|
|
|
|
|
2018-03-18 18:48:51 +00:00
|
|
|
protected $mUser = null;
|
2023-10-31 07:57:33 +00:00
|
|
|
private $blockStore;
|
Support new block schema
Support migration stages when reading and writing blocks.
I tried to set it up for an easy next stage, in which support for the
old schema is removed. I tried to avoid factoring out of shared code
between the two schemas, so that the old schema cases can simply be
deleted without the need to revert unnecessary abstractions.
However, I added HideUserUtils to factor out ipb_deleted queries. Code
review showed that this was already quite complex, with multiple
approaches to the problem, so it benefits from refactoring even without
the schema abstraction.
HideUserUtils is a service rather than a standalone class to support
unit tests, since unit tests do not allow global config access. When
the migration stage config is removed, it will be a service with no
constructor parameters -- an unnecessary abstraction which should
ideally be resolved at that time.
When interpreting result rows, it is possible to share code by using
field aliases. But when constructing WHERE conditions, the actual field
names need to be used, so the migration is more intrusive in
ApiQueryBlocks and SpecialBlockList, where complex conditions are used.
Bug: T346293
Bug: T51504
Bug: T349883
Change-Id: I408acf7a57b0100fe18c455fc13141277a598925
2023-10-27 03:34:10 +00:00
|
|
|
private $blockMigrationStage;
|
2018-03-18 18:48:51 +00:00
|
|
|
|
2021-07-22 03:11:47 +00:00
|
|
|
protected function setUp(): void {
|
2011-01-02 19:58:27 +00:00
|
|
|
parent::setUp();
|
2018-03-18 18:48:51 +00:00
|
|
|
|
|
|
|
|
$this->mUser = $this->getMutableTestUser()->getUser();
|
2022-07-06 15:05:30 +00:00
|
|
|
$this->overrideConfigValue(
|
|
|
|
|
MainConfigNames::BlockCIDRLimit,
|
|
|
|
|
[
|
|
|
|
|
'IPv4' => 16,
|
|
|
|
|
'IPv6' => 19,
|
|
|
|
|
]
|
|
|
|
|
);
|
2023-10-31 07:57:33 +00:00
|
|
|
$this->blockStore = $this->getServiceContainer()->getDatabaseBlockStore();
|
Support new block schema
Support migration stages when reading and writing blocks.
I tried to set it up for an easy next stage, in which support for the
old schema is removed. I tried to avoid factoring out of shared code
between the two schemas, so that the old schema cases can simply be
deleted without the need to revert unnecessary abstractions.
However, I added HideUserUtils to factor out ipb_deleted queries. Code
review showed that this was already quite complex, with multiple
approaches to the problem, so it benefits from refactoring even without
the schema abstraction.
HideUserUtils is a service rather than a standalone class to support
unit tests, since unit tests do not allow global config access. When
the migration stage config is removed, it will be a service with no
constructor parameters -- an unnecessary abstraction which should
ideally be resolved at that time.
When interpreting result rows, it is possible to share code by using
field aliases. But when constructing WHERE conditions, the actual field
names need to be used, so the migration is more intrusive in
ApiQueryBlocks and SpecialBlockList, where complex conditions are used.
Bug: T346293
Bug: T51504
Bug: T349883
Change-Id: I408acf7a57b0100fe18c455fc13141277a598925
2023-10-27 03:34:10 +00:00
|
|
|
$this->blockMigrationStage =
|
|
|
|
|
$this->getConfVar( MainConfigNames::BlockTargetMigrationStage );
|
2011-01-02 19:58:27 +00:00
|
|
|
}
|
2011-06-02 19:32:45 +00:00
|
|
|
|
2012-04-03 13:05:50 +00:00
|
|
|
/**
|
2018-03-18 18:48:51 +00:00
|
|
|
* @param array $extraParams Extra API parameters to pass to doApiRequest
|
2023-07-16 18:14:22 +00:00
|
|
|
* @param Authority|null $blocker User to do the blocking, null to pick arbitrarily
|
2020-09-19 17:07:49 +00:00
|
|
|
* @return array result of doApiRequest
|
2012-04-03 13:05:50 +00:00
|
|
|
*/
|
2023-07-16 18:14:22 +00:00
|
|
|
private function doBlock( array $extraParams = [], Authority $blocker = null ) {
|
2021-11-21 19:13:24 +00:00
|
|
|
$this->assertNotNull( $this->mUser );
|
2011-06-02 19:32:45 +00:00
|
|
|
|
2018-03-18 18:48:51 +00:00
|
|
|
$params = [
|
2011-01-02 19:58:27 +00:00
|
|
|
'action' => 'block',
|
2018-03-18 18:48:51 +00:00
|
|
|
'user' => $this->mUser->getName(),
|
2011-08-08 16:08:48 +00:00
|
|
|
'reason' => 'Some reason',
|
2018-03-18 18:48:51 +00:00
|
|
|
];
|
|
|
|
|
if ( array_key_exists( 'userid', $extraParams ) ) {
|
|
|
|
|
// Make sure we don't have both user and userid
|
|
|
|
|
unset( $params['user'] );
|
|
|
|
|
}
|
2021-04-20 13:42:53 +00:00
|
|
|
$ret = $this->doApiRequestWithToken( array_merge( $params, $extraParams ), null, $blocker );
|
2011-03-21 19:12:41 +00:00
|
|
|
|
2023-10-31 07:57:33 +00:00
|
|
|
$block = $this->blockStore->newFromTarget( $this->mUser->getName() );
|
2011-06-02 19:32:45 +00:00
|
|
|
|
2023-11-23 08:28:31 +00:00
|
|
|
$this->assertInstanceOf( DatabaseBlock::class, $block, 'Block is valid' );
|
2011-01-02 19:58:27 +00:00
|
|
|
|
2021-06-03 10:25:17 +00:00
|
|
|
$this->assertSame( $this->mUser->getName(), $block->getTargetName() );
|
2019-10-20 00:04:00 +00:00
|
|
|
$this->assertSame( 'Some reason', $block->getReasonComment()->text );
|
2018-03-18 18:48:51 +00:00
|
|
|
|
|
|
|
|
return $ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Block by username
|
|
|
|
|
*/
|
|
|
|
|
public function testNormalBlock() {
|
|
|
|
|
$this->doBlock();
|
2011-01-02 19:58:27 +00:00
|
|
|
}
|
|
|
|
|
|
2016-12-07 17:04:02 +00:00
|
|
|
/**
|
|
|
|
|
* Block by user ID
|
|
|
|
|
*/
|
2018-03-18 18:48:51 +00:00
|
|
|
public function testBlockById() {
|
|
|
|
|
$this->doBlock( [ 'userid' => $this->mUser->getId() ] );
|
|
|
|
|
}
|
2016-12-07 17:04:02 +00:00
|
|
|
|
2018-03-18 18:48:51 +00:00
|
|
|
/**
|
|
|
|
|
* A blocked user can't block
|
|
|
|
|
*/
|
|
|
|
|
public function testBlockByBlockedUser() {
|
2023-04-26 09:16:20 +00:00
|
|
|
$this->expectApiErrorCode( 'ipbblocked' );
|
2018-03-18 18:48:51 +00:00
|
|
|
|
|
|
|
|
$blocked = $this->getMutableTestUser( [ 'sysop' ] )->getUser();
|
2019-05-13 14:18:07 +00:00
|
|
|
$block = new DatabaseBlock( [
|
2018-03-18 18:48:51 +00:00
|
|
|
'address' => $blocked->getName(),
|
2023-07-16 23:53:31 +00:00
|
|
|
'by' => $this->getTestSysop()->getUser(),
|
2018-03-18 18:48:51 +00:00
|
|
|
'reason' => 'Capriciousness',
|
|
|
|
|
'timestamp' => '19370101000000',
|
|
|
|
|
'expiry' => 'infinity',
|
|
|
|
|
] );
|
2022-01-12 20:13:39 +00:00
|
|
|
$this->getServiceContainer()->getDatabaseBlockStore()->insertBlock( $block );
|
2018-03-18 18:48:51 +00:00
|
|
|
|
|
|
|
|
$this->doBlock( [], $blocked );
|
|
|
|
|
}
|
2016-12-07 17:04:02 +00:00
|
|
|
|
2018-03-18 18:48:51 +00:00
|
|
|
public function testBlockOfNonexistentUser() {
|
2023-04-26 09:16:20 +00:00
|
|
|
$this->expectApiErrorCode( 'nosuchuser' );
|
2016-12-07 17:04:02 +00:00
|
|
|
|
2018-03-18 18:48:51 +00:00
|
|
|
$this->doBlock( [ 'user' => 'Nonexistent' ] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testBlockOfNonexistentUserId() {
|
|
|
|
|
$id = 948206325;
|
2023-04-26 09:16:20 +00:00
|
|
|
$this->expectApiErrorCode( 'nosuchuserid' );
|
2018-03-18 18:48:51 +00:00
|
|
|
|
2023-07-29 09:44:29 +00:00
|
|
|
$this->assertNull( $this->getServiceContainer()->getUserIdentityLookup()->getUserIdentityByUserId( $id ) );
|
2018-03-18 18:48:51 +00:00
|
|
|
|
|
|
|
|
$this->doBlock( [ 'userid' => $id ] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testBlockWithTag() {
|
2023-07-29 09:44:29 +00:00
|
|
|
$this->getServiceContainer()->getChangeTagsStore()->defineTag( 'custom tag' );
|
2018-09-04 19:50:46 +00:00
|
|
|
|
|
|
|
|
$this->doBlock( [ 'tags' => 'custom tag' ] );
|
|
|
|
|
|
2023-09-25 18:49:16 +00:00
|
|
|
$this->assertSame( 1, (int)$this->getDb()->newSelectQueryBuilder()
|
2023-09-21 16:37:37 +00:00
|
|
|
->select( 'COUNT(*)' )
|
|
|
|
|
->from( 'logging' )
|
|
|
|
|
->join( 'change_tag', null, 'ct_log_id = log_id' )
|
|
|
|
|
->join( 'change_tag_def', null, 'ctd_id = ct_tag_id' )
|
|
|
|
|
->where( [ 'log_type' => 'block', 'ctd_name' => 'custom tag' ] )
|
|
|
|
|
->caller( __METHOD__ )->fetchField() );
|
2018-09-04 19:50:46 +00:00
|
|
|
}
|
|
|
|
|
|
2018-03-18 18:48:51 +00:00
|
|
|
public function testBlockWithProhibitedTag() {
|
2023-04-26 09:16:20 +00:00
|
|
|
$this->expectApiErrorCode( 'tags-apply-no-permission' );
|
2018-03-18 18:48:51 +00:00
|
|
|
|
2023-07-29 09:44:29 +00:00
|
|
|
$this->getServiceContainer()->getChangeTagsStore()->defineTag( 'custom tag' );
|
2018-03-18 18:48:51 +00:00
|
|
|
|
2022-07-06 15:05:30 +00:00
|
|
|
$this->overrideConfigValue(
|
|
|
|
|
MainConfigNames::RevokePermissions,
|
|
|
|
|
[ 'user' => [ 'applychangetags' => true ] ]
|
|
|
|
|
);
|
2018-03-18 18:48:51 +00:00
|
|
|
|
|
|
|
|
$this->doBlock( [ 'tags' => 'custom tag' ] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testBlockWithHide() {
|
2021-06-01 23:06:18 +00:00
|
|
|
$res = $this->doBlock(
|
|
|
|
|
[ 'hidename' => '' ],
|
2023-07-16 23:53:31 +00:00
|
|
|
new UltimateAuthority( $this->getTestSysop()->getUser() )
|
2021-06-01 23:06:18 +00:00
|
|
|
);
|
2018-03-18 18:48:51 +00:00
|
|
|
|
Support new block schema
Support migration stages when reading and writing blocks.
I tried to set it up for an easy next stage, in which support for the
old schema is removed. I tried to avoid factoring out of shared code
between the two schemas, so that the old schema cases can simply be
deleted without the need to revert unnecessary abstractions.
However, I added HideUserUtils to factor out ipb_deleted queries. Code
review showed that this was already quite complex, with multiple
approaches to the problem, so it benefits from refactoring even without
the schema abstraction.
HideUserUtils is a service rather than a standalone class to support
unit tests, since unit tests do not allow global config access. When
the migration stage config is removed, it will be a service with no
constructor parameters -- an unnecessary abstraction which should
ideally be resolved at that time.
When interpreting result rows, it is possible to share code by using
field aliases. But when constructing WHERE conditions, the actual field
names need to be used, so the migration is more intrusive in
ApiQueryBlocks and SpecialBlockList, where complex conditions are used.
Bug: T346293
Bug: T51504
Bug: T349883
Change-Id: I408acf7a57b0100fe18c455fc13141277a598925
2023-10-27 03:34:10 +00:00
|
|
|
if ( $this->blockMigrationStage & SCHEMA_COMPAT_WRITE_OLD ) {
|
|
|
|
|
$this->assertSame( '1', $this->db->newSelectQueryBuilder()
|
|
|
|
|
->select( 'ipb_deleted' )
|
|
|
|
|
->from( 'ipblocks' )
|
|
|
|
|
->where( [ 'ipb_id' => $res[0]['block']['id'] ] )
|
|
|
|
|
->caller( __METHOD__ )->fetchField() );
|
|
|
|
|
}
|
|
|
|
|
if ( $this->blockMigrationStage & SCHEMA_COMPAT_WRITE_NEW ) {
|
|
|
|
|
$this->assertSame( '1', $this->db->newSelectQueryBuilder()
|
|
|
|
|
->select( 'bl_deleted' )
|
|
|
|
|
->from( 'block' )
|
|
|
|
|
->where( [ 'bl_id' => $res[0]['block']['id'] ] )
|
|
|
|
|
->caller( __METHOD__ )->fetchField() );
|
|
|
|
|
}
|
2018-03-18 18:48:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testBlockWithProhibitedHide() {
|
2023-07-16 18:14:22 +00:00
|
|
|
$performer = $this->mockUserAuthorityWithoutPermissions(
|
|
|
|
|
$this->getTestUser()->getUser(),
|
|
|
|
|
[ 'hideuser' ]
|
|
|
|
|
);
|
2023-04-26 09:16:20 +00:00
|
|
|
$this->expectApiErrorCode( 'permissiondenied' );
|
2018-03-18 18:48:51 +00:00
|
|
|
|
2023-07-16 18:14:22 +00:00
|
|
|
$this->doBlock( [ 'hidename' => '' ], $performer );
|
2018-03-18 18:48:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testBlockWithEmailBlock() {
|
2022-07-06 15:05:30 +00:00
|
|
|
$this->overrideConfigValues( [
|
|
|
|
|
MainConfigNames::EnableEmail => true,
|
|
|
|
|
MainConfigNames::EnableUserEmail => true,
|
2019-03-26 18:21:35 +00:00
|
|
|
] );
|
|
|
|
|
|
2018-03-18 18:48:51 +00:00
|
|
|
$res = $this->doBlock( [ 'noemail' => '' ] );
|
|
|
|
|
|
Support new block schema
Support migration stages when reading and writing blocks.
I tried to set it up for an easy next stage, in which support for the
old schema is removed. I tried to avoid factoring out of shared code
between the two schemas, so that the old schema cases can simply be
deleted without the need to revert unnecessary abstractions.
However, I added HideUserUtils to factor out ipb_deleted queries. Code
review showed that this was already quite complex, with multiple
approaches to the problem, so it benefits from refactoring even without
the schema abstraction.
HideUserUtils is a service rather than a standalone class to support
unit tests, since unit tests do not allow global config access. When
the migration stage config is removed, it will be a service with no
constructor parameters -- an unnecessary abstraction which should
ideally be resolved at that time.
When interpreting result rows, it is possible to share code by using
field aliases. But when constructing WHERE conditions, the actual field
names need to be used, so the migration is more intrusive in
ApiQueryBlocks and SpecialBlockList, where complex conditions are used.
Bug: T346293
Bug: T51504
Bug: T349883
Change-Id: I408acf7a57b0100fe18c455fc13141277a598925
2023-10-27 03:34:10 +00:00
|
|
|
if ( $this->blockMigrationStage & SCHEMA_COMPAT_WRITE_OLD ) {
|
|
|
|
|
$this->assertSame( '1', $this->getDb()->newSelectQueryBuilder()
|
|
|
|
|
->select( 'ipb_block_email' )
|
|
|
|
|
->from( 'ipblocks' )
|
|
|
|
|
->where( [ 'ipb_id' => $res[0]['block']['id'] ] )
|
|
|
|
|
->caller( __METHOD__ )->fetchField() );
|
|
|
|
|
}
|
|
|
|
|
if ( $this->blockMigrationStage & SCHEMA_COMPAT_WRITE_NEW ) {
|
|
|
|
|
$this->assertSame( '1', $this->getDb()->newSelectQueryBuilder()
|
|
|
|
|
->select( 'bl_block_email' )
|
|
|
|
|
->from( 'block' )
|
|
|
|
|
->where( [ 'bl_id' => $res[0]['block']['id'] ] )
|
|
|
|
|
->caller( __METHOD__ )->fetchField() );
|
|
|
|
|
}
|
2018-03-18 18:48:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testBlockWithProhibitedEmailBlock() {
|
2022-07-06 15:05:30 +00:00
|
|
|
$this->overrideConfigValues( [
|
|
|
|
|
MainConfigNames::EnableEmail => true,
|
|
|
|
|
MainConfigNames::EnableUserEmail => true,
|
2023-07-20 12:59:42 +00:00
|
|
|
MainConfigNames::RevokePermissions => [ 'sysop' => [ 'blockemail' => true ] ],
|
2019-03-26 18:21:35 +00:00
|
|
|
] );
|
|
|
|
|
|
2023-04-26 09:16:20 +00:00
|
|
|
$this->expectApiErrorCode( 'cantblock-email' );
|
2018-03-18 18:48:51 +00:00
|
|
|
$this->doBlock( [ 'noemail' => '' ] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testBlockWithExpiry() {
|
2021-03-22 16:57:00 +00:00
|
|
|
$fakeTime = 1616432035;
|
|
|
|
|
MWTimestamp::setFakeTime( $fakeTime );
|
2018-03-18 18:48:51 +00:00
|
|
|
$res = $this->doBlock( [ 'expiry' => '1 day' ] );
|
|
|
|
|
|
Support new block schema
Support migration stages when reading and writing blocks.
I tried to set it up for an easy next stage, in which support for the
old schema is removed. I tried to avoid factoring out of shared code
between the two schemas, so that the old schema cases can simply be
deleted without the need to revert unnecessary abstractions.
However, I added HideUserUtils to factor out ipb_deleted queries. Code
review showed that this was already quite complex, with multiple
approaches to the problem, so it benefits from refactoring even without
the schema abstraction.
HideUserUtils is a service rather than a standalone class to support
unit tests, since unit tests do not allow global config access. When
the migration stage config is removed, it will be a service with no
constructor parameters -- an unnecessary abstraction which should
ideally be resolved at that time.
When interpreting result rows, it is possible to share code by using
field aliases. But when constructing WHERE conditions, the actual field
names need to be used, so the migration is more intrusive in
ApiQueryBlocks and SpecialBlockList, where complex conditions are used.
Bug: T346293
Bug: T51504
Bug: T349883
Change-Id: I408acf7a57b0100fe18c455fc13141277a598925
2023-10-27 03:34:10 +00:00
|
|
|
if ( $this->blockMigrationStage & SCHEMA_COMPAT_WRITE_OLD ) {
|
|
|
|
|
$expiry = $this->getDb()->newSelectQueryBuilder()
|
|
|
|
|
->select( 'ipb_expiry' )
|
|
|
|
|
->from( 'ipblocks' )
|
|
|
|
|
->where( [ 'ipb_id' => $res[0]['block']['id'] ] )
|
|
|
|
|
->caller( __METHOD__ )->fetchField();
|
|
|
|
|
}
|
|
|
|
|
if ( $this->blockMigrationStage & SCHEMA_COMPAT_WRITE_NEW ) {
|
|
|
|
|
$expiry = $this->getDb()->newSelectQueryBuilder()
|
|
|
|
|
->select( 'bl_expiry' )
|
|
|
|
|
->from( 'block' )
|
|
|
|
|
->where( [ 'bl_id' => $res[0]['block']['id'] ] )
|
|
|
|
|
->caller( __METHOD__ )->fetchField();
|
|
|
|
|
}
|
2018-03-18 18:48:51 +00:00
|
|
|
|
2021-03-22 16:57:00 +00:00
|
|
|
$this->assertSame( (int)wfTimestamp( TS_UNIX, $expiry ), $fakeTime + 86400 );
|
2018-03-18 18:48:51 +00:00
|
|
|
}
|
2016-12-07 17:04:02 +00:00
|
|
|
|
2018-03-18 18:48:51 +00:00
|
|
|
public function testBlockWithInvalidExpiry() {
|
2023-04-26 09:16:20 +00:00
|
|
|
$this->expectApiErrorCode( 'invalidexpiry' );
|
2016-12-07 17:04:02 +00:00
|
|
|
|
2018-03-18 18:48:51 +00:00
|
|
|
$this->doBlock( [ 'expiry' => '' ] );
|
2016-12-07 17:04:02 +00:00
|
|
|
}
|
|
|
|
|
|
2018-12-19 16:11:59 +00:00
|
|
|
public function testBlockWithoutRestrictions() {
|
|
|
|
|
$this->doBlock();
|
|
|
|
|
|
2023-10-31 07:57:33 +00:00
|
|
|
$block = $this->blockStore->newFromTarget( $this->mUser->getName() );
|
2018-12-19 16:11:59 +00:00
|
|
|
|
|
|
|
|
$this->assertTrue( $block->isSitewide() );
|
2020-02-28 15:45:22 +00:00
|
|
|
$this->assertSame( [], $block->getRestrictions() );
|
2018-12-19 16:11:59 +00:00
|
|
|
}
|
|
|
|
|
|
2021-05-04 15:20:14 +00:00
|
|
|
public function testBlockWithRestrictionsPage() {
|
2018-08-27 22:19:37 +00:00
|
|
|
$title = 'Foo';
|
2023-03-24 03:21:20 +00:00
|
|
|
$this->getExistingTestPage( $title );
|
2018-08-27 22:19:37 +00:00
|
|
|
|
|
|
|
|
$this->doBlock( [
|
|
|
|
|
'partial' => true,
|
|
|
|
|
'pagerestrictions' => $title,
|
2019-07-30 02:15:56 +00:00
|
|
|
'allowusertalk' => true,
|
2018-08-27 22:19:37 +00:00
|
|
|
] );
|
|
|
|
|
|
2023-10-31 07:57:33 +00:00
|
|
|
$block = $this->blockStore->newFromTarget( $this->mUser->getName() );
|
2018-08-27 22:19:37 +00:00
|
|
|
|
|
|
|
|
$this->assertFalse( $block->isSitewide() );
|
2018-12-14 21:08:40 +00:00
|
|
|
$this->assertInstanceOf( PageRestriction::class, $block->getRestrictions()[0] );
|
2018-08-27 22:19:37 +00:00
|
|
|
$this->assertEquals( $title, $block->getRestrictions()[0]->getTitle()->getText() );
|
2021-05-04 15:20:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testBlockWithRestrictionsNamespace() {
|
|
|
|
|
$namespace = NS_TALK;
|
|
|
|
|
|
|
|
|
|
$this->doBlock( [
|
|
|
|
|
'partial' => true,
|
|
|
|
|
'namespacerestrictions' => $namespace,
|
|
|
|
|
'allowusertalk' => true,
|
|
|
|
|
] );
|
|
|
|
|
|
2023-10-31 07:57:33 +00:00
|
|
|
$block = $this->blockStore->newFromTarget( $this->mUser->getName() );
|
2021-05-04 15:20:14 +00:00
|
|
|
|
|
|
|
|
$this->assertInstanceOf( NamespaceRestriction::class, $block->getRestrictions()[0] );
|
|
|
|
|
$this->assertEquals( $namespace, $block->getRestrictions()[0]->getValue() );
|
2018-08-27 22:19:37 +00:00
|
|
|
}
|
|
|
|
|
|
2021-04-29 16:21:50 +00:00
|
|
|
public function testBlockWithRestrictionsAction() {
|
2022-07-06 15:05:30 +00:00
|
|
|
$this->overrideConfigValue(
|
|
|
|
|
MainConfigNames::EnablePartialActionBlocks,
|
|
|
|
|
true
|
|
|
|
|
);
|
2021-04-29 16:21:50 +00:00
|
|
|
|
2022-01-12 20:13:39 +00:00
|
|
|
$blockActionInfo = $this->getServiceContainer()->getBlockActionInfo();
|
2021-04-29 16:21:50 +00:00
|
|
|
$action = 'upload';
|
|
|
|
|
|
|
|
|
|
$this->doBlock( [
|
|
|
|
|
'partial' => true,
|
|
|
|
|
'actionrestrictions' => $action,
|
|
|
|
|
'allowusertalk' => true,
|
|
|
|
|
] );
|
|
|
|
|
|
2023-10-31 07:57:33 +00:00
|
|
|
$block = $this->blockStore->newFromTarget( $this->mUser->getName() );
|
2021-04-29 16:21:50 +00:00
|
|
|
|
|
|
|
|
$this->assertInstanceOf( ActionRestriction::class, $block->getRestrictions()[0] );
|
|
|
|
|
$this->assertEquals( $action, $blockActionInfo->getActionFromId( $block->getRestrictions()[0]->getValue() ) );
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-16 19:06:19 +00:00
|
|
|
public function testBlockingActionWithNoToken() {
|
2023-04-26 09:16:20 +00:00
|
|
|
$this->expectApiErrorCode( 'missingparam' );
|
2012-04-03 20:02:27 +00:00
|
|
|
$this->doApiRequest(
|
2016-02-17 09:09:32 +00:00
|
|
|
[
|
2013-10-23 16:01:33 +00:00
|
|
|
'action' => 'block',
|
2018-03-18 18:48:51 +00:00
|
|
|
'user' => $this->mUser->getName(),
|
2012-04-03 20:02:27 +00:00
|
|
|
'reason' => 'Some reason',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2012-04-03 20:02:27 +00:00
|
|
|
null,
|
|
|
|
|
false,
|
2023-07-16 23:53:31 +00:00
|
|
|
$this->getTestSysop()->getUser()
|
2012-04-03 20:02:27 +00:00
|
|
|
);
|
|
|
|
|
}
|
2018-08-27 22:19:37 +00:00
|
|
|
|
|
|
|
|
public function testBlockWithLargeRange() {
|
2023-04-26 09:16:20 +00:00
|
|
|
$this->expectApiErrorCode( 'baduser' );
|
2021-04-20 13:42:53 +00:00
|
|
|
$this->doApiRequestWithToken(
|
2018-08-27 22:19:37 +00:00
|
|
|
[
|
|
|
|
|
'action' => 'block',
|
|
|
|
|
'user' => '127.0.0.1/64',
|
|
|
|
|
'reason' => 'Some reason',
|
|
|
|
|
],
|
|
|
|
|
null,
|
2023-07-16 23:53:31 +00:00
|
|
|
$this->getTestSysop()->getUser()
|
2018-08-27 22:19:37 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-06 21:29:02 +00:00
|
|
|
public function testBlockingTooManyPageRestrictions() {
|
2023-04-26 09:16:20 +00:00
|
|
|
$this->expectApiErrorCode( 'toomanyvalues' );
|
2021-04-20 13:42:53 +00:00
|
|
|
$this->doApiRequestWithToken(
|
2018-08-27 22:19:37 +00:00
|
|
|
[
|
|
|
|
|
'action' => 'block',
|
|
|
|
|
'user' => $this->mUser->getName(),
|
|
|
|
|
'reason' => 'Some reason',
|
|
|
|
|
'partial' => true,
|
|
|
|
|
'pagerestrictions' => 'One|Two|Three|Four|Five|Six|Seven|Eight|Nine|Ten|Eleven',
|
|
|
|
|
],
|
|
|
|
|
null,
|
2023-07-16 23:53:31 +00:00
|
|
|
$this->getTestSysop()->getUser()
|
2018-08-27 22:19:37 +00:00
|
|
|
);
|
|
|
|
|
}
|
2018-07-13 15:07:51 +00:00
|
|
|
|
|
|
|
|
public function testRangeBlock() {
|
2023-07-29 09:44:29 +00:00
|
|
|
$this->mUser = $this->getServiceContainer()->getUserFactory()->newFromName( '128.0.0.0/16', UserRigorOptions::RIGOR_NONE );
|
2018-07-13 15:07:51 +00:00
|
|
|
$this->doBlock();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testVeryLargeRangeBlock() {
|
2023-07-29 09:44:29 +00:00
|
|
|
$this->mUser = $this->getServiceContainer()->getUserFactory()->newFromName( '128.0.0.0/1', UserRigorOptions::RIGOR_NONE );
|
2023-04-26 09:16:20 +00:00
|
|
|
$this->expectApiErrorCode( 'ip_range_toolarge' );
|
2018-07-13 15:07:51 +00:00
|
|
|
$this->doBlock();
|
|
|
|
|
}
|
2020-09-19 17:07:49 +00:00
|
|
|
|
|
|
|
|
public function testBlockByIdReturns() {
|
|
|
|
|
// See T189073 and Ifdced735b694b85116cb0e43dadbfa8e4cdb8cab for context
|
|
|
|
|
$userId = $this->mUser->getId();
|
|
|
|
|
|
|
|
|
|
$res = $this->doBlock(
|
|
|
|
|
[ 'userid' => $userId ]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$blockResult = $res[0]['block'];
|
|
|
|
|
|
|
|
|
|
$this->assertArrayHasKey( 'user', $blockResult );
|
|
|
|
|
$this->assertSame( $this->mUser->getName(), $blockResult['user'] );
|
|
|
|
|
|
|
|
|
|
$this->assertArrayHasKey( 'userID', $blockResult );
|
|
|
|
|
$this->assertSame( $userId, $blockResult['userID'] );
|
|
|
|
|
}
|
2011-01-02 19:58:27 +00:00
|
|
|
}
|