Merge "debug: Allow the DBQuery channel to be used"
[lhc/web/wiklou.git] / tests / phpunit / includes / specials / SpecialBlockTest.php
1 <?php
2
3 use MediaWiki\Block\BlockRestriction;
4 use MediaWiki\Block\Restriction\PageRestriction;
5 use Wikimedia\TestingAccessWrapper;
6
7 /**
8 * @group Blocking
9 * @group Database
10 * @coversDefaultClass SpecialBlock
11 */
12 class SpecialBlockTest extends SpecialPageTestBase {
13 /**
14 * {@inheritdoc}
15 */
16 protected function newSpecialPage() {
17 return new SpecialBlock();
18 }
19
20 public function tearDown() {
21 parent::tearDown();
22 $this->resetTables();
23 }
24
25 /**
26 * @covers ::getFormFields()
27 */
28 public function testGetFormFields() {
29 $this->setMwGlobals( [
30 'wgEnablePartialBlocks' => false,
31 ] );
32 $page = $this->newSpecialPage();
33 $wrappedPage = TestingAccessWrapper::newFromObject( $page );
34 $fields = $wrappedPage->getFormFields();
35 $this->assertInternalType( 'array', $fields );
36 $this->assertArrayHasKey( 'Target', $fields );
37 $this->assertArrayHasKey( 'Expiry', $fields );
38 $this->assertArrayHasKey( 'Reason', $fields );
39 $this->assertArrayHasKey( 'CreateAccount', $fields );
40 $this->assertArrayHasKey( 'DisableUTEdit', $fields );
41 $this->assertArrayHasKey( 'DisableUTEdit', $fields );
42 $this->assertArrayHasKey( 'AutoBlock', $fields );
43 $this->assertArrayHasKey( 'HardBlock', $fields );
44 $this->assertArrayHasKey( 'PreviousTarget', $fields );
45 $this->assertArrayHasKey( 'Confirm', $fields );
46
47 $this->assertArrayNotHasKey( 'EditingRestriction', $fields );
48 $this->assertArrayNotHasKey( 'PageRestrictions', $fields );
49 }
50
51 /**
52 * @covers ::getFormFields()
53 */
54 public function testGetFormFieldsPartialBlocks() {
55 $this->setMwGlobals( [
56 'wgEnablePartialBlocks' => true,
57 ] );
58 $page = $this->newSpecialPage();
59 $wrappedPage = TestingAccessWrapper::newFromObject( $page );
60 $fields = $wrappedPage->getFormFields();
61
62 $this->assertArrayHasKey( 'EditingRestriction', $fields );
63 $this->assertArrayHasKey( 'PageRestrictions', $fields );
64 }
65
66 /**
67 * @covers ::maybeAlterFormDefaults()
68 */
69 public function testMaybeAlterFormDefaults() {
70 $this->setMwGlobals( [
71 'wgEnablePartialBlocks' => false,
72 ] );
73
74 $block = $this->insertBlock();
75
76 // Refresh the block from the database.
77 $block = Block::newFromTarget( $block->getTarget() );
78
79 $page = $this->newSpecialPage();
80
81 $wrappedPage = TestingAccessWrapper::newFromObject( $page );
82 $wrappedPage->target = $block->getTarget();
83 $fields = $wrappedPage->getFormFields();
84
85 $this->assertSame( (string)$block->getTarget(), $fields['Target']['default'] );
86 $this->assertSame( $block->isHardblock(), $fields['HardBlock']['default'] );
87 $this->assertSame( $block->prevents( 'createaccount' ), $fields['CreateAccount']['default'] );
88 $this->assertSame( $block->isAutoblocking(), $fields['AutoBlock']['default'] );
89 $this->assertSame( $block->prevents( 'editownusertalk' ), $fields['DisableUTEdit']['default'] );
90 $this->assertSame( $block->mReason, $fields['Reason']['default'] );
91 $this->assertSame( 'infinite', $fields['Expiry']['default'] );
92 }
93
94 /**
95 * @covers ::maybeAlterFormDefaults()
96 */
97 public function testMaybeAlterFormDefaultsPartial() {
98 $this->setMwGlobals( [
99 'wgEnablePartialBlocks' => true,
100 ] );
101
102 $badActor = $this->getTestUser()->getUser();
103 $sysop = $this->getTestSysop()->getUser();
104 $pageSaturn = $this->getExistingTestPage( 'Saturn' );
105 $pageMars = $this->getExistingTestPage( 'Mars' );
106
107 $block = new \Block( [
108 'address' => $badActor->getName(),
109 'user' => $badActor->getId(),
110 'by' => $sysop->getId(),
111 'expiry' => 'infinity',
112 'sitewide' => 0,
113 'enableAutoblock' => true,
114 ] );
115
116 $block->setRestrictions( [
117 new PageRestriction( 0, $pageSaturn->getId() ),
118 new PageRestriction( 0, $pageMars->getId() ),
119 ] );
120
121 $block->insert();
122
123 // Refresh the block from the database.
124 $block = Block::newFromTarget( $block->getTarget() );
125
126 $page = $this->newSpecialPage();
127
128 $wrappedPage = TestingAccessWrapper::newFromObject( $page );
129 $wrappedPage->target = $block->getTarget();
130 $fields = $wrappedPage->getFormFields();
131
132 $titles = [
133 $pageMars->getTitle()->getPrefixedText(),
134 $pageSaturn->getTitle()->getPrefixedText(),
135 ];
136
137 $this->assertSame( (string)$block->getTarget(), $fields['Target']['default'] );
138 $this->assertSame( 'partial', $fields['EditingRestriction']['default'] );
139 $this->assertSame( implode( "\n", $titles ), $fields['PageRestrictions']['default'] );
140 }
141
142 /**
143 * @covers ::processForm()
144 */
145 public function testProcessForm() {
146 $this->setMwGlobals( [
147 'wgEnablePartialBlocks' => false,
148 ] );
149 $badActor = $this->getTestUser()->getUser();
150 $context = RequestContext::getMain();
151
152 $page = $this->newSpecialPage();
153 $reason = 'test';
154 $expiry = 'infinity';
155 $data = [
156 'Target' => (string)$badActor,
157 'Expiry' => 'infinity',
158 'Reason' => [
159 $reason,
160 ],
161 'Confirm' => '1',
162 'CreateAccount' => '0',
163 'DisableUTEdit' => '0',
164 'DisableEmail' => '0',
165 'HardBlock' => '0',
166 'AutoBlock' => '1',
167 'HideUser' => '0',
168 'Watch' => '0',
169 ];
170 $result = $page->processForm( $data, $context );
171
172 $this->assertTrue( $result );
173
174 $block = Block::newFromTarget( $badActor );
175 $this->assertSame( $reason, $block->mReason );
176 $this->assertSame( $expiry, $block->getExpiry() );
177 }
178
179 /**
180 * @covers ::processForm()
181 */
182 public function testProcessFormExisting() {
183 $this->setMwGlobals( [
184 'wgEnablePartialBlocks' => false,
185 ] );
186 $badActor = $this->getTestUser()->getUser();
187 $sysop = $this->getTestSysop()->getUser();
188 $context = RequestContext::getMain();
189
190 // Create a block that will be updated.
191 $block = new \Block( [
192 'address' => $badActor->getName(),
193 'user' => $badActor->getId(),
194 'by' => $sysop->getId(),
195 'expiry' => 'infinity',
196 'sitewide' => 0,
197 'enableAutoblock' => false,
198 ] );
199 $block->insert();
200
201 $page = $this->newSpecialPage();
202 $reason = 'test';
203 $expiry = 'infinity';
204 $data = [
205 'Target' => (string)$badActor,
206 'Expiry' => 'infinity',
207 'Reason' => [
208 $reason,
209 ],
210 'Confirm' => '1',
211 'CreateAccount' => '0',
212 'DisableUTEdit' => '0',
213 'DisableEmail' => '0',
214 'HardBlock' => '0',
215 'AutoBlock' => '1',
216 'HideUser' => '0',
217 'Watch' => '0',
218 ];
219 $result = $page->processForm( $data, $context );
220
221 $this->assertTrue( $result );
222
223 $block = Block::newFromTarget( $badActor );
224 $this->assertSame( $reason, $block->mReason );
225 $this->assertSame( $expiry, $block->getExpiry() );
226 $this->assertSame( '1', $block->isAutoblocking() );
227 }
228
229 /**
230 * @covers ::processForm()
231 */
232 public function testProcessFormRestictions() {
233 $this->setMwGlobals( [
234 'wgEnablePartialBlocks' => true,
235 ] );
236 $badActor = $this->getTestUser()->getUser();
237 $context = RequestContext::getMain();
238
239 $pageSaturn = $this->getExistingTestPage( 'Saturn' );
240 $pageMars = $this->getExistingTestPage( 'Mars' );
241
242 $titles = [
243 $pageSaturn->getTitle()->getText(),
244 $pageMars->getTitle()->getText(),
245 ];
246
247 $page = $this->newSpecialPage();
248 $reason = 'test';
249 $expiry = 'infinity';
250 $data = [
251 'Target' => (string)$badActor,
252 'Expiry' => 'infinity',
253 'Reason' => [
254 $reason,
255 ],
256 'Confirm' => '1',
257 'CreateAccount' => '0',
258 'DisableUTEdit' => '0',
259 'DisableEmail' => '0',
260 'HardBlock' => '0',
261 'AutoBlock' => '1',
262 'HideUser' => '0',
263 'Watch' => '0',
264 'EditingRestriction' => 'partial',
265 'PageRestrictions' => implode( "\n", $titles ),
266 ];
267 $result = $page->processForm( $data, $context );
268
269 $this->assertTrue( $result );
270
271 $block = Block::newFromTarget( $badActor );
272 $this->assertSame( $reason, $block->mReason );
273 $this->assertSame( $expiry, $block->getExpiry() );
274 $this->assertCount( 2, $block->getRestrictions() );
275 $this->assertTrue( BlockRestriction::equals( $block->getRestrictions(), [
276 new PageRestriction( $block->getId(), $pageMars->getId() ),
277 new PageRestriction( $block->getId(), $pageSaturn->getId() ),
278 ] ) );
279 }
280
281 /**
282 * @covers ::processForm()
283 */
284 public function testProcessFormRestrictionsChange() {
285 $this->setMwGlobals( [
286 'wgEnablePartialBlocks' => true,
287 ] );
288 $badActor = $this->getTestUser()->getUser();
289 $context = RequestContext::getMain();
290
291 $pageSaturn = $this->getExistingTestPage( 'Saturn' );
292 $pageMars = $this->getExistingTestPage( 'Mars' );
293
294 $titles = [
295 $pageSaturn->getTitle()->getText(),
296 $pageMars->getTitle()->getText(),
297 ];
298
299 // Create a partial block.
300 $page = $this->newSpecialPage();
301 $reason = 'test';
302 $expiry = 'infinity';
303 $data = [
304 'Target' => (string)$badActor,
305 'Expiry' => 'infinity',
306 'Reason' => [
307 $reason,
308 ],
309 'Confirm' => '1',
310 'CreateAccount' => '0',
311 'DisableUTEdit' => '0',
312 'DisableEmail' => '0',
313 'HardBlock' => '0',
314 'AutoBlock' => '1',
315 'HideUser' => '0',
316 'Watch' => '0',
317 'EditingRestriction' => 'partial',
318 'PageRestrictions' => implode( "\n", $titles ),
319 ];
320 $result = $page->processForm( $data, $context );
321
322 $this->assertTrue( $result );
323
324 $block = Block::newFromTarget( $badActor );
325 $this->assertSame( $reason, $block->mReason );
326 $this->assertSame( $expiry, $block->getExpiry() );
327 $this->assertFalse( $block->isSitewide() );
328 $this->assertCount( 2, $block->getRestrictions() );
329 $this->assertTrue( BlockRestriction::equals( $block->getRestrictions(), [
330 new PageRestriction( $block->getId(), $pageMars->getId() ),
331 new PageRestriction( $block->getId(), $pageSaturn->getId() ),
332 ] ) );
333
334 // Remove a page from the partial block.
335 $data['PageRestrictions'] = $pageMars->getTitle()->getText();
336 $result = $page->processForm( $data, $context );
337
338 $this->assertTrue( $result );
339
340 $block = Block::newFromTarget( $badActor );
341 $this->assertSame( $reason, $block->mReason );
342 $this->assertSame( $expiry, $block->getExpiry() );
343 $this->assertFalse( $block->isSitewide() );
344 $this->assertCount( 1, $block->getRestrictions() );
345 $this->assertTrue( BlockRestriction::equals( $block->getRestrictions(), [
346 new PageRestriction( $block->getId(), $pageMars->getId() ),
347 ] ) );
348
349 // Remove the last page from the block.
350 $data['PageRestrictions'] = '';
351 $result = $page->processForm( $data, $context );
352
353 $this->assertTrue( $result );
354
355 $block = Block::newFromTarget( $badActor );
356 $this->assertSame( $reason, $block->mReason );
357 $this->assertSame( $expiry, $block->getExpiry() );
358 $this->assertFalse( $block->isSitewide() );
359 $this->assertCount( 0, $block->getRestrictions() );
360
361 // Change to sitewide.
362 $data['EditingRestriction'] = 'sitewide';
363 $result = $page->processForm( $data, $context );
364
365 $this->assertTrue( $result );
366
367 $block = Block::newFromTarget( $badActor );
368 $this->assertSame( $reason, $block->mReason );
369 $this->assertSame( $expiry, $block->getExpiry() );
370 $this->assertTrue( $block->isSitewide() );
371 $this->assertCount( 0, $block->getRestrictions() );
372
373 // Ensure that there are no restrictions where the blockId is 0.
374 $count = $this->db->selectRowCount(
375 'ipblocks_restrictions',
376 '*',
377 [ 'ir_ipb_id' => 0 ],
378 __METHOD__
379 );
380 $this->assertSame( 0, $count );
381 }
382
383 protected function insertBlock() {
384 $badActor = $this->getTestUser()->getUser();
385 $sysop = $this->getTestSysop()->getUser();
386
387 $block = new \Block( [
388 'address' => $badActor->getName(),
389 'user' => $badActor->getId(),
390 'by' => $sysop->getId(),
391 'expiry' => 'infinity',
392 'sitewide' => 1,
393 'enableAutoblock' => true,
394 ] );
395
396 $block->insert();
397
398 return $block;
399 }
400
401 protected function resetTables() {
402 $this->db->delete( 'ipblocks', '*', __METHOD__ );
403 $this->db->delete( 'ipblocks_restrictions', '*', __METHOD__ );
404 }
405 }