Version 1
[yaffs-website] / web / core / modules / node / tests / src / Functional / NodeTypeTranslationTest.php
1 <?php
2
3 namespace Drupal\Tests\node\Functional;
4
5 use Drupal\Component\Utility\Unicode;
6 use Drupal\language\Entity\ConfigurableLanguage;
7 use Drupal\Tests\BrowserTestBase;
8
9 /**
10  * Ensures that node types translation work correctly.
11  *
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.
15  *
16  * @group node
17  */
18 class NodeTypeTranslationTest extends BrowserTestBase {
19
20   /**
21    * Modules to enable.
22    *
23    * @var array
24    */
25   public static $modules = [
26     'block',
27     'config_translation',
28     'field_ui',
29     'node',
30   ];
31
32   /**
33    * The default language code to use in this test.
34    *
35    * @var array
36    */
37   protected $defaultLangcode = 'fr';
38
39   /**
40    * Languages to enable.
41    *
42    * @var array
43    */
44   protected $additionalLangcodes = ['es'];
45
46   /**
47    * Administrator user for tests.
48    *
49    * @var \Drupal\user\UserInterface
50    */
51   protected $adminUser;
52
53   protected function setUp() {
54     parent::setUp();
55
56     $admin_permissions = [
57       'administer content types',
58       'administer node fields',
59       'administer languages',
60       'administer site configuration',
61       'administer themes',
62       'translate configuration',
63     ];
64
65     // Create and log in user.
66     $this->adminUser = $this->drupalCreateUser($admin_permissions);
67
68     // Add languages.
69     foreach ($this->additionalLangcodes as $langcode) {
70       ConfigurableLanguage::createFromLangcode($langcode)->save();
71     }
72   }
73
74   /**
75    * {@inheritdoc}
76    *
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
79    * test.
80    *
81    * @see https://www.drupal.org/node/2584603
82    */
83   protected function installParameters() {
84     $parameters = parent::installParameters();
85     $parameters['parameters']['langcode'] = $this->defaultLangcode;
86     return $parameters;
87   }
88
89   /**
90    * Tests the node type translation.
91    */
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]);
97
98     // Translate the node type name.
99     $langcode = $this->additionalLangcodes[0];
100     $translated_name = $langcode . '-' . $name;
101     $edit = [
102       "translation[config_names][node.type.$type][name]" => $translated_name,
103     ];
104
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'));
107
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]));
114
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]));
121   }
122
123   /**
124    * Tests the node type title label translation.
125    */
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];
132
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'));
135
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');
139
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'));
142
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
146     // provided.
147     $this->drupalGet("node/add/$type");
148     $this->assertText('Edited title');
149     $this->drupalGet("$langcode/node/add/$type");
150     $this->assertText('Translated title');
151
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');
156
157     $type = Unicode::strtolower($this->randomMachineName(16));
158     $name = $this->randomString();
159     $this->drupalCreateContentType(['type' => $type, 'name' => $name]);
160
161     // Set tabs.
162     $this->drupalPlaceBlock('local_tasks_block', ['primary' => TRUE]);
163
164     // Change default language.
165     $this->drupalPostForm('admin/config/regional/language', ['site_default_language' => 'es'], 'Save configuration');
166
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");
174   }
175
176 }