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->drupalGet("admin/structure/types/manage/page/fields/add-field");
63 $this->assertResponse(200);
66 'field_name' => 'metatag_field',
67 'new_storage_type' => 'metatag',
69 $this->drupalPostForm(NULL, $edit, t('Save and continue'));
70 $this->drupalPostForm(NULL, [], t('Save field settings'));
71 $this->container->get('entity.manager')->clearCachedFieldDefinitions();
75 * Tests that a meta tag with single quote is not double escaped.
77 function testSingleQuote() {
78 $this->_testAString("bla'bleblu");
82 * Tests that a meta tag with a double quote is not double escaped.
84 function testDoubleQuote() {
85 $this->_testAString('bla"bleblu');
89 * Tests that a meta tag with an ampersand is not double escaped.
91 function testAmpersand() {
92 $this->_testAString("blable&blu");
96 * Tests that specific strings are not double escaped.
98 function _testAString($string) {
99 $this->_testConfig($string);
100 $this->_testNode($string);
101 $this->_testEncodedField($string);
105 * Tests that a specific config string is not double encoded.
107 function _testConfig($string) {
108 // The original strings.
109 $title_original = 'Title: ' . $string;
110 $desc_original = 'Description: ' . $string;
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);
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);
120 // Update the Global defaults and test them.
122 'title' => $title_original,
123 'description' => $desc_original,
125 $this->drupalPostForm('admin/config/search/metatag/front', $values, 'Save');
126 $this->assertResponse(200);
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'];
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.');
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.');
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.
147 $this->drupalGet("node/add/page");
149 'title[0][value]' => $title_original,
150 'body[0][value]' => $desc_original,
152 $this->drupalPostForm("node/add/page", $edit, t('Save and publish'));
154 $this->config('system.site')->set('page.front', '/node/1')->save();
156 // Load the front page.
157 $this->drupalGet('<front>');
158 $this->assertResponse(200);
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);
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
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?');
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);
181 * Tests that a specific node string is not double escaped.
183 function _testNode($string) {
184 // The original strings.
185 $title_original = 'Title: ' . $string;
186 $desc_original = 'Description: ' . $string;
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);
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);
196 // Update the Global defaults and test them.
197 $this->drupalGet('admin/config/search/metatag/global');
198 $this->assertResponse(200);
200 'title' => $title_original,
201 'description' => $desc_original,
203 $this->drupalPostForm(NULL, $values, t('Save'));
204 $this->assertResponse(200);
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.
211 $this->drupalGet('node/add/page');
212 $this->assertResponse(200);
214 'title[0][value]' => $title_original,
215 'body[0][value]' => $desc_original,
217 $this->drupalPostForm(NULL, $edit, t('Save and publish'));
218 $this->assertResponse(200);
220 // Load the node page.
221 $this->drupalGet('node/1');
222 $this->assertResponse(200);
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);
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
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);
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
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.');
251 * Tests that fields with encoded HTML entities will not be double-encoded.
253 function _testEncodedField($string) {
254 // The original strings.
255 $title_original = 'Title: ' . $string;
256 $desc_original = 'Description: ' . $string;
258 // The strings after they're encoded, but quotes will not be encoded.
259 $desc_encoded = htmlentities($desc_original, ENT_QUOTES);
261 // The strings double-encoded, to make sure the tags aren't broken.
262 $desc_encodeded = htmlentities($desc_encoded, ENT_QUOTES);
264 // Update the Global defaults and test them.
265 $this->drupalGet('admin/config/search/metatag/global');
266 $this->assertResponse(200);
268 'title' => $title_original,
269 'description' => $desc_original,
271 $this->drupalPostForm(NULL, $values, t('Save'));
272 $this->assertResponse(200);
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.
279 $this->drupalGet('node/add/page');
280 $this->assertResponse(200);
282 'title[0][value]' => $title_original,
283 'body[0][value]' => $desc_original,
285 $this->drupalPostForm(NULL, $edit, t('Save and publish'));
286 $this->assertResponse(200);
288 // Load the node page.
289 $this->drupalGet('node/1');
290 $this->assertResponse(200);
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);
299 // Normal meta tags should be encoded properly.
300 $this->assertRaw('"' . $desc_encoded . '"', 'Confirmed the node "description" meta tag string was encoded properly.');
302 // Normal meta tags with HTML entities should be displayed in their original
304 $this->assertNoRaw('"' . $desc_original . '"', 'Confirmed the node "description" meta tag string does not show in its original form.');
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.');