| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Maintenance; |
| 4 |
|
| 5 |
use BlockUsers; |
| 6 |
use MediaWiki\MainConfigNames; |
| 7 |
use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait; |
| 8 |
use MediaWiki\User\User; |
| 9 |
use MediaWiki\User\UserIdentity; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
class BlockUsersTest extends MaintenanceBaseTestCase { |
| 17 |
use MockAuthorityTrait; |
| 18 |
|
| 19 |
protected function getMaintenanceClass() { |
| 20 |
return BlockUsers::class; |
| 21 |
} |
| 22 |
|
| 23 |
private function commonTestExecute( $options, $fileContents ) { |
| 24 |
|
| 25 |
foreach ( $options as $name => $value ) { |
| 26 |
$this->maintenance->setOption( $name, $value ); |
| 27 |
} |
| 28 |
|
| 29 |
$file = $this->getNewTempFile(); |
| 30 |
file_put_contents( $file, $fileContents ); |
| 31 |
$this->maintenance->setArg( 'file', $file ); |
| 32 |
|
| 33 |
$this->maintenance->execute(); |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
private function getTestUnblockedUsers( int $count ): array { |
| 41 |
$returnArray = []; |
| 42 |
for ( $i = 0; $i < $count; $i++ ) { |
| 43 |
$returnArray[] = $this->getMutableTestUser()->getUserIdentity(); |
| 44 |
} |
| 45 |
return $returnArray; |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
private function getTestBlockedUsers( int $count ): array { |
| 53 |
$returnArray = $this->getTestUnblockedUsers( $count ); |
| 54 |
$blockManager = $this->getServiceContainer()->getBlockManager(); |
| 55 |
$blockUserFactory = $this->getServiceContainer()->getBlockUserFactory(); |
| 56 |
foreach ( $returnArray as $user ) { |
| 57 |
$blockUserFactory->newBlockUser( $user, $this->mockRegisteredUltimateAuthority(), 'infinite', 'test' ) |
| 58 |
->placeBlock(); |
| 59 |
$this->assertNotNull( $blockManager->getBlock( $user, null ) ); |
| 60 |
} |
| 61 |
return $returnArray; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
public function testExecuteForUnblock( $blockedUserCount, $notBlockedUserCount ) { |
| 66 |
$blockedUsers = $this->getTestBlockedUsers( $blockedUserCount ); |
| 67 |
$unblockedUsers = $this->getTestUnblockedUsers( $notBlockedUserCount ); |
| 68 |
|
| 69 |
$fileContents = ''; |
| 70 |
$expectedOutputRegex = '/'; |
| 71 |
foreach ( $blockedUsers as $user ) { |
| 72 |
$fileContents .= $user->getName() . PHP_EOL; |
| 73 |
$userNameForRegex = preg_quote( $user->getName() ); |
| 74 |
$expectedOutputRegex .= ".*Unblocking '$userNameForRegex' succeeded.\n"; |
| 75 |
} |
| 76 |
foreach ( $unblockedUsers as $user ) { |
| 77 |
$fileContents .= $user->getName() . PHP_EOL; |
| 78 |
$userNameForRegex = preg_quote( $user->getName() ); |
| 79 |
$expectedOutputRegex .= ".*Unblocking '$userNameForRegex' failed.*\n"; |
| 80 |
} |
| 81 |
$options['unblock'] = true; |
| 82 |
$this->commonTestExecute( $options, $fileContents ); |
| 83 |
$this->expectOutputRegex( $expectedOutputRegex . '/' ); |
| 84 |
|
| 85 |
$blockManager = $this->getServiceContainer()->getBlockManager(); |
| 86 |
foreach ( $blockedUsers as $user ) { |
| 87 |
$blockManager->clearUserCache( $user ); |
| 88 |
$this->assertNull( $blockManager->getBlock( $user, null ), 'User was not actually unblocked.' ); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
public static function provideExecuteForUnblock() { |
| 93 |
return [ |
| 94 |
'2 blocked users and 1 unblocked user' => [ 2, 1 ], |
| 95 |
'3 blocked users' => [ 3, 0 ], |
| 96 |
]; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
public function testExecuteForBlock( $options, $blockedUserCount, $notBlockedUserCount ) { |
| 101 |
$blockedUsers = $this->getTestBlockedUsers( $blockedUserCount ); |
| 102 |
$unblockedUsers = $this->getTestUnblockedUsers( $notBlockedUserCount ); |
| 103 |
|
| 104 |
$fileContents = ''; |
| 105 |
$expectedOutputRegex = '/'; |
| 106 |
foreach ( $unblockedUsers as $user ) { |
| 107 |
$fileContents .= $user->getName() . PHP_EOL; |
| 108 |
$userNameForRegex = preg_quote( $user->getName() ); |
| 109 |
$expectedOutputRegex .= ".*Blocking '$userNameForRegex' succeeded.\n"; |
| 110 |
} |
| 111 |
foreach ( $blockedUsers as $user ) { |
| 112 |
$fileContents .= $user->getName() . PHP_EOL; |
| 113 |
$userNameForRegex = preg_quote( $user->getName() ); |
| 114 |
$expectedOutputRegex .= ".*Blocking '$userNameForRegex' failed.*\n"; |
| 115 |
} |
| 116 |
$this->commonTestExecute( $options, $fileContents ); |
| 117 |
$this->expectOutputRegex( $expectedOutputRegex . '/' ); |
| 118 |
|
| 119 |
$blockManager = $this->getServiceContainer()->getBlockManager(); |
| 120 |
foreach ( $unblockedUsers as $user ) { |
| 121 |
$blockManager->clearUserCache( $user ); |
| 122 |
$actualBlock = $blockManager->getBlock( $user, null ); |
| 123 |
$this->assertNotNull( $actualBlock, 'User was not blocked' ); |
| 124 |
|
| 125 |
$this->assertSame( $options['reason'] ?? '', $actualBlock->getReasonComment()->text ); |
| 126 |
if ( !isset( $options['expiry'] ) || wfIsInfinity( $options['expiry'] ) ) { |
| 127 |
$this->assertTrue( wfIsInfinity( $actualBlock->getExpiry() ) ); |
| 128 |
} else { |
| 129 |
$this->assertSame( $options['expiry'], $actualBlock->getExpiry() ); |
| 130 |
} |
| 131 |
$this->assertSame( |
| 132 |
$options['performer'] ?? User::MAINTENANCE_SCRIPT_USER, |
| 133 |
$actualBlock->getBlocker()->getName() |
| 134 |
); |
| 135 |
$blockParameters = [ |
| 136 |
'isCreateAccountBlocked' => !isset( $options['allow-createaccount'] ), |
| 137 |
'isEmailBlocked' => !isset( $options['allow-email'] ), |
| 138 |
'isUsertalkEditAllowed' => isset( $options['allow-talkedit'] ), |
| 139 |
'isHardBlock' => !isset( $options['disable-hardblock'] ), |
| 140 |
'isAutoblocking' => !isset( $options['disable-autoblock'] ), |
| 141 |
]; |
| 142 |
foreach ( $blockParameters as $methodName => $expectedReturnValue ) { |
| 143 |
$this->assertSame( |
| 144 |
$expectedReturnValue, |
| 145 |
$actualBlock->$methodName(), |
| 146 |
"::$methodName returned an unexpected value" |
| 147 |
); |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
public static function provideExecuteForBlock() { |
| 153 |
return [ |
| 154 |
'2 blocked users and 1 unblocked user, with specified reason' => [ [ 'reason' => 'testing' ], 2, 1 ], |
| 155 |
'3 unblocked users with most options set' => [ |
| 156 |
[ |
| 157 |
'allow-createaccount' => 1, 'allow-email' => 1, 'allow-talkedit' => 1, 'disable-hardblock' => 1, |
| 158 |
'disable-autoblock' => 1, |
| 159 |
], |
| 160 |
3, 0, |
| 161 |
], |
| 162 |
]; |
| 163 |
} |
| 164 |
|
| 165 |
public function testExecuteForBlockWithSpecifiedPerformer() { |
| 166 |
$this->testExecuteForBlock( [ 'performer' => $this->getTestSysop()->getUserIdentity()->getName() ], 1, 0 ); |
| 167 |
} |
| 168 |
|
| 169 |
public function testExecuteWhenPerformerNameInvalid() { |
| 170 |
|
| 171 |
$this->overrideConfigValue( MainConfigNames::MaxNameChars, 3 ); |
| 172 |
$this->expectCallToFatalError(); |
| 173 |
$this->expectOutputRegex( '/Unable to parse.*username/' ); |
| 174 |
$this->commonTestExecute( [ 'performer' => 'Username-which-is-too-long' ], '' ); |
| 175 |
} |
| 176 |
} |