113f6cdea8262ebec6a685ce5b71ea07df4edfc6
[yaffs-website] / web / core / tests / Drupal / Tests / Component / Gettext / PoHeaderTest.php
1 <?php
2
3 namespace Drupal\Tests\Component\Gettext;
4
5 use Drupal\Component\Gettext\PoHeader;
6 use Drupal\Tests\UnitTestCase;
7
8 /**
9  * Unit tests for the Gettext PO file header handling features.
10  *
11  * @see Drupal\Component\Gettext\PoHeader.
12  *
13  * @group Gettext
14  */
15 class PoHeaderTest extends UnitTestCase {
16
17   /**
18    * Tests that plural expressions are evaluated correctly.
19    *
20    * Validate that the given plural expressions is evaluated with the correct
21    * plural formula.
22    *
23    * @param string $plural
24    *   The plural expression.
25    * @param array $expected
26    *   Array of expected plural positions keyed by plural value.
27    *
28    * @dataProvider providerTestPluralsFormula
29    */
30   public function testPluralsFormula($plural, $expected) {
31     $p = new PoHeader();
32     $parsed = $p->parsePluralForms($plural);
33     list($nplurals, $new_plural) = $parsed;
34     foreach ($expected as $number => $plural_form) {
35       $result = isset($new_plural[$number]) ? $new_plural[$number] : $new_plural['default'];
36       $this->assertEquals($result, $plural_form, 'Difference found at ' . $number . ': ' . $plural_form . ' versus ' . $result);
37     }
38   }
39
40   /**
41    * Data provider for testPluralsFormula.
42    *
43    * Gets pairs of plural expressions and expected plural positions keyed by
44    * plural value.
45    *
46    * @return array
47    *   Pairs of plural expressions and expected plural positions keyed by plural
48    *   value.
49    */
50   public function providerTestPluralsFormula() {
51     return [
52       [
53         'nplurals=1; plural=0;',
54         ['default' => 0],
55       ],
56       [
57         'nplurals=2; plural=(n > 1);',
58         [0 => 0, 1 => 0, 'default' => 1],
59       ],
60       [
61         'nplurals=2; plural=(n!=1);',
62         [1 => 0, 'default' => 1],
63       ],
64       [
65         'nplurals=2; plural=(((n==1)||((n%10)==1))?(0):1);',
66         [
67           1 => 0,
68           11 => 0,
69           21 => 0,
70           31 => 0,
71           41 => 0,
72           51 => 0,
73           61 => 0,
74           71 => 0,
75           81 => 0,
76           91 => 0,
77           101 => 0,
78           111 => 0,
79           121 => 0,
80           131 => 0,
81           141 => 0,
82           151 => 0,
83           161 => 0,
84           171 => 0,
85           181 => 0,
86           191 => 0,
87           'default' => 1,
88         ],
89       ],
90       [
91         'nplurals=3; plural=((((n%10)==1)&&((n%100)!=11))?(0):(((((n%10)>=2)&&((n%10)<=4))&&(((n%100)<10)||((n%100)>=20)))?(1):2));',
92         [
93           1 => 0,
94           2 => 1,
95           3 => 1,
96           4 => 1,
97           21 => 0,
98           22 => 1,
99           23 => 1,
100           24 => 1,
101           31 => 0,
102           32 => 1,
103           33 => 1,
104           34 => 1,
105           41 => 0,
106           42 => 1,
107           43 => 1,
108           44 => 1,
109           51 => 0,
110           52 => 1,
111           53 => 1,
112           54 => 1,
113           61 => 0,
114           62 => 1,
115           63 => 1,
116           64 => 1,
117           71 => 0,
118           72 => 1,
119           73 => 1,
120           74 => 1,
121           81 => 0,
122           82 => 1,
123           83 => 1,
124           84 => 1,
125           91 => 0,
126           92 => 1,
127           93 => 1,
128           94 => 1,
129           101 => 0,
130           102 => 1,
131           103 => 1,
132           104 => 1,
133           121 => 0,
134           122 => 1,
135           123 => 1,
136           124 => 1,
137           131 => 0,
138           132 => 1,
139           133 => 1,
140           134 => 1,
141           141 => 0,
142           142 => 1,
143           143 => 1,
144           144 => 1,
145           151 => 0,
146           152 => 1,
147           153 => 1,
148           154 => 1,
149           161 => 0,
150           162 => 1,
151           163 => 1,
152           164 => 1,
153           171 => 0,
154           172 => 1,
155           173 => 1,
156           174 => 1,
157           181 => 0,
158           182 => 1,
159           183 => 1,
160           184 => 1,
161           191 => 0,
162           192 => 1,
163           193 => 1,
164           194 => 1,
165           'default' => 2,
166         ],
167       ],
168       [
169         'nplurals=3; plural=((n==1)?(0):(((n>=2)&&(n<=4))?(1):2));',
170         [
171           1 => 0,
172           2 => 1,
173           3 => 1,
174           4 => 1,
175           'default' => 2,
176         ],
177       ],
178       [
179         'nplurals=3; plural=((n==1)?(0):(((n==0)||(((n%100)>0)&&((n%100)<20)))?(1):2));',
180         [
181           0 => 1,
182           1 => 0,
183           2 => 1,
184           3 => 1,
185           4 => 1,
186           5 => 1,
187           6 => 1,
188           7 => 1,
189           8 => 1,
190           9 => 1,
191           10 => 1,
192           11 => 1,
193           12 => 1,
194           13 => 1,
195           14 => 1,
196           15 => 1,
197           16 => 1,
198           17 => 1,
199           18 => 1,
200           19 => 1,
201           101 => 1,
202           102 => 1,
203           103 => 1,
204           104 => 1,
205           105 => 1,
206           106 => 1,
207           107 => 1,
208           108 => 1,
209           109 => 1,
210           110 => 1,
211           111 => 1,
212           112 => 1,
213           113 => 1,
214           114 => 1,
215           115 => 1,
216           116 => 1,
217           117 => 1,
218           118 => 1,
219           119 => 1,
220           'default' => 2,
221         ],
222       ],
223       [
224         'nplurals=3; plural=((n==1)?(0):(((((n%10)>=2)&&((n%10)<=4))&&(((n%100)<10)||((n%100)>=20)))?(1):2));',
225         [
226           1 => 0,
227           2 => 1,
228           3 => 1,
229           4 => 1,
230           22 => 1,
231           23 => 1,
232           24 => 1,
233           32 => 1,
234           33 => 1,
235           34 => 1,
236           42 => 1,
237           43 => 1,
238           44 => 1,
239           52 => 1,
240           53 => 1,
241           54 => 1,
242           62 => 1,
243           63 => 1,
244           64 => 1,
245           72 => 1,
246           73 => 1,
247           74 => 1,
248           82 => 1,
249           83 => 1,
250           84 => 1,
251           92 => 1,
252           93 => 1,
253           94 => 1,
254           102 => 1,
255           103 => 1,
256           104 => 1,
257           122 => 1,
258           123 => 1,
259           124 => 1,
260           132 => 1,
261           133 => 1,
262           134 => 1,
263           142 => 1,
264           143 => 1,
265           144 => 1,
266           152 => 1,
267           153 => 1,
268           154 => 1,
269           162 => 1,
270           163 => 1,
271           164 => 1,
272           172 => 1,
273           173 => 1,
274           174 => 1,
275           182 => 1,
276           183 => 1,
277           184 => 1,
278           192 => 1,
279           193 => 1,
280           194 => 1,
281           'default' => 2,
282         ], ],
283       [
284         'nplurals=4; plural=(((n==1)||(n==11))?(0):(((n==2)||(n==12))?(1):(((n>2)&&(n<20))?(2):3)));',
285         [
286           1 => 0,
287           2 => 1,
288           3 => 2,
289           4 => 2,
290           5 => 2,
291           6 => 2,
292           7 => 2,
293           8 => 2,
294           9 => 2,
295           10 => 2,
296           11 => 0,
297           12 => 1,
298           13 => 2,
299           14 => 2,
300           15 => 2,
301           16 => 2,
302           17 => 2,
303           18 => 2,
304           19 => 2,
305           'default' => 3,
306         ],
307       ],
308       [
309         'nplurals=4; plural=(((n%100)==1)?(0):(((n%100)==2)?(1):((((n%100)==3)||((n%100)==4))?(2):3)));',
310         [
311           1 => 0,
312           2 => 1,
313           3 => 2,
314           4 => 2,
315           101 => 0,
316           102 => 1,
317           103 => 2,
318           104 => 2,
319           'default' => 3,
320         ],
321       ],
322       [
323         'nplurals=5; plural=((n==1)?(0):((n==2)?(1):((n<7)?(2):((n<11)?(3):4))));',
324         [
325           0 => 2,
326           1 => 0,
327           2 => 1,
328           3 => 2,
329           4 => 2,
330           5 => 2,
331           6 => 2,
332           7 => 3,
333           8 => 3,
334           9 => 3,
335           10 => 3,
336           'default' => 4,
337         ],
338       ],
339       [
340         'nplurals=6; plural=((n==1)?(0):((n==0)?(1):((n==2)?(2):((((n%100)>=3)&&((n%100)<=10))?(3):((((n%100)>=11)&&((n%100)<=99))?(4):5)))));',
341         [
342           0 => 1,
343           1 => 0,
344           2 => 2,
345           3 => 3,
346           4 => 3,
347           5 => 3,
348           6 => 3,
349           7 => 3,
350           8 => 3,
351           9 => 3,
352           10 => 3,
353           100 => 5,
354           101 => 5,
355           102 => 5,
356           103 => 3,
357           104 => 3,
358           105 => 3,
359           106 => 3,
360           107 => 3,
361           108 => 3,
362           109 => 3,
363           110 => 3,
364           'default' => 4,
365         ],
366       ],
367     ];
368   }
369
370 }