Merge "(Bug 44192) Do not attempt to send a real e-mail in ApiAccountCreationTest"
[lhc/web/wiklou.git] / tests / qunit / suites / resources / mediawiki / mediawiki.util.test.js
1 ( function ( mw, $ ) {
2 QUnit.module( 'mediawiki.util', QUnit.newMwEnvironment() );
3
4 QUnit.test( 'rawurlencode', 1, function ( assert ) {
5 assert.equal( mw.util.rawurlencode( 'Test:A & B/Here' ), 'Test%3AA%20%26%20B%2FHere' );
6 } );
7
8 QUnit.test( 'wikiUrlencode', 1, function ( assert ) {
9 assert.equal( mw.util.wikiUrlencode( 'Test:A & B/Here' ), 'Test:A_%26_B/Here' );
10 } );
11
12 QUnit.test( 'wikiGetlink', 3, function ( assert ) {
13 // Not part of startUp module
14 mw.config.set( 'wgArticlePath', '/wiki/$1' );
15 mw.config.set( 'wgPageName', 'Foobar' );
16
17 var href = mw.util.wikiGetlink( 'Sandbox' );
18 assert.equal( href, '/wiki/Sandbox', 'Simple title; Get link for "Sandbox"' );
19
20 href = mw.util.wikiGetlink( 'Foo:Sandbox ? 5+5=10 ! (test)/subpage' );
21 assert.equal( href, '/wiki/Foo:Sandbox_%3F_5%2B5%3D10_%21_%28test%29/subpage',
22 'Advanced title; Get link for "Foo:Sandbox ? 5+5=10 ! (test)/subpage"' );
23
24 href = mw.util.wikiGetlink();
25 assert.equal( href, '/wiki/Foobar', 'Default title; Get link for current page ("Foobar")' );
26 } );
27
28 QUnit.test( 'wikiScript', 4, function ( assert ) {
29 mw.config.set( {
30 'wgScript': '/w/i.php', // customized wgScript for bug 39103
31 'wgLoadScript': '/w/l.php', // customized wgLoadScript for bug 39103
32 'wgScriptPath': '/w',
33 'wgScriptExtension': '.php'
34 } );
35
36 assert.equal( mw.util.wikiScript(), mw.config.get( 'wgScript' ),
37 'wikiScript() returns wgScript'
38 );
39 assert.equal( mw.util.wikiScript( 'index' ), mw.config.get( 'wgScript' ),
40 'wikiScript( index ) returns wgScript'
41 );
42 assert.equal( mw.util.wikiScript( 'load' ), mw.config.get( 'wgLoadScript' ),
43 'wikiScript( load ) returns wgLoadScript'
44 );
45 assert.equal( mw.util.wikiScript( 'api' ), '/w/api.php', 'API path' );
46 } );
47
48 QUnit.test( 'addCSS', 3, function ( assert ) {
49 var $el, style;
50 $el = $( '<div>' ).attr( 'id', 'mw-addcsstest' ).appendTo( '#qunit-fixture' );
51
52 style = mw.util.addCSS( '#mw-addcsstest { visibility: hidden; }' );
53 assert.equal( typeof style, 'object', 'addCSS returned an object' );
54 assert.strictEqual( style.disabled, false, 'property "disabled" is available and set to false' );
55
56 assert.equal( $el.css( 'visibility' ), 'hidden', 'Added style properties are in effect' );
57
58 // Clean up
59 $( style.ownerNode ).remove();
60 } );
61
62 QUnit.asyncTest( 'toggleToc', 4, function ( assert ) {
63 var tocHtml, $toggleLink;
64
65 function actionC() {
66 QUnit.start();
67 }
68
69 function actionB() {
70 assert.strictEqual( mw.util.toggleToc( $toggleLink, actionC ), true, 'Return boolean true if the TOC is now visible.' );
71 }
72
73 function actionA() {
74 assert.strictEqual( mw.util.toggleToc( $toggleLink, actionB ), false, 'Return boolean false if the TOC is now hidden.' );
75 }
76
77 assert.strictEqual( mw.util.toggleToc(), null, 'Return null if there is no table of contents on the page.' );
78
79 tocHtml = '<table id="toc" class="toc"><tr><td>' +
80 '<div id="toctitle">' +
81 '<h2>Contents</h2>' +
82 '<span class="toctoggle">&nbsp;[<a href="#" class="internal" id="togglelink">Hide</a>&nbsp;]</span>' +
83 '</div>' +
84 '<ul><li></li></ul>' +
85 '</td></tr></table>';
86 $( tocHtml ).appendTo( '#qunit-fixture' ),
87 $toggleLink = $( '#togglelink' );
88
89 assert.strictEqual( $toggleLink.length, 1, 'Toggle link is appended to the page.' );
90
91 actionA();
92 } );
93
94 QUnit.test( 'getParamValue', 5, function ( assert ) {
95 var url;
96
97 url = 'http://example.org/?foo=wrong&foo=right#&foo=bad';
98 assert.equal( mw.util.getParamValue( 'foo', url ), 'right', 'Use latest one, ignore hash' );
99 assert.strictEqual( mw.util.getParamValue( 'bar', url ), null, 'Return null when not found' );
100
101 url = 'http://example.org/#&foo=bad';
102 assert.strictEqual( mw.util.getParamValue( 'foo', url ), null, 'Ignore hash if param is not in querystring but in hash (bug 27427)' );
103
104 url = 'example.org?' + $.param( { 'TEST': 'a b+c' } );
105 assert.strictEqual( mw.util.getParamValue( 'TEST', url ), 'a b+c', 'Bug 30441: getParamValue must understand "+" encoding of space' );
106
107 url = 'example.org?' + $.param( { 'TEST': 'a b+c d' } ); // check for sloppy code from r95332 :)
108 assert.strictEqual( mw.util.getParamValue( 'TEST', url ), 'a b+c d', 'Bug 30441: getParamValue must understand "+" encoding of space (multiple spaces)' );
109 } );
110
111 QUnit.test( 'tooltipAccessKey', 3, function ( assert ) {
112 assert.equal( typeof mw.util.tooltipAccessKeyPrefix, 'string', 'mw.util.tooltipAccessKeyPrefix must be a string' );
113 assert.ok( mw.util.tooltipAccessKeyRegexp instanceof RegExp, 'mw.util.tooltipAccessKeyRegexp instance of RegExp' );
114 assert.ok( mw.util.updateTooltipAccessKeys, 'mw.util.updateTooltipAccessKeys' );
115 } );
116
117 QUnit.test( '$content', 2, function ( assert ) {
118 assert.ok( mw.util.$content instanceof jQuery, 'mw.util.$content instance of jQuery' );
119 assert.strictEqual( mw.util.$content.length, 1, 'mw.util.$content must have length of 1' );
120 } );
121
122 /**
123 * Portlet names are prefixed with 'p-test' to avoid conflict with core
124 * when running the test suite under a wiki page.
125 * Previously, test elements where invisible to the selector since only
126 * one element can have a given id.
127 */
128 QUnit.test( 'addPortletLink', 8, function ( assert ) {
129 var pTestTb, pCustom, vectorTabs, tbRL, cuQuux, $cuQuux, tbMW, $tbMW, tbRLDM, caFoo;
130
131 pTestTb = '\
132 <div class="portlet" id="p-test-tb">\
133 <h5>Toolbox</h5>\
134 <ul class="body"></ul>\
135 </div>';
136 pCustom = '\
137 <div class="portlet" id="p-test-custom">\
138 <h5>Views</h5>\
139 <ul class="body">\
140 <li id="c-foo"><a href="#">Foo</a></li>\
141 <li id="c-barmenu">\
142 <ul>\
143 <li id="c-bar-baz"><a href="#">Baz</a></a>\
144 </ul>\
145 </li>\
146 </ul>\
147 </div>';
148 vectorTabs = '\
149 <div id="p-test-views" class="vectorTabs">\
150 <h5>Views</h5>\
151 <ul></ul>\
152 </div>';
153
154 $( '#qunit-fixture' ).append( pTestTb, pCustom, vectorTabs );
155
156 tbRL = mw.util.addPortletLink( 'p-test-tb', '//mediawiki.org/wiki/ResourceLoader',
157 'ResourceLoader', 't-rl', 'More info about ResourceLoader on MediaWiki.org ', 'l' );
158
159 assert.ok( $.isDomElement( tbRL ), 'addPortletLink returns a valid DOM Element according to $.isDomElement' );
160
161 tbMW = mw.util.addPortletLink( 'p-test-tb', '//mediawiki.org/',
162 'MediaWiki.org', 't-mworg', 'Go to MediaWiki.org ', 'm', tbRL );
163 $tbMW = $( tbMW );
164
165
166 assert.equal( $tbMW.attr( 'id' ), 't-mworg', 'Link has correct ID set' );
167 assert.equal( $tbMW.closest( '.portlet' ).attr( 'id' ), 'p-test-tb', 'Link was inserted within correct portlet' );
168 assert.equal( $tbMW.next().attr( 'id' ), 't-rl', 'Link is in the correct position (by passing nextnode)' );
169
170 cuQuux = mw.util.addPortletLink( 'p-test-custom', '#', 'Quux' );
171 $cuQuux = $( cuQuux );
172
173 assert.equal(
174 $( '#p-test-custom #c-barmenu ul li' ).length,
175 1,
176 'addPortletLink did not add the item to all <ul> elements in the portlet (bug 35082)'
177 );
178
179 tbRLDM = mw.util.addPortletLink( 'p-test-tb', '//mediawiki.org/wiki/RL/DM',
180 'Default modules', 't-rldm', 'List of all default modules ', 'd', '#t-rl' );
181
182 assert.equal( $( tbRLDM ).next().attr( 'id' ), 't-rl', 'Link is in the correct position (by passing CSS selector)' );
183
184 caFoo = mw.util.addPortletLink( 'p-test-views', '#', 'Foo' );
185
186 assert.strictEqual( $tbMW.find( 'span' ).length, 0, 'No <span> element should be added for porlets without vectorTabs class.' );
187 assert.strictEqual( $( caFoo ).find( 'span' ).length, 1, 'A <span> element should be added for porlets with vectorTabs class.' );
188 } );
189
190 QUnit.test( 'jsMessage', 1, function ( assert ) {
191 var a = mw.util.jsMessage( 'MediaWiki is <b>Awesome</b>.' );
192 assert.ok( a, 'Basic checking of return value' );
193
194 // Clean up
195 $( '#mw-js-message' ).remove();
196 } );
197
198 QUnit.test( 'validateEmail', 6, function ( assert ) {
199 assert.strictEqual( mw.util.validateEmail( '' ), null, 'Should return null for empty string ' );
200 assert.strictEqual( mw.util.validateEmail( 'user@localhost' ), true, 'Return true for a valid e-mail address' );
201
202 // testEmailWithCommasAreInvalids
203 assert.strictEqual( mw.util.validateEmail( 'user,foo@example.org' ), false, 'Emails with commas are invalid' );
204 assert.strictEqual( mw.util.validateEmail( 'userfoo@ex,ample.org' ), false, 'Emails with commas are invalid' );
205
206 // testEmailWithHyphens
207 assert.strictEqual( mw.util.validateEmail( 'user-foo@example.org' ), true, 'Emails may contain a hyphen' );
208 assert.strictEqual( mw.util.validateEmail( 'userfoo@ex-ample.org' ), true, 'Emails may contain a hyphen' );
209 } );
210
211 QUnit.test( 'isIPv6Address', 40, function ( assert ) {
212 // Shortcuts
213 function assertFalseIPv6( addy, summary ) {
214 return assert.strictEqual( mw.util.isIPv6Address( addy ), false, summary );
215 }
216
217 function assertTrueIPv6( addy, summary ) {
218 return assert.strictEqual( mw.util.isIPv6Address( addy ), true, summary );
219 }
220
221 // Based on IPTest.php > testisIPv6
222 assertFalseIPv6( ':fc:100::', 'IPv6 starting with lone ":"' );
223 assertFalseIPv6( 'fc:100:::', 'IPv6 ending with a ":::"' );
224 assertFalseIPv6( 'fc:300', 'IPv6 with only 2 words' );
225 assertFalseIPv6( 'fc:100:300', 'IPv6 with only 3 words' );
226
227 $.each(
228 ['fc:100::',
229 'fc:100:a::',
230 'fc:100:a:d::',
231 'fc:100:a:d:1::',
232 'fc:100:a:d:1:e::',
233 'fc:100:a:d:1:e:ac::'], function ( i, addy ) {
234 assertTrueIPv6( addy, addy + ' is a valid IP' );
235 } );
236
237 assertFalseIPv6( 'fc:100:a:d:1:e:ac:0::', 'IPv6 with 8 words ending with "::"' );
238 assertFalseIPv6( 'fc:100:a:d:1:e:ac:0:1::', 'IPv6 with 9 words ending with "::"' );
239
240 assertFalseIPv6( ':::' );
241 assertFalseIPv6( '::0:', 'IPv6 ending in a lone ":"' );
242
243 assertTrueIPv6( '::', 'IPv6 zero address' );
244 $.each(
245 ['::0',
246 '::fc',
247 '::fc:100',
248 '::fc:100:a',
249 '::fc:100:a:d',
250 '::fc:100:a:d:1',
251 '::fc:100:a:d:1:e',
252 '::fc:100:a:d:1:e:ac',
253
254 'fc:100:a:d:1:e:ac:0'], function ( i, addy ) {
255 assertTrueIPv6( addy, addy + ' is a valid IP' );
256 } );
257
258 assertFalseIPv6( '::fc:100:a:d:1:e:ac:0', 'IPv6 with "::" and 8 words' );
259 assertFalseIPv6( '::fc:100:a:d:1:e:ac:0:1', 'IPv6 with 9 words' );
260
261 assertFalseIPv6( ':fc::100', 'IPv6 starting with lone ":"' );
262 assertFalseIPv6( 'fc::100:', 'IPv6 ending with lone ":"' );
263 assertFalseIPv6( 'fc:::100', 'IPv6 with ":::" in the middle' );
264
265 assertTrueIPv6( 'fc::100', 'IPv6 with "::" and 2 words' );
266 assertTrueIPv6( 'fc::100:a', 'IPv6 with "::" and 3 words' );
267 assertTrueIPv6( 'fc::100:a:d', 'IPv6 with "::" and 4 words' );
268 assertTrueIPv6( 'fc::100:a:d:1', 'IPv6 with "::" and 5 words' );
269 assertTrueIPv6( 'fc::100:a:d:1:e', 'IPv6 with "::" and 6 words' );
270 assertTrueIPv6( 'fc::100:a:d:1:e:ac', 'IPv6 with "::" and 7 words' );
271 assertTrueIPv6( '2001::df', 'IPv6 with "::" and 2 words' );
272 assertTrueIPv6( '2001:5c0:1400:a::df', 'IPv6 with "::" and 5 words' );
273 assertTrueIPv6( '2001:5c0:1400:a::df:2', 'IPv6 with "::" and 6 words' );
274
275 assertFalseIPv6( 'fc::100:a:d:1:e:ac:0', 'IPv6 with "::" and 8 words' );
276 assertFalseIPv6( 'fc::100:a:d:1:e:ac:0:1', 'IPv6 with 9 words' );
277 } );
278
279 QUnit.test( 'isIPv4Address', 11, function ( assert ) {
280 // Shortcuts
281 function assertFalseIPv4( addy, summary ) {
282 assert.strictEqual( mw.util.isIPv4Address( addy ), false, summary );
283 }
284
285 function assertTrueIPv4( addy, summary ) {
286 assert.strictEqual( mw.util.isIPv4Address( addy ), true, summary );
287 }
288
289 // Based on IPTest.php > testisIPv4
290 assertFalseIPv4( false, 'Boolean false is not an IP' );
291 assertFalseIPv4( true, 'Boolean true is not an IP' );
292 assertFalseIPv4( '', 'Empty string is not an IP' );
293 assertFalseIPv4( 'abc', '"abc" is not an IP' );
294 assertFalseIPv4( ':', 'Colon is not an IP' );
295 assertFalseIPv4( '124.24.52', 'IPv4 not enough quads' );
296 assertFalseIPv4( '24.324.52.13', 'IPv4 out of range' );
297 assertFalseIPv4( '.24.52.13', 'IPv4 starts with period' );
298
299 assertTrueIPv4( '124.24.52.13', '124.24.52.134 is a valid IP' );
300 assertTrueIPv4( '1.24.52.13', '1.24.52.13 is a valid IP' );
301 assertFalseIPv4( '74.24.52.13/20', 'IPv4 ranges are not recogzized as valid IPs' );
302 } );
303 }( mediaWiki, jQuery ) );