Remove messages.inc, rebuildLanguage.php, writeMessagesArray.inc
[mediawiki.git] / tests / phpunit / includes / title / MediaWikiTitleCodecTest.php
blob1e5aca9129447e6ef07f1a5c817e0057e4cfcdde
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.
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.
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
18 * @file
19 * @license GPL 2+
20 * @author Daniel Kinzler
23 /**
24 * @covers MediaWikiTitleCodec
26 * @group Title
27 * @group Database
28 * ^--- needed because of global state in
30 class MediaWikiTitleCodecTest extends MediaWikiTestCase {
32 public function setUp() {
33 parent::setUp();
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' ),
44 // NOTE: this is why global state is evil.
45 // TODO: refactor access to the interwiki codes so it can be injected.
46 'wgHooks' => array(
47 'InterwikiLoadPrefix' => array(
48 function ( $prefix, &$data ) {
49 if ( $prefix === 'localtestiw' ) {
50 $data = array( 'iw_url' => 'localtestiw' );
51 } elseif ( $prefix === 'remotetestiw' ) {
52 $data = array( 'iw_url' => 'remotetestiw' );
54 return false;
58 ) );
61 /**
62 * Returns a mock GenderCache that will consider a user "female" if the
63 * first part of the user name ends with "a".
65 * @return GenderCache
67 private function getGenderCache() {
68 $genderCache = $this->getMockBuilder( 'GenderCache' )
69 ->disableOriginalConstructor()
70 ->getMock();
72 $genderCache->expects( $this->any() )
73 ->method( 'getGenderOf' )
74 ->will( $this->returnCallback( function( $userName ) {
75 return preg_match( '/^[^- _]+a( |_|$)/u', $userName ) ? 'female' : 'male';
76 } ) );
78 return $genderCache;
81 protected function makeCodec( $lang ) {
82 $gender = $this->getGenderCache();
83 $lang = Language::factory( $lang );
84 return new MediaWikiTitleCodec( $lang, $gender );
87 public function provideFormat() {
88 return array(
89 array( NS_MAIN, 'Foo_Bar', '', 'en', 'Foo Bar' ),
90 array( NS_USER, 'Hansi_Maier', 'stuff_and_so_on', 'en', 'User:Hansi Maier#stuff and so on' ),
91 array( false, 'Hansi_Maier', '', 'en', 'Hansi Maier' ),
92 array(
93 NS_USER_TALK,
94 'hansi__maier',
95 '',
96 'en',
97 'User talk:hansi maier',
98 'User talk:Hansi maier'
101 // getGenderCache() provides a mock that considers first
102 // names ending in "a" to be female.
103 array( NS_USER, 'Lisa_Müller', '', 'de', 'Benutzerin:Lisa Müller' ),
108 * @dataProvider provideFormat
110 public function testFormat( $namespace, $text, $fragment, $lang, $expected, $normalized = null ) {
111 if ( $normalized === null ) {
112 $normalized = $expected;
115 $codec = $this->makeCodec( $lang );
116 $actual = $codec->formatTitle( $namespace, $text, $fragment );
118 $this->assertEquals( $expected, $actual, 'formatted' );
120 // test round trip
121 $parsed = $codec->parseTitle( $actual, NS_MAIN );
122 $actual2 = $codec->formatTitle(
123 $parsed->getNamespace(),
124 $parsed->getText(),
125 $parsed->getFragment()
128 $this->assertEquals( $normalized, $actual2, 'normalized after round trip' );
131 public function provideGetText() {
132 return array(
133 array( NS_MAIN, 'Foo_Bar', '', 'en', 'Foo Bar' ),
134 array( NS_USER, 'Hansi_Maier', 'stuff_and_so_on', 'en', 'Hansi Maier' ),
139 * @dataProvider provideGetText
141 public function testGetText( $namespace, $dbkey, $fragment, $lang, $expected ) {
142 $codec = $this->makeCodec( $lang );
143 $title = new TitleValue( $namespace, $dbkey, $fragment );
145 $actual = $codec->getText( $title );
147 $this->assertEquals( $expected, $actual );
150 public function provideGetPrefixedText() {
151 return array(
152 array( NS_MAIN, 'Foo_Bar', '', 'en', 'Foo Bar' ),
153 array( NS_USER, 'Hansi_Maier', 'stuff_and_so_on', 'en', 'User:Hansi Maier' ),
155 // No capitalization or normalization is applied while formatting!
156 array( NS_USER_TALK, 'hansi__maier', '', 'en', 'User talk:hansi maier' ),
158 // getGenderCache() provides a mock that considers first
159 // names ending in "a" to be female.
160 array( NS_USER, 'Lisa_Müller', '', 'de', 'Benutzerin:Lisa Müller' ),
165 * @dataProvider provideGetPrefixedText
167 public function testGetPrefixedText( $namespace, $dbkey, $fragment, $lang, $expected ) {
168 $codec = $this->makeCodec( $lang );
169 $title = new TitleValue( $namespace, $dbkey, $fragment );
171 $actual = $codec->getPrefixedText( $title );
173 $this->assertEquals( $expected, $actual );
176 public function provideGetFullText() {
177 return array(
178 array( NS_MAIN, 'Foo_Bar', '', 'en', 'Foo Bar' ),
179 array( NS_USER, 'Hansi_Maier', 'stuff_and_so_on', 'en', 'User:Hansi Maier#stuff and so on' ),
181 // No capitalization or normalization is applied while formatting!
182 array( NS_USER_TALK, 'hansi__maier', '', 'en', 'User talk:hansi maier' ),
187 * @dataProvider provideGetFullText
189 public function testGetFullText( $namespace, $dbkey, $fragment, $lang, $expected ) {
190 $codec = $this->makeCodec( $lang );
191 $title = new TitleValue( $namespace, $dbkey, $fragment );
193 $actual = $codec->getFullText( $title );
195 $this->assertEquals( $expected, $actual );
198 public function provideParseTitle() {
199 //TODO: test capitalization and trimming
200 //TODO: test unicode normalization
202 return array(
203 array( ' : Hansi_Maier _ ', NS_MAIN, 'en',
204 new TitleValue( NS_MAIN, 'Hansi_Maier', '' ) ),
205 array( 'User:::1', NS_MAIN, 'de',
206 new TitleValue( NS_USER, '0:0:0:0:0:0:0:1', '' ) ),
207 array( ' lisa Müller', NS_USER, 'de',
208 new TitleValue( NS_USER, 'Lisa_Müller', '' ) ),
209 array( 'benutzerin:lisa Müller#stuff', NS_MAIN, 'de',
210 new TitleValue( NS_USER, 'Lisa_Müller', 'stuff' ) ),
212 array( ':Category:Quux', NS_MAIN, 'en',
213 new TitleValue( NS_CATEGORY, 'Quux', '' ) ),
214 array( 'Category:Quux', NS_MAIN, 'en',
215 new TitleValue( NS_CATEGORY, 'Quux', '' ) ),
216 array( 'Category:Quux', NS_CATEGORY, 'en',
217 new TitleValue( NS_CATEGORY, 'Quux', '' ) ),
218 array( 'Quux', NS_CATEGORY, 'en',
219 new TitleValue( NS_CATEGORY, 'Quux', '' ) ),
220 array( ':Quux', NS_CATEGORY, 'en',
221 new TitleValue( NS_MAIN, 'Quux', '' ) ),
223 // getGenderCache() provides a mock that considers first
224 // names ending in "a" to be female.
226 array( 'a b c', NS_MAIN, 'en',
227 new TitleValue( NS_MAIN, 'A_b_c' ) ),
228 array( ' a b c ', NS_MAIN, 'en',
229 new TitleValue( NS_MAIN, 'A_b_c' ) ),
230 array( ' _ Foo __ Bar_ _', NS_MAIN, 'en',
231 new TitleValue( NS_MAIN, 'Foo_Bar' ) ),
233 //NOTE: cases copied from TitleTest::testSecureAndSplit. Keep in sync.
234 array( 'Sandbox', NS_MAIN, 'en', ),
235 array( 'A "B"', NS_MAIN, 'en', ),
236 array( 'A \'B\'', NS_MAIN, 'en', ),
237 array( '.com', NS_MAIN, 'en', ),
238 array( '~', NS_MAIN, 'en', ),
239 array( '"', NS_MAIN, 'en', ),
240 array( '\'', NS_MAIN, 'en', ),
242 array( 'Talk:Sandbox', NS_MAIN, 'en',
243 new TitleValue( NS_TALK, 'Sandbox' ) ),
244 array( 'Talk:Foo:Sandbox', NS_MAIN, 'en',
245 new TitleValue( NS_TALK, 'Foo:Sandbox' ) ),
246 array( 'File:Example.svg', NS_MAIN, 'en',
247 new TitleValue( NS_FILE, 'Example.svg' ) ),
248 array( 'File_talk:Example.svg', NS_MAIN, 'en',
249 new TitleValue( NS_FILE_TALK, 'Example.svg' ) ),
250 array( 'Foo/.../Sandbox', NS_MAIN, 'en',
251 'Foo/.../Sandbox' ),
252 array( 'Sandbox/...', NS_MAIN, 'en',
253 'Sandbox/...' ),
254 array( 'A~~', NS_MAIN, 'en',
255 'A~~' ),
256 // Length is 256 total, but only title part matters
257 array( 'Category:' . str_repeat( 'x', 248 ), NS_MAIN, 'en',
258 new TitleValue( NS_CATEGORY,
259 'X' . str_repeat( 'x', 247 ) ) ),
260 array( str_repeat( 'x', 252 ), NS_MAIN, 'en',
261 'X' . str_repeat( 'x', 251 ) )
266 * @dataProvider provideParseTitle
268 public function testParseTitle( $text, $ns, $lang, $title = null ) {
269 if ( $title === null ) {
270 $title = str_replace( ' ', '_', trim( $text ) );
273 if ( is_string( $title ) ) {
274 $title = new TitleValue( NS_MAIN, $title, '' );
277 $codec = $this->makeCodec( $lang );
278 $actual = $codec->parseTitle( $text, $ns );
280 $this->assertEquals( $title, $actual );
283 public function provideParseTitle_invalid() {
284 //TODO: test unicode errors
286 return array(
287 array( '#' ),
288 array( '::' ),
289 array( '::xx' ),
290 array( '::##' ),
291 array( ' :: x' ),
293 array( 'Talk:File:Foo.jpg' ),
294 array( 'Talk:localtestiw:Foo' ),
295 array( 'remotetestiw:Foo' ),
296 array( '::1' ), // only valid in user namespace
297 array( 'User::x' ), // leading ":" in a user name is only valid of IPv6 addresses
299 //NOTE: cases copied from TitleTest::testSecureAndSplit. Keep in sync.
300 array( '' ),
301 array( ':' ),
302 array( '__ __' ),
303 array( ' __ ' ),
304 // Bad characters forbidden regardless of wgLegalTitleChars
305 array( 'A [ B' ),
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 // URL encoding
313 array( 'A%20B' ),
314 array( 'A%23B' ),
315 array( 'A%2523B' ),
316 // XML/HTML character entity references
317 // Note: Commented out because they are not marked invalid by the PHP test as
318 // Title::newFromText runs Sanitizer::decodeCharReferencesAndNormalize first.
319 //array( 'A &eacute; B' ),
320 //array( 'A &#233; B' ),
321 //array( 'A &#x00E9; B' ),
322 // Subject of NS_TALK does not roundtrip to NS_MAIN
323 array( 'Talk:File:Example.svg' ),
324 // Directory navigation
325 array( '.' ),
326 array( '..' ),
327 array( './Sandbox' ),
328 array( '../Sandbox' ),
329 array( 'Foo/./Sandbox' ),
330 array( 'Foo/../Sandbox' ),
331 array( 'Sandbox/.' ),
332 array( 'Sandbox/..' ),
333 // Tilde
334 array( 'A ~~~ Name' ),
335 array( 'A ~~~~ Signature' ),
336 array( 'A ~~~~~ Timestamp' ),
337 array( str_repeat( 'x', 256 ) ),
338 // Namespace prefix without actual title
339 array( 'Talk:' ),
340 array( 'Category: ' ),
341 array( 'Category: #bar' )
346 * @dataProvider provideParseTitle_invalid
348 public function testParseTitle_invalid( $text ) {
349 $this->setExpectedException( 'MalformedTitleException' );
351 $codec = $this->makeCodec( 'en' );
352 $codec->parseTitle( $text, NS_MAIN );
355 public function provideGetNamespaceName() {
356 return array(
357 array( NS_MAIN, 'Foo', 'en', '' ),
358 array( NS_USER, 'Foo', 'en', 'User' ),
359 array( NS_USER, 'Hansi Maier', 'de', 'Benutzer' ),
361 // getGenderCache() provides a mock that considers first
362 // names ending in "a" to be female.
363 array( NS_USER, 'Lisa Müller', 'de', 'Benutzerin' ),
368 * @dataProvider provideGetNamespaceName
370 * @param int $namespace
371 * @param string $text
372 * @param string $lang
373 * @param string $expected
375 * @internal param \TitleValue $title
377 public function testGetNamespaceName( $namespace, $text, $lang, $expected ) {
378 $codec = $this->makeCodec( $lang );
379 $name = $codec->getNamespaceName( $namespace, $text );
381 $this->assertEquals( $expected, $name );