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