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' ],
98 'User talk:hansi maier',
99 'User talk:Hansi maier'
102 // getGenderCache() provides a mock that considers first
103 // names ending in "a" to be female.
104 [ NS_USER
, 'Lisa_Müller', '', 'de', 'Benutzerin:Lisa Müller' ],
109 * @dataProvider provideFormat
111 public function testFormat( $namespace, $text, $fragment, $lang, $expected, $normalized = null ) {
112 if ( $normalized === null ) {
113 $normalized = $expected;
116 $codec = $this->makeCodec( $lang );
117 $actual = $codec->formatTitle( $namespace, $text, $fragment );
119 $this->assertEquals( $expected, $actual, 'formatted' );
122 $parsed = $codec->parseTitle( $actual, NS_MAIN
);
123 $actual2 = $codec->formatTitle(
124 $parsed->getNamespace(),
126 $parsed->getFragment()
129 $this->assertEquals( $normalized, $actual2, 'normalized after round trip' );
132 public static function provideGetText() {
134 [ NS_MAIN
, 'Foo_Bar', '', 'en', 'Foo Bar' ],
135 [ NS_USER
, 'Hansi_Maier', 'stuff_and_so_on', 'en', 'Hansi Maier' ],
140 * @dataProvider provideGetText
142 public function testGetText( $namespace, $dbkey, $fragment, $lang, $expected ) {
143 $codec = $this->makeCodec( $lang );
144 $title = new TitleValue( $namespace, $dbkey, $fragment );
146 $actual = $codec->getText( $title );
148 $this->assertEquals( $expected, $actual );
151 public static function provideGetPrefixedText() {
153 [ NS_MAIN
, 'Foo_Bar', '', 'en', 'Foo Bar' ],
154 [ NS_USER
, 'Hansi_Maier', 'stuff_and_so_on', 'en', 'User:Hansi Maier' ],
156 // No capitalization or normalization is applied while formatting!
157 [ NS_USER_TALK
, 'hansi__maier', '', 'en', 'User talk:hansi maier' ],
159 // getGenderCache() provides a mock that considers first
160 // names ending in "a" to be female.
161 [ NS_USER
, 'Lisa_Müller', '', 'de', 'Benutzerin:Lisa Müller' ],
166 * @dataProvider provideGetPrefixedText
168 public function testGetPrefixedText( $namespace, $dbkey, $fragment, $lang, $expected ) {
169 $codec = $this->makeCodec( $lang );
170 $title = new TitleValue( $namespace, $dbkey, $fragment );
172 $actual = $codec->getPrefixedText( $title );
174 $this->assertEquals( $expected, $actual );
177 public static function provideGetFullText() {
179 [ NS_MAIN
, 'Foo_Bar', '', 'en', 'Foo Bar' ],
180 [ NS_USER
, 'Hansi_Maier', 'stuff_and_so_on', 'en', 'User:Hansi Maier#stuff and so on' ],
182 // No capitalization or normalization is applied while formatting!
183 [ NS_USER_TALK
, 'hansi__maier', '', 'en', 'User talk:hansi maier' ],
188 * @dataProvider provideGetFullText
190 public function testGetFullText( $namespace, $dbkey, $fragment, $lang, $expected ) {
191 $codec = $this->makeCodec( $lang );
192 $title = new TitleValue( $namespace, $dbkey, $fragment );
194 $actual = $codec->getFullText( $title );
196 $this->assertEquals( $expected, $actual );
199 public static function provideParseTitle() {
200 // TODO: test capitalization and trimming
201 // TODO: test unicode normalization
204 [ ' : Hansi_Maier _ ', NS_MAIN
, 'en',
205 new TitleValue( NS_MAIN
, 'Hansi_Maier', '' ) ],
206 [ 'User:::1', NS_MAIN
, 'de',
207 new TitleValue( NS_USER
, '0:0:0:0:0:0:0:1', '' ) ],
208 [ ' lisa Müller', NS_USER
, 'de',
209 new TitleValue( NS_USER
, 'Lisa_Müller', '' ) ],
210 [ 'benutzerin:lisa Müller#stuff', NS_MAIN
, 'de',
211 new TitleValue( NS_USER
, 'Lisa_Müller', 'stuff' ) ],
213 [ ':Category:Quux', NS_MAIN
, 'en',
214 new TitleValue( NS_CATEGORY
, 'Quux', '' ) ],
215 [ 'Category:Quux', NS_MAIN
, 'en',
216 new TitleValue( NS_CATEGORY
, 'Quux', '' ) ],
217 [ 'Category:Quux', NS_CATEGORY
, 'en',
218 new TitleValue( NS_CATEGORY
, 'Quux', '' ) ],
219 [ 'Quux', NS_CATEGORY
, 'en',
220 new TitleValue( NS_CATEGORY
, 'Quux', '' ) ],
221 [ ':Quux', NS_CATEGORY
, 'en',
222 new TitleValue( NS_MAIN
, 'Quux', '' ) ],
224 // getGenderCache() provides a mock that considers first
225 // names ending in "a" to be female.
227 [ 'a b c', NS_MAIN
, 'en',
228 new TitleValue( NS_MAIN
, 'A_b_c' ) ],
229 [ ' a b c ', NS_MAIN
, 'en',
230 new TitleValue( NS_MAIN
, 'A_b_c' ) ],
231 [ ' _ Foo __ Bar_ _', NS_MAIN
, 'en',
232 new TitleValue( NS_MAIN
, 'Foo_Bar' ) ],
234 // NOTE: cases copied from TitleTest::testSecureAndSplit. Keep in sync.
235 [ 'Sandbox', NS_MAIN
, 'en', ],
236 [ 'A "B"', NS_MAIN
, 'en', ],
237 [ 'A \'B\'', NS_MAIN
, 'en', ],
238 [ '.com', NS_MAIN
, 'en', ],
239 [ '~', NS_MAIN
, 'en', ],
240 [ '"', NS_MAIN
, 'en', ],
241 [ '\'', NS_MAIN
, 'en', ],
243 [ 'Talk:Sandbox', NS_MAIN
, 'en',
244 new TitleValue( NS_TALK
, 'Sandbox' ) ],
245 [ 'Talk:Foo:Sandbox', NS_MAIN
, 'en',
246 new TitleValue( NS_TALK
, 'Foo:Sandbox' ) ],
247 [ 'File:Example.svg', NS_MAIN
, 'en',
248 new TitleValue( NS_FILE
, 'Example.svg' ) ],
249 [ 'File_talk:Example.svg', NS_MAIN
, 'en',
250 new TitleValue( NS_FILE_TALK
, 'Example.svg' ) ],
251 [ 'Foo/.../Sandbox', NS_MAIN
, 'en',
253 [ 'Sandbox/...', NS_MAIN
, 'en',
255 [ 'A~~', NS_MAIN
, 'en',
257 // Length is 256 total, but only title part matters
258 [ 'Category:' . str_repeat( 'x', 248 ), NS_MAIN
, 'en',
259 new TitleValue( NS_CATEGORY
,
260 'X' . str_repeat( 'x', 247 ) ) ],
261 [ str_repeat( 'x', 252 ), NS_MAIN
, 'en',
262 'X' . str_repeat( 'x', 251 ) ]
267 * @dataProvider provideParseTitle
269 public function testParseTitle( $text, $ns, $lang, $title = null ) {
270 if ( $title === null ) {
271 $title = str_replace( ' ', '_', trim( $text ) );
274 if ( is_string( $title ) ) {
275 $title = new TitleValue( NS_MAIN
, $title, '' );
278 $codec = $this->makeCodec( $lang );
279 $actual = $codec->parseTitle( $text, $ns );
281 $this->assertEquals( $title, $actual );
284 public static function provideParseTitle_invalid() {
285 // TODO: test unicode errors
294 [ 'Talk:File:Foo.jpg' ],
295 [ 'Talk:localtestiw:Foo' ],
296 [ 'remotetestiw:Foo' ],
297 [ '::1' ], // only valid in user namespace
298 [ 'User::x' ], // leading ":" in a user name is only valid of IPv6 addresses
300 // NOTE: cases copied from TitleTest::testSecureAndSplit. Keep in sync.
305 // Bad characters forbidden regardless of wgLegalTitleChars
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 é B' ),
321 // array( 'A é B' ),
322 // array( 'A é B' ),
323 // Subject of NS_TALK does not roundtrip to NS_MAIN
324 [ 'Talk:File:Example.svg' ],
325 // Directory navigation
331 [ 'Foo/../Sandbox' ],
336 [ 'A ~~~~ Signature' ],
337 [ 'A ~~~~~ Timestamp' ],
338 [ str_repeat( 'x', 256 ) ],
339 // Namespace prefix without actual title
347 * @dataProvider provideParseTitle_invalid
349 public function testParseTitle_invalid( $text ) {
350 $this->setExpectedException( 'MalformedTitleException' );
352 $codec = $this->makeCodec( 'en' );
353 $codec->parseTitle( $text, NS_MAIN
);
356 public static function provideGetNamespaceName() {
358 [ NS_MAIN
, 'Foo', 'en', '' ],
359 [ NS_USER
, 'Foo', 'en', 'User' ],
360 [ NS_USER
, 'Hansi Maier', 'de', 'Benutzer' ],
362 // getGenderCache() provides a mock that considers first
363 // names ending in "a" to be female.
364 [ NS_USER
, 'Lisa Müller', 'de', 'Benutzerin' ],
369 * @dataProvider provideGetNamespaceName
371 public function testGetNamespaceName( $namespace, $text, $lang, $expected ) {
372 $codec = $this->makeCodec( $lang );
373 $name = $codec->getNamespaceName( $namespace, $text );
375 $this->assertEquals( $expected, $name );