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