6 * ^--- needed for language cache stuff
8 class TitleTest
extends MediaWikiTestCase
{
9 protected function setUp() {
12 $this->setMwGlobals( array(
13 'wgLanguageCode' => 'en',
14 'wgContLang' => Language
::factory( 'en' ),
16 'wgLang' => Language
::factory( 'en' ),
17 'wgAllowUserJs' => false,
18 'wgDefaultLanguageVariant' => false,
22 function testLegalChars() {
23 $titlechars = Title
::legalChars();
25 foreach ( range( 1, 255 ) as $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" );
30 $this->assertTrue( (bool)preg_match( "/[$titlechars]/", $chr ), "chr($num) = $chr is a valid titlechar" );
36 * See also mediawiki.Title.test.js
38 function testSecureAndSplit() {
51 'File_talk:Example.svg',
55 // Length is 256 total, but only title part matters
56 'Category:' . str_repeat( 'x', 248 ),
57 str_repeat( 'x', 252 )
59 $this->assertInstanceOf( 'Title', Title
::newFromText( $text ), "Valid: $text" );
67 // Bad characters forbidden regardless of wgLegalTitleChars
79 // XML/HTML character entity references
80 // Note: Commented out because they are not marked invalid by the PHP test as
81 // Title::newFromText runs Sanitizer::decodeCharReferencesAndNormalize first.
85 // Subject of NS_TALK does not roundtrip to NS_MAIN
86 'Talk:File:Example.svg',
87 // Directory navigation
100 str_repeat( 'x', 256 ),
101 // Namespace prefix without actual title
107 $this->assertNull( Title
::newFromText( $text ), "Invalid: $text" );
111 public static function provideConvertByteClassToUnicodeClass() {
114 ' %!"$&\'()*,\\-.\\/0-9:;=?@A-Z\\\\^_`a-z~\\x80-\\xFF+',
115 ' %!"$&\'()*,\\-./0-9:;=?@A-Z\\\\\\^_`a-z~+\\u0080-\\uFFFF',
119 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
122 'QWERTY\\x66-\\xFD+',
123 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
131 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
134 'QWERTY\\x66-\\x80+\\x23',
135 'QWERTYf-\\x7F+#\\u0080-\\uFFFF',
138 'QWERTY\\x66-\\x80+\\xD3',
139 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
143 '\\\\\\u0080-\\uFFFF',
147 '\\-\\u0080-\\uFFFF',
151 'QWERTY\\-\\u0080-\\uFFFF',
159 'A-\\x7F\\u0080-\\uFFFF',
162 '\\x66-\\x77QWERTY\\x88-\\x91FXZ',
163 'f-wQWERTYFXZ\\u0080-\\uFFFF',
166 '\\x66-\\x99QWERTY\\xAA-\\xEEFXZ',
167 'f-\\x7FQWERTYFXZ\\u0080-\\uFFFF',
173 * @dataProvider provideConvertByteClassToUnicodeClass
175 function testConvertByteClassToUnicodeClass( $byteClass, $unicodeClass ) {
176 $this->assertEquals( $unicodeClass, Title
::convertByteClassToUnicodeClass( $byteClass ) );
180 * @dataProvider provideBug31100
182 function testBug31100FixSpecialName( $text, $expectedParam ) {
183 $title = Title
::newFromText( $text );
184 $fixed = $title->fixSpecialName();
185 $stuff = explode( '/', $fixed->getDBkey(), 2 );
186 if ( count( $stuff ) == 2 ) {
191 $this->assertEquals( $expectedParam, $par, "Bug 31100 regression check: Title->fixSpecialName() should preserve parameter" );
194 public static function provideBug31100() {
196 array( 'Special:Version', null ),
197 array( 'Special:Version/', '' ),
198 array( 'Special:Version/param', 'param' ),
203 * Auth-less test of Title::isValidMoveOperation
206 * @param string $source
207 * @param string $target
208 * @param array|string|true $expected Required error
209 * @dataProvider provideTestIsValidMoveOperation
211 function testIsValidMoveOperation( $source, $target, $expected ) {
212 $title = Title
::newFromText( $source );
213 $nt = Title
::newFromText( $target );
214 $errors = $title->isValidMoveOperation( $nt, false );
215 if ( $expected === true ) {
216 $this->assertTrue( $errors );
218 $errors = $this->flattenErrorsArray( $errors );
219 foreach ( (array)$expected as $error ) {
220 $this->assertContains( $error, $errors );
226 * Provides test parameter values for testIsValidMoveOperation()
228 function dataTestIsValidMoveOperation() {
230 array( 'Test', 'Test', 'selfmove' ),
231 array( 'File:Test.jpg', 'Page', 'imagenocrossnamespace' )
236 * Auth-less test of Title::userCan
238 * @param array $whitelistRegexp
239 * @param string $source
240 * @param string $action
241 * @param array|string|true $expected Required error
243 * @covers Title::checkReadPermissions
244 * @dataProvider dataWgWhitelistReadRegexp
246 function testWgWhitelistReadRegexp( $whitelistRegexp, $source, $action, $expected ) {
247 // $wgWhitelistReadRegexp must be an array. Since the provided test cases
248 // usually have only one regex, it is more concise to write the lonely regex
249 // as a string. Thus we cast to an array() to honor $wgWhitelistReadRegexp
251 if ( is_string( $whitelistRegexp ) ) {
252 $whitelistRegexp = array( $whitelistRegexp );
255 $title = Title
::newFromDBkey( $source );
257 global $wgGroupPermissions;
258 $oldPermissions = $wgGroupPermissions;
259 // Disallow all so we can ensure our regex works
260 $wgGroupPermissions = array();
261 $wgGroupPermissions['*']['read'] = false;
263 global $wgWhitelistRead;
264 $oldWhitelist = $wgWhitelistRead;
265 // Undo any LocalSettings explicite whitelists so they won't cause a
266 // failing test to succeed. Set it to some random non sense just
267 // to make sure we properly test Title::checkReadPermissions()
268 $wgWhitelistRead = array( 'some random non sense title' );
270 global $wgWhitelistReadRegexp;
271 $oldWhitelistRegexp = $wgWhitelistReadRegexp;
272 $wgWhitelistReadRegexp = $whitelistRegexp;
274 // Just use $wgUser which in test is a user object for '127.0.0.1'
276 // Invalidate user rights cache to take in account $wgGroupPermissions
278 $wgUser->clearInstanceCache();
279 $errors = $title->userCan( $action, $wgUser );
282 $wgGroupPermissions = $oldPermissions;
283 $wgWhitelistRead = $oldWhitelist;
284 $wgWhitelistReadRegexp = $oldWhitelistRegexp;
286 if ( is_bool( $expected ) ) {
287 # Forge the assertion message depending on the assertion expectation
288 $allowableness = $expected
289 ?
" should be allowed"
290 : " should NOT be allowed";
291 $this->assertEquals( $expected, $errors, "User action '$action' on [[$source]] $allowableness." );
293 $errors = $this->flattenErrorsArray( $errors );
294 foreach ( (array)$expected as $error ) {
295 $this->assertContains( $error, $errors );
301 * Provides test parameter values for testWgWhitelistReadRegexp()
303 function dataWgWhitelistReadRegexp() {
308 // Everything, if this doesn't work, we're really in trouble
309 array( '/.*/', 'Main_Page', 'read', $ALLOWED ),
310 array( '/.*/', 'Main_Page', 'edit', $DISALLOWED ),
312 // We validate against the title name, not the db key
313 array( '/^Main_Page$/', 'Main_Page', 'read', $DISALLOWED ),
315 array( '/^Main/', 'Main_Page', 'read', $ALLOWED ),
316 array( '/^Main.*/', 'Main_Page', 'read', $ALLOWED ),
318 array( '/Mic\sCheck/', 'Mic Check', 'read', $ALLOWED ),
320 // ...without unicode modifier
321 array( '/Unicode Test . Yes/', 'Unicode Test Ñ Yes', 'read', $DISALLOWED ),
322 // ...with unicode modifier
323 array( '/Unicode Test . Yes/u', 'Unicode Test Ñ Yes', 'read', $ALLOWED ),
325 array( '/MiC ChEcK/', 'mic check', 'read', $DISALLOWED ),
326 array( '/MiC ChEcK/i', 'mic check', 'read', $ALLOWED ),
328 // From DefaultSettings.php:
329 array( "@^UsEr.*@i", 'User is banned', 'read', $ALLOWED ),
330 array( "@^UsEr.*@i", 'User:John Doe', 'read', $ALLOWED ),
333 array( '/^Special:NewPages$/', 'Special:NewPages', 'read', $ALLOWED ),
334 array( null, 'Special:Newpages', 'read', $DISALLOWED ),
339 function flattenErrorsArray( $errors ) {
341 foreach ( $errors as $error ) {
342 $result[] = $error[0];
348 public static function provideTestIsValidMoveOperation() {
350 array( 'Test', 'Test', 'selfmove' ),
351 array( 'File:Test.jpg', 'Page', 'imagenocrossnamespace' )
356 * @dataProvider provideCasesForGetpageviewlanguage
358 function testGetpageviewlanguage( $expected, $titleText, $contLang, $lang, $variant, $msg = '' ) {
359 global $wgLanguageCode, $wgContLang, $wgLang, $wgDefaultLanguageVariant, $wgAllowUserJs;
361 // Setup environnement for this test
362 $wgLanguageCode = $contLang;
363 $wgContLang = Language
::factory( $contLang );
364 $wgLang = Language
::factory( $lang );
365 $wgDefaultLanguageVariant = $variant;
366 $wgAllowUserJs = true;
368 $title = Title
::newFromText( $titleText );
369 $this->assertInstanceOf( 'Title', $title,
370 "Test must be passed a valid title text, you gave '$titleText'"
372 $this->assertEquals( $expected,
373 $title->getPageViewLanguage()->getCode(),
378 public static function provideCasesForGetpageviewlanguage() {
382 # - wgContLang (expected in most case)
383 # - wgLang (on some specific pages)
384 # - wgDefaultLanguageVariant
387 array( 'fr', 'Help:I_need_somebody', 'fr', 'fr', false ),
388 array( 'es', 'Help:I_need_somebody', 'es', 'zh-tw', false ),
389 array( 'zh', 'Help:I_need_somebody', 'zh', 'zh-tw', false ),
391 array( 'es', 'Help:I_need_somebody', 'es', 'zh-tw', 'zh-cn' ),
392 array( 'es', 'MediaWiki:About', 'es', 'zh-tw', 'zh-cn' ),
393 array( 'es', 'MediaWiki:About/', 'es', 'zh-tw', 'zh-cn' ),
394 array( 'de', 'MediaWiki:About/de', 'es', 'zh-tw', 'zh-cn' ),
395 array( 'en', 'MediaWiki:Common.js', 'es', 'zh-tw', 'zh-cn' ),
396 array( 'en', 'MediaWiki:Common.css', 'es', 'zh-tw', 'zh-cn' ),
397 array( 'en', 'User:JohnDoe/Common.js', 'es', 'zh-tw', 'zh-cn' ),
398 array( 'en', 'User:JohnDoe/Monobook.css', 'es', 'zh-tw', 'zh-cn' ),
400 array( 'zh-cn', 'Help:I_need_somebody', 'zh', 'zh-tw', 'zh-cn' ),
401 array( 'zh', 'MediaWiki:About', 'zh', 'zh-tw', 'zh-cn' ),
402 array( 'zh', 'MediaWiki:About/', 'zh', 'zh-tw', 'zh-cn' ),
403 array( 'de', 'MediaWiki:About/de', 'zh', 'zh-tw', 'zh-cn' ),
404 array( 'zh-cn', 'MediaWiki:About/zh-cn', 'zh', 'zh-tw', 'zh-cn' ),
405 array( 'zh-tw', 'MediaWiki:About/zh-tw', 'zh', 'zh-tw', 'zh-cn' ),
406 array( 'en', 'MediaWiki:Common.js', 'zh', 'zh-tw', 'zh-cn' ),
407 array( 'en', 'MediaWiki:Common.css', 'zh', 'zh-tw', 'zh-cn' ),
408 array( 'en', 'User:JohnDoe/Common.js', 'zh', 'zh-tw', 'zh-cn' ),
409 array( 'en', 'User:JohnDoe/Monobook.css', 'zh', 'zh-tw', 'zh-cn' ),
411 array( 'zh-tw', 'Special:NewPages', 'es', 'zh-tw', 'zh-cn' ),
412 array( 'zh-tw', 'Special:NewPages', 'zh', 'zh-tw', 'zh-cn' ),
418 * @dataProvider provideBaseTitleCases
420 function testExtractingBaseTextFromTitle( $title, $expected, $msg = '' ) {
421 $title = Title
::newFromText( $title );
422 $this->assertEquals( $expected,
423 $title->getBaseText(),
428 public static function provideBaseTitleCases() {
430 # Title, expected base, optional message
431 array( 'User:John_Doe/subOne/subTwo', 'John Doe/subOne' ),
432 array( 'User:Foo/Bar/Baz', 'Foo/Bar' ),
437 * @dataProvider provideRootTitleCases
439 function testExtractingRootTextFromTitle( $title, $expected, $msg = '' ) {
440 $title = Title
::newFromText( $title );
441 $this->assertEquals( $expected,
442 $title->getRootText(),
447 public static function provideRootTitleCases() {
449 # Title, expected base, optional message
450 array( 'User:John_Doe/subOne/subTwo', 'John Doe' ),
451 array( 'User:Foo/Bar/Baz', 'Foo' ),
456 * @todo Handle $wgNamespacesWithSubpages cases
457 * @dataProvider provideSubpageTitleCases
459 function testExtractingSubpageTextFromTitle( $title, $expected, $msg = '' ) {
460 $title = Title
::newFromText( $title );
461 $this->assertEquals( $expected,
462 $title->getSubpageText(),
467 public static function provideSubpageTitleCases() {
469 # Title, expected base, optional message
470 array( 'User:John_Doe/subOne/subTwo', 'subTwo' ),
471 array( 'User:John_Doe/subOne', 'subOne' ),