Merge "Fixed dependencies for jquery.collapsibleTabs"
[lhc/web/wiklou.git] / tests / phpunit / includes / TitleTest.php
1 <?php
2
3 /**
4 *
5 * @group Database
6 * ^--- needed for language cache stuff
7 */
8 class TitleTest extends MediaWikiTestCase {
9
10 protected function setUp() {
11 parent::setUp();
12
13 $this->setMwGlobals( array(
14 'wgLanguageCode' => 'en',
15 'wgContLang' => Language::factory( 'en' ),
16 // User language
17 'wgLang' => Language::factory( 'en' ),
18 'wgAllowUserJs' => false,
19 'wgDefaultLanguageVariant' => false,
20 ) );
21 }
22
23 function testLegalChars() {
24 $titlechars = Title::legalChars();
25
26 foreach ( range( 1, 255 ) as $num ) {
27 $chr = chr( $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" );
30 } else {
31 $this->assertTrue( (bool)preg_match( "/[$titlechars]/", $chr ), "chr($num) = $chr is a valid titlechar" );
32 }
33 }
34 }
35
36 /**
37 * @dataProvider provideBug31100
38 */
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 ) {
44 $par = $stuff[1];
45 } else {
46 $par = null;
47 }
48 $this->assertEquals( $expectedParam, $par, "Bug 31100 regression check: Title->fixSpecialName() should preserve parameter" );
49 }
50
51 public static function provideBug31100() {
52 return array(
53 array( 'Special:Version', null ),
54 array( 'Special:Version/', '' ),
55 array( 'Special:Version/param', 'param' ),
56 );
57 }
58
59 /**
60 * Auth-less test of Title::isValidMoveOperation
61 *
62 * @group Database
63 * @param string $source
64 * @param string $target
65 * @param array|string|true $expected Required error
66 * @dataProvider provideTestIsValidMoveOperation
67 */
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 );
74 } else {
75 $errors = $this->flattenErrorsArray( $errors );
76 foreach ( (array)$expected as $error ) {
77 $this->assertContains( $error, $errors );
78 }
79 }
80 }
81
82 function flattenErrorsArray( $errors ) {
83 $result = array();
84 foreach ( $errors as $error ) {
85 $result[] = $error[0];
86 }
87 return $result;
88 }
89
90 public static function provideTestIsValidMoveOperation() {
91 return array(
92 array( 'Test', 'Test', 'selfmove' ),
93 array( 'File:Test.jpg', 'Page', 'imagenocrossnamespace' )
94 );
95 }
96
97 /**
98 * @dataProvider provideCasesForGetpageviewlanguage
99 */
100 function testGetpageviewlanguage( $expected, $titleText, $contLang, $lang, $variant, $msg = '' ) {
101 global $wgLanguageCode, $wgContLang, $wgLang, $wgDefaultLanguageVariant, $wgAllowUserJs;
102
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;
109
110 $title = Title::newFromText( $titleText );
111 $this->assertInstanceOf( 'Title', $title,
112 "Test must be passed a valid title text, you gave '$titleText'"
113 );
114 $this->assertEquals( $expected,
115 $title->getPageViewLanguage()->getCode(),
116 $msg
117 );
118 }
119
120 function provideCasesForGetpageviewlanguage() {
121 # Format:
122 # - expected
123 # - Title name
124 # - wgContLang (expected in most case)
125 # - wgLang (on some specific pages)
126 # - wgDefaultLanguageVariant
127 # - Optional message
128 return array(
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 ),
132
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' ),
141
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' ),
152
153 array( 'zh-tw', 'Special:NewPages', 'es', 'zh-tw', 'zh-cn' ),
154 array( 'zh-tw', 'Special:NewPages', 'zh', 'zh-tw', 'zh-cn' ),
155
156 );
157 }
158
159 /**
160 * @dataProvider provideBaseTitleCases
161 */
162 function testExtractingBaseTextFromTitle( $title, $expected, $msg='' ) {
163 $title = Title::newFromText( $title );
164 $this->assertEquals( $expected,
165 $title->getBaseText(),
166 $msg
167 );
168 }
169
170 function provideBaseTitleCases() {
171 return array(
172 # Title, expected base, optional message
173 array('User:John_Doe/subOne/subTwo', 'John Doe/subOne' ),
174 array('User:Foo/Bar/Baz', 'Foo/Bar' ),
175 );
176 }
177
178 /**
179 * @dataProvider provideRootTitleCases
180 */
181 function testExtractingRootTextFromTitle( $title, $expected, $msg='' ) {
182 $title = Title::newFromText( $title );
183 $this->assertEquals( $expected,
184 $title->getRootText(),
185 $msg
186 );
187 }
188
189 public static function provideRootTitleCases() {
190 return array(
191 # Title, expected base, optional message
192 array('User:John_Doe/subOne/subTwo', 'John Doe' ),
193 array('User:Foo/Bar/Baz', 'Foo' ),
194 );
195 }
196
197 /**
198 * @todo Handle $wgNamespacesWithSubpages cases
199 * @dataProvider provideSubpageTitleCases
200 */
201 function testExtractingSubpageTextFromTitle( $title, $expected, $msg='' ) {
202 $title = Title::newFromText( $title );
203 $this->assertEquals( $expected,
204 $title->getSubpageText(),
205 $msg
206 );
207 }
208
209 function provideSubpageTitleCases() {
210 return array(
211 # Title, expected base, optional message
212 array('User:John_Doe/subOne/subTwo', 'subTwo' ),
213 array('User:John_Doe/subOne', 'subOne' ),
214 );
215 }
216
217 }