metatag.string_handling.test 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php
  2. /**
  3. * Tests for Metatag's string handling.
  4. */
  5. class MetatagCoreStringHandlingTest extends MetatagTestHelper {
  6. /**
  7. * @var $admin_user
  8. * An admin user.
  9. */
  10. protected $admin_user;
  11. /**
  12. * {@inheritdoc}
  13. */
  14. public static function getInfo() {
  15. return array(
  16. 'name' => 'Metatag core tests for string handling',
  17. 'description' => "Tests Metatag's string handling.",
  18. 'group' => 'Metatag',
  19. 'dependencies' => array('ctools', 'token'),
  20. );
  21. }
  22. /**
  23. * {@inheritdoc}
  24. */
  25. function setUp(array $modules = array()) {
  26. parent::setUp($modules);
  27. $content_type = 'page';
  28. // Create an admin user and log them in.
  29. $perms = array(
  30. // Needed for the content type.
  31. 'create ' . $content_type . ' content',
  32. 'delete any ' . $content_type . ' content',
  33. 'edit any ' . $content_type . ' content',
  34. // This permission is required in order to create new revisions.
  35. 'administer nodes',
  36. );
  37. $this->adminUser = $this->createAdminUser($perms);
  38. // Log in the admin user.
  39. $this->drupalLogin($this->adminUser);
  40. }
  41. /**
  42. * Tests that a meta tag with single quote is not double escaped.
  43. */
  44. function testSingleQuote() {
  45. $this->_testAString("bla'bleblu");
  46. }
  47. /**
  48. * Tests that a meta tag with a double quote is not double escaped.
  49. */
  50. function testDoubleQuote() {
  51. $this->_testAString('bla"bleblu');
  52. }
  53. /**
  54. * Tests that a meta tag with an ampersand is not double escaped.
  55. */
  56. function testAmpersand() {
  57. $this->_testAString("blable&blu");
  58. }
  59. /**
  60. * Tests that specific strings are not double escaped.
  61. */
  62. function _testAString($string) {
  63. $this->_testConfig($string);
  64. $this->_testNode($string);
  65. $this->_testEncodedField($string);
  66. }
  67. /**
  68. * Tests that a specific config string is not double encoded.
  69. */
  70. function _testConfig($string) {
  71. // The original strings.
  72. $title_original = 'Title: ' . $string;
  73. $desc_original = 'Description: ' . $string;
  74. // The strings after they're encoded, but quotes will not be encoded.
  75. $title_encoded = htmlentities($title_original, ENT_QUOTES);
  76. $desc_encoded = htmlentities($desc_original, ENT_QUOTES);
  77. // The strings double-encoded, to make sure the tags aren't broken.
  78. $title_encodeded = htmlentities($title_encoded, ENT_QUOTES);
  79. $desc_encodeded = htmlentities($desc_encoded, ENT_QUOTES);
  80. // Test the front page.
  81. $instance = 'global:frontpage';
  82. // Save the main node configuration form to assign the description tag.
  83. $edit = array(
  84. // Just use [node:title] to avoid problems with the default suffix.
  85. 'metatags[und][title][value]' => $title_original,
  86. // Save the original string.
  87. 'metatags[und][description][value]' => $desc_original,
  88. );
  89. $this->drupalPost('admin/config/search/metatags/config/' . $instance, $edit, 'Save');
  90. $this->assertResponse(200);
  91. // Load the configuration object.
  92. $result = db_select('metatag_config', 'mc')
  93. ->fields('mc', array('config'))
  94. ->condition('mc.instance', $instance)
  95. ->execute()
  96. ->fetchAssoc();
  97. // Unserialize the configuration.
  98. $config = unserialize($result['config']);
  99. // Make sure the title tag is stored correctly.
  100. $this->assertEqual($title_original, $config['title']['value'], 'The title tag was stored in its original format.');
  101. $this->assertNotEqual($title_encoded, $config['title']['value'], 'The title tag was not stored in an encoded format.');
  102. $this->assertNotEqual($title_encodeded, $config['title']['value'], 'The title tag was not stored in a double-encoded format.');
  103. // Make sure the description tag is stored correctly.
  104. $this->assertEqual($desc_original, $config['description']['value'], 'The description tag was stored in its original format.');
  105. $this->assertNotEqual($desc_encoded, $config['description']['value'], 'The description tag was not stored in an encoded format.');
  106. $this->assertNotEqual($desc_encodeded, $config['description']['value'], 'The description tag was not stored in a double-encoded format.');
  107. // Load the front page.
  108. $this->drupalGet('<front>');
  109. $this->assertResponse(200);
  110. // assertTitle() uses xpath, which parses the HTML, so all of the HTML
  111. // entities will be converted automagically.
  112. $this->assertTitle($title_original, 'Confirmed the node title tag is available in its original format.');
  113. $this->assertNoTitle($title_encoded, 'Confirmed the node title tag is not double-encoded.');
  114. $this->assertNoTitle($title_encodeded, 'Confirmed the node title tag is not double-double-encoded?');
  115. // The page title should be HTML encoded; have to do this check manually
  116. // because assertRaw() checks the raw HTML, not the parsed strings like
  117. // xpath does.
  118. $this->assertRaw('<title>' . $title_original . '</title>', 'Confirmed the node title tag is available in its original format.');
  119. $this->assertNoRaw('<title>' . $title_encoded . '</title>', 'Confirmed the node title tag is not double-encoded.');
  120. $this->assertNoRaw('<title>' . $title_encodeded . '</title>', 'Confirmed the node title tag is not double-double-encoded?');
  121. // Again, with xpath the HTML entities will be parsed automagically.
  122. $xpath = $this->xpath("//meta[@name='description']");
  123. $this->assertEqual($xpath[0]['content'], $desc_original);
  124. $this->assertNotEqual($xpath[0]['content'], $desc_encoded);
  125. $this->assertNotEqual($xpath[0]['content'], $desc_encodeded);
  126. }
  127. /**
  128. * Tests that a specific node string is not double escaped.
  129. */
  130. function _testNode($string) {
  131. // The original strings.
  132. $title_original = 'Title: ' . $string;
  133. $desc_original = 'Description: ' . $string;
  134. // The strings after they're encoded, but quotes will not be encoded.
  135. $title_encoded = htmlentities($title_original, ENT_QUOTES);
  136. $desc_encoded = htmlentities($desc_original, ENT_QUOTES);
  137. // The strings double-encoded, to make sure the tags aren't broken.
  138. $title_encodeded = htmlentities($title_encoded, ENT_QUOTES);
  139. $desc_encodeded = htmlentities($desc_encoded, ENT_QUOTES);
  140. // Create a node and check how the meta tag is displayed.
  141. $node = $this->drupalCreateNode(array(
  142. 'title' => $title_original,
  143. 'body' => array(
  144. LANGUAGE_NONE => array(
  145. array(
  146. 'value' => $desc_original,
  147. 'format' => filter_default_format(),
  148. ),
  149. ),
  150. ),
  151. 'metatags' => array(
  152. LANGUAGE_NONE => array(
  153. 'abstract' => array('value' => '[node:title]'),
  154. ),
  155. ),
  156. ));
  157. // Page titles have a suffix added automatically.
  158. $suffix = ' | ' . variable_get('site_name', 'Drupal');
  159. // Load the node page.
  160. $this->drupalGet('node/' . $node->nid);
  161. $this->assertResponse(200);
  162. // assertTitle() uses xpath, which parses the HTML, so all of the HTML
  163. // entities will be converted automagically.
  164. $this->assertTitle($title_original . $suffix, 'Confirmed the node title tag is available in its original format.');
  165. $this->assertNoTitle($title_encoded . $suffix, 'Confirmed the node title tag is not double-encoded.');
  166. $this->assertNoTitle($title_encodeded . $suffix, 'Confirmed the node title tag is not double-double-encoded?');
  167. // The page title should be HTML encoded; have to do this check manually
  168. // because assertRaw() checks the raw HTML, not the parsed strings like
  169. // xpath does.
  170. $this->assertRaw('<title>' . $title_original . $suffix . '</title>', 'Confirmed the node title tag is encoded.');
  171. // Test a few other versions of the title, to ensure it isn't broken
  172. // on another tag.
  173. $xpath = $this->xpath("//meta[@name='abstract']");
  174. $this->assertEqual($xpath[0]['content'], $title_original);
  175. $this->assertNotEqual($xpath[0]['content'], $title_encoded);
  176. $this->assertNotEqual($xpath[0]['content'], $title_encodeded);
  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);
  182. // Normal meta tags should be encoded properly.
  183. $this->assertRaw('"' . $desc_encoded . '"', 'Confirmed the node "description" meta tag string was encoded properly.');
  184. // Normal meta tags with HTML entities should be displayed in their original
  185. // format.
  186. $this->assertNoRaw('"' . $desc_original . '"', 'Confirmed the node "description" meta tag string does not show in its original form.');
  187. // Normal meta tags should not be double-encoded.
  188. $this->assertNoRaw('"' . $desc_encodeded . '"', 'Confirmed the node "description" meta tag string was not double-encoded.');
  189. }
  190. /**
  191. * Tests that fields with encoded HTML entities will not be double-encoded.
  192. */
  193. function _testEncodedField($string) {
  194. // The original strings.
  195. $title_original = 'Title: ' . $string;
  196. $desc_original = 'Description: ' . $string;
  197. // The strings after they're encoded, but quotes will not be encoded.
  198. $desc_encoded = htmlentities($desc_original, ENT_QUOTES);
  199. // The strings double-encoded, to make sure the tags aren't broken.
  200. $desc_encodeded = htmlentities($desc_encoded, ENT_QUOTES);
  201. // Create a node and check how the meta tag is displayed.
  202. $node = $this->drupalCreateNode(array(
  203. 'title' => $title_original,
  204. 'body' => array(
  205. LANGUAGE_NONE => array(
  206. array(
  207. 'value' => $desc_encoded,
  208. 'format' => filter_default_format(),
  209. ),
  210. ),
  211. ),
  212. ));
  213. // Load the node page.
  214. $this->drupalGet('node/' . $node->nid);
  215. $this->assertResponse(200);
  216. // With xpath the HTML entities will be parsed automagically.
  217. $xpath = $this->xpath("//meta[@name='description']");
  218. $this->assertEqual($xpath[0]['content'], $desc_original);
  219. $this->assertNotEqual($xpath[0]['content'], $desc_encoded);
  220. $this->assertNotEqual($xpath[0]['content'], $desc_encodeded);
  221. // Normal meta tags should be encoded properly.
  222. $this->assertRaw('"' . $desc_encoded . '"', 'Confirmed the node "description" meta tag string was encoded properly.');
  223. // Normal meta tags with HTML entities should be displayed in their original
  224. // format.
  225. $this->assertNoRaw('"' . $desc_original . '"', 'Confirmed the node "description" meta tag string does not show in its original form.');
  226. // Normal meta tags should not be double-encoded.
  227. $this->assertNoRaw('"' . $desc_encodeded . '"', 'Confirmed the node "description" meta tag string was not double-encoded.');
  228. }
  229. }