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 );
83 * Provides test parameter values for testIsValidMoveOperation()
85 function dataTestIsValidMoveOperation() {
87 array( 'Test', 'Test', 'selfmove' ),
88 array( 'File:Test.jpg', 'Page', 'imagenocrossnamespace' )
93 * Auth-less test of Title::userCan
95 * @param array $whitelistRegexp
96 * @param string $source
97 * @param string $action
98 * @param array|string|true $expected Required error
100 * @covers Title::checkReadPermission
101 * @dataProvider dataWgWhitelistReadRegexp
103 function testWgWhitelistReadRegexp($whitelistRegexp, $source, $action, $expected) {
105 // $wgWhitelistReadRegexp must be an array. Since the provided test cases
106 // usually have only one regex, it is more concise to write the lonely regex
107 // as a string. Thus we cast to an array() to honor $wgWhitelistReadRegexp
109 if( is_string( $whitelistRegexp ) ) {
110 $whitelistRegexp = array( $whitelistRegexp );
113 $title = Title
::newFromDBkey( $source );
115 global $wgGroupPermissions;
116 $oldPermissions = $wgGroupPermissions;
117 // Disallow all so we can ensure our regex works
118 $wgGroupPermissions = array();
119 $wgGroupPermissions['*']['read'] = false;
121 global $wgWhitelistRead;
122 $oldWhitelist = $wgWhitelistRead;
123 // Undo any LocalSettings explicite whitelists so they won't cause a
124 // failing test to succeed. Set it to some random non sense just
125 // to make sure we properly test Title::checkReadPermissions()
126 $wgWhitelistRead = array( 'some random non sense title' );
128 global $wgWhitelistReadRegexp;
129 $oldWhitelistRegexp = $wgWhitelistReadRegexp;
130 $wgWhitelistReadRegexp = $whitelistRegexp ;
132 // Just use $wgUser which in test is a user object for '127.0.0.1'
134 // Invalidate user rights cache to take in account $wgGroupPermissions
136 $wgUser->clearInstanceCache();
137 $errors = $title->userCan( $action, $wgUser );
140 $wgGroupPermissions = $oldPermissions;
141 $wgWhitelistRead = $oldWhitelist;
142 $wgWhitelistReadRegexp = $oldWhitelistRegexp;
144 if( is_bool( $expected ) ) {
145 # Forge the assertion message depending on the assertion expectation
146 $allowableness = $expected
147 ?
" should be allowed"
148 : " should NOT be allowed"
150 $this->assertEquals( $expected, $errors, "User action '$action' on [[$source]] $allowableness." );
152 $errors = $this->flattenErrorsArray( $errors );
153 foreach ( (array)$expected as $error ) {
154 $this->assertContains( $error, $errors );
160 * Provides test parameter values for testWgWhitelistReadRegexp()
162 function dataWgWhitelistReadRegexp() {
167 // Everything, if this doesn't work, we're really in trouble
168 array( '/.*/', 'Main_Page', 'read', $ALLOWED ),
169 array( '/.*/', 'Main_Page', 'edit', $DISALLOWED ),
171 // We validate against the title name, not the db key
172 array( '/^Main_Page$/', 'Main_Page', 'read', $DISALLOWED ),
174 array( '/^Main/', 'Main_Page', 'read', $ALLOWED ),
175 array( '/^Main.*/', 'Main_Page', 'read', $ALLOWED ),
177 array( '/Mic\sCheck/', 'Mic Check', 'read', $ALLOWED ),
179 // ...without unicode modifier
180 array( '/Unicode Test . Yes/', 'Unicode Test Ñ Yes', 'read', $DISALLOWED ),
181 // ...with unicode modifier
182 array( '/Unicode Test . Yes/u', 'Unicode Test Ñ Yes', 'read', $ALLOWED ),
184 array( '/MiC ChEcK/', 'mic check', 'read', $DISALLOWED ),
185 array( '/MiC ChEcK/i', 'mic check', 'read', $ALLOWED ),
187 // From DefaultSettings.php:
188 array( "@^UsEr.*@i", 'User is banned', 'read', $ALLOWED ),
189 array( "@^UsEr.*@i", 'User:John Doe', 'read', $ALLOWED ),
192 array( '/^Special:NewPages$/', 'Special:NewPages', 'read', $ALLOWED ),
193 array( null, 'Special:Newpages', 'read', $DISALLOWED ),
198 function flattenErrorsArray( $errors ) {
200 foreach ( $errors as $error ) {
201 $result[] = $error[0];
206 public static function provideTestIsValidMoveOperation() {
208 array( 'Test', 'Test', 'selfmove' ),
209 array( 'File:Test.jpg', 'Page', 'imagenocrossnamespace' )
214 * @dataProvider provideCasesForGetpageviewlanguage
216 function testGetpageviewlanguage( $expected, $titleText, $contLang, $lang, $variant, $msg = '' ) {
217 global $wgLanguageCode, $wgContLang, $wgLang, $wgDefaultLanguageVariant, $wgAllowUserJs;
219 // Setup environnement for this test
220 $wgLanguageCode = $contLang;
221 $wgContLang = Language
::factory( $contLang );
222 $wgLang = Language
::factory( $lang );
223 $wgDefaultLanguageVariant = $variant;
224 $wgAllowUserJs = true;
226 $title = Title
::newFromText( $titleText );
227 $this->assertInstanceOf( 'Title', $title,
228 "Test must be passed a valid title text, you gave '$titleText'"
230 $this->assertEquals( $expected,
231 $title->getPageViewLanguage()->getCode(),
236 function provideCasesForGetpageviewlanguage() {
240 # - wgContLang (expected in most case)
241 # - wgLang (on some specific pages)
242 # - wgDefaultLanguageVariant
245 array( 'fr', 'Help:I_need_somebody', 'fr', 'fr', false ),
246 array( 'es', 'Help:I_need_somebody', 'es', 'zh-tw', false ),
247 array( 'zh', 'Help:I_need_somebody', 'zh', 'zh-tw', false ),
249 array( 'es', 'Help:I_need_somebody', 'es', 'zh-tw', 'zh-cn' ),
250 array( 'es', 'MediaWiki:About', 'es', 'zh-tw', 'zh-cn' ),
251 array( 'es', 'MediaWiki:About/', 'es', 'zh-tw', 'zh-cn' ),
252 array( 'de', 'MediaWiki:About/de', 'es', 'zh-tw', 'zh-cn' ),
253 array( 'en', 'MediaWiki:Common.js', 'es', 'zh-tw', 'zh-cn' ),
254 array( 'en', 'MediaWiki:Common.css', 'es', 'zh-tw', 'zh-cn' ),
255 array( 'en', 'User:JohnDoe/Common.js', 'es', 'zh-tw', 'zh-cn' ),
256 array( 'en', 'User:JohnDoe/Monobook.css', 'es', 'zh-tw', 'zh-cn' ),
258 array( 'zh-cn', 'Help:I_need_somebody', 'zh', 'zh-tw', 'zh-cn' ),
259 array( 'zh', 'MediaWiki:About', 'zh', 'zh-tw', 'zh-cn' ),
260 array( 'zh', 'MediaWiki:About/', 'zh', 'zh-tw', 'zh-cn' ),
261 array( 'de', 'MediaWiki:About/de', 'zh', 'zh-tw', 'zh-cn' ),
262 array( 'zh-cn', 'MediaWiki:About/zh-cn', 'zh', 'zh-tw', 'zh-cn' ),
263 array( 'zh-tw', 'MediaWiki:About/zh-tw', 'zh', 'zh-tw', 'zh-cn' ),
264 array( 'en', 'MediaWiki:Common.js', 'zh', 'zh-tw', 'zh-cn' ),
265 array( 'en', 'MediaWiki:Common.css', 'zh', 'zh-tw', 'zh-cn' ),
266 array( 'en', 'User:JohnDoe/Common.js', 'zh', 'zh-tw', 'zh-cn' ),
267 array( 'en', 'User:JohnDoe/Monobook.css', 'zh', 'zh-tw', 'zh-cn' ),
269 array( 'zh-tw', 'Special:NewPages', 'es', 'zh-tw', 'zh-cn' ),
270 array( 'zh-tw', 'Special:NewPages', 'zh', 'zh-tw', 'zh-cn' ),
276 * @dataProvider provideBaseTitleCases
278 function testExtractingBaseTextFromTitle( $title, $expected, $msg='' ) {
279 $title = Title
::newFromText( $title );
280 $this->assertEquals( $expected,
281 $title->getBaseText(),
286 function provideBaseTitleCases() {
288 # Title, expected base, optional message
289 array('User:John_Doe/subOne/subTwo', 'John Doe/subOne' ),
290 array('User:Foo/Bar/Baz', 'Foo/Bar' ),
295 * @dataProvider provideRootTitleCases
297 function testExtractingRootTextFromTitle( $title, $expected, $msg='' ) {
298 $title = Title
::newFromText( $title );
299 $this->assertEquals( $expected,
300 $title->getRootText(),
305 public static function provideRootTitleCases() {
307 # Title, expected base, optional message
308 array('User:John_Doe/subOne/subTwo', 'John Doe' ),
309 array('User:Foo/Bar/Baz', 'Foo' ),
314 * @todo Handle $wgNamespacesWithSubpages cases
315 * @dataProvider provideSubpageTitleCases
317 function testExtractingSubpageTextFromTitle( $title, $expected, $msg='' ) {
318 $title = Title
::newFromText( $title );
319 $this->assertEquals( $expected,
320 $title->getSubpageText(),
325 function provideSubpageTitleCases() {
327 # Title, expected base, optional message
328 array('User:John_Doe/subOne/subTwo', 'subTwo' ),
329 array('User:John_Doe/subOne', 'subOne' ),