3 use MediaWiki\Block\Restriction\PageRestriction
;
4 use MediaWiki\MediaWikiServices
;
5 use Wikimedia\TestingAccessWrapper
;
9 * @coversDefaultClass BlockListPager
11 class BlockListPagerTest
extends MediaWikiTestCase
{
14 * @covers ::formatValue
15 * @dataProvider formatValueEmptyProvider
16 * @dataProvider formatValueDefaultProvider
18 * @param string $value
19 * @param string $expected
21 public function testFormatValue( $name, $value, $expected, $row = null ) {
22 $this->setMwGlobals( [
23 'wgEnablePartialBlocks' => false,
25 $row = $row ?
: new stdClass
;
26 $pager = new BlockListPager( new SpecialPage(), [] );
27 $wrappedPager = TestingAccessWrapper
::newFromObject( $pager );
28 $wrappedPager->mCurrentRow
= $row;
30 $formatted = $pager->formatValue( $name, $value );
31 $this->assertEquals( $expected, $formatted );
37 public function formatValueEmptyProvider() {
42 'Unable to format test',
46 wfTimestamp( TS_UNIX
),
52 'infinite<br />0 minutes left',
58 * Test the default row values.
60 public function formatValueDefaultProvider() {
63 'ipb_address' => '127.0.0.1',
64 'ipb_by_text' => 'Admin',
65 'ipb_create_account' => 1,
68 'ipb_create_account' => 1,
69 'ipb_enable_autoblock' => 1,
71 'ipb_block_email' => 0,
72 'ipb_allow_usertalk' => 0,
80 'Unable to format test',
85 wfTimestamp( TS_UNIX
),
92 'infinite<br />0 minutes left',
104 '<ul><li>account creation disabled</li><li>cannot edit own talk page</li></ul>',
111 * @covers ::formatValue
113 public function testFormatValueRestrictions() {
114 $pager = new BlockListPager( new SpecialPage(), [] );
119 'ipb_anon_only' => 0,
120 'ipb_enable_autoblock' => 0,
121 'ipb_create_account' => 0,
122 'ipb_block_email' => 0,
123 'ipb_allow_usertalk' => 1,
126 $wrappedPager = TestingAccessWrapper
::newFromObject( $pager );
127 $wrappedPager->mCurrentRow
= $row;
129 $pageName = 'Victor Frankenstein';
130 $page = $this->insertPage( $pageName );
131 $title = $page['title'];
132 $pageId = $page['id'];
135 ( new PageRestriction( 0, $pageId ) )->setTitle( $title )
138 $wrappedPager = TestingAccessWrapper
::newFromObject( $pager );
139 $wrappedPager->restrictions
= $restrictions;
141 $formatted = $pager->formatValue( 'ipb_params', '' );
142 $this->assertEquals( '<ul><li>'
143 . wfMessage( 'blocklist-editing' )->text()
144 . '<ul><li><a href="/index.php/'
150 . '</a></li></ul></li></ul>',
156 * @covers ::preprocessResults
158 public function testPreprocessResults() {
159 // Test the Link Cache.
160 $linkCache = MediaWikiServices
::getInstance()->getLinkCache();
161 $wrappedlinkCache = TestingAccessWrapper
::newFromObject( $linkCache );
165 'User_talk:127.0.0.1',
170 foreach ( $links as $link ) {
171 $this->assertNull( $wrappedlinkCache->badLinks
->get( $link ) );
175 'ipb_address' => '127.0.0.1',
176 'by_user_name' => 'Admin',
178 'ipb_timestamp' => $this->db
->timestamp( wfTimestamp( TS_MW
) ),
180 $pager = new BlockListPager( new SpecialPage(), [] );
181 $pager->preprocessResults( [ $row ] );
183 foreach ( $links as $link ) {
184 $this->assertSame( 1, $wrappedlinkCache->badLinks
->get( $link ) );
187 // Test Sitewide Blocks.
189 'ipb_address' => '127.0.0.1',
190 'by_user_name' => 'Admin',
193 $pager = new BlockListPager( new SpecialPage(), [] );
194 $pager->preprocessResults( [ $row ] );
196 $this->assertObjectNotHasAttribute( 'ipb_restrictions', $row );
198 $pageName = 'Victor Frankenstein';
199 $page = $this->getExistingTestPage( 'Victor Frankenstein' );
200 $title = $page->getTitle();
202 $target = '127.0.0.1';
204 // Test Partial Blocks Blocks.
205 $block = new Block( [
206 'address' => $target,
207 'by' => $this->getTestSysop()->getUser()->getId(),
208 'reason' => 'Parce que',
209 'expiry' => $this->db
->getInfinity(),
212 $block->setRestrictions( [
213 new PageRestriction( 0, $page->getId() ),
217 $result = $this->db
->select( 'ipblocks', [ '*' ], [ 'ipb_id' => $block->getId() ] );
219 $pager = new BlockListPager( new SpecialPage(), [] );
220 $pager->preprocessResults( $result );
222 $wrappedPager = TestingAccessWrapper
::newFromObject( $pager );
224 $restrictions = $wrappedPager->restrictions
;
225 $this->assertInternalType( 'array', $restrictions );
227 $restriction = $restrictions[0];
228 $this->assertEquals( $page->getId(), $restriction->getValue() );
229 $this->assertEquals( $page->getId(), $restriction->getTitle()->getArticleId() );
230 $this->assertEquals( $title->getDBKey(), $restriction->getTitle()->getDBKey() );
231 $this->assertEquals( $title->getNamespace(), $restriction->getTitle()->getNamespace() );
233 // Delete the block and the restrictions.