Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Tests\locale\Functional;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\StreamWrapper\PublicStream;
|
Chris@0
|
6 use Drupal\file\Entity\File;
|
Chris@0
|
7 use Drupal\Tests\BrowserTestBase;
|
Chris@0
|
8 use Drupal\Component\Utility\SafeMarkup;
|
Chris@0
|
9
|
Chris@0
|
10 /**
|
Chris@0
|
11 * Base class for testing updates to string translations.
|
Chris@0
|
12 */
|
Chris@0
|
13 abstract class LocaleUpdateBase extends BrowserTestBase {
|
Chris@0
|
14
|
Chris@0
|
15 /**
|
Chris@0
|
16 * Timestamp for an old translation.
|
Chris@0
|
17 *
|
Chris@0
|
18 * @var int
|
Chris@0
|
19 */
|
Chris@0
|
20 protected $timestampOld;
|
Chris@0
|
21
|
Chris@0
|
22 /**
|
Chris@0
|
23 * Timestamp for a medium aged translation.
|
Chris@0
|
24 *
|
Chris@0
|
25 * @var int
|
Chris@0
|
26 */
|
Chris@0
|
27 protected $timestampMedium;
|
Chris@0
|
28
|
Chris@0
|
29 /**
|
Chris@0
|
30 * Timestamp for a new translation.
|
Chris@0
|
31 *
|
Chris@0
|
32 * @var int
|
Chris@0
|
33 */
|
Chris@0
|
34 protected $timestampNew;
|
Chris@0
|
35
|
Chris@0
|
36 /**
|
Chris@0
|
37 * Timestamp for current time.
|
Chris@0
|
38 *
|
Chris@0
|
39 * @var int
|
Chris@0
|
40 */
|
Chris@0
|
41 protected $timestampNow;
|
Chris@0
|
42
|
Chris@0
|
43 /**
|
Chris@0
|
44 * Modules to enable.
|
Chris@0
|
45 *
|
Chris@0
|
46 * @var array
|
Chris@0
|
47 */
|
Chris@0
|
48 public static $modules = ['locale', 'locale_test'];
|
Chris@0
|
49
|
Chris@0
|
50 /**
|
Chris@0
|
51 * {@inheritdoc}
|
Chris@0
|
52 */
|
Chris@0
|
53 protected function setUp() {
|
Chris@0
|
54 parent::setUp();
|
Chris@0
|
55
|
Chris@0
|
56 // Setup timestamps to identify old and new translation sources.
|
Chris@0
|
57 $this->timestampOld = REQUEST_TIME - 300;
|
Chris@0
|
58 $this->timestampMedium = REQUEST_TIME - 200;
|
Chris@0
|
59 $this->timestampNew = REQUEST_TIME - 100;
|
Chris@0
|
60 $this->timestampNow = REQUEST_TIME;
|
Chris@0
|
61
|
Chris@0
|
62 // Enable import of translations. By default this is disabled for automated
|
Chris@0
|
63 // tests.
|
Chris@0
|
64 $this->config('locale.settings')
|
Chris@0
|
65 ->set('translation.import_enabled', TRUE)
|
Chris@0
|
66 ->save();
|
Chris@0
|
67 }
|
Chris@0
|
68
|
Chris@0
|
69 /**
|
Chris@0
|
70 * Sets the value of the default translations directory.
|
Chris@0
|
71 *
|
Chris@0
|
72 * @param string $path
|
Chris@0
|
73 * Path of the translations directory relative to the drupal installation
|
Chris@0
|
74 * directory.
|
Chris@0
|
75 */
|
Chris@0
|
76 protected function setTranslationsDirectory($path) {
|
Chris@0
|
77 file_prepare_directory($path, FILE_CREATE_DIRECTORY);
|
Chris@0
|
78 $this->config('locale.settings')->set('translation.path', $path)->save();
|
Chris@0
|
79 }
|
Chris@0
|
80
|
Chris@0
|
81 /**
|
Chris@0
|
82 * Adds a language.
|
Chris@0
|
83 *
|
Chris@0
|
84 * @param string $langcode
|
Chris@0
|
85 * The language code of the language to add.
|
Chris@0
|
86 */
|
Chris@0
|
87 protected function addLanguage($langcode) {
|
Chris@0
|
88 $edit = ['predefined_langcode' => $langcode];
|
Chris@0
|
89 $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
|
Chris@0
|
90 $this->container->get('language_manager')->reset();
|
Chris@0
|
91 $this->assertTrue(\Drupal::languageManager()->getLanguage($langcode), SafeMarkup::format('Language %langcode added.', ['%langcode' => $langcode]));
|
Chris@0
|
92 }
|
Chris@0
|
93
|
Chris@0
|
94 /**
|
Chris@0
|
95 * Creates a translation file and tests its timestamp.
|
Chris@0
|
96 *
|
Chris@0
|
97 * @param string $path
|
Chris@0
|
98 * Path of the file relative to the public file path.
|
Chris@0
|
99 * @param string $filename
|
Chris@0
|
100 * Name of the file to create.
|
Chris@0
|
101 * @param int $timestamp
|
Chris@0
|
102 * (optional) Timestamp to set the file to. Defaults to current time.
|
Chris@0
|
103 * @param array $translations
|
Chris@0
|
104 * (optional) Array of source/target value translation strings. Only
|
Chris@0
|
105 * singular strings are supported, no plurals. No double quotes are allowed
|
Chris@0
|
106 * in source and translations strings.
|
Chris@0
|
107 */
|
Chris@0
|
108 protected function makePoFile($path, $filename, $timestamp = NULL, array $translations = []) {
|
Chris@0
|
109 $timestamp = $timestamp ? $timestamp : REQUEST_TIME;
|
Chris@0
|
110 $path = 'public://' . $path;
|
Chris@0
|
111 $text = '';
|
Chris@0
|
112 $po_header = <<<EOF
|
Chris@0
|
113 msgid ""
|
Chris@0
|
114 msgstr ""
|
Chris@0
|
115 "Project-Id-Version: Drupal 8\\n"
|
Chris@0
|
116 "MIME-Version: 1.0\\n"
|
Chris@0
|
117 "Content-Type: text/plain; charset=UTF-8\\n"
|
Chris@0
|
118 "Content-Transfer-Encoding: 8bit\\n"
|
Chris@0
|
119 "Plural-Forms: nplurals=2; plural=(n > 1);\\n"
|
Chris@0
|
120
|
Chris@0
|
121 EOF;
|
Chris@0
|
122
|
Chris@0
|
123 // Convert array of translations to Gettext source and translation strings.
|
Chris@0
|
124 if ($translations) {
|
Chris@0
|
125 foreach ($translations as $source => $target) {
|
Chris@0
|
126 $text .= 'msgid "' . $source . '"' . "\n";
|
Chris@0
|
127 $text .= 'msgstr "' . $target . '"' . "\n";
|
Chris@0
|
128 }
|
Chris@0
|
129 }
|
Chris@0
|
130
|
Chris@0
|
131 file_prepare_directory($path, FILE_CREATE_DIRECTORY);
|
Chris@0
|
132 $file = File::create([
|
Chris@0
|
133 'uid' => 1,
|
Chris@0
|
134 'filename' => $filename,
|
Chris@0
|
135 'uri' => $path . '/' . $filename,
|
Chris@0
|
136 'filemime' => 'text/x-gettext-translation',
|
Chris@0
|
137 'timestamp' => $timestamp,
|
Chris@0
|
138 'status' => FILE_STATUS_PERMANENT,
|
Chris@0
|
139 ]);
|
Chris@0
|
140 file_put_contents($file->getFileUri(), $po_header . $text);
|
Chris@0
|
141 touch(drupal_realpath($file->getFileUri()), $timestamp);
|
Chris@0
|
142 $file->save();
|
Chris@0
|
143 }
|
Chris@0
|
144
|
Chris@0
|
145 /**
|
Chris@0
|
146 * Setup the environment containing local and remote translation files.
|
Chris@0
|
147 *
|
Chris@0
|
148 * Update tests require a simulated environment for local and remote files.
|
Chris@0
|
149 * Normally remote files are located at a remote server (e.g. ftp.drupal.org).
|
Chris@0
|
150 * For testing we can not rely on this. A directory in the file system of the
|
Chris@0
|
151 * test site is designated for remote files and is addressed using an absolute
|
Chris@0
|
152 * URL. Because Drupal does not allow files with a po extension to be accessed
|
Chris@0
|
153 * (denied in .htaccess) the translation files get a _po extension. Another
|
Chris@0
|
154 * directory is designated for local translation files.
|
Chris@0
|
155 *
|
Chris@0
|
156 * The environment is set up with the following files. File creation times are
|
Chris@0
|
157 * set to create different variations in test conditions.
|
Chris@0
|
158 * contrib_module_one
|
Chris@0
|
159 * - remote file: timestamp new
|
Chris@0
|
160 * - local file: timestamp old
|
Chris@0
|
161 * contrib_module_two
|
Chris@0
|
162 * - remote file: timestamp old
|
Chris@0
|
163 * - local file: timestamp new
|
Chris@0
|
164 * contrib_module_three
|
Chris@0
|
165 * - remote file: timestamp old
|
Chris@0
|
166 * - local file: timestamp old
|
Chris@0
|
167 * custom_module_one
|
Chris@0
|
168 * - local file: timestamp new
|
Chris@0
|
169 * Time stamp of current translation set by setCurrentTranslations() is always
|
Chris@0
|
170 * timestamp medium. This makes it easy to predict which translation will be
|
Chris@0
|
171 * imported.
|
Chris@0
|
172 */
|
Chris@0
|
173 protected function setTranslationFiles() {
|
Chris@0
|
174 $config = $this->config('locale.settings');
|
Chris@0
|
175
|
Chris@0
|
176 // A flag is set to let the locale_test module replace the project data with
|
Chris@0
|
177 // a set of test projects which match the below project files.
|
Chris@0
|
178 \Drupal::state()->set('locale.test_projects_alter', TRUE);
|
Chris@0
|
179 \Drupal::state()->set('locale.remove_core_project', FALSE);
|
Chris@0
|
180
|
Chris@0
|
181 // Setup the environment.
|
Chris@0
|
182 $public_path = PublicStream::basePath();
|
Chris@0
|
183 $this->setTranslationsDirectory($public_path . '/local');
|
Chris@0
|
184 $config->set('translation.default_filename', '%project-%version.%language._po')->save();
|
Chris@0
|
185
|
Chris@0
|
186 // Setting up sets of translations for the translation files.
|
Chris@0
|
187 $translations_one = ['January' => 'Januar_1', 'February' => 'Februar_1', 'March' => 'Marz_1'];
|
Chris@0
|
188 $translations_two = ['February' => 'Februar_2', 'March' => 'Marz_2', 'April' => 'April_2'];
|
Chris@0
|
189 $translations_three = ['April' => 'April_3', 'May' => 'Mai_3', 'June' => 'Juni_3'];
|
Chris@0
|
190
|
Chris@0
|
191 // Add a number of files to the local file system to serve as remote
|
Chris@0
|
192 // translation server and match the project definitions set in
|
Chris@0
|
193 // locale_test_locale_translation_projects_alter().
|
Chris@0
|
194 $this->makePoFile('remote/8.x/contrib_module_one', 'contrib_module_one-8.x-1.1.de._po', $this->timestampNew, $translations_one);
|
Chris@0
|
195 $this->makePoFile('remote/8.x/contrib_module_two', 'contrib_module_two-8.x-2.0-beta4.de._po', $this->timestampOld, $translations_two);
|
Chris@0
|
196 $this->makePoFile('remote/8.x/contrib_module_three', 'contrib_module_three-8.x-1.0.de._po', $this->timestampOld, $translations_three);
|
Chris@0
|
197
|
Chris@0
|
198 // Add a number of files to the local file system to serve as local
|
Chris@0
|
199 // translation files and match the project definitions set in
|
Chris@0
|
200 // locale_test_locale_translation_projects_alter().
|
Chris@0
|
201 $this->makePoFile('local', 'contrib_module_one-8.x-1.1.de._po', $this->timestampOld, $translations_one);
|
Chris@0
|
202 $this->makePoFile('local', 'contrib_module_two-8.x-2.0-beta4.de._po', $this->timestampNew, $translations_two);
|
Chris@0
|
203 $this->makePoFile('local', 'contrib_module_three-8.x-1.0.de._po', $this->timestampOld, $translations_three);
|
Chris@0
|
204 $this->makePoFile('local', 'custom_module_one.de.po', $this->timestampNew);
|
Chris@0
|
205 }
|
Chris@0
|
206
|
Chris@0
|
207 /**
|
Chris@0
|
208 * Setup existing translations in the database and set up the status of
|
Chris@0
|
209 * existing translations.
|
Chris@0
|
210 */
|
Chris@0
|
211 protected function setCurrentTranslations() {
|
Chris@0
|
212 // Add non customized translations to the database.
|
Chris@0
|
213 $langcode = 'de';
|
Chris@0
|
214 $context = '';
|
Chris@0
|
215 $non_customized_translations = [
|
Chris@0
|
216 'March' => 'Marz',
|
Chris@0
|
217 'June' => 'Juni',
|
Chris@0
|
218 ];
|
Chris@0
|
219 foreach ($non_customized_translations as $source => $translation) {
|
Chris@0
|
220 $string = $this->container->get('locale.storage')->createString([
|
Chris@0
|
221 'source' => $source,
|
Chris@0
|
222 'context' => $context,
|
Chris@0
|
223 ])
|
Chris@0
|
224 ->save();
|
Chris@0
|
225 $this->container->get('locale.storage')->createTranslation([
|
Chris@0
|
226 'lid' => $string->getId(),
|
Chris@0
|
227 'language' => $langcode,
|
Chris@0
|
228 'translation' => $translation,
|
Chris@0
|
229 'customized' => LOCALE_NOT_CUSTOMIZED,
|
Chris@0
|
230 ])->save();
|
Chris@0
|
231 }
|
Chris@0
|
232
|
Chris@0
|
233 // Add customized translations to the database.
|
Chris@0
|
234 $customized_translations = [
|
Chris@0
|
235 'January' => 'Januar_customized',
|
Chris@0
|
236 'February' => 'Februar_customized',
|
Chris@0
|
237 'May' => 'Mai_customized',
|
Chris@0
|
238 ];
|
Chris@0
|
239 foreach ($customized_translations as $source => $translation) {
|
Chris@0
|
240 $string = $this->container->get('locale.storage')->createString([
|
Chris@0
|
241 'source' => $source,
|
Chris@0
|
242 'context' => $context,
|
Chris@0
|
243 ])
|
Chris@0
|
244 ->save();
|
Chris@0
|
245 $this->container->get('locale.storage')->createTranslation([
|
Chris@0
|
246 'lid' => $string->getId(),
|
Chris@0
|
247 'language' => $langcode,
|
Chris@0
|
248 'translation' => $translation,
|
Chris@0
|
249 'customized' => LOCALE_CUSTOMIZED,
|
Chris@0
|
250 ])->save();
|
Chris@0
|
251 }
|
Chris@0
|
252
|
Chris@0
|
253 // Add a state of current translations in locale_files.
|
Chris@0
|
254 $default = [
|
Chris@0
|
255 'langcode' => $langcode,
|
Chris@0
|
256 'uri' => '',
|
Chris@0
|
257 'timestamp' => $this->timestampMedium,
|
Chris@0
|
258 'last_checked' => $this->timestampMedium,
|
Chris@0
|
259 ];
|
Chris@0
|
260 $data[] = [
|
Chris@0
|
261 'project' => 'contrib_module_one',
|
Chris@0
|
262 'filename' => 'contrib_module_one-8.x-1.1.de._po',
|
Chris@0
|
263 'version' => '8.x-1.1',
|
Chris@0
|
264 ];
|
Chris@0
|
265 $data[] = [
|
Chris@0
|
266 'project' => 'contrib_module_two',
|
Chris@0
|
267 'filename' => 'contrib_module_two-8.x-2.0-beta4.de._po',
|
Chris@0
|
268 'version' => '8.x-2.0-beta4',
|
Chris@0
|
269 ];
|
Chris@0
|
270 $data[] = [
|
Chris@0
|
271 'project' => 'contrib_module_three',
|
Chris@0
|
272 'filename' => 'contrib_module_three-8.x-1.0.de._po',
|
Chris@0
|
273 'version' => '8.x-1.0',
|
Chris@0
|
274 ];
|
Chris@0
|
275 $data[] = [
|
Chris@0
|
276 'project' => 'custom_module_one',
|
Chris@0
|
277 'filename' => 'custom_module_one.de.po',
|
Chris@0
|
278 'version' => '',
|
Chris@0
|
279 ];
|
Chris@0
|
280 foreach ($data as $file) {
|
Chris@0
|
281 $file = array_merge($default, $file);
|
Chris@0
|
282 db_insert('locale_file')->fields($file)->execute();
|
Chris@0
|
283 }
|
Chris@0
|
284 }
|
Chris@0
|
285
|
Chris@0
|
286 /**
|
Chris@0
|
287 * Checks the translation of a string.
|
Chris@0
|
288 *
|
Chris@0
|
289 * @param string $source
|
Chris@0
|
290 * Translation source string.
|
Chris@0
|
291 * @param string $translation
|
Chris@0
|
292 * Translation to check. Use empty string to check for a not existing
|
Chris@0
|
293 * translation.
|
Chris@0
|
294 * @param string $langcode
|
Chris@0
|
295 * Language code of the language to translate to.
|
Chris@0
|
296 * @param string $message
|
Chris@0
|
297 * (optional) A message to display with the assertion.
|
Chris@0
|
298 */
|
Chris@0
|
299 protected function assertTranslation($source, $translation, $langcode, $message = '') {
|
Chris@0
|
300 $db_translation = db_query('SELECT translation FROM {locales_target} lt INNER JOIN {locales_source} ls ON ls.lid = lt.lid WHERE ls.source = :source AND lt.language = :langcode', [':source' => $source, ':langcode' => $langcode])->fetchField();
|
Chris@0
|
301 $db_translation = $db_translation == FALSE ? '' : $db_translation;
|
Chris@0
|
302 $this->assertEqual($translation, $db_translation, $message ? $message : format_string('Correct translation of %source (%language)', ['%source' => $source, '%language' => $langcode]));
|
Chris@0
|
303 }
|
Chris@0
|
304
|
Chris@0
|
305 }
|