Merge "Remove unused fields in ForeignDBViaLBRepo"
[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 $pager = new BlockListPager( new SpecialPage(), [] );
115
116 $row = (object)[
117 'ipb_id' => 0,
118 'ipb_user' => 0,
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,
124 'ipb_sitewide' => 0,
125 ];
126 $wrappedPager = TestingAccessWrapper::newFromObject( $pager );
127 $wrappedPager->mCurrentRow = $row;
128
129 $pageName = 'Victor Frankenstein';
130 $page = $this->insertPage( $pageName );
131 $title = $page['title'];
132 $pageId = $page['id'];
133
134 $restrictions = [
135 ( new PageRestriction( 0, $pageId ) )->setTitle( $title )
136 ];
137
138 $wrappedPager = TestingAccessWrapper::newFromObject( $pager );
139 $wrappedPager->restrictions = $restrictions;
140
141 $formatted = $pager->formatValue( 'ipb_params', '' );
142 $this->assertEquals( '<ul><li>'
143 . wfMessage( 'blocklist-editing' )->text()
144 . '<ul><li><a href="/index.php/'
145 . $title->getDBKey()
146 . '" title="'
147 . $pageName
148 . '">'
149 . $pageName
150 . '</a></li></ul></li></ul>',
151 $formatted
152 );
153 }
154
155 /**
156 * @covers ::preprocessResults
157 */
158 public function testPreprocessResults() {
159 // Test the Link Cache.
160 $linkCache = MediaWikiServices::getInstance()->getLinkCache();
161 $wrappedlinkCache = TestingAccessWrapper::newFromObject( $linkCache );
162
163 $links = [
164 'User:127.0.0.1',
165 'User_talk:127.0.0.1',
166 'User:Admin',
167 'User_talk:Admin',
168 ];
169
170 foreach ( $links as $link ) {
171 $this->assertNull( $wrappedlinkCache->badLinks->get( $link ) );
172 }
173
174 $row = (object)[
175 'ipb_address' => '127.0.0.1',
176 'by_user_name' => 'Admin',
177 'ipb_sitewide' => 1,
178 'ipb_timestamp' => $this->db->timestamp( wfTimestamp( TS_MW ) ),
179 ];
180 $pager = new BlockListPager( new SpecialPage(), [] );
181 $pager->preprocessResults( [ $row ] );
182
183 foreach ( $links as $link ) {
184 $this->assertSame( 1, $wrappedlinkCache->badLinks->get( $link ) );
185 }
186
187 // Test Sitewide Blocks.
188 $row = (object)[
189 'ipb_address' => '127.0.0.1',
190 'by_user_name' => 'Admin',
191 'ipb_sitewide' => 1,
192 ];
193 $pager = new BlockListPager( new SpecialPage(), [] );
194 $pager->preprocessResults( [ $row ] );
195
196 $this->assertObjectNotHasAttribute( 'ipb_restrictions', $row );
197
198 $pageName = 'Victor Frankenstein';
199 $page = $this->getExistingTestPage( 'Victor Frankenstein' );
200 $title = $page->getTitle();
201
202 $target = '127.0.0.1';
203
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(),
210 'sitewide' => false,
211 ] );
212 $block->setRestrictions( [
213 new PageRestriction( 0, $page->getId() ),
214 ] );
215 $block->insert();
216
217 $result = $this->db->select( 'ipblocks', [ '*' ], [ 'ipb_id' => $block->getId() ] );
218
219 $pager = new BlockListPager( new SpecialPage(), [] );
220 $pager->preprocessResults( $result );
221
222 $wrappedPager = TestingAccessWrapper::newFromObject( $pager );
223
224 $restrictions = $wrappedPager->restrictions;
225 $this->assertInternalType( 'array', $restrictions );
226
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() );
232
233 // Delete the block and the restrictions.
234 $block->delete();
235 }
236 }