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