Merge "Add Special:Login and Special:Logout as aliases."
[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 protected function setUp() {
10 parent::setUp();
11
12 $this->setMwGlobals( array(
13 'wgLanguageCode' => 'en',
14 'wgContLang' => Language::factory( 'en' ),
15 // User language
16 'wgLang' => Language::factory( 'en' ),
17 'wgAllowUserJs' => false,
18 'wgDefaultLanguageVariant' => false,
19 ) );
20 }
21
22 function testLegalChars() {
23 $titlechars = Title::legalChars();
24
25 foreach ( range( 1, 255 ) as $num ) {
26 $chr = chr( $num );
27 if ( strpos( "#[]{}<>|", $chr ) !== false || preg_match( "/[\\x00-\\x1f\\x7f]/", $chr ) ) {
28 $this->assertFalse( (bool)preg_match( "/[$titlechars]/", $chr ), "chr($num) = $chr is not a valid titlechar" );
29 } else {
30 $this->assertTrue( (bool)preg_match( "/[$titlechars]/", $chr ), "chr($num) = $chr is a valid titlechar" );
31 }
32 }
33 }
34
35 /**
36 * @dataProvider provideBug31100
37 */
38 function testBug31100FixSpecialName( $text, $expectedParam ) {
39 $title = Title::newFromText( $text );
40 $fixed = $title->fixSpecialName();
41 $stuff = explode( '/', $fixed->getDBkey(), 2 );
42 if ( count( $stuff ) == 2 ) {
43 $par = $stuff[1];
44 } else {
45 $par = null;
46 }
47 $this->assertEquals( $expectedParam, $par, "Bug 31100 regression check: Title->fixSpecialName() should preserve parameter" );
48 }
49
50 public static function provideBug31100() {
51 return array(
52 array( 'Special:Version', null ),
53 array( 'Special:Version/', '' ),
54 array( 'Special:Version/param', 'param' ),
55 );
56 }
57
58 /**
59 * Auth-less test of Title::isValidMoveOperation
60 *
61 * @group Database
62 * @param string $source
63 * @param string $target
64 * @param array|string|true $expected Required error
65 * @dataProvider provideTestIsValidMoveOperation
66 */
67 function testIsValidMoveOperation( $source, $target, $expected ) {
68 $title = Title::newFromText( $source );
69 $nt = Title::newFromText( $target );
70 $errors = $title->isValidMoveOperation( $nt, false );
71 if ( $expected === true ) {
72 $this->assertTrue( $errors );
73 } else {
74 $errors = $this->flattenErrorsArray( $errors );
75 foreach ( (array)$expected as $error ) {
76 $this->assertContains( $error, $errors );
77 }
78 }
79 }
80
81 /**
82 * Provides test parameter values for testIsValidMoveOperation()
83 */
84 function dataTestIsValidMoveOperation() {
85 return array(
86 array( 'Test', 'Test', 'selfmove' ),
87 array( 'File:Test.jpg', 'Page', 'imagenocrossnamespace' )
88 );
89 }
90
91 /**
92 * Auth-less test of Title::userCan
93 *
94 * @param array $whitelistRegexp
95 * @param string $source
96 * @param string $action
97 * @param array|string|true $expected Required error
98 *
99 * @covers Title::checkReadPermissions
100 * @dataProvider dataWgWhitelistReadRegexp
101 */
102 function testWgWhitelistReadRegexp( $whitelistRegexp, $source, $action, $expected ) {
103 // $wgWhitelistReadRegexp must be an array. Since the provided test cases
104 // usually have only one regex, it is more concise to write the lonely regex
105 // as a string. Thus we cast to an array() to honor $wgWhitelistReadRegexp
106 // type requisite.
107 if ( is_string( $whitelistRegexp ) ) {
108 $whitelistRegexp = array( $whitelistRegexp );
109 }
110
111 $title = Title::newFromDBkey( $source );
112
113 global $wgGroupPermissions;
114 $oldPermissions = $wgGroupPermissions;
115 // Disallow all so we can ensure our regex works
116 $wgGroupPermissions = array();
117 $wgGroupPermissions['*']['read'] = false;
118
119 global $wgWhitelistRead;
120 $oldWhitelist = $wgWhitelistRead;
121 // Undo any LocalSettings explicite whitelists so they won't cause a
122 // failing test to succeed. Set it to some random non sense just
123 // to make sure we properly test Title::checkReadPermissions()
124 $wgWhitelistRead = array( 'some random non sense title' );
125
126 global $wgWhitelistReadRegexp;
127 $oldWhitelistRegexp = $wgWhitelistReadRegexp;
128 $wgWhitelistReadRegexp = $whitelistRegexp;
129
130 // Just use $wgUser which in test is a user object for '127.0.0.1'
131 global $wgUser;
132 // Invalidate user rights cache to take in account $wgGroupPermissions
133 // change above.
134 $wgUser->clearInstanceCache();
135 $errors = $title->userCan( $action, $wgUser );
136
137 // Restore globals
138 $wgGroupPermissions = $oldPermissions;
139 $wgWhitelistRead = $oldWhitelist;
140 $wgWhitelistReadRegexp = $oldWhitelistRegexp;
141
142 if ( is_bool( $expected ) ) {
143 # Forge the assertion message depending on the assertion expectation
144 $allowableness = $expected
145 ? " should be allowed"
146 : " should NOT be allowed";
147 $this->assertEquals( $expected, $errors, "User action '$action' on [[$source]] $allowableness." );
148 } else {
149 $errors = $this->flattenErrorsArray( $errors );
150 foreach ( (array)$expected as $error ) {
151 $this->assertContains( $error, $errors );
152 }
153 }
154 }
155
156 /**
157 * Provides test parameter values for testWgWhitelistReadRegexp()
158 */
159 function dataWgWhitelistReadRegexp() {
160 $ALLOWED = true;
161 $DISALLOWED = false;
162
163 return array(
164 // Everything, if this doesn't work, we're really in trouble
165 array( '/.*/', 'Main_Page', 'read', $ALLOWED ),
166 array( '/.*/', 'Main_Page', 'edit', $DISALLOWED ),
167
168 // We validate against the title name, not the db key
169 array( '/^Main_Page$/', 'Main_Page', 'read', $DISALLOWED ),
170 // Main page
171 array( '/^Main/', 'Main_Page', 'read', $ALLOWED ),
172 array( '/^Main.*/', 'Main_Page', 'read', $ALLOWED ),
173 // With spaces
174 array( '/Mic\sCheck/', 'Mic Check', 'read', $ALLOWED ),
175 // Unicode multibyte
176 // ...without unicode modifier
177 array( '/Unicode Test . Yes/', 'Unicode Test Ñ Yes', 'read', $DISALLOWED ),
178 // ...with unicode modifier
179 array( '/Unicode Test . Yes/u', 'Unicode Test Ñ Yes', 'read', $ALLOWED ),
180 // Case insensitive
181 array( '/MiC ChEcK/', 'mic check', 'read', $DISALLOWED ),
182 array( '/MiC ChEcK/i', 'mic check', 'read', $ALLOWED ),
183
184 // From DefaultSettings.php:
185 array( "@^UsEr.*@i", 'User is banned', 'read', $ALLOWED ),
186 array( "@^UsEr.*@i", 'User:John Doe', 'read', $ALLOWED ),
187
188 // With namespaces:
189 array( '/^Special:NewPages$/', 'Special:NewPages', 'read', $ALLOWED ),
190 array( null, 'Special:Newpages', 'read', $DISALLOWED ),
191
192 );
193 }
194
195 function flattenErrorsArray( $errors ) {
196 $result = array();
197 foreach ( $errors as $error ) {
198 $result[] = $error[0];
199 }
200
201 return $result;
202 }
203
204 public static function provideTestIsValidMoveOperation() {
205 return array(
206 array( 'Test', 'Test', 'selfmove' ),
207 array( 'File:Test.jpg', 'Page', 'imagenocrossnamespace' )
208 );
209 }
210
211 /**
212 * @dataProvider provideCasesForGetpageviewlanguage
213 */
214 function testGetpageviewlanguage( $expected, $titleText, $contLang, $lang, $variant, $msg = '' ) {
215 global $wgLanguageCode, $wgContLang, $wgLang, $wgDefaultLanguageVariant, $wgAllowUserJs;
216
217 // Setup environnement for this test
218 $wgLanguageCode = $contLang;
219 $wgContLang = Language::factory( $contLang );
220 $wgLang = Language::factory( $lang );
221 $wgDefaultLanguageVariant = $variant;
222 $wgAllowUserJs = true;
223
224 $title = Title::newFromText( $titleText );
225 $this->assertInstanceOf( 'Title', $title,
226 "Test must be passed a valid title text, you gave '$titleText'"
227 );
228 $this->assertEquals( $expected,
229 $title->getPageViewLanguage()->getCode(),
230 $msg
231 );
232 }
233
234 public static function provideCasesForGetpageviewlanguage() {
235 # Format:
236 # - expected
237 # - Title name
238 # - wgContLang (expected in most case)
239 # - wgLang (on some specific pages)
240 # - wgDefaultLanguageVariant
241 # - Optional message
242 return array(
243 array( 'fr', 'Help:I_need_somebody', 'fr', 'fr', false ),
244 array( 'es', 'Help:I_need_somebody', 'es', 'zh-tw', false ),
245 array( 'zh', 'Help:I_need_somebody', 'zh', 'zh-tw', false ),
246
247 array( 'es', 'Help:I_need_somebody', 'es', 'zh-tw', 'zh-cn' ),
248 array( 'es', 'MediaWiki:About', 'es', 'zh-tw', 'zh-cn' ),
249 array( 'es', 'MediaWiki:About/', 'es', 'zh-tw', 'zh-cn' ),
250 array( 'de', 'MediaWiki:About/de', 'es', 'zh-tw', 'zh-cn' ),
251 array( 'en', 'MediaWiki:Common.js', 'es', 'zh-tw', 'zh-cn' ),
252 array( 'en', 'MediaWiki:Common.css', 'es', 'zh-tw', 'zh-cn' ),
253 array( 'en', 'User:JohnDoe/Common.js', 'es', 'zh-tw', 'zh-cn' ),
254 array( 'en', 'User:JohnDoe/Monobook.css', 'es', 'zh-tw', 'zh-cn' ),
255
256 array( 'zh-cn', 'Help:I_need_somebody', 'zh', 'zh-tw', 'zh-cn' ),
257 array( 'zh', 'MediaWiki:About', 'zh', 'zh-tw', 'zh-cn' ),
258 array( 'zh', 'MediaWiki:About/', 'zh', 'zh-tw', 'zh-cn' ),
259 array( 'de', 'MediaWiki:About/de', 'zh', 'zh-tw', 'zh-cn' ),
260 array( 'zh-cn', 'MediaWiki:About/zh-cn', 'zh', 'zh-tw', 'zh-cn' ),
261 array( 'zh-tw', 'MediaWiki:About/zh-tw', 'zh', 'zh-tw', 'zh-cn' ),
262 array( 'en', 'MediaWiki:Common.js', 'zh', 'zh-tw', 'zh-cn' ),
263 array( 'en', 'MediaWiki:Common.css', 'zh', 'zh-tw', 'zh-cn' ),
264 array( 'en', 'User:JohnDoe/Common.js', 'zh', 'zh-tw', 'zh-cn' ),
265 array( 'en', 'User:JohnDoe/Monobook.css', 'zh', 'zh-tw', 'zh-cn' ),
266
267 array( 'zh-tw', 'Special:NewPages', 'es', 'zh-tw', 'zh-cn' ),
268 array( 'zh-tw', 'Special:NewPages', 'zh', 'zh-tw', 'zh-cn' ),
269
270 );
271 }
272
273 /**
274 * @dataProvider provideBaseTitleCases
275 */
276 function testExtractingBaseTextFromTitle( $title, $expected, $msg = '' ) {
277 $title = Title::newFromText( $title );
278 $this->assertEquals( $expected,
279 $title->getBaseText(),
280 $msg
281 );
282 }
283
284 public static function provideBaseTitleCases() {
285 return array(
286 # Title, expected base, optional message
287 array( 'User:John_Doe/subOne/subTwo', 'John Doe/subOne' ),
288 array( 'User:Foo/Bar/Baz', 'Foo/Bar' ),
289 );
290 }
291
292 /**
293 * @dataProvider provideRootTitleCases
294 */
295 function testExtractingRootTextFromTitle( $title, $expected, $msg = '' ) {
296 $title = Title::newFromText( $title );
297 $this->assertEquals( $expected,
298 $title->getRootText(),
299 $msg
300 );
301 }
302
303 public static function provideRootTitleCases() {
304 return array(
305 # Title, expected base, optional message
306 array( 'User:John_Doe/subOne/subTwo', 'John Doe' ),
307 array( 'User:Foo/Bar/Baz', 'Foo' ),
308 );
309 }
310
311 /**
312 * @todo Handle $wgNamespacesWithSubpages cases
313 * @dataProvider provideSubpageTitleCases
314 */
315 function testExtractingSubpageTextFromTitle( $title, $expected, $msg = '' ) {
316 $title = Title::newFromText( $title );
317 $this->assertEquals( $expected,
318 $title->getSubpageText(),
319 $msg
320 );
321 }
322
323 public static function provideSubpageTitleCases() {
324 return array(
325 # Title, expected base, optional message
326 array( 'User:John_Doe/subOne/subTwo', 'subTwo' ),
327 array( 'User:John_Doe/subOne', 'subOne' ),
328 );
329 }
330 }