metatag.string_handling.test 11 KB

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