8bb004f1e48c89b7180697359e1ec900f109f87a
[lhc/web/wiklou.git] / tests / qunit / suites / resources / mediawiki.util / mediawiki.util.js
1 module( 'mediawiki.util.js' );
2
3 test( '-- Initial check', function(){
4
5 ok( mw.util, 'mw.util defined' );
6
7 });
8
9 test( 'rawurlencode', function(){
10
11 equal( mw.util.rawurlencode( 'Test:A & B/Here' ), 'Test%3AA%20%26%20B%2FHere' );
12
13 });
14
15 test( 'wikiUrlencode', function(){
16
17 equal( mw.util.wikiUrlencode( 'Test:A & B/Here' ), 'Test:A_%26_B/Here' );
18
19 });
20
21 test( 'wikiGetlink', function(){
22
23 // Not part of startUp module
24 mw.config.set( 'wgArticlePath', '/wiki/$1' );
25
26 var hrefA = mw.util.wikiGetlink( 'Sandbox' );
27
28 equal( hrefA, '/wiki/Sandbox', 'Simple title; Get link for "Sandbox"' );
29
30 var hrefB = mw.util.wikiGetlink( 'Foo:Sandbox ? 5+5=10 ! (test)/subpage' );
31
32 equal( hrefB, '/wiki/Foo:Sandbox_%3F_5%2B5%3D10_%21_%28test%29/subpage', 'Advanced title; Get link for "Foo:Sandbox ? 5+5=10 ! (test)/subpage"' );
33
34 });
35
36 test( 'wikiScript', function(){
37
38 mw.config.set({
39 'wgScript': '/w/index.php',
40 'wgScriptPath': '/w',
41 'wgScriptExtension': '.php',
42 });
43
44 equal( mw.util.wikiScript(), mw.config.get( 'wgScript' ), 'Defaults to index.php and is equal to wgScript.' );
45
46 });
47
48 test( 'addCSS', function(){
49
50 var a = mw.util.addCSS( '#bodyContent { visibility: hidden; }' );
51 ok( a, 'function works' );
52 deepEqual( a.disabled, false, 'property "disabled" is available and set to false' );
53
54 var $b = $('#bodyContent');
55 equal( $b.css('visibility'), 'hidden', 'Added style properties are in effect.' );
56
57
58 });
59
60 test( 'toggleToc', function(){
61
62 ok( mw.util.toggleToc );
63
64 });
65
66 test( 'getParamValue', function(){
67
68 var url = 'http://mediawiki.org/?foo=wrong&foo=right#&foo=bad';
69
70 equal( mw.util.getParamValue( 'foo', url ), 'right', 'Use latest one, ignore hash' );
71 deepEqual( mw.util.getParamValue( 'bar', url ), null, 'Return null when not found' );
72
73 });
74
75 test( 'getActionFrom', function(){
76
77 // Example urls
78 var urlA = 'http://mediawiki.org/wiki/Article',
79 urlB = 'http://mediawiki.org/w/index.php?title=Article&action=edit',
80 urlC = 'http://mediawiki.org/edit/Article',
81 urlD = 'http://mediawiki.org/w/index.php/Article';
82
83 // Common settings
84 mw.config.set( {
85 'wgActionPaths': [],
86 'wgArticlePath': '/wiki/$1'
87 });
88
89 equal( mw.util.getActionFrom( urlA ), 'view', 'wgArticlePath (/wiki/$1) support' );
90 equal( mw.util.getActionFrom( urlB ), 'edit', 'action-parameter support' );
91
92 // Custom settings
93 mw.config.set( 'wgActionPaths', {
94 'view': '/view/$1',
95 'edit': '/edit/$1'
96 });
97
98 equal( mw.util.getActionFrom( urlB ), 'edit', 'action-parameter support with action path' );
99 equal( mw.util.getActionFrom( urlC ), 'edit', 'wgActionPaths support' );
100
101 // Default settings
102 mw.config.set( {
103 'wgActionPaths': [],
104 'wgArticlePath': '/w/index.php/$1'
105 });
106 equal( mw.util.getActionFrom( urlB ), 'edit', 'action-parameter support with default settings' );
107 equal( mw.util.getActionFrom( urlD ), 'view', 'wgArticlePath (/index.php/$1) support' );
108
109 });
110
111 test( 'getTitleFrom', function(){
112
113 // Example urls
114 var urlA = 'http://mediawiki.org/wiki/Article',
115 urlB = 'http://mediawiki.org/w/index.php?title=Article&action=edit',
116 urlC = 'http://mediawiki.org/edit/Article',
117 urlD = 'http://mediawiki.org/w/index.php/Article';
118
119 // Common settings
120 mw.config.set( {
121 'wgActionPaths': [],
122 'wgArticlePath': '/wiki/$1'
123 });
124
125 equal( mw.util.getTitleFrom( urlA ), 'Article', 'wgArticlePath (/wiki/$1) support' );
126 equal( mw.util.getTitleFrom( urlB ), 'Article', 'title-parameter support' );
127
128 // Custom settings
129 mw.config.set( 'wgActionPaths', {
130 'view': '/view/$1',
131 'edit': '/edit/$1'
132 });
133
134 equal( mw.util.getTitleFrom( urlB ), 'Article', 'title-parameter support with action path' );
135 equal( mw.util.getTitleFrom( urlC ), 'Article', 'wgActionPaths support' );
136
137 // Default settings
138 mw.config.set( {
139 'wgActionPaths': [],
140 'wgArticlePath': '/w/index.php/$1'
141 });
142
143 equal( mw.util.getTitleFrom( urlB ), 'Article', 'title-parameter support with default settings' );
144 equal( mw.util.getTitleFrom( urlD ), 'Article', 'wgArticlePath (/index.php/$1) support' );
145
146 });
147
148 test( 'tooltipAccessKey', function(){
149
150 equal( typeof mw.util.tooltipAccessKeyPrefix, 'string', 'mw.util.tooltipAccessKeyPrefix must be a string' );
151 ok( mw.util.tooltipAccessKeyRegexp instanceof RegExp, 'mw.util.tooltipAccessKeyRegexp instance of RegExp' );
152 ok( mw.util.updateTooltipAccessKeys, 'mw.util.updateTooltipAccessKeys' );
153
154 });
155
156 test( '$content', function(){
157
158 ok( mw.util.$content instanceof jQuery, 'mw.util.$content instance of jQuery' );
159 deepEqual( mw.util.$content.length, 1, 'mw.util.$content must have length of 1' );
160
161 });
162
163 test( 'addPortletLink', function(){
164
165 var A = mw.util.addPortletLink( 'p-tb', 'http://mediawiki.org/wiki/ResourceLoader',
166 'ResourceLoader', 't-rl', 'More info about ResourceLoader on MediaWiki.org ', 'l', '#t-specialpages' );
167
168 ok( $.isDomElement( A ), 'addPortletLink returns a valid DOM Element according to $.isDomElement' );
169
170 var B = mw.util.addPortletLink( "p-tb", "http://mediawiki.org/",
171 "MediaWiki.org", "t-mworg", "Go to MediaWiki.org ", "m", A );
172
173 equal( $( B ).attr( 'id' ), 't-mworg', 'Link has correct ID set' );
174 equal( $( B ).closest( '.portal' ).attr( 'id' ), 'p-tb', 'Link was inserted within correct portlet' );
175 equal( $( B ).next().attr( 'id' ), 't-rl', 'Link is in the correct position (by passing nextnode)' );
176
177 var C = mw.util.addPortletLink( "p-tb", "http://mediawiki.org/wiki/RL/DM",
178 "Default modules", "t-rldm", "List of all default modules ", "d", "#t-rl" );
179
180 equal( $( C ).next().attr( 'id' ), 't-rl', 'Link is in the correct position (by passing CSS selector)' );
181
182 // Clean up
183 $( [A, B, C] ).remove();
184
185 });
186
187 test( 'jsMessage', function(){
188
189 var a = mw.util.jsMessage( "MediaWiki is <b>Awesome</b>." );
190
191 ok( a, 'Basic checking of return value' );
192
193 // Clean up
194 $( '#mw-js-message' ).remove();
195
196 });
197
198 test( 'validateEmail', function(){
199
200 deepEqual( mw.util.validateEmail( "" ), null, 'Empty string should return null' );
201 deepEqual( mw.util.validateEmail( "user@localhost" ), true );
202
203 // testEmailWithCommasAreInvalids
204 deepEqual( mw.util.validateEmail( "user,foo@example.org" ), false, 'Comma' );
205 deepEqual( mw.util.validateEmail( "userfoo@ex,ample.org" ), false, 'Comma' );
206
207 // testEmailWithHyphens
208 deepEqual( mw.util.validateEmail( "user-foo@example.org" ), true, 'Hyphen' );
209 deepEqual( mw.util.validateEmail( "userfoo@ex-ample.org" ), true, 'Hyphen' );
210
211 });
212
213 test( 'isIPv6Address', function(){
214
215 // Based on IPTest.php > IPv6
216 deepEqual( mw.util.isIPv6Address( "" ), false, 'Empty string is not an IP' );
217 deepEqual( mw.util.isIPv6Address( ":fc:100::" ), false, 'IPv6 starting with lone ":"' );
218 deepEqual( mw.util.isIPv6Address( "fc:100::" ), true );
219 deepEqual( mw.util.isIPv6Address( "fc:100:a:d:1:e:ac::" ), true );
220 deepEqual( mw.util.isIPv6Address( ":::" ), false );
221 deepEqual( mw.util.isIPv6Address( "::0:" ), false );
222
223 });
224
225 test( 'isIPv4Address', function(){
226
227 // Based on IPTest.php > IPv4
228 deepEqual( mw.util.isIPv4Address( "" ), false, 'Empty string is not an IP' );
229 deepEqual( mw.util.isIPv4Address( "...." ), false );
230 deepEqual( mw.util.isIPv4Address( "1.24.52.13" ), true );
231
232 });