385e29a893ce3c9753d3699efe2e29a552a15b67
[yaffs-website] / web / modules / contrib / metatag / src / Tests / MetatagStringTest.php
1 <?php
2
3 namespace Drupal\metatag\Tests;
4
5 use Drupal\simpletest\WebTestBase;
6
7 /**
8  * Ensures that the Metatag field works correctly.
9  *
10  * @group Metatag
11  */
12 class MetatagStringTest extends WebTestBase {
13
14   /**
15    * Admin user
16    *
17    * @var \Drupal\Core\Session\AccountInterface
18    */
19   protected $adminUser;
20
21   /**
22    * Modules to enable.
23    *
24    * @var array
25    */
26   public static $modules = [
27     'token',
28     'node',
29     'field_ui',
30     'metatag',
31   ];
32
33   /**
34    * Permissions to grant admin user.
35    *
36    * @var array
37    */
38   protected $permissions = [
39     'administer node fields',
40     'administer content types',
41     'access administration pages',
42     'administer meta tags',
43     'administer nodes',
44     'bypass node access',
45     'administer meta tags',
46     'administer site configuration',
47     'access content',
48   ];
49
50   /**
51    * {@inheritdoc}
52    */
53   function setUp() {
54     parent::setUp();
55     $this->adminUser = $this->drupalCreateUser($this->permissions);
56     $this->drupalLogin($this->adminUser);
57
58     $this->drupalCreateContentType(['type' => 'page', 'display_submitted' => FALSE]);
59
60     // Add a Metatag field to the content type.
61     $this->drupalGet('admin/structure/types');
62     $this->assertResponse(200);
63     $this->drupalGet('admin/structure/types/manage/page/fields/add-field');
64     $this->assertResponse(200);
65     $edit = [
66       'label' => 'Metatag',
67       'field_name' => 'metatag_field',
68       'new_storage_type' => 'metatag',
69     ];
70     $this->drupalPostForm(NULL, $edit, t('Save and continue'));
71     $this->drupalPostForm(NULL, [], t('Save field settings'));
72     $this->container->get('entity.manager')->clearCachedFieldDefinitions();
73   }
74
75   /**
76    * Tests that a meta tag with single quote is not double escaped.
77    */
78   function testSingleQuote() {
79     $this->_testAString("bla'bleblu");
80   }
81
82   /**
83    * Tests that a meta tag with a double quote is not double escaped.
84    */
85   function testDoubleQuote() {
86     $this->_testAString('bla"bleblu');
87   }
88
89   /**
90    * Tests that a meta tag with an ampersand is not double escaped.
91    */
92   function testAmpersand() {
93     $this->_testAString("blable&blu");
94   }
95
96   /**
97    * Tests that specific strings are not double escaped.
98    */
99   function _testAString($string) {
100     $this->_testConfig($string);
101     $this->_testNode($string);
102     $this->_testEncodedField($string);
103   }
104
105   /**
106    * Tests that a specific config string is not double encoded.
107    */
108   function _testConfig($string) {
109     // The original strings.
110     $title_original = 'Title: ' . $string;
111     $desc_original = 'Description: ' . $string;
112
113     // The strings after they're encoded, but quotes will not be encoded.
114     $title_encoded = htmlentities($title_original, ENT_QUOTES);
115     $desc_encoded = htmlentities($desc_original, ENT_QUOTES);
116
117     // The strings double-encoded, to make sure the tags aren't broken.
118     $title_encodeded = htmlentities($title_encoded, ENT_QUOTES);
119     $desc_encodeded = htmlentities($desc_encoded, ENT_QUOTES);
120
121     // Update the Global defaults and test them.
122     $this->drupalGet('admin/config/search/metatag/front');
123     $this->assertResponse(200);
124     $values = [
125       'title' => $title_original,
126       'description' => $desc_original,
127     ];
128     $this->drupalPostForm(NULL, $values, 'Save');
129     $this->assertResponse(200);
130
131     $metatag_defaults = \Drupal::config('metatag.metatag_defaults.front');
132     $default_title = $metatag_defaults->get('tags')['title'];
133     $default_description = $metatag_defaults->get('tags')['description'];
134
135     // Make sure the title tag is stored correctly.
136     $this->assertEqual($title_original, $default_title, 'The title tag was stored in its original format.');
137     $this->assertNotEqual($title_encoded, $default_title, 'The title tag was not stored in an encoded format.');
138     $this->assertNotEqual($title_encodeded, $default_title, 'The title tag was not stored in a double-encoded format.');
139
140     // Make sure the description tag is stored correctly.
141     $this->assertEqual($desc_original, $default_description, 'The description tag was stored in its original format.');
142     $this->assertNotEqual($desc_encoded, $default_description, 'The description tag was not stored in an encoded format.');
143     $this->assertNotEqual($desc_encodeded, $default_description, 'The description tag was not stored in a double-encoded format.');
144
145     // Set up a node without explicit metatag description. This causes the
146     // global default to be used, which contains a token (node:summary). The
147     // token value should be correctly translated.
148
149     // Create a node.
150     $this->drupalGet('node/add/page');
151     $this->assertResponse(200);
152     $edit = [
153       'title[0][value]' => $title_original,
154       'body[0][value]' => $desc_original,
155     ];
156     $this->drupalPostForm(NULL, $edit, t('Save and publish'));
157
158     $this->config('system.site')->set('page.front', '/node/1')->save();
159
160     // Load the front page.
161     $this->drupalGet('<front>');
162     $this->assertResponse(200);
163
164     // Again, with xpath the HTML entities will be parsed automagically.
165     $xpath_title = (string) current($this->xpath("//title"));
166     $this->assertEqual($xpath_title, $title_original);
167     $this->assertNotEqual($xpath_title, $title_encoded);
168     $this->assertNotEqual($xpath_title, $title_encodeded);
169
170     // The page title should be HTML encoded; have to do this check manually
171     // because assertRaw() checks the raw HTML, not the parsed strings like
172     // xpath does.
173     $this->assertRaw('<title>' . $title_encoded . '</title>', 'Confirmed the node title tag is available in its encoded format.');
174     $this->assertNoRaw('<title>' . $title_original . '</title>', 'Confirmed the node title tag is not available in its original format.');
175     $this->assertNoRaw('<title>' . $title_encodeded . '</title>', 'Confirmed the node title tag is not double-double-encoded?');
176
177     // Again, with xpath the HTML entities will be parsed automagically.
178     $xpath = $this->xpath("//meta[@name='description']");
179     $this->assertEqual($xpath[0]['content'], $desc_original);
180     $this->assertNotEqual($xpath[0]['content'], $desc_encoded);
181     $this->assertNotEqual($xpath[0]['content'], $desc_encodeded);
182   }
183
184   /**
185    * Tests that a specific node string is not double escaped.
186    */
187   function _testNode($string) {
188     // The original strings.
189     $title_original = 'Title: ' . $string;
190     $desc_original = 'Description: ' . $string;
191
192     // The strings after they're encoded, but quotes will not be encoded.
193     $title_encoded = htmlentities($title_original, ENT_QUOTES);
194     $desc_encoded = htmlentities($desc_original, ENT_QUOTES);
195
196     // The strings double-encoded, to make sure the tags aren't broken.
197     $title_encodeded = htmlentities($title_encoded, ENT_QUOTES);
198     $desc_encodeded = htmlentities($desc_encoded, ENT_QUOTES);
199
200     // Update the Global defaults and test them.
201     $this->drupalGet('admin/config/search/metatag/global');
202     $this->assertResponse(200);
203     $values = [
204       'title' => $title_original,
205       'description' => $desc_original,
206     ];
207     $this->drupalPostForm(NULL, $values, t('Save'));
208     $this->assertResponse(200);
209
210     // Set up a node without explicit metatag description. This causes the
211     // global default to be used, which contains a token (node:summary). The
212     // token value should be correctly translated.
213
214     // Create a node.
215     $this->drupalGet('node/add/page');
216     $this->assertResponse(200);
217     $edit = [
218       'title[0][value]' => $title_original,
219       'body[0][value]' => $desc_original,
220     ];
221     $this->drupalPostForm(NULL, $edit, t('Save and publish'));
222     $this->assertResponse(200);
223
224     // Load the node page.
225     $this->drupalGet('node/1');
226     $this->assertResponse(200);
227
228     // Again, with xpath the HTML entities will be parsed automagically.
229     $xpath_title = (string) current($this->xpath("//title"));
230     $this->assertEqual($xpath_title, $title_original);
231     $this->assertNotEqual($xpath_title, $title_encoded);
232     $this->assertNotEqual($xpath_title, $title_encodeded);
233
234     // The page title should be HTML encoded; have to do this check manually
235     // because assertRaw() checks the raw HTML, not the parsed strings like
236     // xpath does.
237     $this->assertRaw('<title>' . $title_encoded . '</title>', 'Confirmed the node title tag is encoded.');
238     // Again, with xpath the HTML entities will be parsed automagically.
239     $xpath = $this->xpath("//meta[@name='description']");
240     $value = (string) $xpath[0]['content'];
241     $this->assertEqual($value, $desc_original);
242     $this->assertNotEqual($value, $desc_encoded);
243     $this->assertNotEqual($value, $desc_encodeded);
244
245     // Normal meta tags should be encoded properly.
246     $this->assertRaw('"' . $desc_encoded . '"', 'Confirmed the node "description" meta tag string was encoded properly.');
247     // Normal meta tags with HTML entities should be displayed in their original
248     // format.
249     $this->assertNoRaw('"' . $desc_original . '"', 'Confirmed the node "description" meta tag string does not show in its original form.');
250     // Normal meta tags should not be double-encoded.
251     $this->assertNoRaw('"' . $desc_encodeded . '"', 'Confirmed the node "description" meta tag string was not double-encoded.');
252   }
253
254   /**
255    * Tests that fields with encoded HTML entities will not be double-encoded.
256    */
257   function _testEncodedField($string) {
258     // The original strings.
259     $title_original = 'Title: ' . $string;
260     $desc_original = 'Description: ' . $string;
261
262     // The strings after they're encoded, but quotes will not be encoded.
263     $desc_encoded = htmlentities($desc_original, ENT_QUOTES);
264
265     // The strings double-encoded, to make sure the tags aren't broken.
266     $desc_encodeded = htmlentities($desc_encoded, ENT_QUOTES);
267
268     // Update the Global defaults and test them.
269     $this->drupalGet('admin/config/search/metatag/global');
270     $this->assertResponse(200);
271     $values = [
272       'title' => $title_original,
273       'description' => $desc_original,
274     ];
275     $this->drupalPostForm(NULL, $values, t('Save'));
276     $this->assertResponse(200);
277
278     // Set up a node without explicit metatag description. This causes the
279     // global default to be used, which contains a token (node:summary). The
280     // token value should be correctly translated.
281
282     // Create a node.
283     $this->drupalGet('node/add/page');
284     $this->assertResponse(200);
285     $edit = [
286       'title[0][value]' => $title_original,
287       'body[0][value]' => $desc_original,
288     ];
289     $this->drupalPostForm(NULL, $edit, t('Save and publish'));
290     $this->assertResponse(200);
291
292     // Load the node page.
293     $this->drupalGet('node/1');
294     $this->assertResponse(200);
295
296     // With xpath the HTML entities will be parsed automagically.
297     $xpath = $this->xpath("//meta[@name='description']");
298     $value = (string) $xpath[0]['content'];
299     $this->assertEqual($value, $desc_original);
300     $this->assertNotEqual($value, $desc_encoded);
301     $this->assertNotEqual($value, $desc_encodeded);
302
303     // Normal meta tags should be encoded properly.
304     $this->assertRaw('"' . $desc_encoded . '"', 'Confirmed the node "description" meta tag string was encoded properly.');
305
306     // Normal meta tags with HTML entities should be displayed in their original
307     // format.
308     $this->assertNoRaw('"' . $desc_original . '"', 'Confirmed the node "description" meta tag string does not show in its original form.');
309
310     // Normal meta tags should not be double-encoded.
311     $this->assertNoRaw('"' . $desc_encodeded . '"', 'Confirmed the node "description" meta tag string was not double-encoded.');
312   }
313
314 }
315