1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpMyAdmin\MoTranslator\Tests; |
6
|
|
|
|
7
|
|
|
use PhpMyAdmin\MoTranslator\Translator; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
use function basename; |
10
|
|
|
use function glob; |
11
|
|
|
use function strpos; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Test for MO files parsing. |
15
|
|
|
*/ |
16
|
|
|
class MoFilesTest extends TestCase |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @param mixed $filename |
20
|
|
|
* |
21
|
|
|
* @dataProvider provideMoFiles |
22
|
|
|
*/ |
23
|
|
|
public function testMoFileTranslate($filename): void |
24
|
|
|
{ |
25
|
|
|
$parser = new Translator($filename); |
26
|
|
|
$this->assertEquals( |
27
|
|
|
'Pole', |
28
|
|
|
$parser->gettext('Column') |
29
|
|
|
); |
30
|
|
|
// Non existing string |
31
|
|
|
$this->assertEquals( |
32
|
|
|
'Column parser', |
33
|
|
|
$parser->gettext('Column parser') |
34
|
|
|
); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param mixed $filename |
39
|
|
|
* |
40
|
|
|
* @dataProvider provideMoFiles |
41
|
|
|
*/ |
42
|
|
|
public function testMoFilePlurals($filename): void |
43
|
|
|
{ |
44
|
|
|
$parser = new Translator($filename); |
45
|
|
|
$expected2 = '%d sekundy'; |
46
|
|
|
if (strpos($filename, 'invalid-formula.mo') !== false || strpos($filename, 'lessplurals.mo') !== false) { |
47
|
|
|
$expected0 = '%d sekunda'; |
48
|
|
|
$expected2 = '%d sekunda'; |
49
|
|
|
} elseif (strpos($filename, 'plurals.mo') !== false || strpos($filename, 'noheader.mo') !== false) { |
50
|
|
|
$expected0 = '%d sekundy'; |
51
|
|
|
} else { |
52
|
|
|
$expected0 = '%d sekund'; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$this->assertEquals( |
56
|
|
|
$expected0, |
57
|
|
|
$parser->ngettext( |
58
|
|
|
'%d second', |
59
|
|
|
'%d seconds', |
60
|
|
|
0 |
61
|
|
|
) |
62
|
|
|
); |
63
|
|
|
$this->assertEquals( |
64
|
|
|
'%d sekunda', |
65
|
|
|
$parser->ngettext( |
66
|
|
|
'%d second', |
67
|
|
|
'%d seconds', |
68
|
|
|
1 |
69
|
|
|
) |
70
|
|
|
); |
71
|
|
|
$this->assertEquals( |
72
|
|
|
$expected2, |
73
|
|
|
$parser->ngettext( |
74
|
|
|
'%d second', |
75
|
|
|
'%d seconds', |
76
|
|
|
2 |
77
|
|
|
) |
78
|
|
|
); |
79
|
|
|
$this->assertEquals( |
80
|
|
|
$expected0, |
81
|
|
|
$parser->ngettext( |
82
|
|
|
'%d second', |
83
|
|
|
'%d seconds', |
84
|
|
|
5 |
85
|
|
|
) |
86
|
|
|
); |
87
|
|
|
$this->assertEquals( |
88
|
|
|
$expected0, |
89
|
|
|
$parser->ngettext( |
90
|
|
|
'%d second', |
91
|
|
|
'%d seconds', |
92
|
|
|
10 |
93
|
|
|
) |
94
|
|
|
); |
95
|
|
|
// Non existing string |
96
|
|
|
$this->assertEquals( |
97
|
|
|
'"%d" seconds', |
98
|
|
|
$parser->ngettext( |
99
|
|
|
'"%d" second', |
100
|
|
|
'"%d" seconds', |
101
|
|
|
10 |
102
|
|
|
) |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param mixed $filename |
108
|
|
|
* |
109
|
|
|
* @dataProvider provideMoFiles |
110
|
|
|
*/ |
111
|
|
|
public function testMoFileContext($filename): void |
112
|
|
|
{ |
113
|
|
|
$parser = new Translator($filename); |
114
|
|
|
$this->assertEquals( |
115
|
|
|
'Tabulka', |
116
|
|
|
$parser->pgettext( |
117
|
|
|
'Display format', |
118
|
|
|
'Table' |
119
|
|
|
) |
120
|
|
|
); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param mixed $filename |
125
|
|
|
* |
126
|
|
|
* @dataProvider provideNotTranslatedFiles |
127
|
|
|
*/ |
128
|
|
|
public function testMoFileNotTranslated($filename): void |
129
|
|
|
{ |
130
|
|
|
$parser = new Translator($filename); |
131
|
|
|
$this->assertEquals( |
132
|
|
|
'%d second', |
133
|
|
|
$parser->ngettext( |
134
|
|
|
'%d second', |
135
|
|
|
'%d seconds', |
136
|
|
|
1 |
137
|
|
|
) |
138
|
|
|
); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function provideMoFiles(): array |
142
|
|
|
{ |
143
|
|
|
return $this->getFiles('./tests/data/*.mo'); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function provideErrorMoFiles(): array |
147
|
|
|
{ |
148
|
|
|
return $this->getFiles('./tests/data/error/*.mo'); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function provideNotTranslatedFiles(): array |
152
|
|
|
{ |
153
|
|
|
return $this->getFiles('./tests/data/not-translated/*.mo'); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param mixed $file |
158
|
|
|
* |
159
|
|
|
* @dataProvider provideErrorMoFiles |
160
|
|
|
*/ |
161
|
|
|
public function testEmptyMoFile($file): void |
162
|
|
|
{ |
163
|
|
|
$parser = new Translator($file); |
164
|
|
|
if (basename($file) === 'magic.mo') { |
165
|
|
|
$this->assertEquals(Translator::ERROR_BAD_MAGIC, $parser->error); |
166
|
|
|
} else { |
167
|
|
|
$this->assertEquals(Translator::ERROR_READING, $parser->error); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
$this->assertEquals( |
171
|
|
|
'Table', |
172
|
|
|
$parser->pgettext( |
173
|
|
|
'Display format', |
174
|
|
|
'Table' |
175
|
|
|
) |
176
|
|
|
); |
177
|
|
|
$this->assertEquals( |
178
|
|
|
'"%d" seconds', |
179
|
|
|
$parser->ngettext( |
180
|
|
|
'"%d" second', |
181
|
|
|
'"%d" seconds', |
182
|
|
|
10 |
183
|
|
|
) |
184
|
|
|
); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @param mixed $file |
189
|
|
|
* |
190
|
|
|
* @dataProvider provideMoFiles |
191
|
|
|
*/ |
192
|
|
|
public function testExists($file): void |
193
|
|
|
{ |
194
|
|
|
$parser = new Translator($file); |
195
|
|
|
$this->assertEquals( |
196
|
|
|
true, |
197
|
|
|
$parser->exists('Column') |
198
|
|
|
); |
199
|
|
|
$this->assertEquals( |
200
|
|
|
false, |
201
|
|
|
$parser->exists('Column parser') |
202
|
|
|
); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* @param string $pattern path names pattern to match |
207
|
|
|
*/ |
208
|
|
|
private function getFiles(string $pattern): array |
209
|
|
|
{ |
210
|
|
|
$files = glob($pattern); |
211
|
|
|
if ($files === false) { |
212
|
|
|
return []; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
$result = []; |
216
|
|
|
foreach ($files as $file) { |
217
|
|
|
$result[] = [$file]; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
return $result; |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|