3 use MediaWiki\Block\BlockRestriction
;
4 use MediaWiki\Block\Restriction\PageRestriction
;
5 use Wikimedia\TestingAccessWrapper
;
10 * @coversDefaultClass SpecialBlock
12 class SpecialBlockTest
extends SpecialPageTestBase
{
16 protected function newSpecialPage() {
17 return new SpecialBlock();
20 public function tearDown() {
26 * @covers ::getFormFields()
28 public function testGetFormFields() {
29 $this->setMwGlobals( [
30 'wgEnablePartialBlocks' => false,
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 );
47 $this->assertArrayNotHasKey( 'EditingRestriction', $fields );
48 $this->assertArrayNotHasKey( 'PageRestrictions', $fields );
52 * @covers ::getFormFields()
54 public function testGetFormFieldsPartialBlocks() {
55 $this->setMwGlobals( [
56 'wgEnablePartialBlocks' => true,
58 $page = $this->newSpecialPage();
59 $wrappedPage = TestingAccessWrapper
::newFromObject( $page );
60 $fields = $wrappedPage->getFormFields();
62 $this->assertArrayHasKey( 'EditingRestriction', $fields );
63 $this->assertArrayHasKey( 'PageRestrictions', $fields );
67 * @covers ::maybeAlterFormDefaults()
69 public function testMaybeAlterFormDefaults() {
70 $this->setMwGlobals( [
71 'wgEnablePartialBlocks' => false,
74 $block = $this->insertBlock();
76 // Refresh the block from the database.
77 $block = Block
::newFromTarget( $block->getTarget() );
79 $page = $this->newSpecialPage();
81 $wrappedPage = TestingAccessWrapper
::newFromObject( $page );
82 $wrappedPage->target
= $block->getTarget();
83 $fields = $wrappedPage->getFormFields();
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'] );
95 * @covers ::maybeAlterFormDefaults()
97 public function testMaybeAlterFormDefaultsPartial() {
98 $this->setMwGlobals( [
99 'wgEnablePartialBlocks' => true,
102 $badActor = $this->getTestUser()->getUser();
103 $sysop = $this->getTestSysop()->getUser();
104 $pageSaturn = $this->getExistingTestPage( 'Saturn' );
105 $pageMars = $this->getExistingTestPage( 'Mars' );
107 $block = new \
Block( [
108 'address' => $badActor->getName(),
109 'user' => $badActor->getId(),
110 'by' => $sysop->getId(),
111 'expiry' => 'infinity',
113 'enableAutoblock' => true,
116 $block->setRestrictions( [
117 new PageRestriction( 0, $pageSaturn->getId() ),
118 new PageRestriction( 0, $pageMars->getId() ),
123 // Refresh the block from the database.
124 $block = Block
::newFromTarget( $block->getTarget() );
126 $page = $this->newSpecialPage();
128 $wrappedPage = TestingAccessWrapper
::newFromObject( $page );
129 $wrappedPage->target
= $block->getTarget();
130 $fields = $wrappedPage->getFormFields();
133 $pageMars->getTitle()->getPrefixedText(),
134 $pageSaturn->getTitle()->getPrefixedText(),
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'] );
143 * @covers ::processForm()
145 public function testProcessForm() {
146 $this->setMwGlobals( [
147 'wgEnablePartialBlocks' => false,
149 $badActor = $this->getTestUser()->getUser();
150 $context = RequestContext
::getMain();
152 $page = $this->newSpecialPage();
154 $expiry = 'infinity';
156 'Target' => (string)$badActor,
157 'Expiry' => 'infinity',
162 'CreateAccount' => '0',
163 'DisableUTEdit' => '0',
164 'DisableEmail' => '0',
170 $result = $page->processForm( $data, $context );
172 $this->assertTrue( $result );
174 $block = Block
::newFromTarget( $badActor );
175 $this->assertSame( $reason, $block->mReason
);
176 $this->assertSame( $expiry, $block->getExpiry() );
180 * @covers ::processForm()
182 public function testProcessFormExisting() {
183 $this->setMwGlobals( [
184 'wgEnablePartialBlocks' => false,
186 $badActor = $this->getTestUser()->getUser();
187 $sysop = $this->getTestSysop()->getUser();
188 $context = RequestContext
::getMain();
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',
197 'enableAutoblock' => false,
201 $page = $this->newSpecialPage();
203 $expiry = 'infinity';
205 'Target' => (string)$badActor,
206 'Expiry' => 'infinity',
211 'CreateAccount' => '0',
212 'DisableUTEdit' => '0',
213 'DisableEmail' => '0',
219 $result = $page->processForm( $data, $context );
221 $this->assertTrue( $result );
223 $block = Block
::newFromTarget( $badActor );
224 $this->assertSame( $reason, $block->mReason
);
225 $this->assertSame( $expiry, $block->getExpiry() );
226 $this->assertSame( '1', $block->isAutoblocking() );
230 * @covers ::processForm()
232 public function testProcessFormRestictions() {
233 $this->setMwGlobals( [
234 'wgEnablePartialBlocks' => true,
236 $badActor = $this->getTestUser()->getUser();
237 $context = RequestContext
::getMain();
239 $pageSaturn = $this->getExistingTestPage( 'Saturn' );
240 $pageMars = $this->getExistingTestPage( 'Mars' );
243 $pageSaturn->getTitle()->getText(),
244 $pageMars->getTitle()->getText(),
247 $page = $this->newSpecialPage();
249 $expiry = 'infinity';
251 'Target' => (string)$badActor,
252 'Expiry' => 'infinity',
257 'CreateAccount' => '0',
258 'DisableUTEdit' => '0',
259 'DisableEmail' => '0',
264 'EditingRestriction' => 'partial',
265 'PageRestrictions' => implode( "\n", $titles ),
267 $result = $page->processForm( $data, $context );
269 $this->assertTrue( $result );
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() ),
282 * @covers ::processForm()
284 public function testProcessFormRestrictionsChange() {
285 $this->setMwGlobals( [
286 'wgEnablePartialBlocks' => true,
288 $badActor = $this->getTestUser()->getUser();
289 $context = RequestContext
::getMain();
291 $pageSaturn = $this->getExistingTestPage( 'Saturn' );
292 $pageMars = $this->getExistingTestPage( 'Mars' );
295 $pageSaturn->getTitle()->getText(),
296 $pageMars->getTitle()->getText(),
299 // Create a partial block.
300 $page = $this->newSpecialPage();
302 $expiry = 'infinity';
304 'Target' => (string)$badActor,
305 'Expiry' => 'infinity',
310 'CreateAccount' => '0',
311 'DisableUTEdit' => '0',
312 'DisableEmail' => '0',
317 'EditingRestriction' => 'partial',
318 'PageRestrictions' => implode( "\n", $titles ),
320 $result = $page->processForm( $data, $context );
322 $this->assertTrue( $result );
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() ),
334 // Remove a page from the partial block.
335 $data['PageRestrictions'] = $pageMars->getTitle()->getText();
336 $result = $page->processForm( $data, $context );
338 $this->assertTrue( $result );
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() ),
349 // Remove the last page from the block.
350 $data['PageRestrictions'] = '';
351 $result = $page->processForm( $data, $context );
353 $this->assertTrue( $result );
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() );
361 // Change to sitewide.
362 $data['EditingRestriction'] = 'sitewide';
363 $result = $page->processForm( $data, $context );
365 $this->assertTrue( $result );
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() );
373 // Ensure that there are no restrictions where the blockId is 0.
374 $count = $this->db
->selectRowCount(
375 'ipblocks_restrictions',
377 [ 'ir_ipb_id' => 0 ],
380 $this->assertSame( 0, $count );
383 protected function insertBlock() {
384 $badActor = $this->getTestUser()->getUser();
385 $sysop = $this->getTestSysop()->getUser();
387 $block = new \
Block( [
388 'address' => $badActor->getName(),
389 'user' => $badActor->getId(),
390 'by' => $sysop->getId(),
391 'expiry' => 'infinity',
393 'enableAutoblock' => true,
401 protected function resetTables() {
402 $this->db
->delete( 'ipblocks', '*', __METHOD__
);
403 $this->db
->delete( 'ipblocks_restrictions', '*', __METHOD__
);