NodeTypeTranslationTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. namespace Drupal\Tests\node\Functional;
  3. use Drupal\language\Entity\ConfigurableLanguage;
  4. use Drupal\Tests\BrowserTestBase;
  5. /**
  6. * Ensures that node types translation work correctly.
  7. *
  8. * Note that the child site is installed in French; therefore, when making
  9. * assertions on translated text it is important to provide a langcode. This
  10. * ensures the asserts pass regardless of the Drupal version.
  11. *
  12. * @group node
  13. */
  14. class NodeTypeTranslationTest extends BrowserTestBase {
  15. /**
  16. * Modules to enable.
  17. *
  18. * @var array
  19. */
  20. public static $modules = [
  21. 'block',
  22. 'config_translation',
  23. 'field_ui',
  24. 'node',
  25. ];
  26. /**
  27. * The default language code to use in this test.
  28. *
  29. * @var array
  30. */
  31. protected $defaultLangcode = 'fr';
  32. /**
  33. * Languages to enable.
  34. *
  35. * @var array
  36. */
  37. protected $additionalLangcodes = ['es'];
  38. /**
  39. * Administrator user for tests.
  40. *
  41. * @var \Drupal\user\UserInterface
  42. */
  43. protected $adminUser;
  44. protected function setUp() {
  45. parent::setUp();
  46. $admin_permissions = [
  47. 'administer content types',
  48. 'administer node fields',
  49. 'administer languages',
  50. 'administer site configuration',
  51. 'administer themes',
  52. 'translate configuration',
  53. ];
  54. // Create and log in user.
  55. $this->adminUser = $this->drupalCreateUser($admin_permissions);
  56. // Add languages.
  57. foreach ($this->additionalLangcodes as $langcode) {
  58. ConfigurableLanguage::createFromLangcode($langcode)->save();
  59. }
  60. }
  61. /**
  62. * {@inheritdoc}
  63. *
  64. * Install Drupal in a language other than English for this test. This is not
  65. * needed to test the node type translation itself but acts as a regression
  66. * test.
  67. *
  68. * @see https://www.drupal.org/node/2584603
  69. */
  70. protected function installParameters() {
  71. $parameters = parent::installParameters();
  72. $parameters['parameters']['langcode'] = $this->defaultLangcode;
  73. // Create an empty po file so we don't attempt to download one from
  74. // localize.drupal.org. It does not need to match the version exactly as the
  75. // multi-lingual system will fallback.
  76. \Drupal::service('file_system')->mkdir($this->publicFilesDirectory . '/translations', NULL, TRUE);
  77. file_put_contents($this->publicFilesDirectory . "/translations/drupal-8.0.0.{$this->defaultLangcode}.po", '');
  78. return $parameters;
  79. }
  80. /**
  81. * Tests the node type translation.
  82. */
  83. public function testNodeTypeTranslation() {
  84. $type = mb_strtolower($this->randomMachineName(16));
  85. $name = $this->randomString();
  86. $this->drupalLogin($this->adminUser);
  87. $this->drupalCreateContentType(['type' => $type, 'name' => $name]);
  88. // Translate the node type name.
  89. $langcode = $this->additionalLangcodes[0];
  90. $translated_name = $langcode . '-' . $name;
  91. $edit = [
  92. "translation[config_names][node.type.$type][name]" => $translated_name,
  93. ];
  94. // Edit the title label to avoid having an exception when we save the translation.
  95. $this->drupalPostForm("admin/structure/types/manage/$type/translate/$langcode/add", $edit, t('Save translation'));
  96. // Check the name is translated without admin theme for editing.
  97. $this->drupalPostForm('admin/appearance', ['use_admin_theme' => '0'], t('Save configuration'));
  98. $this->drupalGet("$langcode/node/add/$type");
  99. // This is a Spanish page, so ensure the text asserted is translated in
  100. // Spanish and not French by adding the langcode option.
  101. $this->assertRaw(t('Create @name', ['@name' => $translated_name], ['langcode' => $langcode]));
  102. // Check the name is translated with admin theme for editing.
  103. $this->drupalPostForm('admin/appearance', ['use_admin_theme' => '1'], t('Save configuration'));
  104. $this->drupalGet("$langcode/node/add/$type");
  105. // This is a Spanish page, so ensure the text asserted is translated in
  106. // Spanish and not French by adding the langcode option.
  107. $this->assertRaw(t('Create @name', ['@name' => $translated_name], ['langcode' => $langcode]));
  108. }
  109. /**
  110. * Tests the node type title label translation.
  111. */
  112. public function testNodeTypeTitleLabelTranslation() {
  113. $type = mb_strtolower($this->randomMachineName(16));
  114. $name = $this->randomString();
  115. $this->drupalLogin($this->adminUser);
  116. $this->drupalCreateContentType(['type' => $type, 'name' => $name]);
  117. $langcode = $this->additionalLangcodes[0];
  118. // Edit the title label for it to be displayed on the translation form.
  119. $this->drupalPostForm("admin/structure/types/manage/$type", ['title_label' => 'Edited title'], t('Save content type'));
  120. // Assert that the title label is displayed on the translation form with the right value.
  121. $this->drupalGet("admin/structure/types/manage/$type/translate/$langcode/add");
  122. $this->assertText('Edited title');
  123. // Translate the title label.
  124. $this->drupalPostForm(NULL, ["translation[config_names][core.base_field_override.node.$type.title][label]" => 'Translated title'], t('Save translation'));
  125. // Assert that the right title label is displayed on the node add form. The
  126. // translations are created in this test; therefore, the assertions do not
  127. // use t(). If t() were used then the correct langcodes would need to be
  128. // provided.
  129. $this->drupalGet("node/add/$type");
  130. $this->assertText('Edited title');
  131. $this->drupalGet("$langcode/node/add/$type");
  132. $this->assertText('Translated title');
  133. // Add an e-mail field.
  134. $this->drupalPostForm("admin/structure/types/manage/$type/fields/add-field", ['new_storage_type' => 'email', 'label' => 'Email', 'field_name' => 'email'], 'Save and continue');
  135. $this->drupalPostForm(NULL, [], 'Save field settings');
  136. $this->drupalPostForm(NULL, [], 'Save settings');
  137. $type = mb_strtolower($this->randomMachineName(16));
  138. $name = $this->randomString();
  139. $this->drupalCreateContentType(['type' => $type, 'name' => $name]);
  140. // Set tabs.
  141. $this->drupalPlaceBlock('local_tasks_block', ['primary' => TRUE]);
  142. // Change default language.
  143. $this->drupalPostForm('admin/config/regional/language', ['site_default_language' => 'es'], 'Save configuration');
  144. // Try re-using the email field.
  145. $this->drupalGet("es/admin/structure/types/manage/$type/fields/add-field");
  146. $this->drupalPostForm(NULL, ['existing_storage_name' => 'field_email', 'existing_storage_label' => 'Email'], 'Save and continue');
  147. $this->assertResponse(200);
  148. $this->drupalGet("es/admin/structure/types/manage/$type/fields/node.$type.field_email/translate");
  149. $this->assertResponse(200);
  150. $this->assertText("The configuration objects have different language codes so they cannot be translated");
  151. }
  152. }