6 * ^--- needed for language cache stuff
8 class TitleTest
extends MediaWikiTestCase
{
9 protected function setUp() {
12 $this->setMwGlobals( array(
13 'wgLanguageCode' => 'en',
14 'wgContLang' => Language
::factory( 'en' ),
16 'wgLang' => Language
::factory( 'en' ),
17 'wgAllowUserJs' => false,
18 'wgDefaultLanguageVariant' => false,
22 function testLegalChars() {
23 $titlechars = Title
::legalChars();
25 foreach ( range( 1, 255 ) as $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" );
30 $this->assertTrue( (bool)preg_match( "/[$titlechars]/", $chr ), "chr($num) = $chr is a valid titlechar" );
36 * @dataProvider provideBug31100
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 ) {
47 $this->assertEquals( $expectedParam, $par, "Bug 31100 regression check: Title->fixSpecialName() should preserve parameter" );
50 public static function provideBug31100() {
52 array( 'Special:Version', null ),
53 array( 'Special:Version/', '' ),
54 array( 'Special:Version/param', 'param' ),
59 * Auth-less test of Title::isValidMoveOperation
62 * @param string $source
63 * @param string $target
64 * @param array|string|true $expected Required error
65 * @dataProvider provideTestIsValidMoveOperation
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 );
74 $errors = $this->flattenErrorsArray( $errors );
75 foreach ( (array)$expected as $error ) {
76 $this->assertContains( $error, $errors );
82 * Provides test parameter values for testIsValidMoveOperation()
84 function dataTestIsValidMoveOperation() {
86 array( 'Test', 'Test', 'selfmove' ),
87 array( 'File:Test.jpg', 'Page', 'imagenocrossnamespace' )
92 * Auth-less test of Title::userCan
94 * @param array $whitelistRegexp
95 * @param string $source
96 * @param string $action
97 * @param array|string|true $expected Required error
99 * @covers Title::checkReadPermissions
100 * @dataProvider dataWgWhitelistReadRegexp
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
107 if ( is_string( $whitelistRegexp ) ) {
108 $whitelistRegexp = array( $whitelistRegexp );
111 $title = Title
::newFromDBkey( $source );
113 global $wgGroupPermissions;
114 $oldPermissions = $wgGroupPermissions;
115 // Disallow all so we can ensure our regex works
116 $wgGroupPermissions = array();
117 $wgGroupPermissions['*']['read'] = false;
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' );
126 global $wgWhitelistReadRegexp;
127 $oldWhitelistRegexp = $wgWhitelistReadRegexp;
128 $wgWhitelistReadRegexp = $whitelistRegexp;
130 // Just use $wgUser which in test is a user object for '127.0.0.1'
132 // Invalidate user rights cache to take in account $wgGroupPermissions
134 $wgUser->clearInstanceCache();
135 $errors = $title->userCan( $action, $wgUser );
138 $wgGroupPermissions = $oldPermissions;
139 $wgWhitelistRead = $oldWhitelist;
140 $wgWhitelistReadRegexp = $oldWhitelistRegexp;
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." );
149 $errors = $this->flattenErrorsArray( $errors );
150 foreach ( (array)$expected as $error ) {
151 $this->assertContains( $error, $errors );
157 * Provides test parameter values for testWgWhitelistReadRegexp()
159 function dataWgWhitelistReadRegexp() {
164 // Everything, if this doesn't work, we're really in trouble
165 array( '/.*/', 'Main_Page', 'read', $ALLOWED ),
166 array( '/.*/', 'Main_Page', 'edit', $DISALLOWED ),
168 // We validate against the title name, not the db key
169 array( '/^Main_Page$/', 'Main_Page', 'read', $DISALLOWED ),
171 array( '/^Main/', 'Main_Page', 'read', $ALLOWED ),
172 array( '/^Main.*/', 'Main_Page', 'read', $ALLOWED ),
174 array( '/Mic\sCheck/', 'Mic Check', 'read', $ALLOWED ),
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 ),
181 array( '/MiC ChEcK/', 'mic check', 'read', $DISALLOWED ),
182 array( '/MiC ChEcK/i', 'mic check', 'read', $ALLOWED ),
184 // From DefaultSettings.php:
185 array( "@^UsEr.*@i", 'User is banned', 'read', $ALLOWED ),
186 array( "@^UsEr.*@i", 'User:John Doe', 'read', $ALLOWED ),
189 array( '/^Special:NewPages$/', 'Special:NewPages', 'read', $ALLOWED ),
190 array( null, 'Special:Newpages', 'read', $DISALLOWED ),
195 function flattenErrorsArray( $errors ) {
197 foreach ( $errors as $error ) {
198 $result[] = $error[0];
204 public static function provideTestIsValidMoveOperation() {
206 array( 'Test', 'Test', 'selfmove' ),
207 array( 'File:Test.jpg', 'Page', 'imagenocrossnamespace' )
212 * @dataProvider provideCasesForGetpageviewlanguage
214 function testGetpageviewlanguage( $expected, $titleText, $contLang, $lang, $variant, $msg = '' ) {
215 global $wgLanguageCode, $wgContLang, $wgLang, $wgDefaultLanguageVariant, $wgAllowUserJs;
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;
224 $title = Title
::newFromText( $titleText );
225 $this->assertInstanceOf( 'Title', $title,
226 "Test must be passed a valid title text, you gave '$titleText'"
228 $this->assertEquals( $expected,
229 $title->getPageViewLanguage()->getCode(),
234 public static function provideCasesForGetpageviewlanguage() {
238 # - wgContLang (expected in most case)
239 # - wgLang (on some specific pages)
240 # - wgDefaultLanguageVariant
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 ),
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' ),
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' ),
267 array( 'zh-tw', 'Special:NewPages', 'es', 'zh-tw', 'zh-cn' ),
268 array( 'zh-tw', 'Special:NewPages', 'zh', 'zh-tw', 'zh-cn' ),
274 * @dataProvider provideBaseTitleCases
276 function testExtractingBaseTextFromTitle( $title, $expected, $msg = '' ) {
277 $title = Title
::newFromText( $title );
278 $this->assertEquals( $expected,
279 $title->getBaseText(),
284 public static function provideBaseTitleCases() {
286 # Title, expected base, optional message
287 array( 'User:John_Doe/subOne/subTwo', 'John Doe/subOne' ),
288 array( 'User:Foo/Bar/Baz', 'Foo/Bar' ),
293 * @dataProvider provideRootTitleCases
295 function testExtractingRootTextFromTitle( $title, $expected, $msg = '' ) {
296 $title = Title
::newFromText( $title );
297 $this->assertEquals( $expected,
298 $title->getRootText(),
303 public static function provideRootTitleCases() {
305 # Title, expected base, optional message
306 array( 'User:John_Doe/subOne/subTwo', 'John Doe' ),
307 array( 'User:Foo/Bar/Baz', 'Foo' ),
312 * @todo Handle $wgNamespacesWithSubpages cases
313 * @dataProvider provideSubpageTitleCases
315 function testExtractingSubpageTextFromTitle( $title, $expected, $msg = '' ) {
316 $title = Title
::newFromText( $title );
317 $this->assertEquals( $expected,
318 $title->getSubpageText(),
323 public static function provideSubpageTitleCases() {
325 # Title, expected base, optional message
326 array( 'User:John_Doe/subOne/subTwo', 'subTwo' ),
327 array( 'User:John_Doe/subOne', 'subOne' ),