Merge "(bug 26909) follow up r102947: fix the navigation with 'dir' and 'continue...
[lhc/web/wiklou.git] / tests / qunit / suites / resources / mediawiki / mediawiki.test.js
1 module( 'mediawiki', QUnit.newMwEnvironment() );
2
3 test( '-- Initial check', function() {
4 expect(8);
5
6 ok( window.jQuery, 'jQuery defined' );
7 ok( window.$, '$j defined' );
8 ok( window.$j, '$j defined' );
9 strictEqual( window.$, window.jQuery, '$ alias to jQuery' );
10 strictEqual( window.$j, window.jQuery, '$j alias to jQuery' );
11
12 ok( window.mediaWiki, 'mediaWiki defined' );
13 ok( window.mw, 'mw defined' );
14 strictEqual( window.mw, window.mediaWiki, 'mw alias to mediaWiki' );
15 });
16
17 test( 'mw.Map', function() {
18 expect(17);
19
20 ok( mw.Map, 'mw.Map defined' );
21
22 var conf = new mw.Map(),
23 // Dummy variables
24 funky = function() {},
25 arry = [],
26 nummy = 7;
27
28 // Tests for input validation
29 strictEqual( conf.get( 'inexistantKey' ), null, 'Map.get returns null if selection was a string and the key was not found' );
30 strictEqual( conf.set( 'myKey', 'myValue' ), true, 'Map.set returns boolean true if a value was set for a valid key string' );
31 strictEqual( conf.set( funky, 'Funky' ), false, 'Map.set returns boolean false if key was invalid (Function)' );
32 strictEqual( conf.set( arry, 'Arry' ), false, 'Map.set returns boolean false if key was invalid (Array)' );
33 strictEqual( conf.set( nummy, 'Nummy' ), false, 'Map.set returns boolean false if key was invalid (Number)' );
34 equal( conf.get( 'myKey' ), 'myValue', 'Map.get returns a single value value correctly' );
35 strictEqual( conf.get( nummy ), null, 'Map.get ruturns null if selection was invalid (Number)' );
36 strictEqual( conf.get( funky ), null, 'Map.get ruturns null if selection was invalid (Function)' );
37
38 // Multiple values at once
39 var someValues = {
40 'foo': 'bar',
41 'lorem': 'ipsum',
42 'MediaWiki': true
43 };
44 strictEqual( conf.set( someValues ), true, 'Map.set returns boolean true if multiple values were set by passing an object' );
45 deepEqual( conf.get( ['foo', 'lorem'] ), {
46 'foo': 'bar',
47 'lorem': 'ipsum'
48 }, 'Map.get returns multiple values correctly as an object' );
49
50 deepEqual( conf.get( ['foo', 'notExist'] ), {
51 'foo': 'bar',
52 'notExist': null
53 }, 'Map.get return includes keys that were not found as null values' );
54
55 strictEqual( conf.exists( 'foo' ), true, 'Map.exists returns boolean true if a key exists' );
56 strictEqual( conf.exists( 'notExist' ), false, 'Map.exists returns boolean false if a key does not exists' );
57
58 // Interacting with globals and accessing the values object
59 strictEqual( conf.get(), conf.values, 'Map.get returns the entire values object by reference (if called without arguments)' );
60
61 conf.set( 'globalMapChecker', 'Hi' );
62
63 ok( false === 'globalMapChecker' in window, 'new mw.Map did not store its values in the global window object by default' );
64
65 var globalConf = new mw.Map( true );
66 globalConf.set( 'anotherGlobalMapChecker', 'Hello' );
67
68 ok( 'anotherGlobalMapChecker' in window, 'new mw.Map( true ) did store its values in the global window object' );
69
70 // Whitelist this global variable for QUnit's 'noglobal' mode
71 if ( QUnit.config.noglobals ) {
72 QUnit.config.pollution.push( 'anotherGlobalMapChecker' );
73 }
74 });
75
76 test( 'mw.config', function() {
77 expect(1);
78
79 ok( mw.config instanceof mw.Map, 'mw.config instance of mw.Map' );
80 });
81
82 test( 'mw.message & mw.messages', function() {
83 expect(20);
84
85 ok( mw.messages, 'messages defined' );
86 ok( mw.messages instanceof mw.Map, 'mw.messages instance of mw.Map' );
87 ok( mw.messages.set( 'hello', 'Hello <b>awesome</b> world' ), 'mw.messages.set: Register' );
88
89 var hello = mw.message( 'hello' );
90
91 equal( hello.format, 'plain', 'Message property "format" defaults to "plain"' );
92 strictEqual( hello.map, mw.messages, 'Message property "map" defaults to the global instance in mw.messages' );
93 equal( hello.key, 'hello', 'Message property "key" (currect key)' );
94 deepEqual( hello.parameters, [], 'Message property "parameters" defaults to an empty array' );
95
96 // Todo
97 ok( hello.params, 'Message prototype "params"' );
98
99 hello.format = 'plain';
100 equal( hello.toString(), 'Hello <b>awesome</b> world', 'Message.toString returns the message as a string with the current "format"' );
101
102 equal( hello.escaped(), 'Hello &lt;b&gt;awesome&lt;/b&gt; world', 'Message.escaped returns the escaped message' );
103 equal( hello.format, 'escaped', 'Message.escaped correctly updated the "format" property' );
104
105 hello.parse();
106 equal( hello.format, 'parse', 'Message.parse correctly updated the "format" property' );
107
108 hello.plain();
109 equal( hello.format, 'plain', 'Message.plain correctly updated the "format" property' );
110
111 strictEqual( hello.exists(), true, 'Message.exists returns true for existing messages' );
112
113 var goodbye = mw.message( 'goodbye' );
114 strictEqual( goodbye.exists(), false, 'Message.exists returns false for nonexistent messages' );
115
116 equal( goodbye.plain(), '<goodbye>', 'Message.toString returns plain <key> if format is "plain" and key does not exist' );
117 // bug 30684
118 equal( goodbye.escaped(), '&lt;goodbye&gt;', 'Message.toString returns properly escaped &lt;key&gt; if format is "escaped" and key does not exist' );
119
120 ok( mw.messages.set( 'pluraltestmsg', 'There {{PLURAL:$1|is|are}} $1 {{PLURAL:$1|result|results}}' ), 'mw.messages.set: Register' );
121 var pluralMessage = mw.message( 'pluraltestmsg' , 6 );
122 equal( pluralMessage.plain(), 'There are 6 results', 'plural get resolved when format is plain' );
123 equal( pluralMessage.parse(), 'There are 6 results', 'plural get resolved when format is parse' );
124
125 });
126
127 test( 'mw.msg', function() {
128 expect(11);
129
130 ok( mw.messages.set( 'hello', 'Hello <b>awesome</b> world' ), 'mw.messages.set: Register' );
131 equal( mw.msg( 'hello' ), 'Hello <b>awesome</b> world', 'Gets message with default options (existing message)' );
132 equal( mw.msg( 'goodbye' ), '<goodbye>', 'Gets message with default options (nonexistent message)' );
133
134 ok( mw.messages.set( 'plural-item' , 'Found $1 {{PLURAL:$1|item|items}}' ) );
135 equal( mw.msg( 'plural-item', 5 ), 'Found 5 items', 'Apply plural for count 5' );
136 equal( mw.msg( 'plural-item', 0 ), 'Found 0 items', 'Apply plural for count 0' );
137 equal( mw.msg( 'plural-item', 1 ), 'Found 1 item', 'Apply plural for count 1' );
138
139 ok( mw.messages.set('gender-plural-msg' , '{{GENDER:$1|he|she|they}} {{PLURAL:$2|is|are}} awesome' ) );
140 equal( mw.msg( 'gender-plural-msg', 'male', 1 ), 'he is awesome', 'Gender test for male, plural count 1' );
141 equal( mw.msg( 'gender-plural-msg', 'female', '1' ), 'she is awesome', 'Gender test for female, plural count 1' );
142 equal( mw.msg( 'gender-plural-msg', 'unknown', 10 ), 'they are awesome', 'Gender test for neutral, plural count 10' );
143
144 });
145
146 test( 'mw.loader', function() {
147 expect(2);
148
149 var isAwesomeDone;
150
151 // Async ahead
152 stop();
153
154 mw.loader.testCallback = function () {
155 start();
156 strictEqual( isAwesomeDone, undefined, 'Implementing module is.awesome: isAwesomeDone should still be undefined');
157 isAwesomeDone = true;
158 };
159
160 mw.loader.implement( 'test.callback', [QUnit.fixurl( mw.config.get( 'wgScriptPath' ) + '/tests/qunit/data/callMwLoaderTestCallback.js' )], {}, {} );
161
162 mw.loader.using( 'test.callback', function () {
163
164 // /sample/awesome.js declares the "mw.loader.testCallback" function
165 // which contains a call to start() and ok()
166 strictEqual( isAwesomeDone, true, "test.callback module should've caused isAwesomeDone to be true" );
167 delete mw.loader.testCallback;
168
169 }, function () {
170 start();
171 ok( false, 'Error callback fired while loader.using "test.callback" module' );
172 });
173 });
174
175 test( 'mw.loader.implement', function () {
176 expect(5 - 2);
177
178 var isJsExecuted, $element;
179
180 // Async ahead
181 stop();
182
183 mw.loader.implement(
184 'test.implement',
185 function () {
186 start();
187
188 strictEqual( isJsExecuted, undefined, 'javascript not executed multiple times' );
189 isJsExecuted = true;
190
191 equal( mw.loader.getState( 'test.implement' ), 'loaded', 'module state is "loaded" while implement() is executing javascript' );
192
193 $element = $( '<div class="mw-test-loaderimplement">Foo bar</div>' ).appendTo( '#qunit-fixture' );
194
195 // @broken: equal( $element.css( 'text-align' ),'center', 'CSS stylesheet was applied in time. ("text-align: center")' );
196
197 // Marked broken due to a bug in qunit/index.html, via Special:JavaScriptTest/qunit it works fine
198
199 // CSS @import is easily broken when concatenating stylesheets (bug 34669)
200 // @broken: equal( $element.css( 'float' ), 'right', 'CSS stylesheet was applied in time via @import (bug 34669). ("float: right")' );
201
202 equal( mw.msg( 'test-foobar' ), 'Hello Foobar, $1!', 'Messages are loaded in time' );
203
204 },
205 {
206 "all": "@import url('"
207 + QUnit.fixurl( mw.config.get( 'wgScriptPath' )
208 + '/tests/qunit/data/styleTest.css.php?'
209 + $.param({
210 selector: '.mw-test-loaderimplement',
211 prop: 'float',
212 val: 'right'
213 }) )
214 + "');\n"
215 + '.mw-test-loaderimplement { text-align: center; }'
216 },
217 {
218 "test-foobar": "Hello Foobar, $1!"
219 }
220 );
221
222 mw.loader.load( 'test.implement' );
223
224 });
225
226 test( 'mw.loader bug29107' , function () {
227 expect(2);
228
229 // Message doesn't exist already
230 ok( !mw.messages.exists( 'bug29107' ) );
231
232 // Async! Failure in this test may lead to neither the success nor error callbacks getting called.
233 // Due to QUnit's timeout feauture we won't hang here forever if this happends.
234 stop();
235
236 mw.loader.implement( 'bug29107.messages-only', [], {}, {'bug29107': 'loaded'} );
237 mw.loader.using( 'bug29107.messages-only', function() {
238 start();
239 ok( mw.messages.exists( 'bug29107' ), 'Bug 29107: messages-only module should implement ok' );
240 }, function() {
241 start();
242 ok( false, 'Error callback fired while implementing "bug29107.messages-only" module' );
243 });
244 });
245
246 test( 'mw.loader.bug30825', function() {
247 // This bug was actually already fixed in 1.18 and later when discovered in 1.17.
248 // Test is for regressions!
249
250 expect(2);
251
252 // Forge an URL to the test callback script
253 var target = QUnit.fixurl(
254 mw.config.get( 'wgServer' ) + mw.config.get( 'wgScriptPath' ) + '/tests/qunit/data/qunitOkCall.js'
255 );
256
257 // Confirm that mw.loader.load() works with protocol-relative URLs
258 target = target.replace( /https?:/, '' );
259
260 equal( target.substr( 0, 2 ), '//',
261 'URL must be relative to test relative URLs!'
262 );
263
264 // Async!
265 stop();
266 mw.loader.load( target );
267 });
268
269 test( 'mw.html', function () {
270 expect(13);
271
272 raises( function () {
273 mw.html.escape();
274 }, TypeError, 'html.escape throws a TypeError if argument given is not a string' );
275
276 equal( mw.html.escape( '<mw awesome="awesome" value=\'test\' />' ),
277 '&lt;mw awesome=&quot;awesome&quot; value=&#039;test&#039; /&gt;', 'escape() escapes special characters to html entities' );
278
279 equal( mw.html.element(),
280 '<undefined/>', 'element() always returns a valid html string (even without arguments)' );
281
282 equal( mw.html.element( 'div' ), '<div/>', 'element() Plain DIV (simple)' );
283
284 equal( mw.html.element( 'div', {}, '' ), '<div></div>', 'element() Basic DIV (simple)' );
285
286 equal(
287 mw.html.element(
288 'div', {
289 id: 'foobar'
290 }
291 ),
292 '<div id="foobar"/>',
293 'html.element DIV (attribs)' );
294
295 equal( mw.html.element( 'p', null, 12 ), '<p>12</p>', 'Numbers are valid content and should be casted to a string' );
296
297 equal( mw.html.element( 'p', { title: 12 }, '' ), '<p title="12"></p>', 'Numbers are valid attribute values' );
298
299 // Example from https://www.mediawiki.org/wiki/ResourceLoader/Default_modules#mediaWiki.html
300 equal(
301 mw.html.element(
302 'div',
303 {},
304 new mw.html.Raw(
305 mw.html.element( 'img', { src: '<' } )
306 )
307 ),
308 '<div><img src="&lt;"/></div>',
309 'Raw inclusion of another element'
310 );
311
312 equal(
313 mw.html.element(
314 'option', {
315 selected: true
316 }, 'Foo'
317 ),
318 '<option selected="selected">Foo</option>',
319 'Attributes may have boolean values. True copies the attribute name to the value.'
320 );
321
322 equal(
323 mw.html.element(
324 'option', {
325 value: 'foo',
326 selected: false
327 }, 'Foo'
328 ),
329 '<option value="foo">Foo</option>',
330 'Attributes may have boolean values. False keeps the attribute from output.'
331 );
332
333 equal( mw.html.element( 'div',
334 null, 'a' ),
335 '<div>a</div>',
336 'html.element DIV (content)' );
337
338 equal( mw.html.element( 'a',
339 { href: 'http://mediawiki.org/w/index.php?title=RL&action=history' }, 'a' ),
340 '<a href="http://mediawiki.org/w/index.php?title=RL&amp;action=history">a</a>',
341 'html.element DIV (attribs + content)' );
342
343 });