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

https://gitlab.com/mohamed_hussein/prodt · PHP · 191 lines · 107 code · 25 blank · 59 comment · 0 complexity · c1b6e51ece2d24e0c80471ed29e2636d MD5 · raw file

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