Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / modules / contrib / metatag / src / Tests / MetatagNodeTranslationTest.php
1 <?php
2
3 namespace Drupal\metatag\Tests;
4
5 use Drupal\language\Entity\ConfigurableLanguage;
6 use Drupal\simpletest\WebTestBase;
7
8 /**
9  * Ensures that meta tag values are translated correctly on nodes.
10  *
11  * @group metatag
12  */
13 class MetatagNodeTranslationTest extends WebTestBase {
14
15   /**
16    * Modules to enable.
17    *
18    * @var array
19    */
20   public static $modules = [
21     'content_translation',
22     'field_ui',
23     'metatag',
24     'node',
25   ];
26
27   /**
28    * The default language code to use in this test.
29    *
30    * @var array
31    */
32   protected $defaultLangcode = 'fr';
33
34   /**
35    * Languages to enable.
36    *
37    * @var array
38    */
39   protected $additionalLangcodes = ['es'];
40
41   /**
42    * Administrator user for tests.
43    *
44    * @var \Drupal\user\UserInterface
45    */
46   protected $adminUser;
47
48   protected function setUp() {
49     parent::setUp();
50
51     $admin_permissions = [
52       'administer content types',
53       'administer content translation',
54       'administer languages',
55       'administer nodes',
56       'administer node fields',
57       'bypass node access',
58       'create content translations',
59       'delete content translations',
60       'translate any entity',
61       'update content translations',
62     ];
63
64     // Create and login user.
65     $this->adminUser = $this->drupalCreateUser($admin_permissions);
66
67     // Add languages.
68     foreach ($this->additionalLangcodes as $langcode) {
69       ConfigurableLanguage::createFromLangcode($langcode)->save();
70     }
71   }
72
73   /**
74    * Tests the metatag value translations.
75    */
76   public function testMetatagValueTranslation() {
77     if (floatval(\Drupal::VERSION) <= 8.3) {
78       $save_label = t('Save and publish');
79       $save_label_i18n = t('Save and keep published (this translation)');
80     }
81     else {
82       $save_label = t('Save');
83       $save_label_i18n = t('Save (this translation)');
84     }
85
86     // Set up a content type.
87     $name = $this->randomMachineName() . ' ' . $this->randomMachineName();
88     $this->drupalLogin($this->adminUser);
89     $this->drupalCreateContentType(['type' => 'metatag_node', 'name' => $name]);
90
91     // Add a metatag field to the content type.
92     $this->drupalGet('admin/structure/types');
93     $this->assertResponse(200);
94     $this->drupalGet('admin/structure/types/manage/metatag_node');
95     $this->assertResponse(200);
96     $edit = [
97       'language_configuration[language_alterable]' => TRUE,
98       'language_configuration[content_translation]' => TRUE,
99     ];
100     $this->drupalPostForm(NULL, $edit, t('Save content type'));
101     $this->assertResponse(200);
102
103     $this->drupalGet('admin/structure/types/manage/metatag_node/fields/add-field');
104     $this->assertResponse(200);
105     $edit = [
106       'label' => 'Meta tags',
107       'field_name' => 'meta_tags',
108       'new_storage_type' => 'metatag',
109     ];
110     $this->drupalPostForm(NULL, $edit, t('Save and continue'));
111     $this->assertResponse(200);
112     $this->drupalPostForm(NULL, [], t('Save field settings'));
113     $this->assertResponse(200);
114     $edit = [
115       'translatable' => TRUE,
116     ];
117     $this->drupalPostForm(NULL, $edit, t('Save settings'));
118     $this->assertResponse(200);
119     $this->drupalGet('admin/structure/types/manage/metatag_node/fields/node.metatag_node.field_meta_tags');
120     $this->assertResponse(200);
121
122     // Set up a node without explicit metatag description. This causes the
123     // global default to be used, which contains a token (node:summary). The
124     // token value should be correctly translated.
125
126     // Load the node form.
127     $this->drupalGet('node/add/metatag_node');
128     $this->assertResponse(200);
129
130     // Check the default values are correct.
131     $this->assertFieldByName('field_meta_tags[0][basic][title]', '[node:title] | [site:name]', 'Default title token is present.');
132     $this->assertFieldByName('field_meta_tags[0][basic][description]', '[node:summary]', 'Default description token is present.');
133
134     // Create a node.
135     $edit = [
136       'title[0][value]' => 'Node Français',
137       'body[0][value]' => 'French summary.',
138     ];
139     $this->drupalPostForm(NULL, $edit, $save_label);
140     $this->assertResponse(200);
141
142     $xpath = $this->xpath("//meta[@name='description']");
143     $this->assertEqual(count($xpath), 1, 'Exactly one description meta tag found.');
144     $value = (string) $xpath[0]['content'];
145     $this->assertEqual($value, 'French summary.');
146
147     $this->drupalGet('node/1/translations/add/en/es');
148     $this->assertResponse(200);
149     // Check the default values are there.
150     $this->assertFieldByName('field_meta_tags[0][basic][title]', '[node:title] | [site:name]', 'Default title token is present.');
151     $this->assertFieldByName('field_meta_tags[0][basic][description]', '[node:summary]', 'Default description token is present.');
152
153     $edit = [
154       'title[0][value]' => 'Node Español',
155       'body[0][value]' => 'Spanish summary.',
156     ];
157     $this->drupalPostForm(NULL, $edit, $save_label_i18n);
158     $this->assertResponse(200);
159
160     $this->drupalGet('es/node/1');
161     $this->assertResponse(200);
162     $xpath = $this->xpath("//meta[@name='description']");
163     $this->assertEqual(count($xpath), 1, 'Exactly one description meta tag found.');
164     $value = (string) $xpath[0]['content'];
165     $this->assertEqual($value, 'Spanish summary.');
166     $this->assertNotEqual($value, 'French summary.');
167
168     $this->drupalGet('node/1/edit');
169     $this->assertResponse(200);
170     // Check the default values are there.
171     $this->assertFieldByName('field_meta_tags[0][basic][title]', '[node:title] | [site:name]', 'Default title token is present.');
172     $this->assertFieldByName('field_meta_tags[0][basic][description]', '[node:summary]', 'Default description token is present.');
173
174     // Set explicit values on the description metatag instead using the
175     // defaults.
176     $this->drupalGet('node/1/edit');
177     $this->assertResponse(200);
178     $edit = [
179       'field_meta_tags[0][basic][description]' => 'Overridden French description.',
180     ];
181     $this->drupalPostForm(NULL, $edit, $save_label_i18n);
182     $this->assertResponse(200);
183
184     $xpath = $this->xpath("//meta[@name='description']");
185     $this->assertEqual(count($xpath), 1, 'Exactly one description meta tag found.');
186     $value = (string) $xpath[0]['content'];
187     $this->assertEqual($value, 'Overridden French description.');
188     $this->assertNotEqual($value, 'Spanish summary.');
189     $this->assertNotEqual($value, 'French summary.');
190
191     $this->drupalGet('es/node/1/edit');
192     $this->assertResponse(200);
193     $edit = [
194       'field_meta_tags[0][basic][description]' => 'Overridden Spanish description.',
195     ];
196     $this->drupalPostForm(NULL, $edit, $save_label_i18n);
197     $this->assertResponse(200);
198
199     $xpath = $this->xpath("//meta[@name='description']");
200     $this->assertEqual(count($xpath), 1, 'Exactly one description meta tag found.');
201     $value = (string) $xpath[0]['content'];
202     $this->assertEqual($value, 'Overridden Spanish description.');
203     $this->assertNotEqual($value, 'Spanish summary.');
204     $this->assertNotEqual($value, 'French summary.');
205   }
206
207 }