PageRenderTime 53ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/web/core/modules/locale/tests/src/Functional/LocaleConfigTranslationImportTest.php

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