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