Merge "Fixed dependencies for jquery.collapsibleTabs"
[lhc/web/wiklou.git] / tests / phpunit / languages / 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
15 require_once dirname( __DIR__ ) . '/bootstrap.php';
16
17 /** Tests for MediaWiki languages/LanguageSr.php */
18 class LanguageSrTest extends MediaWikiTestCase {
19 /* Language object. Initialized before each test */
20 private $lang;
21
22 protected function setUp() {
23 $this->lang = Language::factory( 'sr' );
24 }
25 protected function tearDown() {
26 unset( $this->lang );
27 }
28
29 ##### TESTS #######################################################
30
31 function testEasyConversions( ) {
32 $this->assertCyrillic(
33 'шђчћжШЂЧЋЖ',
34 'Cyrillic guessing characters'
35 );
36 $this->assertLatin(
37 'šđč枊ĐČĆŽ',
38 'Latin guessing characters'
39 );
40 }
41
42 function testMixedConversions() {
43 $this->assertCyrillic(
44 'шђчћжШЂЧЋЖ - šđčćž',
45 'Mostly cyrillic characters'
46 );
47 $this->assertLatin(
48 'šđč枊ĐČĆŽ - шђчћж',
49 'Mostly latin characters'
50 );
51 }
52
53 function testSameAmountOfLatinAndCyrillicGetConverted() {
54 $this->assertConverted(
55 '4 latin: šđčć | 4 cyrillic: шђчћ',
56 'sr-ec'
57 );
58 $this->assertConverted(
59 '4 latin: šđčć | 4 cyrillic: шђчћ',
60 'sr-el'
61 );
62 }
63
64 /**
65 * @author Nikola Smolenski
66 */
67 function testConversionToCyrillic() {
68 //A simple convertion of Latin to Cyrillic
69 $this->assertEquals( 'абвг',
70 $this->convertToCyrillic( 'abvg' )
71 );
72 //Same as above, but assert that -{}-s must be removed and not converted
73 $this->assertEquals( 'ljабnjвгdž',
74 $this->convertToCyrillic( '-{lj}-ab-{nj}-vg-{dž}-' )
75 );
76 //A simple convertion of Cyrillic to Cyrillic
77 $this->assertEquals( 'абвг',
78 $this->convertToCyrillic( 'абвг' )
79 );
80 //Same as above, but assert that -{}-s must be removed and not converted
81 $this->assertEquals( 'ljабnjвгdž',
82 $this->convertToCyrillic( '-{lj}-аб-{nj}-вг-{dž}-' )
83 );
84 //This text has some Latin, but is recognized as Cyrillic, so it should not be converted
85 $this->assertEquals( 'abvgшђжчћ',
86 $this->convertToCyrillic( 'abvgшђжчћ' )
87 );
88 //Same as above, but assert that -{}-s must be removed
89 $this->assertEquals( 'љabvgњшђжчћџ',
90 $this->convertToCyrillic( '-{љ}-abvg-{њ}-шђжчћ-{џ}-' )
91 );
92 //This text has some Cyrillic, but is recognized as Latin, so it should be converted
93 $this->assertEquals( 'абвгшђжчћ',
94 $this->convertToCyrillic( 'абвгšđžčć' )
95 );
96 //Same as above, but assert that -{}-s must be removed and not converted
97 $this->assertEquals( 'ljабвгnjшђжчћdž',
98 $this->convertToCyrillic( '-{lj}-абвг-{nj}-šđžčć-{dž}-' )
99 );
100 // Roman numerals are not converted
101 $this->assertEquals( 'а I б II в III г IV шђжчћ',
102 $this->convertToCyrillic( 'a I b II v III g IV šđžčć' )
103 );
104 }
105
106 function testConversionToLatin() {
107 //A simple convertion of Latin to Latin
108 $this->assertEquals( 'abcd',
109 $this->convertToLatin( 'abcd' )
110 );
111 //A simple convertion of Cyrillic to Latin
112 $this->assertEquals( 'abcd',
113 $this->convertToLatin( 'абцд' )
114 );
115 //This text has some Latin, but is recognized as Cyrillic, so it should be converted
116 $this->assertEquals( 'abcdšđžčć',
117 $this->convertToLatin( 'abcdшђжчћ' )
118 );
119 //This text has some Cyrillic, but is recognized as Latin, so it should not be converted
120 $this->assertEquals( 'абцдšđžčć',
121 $this->convertToLatin( 'абцдšđžčć' )
122 );
123 }
124
125 /** @dataProvider providePluralFourForms */
126 function testPluralFourForms( $result, $value ) {
127 $forms = array( 'one', 'few', 'many', 'other' );
128 $this->assertEquals( $result, $this->lang->convertPlural( $value, $forms ) );
129 }
130
131 function providePluralFourForms() {
132 return array (
133 array( 'one', 1 ),
134 array( 'many', 11 ),
135 array( 'one', 91 ),
136 array( 'one', 121 ),
137 array( 'few', 2 ),
138 array( 'few', 3 ),
139 array( 'few', 4 ),
140 array( 'few', 334 ),
141 array( 'many', 5 ),
142 array( 'many', 15 ),
143 array( 'many', 120 ),
144 );
145 }
146 /** @dataProvider providePluralTwoForms */
147 function testPluralTwoForms( $result, $value ) {
148 $forms = array( 'one', 'several' );
149 $this->assertEquals( $result, $this->lang->convertPlural( $value, $forms ) );
150 }
151 function providePluralTwoForms() {
152 return array (
153 array( 'one', 1 ),
154 array( 'several', 11 ),
155 array( 'several', 91 ),
156 array( 'several', 121 ),
157 );
158 }
159
160 ##### HELPERS #####################################################
161 /**
162 *Wrapper to verify text stay the same after applying conversion
163 * @param $text string Text to convert
164 * @param $variant string Language variant 'sr-ec' or 'sr-el'
165 * @param $msg string Optional message
166 */
167 function assertUnConverted( $text, $variant, $msg = '' ) {
168 $this->assertEquals(
169 $text,
170 $this->convertTo( $text, $variant ),
171 $msg
172 );
173 }
174 /**
175 * Wrapper to verify a text is different once converted to a variant.
176 * @param $text string Text to convert
177 * @param $variant string Language variant 'sr-ec' or 'sr-el'
178 * @param $msg string Optional message
179 */
180 function assertConverted( $text, $variant, $msg = '' ) {
181 $this->assertNotEquals(
182 $text,
183 $this->convertTo( $text, $variant ),
184 $msg
185 );
186 }
187
188 /**
189 * Verifiy the given Cyrillic text is not converted when using
190 * using the cyrillic variant and converted to Latin when using
191 * the Latin variant.
192 */
193 function assertCyrillic( $text, $msg = '' ) {
194 $this->assertUnConverted( $text, 'sr-ec', $msg );
195 $this->assertConverted( $text, 'sr-el', $msg );
196 }
197 /**
198 * Verifiy the given Latin text is not converted when using
199 * using the Latin variant and converted to Cyrillic when using
200 * the Cyrillic variant.
201 */
202 function assertLatin( $text, $msg = '' ) {
203 $this->assertUnConverted( $text, 'sr-el', $msg );
204 $this->assertConverted( $text, 'sr-ec', $msg );
205 }
206
207
208 /** Wrapper for converter::convertTo() method*/
209 function convertTo( $text, $variant ) {
210 return $this
211 ->lang
212 ->mConverter
213 ->convertTo(
214 $text, $variant
215 );
216 }
217 function convertToCyrillic( $text ) {
218 return $this->convertTo( $text, 'sr-ec' );
219 }
220 function convertToLatin( $text ) {
221 return $this->convertTo( $text, 'sr-el' );
222 }
223 }