Merge "Fix behaviour of namespaceSelector to match the documentation"
[lhc/web/wiklou.git] / tests / phpunit / includes / TitleTest.php
1 <?php
2
3 class TitleTest extends MediaWikiTestCase {
4
5 function testLegalChars() {
6 $titlechars = Title::legalChars();
7
8 foreach ( range( 1, 255 ) as $num ) {
9 $chr = chr( $num );
10 if ( strpos( "#[]{}<>|", $chr ) !== false || preg_match( "/[\\x00-\\x1f\\x7f]/", $chr ) ) {
11 $this->assertFalse( (bool)preg_match( "/[$titlechars]/", $chr ), "chr($num) = $chr is not a valid titlechar" );
12 } else {
13 $this->assertTrue( (bool)preg_match( "/[$titlechars]/", $chr ), "chr($num) = $chr is a valid titlechar" );
14 }
15 }
16 }
17
18 /**
19 * @dataProvider dataBug31100
20 */
21 function testBug31100FixSpecialName( $text, $expectedParam ) {
22 $title = Title::newFromText( $text );
23 $fixed = $title->fixSpecialName();
24 $stuff = explode( '/', $fixed->getDbKey(), 2 );
25 if ( count( $stuff ) == 2 ) {
26 $par = $stuff[1];
27 } else {
28 $par = null;
29 }
30 $this->assertEquals( $expectedParam, $par, "Bug 31100 regression check: Title->fixSpecialName() should preserve parameter" );
31 }
32
33 function dataBug31100() {
34 return array(
35 array( 'Special:Version', null ),
36 array( 'Special:Version/', '' ),
37 array( 'Special:Version/param', 'param' ),
38 );
39 }
40
41 /**
42 * Auth-less test of Title::isValidMoveOperation
43 *
44 * @group Database
45 * @param string $source
46 * @param string $target
47 * @param array|string|true $expected Required error
48 * @dataProvider dataTestIsValidMoveOperation
49 */
50 function testIsValidMoveOperation( $source, $target, $expected ) {
51 $title = Title::newFromText( $source );
52 $nt = Title::newFromText( $target );
53 $errors = $title->isValidMoveOperation( $nt, false );
54 if ( $expected === true ) {
55 $this->assertTrue( $errors );
56 } else {
57 $errors = $this->flattenErrorsArray( $errors );
58 foreach ( (array)$expected as $error ) {
59 $this->assertContains( $error, $errors );
60 }
61 }
62 }
63
64 function flattenErrorsArray( $errors ) {
65 $result = array();
66 foreach ( $errors as $error ) {
67 $result[] = $error[0];
68 }
69 return $result;
70 }
71
72 function dataTestIsValidMoveOperation() {
73 return array(
74 array( 'Test', 'Test', 'selfmove' ),
75 array( 'File:Test.jpg', 'Page', 'imagenocrossnamespace' )
76 );
77 }
78
79 /**
80 * @dataProvider provideCasesForGetpageviewlanguage
81 */
82 function testGetpageviewlanguage( $expected, $titleText, $contLang, $lang, $variant, $msg='' ) {
83 // Save globals
84 global $wgContLang, $wgLang, $wgAllowUserJs, $wgLanguageCode, $wgDefaultLanguageVariant;
85 $save['wgContLang'] = $wgContLang;
86 $save['wgLang'] = $wgLang;
87 $save['wgAllowUserJs'] = $wgAllowUserJs;
88 $save['wgLanguageCode'] = $wgLanguageCode;
89 $save['wgDefaultLanguageVariant'] = $wgDefaultLanguageVariant;
90
91 // Setup test environnement:
92 $wgContLang = Language::factory( $contLang );
93 $wgLang = Language::factory( $lang );
94 # To test out .js titles:
95 $wgAllowUserJs = true;
96 $wgLanguageCode = $contLang;
97 $wgDefaultLanguageVariant = $variant;
98
99 $title = Title::newFromText( $titleText );
100 $this->assertInstanceOf( 'Title', $title,
101 "Test must be passed a valid title text, you gave '$titleText'"
102 );
103 $this->assertEquals( $expected,
104 $title->getPageViewLanguage()->getCode(),
105 $msg
106 );
107
108 // Restore globals
109 $wgContLang = $save['wgContLang'];
110 $wgLang = $save['wgLang'];
111 $wgAllowUserJs = $save['wgAllowUserJs'];
112 $wgLanguageCode = $save['wgLanguageCode'];
113 $wgDefaultLanguageVariant = $save['wgDefaultLanguageVariant'];
114 }
115
116 function provideCasesForGetpageviewlanguage() {
117 # Format:
118 # - expected
119 # - Title name
120 # - wgContLang (expected in most case)
121 # - wgLang (on some specific pages)
122 # - wgDefaultLanguageVariant
123 # - Optional message
124 return array(
125 array( 'fr', 'Main_page', 'fr', 'fr', false ),
126 array( 'es', 'Main_page', 'es', 'zh-tw', false ),
127 array( 'zh', 'Main_page', 'zh', 'zh-tw', false ),
128
129 array( 'es', 'Main_page', 'es', 'zh-tw', 'zh-cn' ),
130 array( 'es', 'MediaWiki:About', 'es', 'zh-tw', 'zh-cn' ),
131 array( 'es', 'MediaWiki:About/', 'es', 'zh-tw', 'zh-cn' ),
132 array( 'de', 'MediaWiki:About/de', 'es', 'zh-tw', 'zh-cn' ),
133 array( 'en', 'MediaWiki:Common.js', 'es', 'zh-tw', 'zh-cn' ),
134 array( 'en', 'MediaWiki:Common.css', 'es', 'zh-tw', 'zh-cn' ),
135 array( 'en', 'User:JohnDoe/Common.js', 'es', 'zh-tw', 'zh-cn' ),
136 array( 'en', 'User:JohnDoe/Monobook.css', 'es', 'zh-tw', 'zh-cn' ),
137
138 array( 'zh-cn', 'Main_page', 'zh', 'zh-tw', 'zh-cn' ),
139 array( 'zh', 'MediaWiki:About', 'zh', 'zh-tw', 'zh-cn' ),
140 array( 'zh', 'MediaWiki:About/', 'zh', 'zh-tw', 'zh-cn' ),
141 array( 'de', 'MediaWiki:About/de', 'zh', 'zh-tw', 'zh-cn' ),
142 array( 'zh-cn', 'MediaWiki:About/zh-cn', 'zh', 'zh-tw', 'zh-cn' ),
143 array( 'zh-tw', 'MediaWiki:About/zh-tw', 'zh', 'zh-tw', 'zh-cn' ),
144 array( 'en', 'MediaWiki:Common.js', 'zh', 'zh-tw', 'zh-cn' ),
145 array( 'en', 'MediaWiki:Common.css', 'zh', 'zh-tw', 'zh-cn' ),
146 array( 'en', 'User:JohnDoe/Common.js', 'zh', 'zh-tw', 'zh-cn' ),
147 array( 'en', 'User:JohnDoe/Monobook.css', 'zh', 'zh-tw', 'zh-cn' ),
148
149 array( 'zh-tw', 'Special:NewPages', 'es', 'zh-tw', 'zh-cn' ),
150 array( 'zh-tw', 'Special:NewPages', 'zh', 'zh-tw', 'zh-cn' ),
151
152 );
153 }
154
155 /**
156 * @dataProvider provideBaseTitleCases
157 */
158 function testExtractingBaseTextFromTitle( $title, $expected, $msg='' ) {
159 $title = Title::newFromText( $title );
160 $this->assertEquals( $expected,
161 $title->getBaseText(),
162 $msg
163 );
164 }
165
166 function provideBaseTitleCases() {
167 return array(
168 # Title, expected base, optional message
169 array('User:John_Doe/subOne/subTwo', 'John Doe/subOne' ),
170 array('User:Foo/Bar/Baz', 'Foo/Bar' ),
171 );
172 }
173
174 /**
175 * @dataProvider provideRootTitleCases
176 */
177 function testExtractingRootTextFromTitle( $title, $expected, $msg='' ) {
178 $title = Title::newFromText( $title );
179 $this->assertEquals( $expected,
180 $title->getRootText(),
181 $msg
182 );
183 }
184
185 function provideRootTitleCases() {
186 return array(
187 # Title, expected base, optional message
188 array('User:John_Doe/subOne/subTwo', 'John Doe' ),
189 array('User:Foo/Bar/Baz', 'Foo' ),
190 );
191 }
192
193 /**
194 * @todo Handle $wgNamespacesWithSubpages cases
195 * @dataProvider provideSubpageTitleCases
196 */
197 function testExtractingSubpageTextFromTitle( $title, $expected, $msg='' ) {
198 $title = Title::newFromText( $title );
199 $this->assertEquals( $expected,
200 $title->getSubpageText(),
201 $msg
202 );
203 }
204
205 function provideSubpageTitleCases() {
206 return array(
207 # Title, expected base, optional message
208 array('User:John_Doe/subOne/subTwo', 'subTwo' ),
209 array('User:John_Doe/subOne', 'subOne' ),
210 );
211 }
212
213 }