5 * ^--- needed for language cache stuff
7 class TitleTest
extends MediaWikiTestCase
{
8 protected function setUp() {
11 $this->setMwGlobals( array(
12 'wgLanguageCode' => 'en',
13 'wgContLang' => Language
::factory( 'en' ),
15 'wgLang' => Language
::factory( 'en' ),
16 'wgAllowUserJs' => false,
17 'wgDefaultLanguageVariant' => false,
22 * @covers Title::legalChars
24 public function testLegalChars() {
25 $titlechars = Title
::legalChars();
27 foreach ( range( 1, 255 ) as $num ) {
29 if ( strpos( "#[]{}<>|", $chr ) !== false ||
preg_match( "/[\\x00-\\x1f\\x7f]/", $chr ) ) {
30 $this->assertFalse( (bool)preg_match( "/[$titlechars]/", $chr ), "chr($num) = $chr is not a valid titlechar" );
32 $this->assertTrue( (bool)preg_match( "/[$titlechars]/", $chr ), "chr($num) = $chr is a valid titlechar" );
38 * See also mediawiki.Title.test.js
39 * @covers Title::secureAndSplit
40 * @todo This method should be split into 2 separate tests each with a provider
42 public function testSecureAndSplit() {
55 'File_talk:Example.svg',
59 // Length is 256 total, but only title part matters
60 'Category:' . str_repeat( 'x', 248 ),
61 str_repeat( 'x', 252 )
63 $this->assertInstanceOf( 'Title', Title
::newFromText( $text ), "Valid: $text" );
72 // Bad characters forbidden regardless of wgLegalTitleChars
84 // XML/HTML character entity references
85 // Note: Commented out because they are not marked invalid by the PHP test as
86 // Title::newFromText runs Sanitizer::decodeCharReferencesAndNormalize first.
90 // Subject of NS_TALK does not roundtrip to NS_MAIN
91 'Talk:File:Example.svg',
92 // Directory navigation
105 str_repeat( 'x', 256 ),
106 // Namespace prefix without actual title
112 $this->assertNull( Title
::newFromText( $text ), "Invalid: $text" );
116 public static function provideConvertByteClassToUnicodeClass() {
119 ' %!"$&\'()*,\\-.\\/0-9:;=?@A-Z\\\\^_`a-z~\\x80-\\xFF+',
120 ' %!"$&\'()*,\\-./0-9:;=?@A-Z\\\\\\^_`a-z~+\\u0080-\\uFFFF',
124 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
127 'QWERTY\\x66-\\xFD+',
128 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
136 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
139 'QWERTY\\x66-\\x80+\\x23',
140 'QWERTYf-\\x7F+#\\u0080-\\uFFFF',
143 'QWERTY\\x66-\\x80+\\xD3',
144 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
148 '\\\\\\u0080-\\uFFFF',
152 '\\-\\u0080-\\uFFFF',
156 'QWERTY\\-\\u0080-\\uFFFF',
164 'A-\\x7F\\u0080-\\uFFFF',
167 '\\x66-\\x77QWERTY\\x88-\\x91FXZ',
168 'f-wQWERTYFXZ\\u0080-\\uFFFF',
171 '\\x66-\\x99QWERTY\\xAA-\\xEEFXZ',
172 'f-\\x7FQWERTYFXZ\\u0080-\\uFFFF',
178 * @dataProvider provideConvertByteClassToUnicodeClass
179 * @covers Title::convertByteClassToUnicodeClass
181 public function testConvertByteClassToUnicodeClass( $byteClass, $unicodeClass ) {
182 $this->assertEquals( $unicodeClass, Title
::convertByteClassToUnicodeClass( $byteClass ) );
186 * @dataProvider provideBug31100
187 * @covers Title::fixSpecialName
189 public function testBug31100FixSpecialName( $text, $expectedParam ) {
190 $title = Title
::newFromText( $text );
191 $fixed = $title->fixSpecialName();
192 $stuff = explode( '/', $fixed->getDBkey(), 2 );
193 if ( count( $stuff ) == 2 ) {
198 $this->assertEquals( $expectedParam, $par, "Bug 31100 regression check: Title->fixSpecialName() should preserve parameter" );
201 public static function provideBug31100() {
203 array( 'Special:Version', null ),
204 array( 'Special:Version/', '' ),
205 array( 'Special:Version/param', 'param' ),
210 * Auth-less test of Title::isValidMoveOperation
213 * @param string $source
214 * @param string $target
215 * @param array|string|bool $expected Required error
216 * @dataProvider provideTestIsValidMoveOperation
217 * @covers Title::isValidMoveOperation
219 public function testIsValidMoveOperation( $source, $target, $expected ) {
220 $title = Title
::newFromText( $source );
221 $nt = Title
::newFromText( $target );
222 $errors = $title->isValidMoveOperation( $nt, false );
223 if ( $expected === true ) {
224 $this->assertTrue( $errors );
226 $errors = $this->flattenErrorsArray( $errors );
227 foreach ( (array)$expected as $error ) {
228 $this->assertContains( $error, $errors );
234 * Provides test parameter values for testIsValidMoveOperation()
236 public function dataTestIsValidMoveOperation() {
238 array( 'Test', 'Test', 'selfmove' ),
239 array( 'File:Test.jpg', 'Page', 'imagenocrossnamespace' )
244 * Auth-less test of Title::userCan
246 * @param array $whitelistRegexp
247 * @param string $source
248 * @param string $action
249 * @param array|string|bool $expected Required error
251 * @covers Title::checkReadPermissions
252 * @dataProvider dataWgWhitelistReadRegexp
254 public function testWgWhitelistReadRegexp( $whitelistRegexp, $source, $action, $expected ) {
255 // $wgWhitelistReadRegexp must be an array. Since the provided test cases
256 // usually have only one regex, it is more concise to write the lonely regex
257 // as a string. Thus we cast to an array() to honor $wgWhitelistReadRegexp
259 if ( is_string( $whitelistRegexp ) ) {
260 $whitelistRegexp = array( $whitelistRegexp );
263 $title = Title
::newFromDBkey( $source );
265 global $wgGroupPermissions;
266 $oldPermissions = $wgGroupPermissions;
267 // Disallow all so we can ensure our regex works
268 $wgGroupPermissions = array();
269 $wgGroupPermissions['*']['read'] = false;
271 global $wgWhitelistRead;
272 $oldWhitelist = $wgWhitelistRead;
273 // Undo any LocalSettings explicite whitelists so they won't cause a
274 // failing test to succeed. Set it to some random non sense just
275 // to make sure we properly test Title::checkReadPermissions()
276 $wgWhitelistRead = array( 'some random non sense title' );
278 global $wgWhitelistReadRegexp;
279 $oldWhitelistRegexp = $wgWhitelistReadRegexp;
280 $wgWhitelistReadRegexp = $whitelistRegexp;
282 // Just use $wgUser which in test is a user object for '127.0.0.1'
284 // Invalidate user rights cache to take in account $wgGroupPermissions
286 $wgUser->clearInstanceCache();
287 $errors = $title->userCan( $action, $wgUser );
290 $wgGroupPermissions = $oldPermissions;
291 $wgWhitelistRead = $oldWhitelist;
292 $wgWhitelistReadRegexp = $oldWhitelistRegexp;
294 if ( is_bool( $expected ) ) {
295 # Forge the assertion message depending on the assertion expectation
296 $allowableness = $expected
297 ?
" should be allowed"
298 : " should NOT be allowed";
299 $this->assertEquals( $expected, $errors, "User action '$action' on [[$source]] $allowableness." );
301 $errors = $this->flattenErrorsArray( $errors );
302 foreach ( (array)$expected as $error ) {
303 $this->assertContains( $error, $errors );
309 * Provides test parameter values for testWgWhitelistReadRegexp()
311 public function dataWgWhitelistReadRegexp() {
316 // Everything, if this doesn't work, we're really in trouble
317 array( '/.*/', 'Main_Page', 'read', $ALLOWED ),
318 array( '/.*/', 'Main_Page', 'edit', $DISALLOWED ),
320 // We validate against the title name, not the db key
321 array( '/^Main_Page$/', 'Main_Page', 'read', $DISALLOWED ),
323 array( '/^Main/', 'Main_Page', 'read', $ALLOWED ),
324 array( '/^Main.*/', 'Main_Page', 'read', $ALLOWED ),
326 array( '/Mic\sCheck/', 'Mic Check', 'read', $ALLOWED ),
328 // ...without unicode modifier
329 array( '/Unicode Test . Yes/', 'Unicode Test Ñ Yes', 'read', $DISALLOWED ),
330 // ...with unicode modifier
331 array( '/Unicode Test . Yes/u', 'Unicode Test Ñ Yes', 'read', $ALLOWED ),
333 array( '/MiC ChEcK/', 'mic check', 'read', $DISALLOWED ),
334 array( '/MiC ChEcK/i', 'mic check', 'read', $ALLOWED ),
336 // From DefaultSettings.php:
337 array( "@^UsEr.*@i", 'User is banned', 'read', $ALLOWED ),
338 array( "@^UsEr.*@i", 'User:John Doe', 'read', $ALLOWED ),
341 array( '/^Special:NewPages$/', 'Special:NewPages', 'read', $ALLOWED ),
342 array( null, 'Special:Newpages', 'read', $DISALLOWED ),
347 public function flattenErrorsArray( $errors ) {
349 foreach ( $errors as $error ) {
350 $result[] = $error[0];
356 public static function provideTestIsValidMoveOperation() {
358 array( 'Test', 'Test', 'selfmove' ),
359 array( 'File:Test.jpg', 'Page', 'imagenocrossnamespace' )
364 * @dataProvider provideGetPageViewLanguage
365 * @covers Title::getPageViewLanguage
367 public function testGetPageViewLanguage( $expected, $titleText, $contLang, $lang, $variant, $msg = '' ) {
368 global $wgLanguageCode, $wgContLang, $wgLang, $wgDefaultLanguageVariant, $wgAllowUserJs;
370 // Setup environnement for this test
371 $wgLanguageCode = $contLang;
372 $wgContLang = Language
::factory( $contLang );
373 $wgLang = Language
::factory( $lang );
374 $wgDefaultLanguageVariant = $variant;
375 $wgAllowUserJs = true;
377 $title = Title
::newFromText( $titleText );
378 $this->assertInstanceOf( 'Title', $title,
379 "Test must be passed a valid title text, you gave '$titleText'"
381 $this->assertEquals( $expected,
382 $title->getPageViewLanguage()->getCode(),
387 public static function provideGetPageViewLanguage() {
391 # - wgContLang (expected in most case)
392 # - wgLang (on some specific pages)
393 # - wgDefaultLanguageVariant
396 array( 'fr', 'Help:I_need_somebody', 'fr', 'fr', false ),
397 array( 'es', 'Help:I_need_somebody', 'es', 'zh-tw', false ),
398 array( 'zh', 'Help:I_need_somebody', 'zh', 'zh-tw', false ),
400 array( 'es', 'Help:I_need_somebody', 'es', 'zh-tw', 'zh-cn' ),
401 array( 'es', 'MediaWiki:About', 'es', 'zh-tw', 'zh-cn' ),
402 array( 'es', 'MediaWiki:About/', 'es', 'zh-tw', 'zh-cn' ),
403 array( 'de', 'MediaWiki:About/de', 'es', 'zh-tw', 'zh-cn' ),
404 array( 'en', 'MediaWiki:Common.js', 'es', 'zh-tw', 'zh-cn' ),
405 array( 'en', 'MediaWiki:Common.css', 'es', 'zh-tw', 'zh-cn' ),
406 array( 'en', 'User:JohnDoe/Common.js', 'es', 'zh-tw', 'zh-cn' ),
407 array( 'en', 'User:JohnDoe/Monobook.css', 'es', 'zh-tw', 'zh-cn' ),
409 array( 'zh-cn', 'Help:I_need_somebody', 'zh', 'zh-tw', 'zh-cn' ),
410 array( 'zh', 'MediaWiki:About', 'zh', 'zh-tw', 'zh-cn' ),
411 array( 'zh', 'MediaWiki:About/', 'zh', 'zh-tw', 'zh-cn' ),
412 array( 'de', 'MediaWiki:About/de', 'zh', 'zh-tw', 'zh-cn' ),
413 array( 'zh-cn', 'MediaWiki:About/zh-cn', 'zh', 'zh-tw', 'zh-cn' ),
414 array( 'zh-tw', 'MediaWiki:About/zh-tw', 'zh', 'zh-tw', 'zh-cn' ),
415 array( 'en', 'MediaWiki:Common.js', 'zh', 'zh-tw', 'zh-cn' ),
416 array( 'en', 'MediaWiki:Common.css', 'zh', 'zh-tw', 'zh-cn' ),
417 array( 'en', 'User:JohnDoe/Common.js', 'zh', 'zh-tw', 'zh-cn' ),
418 array( 'en', 'User:JohnDoe/Monobook.css', 'zh', 'zh-tw', 'zh-cn' ),
420 array( 'zh-tw', 'Special:NewPages', 'es', 'zh-tw', 'zh-cn' ),
421 array( 'zh-tw', 'Special:NewPages', 'zh', 'zh-tw', 'zh-cn' ),
427 * @dataProvider provideBaseTitleCases
428 * @covers Title::getBaseText
430 public function testGetBaseText( $title, $expected, $msg = '' ) {
431 $title = Title
::newFromText( $title );
432 $this->assertEquals( $expected,
433 $title->getBaseText(),
438 public static function provideBaseTitleCases() {
440 # Title, expected base, optional message
441 array( 'User:John_Doe/subOne/subTwo', 'John Doe/subOne' ),
442 array( 'User:Foo/Bar/Baz', 'Foo/Bar' ),
447 * @dataProvider provideRootTitleCases
448 * @covers Title::getRootText
450 public function testGetRootText( $title, $expected, $msg = '' ) {
451 $title = Title
::newFromText( $title );
452 $this->assertEquals( $expected,
453 $title->getRootText(),
458 public static function provideRootTitleCases() {
460 # Title, expected base, optional message
461 array( 'User:John_Doe/subOne/subTwo', 'John Doe' ),
462 array( 'User:Foo/Bar/Baz', 'Foo' ),
467 * @todo Handle $wgNamespacesWithSubpages cases
468 * @dataProvider provideSubpageTitleCases
469 * @covers Title::getSubpageText
471 public function testGetSubpageText( $title, $expected, $msg = '' ) {
472 $title = Title
::newFromText( $title );
473 $this->assertEquals( $expected,
474 $title->getSubpageText(),
479 public static function provideSubpageTitleCases() {
481 # Title, expected base, optional message
482 array( 'User:John_Doe/subOne/subTwo', 'subTwo' ),
483 array( 'User:John_Doe/subOne', 'subOne' ),