enhance tests name (for --testdox option)
[lhc/web/wiklou.git] / tests / phpunit / languages / LanguageTest.php
1 <?php
2
3 class LanguageTest extends MediaWikiTestCase {
4 private $lang;
5
6 function setUp() {
7 $this->lang = Language::factory( 'en' );
8 }
9 function tearDown() {
10 unset( $this->lang );
11 }
12
13 function testLanguageConvertDoubleWidthToSingleWidth() {
14 $this->assertEquals(
15 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
16 $this->lang->normalizeForSearch(
17 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
18 ),
19 'convertDoubleWidth() with the full alphabet and digits'
20 );
21 }
22
23 function testFormatTimePeriod() {
24 $this->assertEquals(
25 "9.5s",
26 $this->lang->formatTimePeriod( 9.45 ),
27 'formatTimePeriod() rounding (<10s)'
28 );
29
30 $this->assertEquals(
31 "10s",
32 $this->lang->formatTimePeriod( 9.95 ),
33 'formatTimePeriod() rounding (<10s)'
34 );
35
36 $this->assertEquals(
37 "1m 0s",
38 $this->lang->formatTimePeriod( 59.55 ),
39 'formatTimePeriod() rounding (<60s)'
40 );
41
42 $this->assertEquals(
43 "2m 0s",
44 $this->lang->formatTimePeriod( 119.55 ),
45 'formatTimePeriod() rounding (<1h)'
46 );
47
48 $this->assertEquals(
49 "1h 0m 0s",
50 $this->lang->formatTimePeriod( 3599.55 ),
51 'formatTimePeriod() rounding (<1h)'
52 );
53
54 $this->assertEquals(
55 "2h 0m 0s",
56 $this->lang->formatTimePeriod( 7199.55 ),
57 'formatTimePeriod() rounding (>=1h)'
58 );
59
60 $this->assertEquals(
61 "2h 0m",
62 $this->lang->formatTimePeriod( 7199.55, 'avoidseconds' ),
63 'formatTimePeriod() rounding (>=1h), avoidseconds'
64 );
65
66 $this->assertEquals(
67 "2h 0m",
68 $this->lang->formatTimePeriod( 7199.55, 'avoidminutes' ),
69 'formatTimePeriod() rounding (>=1h), avoidminutes'
70 );
71
72 $this->assertEquals(
73 "48h 0m",
74 $this->lang->formatTimePeriod( 172799.55, 'avoidseconds' ),
75 'formatTimePeriod() rounding (=48h), avoidseconds'
76 );
77
78 $this->assertEquals(
79 "3d 0h",
80 $this->lang->formatTimePeriod( 259199.55, 'avoidminutes' ),
81 'formatTimePeriod() rounding (>48h), avoidminutes'
82 );
83
84 $this->assertEquals(
85 "2d 1h 0m",
86 $this->lang->formatTimePeriod( 176399.55, 'avoidseconds' ),
87 'formatTimePeriod() rounding (>48h), avoidseconds'
88 );
89
90 $this->assertEquals(
91 "2d 1h",
92 $this->lang->formatTimePeriod( 176399.55, 'avoidminutes' ),
93 'formatTimePeriod() rounding (>48h), avoidminutes'
94 );
95
96 $this->assertEquals(
97 "3d 0h 0m",
98 $this->lang->formatTimePeriod( 259199.55, 'avoidseconds' ),
99 'formatTimePeriod() rounding (>48h), avoidminutes'
100 );
101
102 $this->assertEquals(
103 "2d 0h 0m",
104 $this->lang->formatTimePeriod( 172801.55, 'avoidseconds' ),
105 'formatTimePeriod() rounding, (>48h), avoidseconds'
106 );
107
108 $this->assertEquals(
109 "2d 1h 1m 1s",
110 $this->lang->formatTimePeriod( 176460.55 ),
111 'formatTimePeriod() rounding, recursion, (>48h)'
112 );
113 }
114
115 function testTruncate() {
116 $this->assertEquals(
117 "XXX",
118 $this->lang->truncate( "1234567890", 0, 'XXX' ),
119 'truncate prefix, len 0, small ellipsis'
120 );
121
122 $this->assertEquals(
123 "12345XXX",
124 $this->lang->truncate( "1234567890", 8, 'XXX' ),
125 'truncate prefix, small ellipsis'
126 );
127
128 $this->assertEquals(
129 "123456789",
130 $this->lang->truncate( "123456789", 5, 'XXXXXXXXXXXXXXX' ),
131 'truncate prefix, large ellipsis'
132 );
133
134 $this->assertEquals(
135 "XXX67890",
136 $this->lang->truncate( "1234567890", -8, 'XXX' ),
137 'truncate suffix, small ellipsis'
138 );
139
140 $this->assertEquals(
141 "123456789",
142 $this->lang->truncate( "123456789", -5, 'XXXXXXXXXXXXXXX' ),
143 'truncate suffix, large ellipsis'
144 );
145 }
146
147 /**
148 * @dataProvider provideHTMLTruncateData()
149 */
150 function testTruncateHtml( $len, $ellipsis, $input, $expected ) {
151 // Actual HTML...
152 $this->assertEquals(
153 $expected,
154 $this->lang->truncateHTML( $input, $len, $ellipsis )
155 );
156 }
157
158 /**
159 * Array format is ($len, $ellipsis, $input, $expected)
160 */
161 function provideHTMLTruncateData() {
162 return array(
163 array( 0, 'XXX', "1234567890", "XXX" ),
164 array( 8, 'XXX', "1234567890", "12345XXX" ),
165 array( 5, 'XXXXXXXXXXXXXXX', '1234567890', "1234567890" ),
166 array( 2, '***',
167 '<p><span style="font-weight:bold;"></span></p>',
168 '<p><span style="font-weight:bold;"></span></p>',
169 ),
170 array( 2, '***',
171 '<p><span style="font-weight:bold;">123456789</span></p>',
172 '<p><span style="font-weight:bold;">***</span></p>',
173 ),
174 array( 2, '***',
175 '<p><span style="font-weight:bold;">&nbsp;23456789</span></p>',
176 '<p><span style="font-weight:bold;">***</span></p>',
177 ),
178 array( 3, '***',
179 '<p><span style="font-weight:bold;">123456789</span></p>',
180 '<p><span style="font-weight:bold;">***</span></p>',
181 ),
182 array( 4, '***',
183 '<p><span style="font-weight:bold;">123456789</span></p>',
184 '<p><span style="font-weight:bold;">1***</span></p>',
185 ),
186 array( 5, '***',
187 '<tt><span style="font-weight:bold;">123456789</span></tt>',
188 '<tt><span style="font-weight:bold;">12***</span></tt>',
189 ),
190 array( 6, '***',
191 '<p><a href="www.mediawiki.org">123456789</a></p>',
192 '<p><a href="www.mediawiki.org">123***</a></p>',
193 ),
194 array( 6, '***',
195 '<p><a href="www.mediawiki.org">12&nbsp;456789</a></p>',
196 '<p><a href="www.mediawiki.org">12&nbsp;***</a></p>',
197 ),
198 array( 7, '***',
199 '<small><span style="font-weight:bold;">123<p id="#moo">456</p>789</span></small>',
200 '<small><span style="font-weight:bold;">123<p id="#moo">4***</p></span></small>',
201 ),
202 array( 8, '***',
203 '<div><span style="font-weight:bold;">123<span>4</span>56789</span></div>',
204 '<div><span style="font-weight:bold;">123<span>4</span>5***</span></div>',
205 ),
206 array( 9, '***',
207 '<p><table style="font-weight:bold;"><tr><td>123456789</td></tr></table></p>',
208 '<p><table style="font-weight:bold;"><tr><td>123456789</td></tr></table></p>',
209 ),
210 array( 10, '***',
211 '<p><font style="font-weight:bold;">123456789</font></p>',
212 '<p><font style="font-weight:bold;">123456789</font></p>',
213 ),
214 );
215 }
216
217 /**
218 * Test Language::isValidBuiltInCode()
219 * @dataProvider provideLanguageCodes
220 */
221 function testBuiltInCodeValidation( $code, $message = '' ) {
222 $this->assertTrue(
223 (bool) Language::isValidBuiltInCode( $code ),
224 "validating code $code $message"
225 );
226 }
227
228 function testBuiltInCodeValidationRejectUnderscore() {
229 $this->assertFalse(
230 (bool) Language::isValidBuiltInCode( 'be_tarask' ),
231 "reject underscore in language code"
232 );
233 }
234
235 function provideLanguageCodes() {
236 return array(
237 array( 'fr' , 'Two letters, minor case' ),
238 array( 'EN' , 'Two letters, upper case' ),
239 array( 'tyv' , 'Three letters' ),
240 array( 'tokipona' , 'long language code' ),
241 array( 'be-tarask', 'With dash' ),
242 array( 'Zh-classical', 'Begin with upper case, dash' ),
243 array( 'Be-x-old', 'With extension (two dashes)' ),
244 );
245 }
246 }