3 use MediaWiki\Block\DatabaseBlock
;
4 use MediaWiki\Block\Restriction\PageRestriction
;
5 use MediaWiki\Block\Restriction\NamespaceRestriction
;
6 use MediaWiki\MediaWikiServices
;
7 use Wikimedia\TestingAccessWrapper
;
11 * @coversDefaultClass BlockListPager
13 class BlockListPagerTest
extends MediaWikiTestCase
{
16 * @covers ::formatValue
17 * @dataProvider formatValueEmptyProvider
18 * @dataProvider formatValueDefaultProvider
20 * @param string $expected
22 public function testFormatValue( $name, $expected = null, $row = null ) {
23 $this->setMwGlobals( [
24 'wgEnablePartialBlocks' => false,
26 // Set the time to now so it does not get off during the test.
27 MWTimestamp
::setFakeTime( MWTimestamp
::time() );
29 $value = $name === 'ipb_timestamp' ? MWTimestamp
::time() : '';
30 $expected = $expected ?? MWTimestamp
::getInstance()->format( 'H:i, j F Y' );
32 $row = $row ?
: new stdClass
;
33 $pager = new BlockListPager( new SpecialPage(), [] );
34 $wrappedPager = TestingAccessWrapper
::newFromObject( $pager );
35 $wrappedPager->mCurrentRow
= $row;
37 $formatted = $pager->formatValue( $name, $value );
38 $this->assertEquals( $expected, $formatted );
41 MWTimestamp
::setFakeTime( false );
47 public function formatValueEmptyProvider() {
51 'Unable to format test',
58 'infinite<br />0 minutes left',
64 * Test the default row values.
66 public function formatValueDefaultProvider() {
69 'ipb_address' => '127.0.0.1',
70 'ipb_by_text' => 'Admin',
71 'ipb_create_account' => 1,
74 'ipb_create_account' => 1,
75 'ipb_enable_autoblock' => 1,
77 'ipb_block_email' => 0,
78 'ipb_allow_usertalk' => 0,
85 'Unable to format test',
95 'infinite<br />0 minutes left',
105 '<ul><li>account creation disabled</li><li>cannot edit own talk page</li></ul>',
112 * @covers ::formatValue
113 * @covers ::getRestrictionListHTML
115 public function testFormatValueRestrictions() {
116 $this->setMwGlobals( [
117 'wgArticlePath' => '/wiki/$1',
118 'wgScript' => '/w/index.php',
121 $pager = new BlockListPager( new SpecialPage(), [] );
126 'ipb_anon_only' => 0,
127 'ipb_enable_autoblock' => 0,
128 'ipb_create_account' => 0,
129 'ipb_block_email' => 0,
130 'ipb_allow_usertalk' => 1,
133 $wrappedPager = TestingAccessWrapper
::newFromObject( $pager );
134 $wrappedPager->mCurrentRow
= $row;
136 $pageName = 'Victor Frankenstein';
137 $page = $this->insertPage( $pageName );
138 $title = $page['title'];
139 $pageId = $page['id'];
142 ( new PageRestriction( 0, $pageId ) )->setTitle( $title ),
143 new NamespaceRestriction( 0, NS_MAIN
),
145 new PageRestriction( 0, 999999 ),
148 $wrappedPager = TestingAccessWrapper
::newFromObject( $pager );
149 $wrappedPager->restrictions
= $restrictions;
151 $formatted = $pager->formatValue( 'ipb_params', '' );
152 $this->assertEquals( '<ul><li>'
153 // FIXME: Expectation value should not be dynamic
154 // and must not depend on a localisation message.
155 // TODO: Mock the message or consider using qqx.
156 . wfMessage( 'blocklist-editing' )->text()
158 . wfMessage( 'blocklist-editing-page' )->text()
160 . '<a href="/wiki/Victor_Frankenstein" title="'
164 . '</a></li></ul></li><li>'
165 . wfMessage( 'blocklist-editing-ns' )->text()
167 . '<a href="/w/index.php?title=Special:AllPages&namespace=0" title="'
170 . wfMessage( 'blanknamespace' )->text()
171 . '</a></li></ul></li></ul></li></ul>',
177 * @covers ::preprocessResults
179 public function testPreprocessResults() {
180 // Test the Link Cache.
181 $linkCache = MediaWikiServices
::getInstance()->getLinkCache();
182 $wrappedlinkCache = TestingAccessWrapper
::newFromObject( $linkCache );
186 'User_talk:127.0.0.1',
191 foreach ( $links as $link ) {
192 $this->assertNull( $wrappedlinkCache->badLinks
->get( $link ) );
196 'ipb_address' => '127.0.0.1',
197 'by_user_name' => 'Admin',
199 'ipb_timestamp' => $this->db
->timestamp( wfTimestamp( TS_MW
) ),
201 $pager = new BlockListPager( new SpecialPage(), [] );
202 $pager->preprocessResults( [ $row ] );
204 foreach ( $links as $link ) {
205 $this->assertSame( 1, $wrappedlinkCache->badLinks
->get( $link ) );
208 // Test Sitewide Blocks.
210 'ipb_address' => '127.0.0.1',
211 'by_user_name' => 'Admin',
214 $pager = new BlockListPager( new SpecialPage(), [] );
215 $pager->preprocessResults( [ $row ] );
217 $this->assertObjectNotHasAttribute( 'ipb_restrictions', $row );
219 $pageName = 'Victor Frankenstein';
220 $page = $this->getExistingTestPage( 'Victor Frankenstein' );
221 $title = $page->getTitle();
223 $target = '127.0.0.1';
225 // Test Partial Blocks Blocks.
226 $block = new DatabaseBlock( [
227 'address' => $target,
228 'by' => $this->getTestSysop()->getUser()->getId(),
229 'reason' => 'Parce que',
230 'expiry' => $this->db
->getInfinity(),
233 $block->setRestrictions( [
234 new PageRestriction( 0, $page->getId() ),
238 $result = $this->db
->select( 'ipblocks', [ '*' ], [ 'ipb_id' => $block->getId() ] );
240 $pager = new BlockListPager( new SpecialPage(), [] );
241 $pager->preprocessResults( $result );
243 $wrappedPager = TestingAccessWrapper
::newFromObject( $pager );
245 $restrictions = $wrappedPager->restrictions
;
246 $this->assertInternalType( 'array', $restrictions );
248 $restriction = $restrictions[0];
249 $this->assertEquals( $page->getId(), $restriction->getValue() );
250 $this->assertEquals( $page->getId(), $restriction->getTitle()->getArticleID() );
251 $this->assertEquals( $title->getDBkey(), $restriction->getTitle()->getDBkey() );
252 $this->assertEquals( $title->getNamespace(), $restriction->getTitle()->getNamespace() );
254 // Delete the block and the restrictions.