6 * ^--- needed for language cache stuff
8 class TitleTest
extends MediaWikiTestCase
{
10 protected function setUp() {
13 $this->setMwGlobals( array(
14 'wgLanguageCode' => 'en',
15 'wgContLang' => Language
::factory( 'en' ),
17 'wgLang' => Language
::factory( 'en' ),
18 'wgAllowUserJs' => false,
19 'wgDefaultLanguageVariant' => false,
23 function testLegalChars() {
24 $titlechars = Title
::legalChars();
26 foreach ( range( 1, 255 ) as $num ) {
28 if ( strpos( "#[]{}<>|", $chr ) !== false ||
preg_match( "/[\\x00-\\x1f\\x7f]/", $chr ) ) {
29 $this->assertFalse( (bool)preg_match( "/[$titlechars]/", $chr ), "chr($num) = $chr is not a valid titlechar" );
31 $this->assertTrue( (bool)preg_match( "/[$titlechars]/", $chr ), "chr($num) = $chr is a valid titlechar" );
37 * @dataProvider provideBug31100
39 function testBug31100FixSpecialName( $text, $expectedParam ) {
40 $title = Title
::newFromText( $text );
41 $fixed = $title->fixSpecialName();
42 $stuff = explode( '/', $fixed->getDbKey(), 2 );
43 if ( count( $stuff ) == 2 ) {
48 $this->assertEquals( $expectedParam, $par, "Bug 31100 regression check: Title->fixSpecialName() should preserve parameter" );
51 public static function provideBug31100() {
53 array( 'Special:Version', null ),
54 array( 'Special:Version/', '' ),
55 array( 'Special:Version/param', 'param' ),
60 * Auth-less test of Title::isValidMoveOperation
63 * @param string $source
64 * @param string $target
65 * @param array|string|true $expected Required error
66 * @dataProvider provideTestIsValidMoveOperation
68 function testIsValidMoveOperation( $source, $target, $expected ) {
69 $title = Title
::newFromText( $source );
70 $nt = Title
::newFromText( $target );
71 $errors = $title->isValidMoveOperation( $nt, false );
72 if ( $expected === true ) {
73 $this->assertTrue( $errors );
75 $errors = $this->flattenErrorsArray( $errors );
76 foreach ( (array)$expected as $error ) {
77 $this->assertContains( $error, $errors );
82 function flattenErrorsArray( $errors ) {
84 foreach ( $errors as $error ) {
85 $result[] = $error[0];
90 public static function provideTestIsValidMoveOperation() {
92 array( 'Test', 'Test', 'selfmove' ),
93 array( 'File:Test.jpg', 'Page', 'imagenocrossnamespace' )
98 * @dataProvider provideCasesForGetpageviewlanguage
100 function testGetpageviewlanguage( $expected, $titleText, $contLang, $lang, $variant, $msg = '' ) {
101 global $wgLanguageCode, $wgContLang, $wgLang, $wgDefaultLanguageVariant, $wgAllowUserJs;
103 // Setup environnement for this test
104 $wgLanguageCode = $contLang;
105 $wgContLang = Language
::factory( $contLang );
106 $wgLang = Language
::factory( $lang );
107 $wgDefaultLanguageVariant = $variant;
108 $wgAllowUserJs = true;
110 $title = Title
::newFromText( $titleText );
111 $this->assertInstanceOf( 'Title', $title,
112 "Test must be passed a valid title text, you gave '$titleText'"
114 $this->assertEquals( $expected,
115 $title->getPageViewLanguage()->getCode(),
120 function provideCasesForGetpageviewlanguage() {
124 # - wgContLang (expected in most case)
125 # - wgLang (on some specific pages)
126 # - wgDefaultLanguageVariant
129 array( 'fr', 'Main_page', 'fr', 'fr', false ),
130 array( 'es', 'Main_page', 'es', 'zh-tw', false ),
131 array( 'zh', 'Main_page', 'zh', 'zh-tw', false ),
133 array( 'es', 'Main_page', 'es', 'zh-tw', 'zh-cn' ),
134 array( 'es', 'MediaWiki:About', 'es', 'zh-tw', 'zh-cn' ),
135 array( 'es', 'MediaWiki:About/', 'es', 'zh-tw', 'zh-cn' ),
136 array( 'de', 'MediaWiki:About/de', 'es', 'zh-tw', 'zh-cn' ),
137 array( 'en', 'MediaWiki:Common.js', 'es', 'zh-tw', 'zh-cn' ),
138 array( 'en', 'MediaWiki:Common.css', 'es', 'zh-tw', 'zh-cn' ),
139 array( 'en', 'User:JohnDoe/Common.js', 'es', 'zh-tw', 'zh-cn' ),
140 array( 'en', 'User:JohnDoe/Monobook.css', 'es', 'zh-tw', 'zh-cn' ),
142 array( 'zh-cn', 'Main_page', 'zh', 'zh-tw', 'zh-cn' ),
143 array( 'zh', 'MediaWiki:About', 'zh', 'zh-tw', 'zh-cn' ),
144 array( 'zh', 'MediaWiki:About/', 'zh', 'zh-tw', 'zh-cn' ),
145 array( 'de', 'MediaWiki:About/de', 'zh', 'zh-tw', 'zh-cn' ),
146 array( 'zh-cn', 'MediaWiki:About/zh-cn', 'zh', 'zh-tw', 'zh-cn' ),
147 array( 'zh-tw', 'MediaWiki:About/zh-tw', 'zh', 'zh-tw', 'zh-cn' ),
148 array( 'en', 'MediaWiki:Common.js', 'zh', 'zh-tw', 'zh-cn' ),
149 array( 'en', 'MediaWiki:Common.css', 'zh', 'zh-tw', 'zh-cn' ),
150 array( 'en', 'User:JohnDoe/Common.js', 'zh', 'zh-tw', 'zh-cn' ),
151 array( 'en', 'User:JohnDoe/Monobook.css', 'zh', 'zh-tw', 'zh-cn' ),
153 array( 'zh-tw', 'Special:NewPages', 'es', 'zh-tw', 'zh-cn' ),
154 array( 'zh-tw', 'Special:NewPages', 'zh', 'zh-tw', 'zh-cn' ),
160 * @dataProvider provideBaseTitleCases
162 function testExtractingBaseTextFromTitle( $title, $expected, $msg='' ) {
163 $title = Title
::newFromText( $title );
164 $this->assertEquals( $expected,
165 $title->getBaseText(),
170 function provideBaseTitleCases() {
172 # Title, expected base, optional message
173 array('User:John_Doe/subOne/subTwo', 'John Doe/subOne' ),
174 array('User:Foo/Bar/Baz', 'Foo/Bar' ),
179 * @dataProvider provideRootTitleCases
181 function testExtractingRootTextFromTitle( $title, $expected, $msg='' ) {
182 $title = Title
::newFromText( $title );
183 $this->assertEquals( $expected,
184 $title->getRootText(),
189 public static function provideRootTitleCases() {
191 # Title, expected base, optional message
192 array('User:John_Doe/subOne/subTwo', 'John Doe' ),
193 array('User:Foo/Bar/Baz', 'Foo' ),
198 * @todo Handle $wgNamespacesWithSubpages cases
199 * @dataProvider provideSubpageTitleCases
201 function testExtractingSubpageTextFromTitle( $title, $expected, $msg='' ) {
202 $title = Title
::newFromText( $title );
203 $this->assertEquals( $expected,
204 $title->getSubpageText(),
209 function provideSubpageTitleCases() {
211 # Title, expected base, optional message
212 array('User:John_Doe/subOne/subTwo', 'subTwo' ),
213 array('User:John_Doe/subOne', 'subOne' ),