df7a5ffeea1f140a7ce557824bb519ba9a59d6bc
[yaffs-website] / web / core / modules / locale / tests / src / Functional / LocaleConfigTranslationImportTest.php
1 <?php
2
3 namespace Drupal\Tests\locale\Functional;
4
5 use Drupal\locale\Locale;
6 use Drupal\Tests\BrowserTestBase;
7 use Drupal\language\Entity\ConfigurableLanguage;
8
9 /**
10  * Tests translation update's effects on configuration translations.
11  *
12  * @group locale
13  */
14 class LocaleConfigTranslationImportTest extends BrowserTestBase {
15
16   /**
17    * Modules to enable.
18    *
19    * @var array
20    */
21   public static $modules = ['language', 'locale_test_translate'];
22
23   /**
24    * {@inheritdoc}
25    */
26   protected function setUp() {
27     parent::setUp();
28
29     // @todo Re-enable the test and only skip it when the translation cannot be
30     //   downloaded, or provide a translation as a fixture instead. See
31     //   https://www.drupal.org/project/drupal/issues/2828143.
32     $this->markTestSkipped();
33   }
34
35   /**
36    * Test update changes configuration translations if enabled after language.
37    */
38   public function testConfigTranslationImport() {
39     $admin_user = $this->drupalCreateUser(['administer modules', 'administer site configuration', 'administer languages', 'access administration pages', 'administer permissions']);
40     $this->drupalLogin($admin_user);
41
42     // Add a language. The Afrikaans translation file of locale_test_translate
43     // (test.af.po) has been prepared with a configuration translation.
44     ConfigurableLanguage::createFromLangcode('af')->save();
45
46     // Enable locale module.
47     $this->container->get('module_installer')->install(['locale']);
48     $this->resetAll();
49
50     // Enable import of translations. By default this is disabled for automated
51     // tests.
52     $this->config('locale.settings')
53       ->set('translation.import_enabled', TRUE)
54       ->save();
55
56     // Add translation permissions now that the locale module has been enabled.
57     $edit = [
58       'authenticated[translate interface]' => 'translate interface',
59     ];
60     $this->drupalPostForm('admin/people/permissions', $edit, t('Save permissions'));
61
62     // Check and update the translation status. This will import the Afrikaans
63     // translations of locale_test_translate module.
64     $this->drupalGet('admin/reports/translations/check');
65
66     // Override the Drupal core translation status to be up to date.
67     // Drupal core should not be a subject in this test.
68     $status = locale_translation_get_status();
69     $status['drupal']['af']->type = 'current';
70     \Drupal::state()->set('locale.translation_status', $status);
71
72     $this->drupalPostForm('admin/reports/translations', [], t('Update translations'));
73
74     // Check if configuration translations have been imported.
75     $override = \Drupal::languageManager()->getLanguageConfigOverride('af', 'system.maintenance');
76     $this->assertEqual($override->get('message'), 'Ons is tans besig met onderhoud op @site. Wees asseblief geduldig, ons sal binnekort weer terug wees.');
77   }
78
79   /**
80    * Test update changes configuration translations if enabled after language.
81    */
82   public function testConfigTranslationModuleInstall() {
83
84     // Enable locale, block and config_translation modules.
85     $this->container->get('module_installer')->install(['block', 'config_translation']);
86     $this->resetAll();
87
88     // The testing profile overrides locale.settings to disable translation
89     // import. Test that this override is in place.
90     $this->assertFalse($this->config('locale.settings')->get('translation.import_enabled'), 'Translations imports are disabled by default in the Testing profile.');
91
92     $admin_user = $this->drupalCreateUser(['administer modules', 'administer site configuration', 'administer languages', 'access administration pages', 'administer permissions', 'translate configuration']);
93     $this->drupalLogin($admin_user);
94
95     // Enable import of translations. By default this is disabled for automated
96     // tests.
97     $this->config('locale.settings')
98       ->set('translation.import_enabled', TRUE)
99       ->save();
100
101     // Add predefined language.
102     $this->drupalPostForm('admin/config/regional/language/add', ['predefined_langcode' => 'af'], t('Add language'));
103
104     // Add the system branding block to the page.
105     $this->drupalPlaceBlock('system_branding_block', ['region' => 'header', 'id' => 'site-branding']);
106     $this->drupalPostForm('admin/config/system/site-information', ['site_slogan' => 'Test site slogan'], 'Save configuration');
107     $this->drupalPostForm('admin/config/system/site-information/translate/af/edit', ['translation[config_names][system.site][slogan]' => 'Test site slogan in Afrikaans'], 'Save translation');
108
109     // Get the front page and ensure that the translated configuration appears.
110     $this->drupalGet('af');
111     $this->assertText('Test site slogan in Afrikaans');
112
113     $override = \Drupal::languageManager()->getLanguageConfigOverride('af', 'locale_test_translate.settings');
114     $this->assertEqual('Locale can translate Afrikaans', $override->get('translatable_default_with_translation'));
115
116     // Update test configuration.
117     $override
118       ->set('translatable_no_default', 'This translation is preserved')
119       ->set('translatable_default_with_translation', 'This translation is preserved')
120       ->set('translatable_default_with_no_translation', 'This translation is preserved')
121       ->save();
122
123     // Install any module.
124     $this->drupalPostForm('admin/modules', ['modules[dblog][enable]' => 'dblog'], t('Install'));
125     $this->assertText('Module Database Logging has been enabled.');
126
127     // Get the front page and ensure that the translated configuration still
128     // appears.
129     $this->drupalGet('af');
130     $this->assertText('Test site slogan in Afrikaans');
131
132     $this->rebuildContainer();
133     $override = \Drupal::languageManager()->getLanguageConfigOverride('af', 'locale_test_translate.settings');
134     $expected = [
135       'translatable_no_default' => 'This translation is preserved',
136       'translatable_default_with_translation' => 'This translation is preserved',
137       'translatable_default_with_no_translation' => 'This translation is preserved'
138     ];
139     $this->assertEqual($expected, $override->get());
140   }
141
142   /**
143    * Test removing a string from Locale deletes configuration translations.
144    */
145   public function testLocaleRemovalAndConfigOverrideDelete() {
146     // Enable the locale module.
147     $this->container->get('module_installer')->install(['locale']);
148     $this->resetAll();
149
150     $admin_user = $this->drupalCreateUser(['administer modules', 'administer site configuration', 'administer languages', 'access administration pages', 'administer permissions', 'translate interface']);
151     $this->drupalLogin($admin_user);
152
153     // Enable import of translations. By default this is disabled for automated
154     // tests.
155     $this->config('locale.settings')
156       ->set('translation.import_enabled', TRUE)
157       ->save();
158
159     // Add predefined language.
160     $this->drupalPostForm('admin/config/regional/language/add', ['predefined_langcode' => 'af'], t('Add language'));
161
162     $override = \Drupal::languageManager()->getLanguageConfigOverride('af', 'locale_test_translate.settings');
163     $this->assertEqual(['translatable_default_with_translation' => 'Locale can translate Afrikaans'], $override->get());
164
165     // Remove the string from translation to simulate a Locale removal. Note
166     // that is no current way of doing this in the UI.
167     $locale_storage = \Drupal::service('locale.storage');
168     $string = $locale_storage->findString(['source' => 'Locale can translate']);
169     \Drupal::service('locale.storage')->delete($string);
170     // Force a rebuild of config translations.
171     $count = Locale::config()->updateConfigTranslations(['locale_test_translate.settings'], ['af']);
172     $this->assertEqual($count, 1, 'Correct count of updated translations');
173
174     $override = \Drupal::languageManager()->getLanguageConfigOverride('af', 'locale_test_translate.settings');
175     $this->assertEqual([], $override->get());
176     $this->assertTrue($override->isNew(), 'The configuration override was deleted when the Locale string was deleted.');
177   }
178
179   /**
180    * Test removing a string from Locale changes configuration translations.
181    */
182   public function testLocaleRemovalAndConfigOverridePreserve() {
183     // Enable the locale module.
184     $this->container->get('module_installer')->install(['locale']);
185     $this->resetAll();
186
187     $admin_user = $this->drupalCreateUser(['administer modules', 'administer site configuration', 'administer languages', 'access administration pages', 'administer permissions', 'translate interface']);
188     $this->drupalLogin($admin_user);
189
190     // Enable import of translations. By default this is disabled for automated
191     // tests.
192     $this->config('locale.settings')
193       ->set('translation.import_enabled', TRUE)
194       ->save();
195
196     // Add predefined language.
197     $this->drupalPostForm('admin/config/regional/language/add', ['predefined_langcode' => 'af'], t('Add language'));
198
199     $override = \Drupal::languageManager()->getLanguageConfigOverride('af', 'locale_test_translate.settings');
200     // Update test configuration.
201     $override
202       ->set('translatable_no_default', 'This translation is preserved')
203       ->set('translatable_default_with_no_translation', 'This translation is preserved')
204       ->save();
205     $expected = [
206       'translatable_default_with_translation' => 'Locale can translate Afrikaans',
207       'translatable_no_default' => 'This translation is preserved',
208       'translatable_default_with_no_translation' => 'This translation is preserved'
209     ];
210     $this->assertEqual($expected, $override->get());
211
212     // Set the translated string to empty.
213     $search = [
214       'string' => 'Locale can translate',
215       'langcode' => 'af',
216       'translation' => 'all',
217     ];
218     $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
219     $textareas = $this->xpath('//textarea');
220     $textarea = current($textareas);
221     $lid = $textarea->getAttribute('name');
222     $edit = [
223       $lid => '',
224     ];
225     $this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
226
227     $override = \Drupal::languageManager()->getLanguageConfigOverride('af', 'locale_test_translate.settings');
228     $expected = [
229       'translatable_no_default' => 'This translation is preserved',
230       'translatable_default_with_no_translation' => 'This translation is preserved'
231     ];
232     $this->assertEqual($expected, $override->get());
233   }
234
235 }