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
19 * @author Daniel Kinzler
23 * @covers MediaWikiTitleCodec
27 * ^--- needed because of global state in
29 class MediaWikiTitleCodecTest
extends MediaWikiTestCase
{
31 public function setUp() {
34 $this->setMwGlobals( [
35 'wgAllowUserJs' => false,
36 'wgDefaultLanguageVariant' => false,
37 'wgMetaNamespace' => 'Project',
38 'wgLocalInterwikis' => [ 'localtestiw' ],
39 'wgCapitalLinks' => true,
41 // NOTE: this is why global state is evil.
42 // TODO: refactor access to the interwiki codes so it can be injected.
44 'InterwikiLoadPrefix' => [
45 function ( $prefix, &$data ) {
46 if ( $prefix === 'localtestiw' ) {
47 $data = [ 'iw_url' => 'localtestiw' ];
48 } elseif ( $prefix === 'remotetestiw' ) {
49 $data = [ 'iw_url' => 'remotetestiw' ];
56 $this->setUserLang( 'en' );
57 $this->setContentLang( 'en' );
61 * Returns a mock GenderCache that will consider a user "female" if the
62 * first part of the user name ends with "a".
66 private function getGenderCache() {
67 $genderCache = $this->getMockBuilder( 'GenderCache' )
68 ->disableOriginalConstructor()
71 $genderCache->expects( $this->any() )
72 ->method( 'getGenderOf' )
73 ->will( $this->returnCallback( function ( $userName ) {
74 return preg_match( '/^[^- _]+a( |_|$)/u', $userName ) ?
'female' : 'male';
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 );
88 public static function provideFormat() {
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' ],
99 'User talk:hansi maier',
100 'User talk:Hansi maier'
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' ],
111 * @dataProvider provideFormat
113 public function testFormat( $namespace, $text, $fragment, $interwiki, $lang, $expected,
116 if ( $normalized === null ) {
117 $normalized = $expected;
120 $codec = $this->makeCodec( $lang );
121 $actual = $codec->formatTitle( $namespace, $text, $fragment, $interwiki );
123 $this->assertEquals( $expected, $actual, 'formatted' );
126 $parsed = $codec->parseTitle( $actual, NS_MAIN
);
127 $actual2 = $codec->formatTitle(
128 $parsed->getNamespace(),
130 $parsed->getFragment(),
131 $parsed->getInterwiki()
134 $this->assertEquals( $normalized, $actual2, 'normalized after round trip' );
137 public static function provideGetText() {
139 [ NS_MAIN
, 'Foo_Bar', '', 'en', 'Foo Bar' ],
140 [ NS_USER
, 'Hansi_Maier', 'stuff_and_so_on', 'en', 'Hansi Maier' ],
145 * @dataProvider provideGetText
147 public function testGetText( $namespace, $dbkey, $fragment, $lang, $expected ) {
148 $codec = $this->makeCodec( $lang );
149 $title = new TitleValue( $namespace, $dbkey, $fragment );
151 $actual = $codec->getText( $title );
153 $this->assertEquals( $expected, $actual );
156 public static function provideGetPrefixedText() {
158 [ NS_MAIN
, 'Foo_Bar', '', 'en', 'Foo Bar' ],
159 [ NS_USER
, 'Hansi_Maier', 'stuff_and_so_on', 'en', 'User:Hansi Maier' ],
161 // No capitalization or normalization is applied while formatting!
162 [ NS_USER_TALK
, 'hansi__maier', '', 'en', 'User talk:hansi maier' ],
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' ],
171 * @dataProvider provideGetPrefixedText
173 public function testGetPrefixedText( $namespace, $dbkey, $fragment, $lang, $expected ) {
174 $codec = $this->makeCodec( $lang );
175 $title = new TitleValue( $namespace, $dbkey, $fragment );
177 $actual = $codec->getPrefixedText( $title );
179 $this->assertEquals( $expected, $actual );
182 public static function provideGetPrefixedDBkey() {
184 [ NS_MAIN
, 'Foo_Bar', '', '', 'en', 'Foo_Bar' ],
185 [ NS_USER
, 'Hansi_Maier', 'stuff_and_so_on', '', 'en', 'User:Hansi_Maier' ],
187 // No capitalization or normalization is applied while formatting!
188 [ NS_USER_TALK
, 'hansi__maier', '', '', 'en', 'User_talk:hansi__maier' ],
190 // getGenderCache() provides a mock that considers first
191 // names ending in "a" to be female.
192 [ NS_USER
, 'Lisa_Müller', '', '', 'de', 'Benutzerin:Lisa_Müller' ],
194 [ NS_MAIN
, 'Remote_page', '', 'remotetestiw', 'en', 'remotetestiw:Remote_page' ]
199 * @dataProvider provideGetPrefixedDBkey
201 public function testGetPrefixedDBkey( $namespace, $dbkey, $fragment,
202 $interwiki, $lang, $expected
204 $codec = $this->makeCodec( $lang );
205 $title = new TitleValue( $namespace, $dbkey, $fragment, $interwiki );
207 $actual = $codec->getPrefixedDBkey( $title );
209 $this->assertEquals( $expected, $actual );
212 public static function provideGetFullText() {
214 [ NS_MAIN
, 'Foo_Bar', '', 'en', 'Foo Bar' ],
215 [ NS_USER
, 'Hansi_Maier', 'stuff_and_so_on', 'en', 'User:Hansi Maier#stuff and so on' ],
217 // No capitalization or normalization is applied while formatting!
218 [ NS_USER_TALK
, 'hansi__maier', '', 'en', 'User talk:hansi maier' ],
223 * @dataProvider provideGetFullText
225 public function testGetFullText( $namespace, $dbkey, $fragment, $lang, $expected ) {
226 $codec = $this->makeCodec( $lang );
227 $title = new TitleValue( $namespace, $dbkey, $fragment );
229 $actual = $codec->getFullText( $title );
231 $this->assertEquals( $expected, $actual );
234 public static function provideParseTitle() {
235 // TODO: test capitalization and trimming
236 // TODO: test unicode normalization
239 [ ' : Hansi_Maier _ ', NS_MAIN
, 'en',
240 new TitleValue( NS_MAIN
, 'Hansi_Maier', '' ) ],
241 [ 'User:::1', NS_MAIN
, 'de',
242 new TitleValue( NS_USER
, '0:0:0:0:0:0:0:1', '' ) ],
243 [ ' lisa Müller', NS_USER
, 'de',
244 new TitleValue( NS_USER
, 'Lisa_Müller', '' ) ],
245 [ 'benutzerin:lisa Müller#stuff', NS_MAIN
, 'de',
246 new TitleValue( NS_USER
, 'Lisa_Müller', 'stuff' ) ],
248 [ ':Category:Quux', NS_MAIN
, 'en',
249 new TitleValue( NS_CATEGORY
, 'Quux', '' ) ],
250 [ 'Category:Quux', NS_MAIN
, 'en',
251 new TitleValue( NS_CATEGORY
, 'Quux', '' ) ],
252 [ 'Category:Quux', NS_CATEGORY
, 'en',
253 new TitleValue( NS_CATEGORY
, 'Quux', '' ) ],
254 [ 'Quux', NS_CATEGORY
, 'en',
255 new TitleValue( NS_CATEGORY
, 'Quux', '' ) ],
256 [ ':Quux', NS_CATEGORY
, 'en',
257 new TitleValue( NS_MAIN
, 'Quux', '' ) ],
259 // getGenderCache() provides a mock that considers first
260 // names ending in "a" to be female.
262 [ 'a b c', NS_MAIN
, 'en',
263 new TitleValue( NS_MAIN
, 'A_b_c' ) ],
264 [ ' a b c ', NS_MAIN
, 'en',
265 new TitleValue( NS_MAIN
, 'A_b_c' ) ],
266 [ ' _ Foo __ Bar_ _', NS_MAIN
, 'en',
267 new TitleValue( NS_MAIN
, 'Foo_Bar' ) ],
269 // NOTE: cases copied from TitleTest::testSecureAndSplit. Keep in sync.
270 [ 'Sandbox', NS_MAIN
, 'en', ],
271 [ 'A "B"', NS_MAIN
, 'en', ],
272 [ 'A \'B\'', NS_MAIN
, 'en', ],
273 [ '.com', NS_MAIN
, 'en', ],
274 [ '~', NS_MAIN
, 'en', ],
275 [ '"', NS_MAIN
, 'en', ],
276 [ '\'', NS_MAIN
, 'en', ],
278 [ 'Talk:Sandbox', NS_MAIN
, 'en',
279 new TitleValue( NS_TALK
, 'Sandbox' ) ],
280 [ 'Talk:Foo:Sandbox', NS_MAIN
, 'en',
281 new TitleValue( NS_TALK
, 'Foo:Sandbox' ) ],
282 [ 'File:Example.svg', NS_MAIN
, 'en',
283 new TitleValue( NS_FILE
, 'Example.svg' ) ],
284 [ 'File_talk:Example.svg', NS_MAIN
, 'en',
285 new TitleValue( NS_FILE_TALK
, 'Example.svg' ) ],
286 [ 'Foo/.../Sandbox', NS_MAIN
, 'en',
288 [ 'Sandbox/...', NS_MAIN
, 'en',
290 [ 'A~~', NS_MAIN
, 'en',
292 // Length is 256 total, but only title part matters
293 [ 'Category:' . str_repeat( 'x', 248 ), NS_MAIN
, 'en',
294 new TitleValue( NS_CATEGORY
,
295 'X' . str_repeat( 'x', 247 ) ) ],
296 [ str_repeat( 'x', 252 ), NS_MAIN
, 'en',
297 'X' . str_repeat( 'x', 251 ) ]
302 * @dataProvider provideParseTitle
304 public function testParseTitle( $text, $ns, $lang, $title = null ) {
305 if ( $title === null ) {
306 $title = str_replace( ' ', '_', trim( $text ) );
309 if ( is_string( $title ) ) {
310 $title = new TitleValue( NS_MAIN
, $title, '' );
313 $codec = $this->makeCodec( $lang );
314 $actual = $codec->parseTitle( $text, $ns );
316 $this->assertEquals( $title, $actual );
319 public static function provideParseTitle_invalid() {
320 // TODO: test unicode errors
329 [ 'Talk:File:Foo.jpg' ],
330 [ 'Talk:localtestiw:Foo' ],
331 [ '::1' ], // only valid in user namespace
332 [ 'User::x' ], // leading ":" in a user name is only valid of IPv6 addresses
334 // NOTE: cases copied from TitleTest::testSecureAndSplit. Keep in sync.
339 // Bad characters forbidden regardless of wgLegalTitleChars
351 // XML/HTML character entity references
352 // Note: Commented out because they are not marked invalid by the PHP test as
353 // Title::newFromText runs Sanitizer::decodeCharReferencesAndNormalize first.
354 // array( 'A é B' ),
355 // array( 'A é B' ),
356 // array( 'A é B' ),
357 // Subject of NS_TALK does not roundtrip to NS_MAIN
358 [ 'Talk:File:Example.svg' ],
359 // Directory navigation
365 [ 'Foo/../Sandbox' ],
370 [ 'A ~~~~ Signature' ],
371 [ 'A ~~~~~ Timestamp' ],
372 [ str_repeat( 'x', 256 ) ],
373 // Namespace prefix without actual title
381 * @dataProvider provideParseTitle_invalid
383 public function testParseTitle_invalid( $text ) {
384 $this->setExpectedException( 'MalformedTitleException' );
386 $codec = $this->makeCodec( 'en' );
387 $codec->parseTitle( $text, NS_MAIN
);
390 public static function provideGetNamespaceName() {
392 [ NS_MAIN
, 'Foo', 'en', '' ],
393 [ NS_USER
, 'Foo', 'en', 'User' ],
394 [ NS_USER
, 'Hansi Maier', 'de', 'Benutzer' ],
396 // getGenderCache() provides a mock that considers first
397 // names ending in "a" to be female.
398 [ NS_USER
, 'Lisa Müller', 'de', 'Benutzerin' ],
403 * @dataProvider provideGetNamespaceName
405 public function testGetNamespaceName( $namespace, $text, $lang, $expected ) {
406 $codec = $this->makeCodec( $lang );
407 $name = $codec->getNamespaceName( $namespace, $text );
409 $this->assertEquals( $expected, $name );