| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Block\BlockUserFactory; |
| 4 |
use MediaWiki\Block\DatabaseBlock; |
| 5 |
use MediaWiki\Block\Restriction\PageRestriction; |
| 6 |
use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait; |
| 7 |
use MediaWiki\User\User; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
class BlockUserTest extends MediaWikiIntegrationTestCase { |
| 14 |
use MockAuthorityTrait; |
| 15 |
|
| 16 |
private User $user; |
| 17 |
private BlockUserFactory $blockUserFactory; |
| 18 |
|
| 19 |
protected function setUp(): void { |
| 20 |
parent::setUp(); |
| 21 |
|
| 22 |
|
| 23 |
$this->user = $this->getTestUser()->getUser(); |
| 24 |
|
| 25 |
|
| 26 |
$this->blockUserFactory = $this->getServiceContainer()->getBlockUserFactory(); |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
public function testValidTarget() { |
| 33 |
$status = $this->blockUserFactory->newBlockUser( |
| 34 |
$this->user, |
| 35 |
$this->mockRegisteredUltimateAuthority(), |
| 36 |
'infinity', |
| 37 |
'test block' |
| 38 |
)->placeBlock(); |
| 39 |
$this->assertStatusGood( $status ); |
| 40 |
$block = $this->user->getBlock(); |
| 41 |
$this->assertSame( 'test block', $block->getReasonComment()->text ); |
| 42 |
$this->assertInstanceOf( DatabaseBlock::class, $block ); |
| 43 |
$this->assertFalse( $block->getHideName() ); |
| 44 |
$this->assertFalse( $block->isCreateAccountBlocked() ); |
| 45 |
$this->assertTrue( $block->isUsertalkEditAllowed() ); |
| 46 |
$this->assertFalse( $block->isEmailBlocked() ); |
| 47 |
$this->assertTrue( $block->isAutoblocking() ); |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public function testHideUser() { |
| 54 |
$status = $this->blockUserFactory->newBlockUser( |
| 55 |
$this->user, |
| 56 |
$this->getTestUser( [ 'sysop', 'suppress' ] )->getUser(), |
| 57 |
'infinity', |
| 58 |
'test hideuser', |
| 59 |
[ |
| 60 |
'isHideUser' => true |
| 61 |
] |
| 62 |
)->placeBlock(); |
| 63 |
$this->assertStatusGood( $status ); |
| 64 |
$block = $this->user->getBlock(); |
| 65 |
$this->assertInstanceOf( DatabaseBlock::class, $block ); |
| 66 |
$this->assertSame( 'test hideuser', $block->getReasonComment()->text ); |
| 67 |
$this->assertTrue( $block->getHideName() ); |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public function testExistingPage() { |
| 74 |
$this->getExistingTestPage( 'Existing Page' ); |
| 75 |
$pageRestriction = PageRestriction::class; |
| 76 |
$page = $pageRestriction::newFromTitle( 'Existing Page' ); |
| 77 |
$status = $this->blockUserFactory->newBlockUser( |
| 78 |
$this->user, |
| 79 |
$this->getTestUser( [ 'sysop', 'suppress' ] )->getUser(), |
| 80 |
'infinity', |
| 81 |
'test existingpage', |
| 82 |
[], |
| 83 |
[ $page ] |
| 84 |
)->placeBlock(); |
| 85 |
$this->assertStatusGood( $status ); |
| 86 |
$block = $this->user->getBlock(); |
| 87 |
$this->assertInstanceOf( DatabaseBlock::class, $block ); |
| 88 |
$this->assertSame( 'test existingpage', $block->getReasonComment()->text ); |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
public function testNonexistentPage() { |
| 95 |
$pageRestriction = PageRestriction::class; |
| 96 |
$page = $pageRestriction::newFromTitle( 'nonexistent' ); |
| 97 |
$status = $this->blockUserFactory->newBlockUser( |
| 98 |
$this->user, |
| 99 |
$this->getTestUser( [ 'sysop', 'suppress' ] )->getUser(), |
| 100 |
'infinity', |
| 101 |
'test nonexistentpage', |
| 102 |
[], |
| 103 |
[ $page ] |
| 104 |
)->placeBlock(); |
| 105 |
$this->assertStatusError( 'cant-block-nonexistent-page', $status ); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
public function testReblock() { |
| 112 |
$blockStatus = $this->blockUserFactory->newBlockUser( |
| 113 |
$this->user, |
| 114 |
$this->mockRegisteredUltimateAuthority(), |
| 115 |
'infinity', |
| 116 |
'test block' |
| 117 |
)->placeBlockUnsafe(); |
| 118 |
$this->assertStatusGood( $blockStatus ); |
| 119 |
$priorBlock = $this->user->getBlock(); |
| 120 |
$this->assertInstanceOf( DatabaseBlock::class, $priorBlock ); |
| 121 |
$this->assertSame( 'test block', $priorBlock->getReasonComment()->text ); |
| 122 |
|
| 123 |
$blockId = $priorBlock->getId(); |
| 124 |
|
| 125 |
$reblockStatus = $this->blockUserFactory->newBlockUser( |
| 126 |
$this->user, |
| 127 |
$this->mockAnonUltimateAuthority(), |
| 128 |
'infinity', |
| 129 |
'test reblock' |
| 130 |
)->placeBlockUnsafe( false ); |
| 131 |
$this->assertStatusError( 'ipb_already_blocked', $reblockStatus ); |
| 132 |
|
| 133 |
$this->user->clearInstanceCache(); |
| 134 |
$block = $this->user->getBlock(); |
| 135 |
$this->assertInstanceOf( DatabaseBlock::class, $block ); |
| 136 |
$this->assertSame( $blockId, $block->getId() ); |
| 137 |
|
| 138 |
$reblockStatus = $this->blockUserFactory->newBlockUser( |
| 139 |
$this->user, |
| 140 |
$this->mockRegisteredUltimateAuthority(), |
| 141 |
'infinity', |
| 142 |
'test reblock' |
| 143 |
)->placeBlockUnsafe( true ); |
| 144 |
$this->assertStatusGood( $reblockStatus ); |
| 145 |
|
| 146 |
$this->user->clearInstanceCache(); |
| 147 |
$block = $this->user->getBlock(); |
| 148 |
$this->assertInstanceOf( DatabaseBlock::class, $block ); |
| 149 |
$this->assertSame( 'test reblock', $block->getReasonComment()->text ); |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
public function testPostHook() { |
| 156 |
$hookBlock = false; |
| 157 |
$hookPriorBlock = false; |
| 158 |
$this->setTemporaryHook( |
| 159 |
'BlockIpComplete', |
| 160 |
static function ( $block, $legacyUser, $priorBlock ) |
| 161 |
use ( &$hookBlock, &$hookPriorBlock ) |
| 162 |
{ |
| 163 |
$hookBlock = $block; |
| 164 |
$hookPriorBlock = $priorBlock; |
| 165 |
} |
| 166 |
); |
| 167 |
|
| 168 |
$blockStatus = $this->blockUserFactory->newBlockUser( |
| 169 |
$this->user, |
| 170 |
$this->mockRegisteredUltimateAuthority(), |
| 171 |
'infinity', |
| 172 |
'test block' |
| 173 |
)->placeBlockUnsafe(); |
| 174 |
$this->assertStatusGood( $blockStatus ); |
| 175 |
$priorBlock = $this->user->getBlock(); |
| 176 |
$this->assertInstanceOf( DatabaseBlock::class, $priorBlock ); |
| 177 |
$this->assertSame( $priorBlock->getId(), $hookBlock->getId() ); |
| 178 |
$this->assertNull( $hookPriorBlock ); |
| 179 |
|
| 180 |
$hookBlock = false; |
| 181 |
$hookPriorBlock = false; |
| 182 |
$reblockStatus = $this->blockUserFactory->newBlockUser( |
| 183 |
$this->user, |
| 184 |
$this->mockRegisteredUltimateAuthority(), |
| 185 |
'infinity', |
| 186 |
'test reblock' |
| 187 |
)->placeBlockUnsafe( true ); |
| 188 |
$this->assertStatusGood( $reblockStatus ); |
| 189 |
|
| 190 |
$this->user->clearInstanceCache(); |
| 191 |
$newBlock = $this->user->getBlock(); |
| 192 |
$this->assertInstanceOf( DatabaseBlock::class, $newBlock ); |
| 193 |
$this->assertSame( $newBlock->getId(), $hookBlock->getId() ); |
| 194 |
$this->assertSame( $priorBlock->getId(), $hookPriorBlock->getId() ); |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
public function testIPBlockAllowedAutoblockPreserved() { |
| 201 |
$blockStatus = $this->blockUserFactory->newBlockUser( |
| 202 |
$this->user, |
| 203 |
$this->mockRegisteredUltimateAuthority(), |
| 204 |
'infinity', |
| 205 |
'test block with autoblocking', |
| 206 |
[ 'isAutoblocking' => true ] |
| 207 |
)->placeBlockUnsafe(); |
| 208 |
$this->assertStatusGood( $blockStatus ); |
| 209 |
$block = $blockStatus->getValue(); |
| 210 |
|
| 211 |
$target = '1.2.3.4'; |
| 212 |
$autoBlockId = $block->doAutoblock( $target ); |
| 213 |
$this->assertNotFalse( $autoBlockId ); |
| 214 |
|
| 215 |
$hookPriorBlock = false; |
| 216 |
$this->setTemporaryHook( |
| 217 |
'BlockIpComplete', |
| 218 |
static function ( $block, $legacyUser, $priorBlock ) |
| 219 |
use ( &$hookPriorBlock ) |
| 220 |
{ |
| 221 |
$hookPriorBlock = $priorBlock; |
| 222 |
} |
| 223 |
); |
| 224 |
|
| 225 |
$IPBlockStatus = $this->blockUserFactory->newBlockUser( |
| 226 |
$target, |
| 227 |
$this->mockRegisteredUltimateAuthority(), |
| 228 |
'infinity', |
| 229 |
'test IP block' |
| 230 |
)->placeBlockUnsafe(); |
| 231 |
$this->assertStatusGood( $IPBlockStatus ); |
| 232 |
$IPBlock = $IPBlockStatus->getValue(); |
| 233 |
$this->assertInstanceOf( DatabaseBlock::class, $IPBlock ); |
| 234 |
$this->assertNull( $hookPriorBlock ); |
| 235 |
|
| 236 |
$blockIds = array_map( |
| 237 |
static function ( DatabaseBlock $block ) { |
| 238 |
return $block->getId(); |
| 239 |
}, |
| 240 |
$this->getServiceContainer()->getDatabaseBlockStore() |
| 241 |
->newListFromTarget( $target, null, true ) |
| 242 |
); |
| 243 |
$this->assertContains( $autoBlockId, $blockIds ); |
| 244 |
$this->assertContains( $IPBlock->getId(), $blockIds ); |
| 245 |
} |
| 246 |
|
| 247 |
} |