Merge "Add way of including all stderr output when executing command"
[lhc/web/wiklou.git] / tests / phpunit / includes / TitleTest.php
1 <?php
2
3 /**
4 *
5 * @group Database
6 * ^--- needed for language cache stuff
7 */
8 class TitleTest extends MediaWikiTestCase {
9 protected function setUp() {
10 parent::setUp();
11
12 $this->setMwGlobals( array(
13 'wgLanguageCode' => 'en',
14 'wgContLang' => Language::factory( 'en' ),
15 // User language
16 'wgLang' => Language::factory( 'en' ),
17 'wgAllowUserJs' => false,
18 'wgDefaultLanguageVariant' => false,
19 ) );
20 }
21
22 function testLegalChars() {
23 $titlechars = Title::legalChars();
24
25 foreach ( range( 1, 255 ) as $num ) {
26 $chr = chr( $num );
27 if ( strpos( "#[]{}<>|", $chr ) !== false || preg_match( "/[\\x00-\\x1f\\x7f]/", $chr ) ) {
28 $this->assertFalse( (bool)preg_match( "/[$titlechars]/", $chr ), "chr($num) = $chr is not a valid titlechar" );
29 } else {
30 $this->assertTrue( (bool)preg_match( "/[$titlechars]/", $chr ), "chr($num) = $chr is a valid titlechar" );
31 }
32 }
33 }
34
35 public static function provideConvertByteClassToUnicodeClass() {
36 return array(
37 array(
38 ' %!"$&\'()*,\\-.\\/0-9:;=?@A-Z\\\\^_`a-z~\\x80-\\xFF+',
39 ' %!"$&\'()*,\\-./0-9:;=?@A-Z\\\\\\^_`a-z~+\\u0080-\\uFFFF',
40 ),
41 array(
42 'QWERTYf-\\xFF+',
43 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
44 ),
45 array(
46 'QWERTY\\x66-\\xFD+',
47 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
48 ),
49 array(
50 'QWERTYf-y+',
51 'QWERTYf-y+',
52 ),
53 array(
54 'QWERTYf-\\x80+',
55 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
56 ),
57 array(
58 'QWERTY\\x66-\\x80+\\x23',
59 'QWERTYf-\\x7F+#\\u0080-\\uFFFF',
60 ),
61 array(
62 'QWERTY\\x66-\\x80+\\xD3',
63 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
64 ),
65 array(
66 '\\\\\\x99',
67 '\\\\\\u0080-\\uFFFF',
68 ),
69 array(
70 '-\\x99',
71 '\\-\\u0080-\\uFFFF',
72 ),
73 array(
74 'QWERTY\\-\\x99',
75 'QWERTY\\-\\u0080-\\uFFFF',
76 ),
77 array(
78 '\\\\x99',
79 '\\\\x99',
80 ),
81 array(
82 'A-\\x9F',
83 'A-\\x7F\\u0080-\\uFFFF',
84 ),
85 array(
86 '\\x66-\\x77QWERTY\\x88-\\x91FXZ',
87 'f-wQWERTYFXZ\\u0080-\\uFFFF',
88 ),
89 array(
90 '\\x66-\\x99QWERTY\\xAA-\\xEEFXZ',
91 'f-\\x7FQWERTYFXZ\\u0080-\\uFFFF',
92 ),
93 );
94 }
95
96 /**
97 * @dataProvider provideConvertByteClassToUnicodeClass
98 */
99 function testConvertByteClassToUnicodeClass( $byteClass, $unicodeClass ) {
100 $this->assertEquals( $unicodeClass, Title::convertByteClassToUnicodeClass( $byteClass ) );
101 }
102
103 /**
104 * @dataProvider provideBug31100
105 */
106 function testBug31100FixSpecialName( $text, $expectedParam ) {
107 $title = Title::newFromText( $text );
108 $fixed = $title->fixSpecialName();
109 $stuff = explode( '/', $fixed->getDBkey(), 2 );
110 if ( count( $stuff ) == 2 ) {
111 $par = $stuff[1];
112 } else {
113 $par = null;
114 }
115 $this->assertEquals( $expectedParam, $par, "Bug 31100 regression check: Title->fixSpecialName() should preserve parameter" );
116 }
117
118 public static function provideBug31100() {
119 return array(
120 array( 'Special:Version', null ),
121 array( 'Special:Version/', '' ),
122 array( 'Special:Version/param', 'param' ),
123 );
124 }
125
126 /**
127 * Auth-less test of Title::isValidMoveOperation
128 *
129 * @group Database
130 * @param string $source
131 * @param string $target
132 * @param array|string|true $expected Required error
133 * @dataProvider provideTestIsValidMoveOperation
134 */
135 function testIsValidMoveOperation( $source, $target, $expected ) {
136 $title = Title::newFromText( $source );
137 $nt = Title::newFromText( $target );
138 $errors = $title->isValidMoveOperation( $nt, false );
139 if ( $expected === true ) {
140 $this->assertTrue( $errors );
141 } else {
142 $errors = $this->flattenErrorsArray( $errors );
143 foreach ( (array)$expected as $error ) {
144 $this->assertContains( $error, $errors );
145 }
146 }
147 }
148
149 /**
150 * Provides test parameter values for testIsValidMoveOperation()
151 */
152 function dataTestIsValidMoveOperation() {
153 return array(
154 array( 'Test', 'Test', 'selfmove' ),
155 array( 'File:Test.jpg', 'Page', 'imagenocrossnamespace' )
156 );
157 }
158
159 /**
160 * Auth-less test of Title::userCan
161 *
162 * @param array $whitelistRegexp
163 * @param string $source
164 * @param string $action
165 * @param array|string|true $expected Required error
166 *
167 * @covers Title::checkReadPermissions
168 * @dataProvider dataWgWhitelistReadRegexp
169 */
170 function testWgWhitelistReadRegexp( $whitelistRegexp, $source, $action, $expected ) {
171 // $wgWhitelistReadRegexp must be an array. Since the provided test cases
172 // usually have only one regex, it is more concise to write the lonely regex
173 // as a string. Thus we cast to an array() to honor $wgWhitelistReadRegexp
174 // type requisite.
175 if ( is_string( $whitelistRegexp ) ) {
176 $whitelistRegexp = array( $whitelistRegexp );
177 }
178
179 $title = Title::newFromDBkey( $source );
180
181 global $wgGroupPermissions;
182 $oldPermissions = $wgGroupPermissions;
183 // Disallow all so we can ensure our regex works
184 $wgGroupPermissions = array();
185 $wgGroupPermissions['*']['read'] = false;
186
187 global $wgWhitelistRead;
188 $oldWhitelist = $wgWhitelistRead;
189 // Undo any LocalSettings explicite whitelists so they won't cause a
190 // failing test to succeed. Set it to some random non sense just
191 // to make sure we properly test Title::checkReadPermissions()
192 $wgWhitelistRead = array( 'some random non sense title' );
193
194 global $wgWhitelistReadRegexp;
195 $oldWhitelistRegexp = $wgWhitelistReadRegexp;
196 $wgWhitelistReadRegexp = $whitelistRegexp;
197
198 // Just use $wgUser which in test is a user object for '127.0.0.1'
199 global $wgUser;
200 // Invalidate user rights cache to take in account $wgGroupPermissions
201 // change above.
202 $wgUser->clearInstanceCache();
203 $errors = $title->userCan( $action, $wgUser );
204
205 // Restore globals
206 $wgGroupPermissions = $oldPermissions;
207 $wgWhitelistRead = $oldWhitelist;
208 $wgWhitelistReadRegexp = $oldWhitelistRegexp;
209
210 if ( is_bool( $expected ) ) {
211 # Forge the assertion message depending on the assertion expectation
212 $allowableness = $expected
213 ? " should be allowed"
214 : " should NOT be allowed";
215 $this->assertEquals( $expected, $errors, "User action '$action' on [[$source]] $allowableness." );
216 } else {
217 $errors = $this->flattenErrorsArray( $errors );
218 foreach ( (array)$expected as $error ) {
219 $this->assertContains( $error, $errors );
220 }
221 }
222 }
223
224 /**
225 * Provides test parameter values for testWgWhitelistReadRegexp()
226 */
227 function dataWgWhitelistReadRegexp() {
228 $ALLOWED = true;
229 $DISALLOWED = false;
230
231 return array(
232 // Everything, if this doesn't work, we're really in trouble
233 array( '/.*/', 'Main_Page', 'read', $ALLOWED ),
234 array( '/.*/', 'Main_Page', 'edit', $DISALLOWED ),
235
236 // We validate against the title name, not the db key
237 array( '/^Main_Page$/', 'Main_Page', 'read', $DISALLOWED ),
238 // Main page
239 array( '/^Main/', 'Main_Page', 'read', $ALLOWED ),
240 array( '/^Main.*/', 'Main_Page', 'read', $ALLOWED ),
241 // With spaces
242 array( '/Mic\sCheck/', 'Mic Check', 'read', $ALLOWED ),
243 // Unicode multibyte
244 // ...without unicode modifier
245 array( '/Unicode Test . Yes/', 'Unicode Test Ñ Yes', 'read', $DISALLOWED ),
246 // ...with unicode modifier
247 array( '/Unicode Test . Yes/u', 'Unicode Test Ñ Yes', 'read', $ALLOWED ),
248 // Case insensitive
249 array( '/MiC ChEcK/', 'mic check', 'read', $DISALLOWED ),
250 array( '/MiC ChEcK/i', 'mic check', 'read', $ALLOWED ),
251
252 // From DefaultSettings.php:
253 array( "@^UsEr.*@i", 'User is banned', 'read', $ALLOWED ),
254 array( "@^UsEr.*@i", 'User:John Doe', 'read', $ALLOWED ),
255
256 // With namespaces:
257 array( '/^Special:NewPages$/', 'Special:NewPages', 'read', $ALLOWED ),
258 array( null, 'Special:Newpages', 'read', $DISALLOWED ),
259
260 );
261 }
262
263 function flattenErrorsArray( $errors ) {
264 $result = array();
265 foreach ( $errors as $error ) {
266 $result[] = $error[0];
267 }
268
269 return $result;
270 }
271
272 public static function provideTestIsValidMoveOperation() {
273 return array(
274 array( 'Test', 'Test', 'selfmove' ),
275 array( 'File:Test.jpg', 'Page', 'imagenocrossnamespace' )
276 );
277 }
278
279 /**
280 * @dataProvider provideCasesForGetpageviewlanguage
281 */
282 function testGetpageviewlanguage( $expected, $titleText, $contLang, $lang, $variant, $msg = '' ) {
283 global $wgLanguageCode, $wgContLang, $wgLang, $wgDefaultLanguageVariant, $wgAllowUserJs;
284
285 // Setup environnement for this test
286 $wgLanguageCode = $contLang;
287 $wgContLang = Language::factory( $contLang );
288 $wgLang = Language::factory( $lang );
289 $wgDefaultLanguageVariant = $variant;
290 $wgAllowUserJs = true;
291
292 $title = Title::newFromText( $titleText );
293 $this->assertInstanceOf( 'Title', $title,
294 "Test must be passed a valid title text, you gave '$titleText'"
295 );
296 $this->assertEquals( $expected,
297 $title->getPageViewLanguage()->getCode(),
298 $msg
299 );
300 }
301
302 public static function provideCasesForGetpageviewlanguage() {
303 # Format:
304 # - expected
305 # - Title name
306 # - wgContLang (expected in most case)
307 # - wgLang (on some specific pages)
308 # - wgDefaultLanguageVariant
309 # - Optional message
310 return array(
311 array( 'fr', 'Help:I_need_somebody', 'fr', 'fr', false ),
312 array( 'es', 'Help:I_need_somebody', 'es', 'zh-tw', false ),
313 array( 'zh', 'Help:I_need_somebody', 'zh', 'zh-tw', false ),
314
315 array( 'es', 'Help:I_need_somebody', 'es', 'zh-tw', 'zh-cn' ),
316 array( 'es', 'MediaWiki:About', 'es', 'zh-tw', 'zh-cn' ),
317 array( 'es', 'MediaWiki:About/', 'es', 'zh-tw', 'zh-cn' ),
318 array( 'de', 'MediaWiki:About/de', 'es', 'zh-tw', 'zh-cn' ),
319 array( 'en', 'MediaWiki:Common.js', 'es', 'zh-tw', 'zh-cn' ),
320 array( 'en', 'MediaWiki:Common.css', 'es', 'zh-tw', 'zh-cn' ),
321 array( 'en', 'User:JohnDoe/Common.js', 'es', 'zh-tw', 'zh-cn' ),
322 array( 'en', 'User:JohnDoe/Monobook.css', 'es', 'zh-tw', 'zh-cn' ),
323
324 array( 'zh-cn', 'Help:I_need_somebody', 'zh', 'zh-tw', 'zh-cn' ),
325 array( 'zh', 'MediaWiki:About', 'zh', 'zh-tw', 'zh-cn' ),
326 array( 'zh', 'MediaWiki:About/', 'zh', 'zh-tw', 'zh-cn' ),
327 array( 'de', 'MediaWiki:About/de', 'zh', 'zh-tw', 'zh-cn' ),
328 array( 'zh-cn', 'MediaWiki:About/zh-cn', 'zh', 'zh-tw', 'zh-cn' ),
329 array( 'zh-tw', 'MediaWiki:About/zh-tw', 'zh', 'zh-tw', 'zh-cn' ),
330 array( 'en', 'MediaWiki:Common.js', 'zh', 'zh-tw', 'zh-cn' ),
331 array( 'en', 'MediaWiki:Common.css', 'zh', 'zh-tw', 'zh-cn' ),
332 array( 'en', 'User:JohnDoe/Common.js', 'zh', 'zh-tw', 'zh-cn' ),
333 array( 'en', 'User:JohnDoe/Monobook.css', 'zh', 'zh-tw', 'zh-cn' ),
334
335 array( 'zh-tw', 'Special:NewPages', 'es', 'zh-tw', 'zh-cn' ),
336 array( 'zh-tw', 'Special:NewPages', 'zh', 'zh-tw', 'zh-cn' ),
337
338 );
339 }
340
341 /**
342 * @dataProvider provideBaseTitleCases
343 */
344 function testExtractingBaseTextFromTitle( $title, $expected, $msg = '' ) {
345 $title = Title::newFromText( $title );
346 $this->assertEquals( $expected,
347 $title->getBaseText(),
348 $msg
349 );
350 }
351
352 public static function provideBaseTitleCases() {
353 return array(
354 # Title, expected base, optional message
355 array( 'User:John_Doe/subOne/subTwo', 'John Doe/subOne' ),
356 array( 'User:Foo/Bar/Baz', 'Foo/Bar' ),
357 );
358 }
359
360 /**
361 * @dataProvider provideRootTitleCases
362 */
363 function testExtractingRootTextFromTitle( $title, $expected, $msg = '' ) {
364 $title = Title::newFromText( $title );
365 $this->assertEquals( $expected,
366 $title->getRootText(),
367 $msg
368 );
369 }
370
371 public static function provideRootTitleCases() {
372 return array(
373 # Title, expected base, optional message
374 array( 'User:John_Doe/subOne/subTwo', 'John Doe' ),
375 array( 'User:Foo/Bar/Baz', 'Foo' ),
376 );
377 }
378
379 /**
380 * @todo Handle $wgNamespacesWithSubpages cases
381 * @dataProvider provideSubpageTitleCases
382 */
383 function testExtractingSubpageTextFromTitle( $title, $expected, $msg = '' ) {
384 $title = Title::newFromText( $title );
385 $this->assertEquals( $expected,
386 $title->getSubpageText(),
387 $msg
388 );
389 }
390
391 public static function provideSubpageTitleCases() {
392 return array(
393 # Title, expected base, optional message
394 array( 'User:John_Doe/subOne/subTwo', 'subTwo' ),
395 array( 'User:John_Doe/subOne', 'subOne' ),
396 );
397 }
398 }