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( array(
35 'wgLanguageCode' => 'en',
36 'wgContLang' => Language
::factory( 'en' ),
38 'wgLang' => Language
::factory( 'en' ),
39 'wgAllowUserJs' => false,
40 'wgDefaultLanguageVariant' => false,
41 'wgMetaNamespace' => 'Project',
42 'wgLocalInterwikis' => array( 'localtestiw' ),
43 'wgCapitalLinks' => true,
45 // NOTE: this is why global state is evil.
46 // TODO: refactor access to the interwiki codes so it can be injected.
48 'InterwikiLoadPrefix' => array(
49 function ( $prefix, &$data ) {
50 if ( $prefix === 'localtestiw' ) {
51 $data = array( 'iw_url' => 'localtestiw' );
52 } elseif ( $prefix === 'remotetestiw' ) {
53 $data = array( 'iw_url' => 'remotetestiw' );
63 * Returns a mock GenderCache that will consider a user "female" if the
64 * first part of the user name ends with "a".
68 private function getGenderCache() {
69 $genderCache = $this->getMockBuilder( 'GenderCache' )
70 ->disableOriginalConstructor()
73 $genderCache->expects( $this->any() )
74 ->method( 'getGenderOf' )
75 ->will( $this->returnCallback( function ( $userName ) {
76 return preg_match( '/^[^- _]+a( |_|$)/u', $userName ) ?
'female' : 'male';
82 protected function makeCodec( $lang ) {
83 $gender = $this->getGenderCache();
84 $lang = Language
::factory( $lang );
85 // language object can came from cache, which does not respect test settings
86 $lang->resetNamespaces();
87 return new MediaWikiTitleCodec( $lang, $gender );
90 public static function provideFormat() {
92 array( NS_MAIN
, 'Foo_Bar', '', 'en', 'Foo Bar' ),
93 array( NS_USER
, 'Hansi_Maier', 'stuff_and_so_on', 'en', 'User:Hansi Maier#stuff and so on' ),
94 array( false, 'Hansi_Maier', '', 'en', 'Hansi Maier' ),
100 'User talk:hansi maier',
101 'User talk:Hansi maier'
104 // getGenderCache() provides a mock that considers first
105 // names ending in "a" to be female.
106 array( NS_USER
, 'Lisa_Müller', '', 'de', 'Benutzerin:Lisa Müller' ),
111 * @dataProvider provideFormat
113 public function testFormat( $namespace, $text, $fragment, $lang, $expected, $normalized = null ) {
114 if ( $normalized === null ) {
115 $normalized = $expected;
118 $codec = $this->makeCodec( $lang );
119 $actual = $codec->formatTitle( $namespace, $text, $fragment );
121 $this->assertEquals( $expected, $actual, 'formatted' );
124 $parsed = $codec->parseTitle( $actual, NS_MAIN
);
125 $actual2 = $codec->formatTitle(
126 $parsed->getNamespace(),
128 $parsed->getFragment()
131 $this->assertEquals( $normalized, $actual2, 'normalized after round trip' );
134 public static function provideGetText() {
136 array( NS_MAIN
, 'Foo_Bar', '', 'en', 'Foo Bar' ),
137 array( NS_USER
, 'Hansi_Maier', 'stuff_and_so_on', 'en', 'Hansi Maier' ),
142 * @dataProvider provideGetText
144 public function testGetText( $namespace, $dbkey, $fragment, $lang, $expected ) {
145 $codec = $this->makeCodec( $lang );
146 $title = new TitleValue( $namespace, $dbkey, $fragment );
148 $actual = $codec->getText( $title );
150 $this->assertEquals( $expected, $actual );
153 public static function provideGetPrefixedText() {
155 array( NS_MAIN
, 'Foo_Bar', '', 'en', 'Foo Bar' ),
156 array( NS_USER
, 'Hansi_Maier', 'stuff_and_so_on', 'en', 'User:Hansi Maier' ),
158 // No capitalization or normalization is applied while formatting!
159 array( NS_USER_TALK
, 'hansi__maier', '', 'en', 'User talk:hansi maier' ),
161 // getGenderCache() provides a mock that considers first
162 // names ending in "a" to be female.
163 array( NS_USER
, 'Lisa_Müller', '', 'de', 'Benutzerin:Lisa Müller' ),
168 * @dataProvider provideGetPrefixedText
170 public function testGetPrefixedText( $namespace, $dbkey, $fragment, $lang, $expected ) {
171 $codec = $this->makeCodec( $lang );
172 $title = new TitleValue( $namespace, $dbkey, $fragment );
174 $actual = $codec->getPrefixedText( $title );
176 $this->assertEquals( $expected, $actual );
179 public static function provideGetFullText() {
181 array( NS_MAIN
, 'Foo_Bar', '', 'en', 'Foo Bar' ),
182 array( NS_USER
, 'Hansi_Maier', 'stuff_and_so_on', 'en', 'User:Hansi Maier#stuff and so on' ),
184 // No capitalization or normalization is applied while formatting!
185 array( NS_USER_TALK
, 'hansi__maier', '', 'en', 'User talk:hansi maier' ),
190 * @dataProvider provideGetFullText
192 public function testGetFullText( $namespace, $dbkey, $fragment, $lang, $expected ) {
193 $codec = $this->makeCodec( $lang );
194 $title = new TitleValue( $namespace, $dbkey, $fragment );
196 $actual = $codec->getFullText( $title );
198 $this->assertEquals( $expected, $actual );
201 public static function provideParseTitle() {
202 // TODO: test capitalization and trimming
203 // TODO: test unicode normalization
206 array( ' : Hansi_Maier _ ', NS_MAIN
, 'en',
207 new TitleValue( NS_MAIN
, 'Hansi_Maier', '' ) ),
208 array( 'User:::1', NS_MAIN
, 'de',
209 new TitleValue( NS_USER
, '0:0:0:0:0:0:0:1', '' ) ),
210 array( ' lisa Müller', NS_USER
, 'de',
211 new TitleValue( NS_USER
, 'Lisa_Müller', '' ) ),
212 array( 'benutzerin:lisa Müller#stuff', NS_MAIN
, 'de',
213 new TitleValue( NS_USER
, 'Lisa_Müller', 'stuff' ) ),
215 array( ':Category:Quux', NS_MAIN
, 'en',
216 new TitleValue( NS_CATEGORY
, 'Quux', '' ) ),
217 array( 'Category:Quux', NS_MAIN
, 'en',
218 new TitleValue( NS_CATEGORY
, 'Quux', '' ) ),
219 array( 'Category:Quux', NS_CATEGORY
, 'en',
220 new TitleValue( NS_CATEGORY
, 'Quux', '' ) ),
221 array( 'Quux', NS_CATEGORY
, 'en',
222 new TitleValue( NS_CATEGORY
, 'Quux', '' ) ),
223 array( ':Quux', NS_CATEGORY
, 'en',
224 new TitleValue( NS_MAIN
, 'Quux', '' ) ),
226 // getGenderCache() provides a mock that considers first
227 // names ending in "a" to be female.
229 array( 'a b c', NS_MAIN
, 'en',
230 new TitleValue( NS_MAIN
, 'A_b_c' ) ),
231 array( ' a b c ', NS_MAIN
, 'en',
232 new TitleValue( NS_MAIN
, 'A_b_c' ) ),
233 array( ' _ Foo __ Bar_ _', NS_MAIN
, 'en',
234 new TitleValue( NS_MAIN
, 'Foo_Bar' ) ),
236 // NOTE: cases copied from TitleTest::testSecureAndSplit. Keep in sync.
237 array( 'Sandbox', NS_MAIN
, 'en', ),
238 array( 'A "B"', NS_MAIN
, 'en', ),
239 array( 'A \'B\'', NS_MAIN
, 'en', ),
240 array( '.com', NS_MAIN
, 'en', ),
241 array( '~', NS_MAIN
, 'en', ),
242 array( '"', NS_MAIN
, 'en', ),
243 array( '\'', NS_MAIN
, 'en', ),
245 array( 'Talk:Sandbox', NS_MAIN
, 'en',
246 new TitleValue( NS_TALK
, 'Sandbox' ) ),
247 array( 'Talk:Foo:Sandbox', NS_MAIN
, 'en',
248 new TitleValue( NS_TALK
, 'Foo:Sandbox' ) ),
249 array( 'File:Example.svg', NS_MAIN
, 'en',
250 new TitleValue( NS_FILE
, 'Example.svg' ) ),
251 array( 'File_talk:Example.svg', NS_MAIN
, 'en',
252 new TitleValue( NS_FILE_TALK
, 'Example.svg' ) ),
253 array( 'Foo/.../Sandbox', NS_MAIN
, 'en',
255 array( 'Sandbox/...', NS_MAIN
, 'en',
257 array( 'A~~', NS_MAIN
, 'en',
259 // Length is 256 total, but only title part matters
260 array( 'Category:' . str_repeat( 'x', 248 ), NS_MAIN
, 'en',
261 new TitleValue( NS_CATEGORY
,
262 'X' . str_repeat( 'x', 247 ) ) ),
263 array( str_repeat( 'x', 252 ), NS_MAIN
, 'en',
264 'X' . str_repeat( 'x', 251 ) )
269 * @dataProvider provideParseTitle
271 public function testParseTitle( $text, $ns, $lang, $title = null ) {
272 if ( $title === null ) {
273 $title = str_replace( ' ', '_', trim( $text ) );
276 if ( is_string( $title ) ) {
277 $title = new TitleValue( NS_MAIN
, $title, '' );
280 $codec = $this->makeCodec( $lang );
281 $actual = $codec->parseTitle( $text, $ns );
283 $this->assertEquals( $title, $actual );
286 public static function provideParseTitle_invalid() {
287 // TODO: test unicode errors
296 array( 'Talk:File:Foo.jpg' ),
297 array( 'Talk:localtestiw:Foo' ),
298 array( 'remotetestiw:Foo' ),
299 array( '::1' ), // only valid in user namespace
300 array( 'User::x' ), // leading ":" in a user name is only valid of IPv6 addresses
302 // NOTE: cases copied from TitleTest::testSecureAndSplit. Keep in sync.
307 // Bad characters forbidden regardless of wgLegalTitleChars
319 // XML/HTML character entity references
320 // Note: Commented out because they are not marked invalid by the PHP test as
321 // Title::newFromText runs Sanitizer::decodeCharReferencesAndNormalize first.
322 // array( 'A é B' ),
323 // array( 'A é B' ),
324 // array( 'A é B' ),
325 // Subject of NS_TALK does not roundtrip to NS_MAIN
326 array( 'Talk:File:Example.svg' ),
327 // Directory navigation
330 array( './Sandbox' ),
331 array( '../Sandbox' ),
332 array( 'Foo/./Sandbox' ),
333 array( 'Foo/../Sandbox' ),
334 array( 'Sandbox/.' ),
335 array( 'Sandbox/..' ),
337 array( 'A ~~~ Name' ),
338 array( 'A ~~~~ Signature' ),
339 array( 'A ~~~~~ Timestamp' ),
340 array( str_repeat( 'x', 256 ) ),
341 // Namespace prefix without actual title
343 array( 'Category: ' ),
344 array( 'Category: #bar' )
349 * @dataProvider provideParseTitle_invalid
351 public function testParseTitle_invalid( $text ) {
352 $this->setExpectedException( 'MalformedTitleException' );
354 $codec = $this->makeCodec( 'en' );
355 $codec->parseTitle( $text, NS_MAIN
);
358 public static function provideGetNamespaceName() {
360 array( NS_MAIN
, 'Foo', 'en', '' ),
361 array( NS_USER
, 'Foo', 'en', 'User' ),
362 array( NS_USER
, 'Hansi Maier', 'de', 'Benutzer' ),
364 // getGenderCache() provides a mock that considers first
365 // names ending in "a" to be female.
366 array( NS_USER
, 'Lisa Müller', 'de', 'Benutzerin' ),
371 * @dataProvider provideGetNamespaceName
373 public function testGetNamespaceName( $namespace, $text, $lang, $expected ) {
374 $codec = $this->makeCodec( $lang );
375 $name = $codec->getNamespaceName( $namespace, $text );
377 $this->assertEquals( $expected, $name );