Merge "Fix use of GenderCache in ApiPageSet::processTitlesArray"
[lhc/web/wiklou.git] / tests / phpunit / includes / specials / pagers / BlockListPagerTest.php
1 <?php
2
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;
8
9 /**
10 * @group Database
11 * @coversDefaultClass BlockListPager
12 */
13 class BlockListPagerTest extends MediaWikiTestCase {
14
15 /**
16 * @var LinkRenderer
17 */
18 private $linkRenderer;
19
20 protected function setUp() {
21 parent::setUp();
22
23 $this->linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
24 }
25
26 /**
27 * @covers ::formatValue
28 * @dataProvider formatValueEmptyProvider
29 * @dataProvider formatValueDefaultProvider
30 * @param string $name
31 * @param string $expected
32 */
33 public function testFormatValue( $name, $expected = null, $row = null ) {
34 $this->setMwGlobals( [
35 'wgEnablePartialBlocks' => false,
36 ] );
37 // Set the time to now so it does not get off during the test.
38 MWTimestamp::setFakeTime( MWTimestamp::time() );
39
40 $value = $name === 'ipb_timestamp' ? MWTimestamp::time() : '';
41 $expected = $expected ?? MWTimestamp::getInstance()->format( 'H:i, j F Y' );
42
43 $row = $row ?: new stdClass;
44 $pager = new BlockListPager( new SpecialPage(), [], $this->linkRenderer );
45 $wrappedPager = TestingAccessWrapper::newFromObject( $pager );
46 $wrappedPager->mCurrentRow = $row;
47
48 $formatted = $pager->formatValue( $name, $value );
49 $this->assertEquals( $expected, $formatted );
50
51 // Reset the time.
52 MWTimestamp::setFakeTime( false );
53 }
54
55 /**
56 * Test empty values.
57 */
58 public function formatValueEmptyProvider() {
59 return [
60 [
61 'test',
62 'Unable to format test',
63 ],
64 [
65 'ipb_timestamp',
66 ],
67 [
68 'ipb_expiry',
69 'infinite<br />0 minutes left',
70 ],
71 ];
72 }
73
74 /**
75 * Test the default row values.
76 */
77 public function formatValueDefaultProvider() {
78 $row = (object)[
79 'ipb_user' => 0,
80 'ipb_address' => '127.0.0.1',
81 'ipb_by_text' => 'Admin',
82 'ipb_create_account' => 1,
83 'ipb_auto' => 0,
84 'ipb_anon_only' => 0,
85 'ipb_create_account' => 1,
86 'ipb_enable_autoblock' => 1,
87 'ipb_deleted' => 0,
88 'ipb_block_email' => 0,
89 'ipb_allow_usertalk' => 0,
90 'ipb_sitewide' => 1,
91 ];
92
93 return [
94 [
95 'test',
96 'Unable to format test',
97 $row,
98 ],
99 [
100 'ipb_timestamp',
101 null,
102 $row,
103 ],
104 [
105 'ipb_expiry',
106 'infinite<br />0 minutes left',
107 $row,
108 ],
109 [
110 'ipb_by',
111 $row->ipb_by_text,
112 $row,
113 ],
114 [
115 'ipb_params',
116 '<ul><li>account creation disabled</li><li>cannot edit own talk page</li></ul>',
117 $row,
118 ]
119 ];
120 }
121
122 /**
123 * @covers ::formatValue
124 * @covers ::getRestrictionListHTML
125 */
126 public function testFormatValueRestrictions() {
127 $this->setMwGlobals( [
128 'wgArticlePath' => '/wiki/$1',
129 'wgScript' => '/w/index.php',
130 ] );
131
132 $pager = new BlockListPager( new SpecialPage(), [], $this->linkRenderer );
133
134 $row = (object)[
135 'ipb_id' => 0,
136 'ipb_user' => 0,
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,
142 'ipb_sitewide' => 0,
143 ];
144 $wrappedPager = TestingAccessWrapper::newFromObject( $pager );
145 $wrappedPager->mCurrentRow = $row;
146
147 $pageName = 'Victor Frankenstein';
148 $page = $this->insertPage( $pageName );
149 $title = $page['title'];
150 $pageId = $page['id'];
151
152 $restrictions = [
153 ( new PageRestriction( 0, $pageId ) )->setTitle( $title ),
154 new NamespaceRestriction( 0, NS_MAIN ),
155 // Deleted page.
156 new PageRestriction( 0, 999999 ),
157 ];
158
159 $wrappedPager = TestingAccessWrapper::newFromObject( $pager );
160 $wrappedPager->restrictions = $restrictions;
161
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()
168 . '<ul><li>'
169 . wfMessage( 'blocklist-editing-page' )->text()
170 . '<ul><li>'
171 . '<a href="/wiki/Victor_Frankenstein" title="'
172 . $pageName
173 . '">'
174 . $pageName
175 . '</a></li></ul></li><li>'
176 . wfMessage( 'blocklist-editing-ns' )->text()
177 . '<ul><li>'
178 . '<a href="/w/index.php?title=Special:AllPages&amp;namespace=0" title="'
179 . 'Special:AllPages'
180 . '">'
181 . wfMessage( 'blanknamespace' )->text()
182 . '</a></li></ul></li></ul></li></ul>',
183 $formatted
184 );
185 }
186
187 /**
188 * @covers ::preprocessResults
189 */
190 public function testPreprocessResults() {
191 // Test the Link Cache.
192 $linkCache = MediaWikiServices::getInstance()->getLinkCache();
193 $wrappedlinkCache = TestingAccessWrapper::newFromObject( $linkCache );
194
195 $links = [
196 'User:127.0.0.1',
197 'User_talk:127.0.0.1',
198 'User:Admin',
199 'User_talk:Admin',
200 ];
201
202 foreach ( $links as $link ) {
203 $this->assertNull( $wrappedlinkCache->badLinks->get( $link ) );
204 }
205
206 $row = (object)[
207 'ipb_address' => '127.0.0.1',
208 'by_user_name' => 'Admin',
209 'ipb_sitewide' => 1,
210 'ipb_timestamp' => $this->db->timestamp( wfTimestamp( TS_MW ) ),
211 ];
212 $pager = new BlockListPager( new SpecialPage(), [], $this->linkRenderer );
213 $pager->preprocessResults( [ $row ] );
214
215 foreach ( $links as $link ) {
216 $this->assertSame( 1, $wrappedlinkCache->badLinks->get( $link ) );
217 }
218
219 // Test Sitewide Blocks.
220 $row = (object)[
221 'ipb_address' => '127.0.0.1',
222 'by_user_name' => 'Admin',
223 'ipb_sitewide' => 1,
224 ];
225 $pager = new BlockListPager( new SpecialPage(), [], $this->linkRenderer );
226 $pager->preprocessResults( [ $row ] );
227
228 $this->assertObjectNotHasAttribute( 'ipb_restrictions', $row );
229
230 $pageName = 'Victor Frankenstein';
231 $page = $this->getExistingTestPage( 'Victor Frankenstein' );
232 $title = $page->getTitle();
233
234 $target = '127.0.0.1';
235
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(),
242 'sitewide' => false,
243 ] );
244 $block->setRestrictions( [
245 new PageRestriction( 0, $page->getId() ),
246 ] );
247 $block->insert();
248
249 $result = $this->db->select( 'ipblocks', [ '*' ], [ 'ipb_id' => $block->getId() ] );
250
251 $pager = new BlockListPager( new SpecialPage(), [], $this->linkRenderer );
252 $pager->preprocessResults( $result );
253
254 $wrappedPager = TestingAccessWrapper::newFromObject( $pager );
255
256 $restrictions = $wrappedPager->restrictions;
257 $this->assertInternalType( 'array', $restrictions );
258
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() );
264
265 // Delete the block and the restrictions.
266 $block->delete();
267 }
268 }