PageRenderTime 26ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/core/modules/system/src/Tests/Mail/HtmlToTextTest.php

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