Followup r106791, r107692 and bug 33014
[lhc/web/wiklou.git] / tests / phpunit / languages / LanguageTest.php
1 <?php
2
3 class LanguageTest extends MediaWikiTestCase {
4
5 /**
6 * @var Language
7 */
8 private $lang;
9
10 function setUp() {
11 $this->lang = Language::factory( 'en' );
12 }
13 function tearDown() {
14 unset( $this->lang );
15 }
16
17 function testLanguageConvertDoubleWidthToSingleWidth() {
18 $this->assertEquals(
19 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
20 $this->lang->normalizeForSearch(
21 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
22 ),
23 'convertDoubleWidth() with the full alphabet and digits'
24 );
25 }
26
27 /** @dataProvider provideFormattableTimes */
28 function testFormatTimePeriod( $seconds, $format, $expected, $desc ) {
29 $this->assertEquals( $expected, $this->lang->formatTimePeriod( $seconds, $format ), $desc );
30 }
31
32 function provideFormattableTimes() {
33 return array(
34 array( 9.45, array(), '9.5s', 'formatTimePeriod() rounding (<10s)' ),
35 array( 9.45, array( 'noabbrevs' => true ), '9.5 seconds', 'formatTimePeriod() rounding (<10s)' ),
36 array( 9.95, array(), '10s', 'formatTimePeriod() rounding (<10s)' ),
37 array( 9.95, array( 'noabbrevs' => true ), '10 seconds', 'formatTimePeriod() rounding (<10s)' ),
38 array( 59.55, array(), '1m 0s', 'formatTimePeriod() rounding (<60s)' ),
39 array( 59.55, array( 'noabbrevs' => true ), '1 minute 0 seconds', 'formatTimePeriod() rounding (<60s)' ),
40 array( 119.55, array(), '2m 0s', 'formatTimePeriod() rounding (<1h)' ),
41 array( 119.55, array( 'noabbrevs' => true ), '2 minutes 0 seconds', 'formatTimePeriod() rounding (<1h)' ),
42 array( 3599.55, array(), '1h 0m 0s', 'formatTimePeriod() rounding (<1h)' ),
43 array( 3599.55, array( 'noabbrevs' => true ), '1 hour 0 minutes 0 seconds', 'formatTimePeriod() rounding (<1h)' ),
44 array( 7199.55, array(), '2h 0m 0s', 'formatTimePeriod() rounding (>=1h)' ),
45 array( 7199.55, array( 'noabbrevs' => true ), '2 hours 0 minutes 0 seconds', 'formatTimePeriod() rounding (>=1h)' ),
46 array( 7199.55, 'avoidseconds', '2h 0m', 'formatTimePeriod() rounding (>=1h), avoidseconds' ),
47 array( 7199.55, array( 'avoid' => 'avoidseconds', 'noabbrevs' => true ), '2 hours 0 minutes', 'formatTimePeriod() rounding (>=1h), avoidseconds' ),
48 array( 7199.55, 'avoidminutes', '2h 0m', 'formatTimePeriod() rounding (>=1h), avoidminutes' ),
49 array( 7199.55, array( 'avoid' => 'avoidminutes', 'noabbrevs' => true ), '2 hours 0 minutes', 'formatTimePeriod() rounding (>=1h), avoidminutes' ),
50 array( 172799.55, 'avoidseconds', '48h 0m', 'formatTimePeriod() rounding (=48h), avoidseconds' ),
51 array( 172799.55, array( 'avoid' => 'avoidseconds', 'noabbrevs' => true ), '48 hours 0 minutes', 'formatTimePeriod() rounding (=48h), avoidseconds' ),
52 array( 259199.55, 'avoidminutes', '3d 0h', 'formatTimePeriod() rounding (>48h), avoidminutes' ),
53 array( 259199.55, array( 'avoid' => 'avoidminutes', 'noabbrevs' => true ), '3 days 0 hours', 'formatTimePeriod() rounding (>48h), avoidminutes' ),
54 array( 176399.55, 'avoidseconds', '2d 1h 0m', 'formatTimePeriod() rounding (>48h), avoidseconds' ),
55 array( 176399.55, array( 'avoid' => 'avoidseconds', 'noabbrevs' => true ), '2 days 1 hour 0 minutes', 'formatTimePeriod() rounding (>48h), avoidseconds' ),
56 array( 176399.55, 'avoidminutes', '2d 1h', 'formatTimePeriod() rounding (>48h), avoidminutes' ),
57 array( 176399.55, array( 'avoid' => 'avoidminutes', 'noabbrevs' => true ), '2 days 1 hour', 'formatTimePeriod() rounding (>48h), avoidminutes' ),
58 array( 259199.55, 'avoidseconds', '3d 0h 0m', 'formatTimePeriod() rounding (>48h), avoidseconds' ),
59 array( 259199.55, array( 'avoid' => 'avoidseconds', 'noabbrevs' => true ), '3 days 0 hours 0 minutes', 'formatTimePeriod() rounding (>48h), avoidseconds' ),
60 array( 172801.55, 'avoidseconds', '2d 0h 0m', 'formatTimePeriod() rounding, (>48h), avoidseconds' ),
61 array( 172801.55, array( 'avoid' => 'avoidseconds', 'noabbrevs' => true ), '2 days 0 hours 0 minutes', 'formatTimePeriod() rounding, (>48h), avoidseconds' ),
62 array( 176460.55, array(), '2d 1h 1m 1s', 'formatTimePeriod() rounding, recursion, (>48h)' ),
63 array( 176460.55, array( 'noabbrevs' => true ), '2 days 1 hour 1 minute 1 second', 'formatTimePeriod() rounding, recursion, (>48h)' ),
64 );
65
66 }
67
68 function testTruncate() {
69 $this->assertEquals(
70 "XXX",
71 $this->lang->truncate( "1234567890", 0, 'XXX' ),
72 'truncate prefix, len 0, small ellipsis'
73 );
74
75 $this->assertEquals(
76 "12345XXX",
77 $this->lang->truncate( "1234567890", 8, 'XXX' ),
78 'truncate prefix, small ellipsis'
79 );
80
81 $this->assertEquals(
82 "123456789",
83 $this->lang->truncate( "123456789", 5, 'XXXXXXXXXXXXXXX' ),
84 'truncate prefix, large ellipsis'
85 );
86
87 $this->assertEquals(
88 "XXX67890",
89 $this->lang->truncate( "1234567890", -8, 'XXX' ),
90 'truncate suffix, small ellipsis'
91 );
92
93 $this->assertEquals(
94 "123456789",
95 $this->lang->truncate( "123456789", -5, 'XXXXXXXXXXXXXXX' ),
96 'truncate suffix, large ellipsis'
97 );
98 }
99
100 /**
101 * @dataProvider provideHTMLTruncateData()
102 */
103 function testTruncateHtml( $len, $ellipsis, $input, $expected ) {
104 // Actual HTML...
105 $this->assertEquals(
106 $expected,
107 $this->lang->truncateHTML( $input, $len, $ellipsis )
108 );
109 }
110
111 /**
112 * Array format is ($len, $ellipsis, $input, $expected)
113 */
114 function provideHTMLTruncateData() {
115 return array(
116 array( 0, 'XXX', "1234567890", "XXX" ),
117 array( 8, 'XXX', "1234567890", "12345XXX" ),
118 array( 5, 'XXXXXXXXXXXXXXX', '1234567890', "1234567890" ),
119 array( 2, '***',
120 '<p><span style="font-weight:bold;"></span></p>',
121 '<p><span style="font-weight:bold;"></span></p>',
122 ),
123 array( 2, '***',
124 '<p><span style="font-weight:bold;">123456789</span></p>',
125 '<p><span style="font-weight:bold;">***</span></p>',
126 ),
127 array( 2, '***',
128 '<p><span style="font-weight:bold;">&nbsp;23456789</span></p>',
129 '<p><span style="font-weight:bold;">***</span></p>',
130 ),
131 array( 3, '***',
132 '<p><span style="font-weight:bold;">123456789</span></p>',
133 '<p><span style="font-weight:bold;">***</span></p>',
134 ),
135 array( 4, '***',
136 '<p><span style="font-weight:bold;">123456789</span></p>',
137 '<p><span style="font-weight:bold;">1***</span></p>',
138 ),
139 array( 5, '***',
140 '<tt><span style="font-weight:bold;">123456789</span></tt>',
141 '<tt><span style="font-weight:bold;">12***</span></tt>',
142 ),
143 array( 6, '***',
144 '<p><a href="www.mediawiki.org">123456789</a></p>',
145 '<p><a href="www.mediawiki.org">123***</a></p>',
146 ),
147 array( 6, '***',
148 '<p><a href="www.mediawiki.org">12&nbsp;456789</a></p>',
149 '<p><a href="www.mediawiki.org">12&nbsp;***</a></p>',
150 ),
151 array( 7, '***',
152 '<small><span style="font-weight:bold;">123<p id="#moo">456</p>789</span></small>',
153 '<small><span style="font-weight:bold;">123<p id="#moo">4***</p></span></small>',
154 ),
155 array( 8, '***',
156 '<div><span style="font-weight:bold;">123<span>4</span>56789</span></div>',
157 '<div><span style="font-weight:bold;">123<span>4</span>5***</span></div>',
158 ),
159 array( 9, '***',
160 '<p><table style="font-weight:bold;"><tr><td>123456789</td></tr></table></p>',
161 '<p><table style="font-weight:bold;"><tr><td>123456789</td></tr></table></p>',
162 ),
163 array( 10, '***',
164 '<p><font style="font-weight:bold;">123456789</font></p>',
165 '<p><font style="font-weight:bold;">123456789</font></p>',
166 ),
167 );
168 }
169
170 /**
171 * Test Language::isValidBuiltInCode()
172 * @dataProvider provideLanguageCodes
173 */
174 function testBuiltInCodeValidation( $code, $message = '' ) {
175 $this->assertTrue(
176 (bool) Language::isValidBuiltInCode( $code ),
177 "validating code $code $message"
178 );
179 }
180
181 function testBuiltInCodeValidationRejectUnderscore() {
182 $this->assertFalse(
183 (bool) Language::isValidBuiltInCode( 'be_tarask' ),
184 "reject underscore in language code"
185 );
186 }
187
188 function provideLanguageCodes() {
189 return array(
190 array( 'fr' , 'Two letters, minor case' ),
191 array( 'EN' , 'Two letters, upper case' ),
192 array( 'tyv' , 'Three letters' ),
193 array( 'tokipona' , 'long language code' ),
194 array( 'be-tarask', 'With dash' ),
195 array( 'Zh-classical', 'Begin with upper case, dash' ),
196 array( 'Be-x-old', 'With extension (two dashes)' ),
197 );
198 }
199
200 /**
201 * @dataProvider provideSprintfDateSamples
202 */
203 function testSprintfDate( $format, $ts, $expected, $msg ) {
204 $this->assertEquals(
205 $expected,
206 $this->lang->sprintfDate( $format, $ts ),
207 "sprintfDate('$format', '$ts'): $msg"
208 );
209 }
210
211 function provideSprintfDateSamples() {
212 return array(
213 array(
214 'xiY',
215 '20111212000000',
216 '1390', // note because we're testing English locale we get Latin-standard digits
217 'Iranian calendar full year'
218 ),
219 array(
220 'xiy',
221 '20111212000000',
222 '90',
223 'Iranian calendar short year'
224 ),
225 );
226 }
227
228 /**
229 * @dataProvider provideFormatSizes
230 */
231 function testFormatSize( $size, $expected, $msg ) {
232 $this->assertEquals(
233 $expected,
234 $this->lang->formatSize( $size ),
235 "formatSize('$size'): $msg"
236 );
237 }
238
239 function provideFormatSizes() {
240 return array(
241 array(
242 0,
243 "0 B",
244 "Zero bytes"
245 ),
246 array(
247 1024,
248 "1 KB",
249 "1 kilobyte"
250 ),
251 array(
252 1024 * 1024,
253 "1 MB",
254 "1,024 megabytes"
255 ),
256 array(
257 1024 * 1024 * 1024,
258 "1 GB",
259 "1 gigabytes"
260 ),
261 array(
262 pow( 1024, 4 ),
263 "1 TB",
264 "1 terabyte"
265 ),
266 array(
267 pow( 1024, 5 ),
268 "1 PB",
269 "1 petabyte"
270 ),
271 array(
272 pow( 1024, 6 ),
273 "1 EB",
274 "1,024 exabyte"
275 ),
276 array(
277 pow( 1024, 7 ),
278 "1 ZB",
279 "1 zetabyte"
280 ),
281 array(
282 pow( 1024, 8 ),
283 "1 YB",
284 "1 yottabyte"
285 ),
286 // How big!? THIS BIG!
287 );
288 }
289 }