Merge "Use the request object provided in User::setCookies"
[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 /** Tests for MediaWiki languages/LanguageSr.php */
20 class LanguageSrTest extends LanguageClassesTestCase {
21 /**
22 * @covers LanguageConverter::convertTo
23 */
24 public function testEasyConversions() {
25 $this->assertCyrillic(
26 'шђчћжШЂЧЋЖ',
27 'Cyrillic guessing characters'
28 );
29 $this->assertLatin(
30 'šđč枊ĐČĆŽ',
31 'Latin guessing characters'
32 );
33 }
34
35 /**
36 * @covers LanguageConverter::convertTo
37 */
38 public function testMixedConversions() {
39 $this->assertCyrillic(
40 'шђчћжШЂЧЋЖ - šđčćž',
41 'Mostly cyrillic characters'
42 );
43 $this->assertLatin(
44 'šđč枊ĐČĆŽ - шђчћж',
45 'Mostly latin characters'
46 );
47 }
48
49 /**
50 * @covers LanguageConverter::convertTo
51 */
52 public function testSameAmountOfLatinAndCyrillicGetConverted() {
53 $this->assertConverted(
54 '4 latin: šđčć | 4 cyrillic: шђчћ',
55 'sr-ec'
56 );
57 $this->assertConverted(
58 '4 latin: šđčć | 4 cyrillic: шђчћ',
59 'sr-el'
60 );
61 }
62
63 /**
64 * @author Nikola Smolenski
65 * @covers LanguageConverter::convertTo
66 */
67 public 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 /**
107 * @covers LanguageConverter::convertTo
108 */
109 public function testConversionToLatin() {
110 //A simple convertion of Latin to Latin
111 $this->assertEquals( 'abcd',
112 $this->convertToLatin( 'abcd' )
113 );
114 //A simple convertion of Cyrillic to Latin
115 $this->assertEquals( 'abcd',
116 $this->convertToLatin( 'абцд' )
117 );
118 //This text has some Latin, but is recognized as Cyrillic, so it should be converted
119 $this->assertEquals( 'abcdšđžčć',
120 $this->convertToLatin( 'abcdшђжчћ' )
121 );
122 //This text has some Cyrillic, but is recognized as Latin, so it should not be converted
123 $this->assertEquals( 'абцдšđžčć',
124 $this->convertToLatin( 'абцдšđžčć' )
125 );
126 }
127
128 /**
129 * @dataProvider providePlural
130 * @covers Language::convertPlural
131 */
132 public function testPlural( $result, $value ) {
133 $forms = array( 'one', 'few', 'other' );
134 $this->assertEquals( $result, $this->getLang()->convertPlural( $value, $forms ) );
135 }
136
137 /**
138 * @dataProvider providePlural
139 * @covers Language::getPluralRuleType
140 */
141 public function testGetPluralRuleType( $result, $value ) {
142 $this->assertEquals( $result, $this->getLang()->getPluralRuleType( $value ) );
143 }
144
145 public static function providePlural() {
146 return array(
147 array( 'one', 1 ),
148 array( 'other', 11 ),
149 array( 'one', 91 ),
150 array( 'one', 121 ),
151 array( 'few', 2 ),
152 array( 'few', 3 ),
153 array( 'few', 4 ),
154 array( 'few', 334 ),
155 array( 'other', 5 ),
156 array( 'other', 15 ),
157 array( 'other', 120 ),
158 );
159 }
160
161 /**
162 * @dataProvider providePluralTwoForms
163 * @covers Language::convertPlural
164 */
165 public function testPluralTwoForms( $result, $value ) {
166 $forms = array( 'one', 'other' );
167 $this->assertEquals( $result, $this->getLang()->convertPlural( $value, $forms ) );
168 }
169
170 public static function providePluralTwoForms() {
171 return array(
172 array( 'one', 1 ),
173 array( 'other', 11 ),
174 array( 'other', 4 ),
175 array( 'one', 91 ),
176 array( 'one', 121 ),
177 );
178 }
179
180 ##### HELPERS #####################################################
181 /**
182 *Wrapper to verify text stay the same after applying conversion
183 * @param string $text Text to convert
184 * @param string $variant Language variant 'sr-ec' or 'sr-el'
185 * @param string $msg Optional message
186 */
187 protected function assertUnConverted( $text, $variant, $msg = '' ) {
188 $this->assertEquals(
189 $text,
190 $this->convertTo( $text, $variant ),
191 $msg
192 );
193 }
194
195 /**
196 * Wrapper to verify a text is different once converted to a variant.
197 * @param string $text Text to convert
198 * @param string $variant Language variant 'sr-ec' or 'sr-el'
199 * @param string $msg Optional message
200 */
201 protected function assertConverted( $text, $variant, $msg = '' ) {
202 $this->assertNotEquals(
203 $text,
204 $this->convertTo( $text, $variant ),
205 $msg
206 );
207 }
208
209 /**
210 * Verifiy the given Cyrillic text is not converted when using
211 * using the cyrillic variant and converted to Latin when using
212 * the Latin variant.
213 * @param string $text Text to convert
214 * @param string $msg Optional message
215 */
216 protected function assertCyrillic( $text, $msg = '' ) {
217 $this->assertUnConverted( $text, 'sr-ec', $msg );
218 $this->assertConverted( $text, 'sr-el', $msg );
219 }
220
221 /**
222 * Verifiy the given Latin text is not converted when using
223 * using the Latin variant and converted to Cyrillic when using
224 * the Cyrillic variant.
225 * @param string $text Text to convert
226 * @param string $msg Optional message
227 */
228 protected function assertLatin( $text, $msg = '' ) {
229 $this->assertUnConverted( $text, 'sr-el', $msg );
230 $this->assertConverted( $text, 'sr-ec', $msg );
231 }
232
233 /** Wrapper for converter::convertTo() method*/
234 protected function convertTo( $text, $variant ) {
235 return $this->getLang()
236 ->mConverter
237 ->convertTo(
238 $text, $variant
239 );
240 }
241
242 protected function convertToCyrillic( $text ) {
243 return $this->convertTo( $text, 'sr-ec' );
244 }
245
246 protected function convertToLatin( $text ) {
247 return $this->convertTo( $text, 'sr-el' );
248 }
249 }