Merge "Drop cache interwiki"
[mediawiki.git] / tests / phpunit / includes / specials / pagers / BlockListPagerTest.php
blob79c6675aea239372a75dcd7362248a51231fab72
1 <?php
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;
24 /**
25 * @group Database
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 */
37 private $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 */
52 private $dbProvider;
54 /** @var RowCommentFormatter */
55 private $rowCommentFormatter;
57 /** @var SpecialPageFactory */
58 private $specialPageFactory;
60 protected function setUp(): void {
61 parent::setUp();
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,
81 $this->blockUtils,
82 $this->hideUserUtils,
83 $this->commentStore,
84 $this->linkBatchFactory,
85 $this->linkRenderer,
86 $this->dbProvider,
87 $this->rowCommentFormatter,
88 $this->specialPageFactory,
93 /**
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}" ],
114 $expected = $link;
117 $pager = $this->getBlockListPager();
118 $wrappedPager = TestingAccessWrapper::newFromObject( $pager );
119 $wrappedPager->mCurrentRow = $row;
121 $formatted = $pager->formatValue( $name, $value );
122 $this->assertStringMatchesFormat( $expected, $formatted );
126 * Test empty values.
128 public static function formatValueEmptyProvider() {
129 $row = (object)[
130 'bl_id' => 1,
133 return [
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() {
144 $row = (object)[
145 'bt_user' => 0,
146 'bt_user_text' => null,
147 'bt_address' => '127.0.0.1',
148 'bl_id' => 1,
149 'bl_by_text' => 'Admin',
150 'bt_auto' => 0,
151 'bl_anon_only' => 0,
152 'bl_create_account' => 1,
153 'bl_enable_autoblock' => 1,
154 'bl_deleted' => 0,
155 'bl_block_email' => 0,
156 'bl_allow_usertalk' => 0,
157 'bl_sitewide' => 1,
160 return [
162 'test',
163 'Unable to format test',
164 $row,
167 'bl_timestamp',
168 '20230405060708',
169 $row,
172 'bl_expiry',
173 'infinite<br />0 seconds left',
174 $row,
177 'by',
178 '<a %s><bdi>Admin</bdi></a>%s',
179 $row,
182 'params',
183 '<ul><li>editing (sitewide)</li>' .
184 '<li>account creation disabled</li><li>cannot edit own talk page</li></ul>',
185 $row,
191 * @covers ::formatValue
192 * @covers ::getRestrictionListHTML
194 public function testFormatValueRestrictions() {
195 $this->overrideConfigValues( [
196 MainConfigNames::Script => '/w/index.php',
197 ] );
199 $pager = $this->getBlockListPager();
201 $row = (object)[
202 'bl_id' => 0,
203 'bt_user' => 0,
204 'bl_anon_only' => 0,
205 'bl_enable_autoblock' => 0,
206 'bl_create_account' => 0,
207 'bl_block_email' => 0,
208 'bl_allow_usertalk' => 1,
209 'bl_sitewide' => 0,
210 'bl_deleted' => 0,
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'];
220 $restrictions = [
221 ( new PageRestriction( 0, $pageId ) )->setTitle( $title ),
222 new NamespaceRestriction( 0, NS_MAIN ),
223 // Deleted page.
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()
236 . '<ul><li>'
237 . wfMessage( 'blocklist-editing-page' )->text()
238 . '<ul><li>'
239 . '<a href="/wiki/Victor_Frankenstein" title="'
240 . $pageName
241 . '">'
242 . $pageName
243 . '</a></li></ul></li><li>'
244 . wfMessage( 'blocklist-editing-ns' )->text()
245 . '<ul><li>'
246 . '<a href="/w/index.php?title=Special:AllPages&amp;namespace=0" title="'
247 . 'Special:AllPages'
248 . '">'
249 . wfMessage( 'blanknamespace' )->text()
250 . '</a></li></ul></li></ul></li></ul>',
251 $formatted
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();
264 $links = [
265 'User:127.0.0.1',
266 'User_talk:127.0.0.1',
267 $admin->getUserPage()->getPrefixedDBkey(),
268 $admin->getTalkPage()->getPrefixedDBkey(),
269 'Comment_link'
272 foreach ( $links as $link ) {
273 $this->assertNull( $wrappedlinkCache->entries->get( $link ) );
276 $row = (object)[
277 'bt_address' => '127.0.0.1',
278 'bt_user' => null,
279 'bt_user_text' => null,
280 'bl_by' => $admin->getId(),
281 'bl_by_text' => $admin->getName(),
282 'bl_sitewide' => 1,
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.
295 $row = (object)[
296 'bt_address' => '127.0.0.1',
297 'bt_user' => null,
298 'bt_user_text' => null,
299 'bl_by' => $admin->getId(),
300 'bl_by_text' => $admin->getName(),
301 'bl_sitewide' => 1,
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(),
321 'sitewide' => false,
322 ] );
323 $block->setRestrictions( [
324 new PageRestriction( 0, $page->getId() ),
325 ] );
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__ )
334 ->fetchResultSet();
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
352 * @coversNothing
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'
361 ] );
362 RequestContext::getMain()->setRequest( $request );
363 $pager = $this->getBlockListPager();
364 $pager->getFullOutput();
365 $this->assertTrue( true );