3 namespace Drupal\Tests\node\Functional;
5 use Drupal\Component\Utility\Unicode;
6 use Drupal\language\Entity\ConfigurableLanguage;
7 use Drupal\Tests\BrowserTestBase;
10 * Ensures that node types translation work correctly.
12 * Note that the child site is installed in French; therefore, when making
13 * assertions on translated text it is important to provide a langcode. This
14 * ensures the asserts pass regardless of the Drupal version.
18 class NodeTypeTranslationTest extends BrowserTestBase {
25 public static $modules = [
33 * The default language code to use in this test.
37 protected $defaultLangcode = 'fr';
40 * Languages to enable.
44 protected $additionalLangcodes = ['es'];
47 * Administrator user for tests.
49 * @var \Drupal\user\UserInterface
53 protected function setUp() {
56 $admin_permissions = [
57 'administer content types',
58 'administer node fields',
59 'administer languages',
60 'administer site configuration',
62 'translate configuration',
65 // Create and log in user.
66 $this->adminUser = $this->drupalCreateUser($admin_permissions);
69 foreach ($this->additionalLangcodes as $langcode) {
70 ConfigurableLanguage::createFromLangcode($langcode)->save();
77 * Install Drupal in a language other than English for this test. This is not
78 * needed to test the node type translation itself but acts as a regression
81 * @see https://www.drupal.org/node/2584603
83 protected function installParameters() {
84 $parameters = parent::installParameters();
85 $parameters['parameters']['langcode'] = $this->defaultLangcode;
90 * Tests the node type translation.
92 public function testNodeTypeTranslation() {
93 $type = Unicode::strtolower($this->randomMachineName(16));
94 $name = $this->randomString();
95 $this->drupalLogin($this->adminUser);
96 $this->drupalCreateContentType(['type' => $type, 'name' => $name]);
98 // Translate the node type name.
99 $langcode = $this->additionalLangcodes[0];
100 $translated_name = $langcode . '-' . $name;
102 "translation[config_names][node.type.$type][name]" => $translated_name,
105 // Edit the title label to avoid having an exception when we save the translation.
106 $this->drupalPostForm("admin/structure/types/manage/$type/translate/$langcode/add", $edit, t('Save translation'));
108 // Check the name is translated without admin theme for editing.
109 $this->drupalPostForm('admin/appearance', ['use_admin_theme' => '0'], t('Save configuration'));
110 $this->drupalGet("$langcode/node/add/$type");
111 // This is a Spanish page, so ensure the text asserted is translated in
112 // Spanish and not French by adding the langcode option.
113 $this->assertRaw(t('Create @name', ['@name' => $translated_name], ['langcode' => $langcode]));
115 // Check the name is translated with admin theme for editing.
116 $this->drupalPostForm('admin/appearance', ['use_admin_theme' => '1'], t('Save configuration'));
117 $this->drupalGet("$langcode/node/add/$type");
118 // This is a Spanish page, so ensure the text asserted is translated in
119 // Spanish and not French by adding the langcode option.
120 $this->assertRaw(t('Create @name', ['@name' => $translated_name], ['langcode' => $langcode]));
124 * Tests the node type title label translation.
126 public function testNodeTypeTitleLabelTranslation() {
127 $type = Unicode::strtolower($this->randomMachineName(16));
128 $name = $this->randomString();
129 $this->drupalLogin($this->adminUser);
130 $this->drupalCreateContentType(['type' => $type, 'name' => $name]);
131 $langcode = $this->additionalLangcodes[0];
133 // Edit the title label for it to be displayed on the translation form.
134 $this->drupalPostForm("admin/structure/types/manage/$type", ['title_label' => 'Edited title'], t('Save content type'));
136 // Assert that the title label is displayed on the translation form with the right value.
137 $this->drupalGet("admin/structure/types/manage/$type/translate/$langcode/add");
138 $this->assertText('Edited title');
140 // Translate the title label.
141 $this->drupalPostForm(NULL, ["translation[config_names][core.base_field_override.node.$type.title][label]" => 'Translated title'], t('Save translation'));
143 // Assert that the right title label is displayed on the node add form. The
144 // translations are created in this test; therefore, the assertions do not
145 // use t(). If t() were used then the correct langcodes would need to be
147 $this->drupalGet("node/add/$type");
148 $this->assertText('Edited title');
149 $this->drupalGet("$langcode/node/add/$type");
150 $this->assertText('Translated title');
152 // Add an e-mail field.
153 $this->drupalPostForm("admin/structure/types/manage/$type/fields/add-field", ['new_storage_type' => 'email', 'label' => 'Email', 'field_name' => 'email'], 'Save and continue');
154 $this->drupalPostForm(NULL, [], 'Save field settings');
155 $this->drupalPostForm(NULL, [], 'Save settings');
157 $type = Unicode::strtolower($this->randomMachineName(16));
158 $name = $this->randomString();
159 $this->drupalCreateContentType(['type' => $type, 'name' => $name]);
162 $this->drupalPlaceBlock('local_tasks_block', ['primary' => TRUE]);
164 // Change default language.
165 $this->drupalPostForm('admin/config/regional/language', ['site_default_language' => 'es'], 'Save configuration');
167 // Try re-using the email field.
168 $this->drupalGet("es/admin/structure/types/manage/$type/fields/add-field");
169 $this->drupalPostForm(NULL, ['existing_storage_name' => 'field_email', 'existing_storage_label' => 'Email'], 'Save and continue');
170 $this->assertResponse(200);
171 $this->drupalGet("es/admin/structure/types/manage/$type/fields/node.$type.field_email/translate");
172 $this->assertResponse(200);
173 $this->assertText("The configuration objects have different language codes so they cannot be translated");