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