Merge "Remove use of BagOStuff TTL constants from unrelated code"
[mediawiki.git] / tests / phpunit / includes / api / query / ApiQueryBlocksTest.php
blob347f76e3df92921d0d50627612031c455e20e9da
1 <?php
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;
13 /**
14 * @group API
15 * @group Database
16 * @group medium
18 * @covers \MediaWiki\Api\ApiQueryBlocks
20 class ApiQueryBlocksTest extends ApiTestCase {
22 public function testExecute() {
23 [ $data ] = $this->doApiRequest( [
24 'action' => 'query',
25 'list' => 'blocks',
26 ] );
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,
36 'by' => $sysop,
37 'expiry' => 'infinity',
38 ] );
40 $this->getServiceContainer()->getDatabaseBlockStore()->insertBlock( $block );
42 [ $data ] = $this->doApiRequest( [
43 'action' => 'query',
44 'list' => 'blocks',
45 ] );
46 $this->assertArrayHasKey( 'query', $data );
47 $this->assertArrayHasKey( 'blocks', $data['query'] );
48 $this->assertCount( 1, $data['query']['blocks'] );
49 $subset = [
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,
63 'by' => $sysop,
64 ] );
66 $this->getServiceContainer()->getDatabaseBlockStore()->insertBlock( $block );
68 [ $data ] = $this->doApiRequest( [
69 'action' => 'query',
70 'list' => 'blocks',
71 ] );
72 $this->assertArrayHasKey( 'query', $data );
73 $this->assertArrayHasKey( 'blocks', $data['query'] );
74 $this->assertCount( 1, $data['query']['blocks'] );
75 $subset = [
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,
91 'by' => $sysop,
92 'expiry' => 'infinity',
93 'sitewide' => 0,
94 ] );
96 $this->getServiceContainer()->getDatabaseBlockStore()->insertBlock( $block );
98 $subset = [
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' )
110 ->row( [
111 'ir_ipb_id' => $block->getId(),
112 'ir_type' => PageRestriction::TYPE_ID,
113 'ir_value' => $pageId,
115 // Page that has been deleted.
116 ->row( [
117 'ir_ipb_id' => $block->getId(),
118 'ir_type' => PageRestriction::TYPE_ID,
119 'ir_value' => 999999,
121 ->row( [
122 'ir_ipb_id' => $block->getId(),
123 'ir_type' => NamespaceRestriction::TYPE_ID,
124 'ir_value' => NS_USER_TALK,
126 // Invalid type
127 ->row( [
128 'ir_ipb_id' => $block->getId(),
129 'ir_type' => 127,
130 'ir_value' => 4,
132 // Action (upload)
133 ->row( [
134 'ir_ipb_id' => $block->getId(),
135 'ir_type' => ActionRestriction::TYPE_ID,
136 'ir_value' => BlockActionInfo::ACTION_UPLOAD,
138 ->caller( __METHOD__ )
139 ->execute();
141 // Test without requesting restrictions.
142 [ $data ] = $this->doApiRequest( [
143 'action' => 'query',
144 'list' => 'blocks',
145 ] );
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(),
151 ] );
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( [
157 'action' => 'query',
158 'list' => 'blocks',
159 'bkprop' => 'id|user|expiry|restrictions'
160 ] );
161 $this->assertArrayHasKey( 'query', $data );
162 $this->assertArrayHasKey( 'blocks', $data['query'] );
163 $this->assertCount( 1, $data['query']['blocks'] );
164 $restrictionsSubset = array_merge( $subset, [
165 'restrictions' => [
166 'pages' => [
168 'id' => $pageId,
169 'ns' => NS_MAIN,
170 'title' => $title,
173 'namespaces' => [
174 NS_USER_TALK,
176 'actions' => [
177 'upload'
180 ] );
181 $this->assertArraySubmapSame( $restrictionsSubset, $data['query']['blocks'][0] );
182 $this->assertArrayNotHasKey( 'partial', $data['query']['blocks'][0] );