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
::class )
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' ],
107 // Strip soft hyphen and Unicode directional formatting characters
108 [ NS_MAIN
, "Foo\xC2\xAD\xD8\x9C\xE2\x80\x8E\xE2\x80\x8F\xE2\x80\xAA\xE2\x80\xAB" .
109 "\xE2\x80\xAC\xE2\x80\xAD\xE2\x80\xAE\xE2\x81\xA6\xE2\x81\xA7" .
110 "\xE2\x81\xA8\xE2\x81\xA9bar", '', '', 'en',
111 "Foo\xC2\xAD\xD8\x9C\xE2\x80\x8E\xE2\x80\x8F\xE2\x80\xAA\xE2\x80\xAB" .
112 "\xE2\x80\xAC\xE2\x80\xAD\xE2\x80\xAE\xE2\x81\xA6\xE2\x81\xA7" .
113 "\xE2\x81\xA8\xE2\x81\xA9bar", 'Foobar' ],
118 * @dataProvider provideFormat
120 public function testFormat( $namespace, $text, $fragment, $interwiki, $lang, $expected,
123 if ( $normalized === null ) {
124 $normalized = $expected;
127 $codec = $this->makeCodec( $lang );
128 $actual = $codec->formatTitle( $namespace, $text, $fragment, $interwiki );
130 $this->assertEquals( $expected, $actual, 'formatted' );
133 $parsed = $codec->parseTitle( $actual, NS_MAIN
);
134 $actual2 = $codec->formatTitle(
135 $parsed->getNamespace(),
137 $parsed->getFragment(),
138 $parsed->getInterwiki()
141 $this->assertEquals( $normalized, $actual2, 'normalized after round trip' );
144 public static function provideGetText() {
146 [ NS_MAIN
, 'Foo_Bar', '', 'en', 'Foo Bar' ],
147 [ NS_USER
, 'Hansi_Maier', 'stuff_and_so_on', 'en', 'Hansi Maier' ],
152 * @dataProvider provideGetText
154 public function testGetText( $namespace, $dbkey, $fragment, $lang, $expected ) {
155 $codec = $this->makeCodec( $lang );
156 $title = new TitleValue( $namespace, $dbkey, $fragment );
158 $actual = $codec->getText( $title );
160 $this->assertEquals( $expected, $actual );
163 public static function provideGetPrefixedText() {
165 [ NS_MAIN
, 'Foo_Bar', '', 'en', 'Foo Bar' ],
166 [ NS_USER
, 'Hansi_Maier', 'stuff_and_so_on', 'en', 'User:Hansi Maier' ],
168 // No capitalization or normalization is applied while formatting!
169 [ NS_USER_TALK
, 'hansi__maier', '', 'en', 'User talk:hansi maier' ],
171 // getGenderCache() provides a mock that considers first
172 // names ending in "a" to be female.
173 [ NS_USER
, 'Lisa_Müller', '', 'de', 'Benutzerin:Lisa Müller' ],
174 [ 1000000, 'Invalid_namespace', '', 'en', ':Invalid namespace' ],
179 * @dataProvider provideGetPrefixedText
181 public function testGetPrefixedText( $namespace, $dbkey, $fragment, $lang, $expected ) {
182 $codec = $this->makeCodec( $lang );
183 $title = new TitleValue( $namespace, $dbkey, $fragment );
185 $actual = $codec->getPrefixedText( $title );
187 $this->assertEquals( $expected, $actual );
190 public static function provideGetPrefixedDBkey() {
192 [ NS_MAIN
, 'Foo_Bar', '', '', 'en', 'Foo_Bar' ],
193 [ NS_USER
, 'Hansi_Maier', 'stuff_and_so_on', '', 'en', 'User:Hansi_Maier' ],
195 // No capitalization or normalization is applied while formatting!
196 [ NS_USER_TALK
, 'hansi__maier', '', '', 'en', 'User_talk:hansi__maier' ],
198 // getGenderCache() provides a mock that considers first
199 // names ending in "a" to be female.
200 [ NS_USER
, 'Lisa_Müller', '', '', 'de', 'Benutzerin:Lisa_Müller' ],
202 [ NS_MAIN
, 'Remote_page', '', 'remotetestiw', 'en', 'remotetestiw:Remote_page' ],
204 // non-existent namespace
205 [ 10000000, 'Foobar', '', '', 'en', ':Foobar' ],
210 * @dataProvider provideGetPrefixedDBkey
212 public function testGetPrefixedDBkey( $namespace, $dbkey, $fragment,
213 $interwiki, $lang, $expected
215 $codec = $this->makeCodec( $lang );
216 $title = new TitleValue( $namespace, $dbkey, $fragment, $interwiki );
218 $actual = $codec->getPrefixedDBkey( $title );
220 $this->assertEquals( $expected, $actual );
223 public static function provideGetFullText() {
225 [ NS_MAIN
, 'Foo_Bar', '', 'en', 'Foo Bar' ],
226 [ NS_USER
, 'Hansi_Maier', 'stuff_and_so_on', 'en', 'User:Hansi Maier#stuff and so on' ],
228 // No capitalization or normalization is applied while formatting!
229 [ NS_USER_TALK
, 'hansi__maier', '', 'en', 'User talk:hansi maier' ],
234 * @dataProvider provideGetFullText
236 public function testGetFullText( $namespace, $dbkey, $fragment, $lang, $expected ) {
237 $codec = $this->makeCodec( $lang );
238 $title = new TitleValue( $namespace, $dbkey, $fragment );
240 $actual = $codec->getFullText( $title );
242 $this->assertEquals( $expected, $actual );
245 public static function provideParseTitle() {
246 // TODO: test capitalization and trimming
247 // TODO: test unicode normalization
250 [ ' : Hansi_Maier _ ', NS_MAIN
, 'en',
251 new TitleValue( NS_MAIN
, 'Hansi_Maier', '' ) ],
252 [ 'User:::1', NS_MAIN
, 'de',
253 new TitleValue( NS_USER
, '0:0:0:0:0:0:0:1', '' ) ],
254 [ ' lisa Müller', NS_USER
, 'de',
255 new TitleValue( NS_USER
, 'Lisa_Müller', '' ) ],
256 [ 'benutzerin:lisa Müller#stuff', NS_MAIN
, 'de',
257 new TitleValue( NS_USER
, 'Lisa_Müller', 'stuff' ) ],
259 [ ':Category:Quux', NS_MAIN
, 'en',
260 new TitleValue( NS_CATEGORY
, 'Quux', '' ) ],
261 [ 'Category:Quux', NS_MAIN
, 'en',
262 new TitleValue( NS_CATEGORY
, 'Quux', '' ) ],
263 [ 'Category:Quux', NS_CATEGORY
, 'en',
264 new TitleValue( NS_CATEGORY
, 'Quux', '' ) ],
265 [ 'Quux', NS_CATEGORY
, 'en',
266 new TitleValue( NS_CATEGORY
, 'Quux', '' ) ],
267 [ ':Quux', NS_CATEGORY
, 'en',
268 new TitleValue( NS_MAIN
, 'Quux', '' ) ],
270 // getGenderCache() provides a mock that considers first
271 // names ending in "a" to be female.
273 [ 'a b c', NS_MAIN
, 'en',
274 new TitleValue( NS_MAIN
, 'A_b_c' ) ],
275 [ ' a b c ', NS_MAIN
, 'en',
276 new TitleValue( NS_MAIN
, 'A_b_c' ) ],
277 [ ' _ Foo __ Bar_ _', NS_MAIN
, 'en',
278 new TitleValue( NS_MAIN
, 'Foo_Bar' ) ],
280 // NOTE: cases copied from TitleTest::testSecureAndSplit. Keep in sync.
281 [ 'Sandbox', NS_MAIN
, 'en', ],
282 [ 'A "B"', NS_MAIN
, 'en', ],
283 [ 'A \'B\'', NS_MAIN
, 'en', ],
284 [ '.com', NS_MAIN
, 'en', ],
285 [ '~', NS_MAIN
, 'en', ],
286 [ '"', NS_MAIN
, 'en', ],
287 [ '\'', NS_MAIN
, 'en', ],
289 [ 'Talk:Sandbox', NS_MAIN
, 'en',
290 new TitleValue( NS_TALK
, 'Sandbox' ) ],
291 [ 'Talk:Foo:Sandbox', NS_MAIN
, 'en',
292 new TitleValue( NS_TALK
, 'Foo:Sandbox' ) ],
293 [ 'File:Example.svg', NS_MAIN
, 'en',
294 new TitleValue( NS_FILE
, 'Example.svg' ) ],
295 [ 'File_talk:Example.svg', NS_MAIN
, 'en',
296 new TitleValue( NS_FILE_TALK
, 'Example.svg' ) ],
297 [ 'Foo/.../Sandbox', NS_MAIN
, 'en',
299 [ 'Sandbox/...', NS_MAIN
, 'en',
301 [ 'A~~', NS_MAIN
, 'en',
303 // Length is 256 total, but only title part matters
304 [ 'Category:' . str_repeat( 'x', 248 ), NS_MAIN
, 'en',
305 new TitleValue( NS_CATEGORY
,
306 'X' . str_repeat( 'x', 247 ) ) ],
307 [ str_repeat( 'x', 252 ), NS_MAIN
, 'en',
308 'X' . str_repeat( 'x', 251 ) ]
313 * @dataProvider provideParseTitle
315 public function testParseTitle( $text, $ns, $lang, $title = null ) {
316 if ( $title === null ) {
317 $title = str_replace( ' ', '_', trim( $text ) );
320 if ( is_string( $title ) ) {
321 $title = new TitleValue( NS_MAIN
, $title, '' );
324 $codec = $this->makeCodec( $lang );
325 $actual = $codec->parseTitle( $text, $ns );
327 $this->assertEquals( $title, $actual );
330 public static function provideParseTitle_invalid() {
331 // TODO: test unicode errors
340 [ 'Talk:File:Foo.jpg' ],
341 [ 'Talk:localtestiw:Foo' ],
342 [ '::1' ], // only valid in user namespace
343 [ 'User::x' ], // leading ":" in a user name is only valid of IPv6 addresses
345 // NOTE: cases copied from TitleTest::testSecureAndSplit. Keep in sync.
350 // Bad characters forbidden regardless of wgLegalTitleChars
362 // XML/HTML character entity references
363 // Note: Commented out because they are not marked invalid by the PHP test as
364 // Title::newFromText runs Sanitizer::decodeCharReferencesAndNormalize first.
365 // [ 'A é B' ],
367 // [ 'A é B' ],
368 // Subject of NS_TALK does not roundtrip to NS_MAIN
369 [ 'Talk:File:Example.svg' ],
370 // Directory navigation
376 [ 'Foo/../Sandbox' ],
381 [ 'A ~~~~ Signature' ],
382 [ 'A ~~~~~ Timestamp' ],
383 [ str_repeat( 'x', 256 ) ],
384 // Namespace prefix without actual title
392 * @dataProvider provideParseTitle_invalid
394 public function testParseTitle_invalid( $text ) {
395 $this->setExpectedException( MalformedTitleException
::class );
397 $codec = $this->makeCodec( 'en' );
398 $codec->parseTitle( $text, NS_MAIN
);
401 public static function provideGetNamespaceName() {
403 [ NS_MAIN
, 'Foo', 'en', '' ],
404 [ NS_USER
, 'Foo', 'en', 'User' ],
405 [ NS_USER
, 'Hansi Maier', 'de', 'Benutzer' ],
407 // getGenderCache() provides a mock that considers first
408 // names ending in "a" to be female.
409 [ NS_USER
, 'Lisa Müller', 'de', 'Benutzerin' ],
414 * @dataProvider provideGetNamespaceName
416 public function testGetNamespaceName( $namespace, $text, $lang, $expected ) {
417 $codec = $this->makeCodec( $lang );
418 $name = $codec->getNamespaceName( $namespace, $text );
420 $this->assertEquals( $expected, $name );