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