Merge "docs: Remove 'code-coverage' placeholder directory"
[lhc/web/wiklou.git] / tests / phpunit / includes / specials / pagers / BlockListPagerTest.php
1 <?php
2
3 use MediaWiki\Block\Restriction\PageRestriction;
4 use MediaWiki\Block\Restriction\NamespaceRestriction;
5 use MediaWiki\MediaWikiServices;
6 use Wikimedia\TestingAccessWrapper;
7
8 /**
9 * @group Database
10 * @coversDefaultClass BlockListPager
11 */
12 class BlockListPagerTest extends MediaWikiTestCase {
13
14 /**
15 * @covers ::formatValue
16 * @dataProvider formatValueEmptyProvider
17 * @dataProvider formatValueDefaultProvider
18 * @param string $name
19 * @param string $value
20 * @param string $expected
21 */
22 public function testFormatValue( $name, $value, $expected, $row = null ) {
23 $this->setMwGlobals( [
24 'wgEnablePartialBlocks' => false,
25 ] );
26 $row = $row ?: new stdClass;
27 $pager = new BlockListPager( new SpecialPage(), [] );
28 $wrappedPager = TestingAccessWrapper::newFromObject( $pager );
29 $wrappedPager->mCurrentRow = $row;
30
31 $formatted = $pager->formatValue( $name, $value );
32 $this->assertEquals( $expected, $formatted );
33 }
34
35 /**
36 * Test empty values.
37 */
38 public function formatValueEmptyProvider() {
39 return [
40 [
41 'test',
42 '',
43 'Unable to format test',
44 ],
45 [
46 'ipb_timestamp',
47 wfTimestamp( TS_UNIX ),
48 date( 'H:i, j F Y' ),
49 ],
50 [
51 'ipb_expiry',
52 '',
53 'infinite<br />0 minutes left',
54 ],
55 ];
56 }
57
58 /**
59 * Test the default row values.
60 */
61 public function formatValueDefaultProvider() {
62 $row = (object)[
63 'ipb_user' => 0,
64 'ipb_address' => '127.0.0.1',
65 'ipb_by_text' => 'Admin',
66 'ipb_create_account' => 1,
67 'ipb_auto' => 0,
68 'ipb_anon_only' => 0,
69 'ipb_create_account' => 1,
70 'ipb_enable_autoblock' => 1,
71 'ipb_deleted' => 0,
72 'ipb_block_email' => 0,
73 'ipb_allow_usertalk' => 0,
74 'ipb_sitewide' => 1,
75 ];
76
77 return [
78 [
79 'test',
80 '',
81 'Unable to format test',
82 $row,
83 ],
84 [
85 'ipb_timestamp',
86 wfTimestamp( TS_UNIX ),
87 date( 'H:i, j F Y' ),
88 $row,
89 ],
90 [
91 'ipb_expiry',
92 '',
93 'infinite<br />0 minutes left',
94 $row,
95 ],
96 [
97 'ipb_by',
98 '',
99 $row->ipb_by_text,
100 $row,
101 ],
102 [
103 'ipb_params',
104 '',
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 ];
145
146 $wrappedPager = TestingAccessWrapper::newFromObject( $pager );
147 $wrappedPager->restrictions = $restrictions;
148
149 $formatted = $pager->formatValue( 'ipb_params', '' );
150 $this->assertEquals( '<ul><li>'
151 // FIXME: Expectation value should not be dynamic
152 // and must not depend on a localisation message.
153 // TODO: Mock the message or consider using qqx.
154 . wfMessage( 'blocklist-editing' )->text()
155 . '<ul><li>'
156 . wfMessage( 'blocklist-editing-page' )->text()
157 . '<ul><li>'
158 . '<a href="/wiki/Victor_Frankenstein" title="'
159 . $pageName
160 . '">'
161 . $pageName
162 . '</a></li></ul></li><li>'
163 . wfMessage( 'blocklist-editing-ns' )->text()
164 . '<ul><li>'
165 . '<a href="/w/index.php?title=Special:AllPages&amp;namespace=0" title="'
166 . 'Special:AllPages'
167 . '">'
168 . wfMessage( 'blanknamespace' )->text()
169 . '</a></li></ul></li></ul></li></ul>',
170 $formatted
171 );
172 }
173
174 /**
175 * @covers ::preprocessResults
176 */
177 public function testPreprocessResults() {
178 // Test the Link Cache.
179 $linkCache = MediaWikiServices::getInstance()->getLinkCache();
180 $wrappedlinkCache = TestingAccessWrapper::newFromObject( $linkCache );
181
182 $links = [
183 'User:127.0.0.1',
184 'User_talk:127.0.0.1',
185 'User:Admin',
186 'User_talk:Admin',
187 ];
188
189 foreach ( $links as $link ) {
190 $this->assertNull( $wrappedlinkCache->badLinks->get( $link ) );
191 }
192
193 $row = (object)[
194 'ipb_address' => '127.0.0.1',
195 'by_user_name' => 'Admin',
196 'ipb_sitewide' => 1,
197 'ipb_timestamp' => $this->db->timestamp( wfTimestamp( TS_MW ) ),
198 ];
199 $pager = new BlockListPager( new SpecialPage(), [] );
200 $pager->preprocessResults( [ $row ] );
201
202 foreach ( $links as $link ) {
203 $this->assertSame( 1, $wrappedlinkCache->badLinks->get( $link ) );
204 }
205
206 // Test Sitewide Blocks.
207 $row = (object)[
208 'ipb_address' => '127.0.0.1',
209 'by_user_name' => 'Admin',
210 'ipb_sitewide' => 1,
211 ];
212 $pager = new BlockListPager( new SpecialPage(), [] );
213 $pager->preprocessResults( [ $row ] );
214
215 $this->assertObjectNotHasAttribute( 'ipb_restrictions', $row );
216
217 $pageName = 'Victor Frankenstein';
218 $page = $this->getExistingTestPage( 'Victor Frankenstein' );
219 $title = $page->getTitle();
220
221 $target = '127.0.0.1';
222
223 // Test Partial Blocks Blocks.
224 $block = new Block( [
225 'address' => $target,
226 'by' => $this->getTestSysop()->getUser()->getId(),
227 'reason' => 'Parce que',
228 'expiry' => $this->db->getInfinity(),
229 'sitewide' => false,
230 ] );
231 $block->setRestrictions( [
232 new PageRestriction( 0, $page->getId() ),
233 ] );
234 $block->insert();
235
236 $result = $this->db->select( 'ipblocks', [ '*' ], [ 'ipb_id' => $block->getId() ] );
237
238 $pager = new BlockListPager( new SpecialPage(), [] );
239 $pager->preprocessResults( $result );
240
241 $wrappedPager = TestingAccessWrapper::newFromObject( $pager );
242
243 $restrictions = $wrappedPager->restrictions;
244 $this->assertInternalType( 'array', $restrictions );
245
246 $restriction = $restrictions[0];
247 $this->assertEquals( $page->getId(), $restriction->getValue() );
248 $this->assertEquals( $page->getId(), $restriction->getTitle()->getArticleId() );
249 $this->assertEquals( $title->getDBKey(), $restriction->getTitle()->getDBKey() );
250 $this->assertEquals( $title->getNamespace(), $restriction->getTitle()->getNamespace() );
251
252 // Delete the block and the restrictions.
253 $block->delete();
254 }
255 }