3 use MediaWiki\Block\Restriction\PageRestriction
;
4 use MediaWiki\Block\Restriction\NamespaceRestriction
;
5 use MediaWiki\MediaWikiServices
;
6 use Wikimedia\TestingAccessWrapper
;
10 * @coversDefaultClass BlockListPager
12 class BlockListPagerTest
extends MediaWikiTestCase
{
15 * @covers ::formatValue
16 * @dataProvider formatValueEmptyProvider
17 * @dataProvider formatValueDefaultProvider
19 * @param string $expected
21 public function testFormatValue( $name, $expected = null, $row = null ) {
22 $this->setMwGlobals( [
23 'wgEnablePartialBlocks' => false,
25 // Set the time to now so it does not get off during the test.
26 MWTimestamp
::setFakeTime( MWTimestamp
::time() );
28 $value = $name === 'ipb_timestamp' ? MWTimestamp
::time() : '';
29 $expected = $expected ?? MWTimestamp
::getInstance()->format( 'H:i, j F Y' );
31 $row = $row ?
: new stdClass
;
32 $pager = new BlockListPager( new SpecialPage(), [] );
33 $wrappedPager = TestingAccessWrapper
::newFromObject( $pager );
34 $wrappedPager->mCurrentRow
= $row;
36 $formatted = $pager->formatValue( $name, $value );
37 $this->assertEquals( $expected, $formatted );
40 MWTimestamp
::setFakeTime( false );
46 public function formatValueEmptyProvider() {
50 'Unable to format test',
57 'infinite<br />0 minutes left',
63 * Test the default row values.
65 public function formatValueDefaultProvider() {
68 'ipb_address' => '127.0.0.1',
69 'ipb_by_text' => 'Admin',
70 'ipb_create_account' => 1,
73 'ipb_create_account' => 1,
74 'ipb_enable_autoblock' => 1,
76 'ipb_block_email' => 0,
77 'ipb_allow_usertalk' => 0,
84 'Unable to format test',
94 'infinite<br />0 minutes left',
104 '<ul><li>account creation disabled</li><li>cannot edit own talk page</li></ul>',
111 * @covers ::formatValue
112 * @covers ::getRestrictionListHTML
114 public function testFormatValueRestrictions() {
115 $this->setMwGlobals( [
116 'wgArticlePath' => '/wiki/$1',
117 'wgScript' => '/w/index.php',
120 $pager = new BlockListPager( new SpecialPage(), [] );
125 'ipb_anon_only' => 0,
126 'ipb_enable_autoblock' => 0,
127 'ipb_create_account' => 0,
128 'ipb_block_email' => 0,
129 'ipb_allow_usertalk' => 1,
132 $wrappedPager = TestingAccessWrapper
::newFromObject( $pager );
133 $wrappedPager->mCurrentRow
= $row;
135 $pageName = 'Victor Frankenstein';
136 $page = $this->insertPage( $pageName );
137 $title = $page['title'];
138 $pageId = $page['id'];
141 ( new PageRestriction( 0, $pageId ) )->setTitle( $title ),
142 new NamespaceRestriction( 0, NS_MAIN
),
144 new PageRestriction( 0, 999999 ),
147 $wrappedPager = TestingAccessWrapper
::newFromObject( $pager );
148 $wrappedPager->restrictions
= $restrictions;
150 $formatted = $pager->formatValue( 'ipb_params', '' );
151 $this->assertEquals( '<ul><li>'
152 // FIXME: Expectation value should not be dynamic
153 // and must not depend on a localisation message.
154 // TODO: Mock the message or consider using qqx.
155 . wfMessage( 'blocklist-editing' )->text()
157 . wfMessage( 'blocklist-editing-page' )->text()
159 . '<a href="/wiki/Victor_Frankenstein" title="'
163 . '</a></li></ul></li><li>'
164 . wfMessage( 'blocklist-editing-ns' )->text()
166 . '<a href="/w/index.php?title=Special:AllPages&namespace=0" title="'
169 . wfMessage( 'blanknamespace' )->text()
170 . '</a></li></ul></li></ul></li></ul>',
176 * @covers ::preprocessResults
178 public function testPreprocessResults() {
179 // Test the Link Cache.
180 $linkCache = MediaWikiServices
::getInstance()->getLinkCache();
181 $wrappedlinkCache = TestingAccessWrapper
::newFromObject( $linkCache );
185 'User_talk:127.0.0.1',
190 foreach ( $links as $link ) {
191 $this->assertNull( $wrappedlinkCache->badLinks
->get( $link ) );
195 'ipb_address' => '127.0.0.1',
196 'by_user_name' => 'Admin',
198 'ipb_timestamp' => $this->db
->timestamp( wfTimestamp( TS_MW
) ),
200 $pager = new BlockListPager( new SpecialPage(), [] );
201 $pager->preprocessResults( [ $row ] );
203 foreach ( $links as $link ) {
204 $this->assertSame( 1, $wrappedlinkCache->badLinks
->get( $link ) );
207 // Test Sitewide Blocks.
209 'ipb_address' => '127.0.0.1',
210 'by_user_name' => 'Admin',
213 $pager = new BlockListPager( new SpecialPage(), [] );
214 $pager->preprocessResults( [ $row ] );
216 $this->assertObjectNotHasAttribute( 'ipb_restrictions', $row );
218 $pageName = 'Victor Frankenstein';
219 $page = $this->getExistingTestPage( 'Victor Frankenstein' );
220 $title = $page->getTitle();
222 $target = '127.0.0.1';
224 // Test Partial Blocks Blocks.
225 $block = new Block( [
226 'address' => $target,
227 'by' => $this->getTestSysop()->getUser()->getId(),
228 'reason' => 'Parce que',
229 'expiry' => $this->db
->getInfinity(),
232 $block->setRestrictions( [
233 new PageRestriction( 0, $page->getId() ),
237 $result = $this->db
->select( 'ipblocks', [ '*' ], [ 'ipb_id' => $block->getId() ] );
239 $pager = new BlockListPager( new SpecialPage(), [] );
240 $pager->preprocessResults( $result );
242 $wrappedPager = TestingAccessWrapper
::newFromObject( $pager );
244 $restrictions = $wrappedPager->restrictions
;
245 $this->assertInternalType( 'array', $restrictions );
247 $restriction = $restrictions[0];
248 $this->assertEquals( $page->getId(), $restriction->getValue() );
249 $this->assertEquals( $page->getId(), $restriction->getTitle()->getArticleID() );
250 $this->assertEquals( $title->getDBKey(), $restriction->getTitle()->getDBKey() );
251 $this->assertEquals( $title->getNamespace(), $restriction->getTitle()->getNamespace() );
253 // Delete the block and the restrictions.