Updated all the contrib modules to their latest versions.
[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   /**
49    * Setup basic environment.
50    */
51   protected function setUp() {
52     parent::setUp();
53
54     $admin_permissions = [
55       'administer content types',
56       'administer content translation',
57       'administer languages',
58       'administer nodes',
59       'administer node fields',
60       'bypass node access',
61       'create content translations',
62       'delete content translations',
63       'translate any entity',
64       'update content translations',
65     ];
66
67     // Create and login user.
68     $this->adminUser = $this->drupalCreateUser($admin_permissions);
69
70     // Add languages.
71     foreach ($this->additionalLangcodes as $langcode) {
72       ConfigurableLanguage::createFromLangcode($langcode)->save();
73     }
74   }
75
76   /**
77    * Tests the metatag value translations.
78    */
79   public function testMetatagValueTranslation() {
80     if (floatval(\Drupal::VERSION) <= 8.3) {
81       $save_label = t('Save and publish');
82       $save_label_i18n = t('Save and keep published (this translation)');
83     }
84     else {
85       $save_label = t('Save');
86       $save_label_i18n = t('Save (this translation)');
87     }
88
89     // Set up a content type.
90     $name = $this->randomMachineName() . ' ' . $this->randomMachineName();
91     $this->drupalLogin($this->adminUser);
92     $this->drupalCreateContentType(['type' => 'metatag_node', 'name' => $name]);
93
94     // Add a metatag field to the content type.
95     $this->drupalGet('admin/structure/types');
96     $this->assertResponse(200);
97     $this->drupalGet('admin/structure/types/manage/metatag_node');
98     $this->assertResponse(200);
99     $edit = [
100       'language_configuration[language_alterable]' => TRUE,
101       'language_configuration[content_translation]' => TRUE,
102     ];
103     $this->drupalPostForm(NULL, $edit, t('Save content type'));
104     $this->assertResponse(200);
105
106     $this->drupalGet('admin/structure/types/manage/metatag_node/fields/add-field');
107     $this->assertResponse(200);
108     $edit = [
109       'label' => 'Meta tags',
110       'field_name' => 'meta_tags',
111       'new_storage_type' => 'metatag',
112     ];
113     $this->drupalPostForm(NULL, $edit, t('Save and continue'));
114     $this->assertResponse(200);
115     $this->drupalPostForm(NULL, [], t('Save field settings'));
116     $this->assertResponse(200);
117     $edit = [
118       'translatable' => TRUE,
119     ];
120     $this->drupalPostForm(NULL, $edit, t('Save settings'));
121     $this->assertResponse(200);
122     $this->drupalGet('admin/structure/types/manage/metatag_node/fields/node.metatag_node.field_meta_tags');
123     $this->assertResponse(200);
124
125     // Set up a node without explicit metatag description. This causes the
126     // global default to be used, which contains a token (node:summary). The
127     // token value should be correctly translated.
128
129     // Load the node form.
130     $this->drupalGet('node/add/metatag_node');
131     $this->assertResponse(200);
132
133     // Check the default values are correct.
134     $this->assertFieldByName('field_meta_tags[0][basic][title]', '[node:title] | [site:name]', 'Default title token is present.');
135     $this->assertFieldByName('field_meta_tags[0][basic][description]', '[node:summary]', 'Default description token is present.');
136
137     // Create a node.
138     $edit = [
139       'title[0][value]' => 'Node Français',
140       'body[0][value]' => 'French summary.',
141     ];
142     $this->drupalPostForm(NULL, $edit, $save_label);
143     $this->assertResponse(200);
144
145     $xpath = $this->xpath("//meta[@name='description']");
146     $this->assertEqual(count($xpath), 1, 'Exactly one description meta tag found.');
147     $value = (string) $xpath[0]['content'];
148     $this->assertEqual($value, 'French summary.');
149
150     $this->drupalGet('node/1/translations/add/en/es');
151     $this->assertResponse(200);
152     // Check the default values are there.
153     $this->assertFieldByName('field_meta_tags[0][basic][title]', '[node:title] | [site:name]', 'Default title token is present.');
154     $this->assertFieldByName('field_meta_tags[0][basic][description]', '[node:summary]', 'Default description token is present.');
155
156     $edit = [
157       'title[0][value]' => 'Node Español',
158       'body[0][value]' => 'Spanish summary.',
159     ];
160     $this->drupalPostForm(NULL, $edit, $save_label_i18n);
161     $this->assertResponse(200);
162
163     $this->drupalGet('es/node/1');
164     $this->assertResponse(200);
165     $xpath = $this->xpath("//meta[@name='description']");
166     $this->assertEqual(count($xpath), 1, 'Exactly one description meta tag found.');
167     $value = (string) $xpath[0]['content'];
168     $this->assertEqual($value, 'Spanish summary.');
169     $this->assertNotEqual($value, 'French summary.');
170
171     $this->drupalGet('node/1/edit');
172     $this->assertResponse(200);
173     // Check the default values are there.
174     $this->assertFieldByName('field_meta_tags[0][basic][title]', '[node:title] | [site:name]', 'Default title token is present.');
175     $this->assertFieldByName('field_meta_tags[0][basic][description]', '[node:summary]', 'Default description token is present.');
176
177     // Set explicit values on the description metatag instead using the
178     // defaults.
179     $this->drupalGet('node/1/edit');
180     $this->assertResponse(200);
181     $edit = [
182       'field_meta_tags[0][basic][description]' => 'Overridden French description.',
183     ];
184     $this->drupalPostForm(NULL, $edit, $save_label_i18n);
185     $this->assertResponse(200);
186
187     $xpath = $this->xpath("//meta[@name='description']");
188     $this->assertEqual(count($xpath), 1, 'Exactly one description meta tag found.');
189     $value = (string) $xpath[0]['content'];
190     $this->assertEqual($value, 'Overridden French description.');
191     $this->assertNotEqual($value, 'Spanish summary.');
192     $this->assertNotEqual($value, 'French summary.');
193
194     $this->drupalGet('es/node/1/edit');
195     $this->assertResponse(200);
196     $edit = [
197       'field_meta_tags[0][basic][description]' => 'Overridden Spanish description.',
198     ];
199     $this->drupalPostForm(NULL, $edit, $save_label_i18n);
200     $this->assertResponse(200);
201
202     $xpath = $this->xpath("//meta[@name='description']");
203     $this->assertEqual(count($xpath), 1, 'Exactly one description meta tag found.');
204     $value = (string) $xpath[0]['content'];
205     $this->assertEqual($value, 'Overridden Spanish description.');
206     $this->assertNotEqual($value, 'Spanish summary.');
207     $this->assertNotEqual($value, 'French summary.');
208   }
209
210 }