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->drupalGet("admin/structure/types/manage/metatag_node/fields/add-field");
85 $this->assertResponse(200);
88 'field_name' => 'metatag_field',
89 'new_storage_type' => 'metatag',
91 $this->drupalPostForm(NULL, $edit, t('Save and continue'));
92 $this->drupalPostForm(NULL, [], t('Save field settings'));
93 $this->container->get('entity.manager')->clearCachedFieldDefinitions();
95 // Enable translation for our test content type.
96 $this->drupalGet('admin/config/regional/content-language');
97 $this->assertResponse(200);
99 'entity_types[node]' => 1,
100 'settings[node][metatag_node][translatable]' => 1,
101 'settings[node][metatag_node][translatable]' => 1,
102 'settings[node][metatag_node][fields][field_metatag_field]' => 1,
104 $this->drupalPostForm(NULL, $edit, t('Save configuration'));
106 $this->drupalGet('admin/structure/types/manage/metatag_node');
107 $this->assertResponse(200);
109 // Set up a node without explicit metatag description. This causes the
110 // global default to be used, which contains a token (node:summary). The
111 // token value should be correctly translated.
114 $this->drupalGet("node/add/metatag_node");
116 'title[0][value]' => 'Node Français',
117 'body[0][value]' => 'French summary.',
119 $this->drupalPostForm(NULL, $edit, t('Save and publish'));
120 $xpath = $this->xpath("//meta[@name='description']");
121 $this->assertEqual(count($xpath), 1, 'Exactly one description meta tag found.');
122 $value = (string) $xpath[0]['content'];
123 $this->assertEqual($value, 'French summary.');
125 $this->drupalGet('node/1/translations/add/en/es');
126 $this->assertResponse(200);
128 'title[0][value]' => 'Node Español',
129 'body[0][value]' => 'Spanish summary.',
131 $this->drupalPostForm(NULL, $edit, t('Save and keep published (this translation)'));
132 $this->drupalGet('es/node/1');
133 $this->assertResponse(200);
134 $xpath = $this->xpath("//meta[@name='description']");
135 $this->assertEqual(count($xpath), 1, 'Exactly one description meta tag found.');
136 $value = (string) $xpath[0]['content'];
137 $this->assertEqual($value, 'Spanish summary.');
138 $this->assertNotEqual($value, 'French summary.');
140 // Set explicit values on the description metatag instead using the
142 $this->drupalGet('node/1/edit');
143 $this->assertResponse(200);
145 'field_metatag_field[0][basic][description]' => 'Overridden French description.',
147 $this->drupalPostForm(NULL, $edit, t('Save and keep published (this translation)'));
148 $xpath = $this->xpath("//meta[@name='description']");
149 $this->assertEqual(count($xpath), 1, 'Exactly one description meta tag found.');
150 $value = (string) $xpath[0]['content'];
151 $this->assertEqual($value, 'Overridden French description.');
152 $this->assertNotEqual($value, 'Spanish summary.');
153 $this->assertNotEqual($value, 'French summary.');
155 $this->drupalGet('es/node/1/edit');
156 $this->assertResponse(200);
158 'field_metatag_field[0][basic][description]' => 'Overridden Spanish description.',
160 $this->drupalPostForm(NULL, $edit, t('Save and keep published (this translation)'));
161 $xpath = $this->xpath("//meta[@name='description']");
162 $this->assertEqual(count($xpath), 1, 'Exactly one description meta tag found.');
163 $value = (string) $xpath[0]['content'];
164 $this->assertEqual($value, 'Overridden Spanish description.');
165 $this->assertNotEqual($value, 'Spanish summary.');
166 $this->assertNotEqual($value, 'French summary.');