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