Merge "Fix API output formatting (change lines delimited with * as bold)"
[lhc/web/wiklou.git] / tests / qunit / suites / resources / mediawiki / mediawiki.Title.test.js
1 ( function ( mw ) {
2 // mw.Title relies on these three config vars
3 // Restore them after each test run
4 var config = {
5 wgFormattedNamespaces: {
6 '-2': 'Media',
7 '-1': 'Special',
8 0: '',
9 1: 'Talk',
10 2: 'User',
11 3: 'User talk',
12 4: 'Wikipedia',
13 5: 'Wikipedia talk',
14 6: 'File',
15 7: 'File talk',
16 8: 'MediaWiki',
17 9: 'MediaWiki talk',
18 10: 'Template',
19 11: 'Template talk',
20 12: 'Help',
21 13: 'Help talk',
22 14: 'Category',
23 15: 'Category talk',
24 // testing custom / localized namespace
25 100: 'Penguins'
26 },
27 wgNamespaceIds: {
28 /*jshint camelcase: false */
29 media: -2,
30 special: -1,
31 '': 0,
32 talk: 1,
33 user: 2,
34 user_talk: 3,
35 wikipedia: 4,
36 wikipedia_talk: 5,
37 file: 6,
38 file_talk: 7,
39 mediawiki: 8,
40 mediawiki_talk: 9,
41 template: 10,
42 template_talk: 11,
43 help: 12,
44 help_talk: 13,
45 category: 14,
46 category_talk: 15,
47 image: 6,
48 image_talk: 7,
49 project: 4,
50 project_talk: 5,
51 /* testing custom / alias */
52 penguins: 100,
53 antarctic_waterfowl: 100
54 },
55 wgCaseSensitiveNamespaces: []
56 };
57
58 QUnit.module( 'mediawiki.Title', QUnit.newMwEnvironment( { config: config } ) );
59
60 QUnit.test( 'Transformation', 8, function ( assert ) {
61 var title;
62
63 title = new mw.Title( 'File:quux pif.jpg' );
64 assert.equal( title.getName(), 'Quux_pif' );
65
66 title = new mw.Title( 'File:Glarg_foo_glang.jpg' );
67 assert.equal( title.getNameText(), 'Glarg foo glang' );
68
69 title = new mw.Title( 'User:ABC.DEF' );
70 assert.equal( title.toText(), 'User:ABC.DEF' );
71 assert.equal( title.getNamespaceId(), 2 );
72 assert.equal( title.getNamespacePrefix(), 'User:' );
73
74 title = new mw.Title( 'uSEr:hAshAr' );
75 assert.equal( title.toText(), 'User:HAshAr' );
76 assert.equal( title.getNamespaceId(), 2 );
77
78 title = new mw.Title( ' MediaWiki: Foo bar .js ' );
79 // Don't ask why, it's the way the backend works. One space is kept of each set
80 assert.equal( title.getName(), 'Foo_bar_.js', 'Merge multiple spaces to a single space.' );
81 } );
82
83 QUnit.test( 'Main text for filename', 8, function ( assert ) {
84 var title = new mw.Title( 'File:foo_bar.JPG' );
85
86 assert.equal( title.getNamespaceId(), 6 );
87 assert.equal( title.getNamespacePrefix(), 'File:' );
88 assert.equal( title.getName(), 'Foo_bar' );
89 assert.equal( title.getNameText(), 'Foo bar' );
90 assert.equal( title.getMain(), 'Foo_bar.JPG' );
91 assert.equal( title.getMainText(), 'Foo bar.JPG' );
92 assert.equal( title.getExtension(), 'JPG' );
93 assert.equal( title.getDotExtension(), '.JPG' );
94 } );
95
96 QUnit.test( 'Namespace detection and conversion', 6, function ( assert ) {
97 var title;
98
99 title = new mw.Title( 'something.PDF', 6 );
100 assert.equal( title.toString(), 'File:Something.PDF' );
101
102 title = new mw.Title( 'NeilK', 3 );
103 assert.equal( title.toString(), 'User_talk:NeilK' );
104 assert.equal( title.toText(), 'User talk:NeilK' );
105
106 title = new mw.Title( 'Frobisher', 100 );
107 assert.equal( title.toString(), 'Penguins:Frobisher' );
108
109 title = new mw.Title( 'antarctic_waterfowl:flightless_yet_cute.jpg' );
110 assert.equal( title.toString(), 'Penguins:Flightless_yet_cute.jpg' );
111
112 title = new mw.Title( 'Penguins:flightless_yet_cute.jpg' );
113 assert.equal( title.toString(), 'Penguins:Flightless_yet_cute.jpg' );
114 } );
115
116 QUnit.test( 'Throw error on invalid title', 1, function ( assert ) {
117 assert.throws( function () {
118 return new mw.Title( '' );
119 }, 'Throw error on empty string' );
120 } );
121
122 QUnit.test( 'Case-sensivity', 3, function ( assert ) {
123 var title;
124
125 // Default config
126 mw.config.set( 'wgCaseSensitiveNamespaces', [] );
127
128 title = new mw.Title( 'article' );
129 assert.equal( title.toString(), 'Article', 'Default config: No sensitive namespaces by default. First-letter becomes uppercase' );
130
131 // $wgCapitalLinks = false;
132 mw.config.set( 'wgCaseSensitiveNamespaces', [0, -2, 1, 4, 5, 6, 7, 10, 11, 12, 13, 14, 15] );
133
134 title = new mw.Title( 'article' );
135 assert.equal( title.toString(), 'article', '$wgCapitalLinks=false: Article namespace is sensitive, first-letter case stays lowercase' );
136
137 title = new mw.Title( 'john', 2 );
138 assert.equal( title.toString(), 'User:John', '$wgCapitalLinks=false: User namespace is insensitive, first-letter becomes uppercase' );
139 } );
140
141 QUnit.test( 'toString / toText', 2, function ( assert ) {
142 var title = new mw.Title( 'Some random page' );
143
144 assert.equal( title.toString(), title.getPrefixedDb() );
145 assert.equal( title.toText(), title.getPrefixedText() );
146 } );
147
148 QUnit.test( 'getExtension', 7, function ( assert ) {
149 function extTest( pagename, ext, description ) {
150 var title = new mw.Title( pagename );
151 assert.equal( title.getExtension(), ext, description || pagename );
152 }
153
154 extTest( 'MediaWiki:Vector.js', 'js' );
155 extTest( 'User:Example/common.css', 'css' );
156 extTest( 'File:Example.longextension', 'longextension', 'Extension parsing not limited (bug 36151)' );
157 extTest( 'Example/information.json', 'json', 'Extension parsing not restricted from any namespace' );
158 extTest( 'Foo.', null, 'Trailing dot is not an extension' );
159 extTest( 'Foo..', null, 'Trailing dots are not an extension' );
160 extTest( 'Foo.a.', null, 'Page name with dots and ending in a dot does not have an extension' );
161
162 // @broken: Throws an exception
163 // extTest( '.NET', null, 'Leading dot is (or is not?) an extension' );
164 } );
165
166 QUnit.test( 'exists', 3, function ( assert ) {
167 var title;
168
169 // Empty registry, checks default to null
170
171 title = new mw.Title( 'Some random page', 4 );
172 assert.strictEqual( title.exists(), null, 'Return null with empty existance registry' );
173
174 // Basic registry, checks default to boolean
175 mw.Title.exist.set( ['Does_exist', 'User_talk:NeilK', 'Wikipedia:Sandbox_rules'], true );
176 mw.Title.exist.set( ['Does_not_exist', 'User:John', 'Foobar'], false );
177
178 title = new mw.Title( 'Project:Sandbox rules' );
179 assert.assertTrue( title.exists(), 'Return true for page titles marked as existing' );
180 title = new mw.Title( 'Foobar' );
181 assert.assertFalse( title.exists(), 'Return false for page titles marked as nonexistent' );
182
183 } );
184
185 QUnit.test( 'getUrl', 2, function ( assert ) {
186 var title;
187
188 // Config
189 mw.config.set( 'wgArticlePath', '/wiki/$1' );
190
191 title = new mw.Title( 'Foobar' );
192 assert.equal( title.getUrl(), '/wiki/Foobar', 'Basic functionally, toString passing to wikiGetlink' );
193
194 title = new mw.Title( 'John Doe', 3 );
195 assert.equal( title.getUrl(), '/wiki/User_talk:John_Doe', 'Escaping in title and namespace for urls' );
196 } );
197
198 }( mediaWiki ) );