-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathAbstractTranslationTestCase.php
More file actions
463 lines (389 loc) · 15.2 KB
/
AbstractTranslationTestCase.php
File metadata and controls
463 lines (389 loc) · 15.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter Shield.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Tests\Language;
use CodeIgniter\CLI\CLI;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
/**
* This abstract test class does the heavy testing of the sufficiency
* and precision of provided per-locale translations.
*
* New tests for new locales should extend this class and marked as "final".
*
* @internal
*/
abstract class AbstractTranslationTestCase extends TestCase
{
/**
* Relative path to the main language folder in main repository.
*
* @var string
*/
private const MAIN_LANGUAGE_REPO = '/src/Language/en/';
/**
* Relative path to the language folder.
*
* @var string
*/
private const LANGUAGE_DIR = '/src/Language/';
/**
* Collection of all locale codes mapped from the
* individual locale translation test case.
*
* @var array<string, string>
*/
public static array $locales = [
ArabicTranslationTest::class => 'ar',
BulgarianTranslationTest::class => 'bg',
// BosnianTranslationTest::class => 'bs',
CzechTranslationTest::class => 'cs',
GermanTranslationTest::class => 'de',
SpanishTranslationTest::class => 'es',
FarsiTranslationTest::class => 'fa',
FrenchTranslationTest::class => 'fr',
// HungarianTranslationTest::class => 'hu',
IndonesianTranslationTest::class => 'id',
ItalianTranslationTest::class => 'it',
JapaneseTranslationTest::class => 'ja',
// KoreanTranslationTest::class => 'ko',
LithuanianTranslationTest::class => 'lt',
// LatvianTranslationTest::class => 'lv',
// MalayalamTranslationTest::class => 'ml',
DutchTranslationTest::class => 'nl',
// NorwegianTranslationTest::class => 'no',
PolishTranslationTest::class => 'pl',
PortugueseTranslationTest::class => 'pt',
BrazilianTranslationTest::class => 'pt-BR',
RussianTranslationTest::class => 'ru',
// SinhalaTranslationTest::class => 'si',
SlovakTranslationTest::class => 'sk',
SerbianTranslationTest::class => 'sr',
SwedishTranslationTest::class => 'sv-SE',
// ThaiTranslationTest::class => 'th',
TurkishTranslationTest::class => 'tr',
UkrainianTranslationTest::class => 'uk',
// VietnameseTranslationTest::class => 'vi',
// SimplifiedChineseTranslationTest::class => 'zh-CN',
// TraditionalChineseTranslationTest::class => 'zh-TW',
];
/**
* A list of language keys that do not differ from
* the untranslated string even if translated correctly.
*
* This array will be filled in each locale's test
* class and the contained values will be skipped in
* testAllIncludedLanguageKeysAreTranslated.
*
* @var list<string>
*/
protected array $excludedLocaleKeyTranslations = [];
// -------------------------------------------------------------------------
// TESTS
// -------------------------------------------------------------------------
/**
* This tests that all language files configured in the main CI4 repository
* have a corresponding language file in the current locale.
*/
#[DataProvider('localesProvider')]
final public function testAllConfiguredLanguageFilesAreTranslated(string $locale): void
{
$filesNotTranslated = array_diff(
$this->expectedSets(),
$this->foundSets($locale),
);
sort($filesNotTranslated);
$count = count($filesNotTranslated);
$this->assertEmpty($filesNotTranslated, sprintf(
'Failed asserting that language %s "%s" in the main repository %s translated in "%s" locale.',
$count > 1 ? 'files' : 'file',
implode('", "', $filesNotTranslated),
$count > 1 ? 'are' : 'is',
$locale,
));
}
/**
* This tests that all translated language files in the current locale have a
* corresponding language file in the main CI4 repository.
*/
#[DataProvider('localesProvider')]
final public function testAllTranslatedLanguageFilesAreConfigured(string $locale): void
{
$filesNotConfigured = array_diff(
$this->foundSets($locale),
$this->expectedSets(),
);
sort($filesNotConfigured);
$count = count($filesNotConfigured);
$this->assertEmpty($filesNotConfigured, sprintf(
'Failed asserting that translated language %s "%s" in "%s" locale %s configured in the main repository.',
$count > 1 ? 'files' : 'file',
implode('", "', $filesNotConfigured),
$locale,
$count > 1 ? 'are' : 'is',
));
}
/**
* This tests that all language keys defined by a language file in the main CI4
* repository have corresponding keys in the current locale.
*/
#[DataProvider('localesProvider')]
final public function testAllConfiguredLanguageKeysAreIncluded(string $locale): void
{
$keysNotIncluded = [];
foreach ($this->foundSets($locale) as $file) {
$missing = array_diff_key(
$this->loadFile($file),
$this->loadFile($file, $locale),
);
foreach (array_keys($missing) as $key) {
$keysNotIncluded[] = substr($file, 0, -4) . '.' . $key;
}
}
sort($keysNotIncluded);
$count = count($keysNotIncluded);
$this->assertEmpty($keysNotIncluded, sprintf(
'Failed asserting that the language %s "%s" in the main repository %s included for translation in "%s" locale.',
$count > 1 ? 'keys' : 'key',
implode('", "', $keysNotIncluded),
$count > 1 ? 'are' : 'is',
$locale,
));
}
/**
* This tests that all included language keys in a language file for the current
* locale have corresponding keys in the main CI4 repository.
*/
#[DataProvider('localesProvider')]
final public function testAllIncludedLanguageKeysAreConfigured(string $locale): void
{
$keysNotConfigured = [];
foreach ($this->foundSets($locale) as $file) {
$extra = array_diff_key(
$this->loadFile($file, $locale),
$this->loadFile($file),
);
foreach (array_keys($extra) as $key) {
$keysNotConfigured[] = substr($file, 0, -4) . '.' . $key;
}
}
sort($keysNotConfigured);
$count = count($keysNotConfigured);
$this->assertEmpty($keysNotConfigured, sprintf(
'Failed asserting that the translated language %s "%s" in "%s" locale %s configured in the main repository.',
$count > 1 ? 'keys' : 'key',
implode('", "', $keysNotConfigured),
$locale,
$count > 1 ? 'are' : 'is',
));
}
/**
* This tests that all included language keys in a language file for the current
* locale that have corresponding keys in the main CI4 repository are really translated
* and do not only copy the main repository's value.
*/
#[DataProvider('localesProvider')]
final public function testAllIncludedLanguageKeysAreTranslated(string $locale): void
{
// These keys are usually not translated because they contain either
// universal abbreviations or simply combine parameters with signs.
static $excludedKeyTranslations = [];
$excludedKeys = array_unique(array_merge($excludedKeyTranslations, $this->excludedLocaleKeyTranslations));
$availableSets = array_intersect($this->expectedSets(), $this->foundSets($locale));
$keysNotTranslated = [];
foreach ($availableSets as $file) {
$originalStrings = $this->loadFile($file);
foreach ($this->loadFile($file, $locale) as $key => $translation) {
$keyName = substr($file, 0, -4) . '.' . $key;
if (in_array($keyName, $excludedKeys, true)) {
continue;
}
if ((array_key_exists($key, $originalStrings) && $originalStrings[$key] === $translation) || $translation === '') {
$keysNotTranslated[] = $keyName;
}
}
}
sort($keysNotTranslated);
$count = count($keysNotTranslated);
$this->assertEmpty($keysNotTranslated, sprintf(
'Failed asserting that the translated language %s "%s" in "%s" locale %s from the original keys in the main repository.',
$count > 1 ? 'keys' : 'key',
implode('", "', $keysNotTranslated),
$locale,
$count > 1 ? 'differ' : 'differs',
));
}
/**
* This tests that the order of all language keys defined by a translation language file
* resembles the order in the main CI4 repository.
*/
#[DataProvider('localesProvider')]
final public function testAllConfiguredLanguageKeysAreInOrder(string $locale): void
{
$diffs = [];
foreach ($this->foundSets($locale) as $file) {
$original = $this->loadFile($file);
$translated = $this->loadFile($file, $locale);
// No need to check the order if the number is already different
// This is handled by the other tests
if (count($original) === count($translated)) {
$trans = array_keys($translated);
foreach (array_keys($original) as $index => $expectedKey) {
$actualKey = $trans[$index] ?? null;
if ($actualKey !== null && $expectedKey !== $actualKey) {
$diffs[] = sprintf(
"\n%s:\n%s\n%s",
$file,
CLI::color("-'{$expectedKey}' => '{$original[$expectedKey]}';", 'red'),
CLI::color("+'{$actualKey}' => '{$translated[$actualKey]}';", 'green'),
);
break;
}
}
}
}
$this->assertEmpty($diffs, sprintf(
"Failed asserting that the translated language keys in \"%s\" locale are ordered correctly.\n%s\n%s",
$locale,
CLI::color('--- Original', 'red') . "\n" . CLI::color('+++ Translated', 'green'),
implode("\n", $diffs),
));
}
/**
* @see https://codeigniter4.github.io/CodeIgniter4/outgoing/localization.html#replacing-parameters
*/
#[DataProvider('localesProvider')]
final public function testAllLocalizationParametersAreNotTranslated(string $locale): void
{
$diffs = [];
foreach ($this->foundSets($locale) as $file) {
$original = $this->loadFile($file);
$translated = $this->loadFile($file, $locale);
foreach ($original as $key => $translation) {
if (! array_key_exists($key, $translated)) {
continue;
}
preg_match_all('/(\{[^\}]+\})/', $translation, $matches);
array_shift($matches);
if ($matches === [[]]) {
unset($matches);
continue;
}
foreach ($matches as $match) {
foreach ($match as $parameter) {
if (! str_contains($translated[$key], $parameter)) {
$id = sprintf('%s.%s', substr($file, 0, -4), $key);
$diffs[$id] ??= [];
$diffs[$id][] = $parameter;
}
}
}
unset($matches);
}
}
ksort($diffs);
$this->assertEmpty($diffs, sprintf(
"Failed asserting that parameters of translation keys are not translated:\n%s",
implode("\n", array_map(
static fn (string $key, array $values): string => sprintf(' * %s => %s', $key, implode(', ', $values)),
array_keys($diffs),
array_values($diffs),
)),
));
}
#[DataProvider('localesProvider')]
final public function testLocaleHasCorrespondingTestCaseFile(string $locale): void
{
$class = array_flip(self::$locales)[$locale];
$this->assertTrue(class_exists($class, false), sprintf(
'Failed asserting that test class "%s" is existing.',
$class,
));
}
/**
* @return array<string, list<string>>
*/
final public static function localesProvider(): iterable
{
$locale = self::$locales[static::class] ?? null;
if (null === $locale) {
static::fail('The locale code should be defined in the $locales property.');
}
return [$locale => [$locale]];
}
// -------------------------------------------------------------------------
// UTILITIES
// -------------------------------------------------------------------------
/**
* Get all the ISO 639-1 and 639-2 locale codes.
*
* @return array<string, list<string>>
*/
final public function translationKeys(): array
{
helper('filesystem');
$sets = [];
$dirs = directory_map(getcwd() . '/Language', 1);
foreach ($dirs as $dir) {
$dir = trim((string) $dir, '\\/');
$sets[$dir] = [$dir];
}
return $sets;
}
/**
* @return array<string, string>
*/
final public function expectedSets(): array
{
static $expected;
if (null === $expected) {
$expected = $this->translationSets();
}
return $expected;
}
/**
* @return array<string, string>
*/
final public function foundSets(string $locale): array
{
return $this->translationSets($locale);
}
/**
* Loads the language keys and translation equivalents.
*
* @return array<string, string>
*/
final public function loadFile(string $file, ?string $locale = null): array
{
$folder = $locale
? getcwd() . self::LANGUAGE_DIR . "{$locale}/"
: getcwd() . self::MAIN_LANGUAGE_REPO;
$file = $folder . $file;
return require $file;
}
/**
* Gets the set of language files for each location.
*
* @return array<string, string>
*/
private function translationSets(?string $locale = null): array
{
helper('filesystem');
$location = $locale
? getcwd() . self::LANGUAGE_DIR . "{$locale}/"
: getcwd() . self::MAIN_LANGUAGE_REPO;
$sets = [];
$files = directory_map($location, 1);
foreach ($files as $file) {
$sets[$file] = $file;
}
return $sets;
}
}