| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Block\BlockActionInfo; |
| 4 |
use MediaWiki\Block\BlockRestrictionStore; |
| 5 |
use MediaWiki\Block\BlockUtils; |
| 6 |
use MediaWiki\Block\DatabaseBlock; |
| 7 |
use MediaWiki\Block\HideUserUtils; |
| 8 |
use MediaWiki\Block\Restriction\NamespaceRestriction; |
| 9 |
use MediaWiki\Block\Restriction\PageRestriction; |
| 10 |
use MediaWiki\Cache\LinkBatchFactory; |
| 11 |
use MediaWiki\CommentFormatter\RowCommentFormatter; |
| 12 |
use MediaWiki\CommentStore\CommentStore; |
| 13 |
use MediaWiki\Context\RequestContext; |
| 14 |
use MediaWiki\Linker\LinkRenderer; |
| 15 |
use MediaWiki\MainConfigNames; |
| 16 |
use MediaWiki\Pager\BlockListPager; |
| 17 |
use MediaWiki\Permissions\SimpleAuthority; |
| 18 |
use MediaWiki\Permissions\UltimateAuthority; |
| 19 |
use MediaWiki\Request\FauxRequest; |
| 20 |
use MediaWiki\SpecialPage\SpecialPageFactory; |
| 21 |
use MediaWiki\Utils\MWTimestamp; |
| 22 |
use Wikimedia\Rdbms\FakeResultWrapper; |
| 23 |
use Wikimedia\Rdbms\IConnectionProvider; |
| 24 |
use Wikimedia\TestingAccessWrapper; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
class BlockListPagerTest extends MediaWikiIntegrationTestCase { |
| 31 |
|
| 32 |
|
| 33 |
private $blockActionInfo; |
| 34 |
|
| 35 |
|
| 36 |
private $blockRestrictionStore; |
| 37 |
|
| 38 |
|
| 39 |
private $blockUtils; |
| 40 |
|
| 41 |
|
| 42 |
private $hideUserUtils; |
| 43 |
|
| 44 |
|
| 45 |
private $commentStore; |
| 46 |
|
| 47 |
|
| 48 |
private $linkRenderer; |
| 49 |
|
| 50 |
|
| 51 |
private $linkBatchFactory; |
| 52 |
|
| 53 |
|
| 54 |
private $dbProvider; |
| 55 |
|
| 56 |
|
| 57 |
private $rowCommentFormatter; |
| 58 |
|
| 59 |
|
| 60 |
private $specialPageFactory; |
| 61 |
|
| 62 |
protected function setUp(): void { |
| 63 |
parent::setUp(); |
| 64 |
|
| 65 |
$services = $this->getServiceContainer(); |
| 66 |
$this->blockActionInfo = $services->getBlockActionInfo(); |
| 67 |
$this->blockRestrictionStore = $services->getBlockRestrictionStore(); |
| 68 |
$this->blockUtils = $services->getBlockUtils(); |
| 69 |
$this->hideUserUtils = $services->getHideUserUtils(); |
| 70 |
$this->commentStore = $services->getCommentStore(); |
| 71 |
$this->linkBatchFactory = $services->getLinkBatchFactory(); |
| 72 |
$this->linkRenderer = $services->getLinkRenderer(); |
| 73 |
$this->dbProvider = $services->getConnectionProvider(); |
| 74 |
$this->rowCommentFormatter = $services->getRowCommentFormatter(); |
| 75 |
$this->specialPageFactory = $services->getSpecialPageFactory(); |
| 76 |
} |
| 77 |
|
| 78 |
private function getBlockListPager() { |
| 79 |
return new BlockListPager( |
| 80 |
RequestContext::getMain(), |
| 81 |
$this->blockActionInfo, |
| 82 |
$this->blockRestrictionStore, |
| 83 |
$this->blockUtils, |
| 84 |
$this->hideUserUtils, |
| 85 |
$this->commentStore, |
| 86 |
$this->linkBatchFactory, |
| 87 |
$this->linkRenderer, |
| 88 |
$this->dbProvider, |
| 89 |
$this->rowCommentFormatter, |
| 90 |
$this->specialPageFactory, |
| 91 |
[] |
| 92 |
); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
public function testFormatValue( $name, $expected, $row ) { |
| 101 |
|
| 102 |
MWTimestamp::setFakeTime( '20230405060708' ); |
| 103 |
|
| 104 |
$value = $row->$name ?? null; |
| 105 |
|
| 106 |
if ( $name === 'bl_timestamp' ) { |
| 107 |
|
| 108 |
|
| 109 |
$linkRenderer = $this->getServiceContainer()->getLinkRenderer(); |
| 110 |
$link = $linkRenderer->makeKnownLink( |
| 111 |
$this->specialPageFactory->getTitleForAlias( 'BlockList' ), |
| 112 |
MWTimestamp::getInstance( $value )->format( 'H:i, j F Y' ), |
| 113 |
[], |
| 114 |
[ 'wpTarget' => "#{$row->bl_id}" ], |
| 115 |
); |
| 116 |
$expected = $link; |
| 117 |
} |
| 118 |
|
| 119 |
$pager = $this->getBlockListPager(); |
| 120 |
$wrappedPager = TestingAccessWrapper::newFromObject( $pager ); |
| 121 |
$wrappedPager->mCurrentRow = $row; |
| 122 |
|
| 123 |
$formatted = $pager->formatValue( $name, $value ); |
| 124 |
$this->assertStringMatchesFormat( $expected, $formatted ); |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
public static function formatValueEmptyProvider() { |
| 131 |
$row = (object)[ |
| 132 |
'bl_id' => 1, |
| 133 |
]; |
| 134 |
|
| 135 |
return [ |
| 136 |
[ 'test', 'Unable to format test', $row ], |
| 137 |
[ 'bl_timestamp', null, $row ], |
| 138 |
[ 'bl_expiry', 'infinite<br />0 seconds left', $row ], |
| 139 |
]; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
public static function formatValueDefaultProvider() { |
| 146 |
$row = (object)[ |
| 147 |
'bt_user' => 0, |
| 148 |
'bt_user_text' => null, |
| 149 |
'bt_address' => '127.0.0.1', |
| 150 |
'bl_id' => 1, |
| 151 |
'bl_by_text' => 'Admin', |
| 152 |
'bt_auto' => 0, |
| 153 |
'bl_anon_only' => 0, |
| 154 |
'bl_create_account' => 1, |
| 155 |
'bl_enable_autoblock' => 1, |
| 156 |
'bl_deleted' => 0, |
| 157 |
'bl_block_email' => 0, |
| 158 |
'bl_allow_usertalk' => 0, |
| 159 |
'bl_sitewide' => 1, |
| 160 |
]; |
| 161 |
|
| 162 |
return [ |
| 163 |
[ |
| 164 |
'test', |
| 165 |
'Unable to format test', |
| 166 |
$row, |
| 167 |
], |
| 168 |
[ |
| 169 |
'bl_timestamp', |
| 170 |
'20230405060708', |
| 171 |
$row, |
| 172 |
], |
| 173 |
[ |
| 174 |
'bl_expiry', |
| 175 |
'infinite<br />0 seconds left', |
| 176 |
$row, |
| 177 |
], |
| 178 |
[ |
| 179 |
'by', |
| 180 |
'<a %s><bdi>Admin</bdi></a>%s', |
| 181 |
$row, |
| 182 |
], |
| 183 |
[ |
| 184 |
'params', |
| 185 |
'<ul><li>editing (sitewide)</li>' . |
| 186 |
'<li>account creation disabled</li><li>cannot edit own talk page</li></ul>', |
| 187 |
$row, |
| 188 |
] |
| 189 |
]; |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
public function testFormatValueRestrictions() { |
| 197 |
$this->overrideConfigValues( [ |
| 198 |
MainConfigNames::Script => '/w/index.php', |
| 199 |
] ); |
| 200 |
|
| 201 |
$pager = $this->getBlockListPager(); |
| 202 |
|
| 203 |
$row = (object)[ |
| 204 |
'bl_id' => 0, |
| 205 |
'bt_user' => 0, |
| 206 |
'bl_anon_only' => 0, |
| 207 |
'bl_enable_autoblock' => 0, |
| 208 |
'bl_create_account' => 0, |
| 209 |
'bl_block_email' => 0, |
| 210 |
'bl_allow_usertalk' => 1, |
| 211 |
'bl_sitewide' => 0, |
| 212 |
'bl_deleted' => 0, |
| 213 |
]; |
| 214 |
$wrappedPager = TestingAccessWrapper::newFromObject( $pager ); |
| 215 |
$wrappedPager->mCurrentRow = $row; |
| 216 |
|
| 217 |
$pageName = 'Victor Frankenstein'; |
| 218 |
$page = $this->insertPage( $pageName ); |
| 219 |
$title = $page['title']; |
| 220 |
$pageId = $page['id']; |
| 221 |
|
| 222 |
$restrictions = [ |
| 223 |
( new PageRestriction( 0, $pageId ) )->setTitle( $title ), |
| 224 |
new NamespaceRestriction( 0, NS_MAIN ), |
| 225 |
|
| 226 |
new PageRestriction( 0, 999999 ), |
| 227 |
]; |
| 228 |
|
| 229 |
$wrappedPager = TestingAccessWrapper::newFromObject( $pager ); |
| 230 |
$wrappedPager->restrictions = $restrictions; |
| 231 |
|
| 232 |
$formatted = $pager->formatValue( 'params', '' ); |
| 233 |
$this->assertEquals( '<ul><li>' |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
. wfMessage( 'blocklist-editing' )->text() |
| 238 |
. '<ul><li>' |
| 239 |
. wfMessage( 'blocklist-editing-page' )->text() |
| 240 |
. '<ul><li>' |
| 241 |
. '<a href="/wiki/Victor_Frankenstein" title="' |
| 242 |
. $pageName |
| 243 |
. '">' |
| 244 |
. $pageName |
| 245 |
. '</a></li></ul></li><li>' |
| 246 |
. wfMessage( 'blocklist-editing-ns' )->text() |
| 247 |
. '<ul><li>' |
| 248 |
. '<a href="/w/index.php?title=Special:AllPages&namespace=0" title="' |
| 249 |
. 'Special:AllPages' |
| 250 |
. '">' |
| 251 |
. wfMessage( 'blanknamespace' )->text() |
| 252 |
. '</a></li></ul></li></ul></li></ul>', |
| 253 |
$formatted |
| 254 |
); |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
public function testPreprocessResults() { |
| 261 |
|
| 262 |
$linkCache = $this->getServiceContainer()->getLinkCache(); |
| 263 |
$wrappedlinkCache = TestingAccessWrapper::newFromObject( $linkCache ); |
| 264 |
$admin = $this->getTestSysop()->getUser(); |
| 265 |
|
| 266 |
$links = [ |
| 267 |
'User:127.0.0.1', |
| 268 |
'User_talk:127.0.0.1', |
| 269 |
$admin->getUserPage()->getPrefixedDBkey(), |
| 270 |
$admin->getTalkPage()->getPrefixedDBkey(), |
| 271 |
'Comment_link' |
| 272 |
]; |
| 273 |
|
| 274 |
foreach ( $links as $link ) { |
| 275 |
$this->assertNull( $wrappedlinkCache->entries->get( $link ) ); |
| 276 |
} |
| 277 |
|
| 278 |
$row = (object)[ |
| 279 |
'bt_address' => '127.0.0.1', |
| 280 |
'bt_user' => null, |
| 281 |
'bt_user_text' => null, |
| 282 |
'bl_by' => $admin->getId(), |
| 283 |
'bl_by_text' => $admin->getName(), |
| 284 |
'bl_sitewide' => 1, |
| 285 |
'bl_timestamp' => $this->getDb()->timestamp( wfTimestamp( TS_MW ) ), |
| 286 |
'bl_reason_text' => '[[Comment link]]', |
| 287 |
'bl_reason_data' => null, |
| 288 |
]; |
| 289 |
$pager = $this->getBlockListPager(); |
| 290 |
$pager->preprocessResults( new FakeResultWrapper( [ $row ] ) ); |
| 291 |
|
| 292 |
foreach ( $links as $link ) { |
| 293 |
$this->assertTrue( $wrappedlinkCache->isBadLink( $link ), "Bad link [[$link]]" ); |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
$row = (object)[ |
| 298 |
'bt_address' => '127.0.0.1', |
| 299 |
'bt_user' => null, |
| 300 |
'bt_user_text' => null, |
| 301 |
'bl_by' => $admin->getId(), |
| 302 |
'bl_by_text' => $admin->getName(), |
| 303 |
'bl_sitewide' => 1, |
| 304 |
'bl_reason_text' => '', |
| 305 |
'bl_reason_data' => null, |
| 306 |
]; |
| 307 |
$pager = $this->getBlockListPager(); |
| 308 |
$pager->preprocessResults( new FakeResultWrapper( [ $row ] ) ); |
| 309 |
|
| 310 |
$this->assertObjectNotHasProperty( 'bl_restrictions', $row ); |
| 311 |
|
| 312 |
$page = $this->getExistingTestPage( 'Victor Frankenstein' ); |
| 313 |
$title = $page->getTitle(); |
| 314 |
|
| 315 |
$target = '127.0.0.1'; |
| 316 |
|
| 317 |
|
| 318 |
$block = new DatabaseBlock( [ |
| 319 |
'address' => $target, |
| 320 |
'by' => $this->getTestSysop()->getUser(), |
| 321 |
'reason' => 'Parce que', |
| 322 |
'expiry' => $this->getDb()->getInfinity(), |
| 323 |
'sitewide' => false, |
| 324 |
] ); |
| 325 |
$block->setRestrictions( [ |
| 326 |
new PageRestriction( 0, $page->getId() ), |
| 327 |
] ); |
| 328 |
$blockStore = $this->getServiceContainer()->getDatabaseBlockStore(); |
| 329 |
$blockStore->insertBlock( $block ); |
| 330 |
|
| 331 |
$pager = $this->getBlockListPager(); |
| 332 |
$result = $this->getDb()->newSelectQueryBuilder() |
| 333 |
->queryInfo( $pager->getQueryInfo() ) |
| 334 |
->where( [ 'bl_id' => $block->getId() ] ) |
| 335 |
->caller( __METHOD__ ) |
| 336 |
->fetchResultSet(); |
| 337 |
|
| 338 |
$pager->preprocessResults( $result ); |
| 339 |
|
| 340 |
$wrappedPager = TestingAccessWrapper::newFromObject( $pager ); |
| 341 |
|
| 342 |
$restrictions = $wrappedPager->restrictions; |
| 343 |
$this->assertIsArray( $restrictions ); |
| 344 |
|
| 345 |
$restriction = $restrictions[0]; |
| 346 |
$this->assertEquals( $page->getId(), $restriction->getValue() ); |
| 347 |
$this->assertEquals( $page->getId(), $restriction->getTitle()->getArticleID() ); |
| 348 |
$this->assertEquals( $title->getDBkey(), $restriction->getTitle()->getDBkey() ); |
| 349 |
$this->assertEquals( $title->getNamespace(), $restriction->getTitle()->getNamespace() ); |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
public function testOffset() { |
| 357 |
if ( $this->getDb()->getType() === 'postgres' ) { |
| 358 |
$this->markTestSkipped( "PostgreSQL fatals when the first part of " . |
| 359 |
"the offset parameter has the wrong timestamp format" ); |
| 360 |
} |
| 361 |
$request = new FauxRequest( [ |
| 362 |
'offset' => '20231115010645|7' |
| 363 |
] ); |
| 364 |
RequestContext::getMain()->setRequest( $request ); |
| 365 |
$pager = $this->getBlockListPager(); |
| 366 |
$pager->getFullOutput(); |
| 367 |
$this->assertTrue( true ); |
| 368 |
} |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
public function testAutoblockLeak() { |
| 375 |
$sysop = $this->getTestSysop()->getUserIdentity(); |
| 376 |
$this->overrideConfigValue( MainConfigNames::UseCodexSpecialBlock, true ); |
| 377 |
|
| 378 |
RequestContext::getMain()->setAuthority( new UltimateAuthority( $sysop ) ); |
| 379 |
|
| 380 |
RequestContext::getMain()->setLanguage( 'qqx' ); |
| 381 |
|
| 382 |
$addr = '127.0.0.1'; |
| 383 |
$block = new DatabaseBlock( [ |
| 384 |
'address' => $addr, |
| 385 |
'auto' => true, |
| 386 |
] ); |
| 387 |
$block->setBlocker( $sysop ); |
| 388 |
$blockStore = $this->getServiceContainer()->getDatabaseBlockStore(); |
| 389 |
$status = $blockStore->insertBlock( $block ); |
| 390 |
$this->assertNotFalse( $status ); |
| 391 |
|
| 392 |
$pager = $this->getBlockListPager(); |
| 393 |
$body = $pager->getBody(); |
| 394 |
|
| 395 |
$this->assertStringContainsString( '(unblocklink)', $body ); |
| 396 |
|
| 397 |
$this->assertStringNotContainsString( $addr, $body ); |
| 398 |
} |
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
private function assertStringNotContainsStringIgnoringPunctuation( $expected, $actual ) { |
| 405 |
$this->assertStringNotContainsString( $expected, $actual ); |
| 406 |
|
| 407 |
$regex = '/' . preg_replace( '/[^A-Za-z0-9]+/', '.+', $expected ) . '/'; |
| 408 |
$this->assertDoesNotMatchRegularExpression( $regex, $actual ); |
| 409 |
} |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
public function testBlockLinkSuppression() { |
| 416 |
$user = $this->getTestUser()->getUserIdentity(); |
| 417 |
$store = $this->getServiceContainer()->getDatabaseBlockStore(); |
| 418 |
$block = new DatabaseBlock(); |
| 419 |
$block->setTarget( $user ); |
| 420 |
$block->setBlocker( $this->getTestSysop()->getUser() ); |
| 421 |
$status = $store->insertBlock( $block ); |
| 422 |
$this->assertNotFalse( $status ); |
| 423 |
$block = new DatabaseBlock( [ |
| 424 |
'hideName' => true |
| 425 |
] ); |
| 426 |
$block->setTarget( $user ); |
| 427 |
$block->setBlocker( $this->getTestSysop()->getUser() ); |
| 428 |
$status = $store->insertBlock( $block, null ); |
| 429 |
$this->assertNotFalse( $status ); |
| 430 |
|
| 431 |
RequestContext::getMain()->setAuthority( |
| 432 |
new SimpleAuthority( |
| 433 |
$this->getTestSysop()->getUserIdentity(), |
| 434 |
[ 'block' ] |
| 435 |
) |
| 436 |
); |
| 437 |
|
| 438 |
$pager = $this->getBlockListPager(); |
| 439 |
$body = $pager->getBody(); |
| 440 |
$this->assertStringNotContainsStringIgnoringPunctuation( $user->getName(), $body ); |
| 441 |
} |
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
public function testAutoblockSuppression() { |
| 448 |
$user = $this->getTestUser()->getUserIdentity(); |
| 449 |
$store = $this->getServiceContainer()->getDatabaseBlockStore(); |
| 450 |
$block = new DatabaseBlock( [ |
| 451 |
'hideName' => true, |
| 452 |
'enableAutoblock' => true, |
| 453 |
] ); |
| 454 |
$block->setTarget( $user ); |
| 455 |
$block->setBlocker( $this->getTestSysop()->getUser() ); |
| 456 |
$status = $store->insertBlock( $block ); |
| 457 |
$this->assertNotFalse( $status ); |
| 458 |
$store->doAutoblock( $block, '127.0.0.42' ); |
| 459 |
|
| 460 |
$pager = $this->getBlockListPager(); |
| 461 |
$body = $pager->getBody(); |
| 462 |
$this->assertStringNotContainsStringIgnoringPunctuation( $user->getName(), $body ); |
| 463 |
} |
| 464 |
} |