2010-12-14 16:26:35 +00:00
|
|
|
<?php
|
|
|
|
|
|
2012-09-05 16:54:15 +00:00
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @group Database
|
|
|
|
|
* ^--- needed for language cache stuff
|
|
|
|
|
*/
|
2010-12-28 18:17:16 +00:00
|
|
|
class TitleTest extends MediaWikiTestCase {
|
2010-12-14 16:26:35 +00:00
|
|
|
|
2012-10-08 10:56:20 +00:00
|
|
|
protected function setUp() {
|
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
|
|
$this->setMwGlobals( array(
|
|
|
|
|
'wgLanguageCode' => 'en',
|
|
|
|
|
'wgContLang' => Language::factory( 'en' ),
|
|
|
|
|
// User language
|
|
|
|
|
'wgLang' => Language::factory( 'en' ),
|
|
|
|
|
'wgAllowUserJs' => false,
|
|
|
|
|
'wgDefaultLanguageVariant' => false,
|
|
|
|
|
) );
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-14 16:26:35 +00:00
|
|
|
function testLegalChars() {
|
|
|
|
|
$titlechars = Title::legalChars();
|
|
|
|
|
|
|
|
|
|
foreach ( range( 1, 255 ) as $num ) {
|
|
|
|
|
$chr = chr( $num );
|
|
|
|
|
if ( strpos( "#[]{}<>|", $chr ) !== false || preg_match( "/[\\x00-\\x1f\\x7f]/", $chr ) ) {
|
|
|
|
|
$this->assertFalse( (bool)preg_match( "/[$titlechars]/", $chr ), "chr($num) = $chr is not a valid titlechar" );
|
|
|
|
|
} else {
|
|
|
|
|
$this->assertTrue( (bool)preg_match( "/[$titlechars]/", $chr ), "chr($num) = $chr is a valid titlechar" );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-05-13 11:41:17 +00:00
|
|
|
|
2011-09-22 21:35:24 +00:00
|
|
|
/**
|
2012-10-08 10:56:20 +00:00
|
|
|
* @dataProvider provideBug31100
|
2011-09-22 21:35:24 +00:00
|
|
|
*/
|
|
|
|
|
function testBug31100FixSpecialName( $text, $expectedParam ) {
|
|
|
|
|
$title = Title::newFromText( $text );
|
|
|
|
|
$fixed = $title->fixSpecialName();
|
|
|
|
|
$stuff = explode( '/', $fixed->getDbKey(), 2 );
|
|
|
|
|
if ( count( $stuff ) == 2 ) {
|
|
|
|
|
$par = $stuff[1];
|
|
|
|
|
} else {
|
|
|
|
|
$par = null;
|
|
|
|
|
}
|
|
|
|
|
$this->assertEquals( $expectedParam, $par, "Bug 31100 regression check: Title->fixSpecialName() should preserve parameter" );
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-08 10:56:20 +00:00
|
|
|
public static function provideBug31100() {
|
2011-09-22 21:35:24 +00:00
|
|
|
return array(
|
|
|
|
|
array( 'Special:Version', null ),
|
|
|
|
|
array( 'Special:Version/', '' ),
|
|
|
|
|
array( 'Special:Version/param', 'param' ),
|
|
|
|
|
);
|
|
|
|
|
}
|
2011-09-29 18:35:34 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Auth-less test of Title::isValidMoveOperation
|
|
|
|
|
*
|
2011-10-02 21:07:17 +00:00
|
|
|
* @group Database
|
2011-09-29 18:35:34 +00:00
|
|
|
* @param string $source
|
|
|
|
|
* @param string $target
|
2012-02-01 20:53:38 +00:00
|
|
|
* @param array|string|true $expected Required error
|
2012-10-08 10:56:20 +00:00
|
|
|
* @dataProvider provideTestIsValidMoveOperation
|
2011-09-29 18:35:34 +00:00
|
|
|
*/
|
|
|
|
|
function testIsValidMoveOperation( $source, $target, $expected ) {
|
|
|
|
|
$title = Title::newFromText( $source );
|
|
|
|
|
$nt = Title::newFromText( $target );
|
|
|
|
|
$errors = $title->isValidMoveOperation( $nt, false );
|
|
|
|
|
if ( $expected === true ) {
|
|
|
|
|
$this->assertTrue( $errors );
|
|
|
|
|
} else {
|
|
|
|
|
$errors = $this->flattenErrorsArray( $errors );
|
|
|
|
|
foreach ( (array)$expected as $error ) {
|
|
|
|
|
$this->assertContains( $error, $errors );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function flattenErrorsArray( $errors ) {
|
|
|
|
|
$result = array();
|
|
|
|
|
foreach ( $errors as $error ) {
|
|
|
|
|
$result[] = $error[0];
|
|
|
|
|
}
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-08 10:56:20 +00:00
|
|
|
public static function provideTestIsValidMoveOperation() {
|
2011-09-29 18:35:34 +00:00
|
|
|
return array(
|
|
|
|
|
array( 'Test', 'Test', 'selfmove' ),
|
|
|
|
|
array( 'File:Test.jpg', 'Page', 'imagenocrossnamespace' )
|
|
|
|
|
);
|
|
|
|
|
}
|
2012-08-30 20:04:42 +00:00
|
|
|
|
2012-06-02 18:07:46 +00:00
|
|
|
/**
|
|
|
|
|
* @dataProvider provideCasesForGetpageviewlanguage
|
|
|
|
|
*/
|
2012-10-08 10:56:20 +00:00
|
|
|
function testGetpageviewlanguage( $expected, $titleText, $contLang, $lang, $variant, $msg = '' ) {
|
|
|
|
|
global $wgLanguageCode, $wgContLang, $wgLang, $wgDefaultLanguageVariant, $wgAllowUserJs;
|
|
|
|
|
|
|
|
|
|
// Setup environnement for this test
|
2012-06-02 18:07:46 +00:00
|
|
|
$wgLanguageCode = $contLang;
|
2012-10-08 10:56:20 +00:00
|
|
|
$wgContLang = Language::factory( $contLang );
|
|
|
|
|
$wgLang = Language::factory( $lang );
|
2012-06-02 18:07:46 +00:00
|
|
|
$wgDefaultLanguageVariant = $variant;
|
2012-10-08 10:56:20 +00:00
|
|
|
$wgAllowUserJs = true;
|
2012-06-02 18:07:46 +00:00
|
|
|
|
|
|
|
|
$title = Title::newFromText( $titleText );
|
|
|
|
|
$this->assertInstanceOf( 'Title', $title,
|
|
|
|
|
"Test must be passed a valid title text, you gave '$titleText'"
|
|
|
|
|
);
|
|
|
|
|
$this->assertEquals( $expected,
|
|
|
|
|
$title->getPageViewLanguage()->getCode(),
|
|
|
|
|
$msg
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function provideCasesForGetpageviewlanguage() {
|
|
|
|
|
# Format:
|
|
|
|
|
# - expected
|
|
|
|
|
# - Title name
|
|
|
|
|
# - wgContLang (expected in most case)
|
|
|
|
|
# - wgLang (on some specific pages)
|
|
|
|
|
# - wgDefaultLanguageVariant
|
|
|
|
|
# - Optional message
|
|
|
|
|
return array(
|
|
|
|
|
array( 'fr', 'Main_page', 'fr', 'fr', false ),
|
|
|
|
|
array( 'es', 'Main_page', 'es', 'zh-tw', false ),
|
|
|
|
|
array( 'zh', 'Main_page', 'zh', 'zh-tw', false ),
|
|
|
|
|
|
|
|
|
|
array( 'es', 'Main_page', 'es', 'zh-tw', 'zh-cn' ),
|
|
|
|
|
array( 'es', 'MediaWiki:About', 'es', 'zh-tw', 'zh-cn' ),
|
|
|
|
|
array( 'es', 'MediaWiki:About/', 'es', 'zh-tw', 'zh-cn' ),
|
|
|
|
|
array( 'de', 'MediaWiki:About/de', 'es', 'zh-tw', 'zh-cn' ),
|
|
|
|
|
array( 'en', 'MediaWiki:Common.js', 'es', 'zh-tw', 'zh-cn' ),
|
|
|
|
|
array( 'en', 'MediaWiki:Common.css', 'es', 'zh-tw', 'zh-cn' ),
|
|
|
|
|
array( 'en', 'User:JohnDoe/Common.js', 'es', 'zh-tw', 'zh-cn' ),
|
|
|
|
|
array( 'en', 'User:JohnDoe/Monobook.css', 'es', 'zh-tw', 'zh-cn' ),
|
|
|
|
|
|
|
|
|
|
array( 'zh-cn', 'Main_page', 'zh', 'zh-tw', 'zh-cn' ),
|
|
|
|
|
array( 'zh', 'MediaWiki:About', 'zh', 'zh-tw', 'zh-cn' ),
|
2012-09-04 15:14:20 +00:00
|
|
|
array( 'zh', 'MediaWiki:About/', 'zh', 'zh-tw', 'zh-cn' ),
|
2012-06-02 18:07:46 +00:00
|
|
|
array( 'de', 'MediaWiki:About/de', 'zh', 'zh-tw', 'zh-cn' ),
|
|
|
|
|
array( 'zh-cn', 'MediaWiki:About/zh-cn', 'zh', 'zh-tw', 'zh-cn' ),
|
|
|
|
|
array( 'zh-tw', 'MediaWiki:About/zh-tw', 'zh', 'zh-tw', 'zh-cn' ),
|
|
|
|
|
array( 'en', 'MediaWiki:Common.js', 'zh', 'zh-tw', 'zh-cn' ),
|
|
|
|
|
array( 'en', 'MediaWiki:Common.css', 'zh', 'zh-tw', 'zh-cn' ),
|
|
|
|
|
array( 'en', 'User:JohnDoe/Common.js', 'zh', 'zh-tw', 'zh-cn' ),
|
|
|
|
|
array( 'en', 'User:JohnDoe/Monobook.css', 'zh', 'zh-tw', 'zh-cn' ),
|
|
|
|
|
|
|
|
|
|
array( 'zh-tw', 'Special:NewPages', 'es', 'zh-tw', 'zh-cn' ),
|
|
|
|
|
array( 'zh-tw', 'Special:NewPages', 'zh', 'zh-tw', 'zh-cn' ),
|
|
|
|
|
|
|
|
|
|
);
|
|
|
|
|
}
|
2012-08-30 20:04:42 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideBaseTitleCases
|
|
|
|
|
*/
|
|
|
|
|
function testExtractingBaseTextFromTitle( $title, $expected, $msg='' ) {
|
|
|
|
|
$title = Title::newFromText( $title );
|
|
|
|
|
$this->assertEquals( $expected,
|
|
|
|
|
$title->getBaseText(),
|
|
|
|
|
$msg
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function provideBaseTitleCases() {
|
|
|
|
|
return array(
|
|
|
|
|
# Title, expected base, optional message
|
|
|
|
|
array('User:John_Doe/subOne/subTwo', 'John Doe/subOne' ),
|
|
|
|
|
array('User:Foo/Bar/Baz', 'Foo/Bar' ),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideRootTitleCases
|
|
|
|
|
*/
|
|
|
|
|
function testExtractingRootTextFromTitle( $title, $expected, $msg='' ) {
|
|
|
|
|
$title = Title::newFromText( $title );
|
|
|
|
|
$this->assertEquals( $expected,
|
|
|
|
|
$title->getRootText(),
|
|
|
|
|
$msg
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-08 10:56:20 +00:00
|
|
|
public static function provideRootTitleCases() {
|
2012-08-30 20:04:42 +00:00
|
|
|
return array(
|
|
|
|
|
# Title, expected base, optional message
|
|
|
|
|
array('User:John_Doe/subOne/subTwo', 'John Doe' ),
|
|
|
|
|
array('User:Foo/Bar/Baz', 'Foo' ),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @todo Handle $wgNamespacesWithSubpages cases
|
|
|
|
|
* @dataProvider provideSubpageTitleCases
|
|
|
|
|
*/
|
|
|
|
|
function testExtractingSubpageTextFromTitle( $title, $expected, $msg='' ) {
|
|
|
|
|
$title = Title::newFromText( $title );
|
|
|
|
|
$this->assertEquals( $expected,
|
|
|
|
|
$title->getSubpageText(),
|
|
|
|
|
$msg
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function provideSubpageTitleCases() {
|
|
|
|
|
return array(
|
|
|
|
|
# Title, expected base, optional message
|
|
|
|
|
array('User:John_Doe/subOne/subTwo', 'subTwo' ),
|
|
|
|
|
array('User:John_Doe/subOne', 'subOne' ),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|