Backup of db before drupal security update
[yaffs-website] / web / core / modules / locale / src / Tests / LocaleExportTest.php
1 <?php
2
3 namespace Drupal\locale\Tests;
4
5 use Drupal\simpletest\WebTestBase;
6
7 /**
8  * Tests the exportation of locale files.
9  *
10  * @group locale
11  */
12 class LocaleExportTest extends WebTestBase {
13
14   /**
15    * Modules to enable.
16    *
17    * @var array
18    */
19   public static $modules = ['locale'];
20
21   /**
22    * A user able to create languages and export translations.
23    */
24   protected $adminUser = NULL;
25
26   /**
27    * {@inheritdoc}
28    */
29   protected function setUp() {
30     parent::setUp();
31
32     $this->adminUser = $this->drupalCreateUser(['administer languages', 'translate interface', 'access administration pages']);
33     $this->drupalLogin($this->adminUser);
34
35     // Copy test po files to the translations directory.
36     file_unmanaged_copy(__DIR__ . '/../../tests/test.de.po', 'translations://', FILE_EXISTS_REPLACE);
37     file_unmanaged_copy(__DIR__ . '/../../tests/test.xx.po', 'translations://', FILE_EXISTS_REPLACE);
38   }
39
40   /**
41    * Test exportation of translations.
42    */
43   public function testExportTranslation() {
44     // First import some known translations.
45     // This will also automatically add the 'fr' language.
46     $name = \Drupal::service('file_system')->tempnam('temporary://', "po_") . '.po';
47     file_put_contents($name, $this->getPoFile());
48     $this->drupalPostForm('admin/config/regional/translate/import', [
49       'langcode' => 'fr',
50       'files[file]' => $name,
51     ], t('Import'));
52     drupal_unlink($name);
53
54     // Get the French translations.
55     $this->drupalPostForm('admin/config/regional/translate/export', [
56       'langcode' => 'fr',
57     ], t('Export'));
58
59     // Ensure we have a translation file.
60     $this->assertRaw('# French translation of Drupal', 'Exported French translation file.');
61     // Ensure our imported translations exist in the file.
62     $this->assertRaw('msgstr "lundi"', 'French translations present in exported file.');
63
64     // Import some more French translations which will be marked as customized.
65     $name = \Drupal::service('file_system')->tempnam('temporary://', "po2_") . '.po';
66     file_put_contents($name, $this->getCustomPoFile());
67     $this->drupalPostForm('admin/config/regional/translate/import', [
68       'langcode' => 'fr',
69       'files[file]' => $name,
70       'customized' => 1,
71     ], t('Import'));
72     drupal_unlink($name);
73
74     // Create string without translation in the locales_source table.
75     $this->container
76       ->get('locale.storage')
77       ->createString()
78       ->setString('February')
79       ->save();
80
81     // Export only customized French translations.
82     $this->drupalPostForm('admin/config/regional/translate/export', [
83       'langcode' => 'fr',
84       'content_options[not_customized]' => FALSE,
85       'content_options[customized]' => TRUE,
86       'content_options[not_translated]' => FALSE,
87     ], t('Export'));
88
89     // Ensure we have a translation file.
90     $this->assertRaw('# French translation of Drupal', 'Exported French translation file with only customized strings.');
91     // Ensure the customized translations exist in the file.
92     $this->assertRaw('msgstr "janvier"', 'French custom translation present in exported file.');
93     // Ensure no untranslated strings exist in the file.
94     $this->assertNoRaw('msgid "February"', 'Untranslated string not present in exported file.');
95
96     // Export only untranslated French translations.
97     $this->drupalPostForm('admin/config/regional/translate/export', [
98       'langcode' => 'fr',
99       'content_options[not_customized]' => FALSE,
100       'content_options[customized]' => FALSE,
101       'content_options[not_translated]' => TRUE,
102     ], t('Export'));
103
104     // Ensure we have a translation file.
105     $this->assertRaw('# French translation of Drupal', 'Exported French translation file with only untranslated strings.');
106     // Ensure no customized translations exist in the file.
107     $this->assertNoRaw('msgstr "janvier"', 'French custom translation not present in exported file.');
108     // Ensure the untranslated strings exist in the file, and with right quotes.
109     $this->assertRaw($this->getUntranslatedString(), 'Empty string present in exported file.');
110   }
111
112   /**
113    * Test exportation of translation template file.
114    */
115   public function testExportTranslationTemplateFile() {
116     // Load an admin page with JavaScript so _drupal_add_library() fires at
117     // least once and _locale_parse_js_file() gets to run at least once so that
118     // the locales_source table gets populated with something.
119     $this->drupalGet('admin/config/regional/language');
120     // Get the translation template file.
121     $this->drupalPostForm('admin/config/regional/translate/export', [], t('Export'));
122     // Ensure we have a translation file.
123     $this->assertRaw('# LANGUAGE translation of PROJECT', 'Exported translation template file.');
124   }
125
126   /**
127    * Helper function that returns a proper .po file.
128    */
129   public function getPoFile() {
130     return <<< EOF
131 msgid ""
132 msgstr ""
133 "Project-Id-Version: Drupal 8\\n"
134 "MIME-Version: 1.0\\n"
135 "Content-Type: text/plain; charset=UTF-8\\n"
136 "Content-Transfer-Encoding: 8bit\\n"
137 "Plural-Forms: nplurals=2; plural=(n > 1);\\n"
138
139 msgid "Monday"
140 msgstr "lundi"
141 EOF;
142   }
143
144   /**
145    * Helper function that returns a .po file which strings will be marked
146    * as customized.
147    */
148   public function getCustomPoFile() {
149     return <<< EOF
150 msgid ""
151 msgstr ""
152 "Project-Id-Version: Drupal 8\\n"
153 "MIME-Version: 1.0\\n"
154 "Content-Type: text/plain; charset=UTF-8\\n"
155 "Content-Transfer-Encoding: 8bit\\n"
156 "Plural-Forms: nplurals=2; plural=(n > 1);\\n"
157
158 msgid "January"
159 msgstr "janvier"
160 EOF;
161   }
162
163   /**
164    * Returns a .po file fragment with an untranslated string.
165    *
166    * @return string
167    *   A .po file fragment with an untranslated string.
168    */
169   public function getUntranslatedString() {
170     return <<< EOF
171 msgid "February"
172 msgstr ""
173 EOF;
174   }
175
176 }