3 namespace Drupal\metatag\Tests;
5 use Drupal\language\Entity\ConfigurableLanguage;
6 use Drupal\simpletest\WebTestBase;
9 * Ensures that meta tag values are translated correctly on nodes.
13 class MetatagNodeTranslationTest extends WebTestBase {
20 public static $modules = [
21 'content_translation',
28 * The default language code to use in this test.
32 protected $defaultLangcode = 'fr';
35 * Languages to enable.
39 protected $additionalLangcodes = ['es'];
42 * Administrator user for tests.
44 * @var \Drupal\user\UserInterface
48 protected function setUp() {
51 $admin_permissions = [
52 'administer content types',
53 'administer content translation',
54 'administer languages',
56 'administer node fields',
58 'create content translations',
59 'delete content translations',
60 'translate any entity',
61 'update content translations',
64 // Create and login user.
65 $this->adminUser = $this->drupalCreateUser($admin_permissions);
68 foreach ($this->additionalLangcodes as $langcode) {
69 ConfigurableLanguage::createFromLangcode($langcode)->save();
74 * Tests the metatag value translations.
76 public function testMetatagValueTranslation() {
77 // Set up a content type.
78 $name = $this->randomString();
79 $this->drupalLogin($this->adminUser);
80 $this->drupalCreateContentType(['type' => 'metatag_node', 'name' => $name]);
82 // Add a metatag field to the content type.
83 $this->drupalGet('admin/structure/types');
84 $this->assertResponse(200);
85 $this->drupalGet('admin/structure/types/manage/metatag_node/fields/add-field');
86 $this->assertResponse(200);
89 'field_name' => 'metatag_field',
90 'new_storage_type' => 'metatag',
92 $this->drupalPostForm(NULL, $edit, t('Save and continue'));
93 $this->drupalPostForm(NULL, [], t('Save field settings'));
94 $this->container->get('entity.manager')->clearCachedFieldDefinitions();
96 // Enable translation for our test content type.
97 $this->drupalGet('admin/config/regional/content-language');
98 $this->assertResponse(200);
100 'entity_types[node]' => 1,
101 'settings[node][metatag_node][translatable]' => 1,
102 'settings[node][metatag_node][translatable]' => 1,
103 'settings[node][metatag_node][fields][field_metatag_field]' => 1,
105 $this->drupalPostForm(NULL, $edit, t('Save configuration'));
107 $this->drupalGet('admin/structure/types/manage/metatag_node');
108 $this->assertResponse(200);
110 // Set up a node without explicit metatag description. This causes the
111 // global default to be used, which contains a token (node:summary). The
112 // token value should be correctly translated.
114 // Load the node form.
115 $this->drupalGet('node/add/metatag_node');
116 $this->assertResponse(200);
118 // Check the default values are correct.
119 $this->assertFieldByName('field_metatag_field[0][basic][title]', '[node:title] | [site:name]', 'Default title token is present.');
120 $this->assertFieldByName('field_metatag_field[0][basic][description]', '[node:summary]', 'Default description token is present.');
124 'title[0][value]' => 'Node Français',
125 'body[0][value]' => 'French summary.',
127 $this->drupalPostForm(NULL, $edit, t('Save and publish'));
128 $xpath = $this->xpath("//meta[@name='description']");
129 $this->assertEqual(count($xpath), 1, 'Exactly one description meta tag found.');
130 $value = (string) $xpath[0]['content'];
131 $this->assertEqual($value, 'French summary.');
133 $this->drupalGet('node/1/translations/add/en/es');
134 $this->assertResponse(200);
135 // Check the default values are there.
136 $this->assertFieldByName('field_metatag_field[0][basic][title]', '[node:title] | [site:name]', 'Default title token is present.');
137 $this->assertFieldByName('field_metatag_field[0][basic][description]', '[node:summary]', 'Default description token is present.');
140 'title[0][value]' => 'Node Español',
141 'body[0][value]' => 'Spanish summary.',
143 $this->drupalPostForm(NULL, $edit, t('Save and keep published (this translation)'));
144 $this->drupalGet('es/node/1');
145 $this->assertResponse(200);
146 $xpath = $this->xpath("//meta[@name='description']");
147 $this->assertEqual(count($xpath), 1, 'Exactly one description meta tag found.');
148 $value = (string) $xpath[0]['content'];
149 $this->assertEqual($value, 'Spanish summary.');
150 $this->assertNotEqual($value, 'French summary.');
152 $this->drupalGet('node/1/edit');
153 $this->assertResponse(200);
154 // Check the default values are there.
155 $this->assertFieldByName('field_metatag_field[0][basic][title]', '[node:title] | [site:name]', 'Default title token is present.');
156 $this->assertFieldByName('field_metatag_field[0][basic][description]', '[node:summary]', 'Default description token is present.');
158 // Set explicit values on the description metatag instead using the
160 $this->drupalGet('node/1/edit');
161 $this->assertResponse(200);
163 'field_metatag_field[0][basic][description]' => 'Overridden French description.',
165 $this->drupalPostForm(NULL, $edit, t('Save and keep published (this translation)'));
166 $xpath = $this->xpath("//meta[@name='description']");
167 $this->assertEqual(count($xpath), 1, 'Exactly one description meta tag found.');
168 $value = (string) $xpath[0]['content'];
169 $this->assertEqual($value, 'Overridden French description.');
170 $this->assertNotEqual($value, 'Spanish summary.');
171 $this->assertNotEqual($value, 'French summary.');
173 $this->drupalGet('es/node/1/edit');
174 $this->assertResponse(200);
176 'field_metatag_field[0][basic][description]' => 'Overridden Spanish description.',
178 $this->drupalPostForm(NULL, $edit, t('Save and keep published (this translation)'));
179 $xpath = $this->xpath("//meta[@name='description']");
180 $this->assertEqual(count($xpath), 1, 'Exactly one description meta tag found.');
181 $value = (string) $xpath[0]['content'];
182 $this->assertEqual($value, 'Overridden Spanish description.');
183 $this->assertNotEqual($value, 'Spanish summary.');
184 $this->assertNotEqual($value, 'French summary.');