forked from drush-ops/drush
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LocaleTest.php
146 lines (125 loc) · 6.21 KB
/
LocaleTest.php
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
<?php
namespace Unish;
use Webmozart\PathUtil\Path;
/**
* @group slow
* @group pm
*/
class LocaleTest extends CommandUnishTestCase
{
/**
* File name of Gettext PO source file.
*
* @var string
*/
protected $sourceFile;
public function setup(): void
{
if (!$this->getSites()) {
$this->setUpDrupal(1, true);
}
$this->drush('pm:install', ['language', 'locale']);
$this->drush('language:add', ['nl'], ['skip-translations' => null]);
$this->sourceFile = Path::join(__DIR__, '/resources/drush_empty_module.nl.po');
$this->drush('locale:import', ['nl', $this->sourceFile]);
$this->assertTranslation('Drush Empty Module', 'NL Drush Empty Module', 'nl', 0);
}
public function tearDown(): void
{
// Disable the locale module to make sure the database tables of locale module are emptied between tests.
$this->drush('pm:uninstall', ['language', 'locale']);
}
public function testLocaleExport()
{
// Export standard translations.
$this->drush('locale:export', ['nl'], ['types' => 'not-customized']);
$this->assertGettextTranslation('Drush Empty Module', 'NL Drush Empty Module');
// Export customized translations.
$this->drush('sql:query', ["UPDATE locales_target SET translation = 'CUSTOM Drush Empty Module', customized = 1"]);
$this->assertTranslation('Drush Empty Module', 'CUSTOM Drush Empty Module', 'nl', 1);
$this->drush('locale:export', ['nl'], ['types' => 'customized']);
// Export untranslated strings.
$this->drush('sql:query', ["INSERT INTO locales_source (source) VALUES ('Something untranslated')"]);
$this->drush('locale:export', ['nl'], ['types' => 'not-translated']);
// Export all.
$this->drush('locale:export', ['nl']);
$this->assertGettextTranslation('Drush Empty Module', 'CUSTOM Drush Empty Module');
$this->assertGettextTranslation('Something untranslated', '');
// Export template file.
$this->drush('locale:export', ['nl'], ['template' => null]);
$this->assertGettextTranslation('Drush Empty Module', '');
$this->assertGettextTranslation('Something untranslated', '');
}
public function testLocaleImport()
{
// Import without override.
$this->drush('sql:query', ["UPDATE locales_target SET translation = 'NO Drush Empty Module'"]);
$this->assertTranslation('Drush Empty Module', 'NO Drush Empty Module', 'nl', 0);
$this->drush('locale:import', ['nl', $this->sourceFile], ['override' => 'none']);
$this->assertTranslation('Drush Empty Module', 'NO Drush Empty Module', 'nl', 0);
// Import with override.
$this->drush('sql:query', ["UPDATE locales_target SET translation = 'NO Drush Empty Module'"]);
$this->assertTranslation('Drush Empty Module', 'NO Drush Empty Module', 'nl', 0);
$this->drush('locale:import', ['nl', $this->sourceFile], ['override' => 'not-customized']);
$this->assertTranslation('Drush Empty Module', 'NL Drush Empty Module', 'nl', 0);
// Import without override of custom translation
$this->drush('sql:query', ["UPDATE locales_target SET translation = 'NO Drush Empty Module', customized = 1"]);
$this->assertTranslation('Drush Empty Module', 'NO Drush Empty Module', 'nl', 1);
$this->drush('locale:import', ['nl', $this->sourceFile], ['override' => 'not-customized']);
$this->assertTranslation('Drush Empty Module', 'NO Drush Empty Module', 'nl', 1);
// Import with override of custom translation.
$this->drush('sql:query', ["UPDATE locales_target SET translation = 'NO Drush Empty Module', customized = 1"]);
$this->assertTranslation('Drush Empty Module', 'NO Drush Empty Module', 'nl', 1);
$this->drush('locale:import', ['nl', $this->sourceFile], ['override' => 'customized']);
$this->assertTranslation('Drush Empty Module', 'NL Drush Empty Module', 'nl', 0);
// Import with override of custom translation as customized.
$this->drush('sql:query', ["UPDATE locales_target SET translation = 'NO Drush Empty Module', customized = 1"]);
$this->assertTranslation('Drush Empty Module', 'NO Drush Empty Module', 'nl', 1);
$this->drush('locale:import', ['nl', $this->sourceFile], ['type' => 'customized', 'override' => 'customized']);
$this->assertTranslation('Drush Empty Module', 'NL Drush Empty Module', 'nl', 1);
}
/**
* @param string $source
* @param string $translation
* @param string $langcode
* @param int $custom
* @param string $context
*/
private function assertTranslation($source, $translation, $langcode, $custom = 0, $context = '')
{
$this->drush('sql:query', ["SELECT ls.source, ls.context, lt.translation, lt.language, lt.customized FROM locales_source ls JOIN locales_target lt ON ls.lid = lt.lid WHERE ls.source = '$source' AND ls.context = '$context' AND lt.language = '$langcode'"]);
$output = $this->getOutputAsList();
$expected = "/$source.*$context.*$translation.*$langcode.*$custom/";
$this->assertMatchesRegularExpression($expected, array_pop($output));
}
/**
* @param $source
* @param $translation
* @param $langcode
*
* @throws \Exception
*/
private function assertGettextTranslation($source, $translation)
{
if (strlen($source) > 71 || strlen($translation) > 71) {
throw new \Exception('This assertion can handle strings up to 71 characters.');
}
$output = $this->getOutputAsList();
$expectedSource = "msgid \"$source\"";
$expectedTranslation = "msgstr \"$translation\"";
$sourceLine = 0;
// The gettext format has source (msgid) and translation (msgstr)
// strings on consecutive lines.
foreach ($output as $key => $row) {
if ($row === $expectedSource) {
$sourceLine = $key;
break;
}
}
if ($sourceLine) {
$this->assertEquals($expectedTranslation, $output[$sourceLine + 1]);
} else {
$this->fail(sprintf('Source string "%s" not found', $source));
}
}
}