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