Debian Codegraph @

tests/phpunit/includes/api/query/ApiQueryBlocksTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Api\Query;
4
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;
12 use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait;
13
14 /**
15 * @group API
16 * @group Database
17 * @group medium
18 *
19 * @covers MediaWiki\Api\ApiQueryBlocks
20 */
21 class ApiQueryBlocksTest extends ApiTestCase {
22 use MockAuthorityTrait;
23
24 public function testExecute() {
25 [ $data ] = $this->doApiRequest( [
26 'action' => 'query',
27 'list' => 'blocks',
28 ] );
29 $this->assertEquals( [ 'batchcomplete' => true, 'query' => [ 'blocks' => [] ] ], $data );
30 }
31
32 public function testExecuteBlock() {
33 $badActor = $this->getTestUser()->getUser();
34 $sysop = $this->getTestSysop()->getUser();
35
36 $block = new DatabaseBlock( [
37 'address' => $badActor,
38 'by' => $sysop,
39 'expiry' => 'infinity',
40 ] );
41
42 $this->getServiceContainer()->getDatabaseBlockStore()->insertBlock( $block );
43
44 [ $data ] = $this->doApiRequest( [
45 'action' => 'query',
46 'list' => 'blocks',
47 ] );
48 $this->assertArrayHasKey( 'query', $data );
49 $this->assertArrayHasKey( 'blocks', $data['query'] );
50 $this->assertCount( 1, $data['query']['blocks'] );
51 $subset = [
52 'id' => $block->getId(),
53 'user' => $badActor->getName(),
54 'expiry' => $block->getExpiry(),
55 ];
56 $this->assertArraySubmapSame( $subset, $data['query']['blocks'][0] );
57 }
58
59 public function testExecuteSitewide() {
60 $badActor = $this->getTestUser()->getUser();
61 $sysop = $this->getTestSysop()->getUser();
62
63 $block = new DatabaseBlock( [
64 'address' => $badActor,
65 'by' => $sysop,
66 ] );
67
68 $this->getServiceContainer()->getDatabaseBlockStore()->insertBlock( $block );
69
70 [ $data ] = $this->doApiRequest( [
71 'action' => 'query',
72 'list' => 'blocks',
73 ] );
74 $this->assertArrayHasKey( 'query', $data );
75 $this->assertArrayHasKey( 'blocks', $data['query'] );
76 $this->assertCount( 1, $data['query']['blocks'] );
77 $subset = [
78 'id' => $block->getId(),
79 'user' => $badActor->getName(),
80 'expiry' => $block->getExpiry(),
81 'partial' => !$block->isSitewide(),
82 ];
83 $this->assertArraySubmapSame( $subset, $data['query']['blocks'][0] );
84 }
85
86 public function testHiddenBlocksVisibility() {
87 // Ensure that hidden blocks are not shown by default.
88 $badActor = $this->getTestUser()->getUser();
89 $sysop = $this->getTestSysop()->getUser();
90
91 $store = $this->getServiceContainer()->getDatabaseBlockStore();
92 $parentBlock = new DatabaseBlock( [
93 'hideName' => true,
94 'enableAutoblock' => true,
95 ] );
96 $parentBlock->setTarget( $badActor );
97 $parentBlock->setBlocker( $sysop );
98 $store->insertBlock( $parentBlock );
99
100 $autoblock = $store
101 ->doAutoblock( $parentBlock, '1.2.3.4' );
102
103 [ $data ] = $this->doApiRequest( [
104 'action' => 'query',
105 'list' => 'blocks',
106 ] );
107 $this->assertCount( 0, $data['query']['blocks'] );
108
109 [ $data ] = $this->doApiRequestWithToken( [
110 'action' => 'query',
111 'list' => 'blocks',
112 ], null, $this->mockRegisteredAuthorityWithPermissions( [ 'hideuser' ] ) );
113
114 $this->assertCount( 2, $data['query']['blocks'] );
115 }
116
117 public function testExecuteRestrictions() {
118 $this->overrideConfigValue( MainConfigNames::EnablePartialActionBlocks, true );
119 $badActor = $this->getTestUser()->getUser();
120 $sysop = $this->getTestSysop()->getUser();
121
122 $block = new DatabaseBlock( [
123 'address' => $badActor,
124 'by' => $sysop,
125 'expiry' => 'infinity',
126 'sitewide' => 0,
127 ] );
128
129 $this->getServiceContainer()->getDatabaseBlockStore()->insertBlock( $block );
130
131 $subset = [
132 'id' => $block->getId(),
133 'user' => $badActor->getName(),
134 'expiry' => $block->getExpiry(),
135 ];
136
137 $title = 'Lady Macbeth';
138 $pageData = $this->insertPage( $title );
139 $pageId = $pageData['id'];
140
141 $this->getDb()->newInsertQueryBuilder()
142 ->insertInto( 'ipblocks_restrictions' )
143 ->row( [
144 'ir_ipb_id' => $block->getId(),
145 'ir_type' => PageRestriction::TYPE_ID,
146 'ir_value' => $pageId,
147 ] )
148 // Page that has been deleted.
149 ->row( [
150 'ir_ipb_id' => $block->getId(),
151 'ir_type' => PageRestriction::TYPE_ID,
152 'ir_value' => 999999,
153 ] )
154 ->row( [
155 'ir_ipb_id' => $block->getId(),
156 'ir_type' => NamespaceRestriction::TYPE_ID,
157 'ir_value' => NS_USER_TALK,
158 ] )
159 // Invalid type
160 ->row( [
161 'ir_ipb_id' => $block->getId(),
162 'ir_type' => 127,
163 'ir_value' => 4,
164 ] )
165 // Action (upload)
166 ->row( [
167 'ir_ipb_id' => $block->getId(),
168 'ir_type' => ActionRestriction::TYPE_ID,
169 'ir_value' => BlockActionInfo::ACTION_UPLOAD,
170 ] )
171 ->caller( __METHOD__ )
172 ->execute();
173
174 // Test without requesting restrictions.
175 [ $data ] = $this->doApiRequest( [
176 'action' => 'query',
177 'list' => 'blocks',
178 ] );
179 $this->assertArrayHasKey( 'query', $data );
180 $this->assertArrayHasKey( 'blocks', $data['query'] );
181 $this->assertCount( 1, $data['query']['blocks'] );
182 $flagSubset = array_merge( $subset, [
183 'partial' => !$block->isSitewide(),
184 ] );
185 $this->assertArraySubmapSame( $flagSubset, $data['query']['blocks'][0] );
186 $this->assertArrayNotHasKey( 'restrictions', $data['query']['blocks'][0] );
187
188 // Test requesting the restrictions.
189 [ $data ] = $this->doApiRequest( [
190 'action' => 'query',
191 'list' => 'blocks',
192 'bkprop' => 'id|user|expiry|restrictions'
193 ] );
194 $this->assertArrayHasKey( 'query', $data );
195 $this->assertArrayHasKey( 'blocks', $data['query'] );
196 $this->assertCount( 1, $data['query']['blocks'] );
197 $restrictionsSubset = array_merge( $subset, [
198 'restrictions' => [
199 'pages' => [
200 [
201 'id' => $pageId,
202 'ns' => NS_MAIN,
203 'title' => $title,
204 ],
205 ],
206 'namespaces' => [
207 NS_USER_TALK,
208 ],
209 'actions' => [
210 'upload'
211 ]
212 ],
213 ] );
214 $this->assertArraySubmapSame( $restrictionsSubset, $data['query']['blocks'][0] );
215 $this->assertArrayNotHasKey( 'partial', $data['query']['blocks'][0] );
216 }
217 }