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