MediaWiki REL1_33
MediaWikiTitleCodecTest.php
Go to the documentation of this file.
1<?php
30
31 public function setUp() {
32 parent::setUp();
33
34 $this->setMwGlobals( [
35 'wgAllowUserJs' => false,
36 'wgDefaultLanguageVariant' => false,
37 'wgMetaNamespace' => 'Project',
38 'wgLocalInterwikis' => [ 'localtestiw' ],
39 'wgCapitalLinks' => true,
40
41 // NOTE: this is why global state is evil.
42 // TODO: refactor access to the interwiki codes so it can be injected.
43 'wgHooks' => [
44 'InterwikiLoadPrefix' => [
45 function ( $prefix, &$data ) {
46 if ( $prefix === 'localtestiw' ) {
47 $data = [ 'iw_url' => 'localtestiw' ];
48 } elseif ( $prefix === 'remotetestiw' ) {
49 $data = [ 'iw_url' => 'remotetestiw' ];
50 }
51 return false;
52 }
53 ]
54 ]
55 ] );
56 $this->setUserLang( 'en' );
57 $this->setContentLang( 'en' );
58 }
59
66 private function getGenderCache() {
67 $genderCache = $this->getMockBuilder( GenderCache::class )
68 ->disableOriginalConstructor()
69 ->getMock();
70
71 $genderCache->expects( $this->any() )
72 ->method( 'getGenderOf' )
73 ->will( $this->returnCallback( function ( $userName ) {
74 return preg_match( '/^[^- _]+a( |_|$)/u', $userName ) ? 'female' : 'male';
75 } ) );
76
77 return $genderCache;
78 }
79
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 );
86 }
87
88 public static function provideFormat() {
89 return [
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' ],
93 [
95 'hansi__maier',
96 '',
97 '',
98 'en',
99 'User talk:hansi maier',
100 'User talk:Hansi maier'
101 ],
102
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 ];
108 }
109
113 public function testFormat( $namespace, $text, $fragment, $interwiki, $lang, $expected,
114 $normalized = null
115 ) {
116 if ( $normalized === null ) {
117 $normalized = $expected;
118 }
119
120 $codec = $this->makeCodec( $lang );
121 $actual = $codec->formatTitle( $namespace, $text, $fragment, $interwiki );
122
123 $this->assertEquals( $expected, $actual, 'formatted' );
124
125 // test round trip
126 $parsed = $codec->parseTitle( $actual, NS_MAIN );
127 $actual2 = $codec->formatTitle(
128 $parsed->getNamespace(),
129 $parsed->getText(),
130 $parsed->getFragment(),
131 $parsed->getInterwiki()
132 );
133
134 $this->assertEquals( $normalized, $actual2, 'normalized after round trip' );
135 }
136
137 public static function provideGetText() {
138 return [
139 [ NS_MAIN, 'Foo_Bar', '', 'en', 'Foo Bar' ],
140 [ NS_USER, 'Hansi_Maier', 'stuff_and_so_on', 'en', 'Hansi Maier' ],
141 ];
142 }
143
147 public function testGetText( $namespace, $dbkey, $fragment, $lang, $expected ) {
148 $codec = $this->makeCodec( $lang );
149 $title = new TitleValue( $namespace, $dbkey, $fragment );
150
151 $actual = $codec->getText( $title );
152
153 $this->assertEquals( $expected, $actual );
154 }
155
156 public static function provideGetPrefixedText() {
157 return [
158 [ NS_MAIN, 'Foo_Bar', '', 'en', 'Foo Bar' ],
159 [ NS_USER, 'Hansi_Maier', 'stuff_and_so_on', 'en', 'User:Hansi Maier' ],
160
161 // No capitalization or normalization is applied while formatting!
162 [ NS_USER_TALK, 'hansi__maier', '', 'en', 'User talk:hansi maier' ],
163
164 // getGenderCache() provides a mock that considers first
165 // names ending in "a" to be female.
166 [ NS_USER, 'Lisa_Müller', '', 'de', 'Benutzerin:Lisa Müller' ],
167 [ 1000000, 'Invalid_namespace', '', 'en', 'Special:Badtitle/NS1000000:Invalid namespace' ],
168 ];
169 }
170
174 public function testGetPrefixedText( $namespace, $dbkey, $fragment, $lang, $expected ) {
175 $codec = $this->makeCodec( $lang );
176 $title = new TitleValue( $namespace, $dbkey, $fragment );
177
178 $actual = $codec->getPrefixedText( $title );
179
180 $this->assertEquals( $expected, $actual );
181 }
182
183 public static function provideGetPrefixedDBkey() {
184 return [
185 [ NS_MAIN, 'Foo_Bar', '', '', 'en', 'Foo_Bar' ],
186 [ NS_USER, 'Hansi_Maier', 'stuff_and_so_on', '', 'en', 'User:Hansi_Maier' ],
187
188 // No capitalization or normalization is applied while formatting!
189 [ NS_USER_TALK, 'hansi__maier', '', '', 'en', 'User_talk:hansi__maier' ],
190
191 // getGenderCache() provides a mock that considers first
192 // names ending in "a" to be female.
193 [ NS_USER, 'Lisa_Müller', '', '', 'de', 'Benutzerin:Lisa_Müller' ],
194
195 [ NS_MAIN, 'Remote_page', '', 'remotetestiw', 'en', 'remotetestiw:Remote_page' ],
196
197 // non-existent namespace
198 [ 10000000, 'Foobar', '', '', 'en', 'Special:Badtitle/NS10000000:Foobar' ],
199 ];
200 }
201
205 public function testGetPrefixedDBkey( $namespace, $dbkey, $fragment,
206 $interwiki, $lang, $expected
207 ) {
208 $codec = $this->makeCodec( $lang );
209 $title = new TitleValue( $namespace, $dbkey, $fragment, $interwiki );
210
211 $actual = $codec->getPrefixedDBkey( $title );
212
213 $this->assertEquals( $expected, $actual );
214 }
215
216 public static function provideGetFullText() {
217 return [
218 [ NS_MAIN, 'Foo_Bar', '', 'en', 'Foo Bar' ],
219 [ NS_USER, 'Hansi_Maier', 'stuff_and_so_on', 'en', 'User:Hansi Maier#stuff and so on' ],
220
221 // No capitalization or normalization is applied while formatting!
222 [ NS_USER_TALK, 'hansi__maier', '', 'en', 'User talk:hansi maier' ],
223 ];
224 }
225
229 public function testGetFullText( $namespace, $dbkey, $fragment, $lang, $expected ) {
230 $codec = $this->makeCodec( $lang );
231 $title = new TitleValue( $namespace, $dbkey, $fragment );
232
233 $actual = $codec->getFullText( $title );
234
235 $this->assertEquals( $expected, $actual );
236 }
237
238 public static function provideParseTitle() {
239 // TODO: test capitalization and trimming
240 // TODO: test unicode normalization
241
242 return [
243 [ ' : Hansi_Maier _ ', NS_MAIN, 'en',
244 new TitleValue( NS_MAIN, 'Hansi_Maier', '' ) ],
245 [ 'User:::1', NS_MAIN, 'de',
246 new TitleValue( NS_USER, '0:0:0:0:0:0:0:1', '' ) ],
247 [ ' lisa Müller', NS_USER, 'de',
248 new TitleValue( NS_USER, 'Lisa_Müller', '' ) ],
249 [ 'benutzerin:lisa Müller#stuff', NS_MAIN, 'de',
250 new TitleValue( NS_USER, 'Lisa_Müller', 'stuff' ) ],
251
252 [ ':Category:Quux', NS_MAIN, 'en',
253 new TitleValue( NS_CATEGORY, 'Quux', '' ) ],
254 [ 'Category:Quux', NS_MAIN, 'en',
255 new TitleValue( NS_CATEGORY, 'Quux', '' ) ],
256 [ 'Category:Quux', NS_CATEGORY, 'en',
257 new TitleValue( NS_CATEGORY, 'Quux', '' ) ],
258 [ 'Quux', NS_CATEGORY, 'en',
259 new TitleValue( NS_CATEGORY, 'Quux', '' ) ],
260 [ ':Quux', NS_CATEGORY, 'en',
261 new TitleValue( NS_MAIN, 'Quux', '' ) ],
262
263 // getGenderCache() provides a mock that considers first
264 // names ending in "a" to be female.
265
266 [ 'a b c', NS_MAIN, 'en',
267 new TitleValue( NS_MAIN, 'A_b_c' ) ],
268 [ ' a b c ', NS_MAIN, 'en',
269 new TitleValue( NS_MAIN, 'A_b_c' ) ],
270 [ ' _ Foo __ Bar_ _', NS_MAIN, 'en',
271 new TitleValue( NS_MAIN, 'Foo_Bar' ) ],
272
273 // NOTE: cases copied from TitleTest::testSecureAndSplit. Keep in sync.
274 [ 'Sandbox', NS_MAIN, 'en', ],
275 [ 'A "B"', NS_MAIN, 'en', ],
276 [ 'A \'B\'', NS_MAIN, 'en', ],
277 [ '.com', NS_MAIN, 'en', ],
278 [ '~', NS_MAIN, 'en', ],
279 [ '"', NS_MAIN, 'en', ],
280 [ '\'', NS_MAIN, 'en', ],
281
282 [ 'Talk:Sandbox', NS_MAIN, 'en',
283 new TitleValue( NS_TALK, 'Sandbox' ) ],
284 [ 'Talk:Foo:Sandbox', NS_MAIN, 'en',
285 new TitleValue( NS_TALK, 'Foo:Sandbox' ) ],
286 [ 'File:Example.svg', NS_MAIN, 'en',
287 new TitleValue( NS_FILE, 'Example.svg' ) ],
288 [ 'File_talk:Example.svg', NS_MAIN, 'en',
289 new TitleValue( NS_FILE_TALK, 'Example.svg' ) ],
290 [ 'Foo/.../Sandbox', NS_MAIN, 'en',
291 'Foo/.../Sandbox' ],
292 [ 'Sandbox/...', NS_MAIN, 'en',
293 'Sandbox/...' ],
294 [ 'A~~', NS_MAIN, 'en',
295 'A~~' ],
296 // Length is 256 total, but only title part matters
297 [ 'Category:' . str_repeat( 'x', 248 ), NS_MAIN, 'en',
299 'X' . str_repeat( 'x', 247 ) ) ],
300 [ str_repeat( 'x', 252 ), NS_MAIN, 'en',
301 'X' . str_repeat( 'x', 251 ) ]
302 ];
303 }
304
308 public function testParseTitle( $text, $ns, $lang, $title = null ) {
309 if ( $title === null ) {
310 $title = str_replace( ' ', '_', trim( $text ) );
311 }
312
313 if ( is_string( $title ) ) {
314 $title = new TitleValue( NS_MAIN, $title, '' );
315 }
316
317 $codec = $this->makeCodec( $lang );
318 $actual = $codec->parseTitle( $text, $ns );
319
320 $this->assertEquals( $title, $actual );
321 }
322
323 public static function provideParseTitle_invalid() {
324 // TODO: test unicode errors
325
326 return [
327 [ '#' ],
328 [ '::' ],
329 [ '::xx' ],
330 [ '::##' ],
331 [ ' :: x' ],
332
333 [ 'Talk:File:Foo.jpg' ],
334 [ 'Talk:localtestiw:Foo' ],
335 [ '::1' ], // only valid in user namespace
336 [ 'User::x' ], // leading ":" in a user name is only valid of IPv6 addresses
337
338 // NOTE: cases copied from TitleTest::testSecureAndSplit. Keep in sync.
339 [ '' ],
340 [ ':' ],
341 [ '__ __' ],
342 [ ' __ ' ],
343 // Bad characters forbidden regardless of wgLegalTitleChars
344 [ 'A [ B' ],
345 [ 'A ] B' ],
346 [ 'A { B' ],
347 [ 'A } B' ],
348 [ 'A < B' ],
349 [ 'A > B' ],
350 [ 'A | B' ],
351 // URL encoding
352 [ 'A%20B' ],
353 [ 'A%23B' ],
354 [ 'A%2523B' ],
355 // XML/HTML character entity references
356 // Note: Commented out because they are not marked invalid by the PHP test as
357 // Title::newFromText runs Sanitizer::decodeCharReferencesAndNormalize first.
358 // [ 'A &eacute; B' ],
359 // [ 'A &#233; B' ],
360 // [ 'A &#x00E9; B' ],
361 // Subject of NS_TALK does not roundtrip to NS_MAIN
362 [ 'Talk:File:Example.svg' ],
363 // Directory navigation
364 [ '.' ],
365 [ '..' ],
366 [ './Sandbox' ],
367 [ '../Sandbox' ],
368 [ 'Foo/./Sandbox' ],
369 [ 'Foo/../Sandbox' ],
370 [ 'Sandbox/.' ],
371 [ 'Sandbox/..' ],
372 // Tilde
373 [ 'A ~~~ Name' ],
374 [ 'A ~~~~ Signature' ],
375 [ 'A ~~~~~ Timestamp' ],
376 [ str_repeat( 'x', 256 ) ],
377 // Namespace prefix without actual title
378 [ 'Talk:' ],
379 [ 'Category: ' ],
380 [ 'Category: #bar' ]
381 ];
382 }
383
387 public function testParseTitle_invalid( $text ) {
388 $this->setExpectedException( MalformedTitleException::class );
389
390 $codec = $this->makeCodec( 'en' );
391 $codec->parseTitle( $text, NS_MAIN );
392 }
393
394 public static function provideGetNamespaceName() {
395 return [
396 [ NS_MAIN, 'Foo', 'en', '' ],
397 [ NS_USER, 'Foo', 'en', 'User' ],
398 [ NS_USER, 'Hansi Maier', 'de', 'Benutzer' ],
399
400 // getGenderCache() provides a mock that considers first
401 // names ending in "a" to be female.
402 [ NS_USER, 'Lisa Müller', 'de', 'Benutzerin' ],
403 ];
404 }
405
409 public function testGetNamespaceName( $namespace, $text, $lang, $expected ) {
410 $codec = $this->makeCodec( $lang );
411 $name = $codec->getNamespaceName( $namespace, $text );
412
413 $this->assertEquals( $expected, $name );
414 }
415}
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
testFormat( $namespace, $text, $fragment, $interwiki, $lang, $expected, $normalized=null)
provideFormat
testParseTitle( $text, $ns, $lang, $title=null)
provideParseTitle
getGenderCache()
Returns a mock GenderCache that will consider a user "female" if the first part of the user name ends...
testGetPrefixedDBkey( $namespace, $dbkey, $fragment, $interwiki, $lang, $expected)
provideGetPrefixedDBkey
testGetPrefixedText( $namespace, $dbkey, $fragment, $lang, $expected)
provideGetPrefixedText
testGetNamespaceName( $namespace, $text, $lang, $expected)
provideGetNamespaceName
testGetText( $namespace, $dbkey, $fragment, $lang, $expected)
provideGetText
testGetFullText( $namespace, $dbkey, $fragment, $lang, $expected)
provideGetFullText
testParseTitle_invalid( $text)
provideParseTitle_invalid
A codec for MediaWiki page titles.
Represents a page (or page fragment) title within MediaWiki.
$data
Utility to generate mapping file used in mw.Title (phpCharToUpper.json)
const NS_USER
Definition Defines.php:75
const NS_FILE
Definition Defines.php:79
const NS_MAIN
Definition Defines.php:73
const NS_FILE_TALK
Definition Defines.php:80
const NS_TALK
Definition Defines.php:74
const NS_USER_TALK
Definition Defines.php:76
const NS_CATEGORY
Definition Defines.php:87
namespace and then decline to actually register it file or subcat img or subcat $title
Definition hooks.txt:955
Allows to change the fields on the form that will be generated $name
Definition hooks.txt:271
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
if(!isset( $args[0])) $lang