Merge "Introducing LinksUpdateTest."
[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 module( 'mediawiki.Title', QUnit.newMwEnvironment({ config: config }) );
59
60 test( '-- Initial check', function () {
61 expect(1);
62 ok( mw.Title, 'mw.Title defined' );
63 });
64
65 test( 'Transformation', function () {
66 expect(8);
67
68 var title;
69
70 title = new mw.Title( 'File:quux pif.jpg' );
71 equal( title.getName(), 'Quux_pif' );
72
73 title = new mw.Title( 'File:Glarg_foo_glang.jpg' );
74 equal( title.getNameText(), 'Glarg foo glang' );
75
76 title = new mw.Title( 'User:ABC.DEF' );
77 equal( title.toText(), 'User:ABC.DEF' );
78 equal( title.getNamespaceId(), 2 );
79 equal( title.getNamespacePrefix(), 'User:' );
80
81 title = new mw.Title( 'uSEr:hAshAr' );
82 equal( title.toText(), 'User:HAshAr' );
83 equal( title.getNamespaceId(), 2 );
84
85 title = new mw.Title( ' MediaWiki: Foo bar .js ' );
86 // Don't ask why, it's the way the backend works. One space is kept of each set
87 equal( title.getName(), 'Foo_bar_.js', "Merge multiple spaces to a single space." );
88 });
89
90 test( 'Main text for filename', function () {
91 expect(8);
92
93 var title = new mw.Title( 'File:foo_bar.JPG' );
94
95 equal( title.getNamespaceId(), 6 );
96 equal( title.getNamespacePrefix(), 'File:' );
97 equal( title.getName(), 'Foo_bar' );
98 equal( title.getNameText(), 'Foo bar' );
99 equal( title.getMain(), 'Foo_bar.JPG' );
100 equal( title.getMainText(), 'Foo bar.JPG' );
101 equal( title.getExtension(), 'JPG' );
102 equal( title.getDotExtension(), '.JPG' );
103 });
104
105 test( 'Namespace detection and conversion', function () {
106 expect(6);
107
108 var title;
109
110 title = new mw.Title( 'something.PDF', 6 );
111 equal( title.toString(), 'File:Something.PDF' );
112
113 title = new mw.Title( 'NeilK', 3 );
114 equal( title.toString(), 'User_talk:NeilK' );
115 equal( title.toText(), 'User talk:NeilK' );
116
117 title = new mw.Title( 'Frobisher', 100 );
118 equal( title.toString(), 'Penguins:Frobisher' );
119
120 title = new mw.Title( 'antarctic_waterfowl:flightless_yet_cute.jpg' );
121 equal( title.toString(), 'Penguins:Flightless_yet_cute.jpg' );
122
123 title = new mw.Title( 'Penguins:flightless_yet_cute.jpg' );
124 equal( title.toString(), 'Penguins:Flightless_yet_cute.jpg' );
125 });
126
127 test( 'Throw error on invalid title', function () {
128 expect(1);
129
130 raises(function () {
131 var title = new mw.Title( '' );
132 }, 'Throw error on empty string' );
133 });
134
135 test( 'Case-sensivity', function () {
136 expect(3);
137
138 var title;
139
140 // Default config
141 mw.config.set( 'wgCaseSensitiveNamespaces', [] );
142
143 title = new mw.Title( 'article' );
144 equal( title.toString(), 'Article', 'Default config: No sensitive namespaces by default. First-letter becomes uppercase' );
145
146 // $wgCapitalLinks = false;
147 mw.config.set( 'wgCaseSensitiveNamespaces', [0, -2, 1, 4, 5, 6, 7, 10, 11, 12, 13, 14, 15] );
148
149 title = new mw.Title( 'article' );
150 equal( title.toString(), 'article', '$wgCapitalLinks=false: Article namespace is sensitive, first-letter case stays lowercase' );
151
152 title = new mw.Title( 'john', 2 );
153 equal( title.toString(), 'User:John', '$wgCapitalLinks=false: User namespace is insensitive, first-letter becomes uppercase' );
154 });
155
156 test( 'toString / toText', function () {
157 expect(2);
158
159 var title = new mw.Title( 'Some random page' );
160
161 equal( title.toString(), title.getPrefixedDb() );
162 equal( title.toText(), title.getPrefixedText() );
163 });
164
165 test( 'Exists', function () {
166 expect(3);
167
168 var title;
169
170 // Empty registry, checks default to null
171
172 title = new mw.Title( 'Some random page', 4 );
173 strictEqual( title.exists(), null, 'Return null with empty existance registry' );
174
175 // Basic registry, checks default to boolean
176 mw.Title.exist.set( ['Does_exist', 'User_talk:NeilK', 'Wikipedia:Sandbox_rules'], true );
177 mw.Title.exist.set( ['Does_not_exist', 'User:John', 'Foobar'], false );
178
179 title = new mw.Title( 'Project:Sandbox rules' );
180 assertTrue( title.exists(), 'Return true for page titles marked as existing' );
181 title = new mw.Title( 'Foobar' );
182 assertFalse( title.exists(), 'Return false for page titles marked as nonexistent' );
183
184 });
185
186 test( 'Url', function () {
187 expect(2);
188
189 var title;
190
191 // Config
192 mw.config.set( 'wgArticlePath', '/wiki/$1' );
193
194 title = new mw.Title( 'Foobar' );
195 equal( title.getUrl(), '/wiki/Foobar', 'Basic functionally, toString passing to wikiGetlink' );
196
197 title = new mw.Title( 'John Doe', 3 );
198 equal( title.getUrl(), '/wiki/User_talk:John_Doe', 'Escaping in title and namespace for urls' );
199 });
200
201 }() );