PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/core/modules/system/lib/Drupal/system/Tests/Mail/HtmlToTextTest.php

https://bitbucket.org/aswinvk28/smartpan-stock-drupal
PHP | 389 lines | 266 code | 21 blank | 102 comment | 3 complexity | e4d80ed6174f909ed0ffa0a0b2848b45 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * @file
  4. * Definition of Drupal\system\Tests\Mail\HtmlToTextTest.
  5. */
  6. namespace Drupal\system\Tests\Mail;
  7. use Drupal\simpletest\WebTestBase;
  8. /**
  9. * Tests for drupal_html_to_text().
  10. */
  11. class HtmlToTextTest extends WebTestBase {
  12. public static function getInfo() {
  13. return array(
  14. 'name' => 'HTML to text conversion',
  15. 'description' => 'Tests drupal_html_to_text().',
  16. 'group' => 'Mail',
  17. );
  18. }
  19. /**
  20. * Converts a string to its PHP source equivalent for display in test messages.
  21. *
  22. * @param $text
  23. * The text string to convert.
  24. *
  25. * @return
  26. * An HTML representation of the text string that, when displayed in a
  27. * browser, represents the PHP source code equivalent of $text.
  28. */
  29. protected function stringToHtml($text) {
  30. return '"' .
  31. str_replace(
  32. array("\n", ' '),
  33. array('\n', '&nbsp;'),
  34. check_plain($text)
  35. ) . '"';
  36. }
  37. /**
  38. * Helper function for testing drupal_html_to_text().
  39. *
  40. * @param $html
  41. * The source HTML string to be converted.
  42. * @param $text
  43. * The expected result of converting $html to text.
  44. * @param $message
  45. * A text message to display in the assertion message.
  46. * @param $allowed_tags
  47. * (optional) An array of allowed tags, or NULL to default to the full
  48. * set of tags supported by drupal_html_to_text().
  49. */
  50. protected function assertHtmlToText($html, $text, $message, $allowed_tags = NULL) {
  51. preg_match_all('/<([a-z0-6]+)/', drupal_strtolower($html), $matches);
  52. $tested_tags = implode(', ', array_unique($matches[1]));
  53. $message .= ' (' . $tested_tags . ')';
  54. $result = drupal_html_to_text($html, $allowed_tags);
  55. $pass = $this->assertEqual($result, $text, check_plain($message));
  56. $verbose = 'html = <pre>' . $this->stringToHtml($html)
  57. . '</pre><br />' . 'result = <pre>' . $this->stringToHtml($result)
  58. . '</pre><br />' . 'expected = <pre>' . $this->stringToHtml($text)
  59. . '</pre>';
  60. $this->verbose($verbose);
  61. if (!$pass) {
  62. $this->pass("Previous test verbose info:<br />$verbose");
  63. }
  64. }
  65. /**
  66. * Test all supported tags of drupal_html_to_text().
  67. */
  68. public function testTags() {
  69. global $base_path, $base_url;
  70. $tests = array(
  71. // @todo Trailing linefeeds should be trimmed.
  72. '<a href = "http://drupal.org">Drupal.org</a>' => "Drupal.org [1]\n\n[1] http://drupal.org\n",
  73. // @todo Footer URLs should be absolute.
  74. "<a href = \"$base_path\">Homepage</a>" => "Homepage [1]\n\n[1] $base_url/\n",
  75. '<address>Drupal</address>' => "Drupal\n",
  76. // @todo The <address> tag is currently not supported.
  77. '<address>Drupal</address><address>Drupal</address>' => "DrupalDrupal\n",
  78. '<b>Drupal</b>' => "*Drupal*\n",
  79. // @todo There should be a space between the '>' and the text.
  80. '<blockquote>Drupal</blockquote>' => ">Drupal\n",
  81. '<blockquote>Drupal</blockquote><blockquote>Drupal</blockquote>' => ">Drupal\n>Drupal\n",
  82. '<br />Drupal<br />Drupal<br /><br />Drupal' => "Drupal\nDrupal\nDrupal\n",
  83. '<br/>Drupal<br/>Drupal<br/><br/>Drupal' => "Drupal\nDrupal\nDrupal\n",
  84. // @todo There should be two line breaks before the paragraph.
  85. '<br/>Drupal<br/>Drupal<br/><br/>Drupal<p>Drupal</p>' => "Drupal\nDrupal\nDrupal\nDrupal\n\n",
  86. '<div>Drupal</div>' => "Drupal\n",
  87. // @todo The <div> tag is currently not supported.
  88. '<div>Drupal</div><div>Drupal</div>' => "DrupalDrupal\n",
  89. '<em>Drupal</em>' => "/Drupal/\n",
  90. '<h1>Drupal</h1>' => "======== DRUPAL ==============================================================\n\n",
  91. '<h1>Drupal</h1><p>Drupal</p>' => "======== DRUPAL ==============================================================\n\nDrupal\n\n",
  92. '<h2>Drupal</h2>' => "-------- DRUPAL --------------------------------------------------------------\n\n",
  93. '<h2>Drupal</h2><p>Drupal</p>' => "-------- DRUPAL --------------------------------------------------------------\n\nDrupal\n\n",
  94. '<h3>Drupal</h3>' => ".... Drupal\n\n",
  95. '<h3>Drupal</h3><p>Drupal</p>' => ".... Drupal\n\nDrupal\n\n",
  96. '<h4>Drupal</h4>' => ".. Drupal\n\n",
  97. '<h4>Drupal</h4><p>Drupal</p>' => ".. Drupal\n\nDrupal\n\n",
  98. '<h5>Drupal</h5>' => "Drupal\n\n",
  99. '<h5>Drupal</h5><p>Drupal</p>' => "Drupal\n\nDrupal\n\n",
  100. '<h6>Drupal</h6>' => "Drupal\n\n",
  101. '<h6>Drupal</h6><p>Drupal</p>' => "Drupal\n\nDrupal\n\n",
  102. '<hr />Drupal<hr />' => "------------------------------------------------------------------------------\nDrupal\n------------------------------------------------------------------------------\n",
  103. '<hr/>Drupal<hr/>' => "------------------------------------------------------------------------------\nDrupal\n------------------------------------------------------------------------------\n",
  104. '<hr/>Drupal<hr/><p>Drupal</p>' => "------------------------------------------------------------------------------\nDrupal\n------------------------------------------------------------------------------\nDrupal\n\n",
  105. '<i>Drupal</i>' => "/Drupal/\n",
  106. '<p>Drupal</p>' => "Drupal\n\n",
  107. '<p>Drupal</p><p>Drupal</p>' => "Drupal\n\nDrupal\n\n",
  108. '<strong>Drupal</strong>' => "*Drupal*\n",
  109. // @todo Tables are currently not supported.
  110. '<table><tr><td>Drupal</td><td>Drupal</td></tr><tr><td>Drupal</td><td>Drupal</td></tr></table>' => "DrupalDrupalDrupalDrupal\n",
  111. '<table><tr><td>Drupal</td></tr></table><p>Drupal</p>' => "Drupal\nDrupal\n\n",
  112. // @todo The <u> tag is currently not supported.
  113. '<u>Drupal</u>' => "Drupal\n",
  114. '<ul><li>Drupal</li></ul>' => " * Drupal\n\n",
  115. '<ul><li>Drupal <em>Drupal</em> Drupal</li></ul>' => " * Drupal /Drupal/ Drupal\n\n",
  116. // @todo Lines containing nothing but spaces should be trimmed.
  117. '<ul><li>Drupal</li><li><ol><li>Drupal</li><li>Drupal</li></ol></li></ul>' => " * Drupal\n * 1) Drupal\n 2) Drupal\n \n\n",
  118. '<ul><li>Drupal</li><li><ol><li>Drupal</li></ol></li><li>Drupal</li></ul>' => " * Drupal\n * 1) Drupal\n \n * Drupal\n\n",
  119. '<ul><li>Drupal</li><li>Drupal</li></ul>' => " * Drupal\n * Drupal\n\n",
  120. '<ul><li>Drupal</li></ul><p>Drupal</p>' => " * Drupal\n\nDrupal\n\n",
  121. '<ol><li>Drupal</li></ol>' => " 1) Drupal\n\n",
  122. '<ol><li>Drupal</li><li><ul><li>Drupal</li><li>Drupal</li></ul></li></ol>' => " 1) Drupal\n 2) * Drupal\n * Drupal\n \n\n",
  123. '<ol><li>Drupal</li><li>Drupal</li></ol>' => " 1) Drupal\n 2) Drupal\n\n",
  124. '<ol>Drupal</ol>' => "Drupal\n\n",
  125. '<ol><li>Drupal</li></ol><p>Drupal</p>' => " 1) Drupal\n\nDrupal\n\n",
  126. '<dl><dt>Drupal</dt></dl>' => "Drupal\n\n",
  127. '<dl><dt>Drupal</dt><dd>Drupal</dd></dl>' => "Drupal\n Drupal\n\n",
  128. '<dl><dt>Drupal</dt><dd>Drupal</dd><dt>Drupal</dt><dd>Drupal</dd></dl>' => "Drupal\n Drupal\nDrupal\n Drupal\n\n",
  129. '<dl><dt>Drupal</dt><dd>Drupal</dd></dl><p>Drupal</p>' => "Drupal\n Drupal\n\nDrupal\n\n",
  130. '<dl><dt>Drupal<dd>Drupal</dl>' => "Drupal\n Drupal\n\n",
  131. '<dl><dt>Drupal</dt></dl><p>Drupal</p>' => "Drupal\n\nDrupal\n\n",
  132. // @todo Again, lines containing only spaces should be trimmed.
  133. '<ul><li>Drupal</li><li><dl><dt>Drupal</dt><dd>Drupal</dd><dt>Drupal</dt><dd>Drupal</dd></dl></li><li>Drupal</li></ul>' => " * Drupal\n * Drupal\n Drupal\n Drupal\n Drupal\n \n * Drupal\n\n",
  134. // Tests malformed HTML tags.
  135. '<br>Drupal<br>Drupal' => "Drupal\nDrupal\n",
  136. '<hr>Drupal<hr>Drupal' => "------------------------------------------------------------------------------\nDrupal\n------------------------------------------------------------------------------\nDrupal\n",
  137. '<ol><li>Drupal<li>Drupal</ol>' => " 1) Drupal\n 2) Drupal\n\n",
  138. '<ul><li>Drupal <em>Drupal</em> Drupal</ul></ul>' => " * Drupal /Drupal/ Drupal\n\n",
  139. '<ul><li>Drupal<li>Drupal</ol>' => " * Drupal\n * Drupal\n\n",
  140. '<ul><li>Drupal<li>Drupal</ul>' => " * Drupal\n * Drupal\n\n",
  141. '<ul>Drupal</ul>' => "Drupal\n\n",
  142. 'Drupal</ul></ol></dl><li>Drupal' => "Drupal\n * Drupal\n",
  143. '<dl>Drupal</dl>' => "Drupal\n\n",
  144. '<dl>Drupal</dl><p>Drupal</p>' => "Drupal\n\nDrupal\n\n",
  145. '<dt>Drupal</dt>' => "Drupal\n",
  146. // Tests some unsupported HTML tags.
  147. '<html>Drupal</html>' => "Drupal\n",
  148. // @todo Perhaps the contents of <script> tags should be dropped.
  149. '<script type="text/javascript">Drupal</script>' => "Drupal\n",
  150. // A couple of tests for Unicode characters.
  151. '<q>I <em>will</em> be back…</q>' => "I /will/ be back…\n",
  152. 'FrançAIS is ÜBER-åwesome' => "FrançAIS is ÜBER-åwesome\n",
  153. );
  154. foreach ($tests as $html => $text) {
  155. $this->assertHtmlToText($html, $text, 'Supported tags');
  156. }
  157. }
  158. /**
  159. * Test $allowed_tags argument of drupal_html_to_text().
  160. */
  161. public function testDrupalHtmlToTextArgs() {
  162. // The second parameter of drupal_html_to_text() overrules the allowed tags.
  163. $this->assertHtmlToText(
  164. 'Drupal <b>Drupal</b> Drupal',
  165. "Drupal *Drupal* Drupal\n",
  166. 'Allowed <b> tag found',
  167. array('b')
  168. );
  169. $this->assertHtmlToText(
  170. 'Drupal <h1>Drupal</h1> Drupal',
  171. "Drupal Drupal Drupal\n",
  172. 'Disallowed <h1> tag not found',
  173. array('b')
  174. );
  175. $this->assertHtmlToText(
  176. 'Drupal <p><em><b>Drupal</b></em><p> Drupal',
  177. "Drupal Drupal Drupal\n",
  178. 'Disallowed <p>, <em>, and <b> tags not found',
  179. array('a', 'br', 'h1')
  180. );
  181. $this->assertHtmlToText(
  182. '<html><body>Drupal</body></html>',
  183. "Drupal\n",
  184. 'Unsupported <html> and <body> tags not found',
  185. array('html', 'body')
  186. );
  187. }
  188. /**
  189. * Test that whitespace is collapsed.
  190. */
  191. public function testDrupalHtmltoTextCollapsesWhitespace() {
  192. $input = "<p>Drupal Drupal\n\nDrupal<pre>Drupal Drupal\n\nDrupal</pre>Drupal Drupal\n\nDrupal</p>";
  193. // @todo The whitespace should be collapsed.
  194. $collapsed = "Drupal Drupal\n\nDrupalDrupal Drupal\n\nDrupalDrupal Drupal\n\nDrupal\n\n";
  195. $this->assertHtmlToText(
  196. $input,
  197. $collapsed,
  198. 'Whitespace is collapsed',
  199. array('p')
  200. );
  201. }
  202. /**
  203. * Test that text separated by block-level tags in HTML get separated by
  204. * (at least) a newline in the plaintext version.
  205. */
  206. public function testDrupalHtmlToTextBlockTagToNewline() {
  207. $input = '[text]'
  208. . '<blockquote>[blockquote]</blockquote>'
  209. . '<br />[br]'
  210. . '<dl><dt>[dl-dt]</dt>'
  211. . '<dt>[dt]</dt>'
  212. . '<dd>[dd]</dd>'
  213. . '<dd>[dd-dl]</dd></dl>'
  214. . '<h1>[h1]</h1>'
  215. . '<h2>[h2]</h2>'
  216. . '<h3>[h3]</h3>'
  217. . '<h4>[h4]</h4>'
  218. . '<h5>[h5]</h5>'
  219. . '<h6>[h6]</h6>'
  220. . '<hr />[hr]'
  221. . '<ol><li>[ol-li]</li>'
  222. . '<li>[li]</li>'
  223. . '<li>[li-ol]</li></ol>'
  224. . '<p>[p]</p>'
  225. . '<ul><li>[ul-li]</li>'
  226. . '<li>[li-ul]</li></ul>'
  227. . '[text]';
  228. $output = drupal_html_to_text($input);
  229. $pass = $this->assertFalse(
  230. preg_match('/\][^\n]*\[/s', $output),
  231. 'Block-level HTML tags should force newlines'
  232. );
  233. if (!$pass) {
  234. $this->verbose($this->stringToHtml($output));
  235. }
  236. $output_upper = drupal_strtoupper($output);
  237. $upper_input = drupal_strtoupper($input);
  238. $upper_output = drupal_html_to_text($upper_input);
  239. $pass = $this->assertEqual(
  240. $upper_output,
  241. $output_upper,
  242. 'Tag recognition should be case-insensitive'
  243. );
  244. if (!$pass) {
  245. $this->verbose(
  246. $upper_output
  247. . '<br />should be equal to <br />'
  248. . $output_upper
  249. );
  250. }
  251. }
  252. /**
  253. * Test that headers are properly separated from surrounding text.
  254. */
  255. public function testHeaderSeparation() {
  256. $html = 'Drupal<h1>Drupal</h1>Drupal';
  257. // @todo There should be more space above the header than below it.
  258. $text = "Drupal\n======== DRUPAL ==============================================================\n\nDrupal\n";
  259. $this->assertHtmlToText($html, $text,
  260. 'Text before and after <h1> tag');
  261. $html = '<p>Drupal</p><h1>Drupal</h1>Drupal';
  262. // @todo There should be more space above the header than below it.
  263. $text = "Drupal\n\n======== DRUPAL ==============================================================\n\nDrupal\n";
  264. $this->assertHtmlToText($html, $text,
  265. 'Paragraph before and text after <h1> tag');
  266. $html = 'Drupal<h1>Drupal</h1><p>Drupal</p>';
  267. // @todo There should be more space above the header than below it.
  268. $text = "Drupal\n======== DRUPAL ==============================================================\n\nDrupal\n\n";
  269. $this->assertHtmlToText($html, $text,
  270. 'Text before and paragraph after <h1> tag');
  271. $html = '<p>Drupal</p><h1>Drupal</h1><p>Drupal</p>';
  272. $text = "Drupal\n\n======== DRUPAL ==============================================================\n\nDrupal\n\n";
  273. $this->assertHtmlToText($html, $text,
  274. 'Paragraph before and after <h1> tag');
  275. }
  276. /**
  277. * Test that footnote references are properly generated.
  278. */
  279. public function testFootnoteReferences() {
  280. global $base_path, $base_url;
  281. $source = '<a href="http://www.example.com/node/1">Host and path</a>'
  282. . '<br /><a href="http://www.example.com">Host, no path</a>'
  283. . '<br /><a href="' . $base_path . 'node/1">Path, no host</a>'
  284. . '<br /><a href="node/1">Relative path</a>';
  285. // @todo Footnote URLs should be absolute.
  286. $tt = "Host and path [1]"
  287. . "\nHost, no path [2]"
  288. // @todo The following two references should be combined.
  289. . "\nPath, no host [3]"
  290. . "\nRelative path [4]"
  291. . "\n"
  292. . "\n[1] http://www.example.com/node/1"
  293. . "\n[2] http://www.example.com"
  294. // @todo The following two references should be combined.
  295. . "\n[3] $base_url/node/1"
  296. . "\n[4] node/1\n";
  297. $this->assertHtmlToText($source, $tt, 'Footnotes');
  298. }
  299. /**
  300. * Test that combinations of paragraph breaks, line breaks, linefeeds,
  301. * and spaces are properly handled.
  302. */
  303. public function testDrupalHtmlToTextParagraphs() {
  304. $tests = array();
  305. $tests[] = array(
  306. 'html' => "<p>line 1<br />\nline 2<br />line 3\n<br />line 4</p><p>paragraph</p>",
  307. // @todo Trailing line breaks should be trimmed.
  308. 'text' => "line 1\nline 2\nline 3\nline 4\n\nparagraph\n\n",
  309. );
  310. $tests[] = array(
  311. 'html' => "<p>line 1<br /> line 2</p> <p>line 4<br /> line 5</p> <p>0</p>",
  312. // @todo Trailing line breaks should be trimmed.
  313. 'text' => "line 1\nline 2\n\nline 4\nline 5\n\n0\n\n",
  314. );
  315. foreach ($tests as $test) {
  316. $this->assertHtmlToText($test['html'], $test['text'], 'Paragraph breaks');
  317. }
  318. }
  319. /**
  320. * Tests that drupal_html_to_text() wraps before 1000 characters.
  321. *
  322. * RFC 3676 says, "The Text/Plain media type is the lowest common
  323. * denominator of Internet email, with lines of no more than 998 characters."
  324. *
  325. * RFC 2046 says, "SMTP [RFC-821] allows a maximum of 998 octets before the
  326. * next CRLF sequence."
  327. *
  328. * RFC 821 says, "The maximum total length of a text line including the
  329. * <CRLF> is 1000 characters."
  330. */
  331. public function testVeryLongLineWrap() {
  332. $input = 'Drupal<br /><p>' . str_repeat('x', 2100) . '</><br />Drupal';
  333. $output = drupal_html_to_text($input);
  334. $eol = settings()->get('mail_line_endings', PHP_EOL);
  335. $maximum_line_length = 0;
  336. foreach (explode($eol, $output) as $line) {
  337. // We must use strlen() rather than drupal_strlen() in order to count
  338. // octets rather than characters.
  339. $maximum_line_length = max($maximum_line_length, strlen($line . $eol));
  340. }
  341. $verbose = 'Maximum line length found was ' . $maximum_line_length . ' octets.';
  342. // @todo This should assert that $maximum_line_length <= 1000.
  343. $this->pass($verbose);
  344. }
  345. /**
  346. * Tests that drupal_wrap_mail() removes trailing whitespace before newlines.
  347. */
  348. public function testRemoveTrailingWhitespace() {
  349. $text = "Hi there! \nHerp Derp";
  350. $mail_lines = explode("\n", drupal_wrap_mail($text));
  351. $this->assertNotEqual(" ", substr($mail_lines[0], -1), 'Trailing whitespace removed.');
  352. }
  353. /**
  354. * Tests that drupal_wrap_mail() does not remove the trailing whitespace from
  355. * Usenet style signatures.
  356. *
  357. * RFC 3676 says, "This is a special case; an (optionally quoted or quoted and
  358. * stuffed) line consisting of DASH DASH SP is neither fixed nor flowed."
  359. */
  360. public function testUsenetSignature() {
  361. $text = "Hi there!\n-- \nHerp Derp";
  362. $mail_lines = explode("\n", drupal_wrap_mail($text));
  363. $this->assertEqual("-- ", $mail_lines[1], 'Trailing whitespace not removed for dash-dash-space signatures.');
  364. $text = "Hi there!\n-- \nHerp Derp";
  365. $mail_lines = explode("\n", drupal_wrap_mail($text));
  366. $this->assertEqual("--", $mail_lines[1], 'Trailing whitespace removed for incorrect dash-dash-space signatures.');
  367. }
  368. }