Debian Codegraph @

tests/phpunit/includes/block/BlockRestrictionStoreTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Block;
4
5 use MediaWiki\Block\BlockRestrictionStore;
6 use MediaWiki\Block\DatabaseBlock;
7 use MediaWiki\Block\Restriction\NamespaceRestriction;
8 use MediaWiki\Block\Restriction\PageRestriction;
9 use MediaWiki\Block\Restriction\Restriction;
10 use MediaWiki\MainConfigNames;
11
12 /**
13 * @group Database
14 * @group Blocking
15 * @coversDefaultClass \MediaWiki\Block\BlockRestrictionStore
16 */
17 class BlockRestrictionStoreTest extends \MediaWikiLangTestCase {
18
19 protected BlockRestrictionStore $blockRestrictionStore;
20
21 protected function setUp(): void {
22 parent::setUp();
23
24 $this->blockRestrictionStore = $this->getServiceContainer()->getBlockRestrictionStore();
25 }
26
27 /**
28 * @covers ::loadByBlockId
29 * @covers ::resultToRestrictions
30 * @covers ::rowToRestriction
31 */
32 public function testLoadMultipleRestrictions() {
33 $this->overrideConfigValue( MainConfigNames::BlockDisablesLogin, false );
34 $block = $this->insertBlock();
35
36 $pageFoo = $this->getExistingTestPage( 'Foo' );
37 $pageBar = $this->getExistingTestPage( 'Bar' );
38
39 $this->blockRestrictionStore->insert( [
40 new PageRestriction( $block->getId(), $pageFoo->getId() ),
41 new PageRestriction( $block->getId(), $pageBar->getId() ),
42 new NamespaceRestriction( $block->getId(), NS_USER ),
43 ] );
44
45 $restrictions = $this->blockRestrictionStore->loadByBlockId( $block->getId() );
46
47 $this->assertCount( 3, $restrictions );
48 }
49
50 /**
51 * @covers ::loadByBlockId
52 * @covers ::resultToRestrictions
53 * @covers ::rowToRestriction
54 */
55 public function testWithNoRestrictions() {
56 $block = $this->insertBlock();
57 $restrictions = $this->blockRestrictionStore->loadByBlockId( $block->getId() );
58 $this->assertSame( [], $restrictions );
59 }
60
61 /**
62 * @covers ::loadByBlockId
63 * @covers ::resultToRestrictions
64 * @covers ::rowToRestriction
65 */
66 public function testWithEmptyParam() {
67 $restrictions = $this->blockRestrictionStore->loadByBlockId( [] );
68 $this->assertSame( [], $restrictions );
69 }
70
71 /**
72 * @covers ::loadByBlockId
73 * @covers ::resultToRestrictions
74 * @covers ::rowToRestriction
75 */
76 public function testIgnoreNotSupportedTypes() {
77 $block = $this->insertBlock();
78
79 $pageFoo = $this->getExistingTestPage( 'Foo' );
80 $pageBar = $this->getExistingTestPage( 'Bar' );
81
82 // valid type
83 $this->insertRestriction( $block->getId(), PageRestriction::TYPE_ID, $pageFoo->getId() );
84 $this->insertRestriction( $block->getId(), NamespaceRestriction::TYPE_ID, NS_USER );
85
86 // invalid type
87 $this->insertRestriction( $block->getId(), 9, $pageBar->getId() );
88 $this->insertRestriction( $block->getId(), 10, NS_FILE );
89
90 $restrictions = $this->blockRestrictionStore->loadByBlockId( $block->getId() );
91 $this->assertCount( 2, $restrictions );
92 }
93
94 /**
95 * @covers ::loadByBlockId
96 * @covers ::resultToRestrictions
97 * @covers ::rowToRestriction
98 */
99 public function testMappingPageRestrictionObject() {
100 $block = $this->insertBlock();
101 $title = 'Lady Macbeth';
102 $page = $this->getExistingTestPage( $title );
103
104 // Test Page Restrictions.
105 $this->blockRestrictionStore->insert( [
106 new PageRestriction( $block->getId(), $page->getId() ),
107 ] );
108
109 $restrictions = $this->blockRestrictionStore->loadByBlockId( $block->getId() );
110
111 [ $pageRestriction ] = $restrictions;
112 $this->assertInstanceOf( PageRestriction::class, $pageRestriction );
113 $this->assertEquals( $block->getId(), $pageRestriction->getBlockId() );
114 $this->assertEquals( $page->getId(), $pageRestriction->getValue() );
115 $this->assertEquals( PageRestriction::TYPE, $pageRestriction->getType() );
116 $this->assertEquals( $pageRestriction->getTitle()->getText(), $title );
117 }
118
119 /**
120 * @covers ::loadByBlockId
121 * @covers ::resultToRestrictions
122 * @covers ::rowToRestriction
123 */
124 public function testMappingNamespaceRestrictionObject() {
125 $block = $this->insertBlock();
126
127 $this->blockRestrictionStore->insert( [
128 new NamespaceRestriction( $block->getId(), NS_USER ),
129 ] );
130
131 $restrictions = $this->blockRestrictionStore->loadByBlockId( $block->getId() );
132
133 [ $namespaceRestriction ] = $restrictions;
134 $this->assertInstanceOf( NamespaceRestriction::class, $namespaceRestriction );
135 $this->assertEquals( $block->getId(), $namespaceRestriction->getBlockId() );
136 $this->assertSame( NS_USER, $namespaceRestriction->getValue() );
137 $this->assertEquals( NamespaceRestriction::TYPE, $namespaceRestriction->getType() );
138 }
139
140 /**
141 * @covers ::insert
142 */
143 public function testInsert() {
144 $block = $this->insertBlock();
145
146 $pageFoo = $this->getExistingTestPage( 'Foo' );
147 $pageBar = $this->getExistingTestPage( 'Bar' );
148
149 $restrictions = [
150 new PageRestriction( $block->getId(), $pageFoo->getId() ),
151 new PageRestriction( $block->getId(), $pageBar->getId() ),
152 new NamespaceRestriction( $block->getId(), NS_USER )
153 ];
154
155 $result = $this->blockRestrictionStore->insert( $restrictions );
156 $this->assertTrue( $result );
157
158 $result = $this->blockRestrictionStore->insert( [] );
159 $this->assertFalse( $result );
160 }
161
162 /**
163 * @covers ::insert
164 */
165 public function testInsertTypes() {
166 $block = $this->insertBlock();
167
168 $pageFoo = $this->getExistingTestPage( 'Foo' );
169 $pageBar = $this->getExistingTestPage( 'Bar' );
170
171 $invalid = $this->createMock( Restriction::class );
172 $invalid->method( 'toRow' )
173 ->willReturn( [
174 'ir_ipb_id' => $block->getId(),
175 'ir_type' => 9,
176 'ir_value' => 42,
177 ] );
178
179 $restrictions = [
180 new PageRestriction( $block->getId(), $pageFoo->getId() ),
181 new PageRestriction( $block->getId(), $pageBar->getId() ),
182 new NamespaceRestriction( $block->getId(), NS_USER ),
183 $invalid,
184 ];
185
186 $result = $this->blockRestrictionStore->insert( $restrictions );
187 $this->assertTrue( $result );
188
189 $restrictions = $this->blockRestrictionStore->loadByBlockId( $block->getId() );
190 $this->assertCount( 3, $restrictions );
191 }
192
193 /**
194 * @covers ::update
195 * @covers ::restrictionsByBlockId
196 * @covers ::restrictionsToRemove
197 */
198 public function testUpdateInsert() {
199 $block = $this->insertBlock();
200 $pageFoo = $this->getExistingTestPage( 'Foo' );
201 $pageBar = $this->getExistingTestPage( 'Bar' );
202 $this->blockRestrictionStore->insert( [
203 new PageRestriction( $block->getId(), $pageFoo->getId() ),
204 ] );
205
206 $this->blockRestrictionStore->update( [
207 new PageRestriction( $block->getId(), $pageBar->getId() ),
208 new NamespaceRestriction( $block->getId(), NS_USER ),
209 ] );
210
211 $result = $this->getDb()->newSelectQueryBuilder()
212 ->select( [ '*' ] )
213 ->from( 'ipblocks_restrictions' )
214 ->where( [ 'ir_ipb_id' => $block->getId() ] )
215 ->fetchResultSet();
216
217 $this->assertEquals( 2, $result->numRows() );
218 $row = $result->fetchObject();
219 $this->assertEquals( $block->getId(), $row->ir_ipb_id );
220 $this->assertEquals( $pageBar->getId(), $row->ir_value );
221 }
222
223 /**
224 * @covers ::update
225 * @covers ::restrictionsByBlockId
226 * @covers ::restrictionsToRemove
227 */
228 public function testUpdateChange() {
229 $block = $this->insertBlock();
230 $page = $this->getExistingTestPage( 'Foo' );
231
232 $this->blockRestrictionStore->update( [
233 new PageRestriction( $block->getId(), $page->getId() ),
234 ] );
235
236 $result = $this->getDb()->newSelectQueryBuilder()
237 ->select( [ '*' ] )
238 ->from( 'ipblocks_restrictions' )
239 ->where( [ 'ir_ipb_id' => $block->getId() ] )
240 ->fetchResultSet();
241
242 $this->assertSame( 1, $result->numRows() );
243 $row = $result->fetchObject();
244 $this->assertEquals( $block->getId(), $row->ir_ipb_id );
245 $this->assertEquals( $page->getId(), $row->ir_value );
246 }
247
248 /**
249 * @covers ::update
250 * @covers ::restrictionsByBlockId
251 * @covers ::restrictionsToRemove
252 */
253 public function testUpdateNoRestrictions() {
254 $block = $this->insertBlock();
255
256 $this->blockRestrictionStore->update( [] );
257
258 $result = $this->getDb()->newSelectQueryBuilder()
259 ->select( [ '*' ] )
260 ->from( 'ipblocks_restrictions' )
261 ->where( [ 'ir_ipb_id' => $block->getId() ] )
262 ->fetchResultSet();
263
264 $this->assertSame( 0, $result->numRows() );
265 }
266
267 /**
268 * @covers ::update
269 * @covers ::restrictionsByBlockId
270 * @covers ::restrictionsToRemove
271 */
272 public function testUpdateSame() {
273 $block = $this->insertBlock();
274 $page = $this->getExistingTestPage( 'Foo' );
275 $this->blockRestrictionStore->insert( [
276 new PageRestriction( $block->getId(), $page->getId() ),
277 ] );
278
279 $this->blockRestrictionStore->update( [
280 new PageRestriction( $block->getId(), $page->getId() ),
281 ] );
282
283 $result = $this->getDb()->newSelectQueryBuilder()
284 ->select( [ '*' ] )
285 ->from( 'ipblocks_restrictions' )
286 ->where( [ 'ir_ipb_id' => $block->getId() ] )
287 ->fetchResultSet();
288
289 $this->assertSame( 1, $result->numRows() );
290 $row = $result->fetchObject();
291 $this->assertEquals( $block->getId(), $row->ir_ipb_id );
292 $this->assertEquals( $page->getId(), $row->ir_value );
293 }
294
295 /**
296 * @covers ::updateByParentBlockId
297 */
298 public function testDeleteAllUpdateByParentBlockId() {
299 // Create a block and an autoblock (a child block)
300 $block = $this->insertBlock();
301 $pageFoo = $this->getExistingTestPage( 'Foo' );
302 $pageBar = $this->getExistingTestPage( 'Bar' );
303 $this->blockRestrictionStore->insert( [
304 new PageRestriction( $block->getId(), $pageFoo->getId() ),
305 ] );
306 $autoblockId = $this->getServiceContainer()->getDatabaseBlockStore()
307 ->doAutoblock( $block, '127.0.0.1' );
308
309 // Ensure that the restrictions on the block have not changed.
310 $restrictions = $this->blockRestrictionStore->loadByBlockId( $block->getId() );
311 $this->assertCount( 1, $restrictions );
312 $this->assertEquals( $pageFoo->getId(), $restrictions[0]->getValue() );
313
314 // Ensure that the restrictions on the autoblock are the same as the block.
315 $restrictions = $this->blockRestrictionStore->loadByBlockId( $autoblockId );
316 $this->assertCount( 1, $restrictions );
317 $this->assertEquals( $pageFoo->getId(), $restrictions[0]->getValue() );
318
319 // Update the restrictions on the autoblock (but leave the block unchanged)
320 $this->blockRestrictionStore->updateByParentBlockId( $block->getId(), [
321 new PageRestriction( $block->getId(), $pageBar->getId() ),
322 ] );
323
324 // Ensure that the restrictions on the block have not changed.
325 $restrictions = $this->blockRestrictionStore->loadByBlockId( $block->getId() );
326 $this->assertCount( 1, $restrictions );
327 $this->assertEquals( $pageFoo->getId(), $restrictions[0]->getValue() );
328
329 // Ensure that the restrictions on the autoblock have been updated.
330 $restrictions = $this->blockRestrictionStore->loadByBlockId( $autoblockId );
331 $this->assertCount( 1, $restrictions );
332 $this->assertEquals( $pageBar->getId(), $restrictions[0]->getValue() );
333 }
334
335 /**
336 * @covers ::updateByParentBlockId
337 */
338 public function testUpdateByParentBlockId() {
339 // Create a block and an autoblock (a child block)
340 $block = $this->insertBlock();
341 $page = $this->getExistingTestPage( 'Foo' );
342 $this->blockRestrictionStore->insert( [
343 new PageRestriction( $block->getId(), $page->getId() ),
344 ] );
345 $autoblockId = $this->getServiceContainer()->getDatabaseBlockStore()
346 ->doAutoblock( $block, '127.0.0.1' );
347
348 // Ensure that the restrictions on the block have not changed.
349 $restrictions = $this->blockRestrictionStore->loadByBlockId( $block->getId() );
350 $this->assertCount( 1, $restrictions );
351
352 // Ensure that the restrictions on the autoblock have not changed.
353 $restrictions = $this->blockRestrictionStore->loadByBlockId( $autoblockId );
354 $this->assertCount( 1, $restrictions );
355
356 // Remove the restrictions on the autoblock (but leave the block unchanged)
357 $this->blockRestrictionStore->updateByParentBlockId( $block->getId(), [] );
358
359 // Ensure that the restrictions on the block have not changed.
360 $restrictions = $this->blockRestrictionStore->loadByBlockId( $block->getId() );
361 $this->assertCount( 1, $restrictions );
362
363 // Ensure that the restrictions on the autoblock have been updated.
364 $restrictions = $this->blockRestrictionStore->loadByBlockId( $autoblockId );
365 $this->assertSame( [], $restrictions );
366 }
367
368 /**
369 * @covers ::updateByParentBlockId
370 */
371 public function testNoAutoblocksUpdateByParentBlockId() {
372 // Create a block with no autoblock.
373 $block = $this->insertBlock();
374 $page = $this->getExistingTestPage( 'Foo' );
375 $this->blockRestrictionStore->insert( [
376 new PageRestriction( $block->getId(), $page->getId() ),
377 ] );
378
379 // Ensure that the restrictions on the block have not changed.
380 $restrictions = $this->blockRestrictionStore->loadByBlockId( $block->getId() );
381 $this->assertCount( 1, $restrictions );
382
383 // Update the restrictions on any autoblocks (there are none).
384 $this->blockRestrictionStore->updateByParentBlockId( $block->getId(), $restrictions );
385
386 // Ensure that the restrictions on the block have not changed.
387 $restrictions = $this->blockRestrictionStore->loadByBlockId( $block->getId() );
388 $this->assertCount( 1, $restrictions );
389 }
390
391 /**
392 * @covers ::delete
393 */
394 public function testDelete() {
395 $block = $this->insertBlock();
396 $page = $this->getExistingTestPage( 'Foo' );
397 $this->blockRestrictionStore->insert( [
398 new PageRestriction( $block->getId(), $page->getId() ),
399 ] );
400
401 $restrictions = $this->blockRestrictionStore->loadByBlockId( $block->getId() );
402 $this->assertCount( 1, $restrictions );
403
404 $result = $this->blockRestrictionStore->delete( $restrictions );
405 $this->assertTrue( $result );
406
407 $restrictions = $this->blockRestrictionStore->loadByBlockId( $block->getId() );
408 $this->assertSame( [], $restrictions );
409 }
410
411 /**
412 * @covers ::deleteByBlockId
413 */
414 public function testDeleteByBlockId() {
415 $block = $this->insertBlock();
416 $page = $this->getExistingTestPage( 'Foo' );
417 $this->blockRestrictionStore->insert( [
418 new PageRestriction( $block->getId(), $page->getId() ),
419 ] );
420
421 $restrictions = $this->blockRestrictionStore->loadByBlockId( $block->getId() );
422 $this->assertCount( 1, $restrictions );
423
424 $result = $this->blockRestrictionStore->deleteByBlockId( $block->getId() );
425 $this->assertNotFalse( $result );
426
427 $restrictions = $this->blockRestrictionStore->loadByBlockId( $block->getId() );
428 $this->assertSame( [], $restrictions );
429 }
430
431 /**
432 * @covers ::equals
433 * @dataProvider equalsDataProvider
434 *
435 * @param array $a
436 * @param array $b
437 * @param bool $expected
438 */
439 public function testEquals( array $a, array $b, $expected ) {
440 $this->assertSame( $expected, $this->blockRestrictionStore->equals( $a, $b ) );
441 }
442
443 public function equalsDataProvider() {
444 return [
445 [
446 [
447 new PageRestriction( 1, 1 ),
448 ],
449 [
450 new PageRestriction( 1, 2 )
451 ],
452 false,
453 ],
454 [
455 [
456 new PageRestriction( 1, 1 ),
457 ],
458 [
459 new PageRestriction( 1, 1 ),
460 new PageRestriction( 1, 2 )
461 ],
462 false,
463 ],
464 [
465 [],
466 [],
467 true,
468 ],
469 [
470 [
471 new PageRestriction( 1, 1 ),
472 new PageRestriction( 1, 2 ),
473 new PageRestriction( 2, 3 ),
474 ],
475 [
476 new PageRestriction( 2, 3 ),
477 new PageRestriction( 1, 2 ),
478 new PageRestriction( 1, 1 ),
479 ],
480 true
481 ],
482 [
483 [
484 new NamespaceRestriction( 1, NS_USER ),
485 ],
486 [
487 new NamespaceRestriction( 1, NS_USER ),
488 ],
489 true
490 ],
491 [
492 [
493 new NamespaceRestriction( 1, NS_USER ),
494 ],
495 [
496 new NamespaceRestriction( 1, NS_TALK ),
497 ],
498 false
499 ],
500 ];
501 }
502
503 /**
504 * @covers ::setBlockId
505 */
506 public function testSetBlockId() {
507 $restrictions = [
508 new PageRestriction( 1, 1 ),
509 new PageRestriction( 1, 2 ),
510 new NamespaceRestriction( 1, NS_USER ),
511 ];
512
513 $this->assertSame( 1, $restrictions[0]->getBlockId() );
514 $this->assertSame( 1, $restrictions[1]->getBlockId() );
515 $this->assertSame( 1, $restrictions[2]->getBlockId() );
516
517 $result = $this->blockRestrictionStore->setBlockId( 2, $restrictions );
518
519 foreach ( $result as $restriction ) {
520 $this->assertSame( 2, $restriction->getBlockId() );
521 }
522 }
523
524 protected function insertBlock() {
525 $badActor = $this->getTestUser()->getUser();
526 $sysop = $this->getTestSysop()->getUser();
527
528 $block = new DatabaseBlock( [
529 'address' => $badActor,
530 'by' => $sysop,
531 'expiry' => 'infinity',
532 'sitewide' => 0,
533 'enableAutoblock' => true,
534 ] );
535
536 $this->getServiceContainer()->getDatabaseBlockStore()->insertBlock( $block );
537
538 return $block;
539 }
540
541 protected function insertRestriction( $blockId, $type, $value ) {
542 $this->getDb()->newInsertQueryBuilder()
543 ->insertInto( 'ipblocks_restrictions' )
544 ->row( [
545 'ir_ipb_id' => $blockId,
546 'ir_type' => $type,
547 'ir_value' => $value,
548 ] )
549 ->caller( __METHOD__ )
550 ->execute();
551 }
552 }