0e9ff0d707ecc8e7f5a0ac1fbfbb5a5159e4449e
[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->drupalGet("admin/structure/types/manage/metatag_node/fields/add-field");
85     $this->assertResponse(200);
86     $edit = [
87       'label' => 'Metatag',
88       'field_name' => 'metatag_field',
89       'new_storage_type' => 'metatag',
90     ];
91     $this->drupalPostForm(NULL, $edit, t('Save and continue'));
92     $this->drupalPostForm(NULL, [], t('Save field settings'));
93     $this->container->get('entity.manager')->clearCachedFieldDefinitions();
94
95     // Enable translation for our test content type.
96     $this->drupalGet('admin/config/regional/content-language');
97     $this->assertResponse(200);
98     $edit = [
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,
103     ];
104     $this->drupalPostForm(NULL, $edit, t('Save configuration'));
105
106     $this->drupalGet('admin/structure/types/manage/metatag_node');
107     $this->assertResponse(200);
108
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.
112
113     // Create a node.
114     $this->drupalGet("node/add/metatag_node");
115     $edit = [
116       'title[0][value]' => 'Node Français',
117       'body[0][value]' => 'French summary.',
118     ];
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.');
124
125     $this->drupalGet('node/1/translations/add/en/es');
126     $this->assertResponse(200);
127     $edit = [
128       'title[0][value]' => 'Node Español',
129       'body[0][value]' => 'Spanish summary.',
130     ];
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.');
139
140     // Set explicit values on the description metatag instead using the
141     // defaults.
142     $this->drupalGet('node/1/edit');
143     $this->assertResponse(200);
144     $edit = [
145       'field_metatag_field[0][basic][description]' => 'Overridden French description.',
146     ];
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.');
154
155     $this->drupalGet('es/node/1/edit');
156     $this->assertResponse(200);
157     $edit = [
158       'field_metatag_field[0][basic][description]' => 'Overridden Spanish description.',
159     ];
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.');
167   }
168 }