PageRenderTime 24ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/web/modules/contrib/metatag/tests/src/Functional/MetatagStringTest.php

https://gitlab.com/mohamed_hussein/prodt
PHP | 322 lines | 197 code | 43 blank | 82 comment | 0 complexity | e706cf12a8d295921f86c4dc91eecdc9 MD5 | raw file
  1. <?php
  2. namespace Drupal\Tests\metatag\Functional;
  3. use Drupal\Tests\BrowserTestBase;
  4. use Drupal\Core\StringTranslation\StringTranslationTrait;
  5. /**
  6. * Ensures that the Metatag field works correctly.
  7. *
  8. * @group metatag
  9. */
  10. class MetatagStringTest extends BrowserTestBase {
  11. use StringTranslationTrait;
  12. /**
  13. * Admin user.
  14. *
  15. * @var \Drupal\Core\Session\AccountInterface
  16. */
  17. protected $adminUser;
  18. /**
  19. * Modules to enable.
  20. *
  21. * @var array
  22. */
  23. protected static $modules = [
  24. 'token',
  25. 'node',
  26. 'field_ui',
  27. 'metatag',
  28. ];
  29. /**
  30. * {@inheritdoc}
  31. */
  32. protected $defaultTheme = 'stark';
  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. * {@inheritdoc}
  51. */
  52. protected function setUp(): void {
  53. parent::setUp();
  54. $this->adminUser = $this->drupalCreateUser($this->permissions);
  55. $this->drupalLogin($this->adminUser);
  56. $this->drupalCreateContentType(['type' => 'page', 'display_submitted' => FALSE]);
  57. // Add a Metatag field to the content type.
  58. $this->drupalGet('admin/structure/types');
  59. $this->assertSession()->statusCodeEquals(200);
  60. $this->drupalGet('admin/structure/types/manage/page/fields/add-field');
  61. $this->assertSession()->statusCodeEquals(200);
  62. $edit = [
  63. 'label' => 'Metatag',
  64. 'field_name' => 'metatag_field',
  65. 'new_storage_type' => 'metatag',
  66. ];
  67. $this->submitForm($edit, $this->t('Save and continue'));
  68. $this->submitForm([], $this->t('Save field settings'));
  69. $this->container->get('entity_field.manager')->clearCachedFieldDefinitions();
  70. }
  71. /**
  72. * Tests that a meta tag with single quote is not double escaped.
  73. */
  74. public function testSingleQuote() {
  75. $this->checkString("bla'bleblu");
  76. }
  77. /**
  78. * Tests that a meta tag with a double quote is not double escaped.
  79. */
  80. public function testDoubleQuote() {
  81. $this->checkString('bla"bleblu');
  82. }
  83. /**
  84. * Tests that a meta tag with an ampersand is not double escaped.
  85. */
  86. public function testAmpersand() {
  87. $this->checkString("blable&blu");
  88. }
  89. /**
  90. * Tests that specific strings are not double escaped.
  91. */
  92. public function checkString($string) {
  93. $this->checkConfig($string);
  94. $this->checkNode($string);
  95. $this->checkEncodedField($string);
  96. }
  97. /**
  98. * Tests that a specific config string is not double encoded.
  99. */
  100. public function checkConfig($string) {
  101. // The original strings.
  102. $title_original = 'Title: ' . $string;
  103. $desc_original = 'Description: ' . $string;
  104. // The strings after they're encoded, but quotes will not be encoded.
  105. $title_encoded = htmlentities($title_original, ENT_QUOTES);
  106. $desc_encoded = htmlentities($desc_original, ENT_QUOTES);
  107. // The strings double-encoded, to make sure the tags aren't broken.
  108. $title_encodeded = htmlentities($title_encoded, ENT_QUOTES);
  109. $desc_encodeded = htmlentities($desc_encoded, ENT_QUOTES);
  110. // Update the Global defaults and test them.
  111. $this->drupalGet('admin/config/search/metatag/front');
  112. $session = $this->assertSession();
  113. $session->statusCodeEquals(200);
  114. $edit = [
  115. 'title' => $title_original,
  116. 'description' => $desc_original,
  117. ];
  118. $this->submitForm($edit, 'Save');
  119. $session->statusCodeEquals(200);
  120. $metatag_defaults = \Drupal::config('metatag.metatag_defaults.front');
  121. $default_title = $metatag_defaults->get('tags')['title'];
  122. $default_description = $metatag_defaults->get('tags')['description'];
  123. // Make sure the title tag is stored correctly.
  124. $this->assertEquals($title_original, $default_title, 'The title tag was stored in its original format.');
  125. $this->assertNotEquals($title_encoded, $default_title, 'The title tag was not stored in an encoded format.');
  126. $this->assertNotEquals($title_encodeded, $default_title, 'The title tag was not stored in a double-encoded format.');
  127. // Make sure the description tag is stored correctly.
  128. $this->assertEquals($desc_original, $default_description, 'The description tag was stored in its original format.');
  129. $this->assertNotEquals($desc_encoded, $default_description, 'The description tag was not stored in an encoded format.');
  130. $this->assertNotEquals($desc_encodeded, $default_description, 'The description tag was not stored in a double-encoded format.');
  131. // Set up a node without explicit metatag description. This causes the
  132. // global default to be used, which contains a token (node:summary). The
  133. // token value should be correctly translated.
  134. // Create a node.
  135. $this->drupalGet('node/add/page');
  136. $session->statusCodeEquals(200);
  137. $edit = [
  138. 'title[0][value]' => $title_original,
  139. 'body[0][value]' => $desc_original,
  140. ];
  141. $this->submitForm($edit, 'Save');
  142. $this->config('system.site')->set('page.front', '/node/1')->save();
  143. // Load the front page.
  144. $this->drupalGet('<front>');
  145. $session->statusCodeEquals(200);
  146. // Again, with xpath the HTML entities will be parsed automagically.
  147. $xpath_title = current($this->xpath("//title"))->getText();
  148. $this->assertEquals($xpath_title, $title_original);
  149. $this->assertNotEquals($xpath_title, $title_encoded);
  150. $this->assertNotEquals($xpath_title, $title_encodeded);
  151. // The page title should be HTML encoded; have to do this check manually
  152. // because assertRaw() checks the raw HTML, not the parsed strings like
  153. // xpath does.
  154. $session->responseContains('<title>' . $title_encoded . '</title>', 'Confirmed the node title tag is available in its encoded format.');
  155. $session->responseNotContains('<title>' . $title_original . '</title>', 'Confirmed the node title tag is not available in its original format.');
  156. $session->responseNotContains('<title>' . $title_encodeded . '</title>', 'Confirmed the node title tag is not double-double-encoded?');
  157. // Again, with xpath the HTML entities will be parsed automagically.
  158. $xpath = $this->xpath("//meta[@name='description']");
  159. $this->assertEquals($xpath[0]->getAttribute('content'), $desc_original);
  160. $this->assertNotEquals($xpath[0]->getAttribute('content'), $desc_encoded);
  161. $this->assertNotEquals($xpath[0]->getAttribute('content'), $desc_encodeded);
  162. }
  163. /**
  164. * Tests that a specific node string is not double escaped.
  165. */
  166. public function checkNode($string) {
  167. // The original strings.
  168. $title_original = 'Title: ' . $string;
  169. $desc_original = 'Description: ' . $string;
  170. // The strings after they're encoded, but quotes will not be encoded.
  171. $title_encoded = htmlentities($title_original, ENT_QUOTES);
  172. $desc_encoded = htmlentities($desc_original, ENT_QUOTES);
  173. // The strings double-encoded, to make sure the tags aren't broken.
  174. $title_encodeded = htmlentities($title_encoded, ENT_QUOTES);
  175. $desc_encodeded = htmlentities($desc_encoded, ENT_QUOTES);
  176. // Update the Global defaults and test them.
  177. $this->drupalGet('admin/config/search/metatag/global');
  178. $session = $this->assertSession();
  179. $session->statusCodeEquals(200);
  180. $edit = [
  181. 'title' => $title_original,
  182. 'description' => $desc_original,
  183. ];
  184. $this->submitForm($edit, $this->t('Save'));
  185. $session->statusCodeEquals(200);
  186. // Set up a node without explicit metatag description. This causes the
  187. // global default to be used, which contains a token (node:summary). The
  188. // token value should be correctly translated.
  189. // Create a node.
  190. $this->drupalGet('node/add/page');
  191. $session->statusCodeEquals(200);
  192. $edit = [
  193. 'title[0][value]' => $title_original,
  194. 'body[0][value]' => $desc_original,
  195. ];
  196. $this->submitForm($edit, 'Save');
  197. $session->statusCodeEquals(200);
  198. // Load the node page.
  199. $this->drupalGet('node/1');
  200. $session->statusCodeEquals(200);
  201. // Again, with xpath the HTML entities will be parsed automagically.
  202. $xpath_title = current($this->xpath("//title"))->getText();
  203. $this->assertEquals($xpath_title, $title_original);
  204. $this->assertNotEquals($xpath_title, $title_encoded);
  205. $this->assertNotEquals($xpath_title, $title_encodeded);
  206. // The page title should be HTML encoded; have to do this check manually
  207. // because assertRaw() checks the raw HTML, not the parsed strings like
  208. // xpath does.
  209. $session->responseContains('<title>' . $title_encoded . '</title>', 'Confirmed the node title tag is encoded.');
  210. // Again, with xpath the HTML entities will be parsed automagically.
  211. $xpath = $this->xpath("//meta[@name='description']");
  212. $value = $xpath[0]->getAttribute('content');
  213. $this->assertEquals($value, $desc_original);
  214. $this->assertNotEquals($value, $desc_encoded);
  215. $this->assertNotEquals($value, $desc_encodeded);
  216. // Normal meta tags should be encoded properly.
  217. $session->responseContains('"' . $desc_encoded . '"', 'Confirmed the node "description" meta tag string was encoded properly.');
  218. // Normal meta tags with HTML entities should be displayed in their original
  219. // format.
  220. $session->responseNotContains('"' . $desc_original . '"', 'Confirmed the node "description" meta tag string does not show in its original form.');
  221. // Normal meta tags should not be double-encoded.
  222. $session->responseNotContains('"' . $desc_encodeded . '"', 'Confirmed the node "description" meta tag string was not double-encoded.');
  223. }
  224. /**
  225. * Tests that fields with encoded HTML entities will not be double-encoded.
  226. */
  227. public function checkEncodedField($string) {
  228. // The original strings.
  229. $title_original = 'Title: ' . $string;
  230. $desc_original = 'Description: ' . $string;
  231. // The strings after they're encoded, but quotes will not be encoded.
  232. $desc_encoded = htmlentities($desc_original, ENT_QUOTES);
  233. // The strings double-encoded, to make sure the tags aren't broken.
  234. $desc_encodeded = htmlentities($desc_encoded, ENT_QUOTES);
  235. // Update the Global defaults and test them.
  236. $this->drupalGet('admin/config/search/metatag/global');
  237. $session = $this->assertSession();
  238. $session->statusCodeEquals(200);
  239. $edit = [
  240. 'title' => $title_original,
  241. 'description' => $desc_original,
  242. ];
  243. $this->submitForm($edit, $this->t('Save'));
  244. $session->statusCodeEquals(200);
  245. // Set up a node without explicit metatag description. This causes the
  246. // global default to be used, which contains a token (node:summary). The
  247. // token value should be correctly translated.
  248. // Create a node.
  249. $this->drupalGet('node/add/page');
  250. $session->statusCodeEquals(200);
  251. $edit = [
  252. 'title[0][value]' => $title_original,
  253. 'body[0][value]' => $desc_original,
  254. ];
  255. $this->submitForm($edit, 'Save');
  256. $session->statusCodeEquals(200);
  257. // Load the node page.
  258. $this->drupalGet('node/1');
  259. $session->statusCodeEquals(200);
  260. // With xpath the HTML entities will be parsed automagically.
  261. $xpath = $this->xpath("//meta[@name='description']");
  262. $value = $xpath[0]->getAttribute('content');
  263. $this->assertEquals($value, $desc_original);
  264. $this->assertNotEquals($value, $desc_encoded);
  265. $this->assertNotEquals($value, $desc_encodeded);
  266. // Normal meta tags should be encoded properly.
  267. $session->responseContains('"' . $desc_encoded . '"', 'Confirmed the node "description" meta tag string was encoded properly.');
  268. // Normal meta tags with HTML entities should be displayed in their original
  269. // format.
  270. $session->responseNotContains('"' . $desc_original . '"', 'Confirmed the node "description" meta tag string does not show in its original form.');
  271. // Normal meta tags should not be double-encoded.
  272. $session->responseNotContains('"' . $desc_encodeded . '"', 'Confirmed the node "description" meta tag string was not double-encoded.');
  273. }
  274. }