3 namespace Drupal\metatag\Tests;
5 use Drupal\simpletest\WebTestBase;
8 * Ensures that the Metatag field works correctly.
12 class MetatagStringTest extends WebTestBase {
17 * @var \Drupal\Core\Session\AccountInterface
26 public static $modules = [
34 * Permissions to grant admin user.
38 protected $permissions = [
39 'administer node fields',
40 'administer content types',
41 'access administration pages',
42 'administer meta tags',
45 'administer meta tags',
46 'administer site configuration',
55 $this->adminUser = $this->drupalCreateUser($this->permissions);
56 $this->drupalLogin($this->adminUser);
58 $this->drupalCreateContentType(['type' => 'page', 'display_submitted' => FALSE]);
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);
67 'field_name' => 'metatag_field',
68 'new_storage_type' => 'metatag',
70 $this->drupalPostForm(NULL, $edit, t('Save and continue'));
71 $this->drupalPostForm(NULL, [], t('Save field settings'));
72 $this->container->get('entity.manager')->clearCachedFieldDefinitions();
76 * Tests that a meta tag with single quote is not double escaped.
78 function testSingleQuote() {
79 $this->_testAString("bla'bleblu");
83 * Tests that a meta tag with a double quote is not double escaped.
85 function testDoubleQuote() {
86 $this->_testAString('bla"bleblu');
90 * Tests that a meta tag with an ampersand is not double escaped.
92 function testAmpersand() {
93 $this->_testAString("blable&blu");
97 * Tests that specific strings are not double escaped.
99 function _testAString($string) {
100 $this->_testConfig($string);
101 $this->_testNode($string);
102 $this->_testEncodedField($string);
106 * Tests that a specific config string is not double encoded.
108 function _testConfig($string) {
109 // The original strings.
110 $title_original = 'Title: ' . $string;
111 $desc_original = 'Description: ' . $string;
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);
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);
121 // Update the Global defaults and test them.
122 $this->drupalGet('admin/config/search/metatag/front');
123 $this->assertResponse(200);
125 'title' => $title_original,
126 'description' => $desc_original,
128 $this->drupalPostForm(NULL, $values, 'Save');
129 $this->assertResponse(200);
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'];
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.');
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.');
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.
150 $this->drupalGet('node/add/page');
151 $this->assertResponse(200);
153 'title[0][value]' => $title_original,
154 'body[0][value]' => $desc_original,
156 $this->drupalPostForm(NULL, $edit, t('Save and publish'));
158 $this->config('system.site')->set('page.front', '/node/1')->save();
160 // Load the front page.
161 $this->drupalGet('<front>');
162 $this->assertResponse(200);
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);
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
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?');
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);
185 * Tests that a specific node string is not double escaped.
187 function _testNode($string) {
188 // The original strings.
189 $title_original = 'Title: ' . $string;
190 $desc_original = 'Description: ' . $string;
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);
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);
200 // Update the Global defaults and test them.
201 $this->drupalGet('admin/config/search/metatag/global');
202 $this->assertResponse(200);
204 'title' => $title_original,
205 'description' => $desc_original,
207 $this->drupalPostForm(NULL, $values, t('Save'));
208 $this->assertResponse(200);
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.
215 $this->drupalGet('node/add/page');
216 $this->assertResponse(200);
218 'title[0][value]' => $title_original,
219 'body[0][value]' => $desc_original,
221 $this->drupalPostForm(NULL, $edit, t('Save and publish'));
222 $this->assertResponse(200);
224 // Load the node page.
225 $this->drupalGet('node/1');
226 $this->assertResponse(200);
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);
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
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);
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
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.');
255 * Tests that fields with encoded HTML entities will not be double-encoded.
257 function _testEncodedField($string) {
258 // The original strings.
259 $title_original = 'Title: ' . $string;
260 $desc_original = 'Description: ' . $string;
262 // The strings after they're encoded, but quotes will not be encoded.
263 $desc_encoded = htmlentities($desc_original, ENT_QUOTES);
265 // The strings double-encoded, to make sure the tags aren't broken.
266 $desc_encodeded = htmlentities($desc_encoded, ENT_QUOTES);
268 // Update the Global defaults and test them.
269 $this->drupalGet('admin/config/search/metatag/global');
270 $this->assertResponse(200);
272 'title' => $title_original,
273 'description' => $desc_original,
275 $this->drupalPostForm(NULL, $values, t('Save'));
276 $this->assertResponse(200);
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.
283 $this->drupalGet('node/add/page');
284 $this->assertResponse(200);
286 'title[0][value]' => $title_original,
287 'body[0][value]' => $desc_original,
289 $this->drupalPostForm(NULL, $edit, t('Save and publish'));
290 $this->assertResponse(200);
292 // Load the node page.
293 $this->drupalGet('node/1');
294 $this->assertResponse(200);
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);
303 // Normal meta tags should be encoded properly.
304 $this->assertRaw('"' . $desc_encoded . '"', 'Confirmed the node "description" meta tag string was encoded properly.');
306 // Normal meta tags with HTML entities should be displayed in their original
308 $this->assertNoRaw('"' . $desc_original . '"', 'Confirmed the node "description" meta tag string does not show in its original form.');
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.');