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\Request\FauxRequest
;
18 use MediaWiki\SpecialPage\SpecialPageFactory
;
19 use MediaWiki\Utils\MWTimestamp
;
20 use Wikimedia\Rdbms\FakeResultWrapper
;
21 use Wikimedia\Rdbms\IConnectionProvider
;
22 use Wikimedia\TestingAccessWrapper
;
26 * @coversDefaultClass \MediaWiki\Pager\BlockListPager
28 class BlockListPagerTest
extends MediaWikiIntegrationTestCase
{
30 /** @var BlockActionInfo */
31 private $blockActionInfo;
33 /** @var BlockRestrictionStore */
34 private $blockRestrictionStore;
36 /** @var BlockUtils */
39 /** @var HideUserUtils */
40 private $hideUserUtils;
42 /** @var CommentStore */
43 private $commentStore;
45 /** @var LinkRenderer */
46 private $linkRenderer;
48 /** @var LinkBatchFactory */
49 private $linkBatchFactory;
51 /** @var IConnectionProvider */
54 /** @var RowCommentFormatter */
55 private $rowCommentFormatter;
57 /** @var SpecialPageFactory */
58 private $specialPageFactory;
60 protected function setUp(): void
{
63 $services = $this->getServiceContainer();
64 $this->blockActionInfo
= $services->getBlockActionInfo();
65 $this->blockRestrictionStore
= $services->getBlockRestrictionStore();
66 $this->blockUtils
= $services->getBlockUtils();
67 $this->hideUserUtils
= $services->getHideUserUtils();
68 $this->commentStore
= $services->getCommentStore();
69 $this->linkBatchFactory
= $services->getLinkBatchFactory();
70 $this->linkRenderer
= $services->getLinkRenderer();
71 $this->dbProvider
= $services->getConnectionProvider();
72 $this->rowCommentFormatter
= $services->getRowCommentFormatter();
73 $this->specialPageFactory
= $services->getSpecialPageFactory();
76 private function getBlockListPager() {
77 return new BlockListPager(
78 RequestContext
::getMain(),
79 $this->blockActionInfo
,
80 $this->blockRestrictionStore
,
84 $this->linkBatchFactory
,
87 $this->rowCommentFormatter
,
88 $this->specialPageFactory
,
94 * @covers ::formatValue
95 * @dataProvider formatValueEmptyProvider
96 * @dataProvider formatValueDefaultProvider
98 public function testFormatValue( $name, $expected, $row ) {
99 // Set the time to now so it does not get off during the test.
100 MWTimestamp
::setFakeTime( '20230405060708' );
102 $value = $row->$name ??
null;
104 if ( $name === 'bl_timestamp' ) {
105 // Wrap the expected timestamp in a string with the timestamp in the format
106 // used by the BlockListPager.
107 $linkRenderer = $this->getServiceContainer()->getLinkRenderer();
108 $link = $linkRenderer->makeKnownLink(
109 $this->specialPageFactory
->getTitleForAlias( 'BlockList' ),
110 MWTimestamp
::getInstance( $value )->format( 'H:i, j F Y' ),
112 [ 'wpTarget' => "#{$row->bl_id}" ],
117 $pager = $this->getBlockListPager();
118 $wrappedPager = TestingAccessWrapper
::newFromObject( $pager );
119 $wrappedPager->mCurrentRow
= $row;
121 $formatted = $pager->formatValue( $name, $value );
122 $this->assertStringMatchesFormat( $expected, $formatted );
128 public static function formatValueEmptyProvider() {
134 [ 'test', 'Unable to format test', $row ],
135 [ 'bl_timestamp', null, $row ],
136 [ 'bl_expiry', 'infinite<br />0 seconds left', $row ],
141 * Test the default row values.
143 public static function formatValueDefaultProvider() {
146 'bt_user_text' => null,
147 'bt_address' => '127.0.0.1',
149 'bl_by_text' => 'Admin',
152 'bl_create_account' => 1,
153 'bl_enable_autoblock' => 1,
155 'bl_block_email' => 0,
156 'bl_allow_usertalk' => 0,
163 'Unable to format test',
173 'infinite<br />0 seconds left',
178 '<a %s><bdi>Admin</bdi></a>%s',
183 '<ul><li>editing (sitewide)</li>' .
184 '<li>account creation disabled</li><li>cannot edit own talk page</li></ul>',
191 * @covers ::formatValue
192 * @covers ::getRestrictionListHTML
194 public function testFormatValueRestrictions() {
195 $this->overrideConfigValues( [
196 MainConfigNames
::Script
=> '/w/index.php',
199 $pager = $this->getBlockListPager();
205 'bl_enable_autoblock' => 0,
206 'bl_create_account' => 0,
207 'bl_block_email' => 0,
208 'bl_allow_usertalk' => 1,
212 $wrappedPager = TestingAccessWrapper
::newFromObject( $pager );
213 $wrappedPager->mCurrentRow
= $row;
215 $pageName = 'Victor Frankenstein';
216 $page = $this->insertPage( $pageName );
217 $title = $page['title'];
218 $pageId = $page['id'];
221 ( new PageRestriction( 0, $pageId ) )->setTitle( $title ),
222 new NamespaceRestriction( 0, NS_MAIN
),
224 new PageRestriction( 0, 999999 ),
227 $wrappedPager = TestingAccessWrapper
::newFromObject( $pager );
228 $wrappedPager->restrictions
= $restrictions;
230 $formatted = $pager->formatValue( 'params', '' );
231 $this->assertEquals( '<ul><li>'
232 // FIXME: Expectation value should not be dynamic
233 // and must not depend on a localisation message.
234 // TODO: Mock the message or consider using qqx.
235 . wfMessage( 'blocklist-editing' )->text()
237 . wfMessage( 'blocklist-editing-page' )->text()
239 . '<a href="/wiki/Victor_Frankenstein" title="'
243 . '</a></li></ul></li><li>'
244 . wfMessage( 'blocklist-editing-ns' )->text()
246 . '<a href="/w/index.php?title=Special:AllPages&namespace=0" title="'
249 . wfMessage( 'blanknamespace' )->text()
250 . '</a></li></ul></li></ul></li></ul>',
256 * @covers ::preprocessResults
258 public function testPreprocessResults() {
259 // Test the Link Cache.
260 $linkCache = $this->getServiceContainer()->getLinkCache();
261 $wrappedlinkCache = TestingAccessWrapper
::newFromObject( $linkCache );
262 $admin = $this->getTestSysop()->getUser();
266 'User_talk:127.0.0.1',
267 $admin->getUserPage()->getPrefixedDBkey(),
268 $admin->getTalkPage()->getPrefixedDBkey(),
272 foreach ( $links as $link ) {
273 $this->assertNull( $wrappedlinkCache->entries
->get( $link ) );
277 'bt_address' => '127.0.0.1',
279 'bt_user_text' => null,
280 'bl_by' => $admin->getId(),
281 'bl_by_text' => $admin->getName(),
283 'bl_timestamp' => $this->getDb()->timestamp( wfTimestamp( TS_MW
) ),
284 'bl_reason_text' => '[[Comment link]]',
285 'bl_reason_data' => null,
287 $pager = $this->getBlockListPager();
288 $pager->preprocessResults( new FakeResultWrapper( [ $row ] ) );
290 foreach ( $links as $link ) {
291 $this->assertTrue( $wrappedlinkCache->isBadLink( $link ), "Bad link [[$link]]" );
294 // Test sitewide blocks.
296 'bt_address' => '127.0.0.1',
298 'bt_user_text' => null,
299 'bl_by' => $admin->getId(),
300 'bl_by_text' => $admin->getName(),
302 'bl_reason_text' => '',
303 'bl_reason_data' => null,
305 $pager = $this->getBlockListPager();
306 $pager->preprocessResults( new FakeResultWrapper( [ $row ] ) );
308 $this->assertObjectNotHasProperty( 'bl_restrictions', $row );
310 $page = $this->getExistingTestPage( 'Victor Frankenstein' );
311 $title = $page->getTitle();
313 $target = '127.0.0.1';
315 // Test partial blocks.
316 $block = new DatabaseBlock( [
317 'address' => $target,
318 'by' => $this->getTestSysop()->getUser(),
319 'reason' => 'Parce que',
320 'expiry' => $this->getDb()->getInfinity(),
323 $block->setRestrictions( [
324 new PageRestriction( 0, $page->getId() ),
326 $blockStore = $this->getServiceContainer()->getDatabaseBlockStore();
327 $blockStore->insertBlock( $block );
329 $pager = $this->getBlockListPager();
330 $result = $this->getDb()->newSelectQueryBuilder()
331 ->queryInfo( $pager->getQueryInfo() )
332 ->where( [ 'bl_id' => $block->getId() ] )
333 ->caller( __METHOD__
)
336 $pager->preprocessResults( $result );
338 $wrappedPager = TestingAccessWrapper
::newFromObject( $pager );
340 $restrictions = $wrappedPager->restrictions
;
341 $this->assertIsArray( $restrictions );
343 $restriction = $restrictions[0];
344 $this->assertEquals( $page->getId(), $restriction->getValue() );
345 $this->assertEquals( $page->getId(), $restriction->getTitle()->getArticleID() );
346 $this->assertEquals( $title->getDBkey(), $restriction->getTitle()->getDBkey() );
347 $this->assertEquals( $title->getNamespace(), $restriction->getTitle()->getNamespace() );
351 * T352310 regression test
354 public function testOffset() {
355 if ( $this->getDb()->getType() === 'postgres' ) {
356 $this->markTestSkipped( "PostgreSQL fatals when the first part of " .
357 "the offset parameter has the wrong timestamp format" );
359 $request = new FauxRequest( [
360 'offset' => '20231115010645|7'
362 RequestContext
::getMain()->setRequest( $request );
363 $pager = $this->getBlockListPager();
364 $pager->getFullOutput();
365 $this->assertTrue( true );