Merge "Fix warnings in SpecialEmailuser"
[lhc/web/wiklou.git] / tests / phpunit / languages / classes / LanguageSrTest.php
1 <?php
2 /**
3 * PHPUnit tests for the Serbian language.
4 * The language can be represented using two scripts:
5 * - Latin (SR_el)
6 * - Cyrillic (SR_ec)
7 * Both representations seems to be bijective, hence MediaWiki can convert
8 * from one script to the other.
9 *
10 * @author Antoine Musso <hashar at free dot fr>
11 * @copyright Copyright © 2011, Antoine Musso <hashar at free dot fr>
12 * @file
13 *
14 * @todo methods in test class should be tidied:
15 * - Should be split into separate test methods and data providers
16 * - Tests for LanguageConverter and Language should probably be separate..
17 */
18
19 /**
20 * @covers LanguageSr
21 * @covers SrConverter
22 */
23 class LanguageSrTest extends LanguageClassesTestCase {
24 /**
25 * @covers Language::hasVariants
26 */
27 public function testHasVariants() {
28 $this->assertTrue( $this->getLang()->hasVariants(), 'sr has variants' );
29 }
30
31 /**
32 * @covers Language::hasVariant
33 */
34 public function testHasVariant() {
35 $langs = [
36 'sr' => $this->getLang(),
37 'sr-ec' => Language::factory( 'sr-ec' ),
38 'sr-cyrl' => Language::factory( 'sr-cyrl' ),
39 ];
40 foreach ( $langs as $code => $l ) {
41 $p = $l->getParentLanguage();
42 $this->assertTrue( $p !== null, 'parent language exists' );
43 $this->assertEquals( 'sr', $p->getCode(), 'sr is parent language' );
44 $this->assertTrue( $p instanceof LanguageSr, 'parent is LanguageSr' );
45 // This is a valid variant of the base
46 $this->assertTrue( $p->hasVariant( $l->getCode() ) );
47 // This test should be tweaked if/when sr-ec is renamed (T117845)
48 // to swap the roles of sr-ec and sr-Cyrl
49 $this->assertTrue( $l->hasVariant( 'sr-ec' ), 'sr-ec exists' );
50 // note that sr-cyrl is an alias, not a (strict) variant name
51 foreach ( [ 'sr-EC', 'sr-Cyrl', 'sr-cyrl', 'sr-bogus' ] as $v ) {
52 $this->assertFalse( $l->hasVariant( $v ), "$v is not a variant of $code" );
53 }
54 }
55 }
56
57 /**
58 * @covers Language::hasVariant
59 */
60 public function testHasVariantBogus() {
61 $langs = [
62 // Note that case matters when calling Language::factory();
63 // these are all bogus language codes
64 'sr-EC' => Language::factory( 'sr-EC' ),
65 'sr-Cyrl' => Language::factory( 'sr-Cyrl' ),
66 'sr-bogus' => Language::factory( 'sr-bogus' ),
67 ];
68 foreach ( $langs as $code => $l ) {
69 $p = $l->getParentLanguage();
70 $this->assertTrue( $p === null, 'no parent for bogus language' );
71 $this->assertFalse( $l instanceof LanguageSr, "$code is not sr" );
72 $this->assertFalse( $this->getLang()->hasVariant( $code ), "$code is not a sr variant" );
73 foreach ( [ 'sr', 'sr-ec', 'sr-EC', 'sr-Cyrl', 'sr-cyrl', 'sr-bogus' ] as $v ) {
74 if ( $v !== $code ) {
75 $this->assertFalse( $l->hasVariant( $v ), "no variant $v" );
76 }
77 }
78 }
79 }
80
81 /**
82 * @covers LanguageConverter::convertTo
83 */
84 public function testEasyConversions() {
85 $this->assertCyrillic(
86 'шђчћжШЂЧЋЖ',
87 'Cyrillic guessing characters'
88 );
89 $this->assertLatin(
90 'šđč枊ĐČĆŽ',
91 'Latin guessing characters'
92 );
93 }
94
95 /**
96 * @covers LanguageConverter::convertTo
97 */
98 public function testMixedConversions() {
99 $this->assertCyrillic(
100 'шђчћжШЂЧЋЖ - šđčćž',
101 'Mostly Cyrillic characters'
102 );
103 $this->assertLatin(
104 'šđč枊ĐČĆŽ - шђчћж',
105 'Mostly Latin characters'
106 );
107 }
108
109 /**
110 * @covers LanguageConverter::convertTo
111 */
112 public function testSameAmountOfLatinAndCyrillicGetConverted() {
113 $this->assertConverted(
114 '4 Latin: šđčć | 4 Cyrillic: шђчћ',
115 'sr-ec'
116 );
117 $this->assertConverted(
118 '4 Latin: šđčć | 4 Cyrillic: шђчћ',
119 'sr-el'
120 );
121 }
122
123 /**
124 * @author Nikola Smolenski
125 * @covers LanguageConverter::convertTo
126 */
127 public function testConversionToCyrillic() {
128 // A simple conversion of Latin to Cyrillic
129 $this->assertEquals( 'абвг',
130 $this->convertToCyrillic( 'abvg' )
131 );
132 // Same as above, but assert that -{}-s must be removed and not converted
133 $this->assertEquals( 'ljабnjвгdž',
134 $this->convertToCyrillic( '-{lj}-ab-{nj}-vg-{dž}-' )
135 );
136 // A simple conversion of Cyrillic to Cyrillic
137 $this->assertEquals( 'абвг',
138 $this->convertToCyrillic( 'абвг' )
139 );
140 // Same as above, but assert that -{}-s must be removed and not converted
141 $this->assertEquals( 'ljабnjвгdž',
142 $this->convertToCyrillic( '-{lj}-аб-{nj}-вг-{dž}-' )
143 );
144 // This text has some Latin, but is recognized as Cyrillic, so it should not be converted
145 $this->assertEquals( 'abvgшђжчћ',
146 $this->convertToCyrillic( 'abvgшђжчћ' )
147 );
148 // Same as above, but assert that -{}-s must be removed
149 $this->assertEquals( 'љabvgњшђжчћџ',
150 $this->convertToCyrillic( '-{љ}-abvg-{њ}-шђжчћ-{џ}-' )
151 );
152 // This text has some Cyrillic, but is recognized as Latin, so it should be converted
153 $this->assertEquals( 'абвгшђжчћ',
154 $this->convertToCyrillic( 'абвгšđžčć' )
155 );
156 // Same as above, but assert that -{}-s must be removed and not converted
157 $this->assertEquals( 'ljабвгnjшђжчћdž',
158 $this->convertToCyrillic( '-{lj}-абвг-{nj}-šđžčć-{dž}-' )
159 );
160 // Roman numerals are not converted
161 $this->assertEquals( 'а I б II в III г IV шђжчћ',
162 $this->convertToCyrillic( 'a I b II v III g IV šđžčć' )
163 );
164 }
165
166 /**
167 * @covers LanguageConverter::convertTo
168 */
169 public function testConversionToLatin() {
170 // A simple conversion of Latin to Latin
171 $this->assertEquals( 'abcd',
172 $this->convertToLatin( 'abcd' )
173 );
174 // A simple conversion of Cyrillic to Latin
175 $this->assertEquals( 'abcd',
176 $this->convertToLatin( 'абцд' )
177 );
178 // This text has some Latin, but is recognized as Cyrillic, so it should be converted
179 $this->assertEquals( 'abcdšđžčć',
180 $this->convertToLatin( 'abcdшђжчћ' )
181 );
182 // This text has some Cyrillic, but is recognized as Latin, so it should not be converted
183 $this->assertEquals( 'абцдšđžčć',
184 $this->convertToLatin( 'абцдšđžčć' )
185 );
186 }
187
188 /**
189 * @dataProvider providePlural
190 * @covers Language::convertPlural
191 */
192 public function testPlural( $result, $value ) {
193 $forms = [ 'one', 'few', 'other' ];
194 $this->assertEquals( $result, $this->getLang()->convertPlural( $value, $forms ) );
195 }
196
197 /**
198 * @dataProvider providePlural
199 * @covers Language::getPluralRuleType
200 */
201 public function testGetPluralRuleType( $result, $value ) {
202 $this->assertEquals( $result, $this->getLang()->getPluralRuleType( $value ) );
203 }
204
205 public static function providePlural() {
206 return [
207 [ 'one', 1 ],
208 [ 'other', 11 ],
209 [ 'one', 91 ],
210 [ 'one', 121 ],
211 [ 'few', 2 ],
212 [ 'few', 3 ],
213 [ 'few', 4 ],
214 [ 'few', 334 ],
215 [ 'other', 5 ],
216 [ 'other', 15 ],
217 [ 'other', 120 ],
218 ];
219 }
220
221 /**
222 * @dataProvider providePluralTwoForms
223 * @covers Language::convertPlural
224 */
225 public function testPluralTwoForms( $result, $value ) {
226 $forms = [ 'one', 'other' ];
227 $this->assertEquals( $result, $this->getLang()->convertPlural( $value, $forms ) );
228 }
229
230 public static function providePluralTwoForms() {
231 return [
232 [ 'one', 1 ],
233 [ 'other', 11 ],
234 [ 'other', 4 ],
235 [ 'one', 91 ],
236 [ 'one', 121 ],
237 ];
238 }
239
240 # #### HELPERS #####################################################
241 /**
242 *Wrapper to verify text stay the same after applying conversion
243 * @param string $text Text to convert
244 * @param string $variant Language variant 'sr-ec' or 'sr-el'
245 * @param string $msg Optional message
246 */
247 protected function assertUnConverted( $text, $variant, $msg = '' ) {
248 $this->assertEquals(
249 $text,
250 $this->convertTo( $text, $variant ),
251 $msg
252 );
253 }
254
255 /**
256 * Wrapper to verify a text is different once converted to a variant.
257 * @param string $text Text to convert
258 * @param string $variant Language variant 'sr-ec' or 'sr-el'
259 * @param string $msg Optional message
260 */
261 protected function assertConverted( $text, $variant, $msg = '' ) {
262 $this->assertNotEquals(
263 $text,
264 $this->convertTo( $text, $variant ),
265 $msg
266 );
267 }
268
269 /**
270 * Verifiy the given Cyrillic text is not converted when using
271 * using the Cyrillic variant and converted to Latin when using
272 * the Latin variant.
273 * @param string $text Text to convert
274 * @param string $msg Optional message
275 */
276 protected function assertCyrillic( $text, $msg = '' ) {
277 $this->assertUnConverted( $text, 'sr-ec', $msg );
278 $this->assertConverted( $text, 'sr-el', $msg );
279 }
280
281 /**
282 * Verifiy the given Latin text is not converted when using
283 * using the Latin variant and converted to Cyrillic when using
284 * the Cyrillic variant.
285 * @param string $text Text to convert
286 * @param string $msg Optional message
287 */
288 protected function assertLatin( $text, $msg = '' ) {
289 $this->assertUnConverted( $text, 'sr-el', $msg );
290 $this->assertConverted( $text, 'sr-ec', $msg );
291 }
292
293 /** Wrapper for converter::convertTo() method*/
294 protected function convertTo( $text, $variant ) {
295 return $this->getLang()
296 ->mConverter
297 ->convertTo(
298 $text, $variant
299 );
300 }
301
302 protected function convertToCyrillic( $text ) {
303 return $this->convertTo( $text, 'sr-ec' );
304 }
305
306 protected function convertToLatin( $text ) {
307 return $this->convertTo( $text, 'sr-el' );
308 }
309 }