Updated to Drupal 8.5. Core Media not yet in use.
[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   protected 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   public function testSingleQuote() {
79     $this->checkString("bla'bleblu");
80   }
81
82   /**
83    * Tests that a meta tag with a double quote is not double escaped.
84    */
85   public function testDoubleQuote() {
86     $this->checkString('bla"bleblu');
87   }
88
89   /**
90    * Tests that a meta tag with an ampersand is not double escaped.
91    */
92   public function testAmpersand() {
93     $this->checkString("blable&blu");
94   }
95
96   /**
97    * Tests that specific strings are not double escaped.
98    */
99   public function checkString($string) {
100     $this->checkConfig($string);
101     $this->checkNode($string);
102     $this->checkEncodedField($string);
103   }
104
105   /**
106    * Tests that a specific config string is not double encoded.
107    */
108   public function checkConfig($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     $edit = [
125       'title' => $title_original,
126       'description' => $desc_original,
127     ];
128     $this->drupalPostForm(NULL, $edit, '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     $save_label = (floatval(\Drupal::VERSION) <= 8.3) ? t('Save and publish') : t('Save');
157     $this->drupalPostForm(NULL, $edit, $save_label);
158
159     $this->config('system.site')->set('page.front', '/node/1')->save();
160
161     // Load the front page.
162     $this->drupalGet('<front>');
163     $this->assertResponse(200);
164
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);
170
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
173     // xpath does.
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?');
177
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);
183   }
184
185   /**
186    * Tests that a specific node string is not double escaped.
187    */
188   public function checkNode($string) {
189     $save_label = (floatval(\Drupal::VERSION) <= 8.3) ? t('Save and publish') : t('Save');
190
191     // The original strings.
192     $title_original = 'Title: ' . $string;
193     $desc_original = 'Description: ' . $string;
194
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);
198
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);
202
203     // Update the Global defaults and test them.
204     $this->drupalGet('admin/config/search/metatag/global');
205     $this->assertResponse(200);
206     $edit = [
207       'title' => $title_original,
208       'description' => $desc_original,
209     ];
210     $this->drupalPostForm(NULL, $edit, t('Save'));
211     $this->assertResponse(200);
212
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.
216
217     // Create a node.
218     $this->drupalGet('node/add/page');
219     $this->assertResponse(200);
220     $edit = [
221       'title[0][value]' => $title_original,
222       'body[0][value]' => $desc_original,
223     ];
224     $this->drupalPostForm(NULL, $edit, $save_label);
225     $this->assertResponse(200);
226
227     // Load the node page.
228     $this->drupalGet('node/1');
229     $this->assertResponse(200);
230
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);
236
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
239     // xpath does.
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);
247
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
251     // format.
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.');
255   }
256
257   /**
258    * Tests that fields with encoded HTML entities will not be double-encoded.
259    */
260   public function checkEncodedField($string) {
261     $save_label = (floatval(\Drupal::VERSION) <= 8.3) ? t('Save and publish') : t('Save');
262
263     // The original strings.
264     $title_original = 'Title: ' . $string;
265     $desc_original = 'Description: ' . $string;
266
267     // The strings after they're encoded, but quotes will not be encoded.
268     $desc_encoded = htmlentities($desc_original, ENT_QUOTES);
269
270     // The strings double-encoded, to make sure the tags aren't broken.
271     $desc_encodeded = htmlentities($desc_encoded, ENT_QUOTES);
272
273     // Update the Global defaults and test them.
274     $this->drupalGet('admin/config/search/metatag/global');
275     $this->assertResponse(200);
276     $edit = [
277       'title' => $title_original,
278       'description' => $desc_original,
279     ];
280     $this->drupalPostForm(NULL, $edit, t('Save'));
281     $this->assertResponse(200);
282
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.
286
287     // Create a node.
288     $this->drupalGet('node/add/page');
289     $this->assertResponse(200);
290     $edit = [
291       'title[0][value]' => $title_original,
292       'body[0][value]' => $desc_original,
293     ];
294     $this->drupalPostForm(NULL, $edit, $save_label);
295     $this->assertResponse(200);
296
297     // Load the node page.
298     $this->drupalGet('node/1');
299     $this->assertResponse(200);
300
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);
307
308     // Normal meta tags should be encoded properly.
309     $this->assertRaw('"' . $desc_encoded . '"', 'Confirmed the node "description" meta tag string was encoded properly.');
310
311     // Normal meta tags with HTML entities should be displayed in their original
312     // format.
313     $this->assertNoRaw('"' . $desc_original . '"', 'Confirmed the node "description" meta tag string does not show in its original form.');
314
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.');
317   }
318
319 }