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