Localisation updates for core and extension messages from translatewiki.net (2011...
[lhc/web/wiklou.git] / resources / test / unit / 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( 'addCSS', function(){
22
23 var a = mw.util.addCSS( '#bodyContent { visibility: hidden; }' );
24 ok( a, 'function works' );
25 deepEqual( a.disabled, false, 'property "disabled" is available and set to false' );
26
27 var $b = $('#bodyContent');
28 equal( $b.css('visibility'), 'hidden', 'Added style properties are in effect.' );
29
30
31 });
32
33 test( 'toggleToc', function(){
34
35 ok( mw.util.toggleToc );
36
37 });
38
39 test( 'wikiGetlink', function(){
40
41 // Not part of startUp module
42 mw.config.set( 'wgArticlePath', '/wiki/$1' );
43
44 var hrefA = mw.util.wikiGetlink( 'Sandbox' );
45
46 equal( hrefA, '/wiki/Sandbox', 'Simple title; Get link for "Sandbox"' );
47
48 var hrefB = mw.util.wikiGetlink( 'Foo:Sandbox ? 5+5=10 ! (test)/subpage' );
49
50 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"' );
51
52 });
53
54 test( 'getParamValue', function(){
55
56 var url = 'http://mediawiki.org/?foo=wrong&foo=right#&foo=bad';
57
58 equal( mw.util.getParamValue( 'foo', url ), 'right', 'Use latest one, ignore hash' );
59 deepEqual( mw.util.getParamValue( 'bar', url ), null, 'Return null when not found' );
60
61 });
62
63 test( 'getActionFrom', function(){
64
65 // Example urls
66 var urlA = 'http://mediawiki.org/wiki/Article',
67 urlB = 'http://mediawiki.org/w/index.php?title=Article&action=edit',
68 urlC = 'http://mediawiki.org/edit/Article',
69 urlD = 'http://mediawiki.org/w/index.php/Article';
70
71 // Common settings
72 mw.config.set( {
73 'wgActionPaths': [],
74 'wgArticlePath': '/wiki/$1'
75 });
76
77 equal( mw.util.getActionFrom( urlA ), 'view', 'wgArticlePath (/wiki/$1) support' );
78 equal( mw.util.getActionFrom( urlB ), 'edit', 'action-parameter support' );
79
80 // Custom settings
81 mw.config.set( 'wgActionPaths', {
82 'view': '/view/$1',
83 'edit': '/edit/$1'
84 });
85
86 equal( mw.util.getActionFrom( urlC ), 'edit', 'wgActionPaths support' );
87
88 // Default settings
89 mw.config.set( {
90 'wgActionPaths': [],
91 'wgArticlePath': '/w/index.php/$1'
92 });
93 equal( mw.util.getActionFrom( urlD ), 'view', 'wgArticlePath (/index.php/$1) support' );
94
95 });
96
97 test( 'getTitleFrom', function(){
98
99 // Example urls
100 var urlA = 'http://mediawiki.org/wiki/Article',
101 urlB = 'http://mediawiki.org/w/index.php?title=Article&action=edit',
102 urlC = 'http://mediawiki.org/edit/Article',
103 urlD = 'http://mediawiki.org/w/index.php/Article';
104
105 // Common settings
106 mw.config.set( {
107 'wgActionPaths': [],
108 'wgArticlePath': '/wiki/$1'
109 });
110
111 equal( mw.util.getTitleFrom( urlA ), 'Article', 'wgArticlePath (/wiki/$1) support' );
112 equal( mw.util.getTitleFrom( urlB ), 'Article', 'action-parameter support' );
113
114 // Custom settings
115 mw.config.set( 'wgActionPaths', {
116 'view': '/view/$1',
117 'edit': '/edit/$1'
118 });
119
120 equal( mw.util.getTitleFrom( urlC ), 'Article', 'wgActionPaths support' );
121
122 // Default settings
123 mw.config.set( {
124 'wgActionPaths': [],
125 'wgArticlePath': '/w/index.php/$1'
126 });
127
128 equal( mw.util.getTitleFrom( urlD ), 'Article', 'wgArticlePath (/index.php/$1) support' );
129
130 });
131
132 test( 'tooltipAccessKey', function(){
133
134 equal( typeof mw.util.tooltipAccessKeyPrefix, 'string', 'mw.util.tooltipAccessKeyPrefix must be a string' );
135 ok( mw.util.tooltipAccessKeyRegexp instanceof RegExp, 'mw.util.tooltipAccessKeyRegexp instance of RegExp' );
136 ok( mw.util.updateTooltipAccessKeys, 'mw.util.updateTooltipAccessKeys' );
137
138 });
139
140 test( '$content', function(){
141
142 ok( mw.util.$content instanceof jQuery, 'mw.util.$content instance of jQuery' );
143 deepEqual( mw.util.$content.length, 1, 'mw.util.$content must have length of 1' );
144
145 });
146
147 test( 'addPortletLink', function(){
148
149 var a = mw.util.addPortletLink( 'p-tb', 'http://mediawiki.org/wiki/ResourceLoader', 'ResourceLoader', 't-rl', 'More info about ResourceLoader on MediaWiki.org ', 'l', '#t-specialpages' );
150
151 ok( $.isDomElement(a), 'addPortletLink returns a DomElement' );
152
153 var b = mw.util.addPortletLink( "p-tb", "http://mediawiki.org/", "MediaWiki.org", "t-mworg", "Go to MediaWiki.org ", "m", "#t-rl" );
154
155 equal( $(a).text(), 'ResourceLoader', 'Link contains correct text' );
156 equal( $(b).next().text(), 'ResourceLoader', 'Link was inserted in correct nextnode position' );
157
158 });
159
160 test( 'jsMessage', function(){
161
162 var a = mw.util.jsMessage( "MediaWiki is <b>Awesome</b>." );
163
164 ok( a, 'Basic return value checking' );
165
166 });
167
168 test( 'validateEmail', function(){
169
170 deepEqual( mw.util.validateEmail( "" ), null, 'Empty string should return null' );
171 deepEqual( mw.util.validateEmail( "user@localhost" ), true );
172
173 // testEmailWithCommasAreInvalids
174 deepEqual( mw.util.validateEmail( "user,foo@example.org" ), false, 'Comma' );
175 deepEqual( mw.util.validateEmail( "userfoo@ex,ample.org" ), false, 'Comma' );
176
177 // testEmailWithHyphens
178 deepEqual( mw.util.validateEmail( "user-foo@example.org" ), true, 'Hyphen' );
179 deepEqual( mw.util.validateEmail( "userfoo@ex-ample.org" ), true, 'Hyphen' );
180
181 });
182
183 test( 'isIPv6Address', function(){
184
185 // Based on IPTest.php > IPv6
186 deepEqual( mw.util.isIPv6Address( "" ), false, 'Empty string is not an IP' );
187 deepEqual( mw.util.isIPv6Address( ":fc:100::" ), false, 'IPv6 starting with lone ":"' );
188 deepEqual( mw.util.isIPv6Address( "fc:100::" ), true );
189 deepEqual( mw.util.isIPv6Address( "fc:100:a:d:1:e:ac::" ), true );
190 deepEqual( mw.util.isIPv6Address( ":::" ), false );
191 deepEqual( mw.util.isIPv6Address( "::0:" ), false );
192
193 });
194
195 test( 'isIPv4Address', function(){
196
197 // Based on IPTest.php > IPv4
198 deepEqual( mw.util.isIPv4Address( "" ), false, 'Empty string is not an IP' );
199 deepEqual( mw.util.isIPv4Address( "...." ), false );
200 deepEqual( mw.util.isIPv4Address( "1.24.52.13" ), true );
201
202 });