Merge "Pass through errors creating the main page to the installer."
[lhc/web/wiklou.git] / tests / phpunit / includes / title / MediaWikiTitleCodecTest.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 * @author Daniel Kinzler
20 */
21
22 /**
23 * @covers MediaWikiTitleCodec
24 *
25 * @group Title
26 * @group Database
27 * ^--- needed because of global state in
28 */
29 class MediaWikiTitleCodecTest extends MediaWikiTestCase {
30
31 public function setUp() {
32 parent::setUp();
33
34 $this->setMwGlobals( [
35 'wgAllowUserJs' => false,
36 'wgDefaultLanguageVariant' => false,
37 'wgMetaNamespace' => 'Project',
38 'wgLocalInterwikis' => [ 'localtestiw' ],
39 'wgCapitalLinks' => true,
40
41 // NOTE: this is why global state is evil.
42 // TODO: refactor access to the interwiki codes so it can be injected.
43 'wgHooks' => [
44 'InterwikiLoadPrefix' => [
45 function ( $prefix, &$data ) {
46 if ( $prefix === 'localtestiw' ) {
47 $data = [ 'iw_url' => 'localtestiw' ];
48 } elseif ( $prefix === 'remotetestiw' ) {
49 $data = [ 'iw_url' => 'remotetestiw' ];
50 }
51 return false;
52 }
53 ]
54 ]
55 ] );
56 $this->setUserLang( 'en' );
57 $this->setContentLang( 'en' );
58 }
59
60 /**
61 * Returns a mock GenderCache that will consider a user "female" if the
62 * first part of the user name ends with "a".
63 *
64 * @return GenderCache
65 */
66 private function getGenderCache() {
67 $genderCache = $this->getMockBuilder( 'GenderCache' )
68 ->disableOriginalConstructor()
69 ->getMock();
70
71 $genderCache->expects( $this->any() )
72 ->method( 'getGenderOf' )
73 ->will( $this->returnCallback( function ( $userName ) {
74 return preg_match( '/^[^- _]+a( |_|$)/u', $userName ) ? 'female' : 'male';
75 } ) );
76
77 return $genderCache;
78 }
79
80 protected function makeCodec( $lang ) {
81 $gender = $this->getGenderCache();
82 $lang = Language::factory( $lang );
83 // language object can came from cache, which does not respect test settings
84 $lang->resetNamespaces();
85 return new MediaWikiTitleCodec( $lang, $gender );
86 }
87
88 public static function provideFormat() {
89 return [
90 [ NS_MAIN, 'Foo_Bar', '', '', 'en', 'Foo Bar' ],
91 [ NS_USER, 'Hansi_Maier', 'stuff_and_so_on', '', 'en', 'User:Hansi Maier#stuff and so on' ],
92 [ false, 'Hansi_Maier', '', '', 'en', 'Hansi Maier' ],
93 [
94 NS_USER_TALK,
95 'hansi__maier',
96 '',
97 '',
98 'en',
99 'User talk:hansi maier',
100 'User talk:Hansi maier'
101 ],
102
103 // getGenderCache() provides a mock that considers first
104 // names ending in "a" to be female.
105 [ NS_USER, 'Lisa_Müller', '', '', 'de', 'Benutzerin:Lisa Müller' ],
106 [ NS_MAIN, 'FooBar', '', 'remotetestiw', 'en', 'remotetestiw:FooBar' ],
107 ];
108 }
109
110 /**
111 * @dataProvider provideFormat
112 */
113 public function testFormat( $namespace, $text, $fragment, $interwiki, $lang, $expected,
114 $normalized = null
115 ) {
116 if ( $normalized === null ) {
117 $normalized = $expected;
118 }
119
120 $codec = $this->makeCodec( $lang );
121 $actual = $codec->formatTitle( $namespace, $text, $fragment, $interwiki );
122
123 $this->assertEquals( $expected, $actual, 'formatted' );
124
125 // test round trip
126 $parsed = $codec->parseTitle( $actual, NS_MAIN );
127 $actual2 = $codec->formatTitle(
128 $parsed->getNamespace(),
129 $parsed->getText(),
130 $parsed->getFragment(),
131 $parsed->getInterwiki()
132 );
133
134 $this->assertEquals( $normalized, $actual2, 'normalized after round trip' );
135 }
136
137 public static function provideGetText() {
138 return [
139 [ NS_MAIN, 'Foo_Bar', '', 'en', 'Foo Bar' ],
140 [ NS_USER, 'Hansi_Maier', 'stuff_and_so_on', 'en', 'Hansi Maier' ],
141 ];
142 }
143
144 /**
145 * @dataProvider provideGetText
146 */
147 public function testGetText( $namespace, $dbkey, $fragment, $lang, $expected ) {
148 $codec = $this->makeCodec( $lang );
149 $title = new TitleValue( $namespace, $dbkey, $fragment );
150
151 $actual = $codec->getText( $title );
152
153 $this->assertEquals( $expected, $actual );
154 }
155
156 public static function provideGetPrefixedText() {
157 return [
158 [ NS_MAIN, 'Foo_Bar', '', 'en', 'Foo Bar' ],
159 [ NS_USER, 'Hansi_Maier', 'stuff_and_so_on', 'en', 'User:Hansi Maier' ],
160
161 // No capitalization or normalization is applied while formatting!
162 [ NS_USER_TALK, 'hansi__maier', '', 'en', 'User talk:hansi maier' ],
163
164 // getGenderCache() provides a mock that considers first
165 // names ending in "a" to be female.
166 [ NS_USER, 'Lisa_Müller', '', 'de', 'Benutzerin:Lisa Müller' ],
167 ];
168 }
169
170 /**
171 * @dataProvider provideGetPrefixedText
172 */
173 public function testGetPrefixedText( $namespace, $dbkey, $fragment, $lang, $expected ) {
174 $codec = $this->makeCodec( $lang );
175 $title = new TitleValue( $namespace, $dbkey, $fragment );
176
177 $actual = $codec->getPrefixedText( $title );
178
179 $this->assertEquals( $expected, $actual );
180 }
181
182 public static function provideGetFullText() {
183 return [
184 [ NS_MAIN, 'Foo_Bar', '', 'en', 'Foo Bar' ],
185 [ NS_USER, 'Hansi_Maier', 'stuff_and_so_on', 'en', 'User:Hansi Maier#stuff and so on' ],
186
187 // No capitalization or normalization is applied while formatting!
188 [ NS_USER_TALK, 'hansi__maier', '', 'en', 'User talk:hansi maier' ],
189 ];
190 }
191
192 /**
193 * @dataProvider provideGetFullText
194 */
195 public function testGetFullText( $namespace, $dbkey, $fragment, $lang, $expected ) {
196 $codec = $this->makeCodec( $lang );
197 $title = new TitleValue( $namespace, $dbkey, $fragment );
198
199 $actual = $codec->getFullText( $title );
200
201 $this->assertEquals( $expected, $actual );
202 }
203
204 public static function provideParseTitle() {
205 // TODO: test capitalization and trimming
206 // TODO: test unicode normalization
207
208 return [
209 [ ' : Hansi_Maier _ ', NS_MAIN, 'en',
210 new TitleValue( NS_MAIN, 'Hansi_Maier', '' ) ],
211 [ 'User:::1', NS_MAIN, 'de',
212 new TitleValue( NS_USER, '0:0:0:0:0:0:0:1', '' ) ],
213 [ ' lisa Müller', NS_USER, 'de',
214 new TitleValue( NS_USER, 'Lisa_Müller', '' ) ],
215 [ 'benutzerin:lisa Müller#stuff', NS_MAIN, 'de',
216 new TitleValue( NS_USER, 'Lisa_Müller', 'stuff' ) ],
217
218 [ ':Category:Quux', NS_MAIN, 'en',
219 new TitleValue( NS_CATEGORY, 'Quux', '' ) ],
220 [ 'Category:Quux', NS_MAIN, 'en',
221 new TitleValue( NS_CATEGORY, 'Quux', '' ) ],
222 [ 'Category:Quux', NS_CATEGORY, 'en',
223 new TitleValue( NS_CATEGORY, 'Quux', '' ) ],
224 [ 'Quux', NS_CATEGORY, 'en',
225 new TitleValue( NS_CATEGORY, 'Quux', '' ) ],
226 [ ':Quux', NS_CATEGORY, 'en',
227 new TitleValue( NS_MAIN, 'Quux', '' ) ],
228
229 // getGenderCache() provides a mock that considers first
230 // names ending in "a" to be female.
231
232 [ 'a b c', NS_MAIN, 'en',
233 new TitleValue( NS_MAIN, 'A_b_c' ) ],
234 [ ' a b c ', NS_MAIN, 'en',
235 new TitleValue( NS_MAIN, 'A_b_c' ) ],
236 [ ' _ Foo __ Bar_ _', NS_MAIN, 'en',
237 new TitleValue( NS_MAIN, 'Foo_Bar' ) ],
238
239 // NOTE: cases copied from TitleTest::testSecureAndSplit. Keep in sync.
240 [ 'Sandbox', NS_MAIN, 'en', ],
241 [ 'A "B"', NS_MAIN, 'en', ],
242 [ 'A \'B\'', NS_MAIN, 'en', ],
243 [ '.com', NS_MAIN, 'en', ],
244 [ '~', NS_MAIN, 'en', ],
245 [ '"', NS_MAIN, 'en', ],
246 [ '\'', NS_MAIN, 'en', ],
247
248 [ 'Talk:Sandbox', NS_MAIN, 'en',
249 new TitleValue( NS_TALK, 'Sandbox' ) ],
250 [ 'Talk:Foo:Sandbox', NS_MAIN, 'en',
251 new TitleValue( NS_TALK, 'Foo:Sandbox' ) ],
252 [ 'File:Example.svg', NS_MAIN, 'en',
253 new TitleValue( NS_FILE, 'Example.svg' ) ],
254 [ 'File_talk:Example.svg', NS_MAIN, 'en',
255 new TitleValue( NS_FILE_TALK, 'Example.svg' ) ],
256 [ 'Foo/.../Sandbox', NS_MAIN, 'en',
257 'Foo/.../Sandbox' ],
258 [ 'Sandbox/...', NS_MAIN, 'en',
259 'Sandbox/...' ],
260 [ 'A~~', NS_MAIN, 'en',
261 'A~~' ],
262 // Length is 256 total, but only title part matters
263 [ 'Category:' . str_repeat( 'x', 248 ), NS_MAIN, 'en',
264 new TitleValue( NS_CATEGORY,
265 'X' . str_repeat( 'x', 247 ) ) ],
266 [ str_repeat( 'x', 252 ), NS_MAIN, 'en',
267 'X' . str_repeat( 'x', 251 ) ]
268 ];
269 }
270
271 /**
272 * @dataProvider provideParseTitle
273 */
274 public function testParseTitle( $text, $ns, $lang, $title = null ) {
275 if ( $title === null ) {
276 $title = str_replace( ' ', '_', trim( $text ) );
277 }
278
279 if ( is_string( $title ) ) {
280 $title = new TitleValue( NS_MAIN, $title, '' );
281 }
282
283 $codec = $this->makeCodec( $lang );
284 $actual = $codec->parseTitle( $text, $ns );
285
286 $this->assertEquals( $title, $actual );
287 }
288
289 public static function provideParseTitle_invalid() {
290 // TODO: test unicode errors
291
292 return [
293 [ '#' ],
294 [ '::' ],
295 [ '::xx' ],
296 [ '::##' ],
297 [ ' :: x' ],
298
299 [ 'Talk:File:Foo.jpg' ],
300 [ 'Talk:localtestiw:Foo' ],
301 [ '::1' ], // only valid in user namespace
302 [ 'User::x' ], // leading ":" in a user name is only valid of IPv6 addresses
303
304 // NOTE: cases copied from TitleTest::testSecureAndSplit. Keep in sync.
305 [ '' ],
306 [ ':' ],
307 [ '__ __' ],
308 [ ' __ ' ],
309 // Bad characters forbidden regardless of wgLegalTitleChars
310 [ 'A [ B' ],
311 [ 'A ] B' ],
312 [ 'A { B' ],
313 [ 'A } B' ],
314 [ 'A < B' ],
315 [ 'A > B' ],
316 [ 'A | B' ],
317 // URL encoding
318 [ 'A%20B' ],
319 [ 'A%23B' ],
320 [ 'A%2523B' ],
321 // XML/HTML character entity references
322 // Note: Commented out because they are not marked invalid by the PHP test as
323 // Title::newFromText runs Sanitizer::decodeCharReferencesAndNormalize first.
324 // array( 'A &eacute; B' ),
325 // array( 'A &#233; B' ),
326 // array( 'A &#x00E9; B' ),
327 // Subject of NS_TALK does not roundtrip to NS_MAIN
328 [ 'Talk:File:Example.svg' ],
329 // Directory navigation
330 [ '.' ],
331 [ '..' ],
332 [ './Sandbox' ],
333 [ '../Sandbox' ],
334 [ 'Foo/./Sandbox' ],
335 [ 'Foo/../Sandbox' ],
336 [ 'Sandbox/.' ],
337 [ 'Sandbox/..' ],
338 // Tilde
339 [ 'A ~~~ Name' ],
340 [ 'A ~~~~ Signature' ],
341 [ 'A ~~~~~ Timestamp' ],
342 [ str_repeat( 'x', 256 ) ],
343 // Namespace prefix without actual title
344 [ 'Talk:' ],
345 [ 'Category: ' ],
346 [ 'Category: #bar' ]
347 ];
348 }
349
350 /**
351 * @dataProvider provideParseTitle_invalid
352 */
353 public function testParseTitle_invalid( $text ) {
354 $this->setExpectedException( 'MalformedTitleException' );
355
356 $codec = $this->makeCodec( 'en' );
357 $codec->parseTitle( $text, NS_MAIN );
358 }
359
360 public static function provideGetNamespaceName() {
361 return [
362 [ NS_MAIN, 'Foo', 'en', '' ],
363 [ NS_USER, 'Foo', 'en', 'User' ],
364 [ NS_USER, 'Hansi Maier', 'de', 'Benutzer' ],
365
366 // getGenderCache() provides a mock that considers first
367 // names ending in "a" to be female.
368 [ NS_USER, 'Lisa Müller', 'de', 'Benutzerin' ],
369 ];
370 }
371
372 /**
373 * @dataProvider provideGetNamespaceName
374 */
375 public function testGetNamespaceName( $namespace, $text, $lang, $expected ) {
376 $codec = $this->makeCodec( $lang );
377 $name = $codec->getNamespaceName( $namespace, $text );
378
379 $this->assertEquals( $expected, $name );
380 }
381 }