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