Debian Codegraph @

tests/phpunit/integration/includes/block/DatabaseBlockStoreTest.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\Block\DatabaseBlock;
4 use MediaWiki\Block\DatabaseBlockStore;
5 use MediaWiki\Block\Restriction\NamespaceRestriction;
6 use MediaWiki\Config\ServiceOptions;
7 use MediaWiki\HookContainer\HookContainer;
8 use MediaWiki\MainConfigNames;
9 use MediaWiki\Tests\Unit\DummyServicesTrait;
10 use MediaWiki\User\User;
11 use Psr\Log\NullLogger;
12 use Wikimedia\IPUtils;
13
14 /**
15 * Integration tests for DatabaseBlockStore.
16 *
17 * @author DannyS712
18 * @group Blocking
19 * @group Database
20 * @covers \MediaWiki\Block\DatabaseBlockStore
21 * @coversDefaultClass \MediaWiki\Block\DatabaseBlockStore
22 */
23 class DatabaseBlockStoreTest extends MediaWikiIntegrationTestCase {
24 use DummyServicesTrait;
25
26 private User $sysop;
27 private int $expiredBlockId = 11111;
28 private int $unexpiredBlockId = 22222;
29 private int $autoblockId = 33333;
30
31 /**
32 * @param array $options
33 * - config: Override the ServiceOptions config
34 * - constructorArgs: Override the constructor arguments
35 * @return DatabaseBlockStore
36 */
37 private function getStore( array $options = [] ): DatabaseBlockStore {
38 $overrideConfig = $options['config'] ?? [];
39 $overrideConstructorArgs = $options['constructorArgs'] ?? [];
40
41 $defaultConfig = [
42 MainConfigNames::AutoblockExpiry => 86400,
43 MainConfigNames::BlockCIDRLimit => [ 'IPv4' => 16, 'IPv6' => 19 ],
44 MainConfigNames::BlockDisablesLogin => false,
45 MainConfigNames::PutIPinRC => true,
46 MainConfigNames::UpdateRowsPerQuery => 10,
47 ];
48 $config = array_merge( $defaultConfig, $overrideConfig );
49
50 // This ensures continuation after hooks
51 $hookContainer = $this->createMock( HookContainer::class );
52 $hookContainer->method( 'run' )
53 ->willReturn( true );
54
55 // Most tests need read only to be false
56 $readOnlyMode = $this->getDummyReadOnlyMode( false );
57
58 $services = $this->getServiceContainer();
59 $defaultConstructorArgs = [
60 'serviceOptions' => new ServiceOptions(
61 DatabaseBlockStore::CONSTRUCTOR_OPTIONS,
62 $config
63 ),
64 'logger' => new NullLogger(),
65 'actorStoreFactory' => $services->getActorStoreFactory(),
66 'blockRestrictionStore' => $services->getBlockRestrictionStore(),
67 'commentStore' => $services->getCommentStore(),
68 'hookContainer' => $hookContainer,
69 'dbProvider' => $services->getDBLoadBalancerFactory(),
70 'readOnlyMode' => $readOnlyMode,
71 'userFactory' => $services->getUserFactory(),
72 'tempUserConfig' => $services->getTempUserConfig(),
73 'blockUtils' => $services->getBlockUtils(),
74 'autoblockExemptionList' => $services->getAutoblockExemptionList(),
75 ];
76 $constructorArgs = array_merge( $defaultConstructorArgs, $overrideConstructorArgs );
77
78 return new DatabaseBlockStore( ...array_values( $constructorArgs ) );
79 }
80
81 /**
82 * @param array $options
83 * - target: The intended target, an unblocked user by default
84 * - autoblock: Whether this block is autoblocking
85 * @return DatabaseBlock
86 */
87 private function getBlock( array $options = [] ): DatabaseBlock {
88 $target = $options['target'] ?? $this->getTestUser()->getUser();
89 $autoblock = $options['autoblock'] ?? false;
90
91 return new DatabaseBlock( [
92 'by' => $this->sysop,
93 'address' => $target,
94 'enableAutoblock' => $autoblock,
95 ] );
96 }
97
98 /**
99 * Check that an autoblock corresponds to a parent block. The following are not
100 * required to be equal, so are not tested:
101 * - target
102 * - type
103 * - expiry
104 * - autoblocking
105 *
106 * @param DatabaseBlock $block
107 * @param DatabaseBlock $autoblock
108 */
109 private function assertAutoblockEqualsBlock(
110 DatabaseBlock $block,
111 DatabaseBlock $autoblock
112 ) {
113 $this->assertSame( $autoblock->getParentBlockId(), $block->getId() );
114 $this->assertSame( $autoblock->isHardblock(), $block->isHardblock() );
115 $this->assertSame( $autoblock->isCreateAccountBlocked(), $block->isCreateAccountBlocked() );
116 $this->assertSame( $autoblock->getHideName(), $block->getHideName() );
117 $this->assertSame( $autoblock->isEmailBlocked(), $block->isEmailBlocked() );
118 $this->assertSame( $autoblock->isUsertalkEditAllowed(), $block->isUsertalkEditAllowed() );
119 $this->assertSame( $autoblock->isSitewide(), $block->isSitewide() );
120 $this->assertSame(
121 $autoblock->getReasonComment()->text,
122 wfMessage( 'autoblocker', $block->getTargetName(), $block->getReasonComment()->text )->text()
123 );
124
125 $restrictionStore = $this->getServiceContainer()->getBlockRestrictionStore();
126 $this->assertTrue(
127 $restrictionStore->equals(
128 $autoblock->getRestrictions(),
129 $block->getRestrictions()
130 )
131 );
132 }
133
134 /**
135 * @covers ::newFromID
136 * @covers ::newListFromTarget
137 * @covers ::newFromRow
138 */
139 public function testNewFromID_exists() {
140 $block = new DatabaseBlock( [
141 'address' => '1.2.3.4',
142 'by' => $this->getTestSysop()->getUser(),
143 ] );
144 $store = $this->getStore();
145 $inserted = $store->insertBlock( $block );
146 $this->assertTrue(
147 (bool)$inserted['id'],
148 'Block inserted correctly'
149 );
150
151 $blockId = $inserted['id'];
152 $newFromIdRes = $store->newFromID( $blockId );
153 $this->assertInstanceOf(
154 DatabaseBlock::class,
155 $newFromIdRes,
156 'Looking up an existing block by id'
157 );
158
159 $newListRes = $store->newListFromTarget( "#$blockId" );
160 $this->assertCount(
161 1,
162 $newListRes,
163 'newListFromTarget with a block id for an existing block'
164 );
165 $this->assertInstanceOf(
166 DatabaseBlock::class,
167 $newListRes[0],
168 'DatabaseBlock returned'
169 );
170 $this->assertSame(
171 $blockId,
172 $newListRes[0]->getId(),
173 'Block returned is the correct one'
174 );
175 }
176
177 /**
178 * @covers ::newFromID
179 * @covers ::newListFromTarget
180 */
181 public function testNewFromID_missing() {
182 $store = $this->getStore();
183 $missingBlockId = 9998;
184 $dbRow = $this->getDb()->newSelectQueryBuilder()
185 ->select( '*' )
186 ->from( 'block' )
187 ->where( [ 'bl_id' => $missingBlockId ] )
188 ->caller( __METHOD__ )
189 ->fetchRow();
190 $this->assertFalse(
191 $dbRow,
192 "Sanity check: make sure there is no block with id $missingBlockId"
193 );
194
195 $newFromIdRes = $store->newFromID( $missingBlockId );
196 $this->assertNull(
197 $newFromIdRes,
198 'Looking up a missing block by id'
199 );
200
201 $newListRes = $store->newListFromTarget( "#$missingBlockId" );
202 $this->assertCount(
203 0,
204 $newListRes,
205 'newListFromTarget with a block id for a missing block'
206 );
207 }
208
209 /**
210 * @covers ::getQueryInfo
211 */
212 public function testGetQueryInfo() {
213 // We don't list all of the fields that should be included, because that just
214 // duplicates the function itself. Instead, check the structure and the field
215 // aliases. The fact that this query info is everything needed to create a block
216 // is validated by its uses within the service
217 $queryInfo = $this->getStore()->getQueryInfo();
218 $this->assertArrayHasKey( 'tables', $queryInfo );
219 $this->assertArrayHasKey( 'fields', $queryInfo );
220 $this->assertArrayHasKey( 'joins', $queryInfo );
221
222 $this->assertIsArray( $queryInfo['fields'] );
223 $this->assertArrayHasKey( 'bl_by', $queryInfo['fields'] );
224 $this->assertSame( 'block_by_actor.actor_user', $queryInfo['fields']['bl_by'] );
225 $this->assertArrayHasKey( 'bl_by_text', $queryInfo['fields'] );
226 $this->assertSame( 'block_by_actor.actor_name', $queryInfo['fields']['bl_by_text'] );
227 }
228
229 /**
230 * @covers ::newListFromIPs
231 * @covers ::newFromRow
232 */
233 public function testNewListFromIPs() {
234 $block = new DatabaseBlock( [
235 'address' => '1.2.3.4',
236 'by' => $this->getTestSysop()->getUser(),
237 ] );
238 $store = $this->getStore();
239 $inserted = $store->insertBlock( $block );
240 $this->assertTrue(
241 (bool)$inserted['id'],
242 'Sanity check: block inserted correctly'
243 );
244
245 // Early return of empty array if no ips in the list
246 $list = $store->newListFromIPs( [], true );
247 $this->assertCount(
248 0,
249 $list,
250 'No matching blocks'
251 );
252
253 // Empty array for no match
254 $list = $store->newListFromIPs(
255 [ '10.1.1.1', '192.168.1.1' ],
256 true
257 );
258 $this->assertCount(
259 0,
260 $list,
261 'No blocks retrieved if all ips are invalid or trusted proxies'
262 );
263
264 // Actually fetching, block was inserted above
265 $list = $store->newListFromIPs( [ '1.2.3.4' ], true );
266 $this->assertCount(
267 1,
268 $list,
269 'Block retrieved for the blocked ip'
270 );
271 $this->assertInstanceOf(
272 DatabaseBlock::class,
273 $list[0],
274 'Sanity check: DatabaseBlock returned'
275 );
276 $this->assertSame(
277 $inserted['id'],
278 $list[0]->getId(),
279 'Block returned is the correct one'
280 );
281 }
282
283 public static function provideGetRangeCond() {
284 // $start, $end, $expect
285 $hex1 = IPUtils::toHex( '1.2.3.4' );
286 $hex2 = IPUtils::toHex( '1.2.3.5' );
287 yield 'IPv4 start, same end' => [
288 $hex1,
289 null,
290 "((bt_ip_hex = '$hex1' AND bt_range_start IS NULL)"
291 . " OR (bt_range_start LIKE '0102%' ESCAPE '`'"
292 . " AND bt_range_start <= '$hex1'"
293 . " AND bt_range_end >= '$hex1'))"
294 ];
295 yield 'IPv4 start, different end' => [
296 $hex1,
297 $hex2,
298 "(bt_range_start LIKE '0102%' ESCAPE '`'"
299 . " AND bt_range_start <= '$hex1'"
300 . " AND bt_range_end >= '$hex2')"
301 ];
302 $hex3 = IPUtils::toHex( '2000:DEAD:BEEF:A:0:0:0:0' );
303 $hex4 = IPUtils::toHex( '2000:DEAD:BEEF:A:0:0:000A:000F' );
304 yield 'IPv6 start, same end' => [
305 $hex3,
306 null,
307 "((bt_ip_hex = '$hex3' AND bt_range_start IS NULL)"
308 . " OR (bt_range_start LIKE 'v6-2000%' ESCAPE '`'"
309 . " AND bt_range_start <= '$hex3'"
310 . " AND bt_range_end >= '$hex3'))"
311 ];
312 yield 'IPv6 start, different end' => [
313 $hex3,
314 $hex4,
315 "(bt_range_start LIKE 'v6-2000%' ESCAPE '`'"
316 . " AND bt_range_start <= '$hex3'"
317 . " AND bt_range_end >= '$hex4')"
318 ];
319 }
320
321 /**
322 * @dataProvider provideGetRangeCond
323 * @covers ::getRangeCond
324 * @covers ::getIpFragment
325 */
326 public function testGetRangeCond( $start, $end, $expect ) {
327 $this->assertSame(
328 $expect,
329 $this->getStore()->getRangeCond( $start, $end ) );
330 }
331
332 public static function provideGetRangeCondIntegrated() {
333 return [
334 'single IP block' => [ '3.3.3.3', '3.3.3.3', true ],
335 '/32 range blocks single IP' => [ '3.3.3.3/32', '3.3.3.3', true ],
336 'single IP block mismatch' => [ '3.3.3.3', '3.3.3.4', false ],
337 '/32 range mismatch' => [ '3.3.3.3/32', '3.3.3.4', false ],
338 '/24 match' => [ '3.3.3.0/24', '3.3.3.0', true ],
339 '/24 mismatch' => [ '3.3.3.0/24', '3.3.4.0', false ],
340 'range search exact match' => [ '3.3.3.0/24', '3.3.3.0/24', true ],
341 'encompassing range match' => [ '3.3.3.0/24', '3.3.3.1/27', true ],
342 'excessive range mismatch' => [ '3.3.0.0/24', '3.3.0.0/22', false ],
343 ];
344 }
345
346 /**
347 * Test getRangeCond() by inserting blocks and checking for matches
348 *
349 * @dataProvider provideGetRangeCondIntegrated
350 * @param string $blockTarget
351 * @param string $searchTarget
352 * @param bool $isBlocked
353 */
354 public function testGetRangeCondIntegrated( $blockTarget, $searchTarget, $isBlocked ) {
355 $store = $this->getStore();
356 $store->insertBlock( $this->getBlock( [ 'target' => $blockTarget ] ) );
357 [ $start, $end ] = IPUtils::parseRange( $searchTarget );
358 $rows = $this->getDb()->newSelectQueryBuilder()
359 ->queryInfo( $store->getQueryInfo() )
360 ->where( $store->getRangeCond( $start, $end ) )
361 ->fetchResultSet();
362 $this->assertSame( $isBlocked ? 1 : 0, $rows->numRows() );
363 }
364
365 /**
366 * @dataProvider provideInsertBlockSuccess
367 */
368 public function testInsertBlockSuccess( $options ) {
369 $block = $this->getBlock( $options['block'] ?? [] );
370 $block->setRestrictions( [
371 new NamespaceRestriction( 0, NS_MAIN ),
372 ] );
373
374 $store = $this->getStore( $options['store'] ?? [] );
375 $result = $store->insertBlock( $block );
376
377 $this->assertIsArray( $result );
378 $this->assertArrayHasKey( 'id', $result );
379 $this->assertArrayHasKey( 'autoIds', $result );
380 $this->assertCount( 0, $result['autoIds'] );
381
382 $retrievedBlock = $store->newFromID( $result['id'] );
383 $this->assertTrue( $block->equals( $retrievedBlock ) );
384 }
385
386 public static function provideInsertBlockSuccess() {
387 return [
388 'No conflicting block, not autoblocking' => [
389 'block' => [
390 'autoblock' => false,
391 ],
392 ],
393 'No conflicting block, autoblocking but IP not in recent changes' => [
394 [
395 'block' => [
396 'autoblock' => true,
397 ],
398 'store' => [
399 'constructorArgs' => [ MainConfigNames::PutIPinRC => false ],
400 ],
401 ],
402 ],
403 'No conflicting block, autoblocking but no recent edits' => [
404 'block' => [
405 'autoblock' => true,
406 ],
407 ],
408 'Conflicting block, expired' => [
409 'block' => [
410 // Blocked with expired block in addDBData
411 'target' => '1.1.1.1',
412 ],
413 ],
414 ];
415 }
416
417 public function testInsertBlockConflict() {
418 $block = $this->getBlock( [ 'target' => $this->sysop ] );
419
420 $store = $this->getStore();
421 $result = $store->insertBlock( $block );
422
423 $this->assertFalse( $result );
424 $this->assertNull( $block->getId() );
425 }
426
427 /**
428 * @dataProvider provideInsertBlockLogout
429 */
430 public function testInsertBlockLogout( $options, $expectTokenEqual ) {
431 $block = $this->getBlock();
432 $userFactory = $this->getServiceContainer()->getUserFactory();
433 $targetToken = $userFactory->newFromUserIdentity( $block->getTargetUserIdentity() )->getToken();
434
435 $store = $this->getStore( $options );
436 $store->insertBlock( $block );
437
438 $this->assertSame(
439 $expectTokenEqual,
440 $targetToken === $userFactory->newFromUserIdentity( $block->getTargetUserIdentity() )->getToken()
441 );
442 }
443
444 public static function provideInsertBlockLogout() {
445 return [
446 'Blocked user can log in' => [
447 [
448 'config' => [ MainConfigNames::BlockDisablesLogin => false ],
449 ],
450 true,
451 ],
452 'Blocked user cannot log in' => [
453 [
454 'config' => [ MainConfigNames::BlockDisablesLogin => true ],
455 ],
456 false,
457 ],
458 ];
459 }
460
461 public function testInsertBlockAutoblock() {
462 // This is quicker than adding a recent change for an unblocked user.
463 // See addDBDataOnce documentation for more details.
464 $target = $this->sysop;
465 $store = $this->getStore();
466 $store->deleteBlocksMatchingConds( [ 'bt_user' => $target->getId() ] );
467 $block = $this->getBlock( [
468 'autoblock' => true,
469 'target' => $target,
470 ] );
471
472 $result = $store->insertBlock( $block );
473
474 $this->assertIsArray( $result );
475 $this->assertArrayHasKey( 'autoIds', $result );
476 $this->assertCount( 1, $result['autoIds'] );
477
478 $retrievedBlock = $store->newFromID( $result['autoIds'][0] );
479 $this->assertSame( $block->getId(), $retrievedBlock->getParentBlockId() );
480 $this->assertAutoblockEqualsBlock( $block, $retrievedBlock );
481 }
482
483 public function testInsertBlockError() {
484 $block = $this->createMock( DatabaseBlock::class );
485
486 $this->expectException( InvalidArgumentException::class );
487 $this->expectExceptionMessage( 'insert' );
488
489 $store = $this->getStore();
490 $store->insertBlock( $block );
491 }
492
493 public function testUpdateBlock() {
494 $store = $this->getStore();
495 $existingBlock = $store->newFromTarget( $this->sysop );
496
497 // Insert an autoblock for T351173 regression testing
498 $autoblockId = $store->doAutoblock( $existingBlock, '127.0.0.1' );
499
500 // Modify a block option
501 $existingBlock->isUsertalkEditAllowed( true );
502 $newExpiry = wfTimestamp( TS_MW, time() + 1000 );
503 $existingBlock->setExpiry( $newExpiry );
504
505 $result = $store->updateBlock( $existingBlock );
506
507 $updatedBlock = $store->newFromID( $result['id'] );
508 $autoblock = $store->newFromID( $autoblockId );
509
510 $this->assertTrue( $updatedBlock->equals( $existingBlock ) );
511 $this->assertAutoblockEqualsBlock( $existingBlock, $autoblock );
512 $this->assertLessThanOrEqual( $newExpiry, $autoblock->getExpiry() );
513 }
514
515 public function testUpdateBlockAddOrRemoveAutoblock() {
516 $store = $this->getStore();
517 // Existing block is autoblocking to begin with
518 $existingBlock = $store->newFromTarget( $this->sysop );
519 $existingBlock->isAutoblocking( false );
520
521 $result = $store->updateBlock( $existingBlock );
522
523 $updatedBlock = $store->newFromID( $result['id'] );
524
525 $this->assertTrue( $updatedBlock->equals( $existingBlock ) );
526 $this->assertCount( 0, $result['autoIds'] );
527
528 // Test adding an autoblock in the same test run, since we need the
529 // target to be the sysop (see addDBDataOnce documentation), and the
530 // sysop is blocked with an autoblock between test runs.
531 $existingBlock->isAutoblocking( true );
532 $result = $store->updateBlock( $existingBlock );
533
534 $updatedBlock = $store->newFromID( $result['id'] );
535 $autoblock = $store->newFromID( $result['autoIds'][0] );
536
537 $this->assertTrue( $updatedBlock->equals( $existingBlock ) );
538 $this->assertAutoblockEqualsBlock( $existingBlock, $autoblock );
539 }
540
541 /**
542 * @dataProvider provideUpdateBlockRestrictions
543 */
544 public function testUpdateBlockRestrictions( $expectedCount ) {
545 $store = $this->getStore();
546 $existingBlock = $store->newFromTarget( $this->sysop );
547 $restrictions = [];
548 for ( $ns = 0; $ns < $expectedCount; $ns++ ) {
549 $restrictions[] = new NamespaceRestriction( $existingBlock->getId(), $ns );
550 }
551 $existingBlock->setRestrictions( $restrictions );
552
553 $result = $store->updateBlock( $existingBlock );
554
555 $retrievedBlock = $store->newFromID( $result['id'] );
556 $this->assertCount(
557 $expectedCount,
558 $retrievedBlock->getRestrictions()
559 );
560 }
561
562 public static function provideUpdateBlockRestrictions() {
563 return [
564 'Restrictions deleted if removed' => [ 0 ],
565 'Restrictions changed if updated' => [ 2 ],
566 ];
567 }
568
569 public function testDeleteBlockSuccess() {
570 $store = $this->getStore();
571 $target = $this->sysop;
572 $block = $store->newFromTarget( $target );
573
574 $this->assertTrue( $store->deleteBlock( $block ) );
575 $this->assertNull( $store->newFromTarget( $target ) );
576 }
577
578 public function testDeleteBlockFailureReadOnly() {
579 $store = $this->getStore( [
580 'constructorArgs' => [
581 'readOnlyMode' => $this->getDummyReadOnlyMode( true )
582 ],
583 ] );
584 $target = $this->sysop;
585 $block = $store->newFromTarget( $target );
586
587 $this->assertFalse( $store->deleteBlock( $block ) );
588 $this->assertTrue( (bool)$store->newFromTarget( $target ) );
589 }
590
591 public function testDeleteBlockFailureNoBlockId() {
592 $block = $this->createMock( DatabaseBlock::class );
593 $block->method( 'getId' )
594 ->willReturn( null );
595 $block->method( 'getWikiId' )
596 ->willReturn( DatabaseBlock::LOCAL );
597
598 $this->expectException( InvalidArgumentException::class );
599 $this->expectExceptionMessage( 'delete' );
600
601 $store = $this->getStore();
602 $store->deleteBlock( $block );
603 }
604
605 /**
606 * Check whether expired blocks and restrictions were removed from the database.
607 *
608 * @param int $blockId
609 * @param bool $expected Whether to expect to find any rows
610 */
611 private function assertPurgeWorked( int $blockId, bool $expected ): void {
612 $blockRows = (bool)$this->getDb()->newSelectQueryBuilder()
613 ->select( 'bl_id' )
614 ->from( 'block' )
615 ->where( [ 'bl_id' => $blockId ] )
616 ->fetchResultSet()->numRows();
617 $blockRestrictionsRows = (bool)$this->getDb()->newSelectQueryBuilder()
618 ->select( 'ir_ipb_id' )
619 ->from( 'ipblocks_restrictions' )
620 ->where( [ 'ir_ipb_id' => $blockId ] )
621 ->fetchResultSet()->numRows();
622
623 $this->assertSame( $expected, $blockRows );
624 $this->assertSame( $expected, $blockRestrictionsRows );
625 }
626
627 public function testPurgeExpiredBlocksSuccess() {
628 $store = $this->getStore();
629 $store->purgeExpiredBlocks();
630
631 $this->assertPurgeWorked( $this->expiredBlockId, false );
632 $this->assertPurgeWorked( $this->unexpiredBlockId, true );
633 }
634
635 public function testPurgeExpiredBlocksFailureReadOnly() {
636 $store = $this->getStore( [
637 'constructorArgs' => [
638 'readOnlyMode' => $this->getDummyReadOnlyMode( true ),
639 ],
640 ] );
641 $store->purgeExpiredBlocks();
642
643 $this->assertPurgeWorked( $this->expiredBlockId, true );
644 }
645
646 /**
647 * In order to autoblock a user, they must have a recent change.
648 *
649 * Make a recent change for the test sysop. This user persists between test runs,
650 * so will always have this recent change.
651 *
652 * Regular test users don't persist between test runs, because the TestUserRegistry
653 * is cleared between runs. If we tested autoblocking on a regular test user, we
654 * would need to make a recent change for each test, which is slow.
655 *
656 * Instead we always test autoblocks on the test sysop.
657 */
658 public function addDBDataOnce() {
659 $this->editPage(
660 'DatabaseBlockStoreTest test page',
661 'an edit',
662 'a summary',
663 NS_MAIN,
664 $this->getTestSysop()->getUser()
665 );
666 }
667
668 /**
669 * Three blocks are added:
670 * - an expired block with restrictions, against an IP
671 * - a current block with restrictions, against a user with recent changes
672 * - a current autoblock from the current block above
673 */
674 public function addDBData() {
675 $this->sysop = $this->getTestSysop()->getUser();
676
677 // Get a comment ID. One was added in addDBDataOnce.
678 $commentId = $this->getDb()->newSelectQueryBuilder()
679 ->select( 'comment_id' )
680 ->from( 'comment' )
681 ->caller( __METHOD__ )
682 ->fetchField();
683
684 $commonBlockData = [
685 'bl_by_actor' => $this->sysop->getActorId(),
686 'bl_reason_id' => $commentId,
687 'bl_timestamp' => $this->getDb()->timestamp( '20000101000000' ),
688 'bl_anon_only' => 0,
689 'bl_create_account' => 0,
690 'bl_deleted' => 0,
691 'bl_block_email' => 0,
692 'bl_allow_usertalk' => 0,
693 'bl_sitewide' => 0,
694 ];
695
696 $targetRows = [
697 '1.1.1.1' => [
698 'bt_address' => '1.1.1.1',
699 'bt_ip_hex' => IPUtils::toHex( '1.1.1.1' ),
700 'bt_auto' => 0,
701 ],
702 'sysop' => [
703 'bt_user' => $this->sysop->getId(),
704 'bt_user_text' => $this->sysop->getName(),
705 'bt_auto' => 0,
706 ],
707 '2.2.2.2' => [
708 'bt_address' => '2.2.2.2',
709 'bt_ip_hex' => IPUtils::toHex( '2.2.2.2' ),
710 'bt_auto' => 1,
711 ]
712 ];
713 $targetIds = [];
714 foreach ( $targetRows as $i => $row ) {
715 $this->getDb()->newInsertQueryBuilder()
716 ->insertInto( 'block_target' )
717 ->row( $row + [ 'bt_count' => 1 ] )
718 ->execute();
719 $targetIds[$i] = $this->getDb()->insertId();
720 }
721
722 $blockData = [
723 [
724 'bl_id' => $this->expiredBlockId,
725 'bl_target' => $targetIds['1.1.1.1'],
726 'bl_expiry' => $this->getDb()->timestamp( '20010101000000' ),
727 'bl_enable_autoblock' => 0,
728 'bl_parent_block_id' => 0,
729 ] + $commonBlockData,
730 [
731 'bl_id' => $this->unexpiredBlockId,
732 'bl_target' => $targetIds['sysop'],
733 'bl_expiry' => $this->getDb()->getInfinity(),
734 'bl_enable_autoblock' => 1,
735 'bl_parent_block_id' => 0,
736 ] + $commonBlockData,
737 [
738 'bl_id' => $this->autoblockId,
739 'bl_target' => $targetIds['2.2.2.2'],
740 'bl_expiry' => $this->getDb()->getInfinity(),
741 'bl_enable_autoblock' => 0,
742 'bl_parent_block_id' => $this->unexpiredBlockId,
743 ] + $commonBlockData,
744 ];
745
746 $restrictionData = [
747 [
748 'ir_ipb_id' => $this->expiredBlockId,
749 'ir_type' => 1,
750 'ir_value' => 1,
751 ],
752 [
753 'ir_ipb_id' => $this->unexpiredBlockId,
754 'ir_type' => 2,
755 'ir_value' => 2,
756 ],
757 [
758 'ir_ipb_id' => $this->autoblockId,
759 'ir_type' => 2,
760 'ir_value' => 2,
761 ],
762 ];
763
764 $this->getDb()->newInsertQueryBuilder()
765 ->insertInto( 'block' )
766 ->rows( $blockData )
767 ->caller( __METHOD__ )
768 ->execute();
769
770 $this->getDb()->newInsertQueryBuilder()
771 ->insertInto( 'ipblocks_restrictions' )
772 ->rows( $restrictionData )
773 ->caller( __METHOD__ )
774 ->execute();
775 }
776
777 }