2018-06-26 23:12:05 +00:00
|
|
|
<?php
|
|
|
|
|
|
2024-02-16 19:35:21 +00:00
|
|
|
namespace MediaWiki\Tests\Api\Query;
|
|
|
|
|
|
2023-10-11 23:13:13 +00:00
|
|
|
use MediaWiki\Block\BlockActionInfo;
|
2019-05-13 14:18:07 +00:00
|
|
|
use MediaWiki\Block\DatabaseBlock;
|
2023-07-29 09:02:26 +00:00
|
|
|
use MediaWiki\Block\Restriction\ActionRestriction;
|
2018-11-15 22:32:09 +00:00
|
|
|
use MediaWiki\Block\Restriction\NamespaceRestriction;
|
2020-01-10 00:00:51 +00:00
|
|
|
use MediaWiki\Block\Restriction\PageRestriction;
|
2022-07-19 10:57:01 +00:00
|
|
|
use MediaWiki\MainConfigNames;
|
2024-02-16 19:35:21 +00:00
|
|
|
use MediaWiki\Tests\Api\ApiTestCase;
|
2018-06-26 23:12:05 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @group API
|
|
|
|
|
* @group Database
|
|
|
|
|
* @group medium
|
|
|
|
|
*
|
2024-02-16 18:04:47 +00:00
|
|
|
* @covers \ApiQueryBlocks
|
2018-06-26 23:12:05 +00:00
|
|
|
*/
|
|
|
|
|
class ApiQueryBlocksTest extends ApiTestCase {
|
|
|
|
|
|
|
|
|
|
public function testExecute() {
|
|
|
|
|
[ $data ] = $this->doApiRequest( [
|
|
|
|
|
'action' => 'query',
|
|
|
|
|
'list' => 'blocks',
|
|
|
|
|
] );
|
|
|
|
|
$this->assertEquals( [ 'batchcomplete' => true, 'query' => [ 'blocks' => [] ] ], $data );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testExecuteBlock() {
|
|
|
|
|
$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-06-26 23:12:05 +00:00
|
|
|
'expiry' => 'infinity',
|
|
|
|
|
] );
|
|
|
|
|
|
2022-01-12 20:13:39 +00:00
|
|
|
$this->getServiceContainer()->getDatabaseBlockStore()->insertBlock( $block );
|
2018-06-26 23:12:05 +00:00
|
|
|
|
|
|
|
|
[ $data ] = $this->doApiRequest( [
|
|
|
|
|
'action' => 'query',
|
|
|
|
|
'list' => 'blocks',
|
|
|
|
|
] );
|
2019-12-10 13:38:31 +00:00
|
|
|
$this->assertArrayHasKey( 'query', $data );
|
|
|
|
|
$this->assertArrayHasKey( 'blocks', $data['query'] );
|
2018-06-26 23:12:05 +00:00
|
|
|
$this->assertCount( 1, $data['query']['blocks'] );
|
|
|
|
|
$subset = [
|
|
|
|
|
'id' => $block->getId(),
|
|
|
|
|
'user' => $badActor->getName(),
|
|
|
|
|
'expiry' => $block->getExpiry(),
|
|
|
|
|
];
|
2020-03-19 22:46:38 +00:00
|
|
|
$this->assertArraySubmapSame( $subset, $data['query']['blocks'][0] );
|
2018-06-26 23:12:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testExecuteSitewide() {
|
|
|
|
|
$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-06-26 23:12:05 +00:00
|
|
|
] );
|
|
|
|
|
|
2022-01-12 20:13:39 +00:00
|
|
|
$this->getServiceContainer()->getDatabaseBlockStore()->insertBlock( $block );
|
2018-06-26 23:12:05 +00:00
|
|
|
|
|
|
|
|
[ $data ] = $this->doApiRequest( [
|
|
|
|
|
'action' => 'query',
|
|
|
|
|
'list' => 'blocks',
|
|
|
|
|
] );
|
2019-12-10 13:38:31 +00:00
|
|
|
$this->assertArrayHasKey( 'query', $data );
|
|
|
|
|
$this->assertArrayHasKey( 'blocks', $data['query'] );
|
2018-06-26 23:12:05 +00:00
|
|
|
$this->assertCount( 1, $data['query']['blocks'] );
|
|
|
|
|
$subset = [
|
|
|
|
|
'id' => $block->getId(),
|
|
|
|
|
'user' => $badActor->getName(),
|
|
|
|
|
'expiry' => $block->getExpiry(),
|
|
|
|
|
'partial' => !$block->isSitewide(),
|
|
|
|
|
];
|
2020-03-19 22:46:38 +00:00
|
|
|
$this->assertArraySubmapSame( $subset, $data['query']['blocks'][0] );
|
2018-06-26 23:12:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testExecuteRestrictions() {
|
2022-07-19 10:57:01 +00:00
|
|
|
$this->overrideConfigValue( MainConfigNames::EnablePartialActionBlocks, true );
|
2018-06-26 23:12:05 +00:00
|
|
|
$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-06-26 23:12:05 +00:00
|
|
|
'expiry' => 'infinity',
|
|
|
|
|
'sitewide' => 0,
|
|
|
|
|
] );
|
|
|
|
|
|
2022-01-12 20:13:39 +00:00
|
|
|
$this->getServiceContainer()->getDatabaseBlockStore()->insertBlock( $block );
|
2018-06-26 23:12:05 +00:00
|
|
|
|
|
|
|
|
$subset = [
|
|
|
|
|
'id' => $block->getId(),
|
|
|
|
|
'user' => $badActor->getName(),
|
|
|
|
|
'expiry' => $block->getExpiry(),
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$title = 'Lady Macbeth';
|
|
|
|
|
$pageData = $this->insertPage( $title );
|
|
|
|
|
$pageId = $pageData['id'];
|
|
|
|
|
|
2024-04-14 19:33:50 +00:00
|
|
|
$this->db->newInsertQueryBuilder()
|
|
|
|
|
->insertInto( 'ipblocks_restrictions' )
|
|
|
|
|
->row( [
|
|
|
|
|
'ir_ipb_id' => $block->getId(),
|
|
|
|
|
'ir_type' => PageRestriction::TYPE_ID,
|
|
|
|
|
'ir_value' => $pageId,
|
|
|
|
|
] )
|
|
|
|
|
// Page that has been deleted.
|
|
|
|
|
->row( [
|
|
|
|
|
'ir_ipb_id' => $block->getId(),
|
|
|
|
|
'ir_type' => PageRestriction::TYPE_ID,
|
|
|
|
|
'ir_value' => 999999,
|
|
|
|
|
] )
|
|
|
|
|
->row( [
|
|
|
|
|
'ir_ipb_id' => $block->getId(),
|
|
|
|
|
'ir_type' => NamespaceRestriction::TYPE_ID,
|
|
|
|
|
'ir_value' => NS_USER_TALK,
|
|
|
|
|
] )
|
|
|
|
|
// Invalid type
|
|
|
|
|
->row( [
|
|
|
|
|
'ir_ipb_id' => $block->getId(),
|
|
|
|
|
'ir_type' => 127,
|
|
|
|
|
'ir_value' => 4,
|
|
|
|
|
] )
|
|
|
|
|
// Action (upload)
|
|
|
|
|
->row( [
|
|
|
|
|
'ir_ipb_id' => $block->getId(),
|
|
|
|
|
'ir_type' => ActionRestriction::TYPE_ID,
|
|
|
|
|
'ir_value' => BlockActionInfo::ACTION_UPLOAD,
|
|
|
|
|
] )
|
|
|
|
|
->caller( __METHOD__ )
|
|
|
|
|
->execute();
|
2018-06-26 23:12:05 +00:00
|
|
|
|
|
|
|
|
// Test without requesting restrictions.
|
|
|
|
|
[ $data ] = $this->doApiRequest( [
|
|
|
|
|
'action' => 'query',
|
|
|
|
|
'list' => 'blocks',
|
|
|
|
|
] );
|
2019-12-10 13:38:31 +00:00
|
|
|
$this->assertArrayHasKey( 'query', $data );
|
|
|
|
|
$this->assertArrayHasKey( 'blocks', $data['query'] );
|
2018-06-26 23:12:05 +00:00
|
|
|
$this->assertCount( 1, $data['query']['blocks'] );
|
|
|
|
|
$flagSubset = array_merge( $subset, [
|
|
|
|
|
'partial' => !$block->isSitewide(),
|
|
|
|
|
] );
|
2020-03-19 22:46:38 +00:00
|
|
|
$this->assertArraySubmapSame( $flagSubset, $data['query']['blocks'][0] );
|
2018-06-26 23:12:05 +00:00
|
|
|
$this->assertArrayNotHasKey( 'restrictions', $data['query']['blocks'][0] );
|
|
|
|
|
|
|
|
|
|
// Test requesting the restrictions.
|
|
|
|
|
[ $data ] = $this->doApiRequest( [
|
|
|
|
|
'action' => 'query',
|
|
|
|
|
'list' => 'blocks',
|
|
|
|
|
'bkprop' => 'id|user|expiry|restrictions'
|
|
|
|
|
] );
|
2019-12-10 13:38:31 +00:00
|
|
|
$this->assertArrayHasKey( 'query', $data );
|
|
|
|
|
$this->assertArrayHasKey( 'blocks', $data['query'] );
|
2018-06-26 23:12:05 +00:00
|
|
|
$this->assertCount( 1, $data['query']['blocks'] );
|
|
|
|
|
$restrictionsSubset = array_merge( $subset, [
|
|
|
|
|
'restrictions' => [
|
|
|
|
|
'pages' => [
|
|
|
|
|
[
|
|
|
|
|
'id' => $pageId,
|
2023-07-29 09:02:26 +00:00
|
|
|
'ns' => NS_MAIN,
|
2018-06-26 23:12:05 +00:00
|
|
|
'title' => $title,
|
|
|
|
|
],
|
|
|
|
|
],
|
2018-11-15 22:32:09 +00:00
|
|
|
'namespaces' => [
|
|
|
|
|
NS_USER_TALK,
|
|
|
|
|
],
|
2021-05-07 11:45:50 +00:00
|
|
|
'actions' => [
|
|
|
|
|
'upload'
|
|
|
|
|
]
|
2018-06-26 23:12:05 +00:00
|
|
|
],
|
|
|
|
|
] );
|
2020-03-19 22:46:38 +00:00
|
|
|
$this->assertArraySubmapSame( $restrictionsSubset, $data['query']['blocks'][0] );
|
2018-06-26 23:12:05 +00:00
|
|
|
$this->assertArrayNotHasKey( 'partial', $data['query']['blocks'][0] );
|
|
|
|
|
}
|
|
|
|
|
}
|