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' ),
92 array( NS_USER_TALK
, 'hansi__maier', '', 'en', 'User talk:hansi maier', 'User talk:Hansi maier' ),
94 // getGenderCache() provides a mock that considers first
95 // names ending in "a" to be female.
96 array( NS_USER
, 'Lisa_Müller', '', 'de', 'Benutzerin:Lisa Müller' ),
101 * @dataProvider provideFormat
103 public function testFormat( $namespace, $text, $fragment, $lang, $expected, $normalized = null ) {
104 if ( $normalized === null ) {
105 $normalized = $expected;
108 $codec = $this->makeCodec( $lang );
109 $actual = $codec->formatTitle( $namespace, $text, $fragment );
111 $this->assertEquals( $expected, $actual, 'formatted' );
114 $parsed = $codec->parseTitle( $actual, NS_MAIN
);
115 $actual2 = $codec->formatTitle( $parsed->getNamespace(), $parsed->getText(), $parsed->getFragment() );
117 $this->assertEquals( $normalized, $actual2, 'normalized after round trip' );
120 public function provideGetText() {
122 array( NS_MAIN
, 'Foo_Bar', '', 'en', 'Foo Bar' ),
123 array( NS_USER
, 'Hansi_Maier', 'stuff_and_so_on', 'en', 'Hansi Maier' ),
128 * @dataProvider provideGetText
130 public function testGetText( $namespace, $dbkey, $fragment, $lang, $expected ) {
131 $codec = $this->makeCodec( $lang );
132 $title = new TitleValue( $namespace, $dbkey, $fragment );
134 $actual = $codec->getText( $title );
136 $this->assertEquals( $expected, $actual );
139 public function provideGetPrefixedText() {
141 array( NS_MAIN
, 'Foo_Bar', '', 'en', 'Foo Bar' ),
142 array( NS_USER
, 'Hansi_Maier', 'stuff_and_so_on', 'en', 'User:Hansi Maier' ),
144 // No capitalization or normalization is applied while formatting!
145 array( NS_USER_TALK
, 'hansi__maier', '', 'en', 'User talk:hansi maier' ),
147 // getGenderCache() provides a mock that considers first
148 // names ending in "a" to be female.
149 array( NS_USER
, 'Lisa_Müller', '', 'de', 'Benutzerin:Lisa Müller' ),
154 * @dataProvider provideGetPrefixedText
156 public function testGetPrefixedText( $namespace, $dbkey, $fragment, $lang, $expected ) {
157 $codec = $this->makeCodec( $lang );
158 $title = new TitleValue( $namespace, $dbkey, $fragment );
160 $actual = $codec->getPrefixedText( $title );
162 $this->assertEquals( $expected, $actual );
165 public function provideGetFullText() {
167 array( NS_MAIN
, 'Foo_Bar', '', 'en', 'Foo Bar' ),
168 array( NS_USER
, 'Hansi_Maier', 'stuff_and_so_on', 'en', 'User:Hansi Maier#stuff and so on' ),
170 // No capitalization or normalization is applied while formatting!
171 array( NS_USER_TALK
, 'hansi__maier', '', 'en', 'User talk:hansi maier' ),
176 * @dataProvider provideGetFullText
178 public function testGetFullText( $namespace, $dbkey, $fragment, $lang, $expected ) {
179 $codec = $this->makeCodec( $lang );
180 $title = new TitleValue( $namespace, $dbkey, $fragment );
182 $actual = $codec->getFullText( $title );
184 $this->assertEquals( $expected, $actual );
187 public function provideParseTitle() {
188 //TODO: test capitalization and trimming
189 //TODO: test unicode normalization
192 array( ' : Hansi_Maier _ ', NS_MAIN
, 'en',
193 new TitleValue( NS_MAIN
, 'Hansi_Maier', '' ) ),
194 array( 'User:::1', NS_MAIN
, 'de',
195 new TitleValue( NS_USER
, '0:0:0:0:0:0:0:1', '' ) ),
196 array( ' lisa Müller', NS_USER
, 'de',
197 new TitleValue( NS_USER
, 'Lisa_Müller', '' ) ),
198 array( 'benutzerin:lisa Müller#stuff', NS_MAIN
, 'de',
199 new TitleValue( NS_USER
, 'Lisa_Müller', 'stuff' ) ),
201 array( ':Category:Quux', NS_MAIN
, 'en',
202 new TitleValue( NS_CATEGORY
, 'Quux', '' ) ),
203 array( 'Category:Quux', NS_MAIN
, 'en',
204 new TitleValue( NS_CATEGORY
, 'Quux', '' ) ),
205 array( 'Category:Quux', NS_CATEGORY
, 'en',
206 new TitleValue( NS_CATEGORY
, 'Quux', '' ) ),
207 array( 'Quux', NS_CATEGORY
, 'en',
208 new TitleValue( NS_CATEGORY
, 'Quux', '' ) ),
209 array( ':Quux', NS_CATEGORY
, 'en',
210 new TitleValue( NS_MAIN
, 'Quux', '' ) ),
212 // getGenderCache() provides a mock that considers first
213 // names ending in "a" to be female.
215 array( 'a b c', NS_MAIN
, 'en',
216 new TitleValue( NS_MAIN
, 'A_b_c' ) ),
217 array( ' a b c ', NS_MAIN
, 'en',
218 new TitleValue( NS_MAIN
, 'A_b_c' ) ),
219 array( ' _ Foo __ Bar_ _', NS_MAIN
, 'en',
220 new TitleValue( NS_MAIN
, 'Foo_Bar' ) ),
222 //NOTE: cases copied from TitleTest::testSecureAndSplit. Keep in sync.
223 array( 'Sandbox', NS_MAIN
, 'en', ),
224 array( 'A "B"', NS_MAIN
, 'en', ),
225 array( 'A \'B\'', NS_MAIN
, 'en', ),
226 array( '.com', NS_MAIN
, 'en', ),
227 array( '~', NS_MAIN
, 'en', ),
228 array( '"', NS_MAIN
, 'en', ),
229 array( '\'', NS_MAIN
, 'en', ),
231 array( 'Talk:Sandbox', NS_MAIN
, 'en',
232 new TitleValue( NS_TALK
, 'Sandbox' ) ),
233 array( 'Talk:Foo:Sandbox', NS_MAIN
, 'en',
234 new TitleValue( NS_TALK
, 'Foo:Sandbox' ) ),
235 array( 'File:Example.svg', NS_MAIN
, 'en',
236 new TitleValue( NS_FILE
, 'Example.svg' ) ),
237 array( 'File_talk:Example.svg', NS_MAIN
, 'en',
238 new TitleValue( NS_FILE_TALK
, 'Example.svg' ) ),
239 array( 'Foo/.../Sandbox', NS_MAIN
, 'en',
241 array( 'Sandbox/...', NS_MAIN
, 'en',
243 array( 'A~~', NS_MAIN
, 'en',
245 // Length is 256 total, but only title part matters
246 array( 'Category:' . str_repeat( 'x', 248 ), NS_MAIN
, 'en',
247 new TitleValue( NS_CATEGORY
,
248 'X' . str_repeat( 'x', 247 ) ) ),
249 array( str_repeat( 'x', 252 ), NS_MAIN
, 'en',
250 'X' . str_repeat( 'x', 251 ) )
255 * @dataProvider provideParseTitle
257 public function testParseTitle( $text, $ns, $lang, $title = null ) {
258 if ( $title === null ) {
259 $title = str_replace( ' ', '_', trim( $text ) );
262 if ( is_string( $title ) ) {
263 $title = new TitleValue( NS_MAIN
, $title, '' );
266 $codec = $this->makeCodec( $lang );
267 $actual = $codec->parseTitle( $text, $ns );
269 $this->assertEquals( $title, $actual );
272 public function provideParseTitle_invalid() {
273 //TODO: test unicode errors
282 array( 'Talk:File:Foo.jpg' ),
283 array( 'Talk:localtestiw:Foo' ),
284 array( 'remotetestiw:Foo' ),
285 array( '::1' ), // only valid in user namespace
286 array( 'User::x' ), // leading ":" in a user name is only valid of IPv6 addresses
288 //NOTE: cases copied from TitleTest::testSecureAndSplit. Keep in sync.
293 // Bad characters forbidden regardless of wgLegalTitleChars
305 // XML/HTML character entity references
306 // Note: Commented out because they are not marked invalid by the PHP test as
307 // Title::newFromText runs Sanitizer::decodeCharReferencesAndNormalize first.
308 //array( 'A é B' ),
309 //array( 'A é B' ),
310 //array( 'A é B' ),
311 // Subject of NS_TALK does not roundtrip to NS_MAIN
312 array( 'Talk:File:Example.svg' ),
313 // Directory navigation
316 array( './Sandbox' ),
317 array( '../Sandbox' ),
318 array( 'Foo/./Sandbox' ),
319 array( 'Foo/../Sandbox' ),
320 array( 'Sandbox/.' ),
321 array( 'Sandbox/..' ),
323 array( 'A ~~~ Name' ),
324 array( 'A ~~~~ Signature' ),
325 array( 'A ~~~~~ Timestamp' ),
326 array( str_repeat( 'x', 256 ) ),
327 // Namespace prefix without actual title
329 array( 'Category: ' ),
330 array( 'Category: #bar' )
335 * @dataProvider provideParseTitle_invalid
337 public function testParseTitle_invalid( $text ) {
338 $this->setExpectedException( 'MalformedTitleException' );
340 $codec = $this->makeCodec( 'en' );
341 $codec->parseTitle( $text, NS_MAIN
);
344 public function provideGetNamespaceName() {
346 array( NS_MAIN
, 'Foo', 'en', '' ),
347 array( NS_USER
, 'Foo', 'en', 'User' ),
348 array( NS_USER
, 'Hansi Maier', 'de', 'Benutzer' ),
350 // getGenderCache() provides a mock that considers first
351 // names ending in "a" to be female.
352 array( NS_USER
, 'Lisa Müller', 'de', 'Benutzerin' ),
357 * @dataProvider provideGetNamespaceName
364 * @internal param \TitleValue $title
366 public function testGetNamespaceName( $namespace, $text, $lang, $expected ) {
367 $codec = $this->makeCodec( $lang );
368 $name = $codec->getNamespaceName( $namespace, $text );
370 $this->assertEquals( $expected, $name );