2 // mw.Title relies on these three config vars
3 // Restore them after each test run
5 wgFormattedNamespaces
: {
24 // testing custom / localized namespace
28 /*jshint camelcase: false */
51 /* testing custom / alias */
53 antarctic_waterfowl
: 100
55 wgCaseSensitiveNamespaces
: []
57 repeat = function ( input
, multiplier
) {
58 return new Array( multiplier
+ 1 ).join( input
);
61 // See also TitleTest.php#testSecureAndSplit
73 'File_talk:Example.svg',
77 // Length is 256 total, but only title part matters
78 'Category:' + repeat( 'x', 248 ),
85 // Bad characters forbidden regardless of wgLegalTitleChars
97 // XML/HTML character entity references
98 // Note: The ones with # are commented out as those are interpreted as fragment and
99 // as such end up being valid.
103 // Subject of NS_TALK does not roundtrip to NS_MAIN
104 'Talk:File:Example.svg',
105 // Directory navigation
119 // Extension separation is a js invention, for length
120 // purposes it is part of the title
121 repeat( 'x', 252 ) + '.json',
122 // Namespace prefix without actual title
130 QUnit
.module( 'mediawiki.Title', QUnit
.newMwEnvironment( { config
: config
} ) );
132 QUnit
.test( 'constructor', cases
.invalid
.length
, function ( assert
) {
134 for ( i
= 0; i
< cases
.valid
.length
; i
++ ) {
135 title
= new mw
.Title( cases
.valid
[i
] );
137 for ( i
= 0; i
< cases
.invalid
.length
; i
++ ) {
138 /*jshint loopfunc:true */
139 title
= cases
.invalid
[i
];
140 assert
.throws( function () {
141 return new mw
.Title( title
);
142 }, cases
.invalid
[i
] );
146 QUnit
.test( 'newFromText', cases
.valid
.length
+ cases
.invalid
.length
, function ( assert
) {
148 for ( i
= 0; i
< cases
.valid
.length
; i
++ ) {
150 $.type( mw
.Title
.newFromText( cases
.valid
[i
] ) ),
155 for ( i
= 0; i
< cases
.invalid
.length
; i
++ ) {
157 $.type( mw
.Title
.newFromText( cases
.invalid
[i
] ) ),
164 QUnit
.test( 'Basic parsing', 12, function ( assert
) {
166 title
= new mw
.Title( 'File:Foo_bar.JPG' );
168 assert
.equal( title
.getNamespaceId(), 6 );
169 assert
.equal( title
.getNamespacePrefix(), 'File:' );
170 assert
.equal( title
.getName(), 'Foo_bar' );
171 assert
.equal( title
.getNameText(), 'Foo bar' );
172 assert
.equal( title
.getExtension(), 'JPG' );
173 assert
.equal( title
.getDotExtension(), '.JPG' );
174 assert
.equal( title
.getMain(), 'Foo_bar.JPG' );
175 assert
.equal( title
.getMainText(), 'Foo bar.JPG' );
176 assert
.equal( title
.getPrefixedDb(), 'File:Foo_bar.JPG' );
177 assert
.equal( title
.getPrefixedText(), 'File:Foo bar.JPG' );
179 title
= new mw
.Title( 'Foo#bar' );
180 assert
.equal( title
.getPrefixedText(), 'Foo' );
181 assert
.equal( title
.getFragment(), 'bar' );
184 QUnit
.test( 'Transformation', 11, function ( assert
) {
187 title
= new mw
.Title( 'File:quux pif.jpg' );
188 assert
.equal( title
.getNameText(), 'Quux pif', 'First character of title' );
190 title
= new mw
.Title( 'File:Glarg_foo_glang.jpg' );
191 assert
.equal( title
.getNameText(), 'Glarg foo glang', 'Underscores' );
193 title
= new mw
.Title( 'User:ABC.DEF' );
194 assert
.equal( title
.toText(), 'User:ABC.DEF', 'Round trip text' );
195 assert
.equal( title
.getNamespaceId(), 2, 'Parse canonical namespace prefix' );
197 title
= new mw
.Title( 'Image:quux pix.jpg' );
198 assert
.equal( title
.getNamespacePrefix(), 'File:', 'Transform alias to canonical namespace' );
200 title
= new mw
.Title( 'uSEr:hAshAr' );
201 assert
.equal( title
.toText(), 'User:HAshAr' );
202 assert
.equal( title
.getNamespaceId(), 2, 'Case-insensitive namespace prefix' );
204 // Don't ask why, it's the way the backend works. One space is kept of each set.
205 title
= new mw
.Title( 'Foo __ \t __ bar' );
206 assert
.equal( title
.getMain(), 'Foo_bar', 'Merge multiple types of whitespace/underscores into a single underscore' );
208 // Regression test: Previously it would only detect an extension if there is no space after it
209 title
= new mw
.Title( 'Example.js ' );
210 assert
.equal( title
.getExtension(), 'js', 'Space after an extension is stripped' );
212 title
= new mw
.Title( 'Example#foo' );
213 assert
.equal( title
.getFragment(), 'foo', 'Fragment' );
215 title
= new mw
.Title( 'Example#_foo_bar baz_' );
216 assert
.equal( title
.getFragment(), ' foo bar baz', 'Fragment' );
219 QUnit
.test( 'Namespace detection and conversion', 10, function ( assert
) {
222 title
= new mw
.Title( 'File:User:Example' );
223 assert
.equal( title
.getNamespaceId(), 6, 'Titles can contain namespace prefixes, which are otherwise ignored' );
225 title
= new mw
.Title( 'Example', 6 );
226 assert
.equal( title
.getNamespaceId(), 6, 'Default namespace passed is used' );
228 title
= new mw
.Title( 'User:Example', 6 );
229 assert
.equal( title
.getNamespaceId(), 2, 'Included namespace prefix overrides the given default' );
231 title
= new mw
.Title( ':Example', 6 );
232 assert
.equal( title
.getNamespaceId(), 0, 'Colon forces main namespace' );
234 title
= new mw
.Title( 'something.PDF', 6 );
235 assert
.equal( title
.toString(), 'File:Something.PDF' );
237 title
= new mw
.Title( 'NeilK', 3 );
238 assert
.equal( title
.toString(), 'User_talk:NeilK' );
239 assert
.equal( title
.toText(), 'User talk:NeilK' );
241 title
= new mw
.Title( 'Frobisher', 100 );
242 assert
.equal( title
.toString(), 'Penguins:Frobisher' );
244 title
= new mw
.Title( 'antarctic_waterfowl:flightless_yet_cute.jpg' );
245 assert
.equal( title
.toString(), 'Penguins:Flightless_yet_cute.jpg' );
247 title
= new mw
.Title( 'Penguins:flightless_yet_cute.jpg' );
248 assert
.equal( title
.toString(), 'Penguins:Flightless_yet_cute.jpg' );
251 QUnit
.test( 'Throw error on invalid title', 1, function ( assert
) {
252 assert
.throws( function () {
253 return new mw
.Title( '' );
254 }, 'Throw error on empty string' );
257 QUnit
.test( 'Case-sensivity', 3, function ( assert
) {
261 mw
.config
.set( 'wgCaseSensitiveNamespaces', [] );
263 title
= new mw
.Title( 'article' );
264 assert
.equal( title
.toString(), 'Article', 'Default config: No sensitive namespaces by default. First-letter becomes uppercase' );
266 // $wgCapitalLinks = false;
267 mw
.config
.set( 'wgCaseSensitiveNamespaces', [0, -2, 1, 4, 5, 6, 7, 10, 11, 12, 13, 14, 15] );
269 title
= new mw
.Title( 'article' );
270 assert
.equal( title
.toString(), 'article', '$wgCapitalLinks=false: Article namespace is sensitive, first-letter case stays lowercase' );
272 title
= new mw
.Title( 'john', 2 );
273 assert
.equal( title
.toString(), 'User:John', '$wgCapitalLinks=false: User namespace is insensitive, first-letter becomes uppercase' );
276 QUnit
.test( 'toString / toText', 2, function ( assert
) {
277 var title
= new mw
.Title( 'Some random page' );
279 assert
.equal( title
.toString(), title
.getPrefixedDb() );
280 assert
.equal( title
.toText(), title
.getPrefixedText() );
283 QUnit
.test( 'getExtension', 7, function ( assert
) {
284 function extTest( pagename
, ext
, description
) {
285 var title
= new mw
.Title( pagename
);
286 assert
.equal( title
.getExtension(), ext
, description
|| pagename
);
289 extTest( 'MediaWiki:Vector.js', 'js' );
290 extTest( 'User:Example/common.css', 'css' );
291 extTest( 'File:Example.longextension', 'longextension', 'Extension parsing not limited (bug 36151)' );
292 extTest( 'Example/information.json', 'json', 'Extension parsing not restricted from any namespace' );
293 extTest( 'Foo.', null, 'Trailing dot is not an extension' );
294 extTest( 'Foo..', null, 'Trailing dots are not an extension' );
295 extTest( 'Foo.a.', null, 'Page name with dots and ending in a dot does not have an extension' );
297 // @broken: Throws an exception
298 // extTest( '.NET', null, 'Leading dot is (or is not?) an extension' );
301 QUnit
.test( 'exists', 3, function ( assert
) {
304 // Empty registry, checks default to null
306 title
= new mw
.Title( 'Some random page', 4 );
307 assert
.strictEqual( title
.exists(), null, 'Return null with empty existance registry' );
309 // Basic registry, checks default to boolean
310 mw
.Title
.exist
.set( ['Does_exist', 'User_talk:NeilK', 'Wikipedia:Sandbox_rules'], true );
311 mw
.Title
.exist
.set( ['Does_not_exist', 'User:John', 'Foobar'], false );
313 title
= new mw
.Title( 'Project:Sandbox rules' );
314 assert
.assertTrue( title
.exists(), 'Return true for page titles marked as existing' );
315 title
= new mw
.Title( 'Foobar' );
316 assert
.assertFalse( title
.exists(), 'Return false for page titles marked as nonexistent' );
320 QUnit
.test( 'getUrl', 2, function ( assert
) {
324 mw
.config
.set( 'wgArticlePath', '/wiki/$1' );
326 title
= new mw
.Title( 'Foobar' );
327 assert
.equal( title
.getUrl(), '/wiki/Foobar', 'Basic functionally, toString passing to wikiGetlink' );
329 title
= new mw
.Title( 'John Doe', 3 );
330 assert
.equal( title
.getUrl(), '/wiki/User_talk:John_Doe', 'Escaping in title and namespace for urls' );
333 }( mediaWiki
, jQuery
) );