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
{
18 private $linkRenderer;
20 protected function setUp() {
23 $this->linkRenderer
= MediaWikiServices
::getInstance()->getLinkRenderer();
27 * @covers ::formatValue
28 * @dataProvider formatValueEmptyProvider
29 * @dataProvider formatValueDefaultProvider
31 * @param string $expected
33 public function testFormatValue( $name, $expected = null, $row = null ) {
34 $this->setMwGlobals( [
35 'wgEnablePartialBlocks' => false,
37 // Set the time to now so it does not get off during the test.
38 MWTimestamp
::setFakeTime( MWTimestamp
::time() );
40 $value = $name === 'ipb_timestamp' ? MWTimestamp
::time() : '';
41 $expected = $expected ?? MWTimestamp
::getInstance()->format( 'H:i, j F Y' );
43 $row = $row ?
: new stdClass
;
44 $pager = new BlockListPager( new SpecialPage(), [], $this->linkRenderer
);
45 $wrappedPager = TestingAccessWrapper
::newFromObject( $pager );
46 $wrappedPager->mCurrentRow
= $row;
48 $formatted = $pager->formatValue( $name, $value );
49 $this->assertEquals( $expected, $formatted );
52 MWTimestamp
::setFakeTime( false );
58 public function formatValueEmptyProvider() {
62 'Unable to format test',
69 'infinite<br />0 minutes left',
75 * Test the default row values.
77 public function formatValueDefaultProvider() {
80 'ipb_address' => '127.0.0.1',
81 'ipb_by_text' => 'Admin',
82 'ipb_create_account' => 1,
85 'ipb_create_account' => 1,
86 'ipb_enable_autoblock' => 1,
88 'ipb_block_email' => 0,
89 'ipb_allow_usertalk' => 0,
96 'Unable to format test',
106 'infinite<br />0 minutes left',
116 '<ul><li>account creation disabled</li><li>cannot edit own talk page</li></ul>',
123 * @covers ::formatValue
124 * @covers ::getRestrictionListHTML
126 public function testFormatValueRestrictions() {
127 $this->setMwGlobals( [
128 'wgArticlePath' => '/wiki/$1',
129 'wgScript' => '/w/index.php',
132 $pager = new BlockListPager( new SpecialPage(), [], $this->linkRenderer
);
137 'ipb_anon_only' => 0,
138 'ipb_enable_autoblock' => 0,
139 'ipb_create_account' => 0,
140 'ipb_block_email' => 0,
141 'ipb_allow_usertalk' => 1,
144 $wrappedPager = TestingAccessWrapper
::newFromObject( $pager );
145 $wrappedPager->mCurrentRow
= $row;
147 $pageName = 'Victor Frankenstein';
148 $page = $this->insertPage( $pageName );
149 $title = $page['title'];
150 $pageId = $page['id'];
153 ( new PageRestriction( 0, $pageId ) )->setTitle( $title ),
154 new NamespaceRestriction( 0, NS_MAIN
),
156 new PageRestriction( 0, 999999 ),
159 $wrappedPager = TestingAccessWrapper
::newFromObject( $pager );
160 $wrappedPager->restrictions
= $restrictions;
162 $formatted = $pager->formatValue( 'ipb_params', '' );
163 $this->assertEquals( '<ul><li>'
164 // FIXME: Expectation value should not be dynamic
165 // and must not depend on a localisation message.
166 // TODO: Mock the message or consider using qqx.
167 . wfMessage( 'blocklist-editing' )->text()
169 . wfMessage( 'blocklist-editing-page' )->text()
171 . '<a href="/wiki/Victor_Frankenstein" title="'
175 . '</a></li></ul></li><li>'
176 . wfMessage( 'blocklist-editing-ns' )->text()
178 . '<a href="/w/index.php?title=Special:AllPages&namespace=0" title="'
181 . wfMessage( 'blanknamespace' )->text()
182 . '</a></li></ul></li></ul></li></ul>',
188 * @covers ::preprocessResults
190 public function testPreprocessResults() {
191 // Test the Link Cache.
192 $linkCache = MediaWikiServices
::getInstance()->getLinkCache();
193 $wrappedlinkCache = TestingAccessWrapper
::newFromObject( $linkCache );
197 'User_talk:127.0.0.1',
202 foreach ( $links as $link ) {
203 $this->assertNull( $wrappedlinkCache->badLinks
->get( $link ) );
207 'ipb_address' => '127.0.0.1',
208 'by_user_name' => 'Admin',
210 'ipb_timestamp' => $this->db
->timestamp( wfTimestamp( TS_MW
) ),
212 $pager = new BlockListPager( new SpecialPage(), [], $this->linkRenderer
);
213 $pager->preprocessResults( [ $row ] );
215 foreach ( $links as $link ) {
216 $this->assertSame( 1, $wrappedlinkCache->badLinks
->get( $link ) );
219 // Test Sitewide Blocks.
221 'ipb_address' => '127.0.0.1',
222 'by_user_name' => 'Admin',
225 $pager = new BlockListPager( new SpecialPage(), [], $this->linkRenderer
);
226 $pager->preprocessResults( [ $row ] );
228 $this->assertObjectNotHasAttribute( 'ipb_restrictions', $row );
230 $pageName = 'Victor Frankenstein';
231 $page = $this->getExistingTestPage( 'Victor Frankenstein' );
232 $title = $page->getTitle();
234 $target = '127.0.0.1';
236 // Test Partial Blocks Blocks.
237 $block = new DatabaseBlock( [
238 'address' => $target,
239 'by' => $this->getTestSysop()->getUser()->getId(),
240 'reason' => 'Parce que',
241 'expiry' => $this->db
->getInfinity(),
244 $block->setRestrictions( [
245 new PageRestriction( 0, $page->getId() ),
249 $result = $this->db
->select( 'ipblocks', [ '*' ], [ 'ipb_id' => $block->getId() ] );
251 $pager = new BlockListPager( new SpecialPage(), [], $this->linkRenderer
);
252 $pager->preprocessResults( $result );
254 $wrappedPager = TestingAccessWrapper
::newFromObject( $pager );
256 $restrictions = $wrappedPager->restrictions
;
257 $this->assertInternalType( 'array', $restrictions );
259 $restriction = $restrictions[0];
260 $this->assertEquals( $page->getId(), $restriction->getValue() );
261 $this->assertEquals( $page->getId(), $restriction->getTitle()->getArticleID() );
262 $this->assertEquals( $title->getDBkey(), $restriction->getTitle()->getDBkey() );
263 $this->assertEquals( $title->getNamespace(), $restriction->getTitle()->getNamespace() );
265 // Delete the block and the restrictions.