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
20 * @author Daniel Kinzler
24 * @covers MediaWikiTitleCodec
28 * ^--- needed because of global state in
30 class MediaWikiTitleCodecTest
extends MediaWikiTestCase
{
32 public function setUp() {
35 $this->setMwGlobals( array(
36 'wgLanguageCode' => 'en',
37 'wgContLang' => Language
::factory( 'en' ),
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.
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' );
62 * Returns a mock GenderCache that will consider a user "female" if the
63 * first part of the user name ends with "a".
67 private function getGenderCache() {
68 $genderCache = $this->getMockBuilder( 'GenderCache' )
69 ->disableOriginalConstructor()
72 $genderCache->expects( $this->any() )
73 ->method( 'getGenderOf' )
74 ->will( $this->returnCallback( function( $userName ) {
75 return preg_match( '/^[^- _]+a( |_|$)/u', $userName ) ?
'female' : 'male';
81 protected function makeCodec( $lang ) {
82 $gender = $this->getGenderCache();
83 $lang = Language
::factory( $lang );
84 return new MediaWikiTitleCodec( $lang, $gender );
87 public function provideFormat() {
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' ),
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' );
121 $parsed = $codec->parseTitle( $actual, NS_MAIN
);
122 $actual2 = $codec->formatTitle(
123 $parsed->getNamespace(),
125 $parsed->getFragment()
128 $this->assertEquals( $normalized, $actual2, 'normalized after round trip' );
131 public function provideGetText() {
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() {
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() {
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
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',
252 array( 'Sandbox/...', NS_MAIN
, 'en',
254 array( 'A~~', NS_MAIN
, 'en',
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
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.
304 // Bad characters forbidden regardless of wgLegalTitleChars
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 é B' ),
320 //array( 'A é B' ),
321 //array( 'A é B' ),
322 // Subject of NS_TALK does not roundtrip to NS_MAIN
323 array( 'Talk:File:Example.svg' ),
324 // Directory navigation
327 array( './Sandbox' ),
328 array( '../Sandbox' ),
329 array( 'Foo/./Sandbox' ),
330 array( 'Foo/../Sandbox' ),
331 array( 'Sandbox/.' ),
332 array( 'Sandbox/..' ),
334 array( 'A ~~~ Name' ),
335 array( 'A ~~~~ Signature' ),
336 array( 'A ~~~~~ Timestamp' ),
337 array( str_repeat( 'x', 256 ) ),
338 // Namespace prefix without actual title
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() {
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 );