3 namespace MediaWiki\Tests\Api\Query
;
5 use MediaWiki\Block\BlockActionInfo
;
6 use MediaWiki\Block\DatabaseBlock
;
7 use MediaWiki\Block\Restriction\ActionRestriction
;
8 use MediaWiki\Block\Restriction\NamespaceRestriction
;
9 use MediaWiki\Block\Restriction\PageRestriction
;
10 use MediaWiki\MainConfigNames
;
11 use MediaWiki\Tests\Api\ApiTestCase
;
18 * @covers \MediaWiki\Api\ApiQueryBlocks
20 class ApiQueryBlocksTest
extends ApiTestCase
{
22 public function testExecute() {
23 [ $data ] = $this->doApiRequest( [
27 $this->assertEquals( [ 'batchcomplete' => true, 'query' => [ 'blocks' => [] ] ], $data );
30 public function testExecuteBlock() {
31 $badActor = $this->getTestUser()->getUser();
32 $sysop = $this->getTestSysop()->getUser();
34 $block = new DatabaseBlock( [
35 'address' => $badActor,
37 'expiry' => 'infinity',
40 $this->getServiceContainer()->getDatabaseBlockStore()->insertBlock( $block );
42 [ $data ] = $this->doApiRequest( [
46 $this->assertArrayHasKey( 'query', $data );
47 $this->assertArrayHasKey( 'blocks', $data['query'] );
48 $this->assertCount( 1, $data['query']['blocks'] );
50 'id' => $block->getId(),
51 'user' => $badActor->getName(),
52 'expiry' => $block->getExpiry(),
54 $this->assertArraySubmapSame( $subset, $data['query']['blocks'][0] );
57 public function testExecuteSitewide() {
58 $badActor = $this->getTestUser()->getUser();
59 $sysop = $this->getTestSysop()->getUser();
61 $block = new DatabaseBlock( [
62 'address' => $badActor,
66 $this->getServiceContainer()->getDatabaseBlockStore()->insertBlock( $block );
68 [ $data ] = $this->doApiRequest( [
72 $this->assertArrayHasKey( 'query', $data );
73 $this->assertArrayHasKey( 'blocks', $data['query'] );
74 $this->assertCount( 1, $data['query']['blocks'] );
76 'id' => $block->getId(),
77 'user' => $badActor->getName(),
78 'expiry' => $block->getExpiry(),
79 'partial' => !$block->isSitewide(),
81 $this->assertArraySubmapSame( $subset, $data['query']['blocks'][0] );
84 public function testExecuteRestrictions() {
85 $this->overrideConfigValue( MainConfigNames
::EnablePartialActionBlocks
, true );
86 $badActor = $this->getTestUser()->getUser();
87 $sysop = $this->getTestSysop()->getUser();
89 $block = new DatabaseBlock( [
90 'address' => $badActor,
92 'expiry' => 'infinity',
96 $this->getServiceContainer()->getDatabaseBlockStore()->insertBlock( $block );
99 'id' => $block->getId(),
100 'user' => $badActor->getName(),
101 'expiry' => $block->getExpiry(),
104 $title = 'Lady Macbeth';
105 $pageData = $this->insertPage( $title );
106 $pageId = $pageData['id'];
108 $this->getDb()->newInsertQueryBuilder()
109 ->insertInto( 'ipblocks_restrictions' )
111 'ir_ipb_id' => $block->getId(),
112 'ir_type' => PageRestriction
::TYPE_ID
,
113 'ir_value' => $pageId,
115 // Page that has been deleted.
117 'ir_ipb_id' => $block->getId(),
118 'ir_type' => PageRestriction
::TYPE_ID
,
119 'ir_value' => 999999,
122 'ir_ipb_id' => $block->getId(),
123 'ir_type' => NamespaceRestriction
::TYPE_ID
,
124 'ir_value' => NS_USER_TALK
,
128 'ir_ipb_id' => $block->getId(),
134 'ir_ipb_id' => $block->getId(),
135 'ir_type' => ActionRestriction
::TYPE_ID
,
136 'ir_value' => BlockActionInfo
::ACTION_UPLOAD
,
138 ->caller( __METHOD__
)
141 // Test without requesting restrictions.
142 [ $data ] = $this->doApiRequest( [
146 $this->assertArrayHasKey( 'query', $data );
147 $this->assertArrayHasKey( 'blocks', $data['query'] );
148 $this->assertCount( 1, $data['query']['blocks'] );
149 $flagSubset = array_merge( $subset, [
150 'partial' => !$block->isSitewide(),
152 $this->assertArraySubmapSame( $flagSubset, $data['query']['blocks'][0] );
153 $this->assertArrayNotHasKey( 'restrictions', $data['query']['blocks'][0] );
155 // Test requesting the restrictions.
156 [ $data ] = $this->doApiRequest( [
159 'bkprop' => 'id|user|expiry|restrictions'
161 $this->assertArrayHasKey( 'query', $data );
162 $this->assertArrayHasKey( 'blocks', $data['query'] );
163 $this->assertCount( 1, $data['query']['blocks'] );
164 $restrictionsSubset = array_merge( $subset, [
181 $this->assertArraySubmapSame( $restrictionsSubset, $data['query']['blocks'][0] );
182 $this->assertArrayNotHasKey( 'partial', $data['query']['blocks'][0] );