Version 1
[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->drupalGet("admin/structure/types/manage/page/fields/add-field");
63     $this->assertResponse(200);
64     $edit = [
65       'label' => 'Metatag',
66       'field_name' => 'metatag_field',
67       'new_storage_type' => 'metatag',
68     ];
69     $this->drupalPostForm(NULL, $edit, t('Save and continue'));
70     $this->drupalPostForm(NULL, [], t('Save field settings'));
71     $this->container->get('entity.manager')->clearCachedFieldDefinitions();
72   }
73
74   /**
75    * Tests that a meta tag with single quote is not double escaped.
76    */
77   function testSingleQuote() {
78     $this->_testAString("bla'bleblu");
79   }
80
81   /**
82    * Tests that a meta tag with a double quote is not double escaped.
83    */
84   function testDoubleQuote() {
85     $this->_testAString('bla"bleblu');
86   }
87
88   /**
89    * Tests that a meta tag with an ampersand is not double escaped.
90    */
91   function testAmpersand() {
92     $this->_testAString("blable&blu");
93   }
94
95   /**
96    * Tests that specific strings are not double escaped.
97    */
98   function _testAString($string) {
99     $this->_testConfig($string);
100     $this->_testNode($string);
101     $this->_testEncodedField($string);
102   }
103
104   /**
105    * Tests that a specific config string is not double encoded.
106    */
107   function _testConfig($string) {
108     // The original strings.
109     $title_original = 'Title: ' . $string;
110     $desc_original = 'Description: ' . $string;
111
112     // The strings after they're encoded, but quotes will not be encoded.
113     $title_encoded = htmlentities($title_original, ENT_QUOTES);
114     $desc_encoded = htmlentities($desc_original, ENT_QUOTES);
115
116     // The strings double-encoded, to make sure the tags aren't broken.
117     $title_encodeded = htmlentities($title_encoded, ENT_QUOTES);
118     $desc_encodeded = htmlentities($desc_encoded, ENT_QUOTES);
119
120     // Update the Global defaults and test them.
121     $values = [
122       'title' => $title_original,
123       'description' => $desc_original,
124     ];
125     $this->drupalPostForm('admin/config/search/metatag/front', $values, 'Save');
126     $this->assertResponse(200);
127
128     $metatag_defaults = \Drupal::config('metatag.metatag_defaults.front');
129     $default_title = $metatag_defaults->get('tags')['title'];
130     $default_description = $metatag_defaults->get('tags')['description'];
131
132     // Make sure the title tag is stored correctly.
133     $this->assertEqual($title_original, $default_title, 'The title tag was stored in its original format.');
134     $this->assertNotEqual($title_encoded, $default_title, 'The title tag was not stored in an encoded format.');
135     $this->assertNotEqual($title_encodeded, $default_title, 'The title tag was not stored in a double-encoded format.');
136
137     // Make sure the description tag is stored correctly.
138     $this->assertEqual($desc_original, $default_description, 'The description tag was stored in its original format.');
139     $this->assertNotEqual($desc_encoded, $default_description, 'The description tag was not stored in an encoded format.');
140     $this->assertNotEqual($desc_encodeded, $default_description, 'The description tag was not stored in a double-encoded format.');
141
142     // Set up a node without explicit metatag description. This causes the
143     // global default to be used, which contains a token (node:summary). The
144     // token value should be correctly translated.
145
146     // Create a node.
147     $this->drupalGet("node/add/page");
148     $edit = [
149       'title[0][value]' => $title_original,
150       'body[0][value]' => $desc_original,
151     ];
152     $this->drupalPostForm("node/add/page", $edit, t('Save and publish'));
153
154     $this->config('system.site')->set('page.front', '/node/1')->save();
155
156     // Load the front page.
157     $this->drupalGet('<front>');
158     $this->assertResponse(200);
159
160     // Again, with xpath the HTML entities will be parsed automagically.
161     $xpath_title = (string) current($this->xpath("//title"));
162     $this->assertEqual($xpath_title, $title_original);
163     $this->assertNotEqual($xpath_title, $title_encoded);
164     $this->assertNotEqual($xpath_title, $title_encodeded);
165
166     // The page title should be HTML encoded; have to do this check manually
167     // because assertRaw() checks the raw HTML, not the parsed strings like
168     // xpath does.
169     $this->assertRaw('<title>' . $title_encoded . '</title>', 'Confirmed the node title tag is available in its encoded format.');
170     $this->assertNoRaw('<title>' . $title_original . '</title>', 'Confirmed the node title tag is not available in its original format.');
171     $this->assertNoRaw('<title>' . $title_encodeded . '</title>', 'Confirmed the node title tag is not double-double-encoded?');
172
173     // Again, with xpath the HTML entities will be parsed automagically.
174     $xpath = $this->xpath("//meta[@name='description']");
175     $this->assertEqual($xpath[0]['content'], $desc_original);
176     $this->assertNotEqual($xpath[0]['content'], $desc_encoded);
177     $this->assertNotEqual($xpath[0]['content'], $desc_encodeded);
178   }
179
180   /**
181    * Tests that a specific node string is not double escaped.
182    */
183   function _testNode($string) {
184     // The original strings.
185     $title_original = 'Title: ' . $string;
186     $desc_original = 'Description: ' . $string;
187
188     // The strings after they're encoded, but quotes will not be encoded.
189     $title_encoded = htmlentities($title_original, ENT_QUOTES);
190     $desc_encoded = htmlentities($desc_original, ENT_QUOTES);
191
192     // The strings double-encoded, to make sure the tags aren't broken.
193     $title_encodeded = htmlentities($title_encoded, ENT_QUOTES);
194     $desc_encodeded = htmlentities($desc_encoded, ENT_QUOTES);
195
196     // Update the Global defaults and test them.
197     $this->drupalGet('admin/config/search/metatag/global');
198     $this->assertResponse(200);
199     $values = [
200       'title' => $title_original,
201       'description' => $desc_original,
202     ];
203     $this->drupalPostForm(NULL, $values, t('Save'));
204     $this->assertResponse(200);
205
206     // Set up a node without explicit metatag description. This causes the
207     // global default to be used, which contains a token (node:summary). The
208     // token value should be correctly translated.
209
210     // Create a node.
211     $this->drupalGet('node/add/page');
212     $this->assertResponse(200);
213     $edit = [
214       'title[0][value]' => $title_original,
215       'body[0][value]' => $desc_original,
216     ];
217     $this->drupalPostForm(NULL, $edit, t('Save and publish'));
218     $this->assertResponse(200);
219
220     // Load the node page.
221     $this->drupalGet('node/1');
222     $this->assertResponse(200);
223
224     // Again, with xpath the HTML entities will be parsed automagically.
225     $xpath_title = (string) current($this->xpath("//title"));
226     $this->assertEqual($xpath_title, $title_original);
227     $this->assertNotEqual($xpath_title, $title_encoded);
228     $this->assertNotEqual($xpath_title, $title_encodeded);
229
230     // The page title should be HTML encoded; have to do this check manually
231     // because assertRaw() checks the raw HTML, not the parsed strings like
232     // xpath does.
233     $this->assertRaw('<title>' . $title_encoded . '</title>', 'Confirmed the node title tag is encoded.');
234     // Again, with xpath the HTML entities will be parsed automagically.
235     $xpath = $this->xpath("//meta[@name='description']");
236     $value = (string) $xpath[0]['content'];
237     $this->assertEqual($value, $desc_original);
238     $this->assertNotEqual($value, $desc_encoded);
239     $this->assertNotEqual($value, $desc_encodeded);
240
241     // Normal meta tags should be encoded properly.
242     $this->assertRaw('"' . $desc_encoded . '"', 'Confirmed the node "description" meta tag string was encoded properly.');
243     // Normal meta tags with HTML entities should be displayed in their original
244     // format.
245     $this->assertNoRaw('"' . $desc_original . '"', 'Confirmed the node "description" meta tag string does not show in its original form.');
246     // Normal meta tags should not be double-encoded.
247     $this->assertNoRaw('"' . $desc_encodeded . '"', 'Confirmed the node "description" meta tag string was not double-encoded.');
248   }
249
250   /**
251    * Tests that fields with encoded HTML entities will not be double-encoded.
252    */
253   function _testEncodedField($string) {
254     // The original strings.
255     $title_original = 'Title: ' . $string;
256     $desc_original = 'Description: ' . $string;
257
258     // The strings after they're encoded, but quotes will not be encoded.
259     $desc_encoded = htmlentities($desc_original, ENT_QUOTES);
260
261     // The strings double-encoded, to make sure the tags aren't broken.
262     $desc_encodeded = htmlentities($desc_encoded, ENT_QUOTES);
263
264     // Update the Global defaults and test them.
265     $this->drupalGet('admin/config/search/metatag/global');
266     $this->assertResponse(200);
267     $values = [
268       'title' => $title_original,
269       'description' => $desc_original,
270     ];
271     $this->drupalPostForm(NULL, $values, t('Save'));
272     $this->assertResponse(200);
273
274     // Set up a node without explicit metatag description. This causes the
275     // global default to be used, which contains a token (node:summary). The
276     // token value should be correctly translated.
277
278     // Create a node.
279     $this->drupalGet('node/add/page');
280     $this->assertResponse(200);
281     $edit = [
282       'title[0][value]' => $title_original,
283       'body[0][value]' => $desc_original,
284     ];
285     $this->drupalPostForm(NULL, $edit, t('Save and publish'));
286     $this->assertResponse(200);
287
288     // Load the node page.
289     $this->drupalGet('node/1');
290     $this->assertResponse(200);
291
292     // With xpath the HTML entities will be parsed automagically.
293     $xpath = $this->xpath("//meta[@name='description']");
294     $value = (string) $xpath[0]['content'];
295     $this->assertEqual($value, $desc_original);
296     $this->assertNotEqual($value, $desc_encoded);
297     $this->assertNotEqual($value, $desc_encodeded);
298
299     // Normal meta tags should be encoded properly.
300     $this->assertRaw('"' . $desc_encoded . '"', 'Confirmed the node "description" meta tag string was encoded properly.');
301
302     // Normal meta tags with HTML entities should be displayed in their original
303     // format.
304     $this->assertNoRaw('"' . $desc_original . '"', 'Confirmed the node "description" meta tag string does not show in its original form.');
305
306     // Normal meta tags should not be double-encoded.
307     $this->assertNoRaw('"' . $desc_encodeded . '"', 'Confirmed the node "description" meta tag string was not double-encoded.');
308   }
309
310 }
311