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" );
71 // Bad characters forbidden regardless of wgLegalTitleChars
83 // XML/HTML character entity references
84 // Note: Commented out because they are not marked invalid by the PHP test as
85 // Title::newFromText runs Sanitizer::decodeCharReferencesAndNormalize first.
89 // Subject of NS_TALK does not roundtrip to NS_MAIN
90 'Talk:File:Example.svg',
91 // Directory navigation
104 str_repeat( 'x', 256 ),
105 // Namespace prefix without actual title
111 $this->assertNull( Title
::newFromText( $text ), "Invalid: $text" );
115 public static function provideConvertByteClassToUnicodeClass() {
118 ' %!"$&\'()*,\\-.\\/0-9:;=?@A-Z\\\\^_`a-z~\\x80-\\xFF+',
119 ' %!"$&\'()*,\\-./0-9:;=?@A-Z\\\\\\^_`a-z~+\\u0080-\\uFFFF',
123 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
126 'QWERTY\\x66-\\xFD+',
127 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
135 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
138 'QWERTY\\x66-\\x80+\\x23',
139 'QWERTYf-\\x7F+#\\u0080-\\uFFFF',
142 'QWERTY\\x66-\\x80+\\xD3',
143 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
147 '\\\\\\u0080-\\uFFFF',
151 '\\-\\u0080-\\uFFFF',
155 'QWERTY\\-\\u0080-\\uFFFF',
163 'A-\\x7F\\u0080-\\uFFFF',
166 '\\x66-\\x77QWERTY\\x88-\\x91FXZ',
167 'f-wQWERTYFXZ\\u0080-\\uFFFF',
170 '\\x66-\\x99QWERTY\\xAA-\\xEEFXZ',
171 'f-\\x7FQWERTYFXZ\\u0080-\\uFFFF',
177 * @dataProvider provideConvertByteClassToUnicodeClass
178 * @covers Title::convertByteClassToUnicodeClass
180 public function testConvertByteClassToUnicodeClass( $byteClass, $unicodeClass ) {
181 $this->assertEquals( $unicodeClass, Title
::convertByteClassToUnicodeClass( $byteClass ) );
185 * @dataProvider provideBug31100
186 * @covers Title::fixSpecialName
188 public function testBug31100FixSpecialName( $text, $expectedParam ) {
189 $title = Title
::newFromText( $text );
190 $fixed = $title->fixSpecialName();
191 $stuff = explode( '/', $fixed->getDBkey(), 2 );
192 if ( count( $stuff ) == 2 ) {
197 $this->assertEquals( $expectedParam, $par, "Bug 31100 regression check: Title->fixSpecialName() should preserve parameter" );
200 public static function provideBug31100() {
202 array( 'Special:Version', null ),
203 array( 'Special:Version/', '' ),
204 array( 'Special:Version/param', 'param' ),
209 * Auth-less test of Title::isValidMoveOperation
212 * @param string $source
213 * @param string $target
214 * @param array|string|bool $expected Required error
215 * @dataProvider provideTestIsValidMoveOperation
216 * @covers Title::isValidMoveOperation
218 public function testIsValidMoveOperation( $source, $target, $expected ) {
219 $title = Title
::newFromText( $source );
220 $nt = Title
::newFromText( $target );
221 $errors = $title->isValidMoveOperation( $nt, false );
222 if ( $expected === true ) {
223 $this->assertTrue( $errors );
225 $errors = $this->flattenErrorsArray( $errors );
226 foreach ( (array)$expected as $error ) {
227 $this->assertContains( $error, $errors );
233 * Provides test parameter values for testIsValidMoveOperation()
235 public function dataTestIsValidMoveOperation() {
237 array( 'Test', 'Test', 'selfmove' ),
238 array( 'File:Test.jpg', 'Page', 'imagenocrossnamespace' )
243 * Auth-less test of Title::userCan
245 * @param array $whitelistRegexp
246 * @param string $source
247 * @param string $action
248 * @param array|string|bool $expected Required error
250 * @covers Title::checkReadPermissions
251 * @dataProvider dataWgWhitelistReadRegexp
253 public function testWgWhitelistReadRegexp( $whitelistRegexp, $source, $action, $expected ) {
254 // $wgWhitelistReadRegexp must be an array. Since the provided test cases
255 // usually have only one regex, it is more concise to write the lonely regex
256 // as a string. Thus we cast to an array() to honor $wgWhitelistReadRegexp
258 if ( is_string( $whitelistRegexp ) ) {
259 $whitelistRegexp = array( $whitelistRegexp );
262 $title = Title
::newFromDBkey( $source );
264 global $wgGroupPermissions;
265 $oldPermissions = $wgGroupPermissions;
266 // Disallow all so we can ensure our regex works
267 $wgGroupPermissions = array();
268 $wgGroupPermissions['*']['read'] = false;
270 global $wgWhitelistRead;
271 $oldWhitelist = $wgWhitelistRead;
272 // Undo any LocalSettings explicite whitelists so they won't cause a
273 // failing test to succeed. Set it to some random non sense just
274 // to make sure we properly test Title::checkReadPermissions()
275 $wgWhitelistRead = array( 'some random non sense title' );
277 global $wgWhitelistReadRegexp;
278 $oldWhitelistRegexp = $wgWhitelistReadRegexp;
279 $wgWhitelistReadRegexp = $whitelistRegexp;
281 // Just use $wgUser which in test is a user object for '127.0.0.1'
283 // Invalidate user rights cache to take in account $wgGroupPermissions
285 $wgUser->clearInstanceCache();
286 $errors = $title->userCan( $action, $wgUser );
289 $wgGroupPermissions = $oldPermissions;
290 $wgWhitelistRead = $oldWhitelist;
291 $wgWhitelistReadRegexp = $oldWhitelistRegexp;
293 if ( is_bool( $expected ) ) {
294 # Forge the assertion message depending on the assertion expectation
295 $allowableness = $expected
296 ?
" should be allowed"
297 : " should NOT be allowed";
298 $this->assertEquals( $expected, $errors, "User action '$action' on [[$source]] $allowableness." );
300 $errors = $this->flattenErrorsArray( $errors );
301 foreach ( (array)$expected as $error ) {
302 $this->assertContains( $error, $errors );
308 * Provides test parameter values for testWgWhitelistReadRegexp()
310 public function dataWgWhitelistReadRegexp() {
315 // Everything, if this doesn't work, we're really in trouble
316 array( '/.*/', 'Main_Page', 'read', $ALLOWED ),
317 array( '/.*/', 'Main_Page', 'edit', $DISALLOWED ),
319 // We validate against the title name, not the db key
320 array( '/^Main_Page$/', 'Main_Page', 'read', $DISALLOWED ),
322 array( '/^Main/', 'Main_Page', 'read', $ALLOWED ),
323 array( '/^Main.*/', 'Main_Page', 'read', $ALLOWED ),
325 array( '/Mic\sCheck/', 'Mic Check', 'read', $ALLOWED ),
327 // ...without unicode modifier
328 array( '/Unicode Test . Yes/', 'Unicode Test Ñ Yes', 'read', $DISALLOWED ),
329 // ...with unicode modifier
330 array( '/Unicode Test . Yes/u', 'Unicode Test Ñ Yes', 'read', $ALLOWED ),
332 array( '/MiC ChEcK/', 'mic check', 'read', $DISALLOWED ),
333 array( '/MiC ChEcK/i', 'mic check', 'read', $ALLOWED ),
335 // From DefaultSettings.php:
336 array( "@^UsEr.*@i", 'User is banned', 'read', $ALLOWED ),
337 array( "@^UsEr.*@i", 'User:John Doe', 'read', $ALLOWED ),
340 array( '/^Special:NewPages$/', 'Special:NewPages', 'read', $ALLOWED ),
341 array( null, 'Special:Newpages', 'read', $DISALLOWED ),
346 public function flattenErrorsArray( $errors ) {
348 foreach ( $errors as $error ) {
349 $result[] = $error[0];
355 public static function provideTestIsValidMoveOperation() {
357 array( 'Test', 'Test', 'selfmove' ),
358 array( 'File:Test.jpg', 'Page', 'imagenocrossnamespace' )
363 * @dataProvider provideGetPageViewLanguage
364 * @covers Title::getPageViewLanguage
366 public function testGetPageViewLanguage( $expected, $titleText, $contLang, $lang, $variant, $msg = '' ) {
367 global $wgLanguageCode, $wgContLang, $wgLang, $wgDefaultLanguageVariant, $wgAllowUserJs;
369 // Setup environnement for this test
370 $wgLanguageCode = $contLang;
371 $wgContLang = Language
::factory( $contLang );
372 $wgLang = Language
::factory( $lang );
373 $wgDefaultLanguageVariant = $variant;
374 $wgAllowUserJs = true;
376 $title = Title
::newFromText( $titleText );
377 $this->assertInstanceOf( 'Title', $title,
378 "Test must be passed a valid title text, you gave '$titleText'"
380 $this->assertEquals( $expected,
381 $title->getPageViewLanguage()->getCode(),
386 public static function provideGetPageViewLanguage() {
390 # - wgContLang (expected in most case)
391 # - wgLang (on some specific pages)
392 # - wgDefaultLanguageVariant
395 array( 'fr', 'Help:I_need_somebody', 'fr', 'fr', false ),
396 array( 'es', 'Help:I_need_somebody', 'es', 'zh-tw', false ),
397 array( 'zh', 'Help:I_need_somebody', 'zh', 'zh-tw', false ),
399 array( 'es', 'Help:I_need_somebody', 'es', 'zh-tw', 'zh-cn' ),
400 array( 'es', 'MediaWiki:About', 'es', 'zh-tw', 'zh-cn' ),
401 array( 'es', 'MediaWiki:About/', 'es', 'zh-tw', 'zh-cn' ),
402 array( 'de', 'MediaWiki:About/de', 'es', 'zh-tw', 'zh-cn' ),
403 array( 'en', 'MediaWiki:Common.js', 'es', 'zh-tw', 'zh-cn' ),
404 array( 'en', 'MediaWiki:Common.css', 'es', 'zh-tw', 'zh-cn' ),
405 array( 'en', 'User:JohnDoe/Common.js', 'es', 'zh-tw', 'zh-cn' ),
406 array( 'en', 'User:JohnDoe/Monobook.css', 'es', 'zh-tw', 'zh-cn' ),
408 array( 'zh-cn', 'Help:I_need_somebody', 'zh', 'zh-tw', 'zh-cn' ),
409 array( 'zh', 'MediaWiki:About', 'zh', 'zh-tw', 'zh-cn' ),
410 array( 'zh', 'MediaWiki:About/', 'zh', 'zh-tw', 'zh-cn' ),
411 array( 'de', 'MediaWiki:About/de', 'zh', 'zh-tw', 'zh-cn' ),
412 array( 'zh-cn', 'MediaWiki:About/zh-cn', 'zh', 'zh-tw', 'zh-cn' ),
413 array( 'zh-tw', 'MediaWiki:About/zh-tw', 'zh', 'zh-tw', 'zh-cn' ),
414 array( 'en', 'MediaWiki:Common.js', 'zh', 'zh-tw', 'zh-cn' ),
415 array( 'en', 'MediaWiki:Common.css', 'zh', 'zh-tw', 'zh-cn' ),
416 array( 'en', 'User:JohnDoe/Common.js', 'zh', 'zh-tw', 'zh-cn' ),
417 array( 'en', 'User:JohnDoe/Monobook.css', 'zh', 'zh-tw', 'zh-cn' ),
419 array( 'zh-tw', 'Special:NewPages', 'es', 'zh-tw', 'zh-cn' ),
420 array( 'zh-tw', 'Special:NewPages', 'zh', 'zh-tw', 'zh-cn' ),
426 * @dataProvider provideBaseTitleCases
427 * @covers Title::getBaseText
429 public function testGetBaseText( $title, $expected, $msg = '' ) {
430 $title = Title
::newFromText( $title );
431 $this->assertEquals( $expected,
432 $title->getBaseText(),
437 public static function provideBaseTitleCases() {
439 # Title, expected base, optional message
440 array( 'User:John_Doe/subOne/subTwo', 'John Doe/subOne' ),
441 array( 'User:Foo/Bar/Baz', 'Foo/Bar' ),
446 * @dataProvider provideRootTitleCases
447 * @covers Title::getRootText
449 public function testGetRootText( $title, $expected, $msg = '' ) {
450 $title = Title
::newFromText( $title );
451 $this->assertEquals( $expected,
452 $title->getRootText(),
457 public static function provideRootTitleCases() {
459 # Title, expected base, optional message
460 array( 'User:John_Doe/subOne/subTwo', 'John Doe' ),
461 array( 'User:Foo/Bar/Baz', 'Foo' ),
466 * @todo Handle $wgNamespacesWithSubpages cases
467 * @dataProvider provideSubpageTitleCases
468 * @covers Title::getSubpageText
470 public function testGetSubpageText( $title, $expected, $msg = '' ) {
471 $title = Title
::newFromText( $title );
472 $this->assertEquals( $expected,
473 $title->getSubpageText(),
478 public static function provideSubpageTitleCases() {
480 # Title, expected base, optional message
481 array( 'User:John_Doe/subOne/subTwo', 'subTwo' ),
482 array( 'User:John_Doe/subOne', 'subOne' ),