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',
53 protected function setUp() {
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 public function testSingleQuote() {
79 $this->checkString("bla'bleblu");
83 * Tests that a meta tag with a double quote is not double escaped.
85 public function testDoubleQuote() {
86 $this->checkString('bla"bleblu');
90 * Tests that a meta tag with an ampersand is not double escaped.
92 public function testAmpersand() {
93 $this->checkString("blable&blu");
97 * Tests that specific strings are not double escaped.
99 public function checkString($string) {
100 $this->checkConfig($string);
101 $this->checkNode($string);
102 $this->checkEncodedField($string);
106 * Tests that a specific config string is not double encoded.
108 public function checkConfig($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, $edit, '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 $save_label = (floatval(\Drupal::VERSION) <= 8.3) ? t('Save and publish') : t('Save');
157 $this->drupalPostForm(NULL, $edit, $save_label);
159 $this->config('system.site')->set('page.front', '/node/1')->save();
161 // Load the front page.
162 $this->drupalGet('<front>');
163 $this->assertResponse(200);
165 // Again, with xpath the HTML entities will be parsed automagically.
166 $xpath_title = (string) current($this->xpath("//title"));
167 $this->assertEqual($xpath_title, $title_original);
168 $this->assertNotEqual($xpath_title, $title_encoded);
169 $this->assertNotEqual($xpath_title, $title_encodeded);
171 // The page title should be HTML encoded; have to do this check manually
172 // because assertRaw() checks the raw HTML, not the parsed strings like
174 $this->assertRaw('<title>' . $title_encoded . '</title>', 'Confirmed the node title tag is available in its encoded format.');
175 $this->assertNoRaw('<title>' . $title_original . '</title>', 'Confirmed the node title tag is not available in its original format.');
176 $this->assertNoRaw('<title>' . $title_encodeded . '</title>', 'Confirmed the node title tag is not double-double-encoded?');
178 // Again, with xpath the HTML entities will be parsed automagically.
179 $xpath = $this->xpath("//meta[@name='description']");
180 $this->assertEqual($xpath[0]['content'], $desc_original);
181 $this->assertNotEqual($xpath[0]['content'], $desc_encoded);
182 $this->assertNotEqual($xpath[0]['content'], $desc_encodeded);
186 * Tests that a specific node string is not double escaped.
188 public function checkNode($string) {
189 $save_label = (floatval(\Drupal::VERSION) <= 8.3) ? t('Save and publish') : t('Save');
191 // The original strings.
192 $title_original = 'Title: ' . $string;
193 $desc_original = 'Description: ' . $string;
195 // The strings after they're encoded, but quotes will not be encoded.
196 $title_encoded = htmlentities($title_original, ENT_QUOTES);
197 $desc_encoded = htmlentities($desc_original, ENT_QUOTES);
199 // The strings double-encoded, to make sure the tags aren't broken.
200 $title_encodeded = htmlentities($title_encoded, ENT_QUOTES);
201 $desc_encodeded = htmlentities($desc_encoded, ENT_QUOTES);
203 // Update the Global defaults and test them.
204 $this->drupalGet('admin/config/search/metatag/global');
205 $this->assertResponse(200);
207 'title' => $title_original,
208 'description' => $desc_original,
210 $this->drupalPostForm(NULL, $edit, t('Save'));
211 $this->assertResponse(200);
213 // Set up a node without explicit metatag description. This causes the
214 // global default to be used, which contains a token (node:summary). The
215 // token value should be correctly translated.
218 $this->drupalGet('node/add/page');
219 $this->assertResponse(200);
221 'title[0][value]' => $title_original,
222 'body[0][value]' => $desc_original,
224 $this->drupalPostForm(NULL, $edit, $save_label);
225 $this->assertResponse(200);
227 // Load the node page.
228 $this->drupalGet('node/1');
229 $this->assertResponse(200);
231 // Again, with xpath the HTML entities will be parsed automagically.
232 $xpath_title = (string) current($this->xpath("//title"));
233 $this->assertEqual($xpath_title, $title_original);
234 $this->assertNotEqual($xpath_title, $title_encoded);
235 $this->assertNotEqual($xpath_title, $title_encodeded);
237 // The page title should be HTML encoded; have to do this check manually
238 // because assertRaw() checks the raw HTML, not the parsed strings like
240 $this->assertRaw('<title>' . $title_encoded . '</title>', 'Confirmed the node title tag is encoded.');
241 // Again, with xpath the HTML entities will be parsed automagically.
242 $xpath = $this->xpath("//meta[@name='description']");
243 $value = (string) $xpath[0]['content'];
244 $this->assertEqual($value, $desc_original);
245 $this->assertNotEqual($value, $desc_encoded);
246 $this->assertNotEqual($value, $desc_encodeded);
248 // Normal meta tags should be encoded properly.
249 $this->assertRaw('"' . $desc_encoded . '"', 'Confirmed the node "description" meta tag string was encoded properly.');
250 // Normal meta tags with HTML entities should be displayed in their original
252 $this->assertNoRaw('"' . $desc_original . '"', 'Confirmed the node "description" meta tag string does not show in its original form.');
253 // Normal meta tags should not be double-encoded.
254 $this->assertNoRaw('"' . $desc_encodeded . '"', 'Confirmed the node "description" meta tag string was not double-encoded.');
258 * Tests that fields with encoded HTML entities will not be double-encoded.
260 public function checkEncodedField($string) {
261 $save_label = (floatval(\Drupal::VERSION) <= 8.3) ? t('Save and publish') : t('Save');
263 // The original strings.
264 $title_original = 'Title: ' . $string;
265 $desc_original = 'Description: ' . $string;
267 // The strings after they're encoded, but quotes will not be encoded.
268 $desc_encoded = htmlentities($desc_original, ENT_QUOTES);
270 // The strings double-encoded, to make sure the tags aren't broken.
271 $desc_encodeded = htmlentities($desc_encoded, ENT_QUOTES);
273 // Update the Global defaults and test them.
274 $this->drupalGet('admin/config/search/metatag/global');
275 $this->assertResponse(200);
277 'title' => $title_original,
278 'description' => $desc_original,
280 $this->drupalPostForm(NULL, $edit, t('Save'));
281 $this->assertResponse(200);
283 // Set up a node without explicit metatag description. This causes the
284 // global default to be used, which contains a token (node:summary). The
285 // token value should be correctly translated.
288 $this->drupalGet('node/add/page');
289 $this->assertResponse(200);
291 'title[0][value]' => $title_original,
292 'body[0][value]' => $desc_original,
294 $this->drupalPostForm(NULL, $edit, $save_label);
295 $this->assertResponse(200);
297 // Load the node page.
298 $this->drupalGet('node/1');
299 $this->assertResponse(200);
301 // With xpath the HTML entities will be parsed automagically.
302 $xpath = $this->xpath("//meta[@name='description']");
303 $value = (string) $xpath[0]['content'];
304 $this->assertEqual($value, $desc_original);
305 $this->assertNotEqual($value, $desc_encoded);
306 $this->assertNotEqual($value, $desc_encodeded);
308 // Normal meta tags should be encoded properly.
309 $this->assertRaw('"' . $desc_encoded . '"', 'Confirmed the node "description" meta tag string was encoded properly.');
311 // Normal meta tags with HTML entities should be displayed in their original
313 $this->assertNoRaw('"' . $desc_original . '"', 'Confirmed the node "description" meta tag string does not show in its original form.');
315 // Normal meta tags should not be double-encoded.
316 $this->assertNoRaw('"' . $desc_encodeded . '"', 'Confirmed the node "description" meta tag string was not double-encoded.');