8681258d3fb6d352734ad526efb64c9526147e43
[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     // Set up a content type.
78     $name = $this->randomString();
79     $this->drupalLogin($this->adminUser);
80     $this->drupalCreateContentType(['type' => 'metatag_node', 'name' => $name]);
81
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);
87     $edit = [
88       'label' => 'Metatag',
89       'field_name' => 'metatag_field',
90       'new_storage_type' => 'metatag',
91     ];
92     $this->drupalPostForm(NULL, $edit, t('Save and continue'));
93     $this->drupalPostForm(NULL, [], t('Save field settings'));
94     $this->container->get('entity.manager')->clearCachedFieldDefinitions();
95
96     // Enable translation for our test content type.
97     $this->drupalGet('admin/config/regional/content-language');
98     $this->assertResponse(200);
99     $edit = [
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,
104     ];
105     $this->drupalPostForm(NULL, $edit, t('Save configuration'));
106
107     $this->drupalGet('admin/structure/types/manage/metatag_node');
108     $this->assertResponse(200);
109
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.
113
114     // Load the node form.
115     $this->drupalGet('node/add/metatag_node');
116     $this->assertResponse(200);
117
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.');
121
122     // Create a node.
123     $edit = [
124       'title[0][value]' => 'Node Français',
125       'body[0][value]' => 'French summary.',
126     ];
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.');
132
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.');
138
139     $edit = [
140       'title[0][value]' => 'Node Español',
141       'body[0][value]' => 'Spanish summary.',
142     ];
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.');
151
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.');
157
158     // Set explicit values on the description metatag instead using the
159     // defaults.
160     $this->drupalGet('node/1/edit');
161     $this->assertResponse(200);
162     $edit = [
163       'field_metatag_field[0][basic][description]' => 'Overridden French description.',
164     ];
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.');
172
173     $this->drupalGet('es/node/1/edit');
174     $this->assertResponse(200);
175     $edit = [
176       'field_metatag_field[0][basic][description]' => 'Overridden Spanish description.',
177     ];
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.');
185   }
186 }