mail.test 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. <?php
  2. /**
  3. * @file
  4. * Test the Drupal mailing system.
  5. */
  6. class MailTestCase extends DrupalWebTestCase implements MailSystemInterface {
  7. /**
  8. * The most recent message that was sent through the test case.
  9. *
  10. * We take advantage here of the fact that static variables are shared among
  11. * all instance of the same class.
  12. */
  13. private static $sent_message;
  14. public static function getInfo() {
  15. return array(
  16. 'name' => 'Mail system',
  17. 'description' => 'Performs tests on the pluggable mailing framework.',
  18. 'group' => 'System',
  19. );
  20. }
  21. function setUp() {
  22. parent::setUp(array('simpletest'));
  23. // Set MailTestCase (i.e. this class) as the SMTP library
  24. variable_set('mail_system', array('default-system' => 'MailTestCase'));
  25. }
  26. /**
  27. * Assert that the pluggable mail system is functional.
  28. */
  29. function testPluggableFramework() {
  30. global $language;
  31. // Use MailTestCase for sending a message.
  32. $message = drupal_mail('simpletest', 'mail_test', 'testing@example.com', $language);
  33. // Assert whether the message was sent through the send function.
  34. $this->assertEqual(self::$sent_message['to'], 'testing@example.com', 'Pluggable mail system is extendable.');
  35. }
  36. /**
  37. * Test that message sending may be canceled.
  38. *
  39. * @see simpletest_mail_alter()
  40. */
  41. function testCancelMessage() {
  42. global $language;
  43. // Reset the class variable holding a copy of the last sent message.
  44. self::$sent_message = NULL;
  45. // Send a test message that simpletest_mail_alter should cancel.
  46. $message = drupal_mail('simpletest', 'cancel_test', 'cancel@example.com', $language);
  47. // Assert that the message was not actually sent.
  48. $this->assertNull(self::$sent_message, 'Message was canceled.');
  49. }
  50. /**
  51. * Concatenate and wrap the e-mail body for plain-text mails.
  52. *
  53. * @see DefaultMailSystem
  54. */
  55. public function format(array $message) {
  56. // Join the body array into one string.
  57. $message['body'] = implode("\n\n", $message['body']);
  58. // Convert any HTML to plain-text.
  59. $message['body'] = drupal_html_to_text($message['body']);
  60. // Wrap the mail body for sending.
  61. $message['body'] = drupal_wrap_mail($message['body']);
  62. return $message;
  63. }
  64. /**
  65. * Send function that is called through the mail system.
  66. */
  67. public function mail(array $message) {
  68. self::$sent_message = $message;
  69. }
  70. }
  71. /**
  72. * Unit tests for drupal_html_to_text().
  73. */
  74. class DrupalHtmlToTextTestCase extends DrupalWebTestCase {
  75. public static function getInfo() {
  76. return array(
  77. 'name' => 'HTML to text conversion',
  78. 'description' => 'Tests drupal_html_to_text().',
  79. 'group' => 'Mail',
  80. );
  81. }
  82. /**
  83. * Converts a string to its PHP source equivalent for display in test messages.
  84. *
  85. * @param $text
  86. * The text string to convert.
  87. *
  88. * @return
  89. * An HTML representation of the text string that, when displayed in a
  90. * browser, represents the PHP source code equivalent of $text.
  91. */
  92. function stringToHtml($text) {
  93. return '"' .
  94. str_replace(
  95. array("\n", ' '),
  96. array('\n', '&nbsp;'),
  97. check_plain($text)
  98. ) . '"';
  99. }
  100. /**
  101. * Helper function for testing drupal_html_to_text().
  102. *
  103. * @param $html
  104. * The source HTML string to be converted.
  105. * @param $text
  106. * The expected result of converting $html to text.
  107. * @param $message
  108. * A text message to display in the assertion message.
  109. * @param $allowed_tags
  110. * (optional) An array of allowed tags, or NULL to default to the full
  111. * set of tags supported by drupal_html_to_text().
  112. */
  113. function assertHtmlToText($html, $text, $message, $allowed_tags = NULL) {
  114. preg_match_all('/<([a-z0-6]+)/', drupal_strtolower($html), $matches);
  115. $tested_tags = implode(', ', array_unique($matches[1]));
  116. $message .= ' (' . $tested_tags . ')';
  117. $result = drupal_html_to_text($html, $allowed_tags);
  118. $pass = $this->assertEqual($result, $text, check_plain($message));
  119. $verbose = 'html = <pre>' . $this->stringToHtml($html)
  120. . '</pre><br />' . 'result = <pre>' . $this->stringToHtml($result)
  121. . '</pre><br />' . 'expected = <pre>' . $this->stringToHtml($text)
  122. . '</pre>';
  123. $this->verbose($verbose);
  124. if (!$pass) {
  125. $this->pass("Previous test verbose info:<br />$verbose");
  126. }
  127. }
  128. /**
  129. * Test all supported tags of drupal_html_to_text().
  130. */
  131. function testTags() {
  132. global $base_path, $base_url;
  133. $tests = array(
  134. // @todo Trailing linefeeds should be trimmed.
  135. '<a href = "http://drupal.org">Drupal.org</a>' => "Drupal.org [1]\n\n[1] http://drupal.org\n",
  136. // @todo Footer URLs should be absolute.
  137. "<a href = \"$base_path\">Homepage</a>" => "Homepage [1]\n\n[1] $base_url/\n",
  138. '<address>Drupal</address>' => "Drupal\n",
  139. // @todo The <address> tag is currently not supported.
  140. '<address>Drupal</address><address>Drupal</address>' => "DrupalDrupal\n",
  141. '<b>Drupal</b>' => "*Drupal*\n",
  142. // @todo There should be a space between the '>' and the text.
  143. '<blockquote>Drupal</blockquote>' => ">Drupal\n",
  144. '<blockquote>Drupal</blockquote><blockquote>Drupal</blockquote>' => ">Drupal\n>Drupal\n",
  145. '<br />Drupal<br />Drupal<br /><br />Drupal' => "Drupal\nDrupal\nDrupal\n",
  146. '<br/>Drupal<br/>Drupal<br/><br/>Drupal' => "Drupal\nDrupal\nDrupal\n",
  147. // @todo There should be two line breaks before the paragraph.
  148. '<br/>Drupal<br/>Drupal<br/><br/>Drupal<p>Drupal</p>' => "Drupal\nDrupal\nDrupal\nDrupal\n\n",
  149. '<div>Drupal</div>' => "Drupal\n",
  150. // @todo The <div> tag is currently not supported.
  151. '<div>Drupal</div><div>Drupal</div>' => "DrupalDrupal\n",
  152. '<em>Drupal</em>' => "/Drupal/\n",
  153. '<h1>Drupal</h1>' => "======== DRUPAL ==============================================================\n\n",
  154. '<h1>Drupal</h1><p>Drupal</p>' => "======== DRUPAL ==============================================================\n\nDrupal\n\n",
  155. '<h2>Drupal</h2>' => "-------- DRUPAL --------------------------------------------------------------\n\n",
  156. '<h2>Drupal</h2><p>Drupal</p>' => "-------- DRUPAL --------------------------------------------------------------\n\nDrupal\n\n",
  157. '<h3>Drupal</h3>' => ".... Drupal\n\n",
  158. '<h3>Drupal</h3><p>Drupal</p>' => ".... Drupal\n\nDrupal\n\n",
  159. '<h4>Drupal</h4>' => ".. Drupal\n\n",
  160. '<h4>Drupal</h4><p>Drupal</p>' => ".. Drupal\n\nDrupal\n\n",
  161. '<h5>Drupal</h5>' => "Drupal\n\n",
  162. '<h5>Drupal</h5><p>Drupal</p>' => "Drupal\n\nDrupal\n\n",
  163. '<h6>Drupal</h6>' => "Drupal\n\n",
  164. '<h6>Drupal</h6><p>Drupal</p>' => "Drupal\n\nDrupal\n\n",
  165. '<hr />Drupal<hr />' => "------------------------------------------------------------------------------\nDrupal\n------------------------------------------------------------------------------\n",
  166. '<hr/>Drupal<hr/>' => "------------------------------------------------------------------------------\nDrupal\n------------------------------------------------------------------------------\n",
  167. '<hr/>Drupal<hr/><p>Drupal</p>' => "------------------------------------------------------------------------------\nDrupal\n------------------------------------------------------------------------------\nDrupal\n\n",
  168. '<i>Drupal</i>' => "/Drupal/\n",
  169. '<p>Drupal</p>' => "Drupal\n\n",
  170. '<p>Drupal</p><p>Drupal</p>' => "Drupal\n\nDrupal\n\n",
  171. '<strong>Drupal</strong>' => "*Drupal*\n",
  172. // @todo Tables are currently not supported.
  173. '<table><tr><td>Drupal</td><td>Drupal</td></tr><tr><td>Drupal</td><td>Drupal</td></tr></table>' => "DrupalDrupalDrupalDrupal\n",
  174. '<table><tr><td>Drupal</td></tr></table><p>Drupal</p>' => "Drupal\nDrupal\n\n",
  175. // @todo The <u> tag is currently not supported.
  176. '<u>Drupal</u>' => "Drupal\n",
  177. '<ul><li>Drupal</li></ul>' => " * Drupal\n\n",
  178. '<ul><li>Drupal <em>Drupal</em> Drupal</li></ul>' => " * Drupal /Drupal/ Drupal\n\n",
  179. // @todo Lines containing nothing but spaces should be trimmed.
  180. '<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",
  181. '<ul><li>Drupal</li><li><ol><li>Drupal</li></ol></li><li>Drupal</li></ul>' => " * Drupal\n * 1) Drupal\n \n * Drupal\n\n",
  182. '<ul><li>Drupal</li><li>Drupal</li></ul>' => " * Drupal\n * Drupal\n\n",
  183. '<ul><li>Drupal</li></ul><p>Drupal</p>' => " * Drupal\n\nDrupal\n\n",
  184. '<ol><li>Drupal</li></ol>' => " 1) Drupal\n\n",
  185. '<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",
  186. '<ol><li>Drupal</li><li>Drupal</li></ol>' => " 1) Drupal\n 2) Drupal\n\n",
  187. '<ol>Drupal</ol>' => "Drupal\n\n",
  188. '<ol><li>Drupal</li></ol><p>Drupal</p>' => " 1) Drupal\n\nDrupal\n\n",
  189. '<dl><dt>Drupal</dt></dl>' => "Drupal\n\n",
  190. '<dl><dt>Drupal</dt><dd>Drupal</dd></dl>' => "Drupal\n Drupal\n\n",
  191. '<dl><dt>Drupal</dt><dd>Drupal</dd><dt>Drupal</dt><dd>Drupal</dd></dl>' => "Drupal\n Drupal\nDrupal\n Drupal\n\n",
  192. '<dl><dt>Drupal</dt><dd>Drupal</dd></dl><p>Drupal</p>' => "Drupal\n Drupal\n\nDrupal\n\n",
  193. '<dl><dt>Drupal<dd>Drupal</dl>' => "Drupal\n Drupal\n\n",
  194. '<dl><dt>Drupal</dt></dl><p>Drupal</p>' => "Drupal\n\nDrupal\n\n",
  195. // @todo Again, lines containing only spaces should be trimmed.
  196. '<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",
  197. // Tests malformed HTML tags.
  198. '<br>Drupal<br>Drupal' => "Drupal\nDrupal\n",
  199. '<hr>Drupal<hr>Drupal' => "------------------------------------------------------------------------------\nDrupal\n------------------------------------------------------------------------------\nDrupal\n",
  200. '<ol><li>Drupal<li>Drupal</ol>' => " 1) Drupal\n 2) Drupal\n\n",
  201. '<ul><li>Drupal <em>Drupal</em> Drupal</ul></ul>' => " * Drupal /Drupal/ Drupal\n\n",
  202. '<ul><li>Drupal<li>Drupal</ol>' => " * Drupal\n * Drupal\n\n",
  203. '<ul><li>Drupal<li>Drupal</ul>' => " * Drupal\n * Drupal\n\n",
  204. '<ul>Drupal</ul>' => "Drupal\n\n",
  205. 'Drupal</ul></ol></dl><li>Drupal' => "Drupal\n * Drupal\n",
  206. '<dl>Drupal</dl>' => "Drupal\n\n",
  207. '<dl>Drupal</dl><p>Drupal</p>' => "Drupal\n\nDrupal\n\n",
  208. '<dt>Drupal</dt>' => "Drupal\n",
  209. // Tests some unsupported HTML tags.
  210. '<html>Drupal</html>' => "Drupal\n",
  211. // @todo Perhaps the contents of <script> tags should be dropped.
  212. '<script type="text/javascript">Drupal</script>' => "Drupal\n",
  213. );
  214. foreach ($tests as $html => $text) {
  215. $this->assertHtmlToText($html, $text, 'Supported tags');
  216. }
  217. }
  218. /**
  219. * Test $allowed_tags argument of drupal_html_to_text().
  220. */
  221. function testDrupalHtmlToTextArgs() {
  222. // The second parameter of drupal_html_to_text() overrules the allowed tags.
  223. $this->assertHtmlToText(
  224. 'Drupal <b>Drupal</b> Drupal',
  225. "Drupal *Drupal* Drupal\n",
  226. 'Allowed <b> tag found',
  227. array('b')
  228. );
  229. $this->assertHtmlToText(
  230. 'Drupal <h1>Drupal</h1> Drupal',
  231. "Drupal Drupal Drupal\n",
  232. 'Disallowed <h1> tag not found',
  233. array('b')
  234. );
  235. $this->assertHtmlToText(
  236. 'Drupal <p><em><b>Drupal</b></em><p> Drupal',
  237. "Drupal Drupal Drupal\n",
  238. 'Disallowed <p>, <em>, and <b> tags not found',
  239. array('a', 'br', 'h1')
  240. );
  241. $this->assertHtmlToText(
  242. '<html><body>Drupal</body></html>',
  243. "Drupal\n",
  244. 'Unsupported <html> and <body> tags not found',
  245. array('html', 'body')
  246. );
  247. }
  248. /**
  249. * Tests that drupal_wrap_mail() removes trailing whitespace before newlines.
  250. */
  251. function testDrupalHtmltoTextRemoveTrailingWhitespace() {
  252. $text = "Hi there! \nHerp Derp";
  253. $mail_lines = explode("\n", drupal_wrap_mail($text));
  254. $this->assertNotEqual(" ", substr($mail_lines[0], -1), 'Trailing whitespace removed.');
  255. }
  256. /**
  257. * Tests drupal_wrap_mail() retains whitespace from Usenet style signatures.
  258. *
  259. * RFC 3676 says, "This is a special case; an (optionally quoted or quoted and
  260. * stuffed) line consisting of DASH DASH SP is neither fixed nor flowed."
  261. */
  262. function testDrupalHtmltoTextUsenetSignature() {
  263. $text = "Hi there!\n-- \nHerp Derp";
  264. $mail_lines = explode("\n", drupal_wrap_mail($text));
  265. $this->assertEqual("-- ", $mail_lines[1], 'Trailing whitespace not removed for dash-dash-space signatures.');
  266. $text = "Hi there!\n-- \nHerp Derp";
  267. $mail_lines = explode("\n", drupal_wrap_mail($text));
  268. $this->assertEqual("--", $mail_lines[1], 'Trailing whitespace removed for incorrect dash-dash-space signatures.');
  269. }
  270. /**
  271. * Test that whitespace is collapsed.
  272. */
  273. function testDrupalHtmltoTextCollapsesWhitespace() {
  274. $input = "<p>Drupal Drupal\n\nDrupal<pre>Drupal Drupal\n\nDrupal</pre>Drupal Drupal\n\nDrupal</p>";
  275. // @todo The whitespace should be collapsed.
  276. $collapsed = "Drupal Drupal\n\nDrupalDrupal Drupal\n\nDrupalDrupal Drupal\n\nDrupal\n\n";
  277. $this->assertHtmlToText(
  278. $input,
  279. $collapsed,
  280. 'Whitespace is collapsed',
  281. array('p')
  282. );
  283. }
  284. /**
  285. * Test that text separated by block-level tags in HTML get separated by
  286. * (at least) a newline in the plaintext version.
  287. */
  288. function testDrupalHtmlToTextBlockTagToNewline() {
  289. $input = '[text]'
  290. . '<blockquote>[blockquote]</blockquote>'
  291. . '<br />[br]'
  292. . '<dl><dt>[dl-dt]</dt>'
  293. . '<dt>[dt]</dt>'
  294. . '<dd>[dd]</dd>'
  295. . '<dd>[dd-dl]</dd></dl>'
  296. . '<h1>[h1]</h1>'
  297. . '<h2>[h2]</h2>'
  298. . '<h3>[h3]</h3>'
  299. . '<h4>[h4]</h4>'
  300. . '<h5>[h5]</h5>'
  301. . '<h6>[h6]</h6>'
  302. . '<hr />[hr]'
  303. . '<ol><li>[ol-li]</li>'
  304. . '<li>[li]</li>'
  305. . '<li>[li-ol]</li></ol>'
  306. . '<p>[p]</p>'
  307. . '<ul><li>[ul-li]</li>'
  308. . '<li>[li-ul]</li></ul>'
  309. . '[text]';
  310. $output = drupal_html_to_text($input);
  311. $pass = $this->assertFalse(
  312. preg_match('/\][^\n]*\[/s', $output),
  313. 'Block-level HTML tags should force newlines'
  314. );
  315. if (!$pass) {
  316. $this->verbose($this->stringToHtml($output));
  317. }
  318. $output_upper = drupal_strtoupper($output);
  319. $upper_input = drupal_strtoupper($input);
  320. $upper_output = drupal_html_to_text($upper_input);
  321. $pass = $this->assertEqual(
  322. $upper_output,
  323. $output_upper,
  324. 'Tag recognition should be case-insensitive'
  325. );
  326. if (!$pass) {
  327. $this->verbose(
  328. $upper_output
  329. . '<br />should be equal to <br />'
  330. . $output_upper
  331. );
  332. }
  333. }
  334. /**
  335. * Test that headers are properly separated from surrounding text.
  336. */
  337. function testHeaderSeparation() {
  338. $html = 'Drupal<h1>Drupal</h1>Drupal';
  339. // @todo There should be more space above the header than below it.
  340. $text = "Drupal\n======== DRUPAL ==============================================================\n\nDrupal\n";
  341. $this->assertHtmlToText($html, $text,
  342. 'Text before and after <h1> tag');
  343. $html = '<p>Drupal</p><h1>Drupal</h1>Drupal';
  344. // @todo There should be more space above the header than below it.
  345. $text = "Drupal\n\n======== DRUPAL ==============================================================\n\nDrupal\n";
  346. $this->assertHtmlToText($html, $text,
  347. 'Paragraph before and text after <h1> tag');
  348. $html = 'Drupal<h1>Drupal</h1><p>Drupal</p>';
  349. // @todo There should be more space above the header than below it.
  350. $text = "Drupal\n======== DRUPAL ==============================================================\n\nDrupal\n\n";
  351. $this->assertHtmlToText($html, $text,
  352. 'Text before and paragraph after <h1> tag');
  353. $html = '<p>Drupal</p><h1>Drupal</h1><p>Drupal</p>';
  354. $text = "Drupal\n\n======== DRUPAL ==============================================================\n\nDrupal\n\n";
  355. $this->assertHtmlToText($html, $text,
  356. 'Paragraph before and after <h1> tag');
  357. }
  358. /**
  359. * Test that footnote references are properly generated.
  360. */
  361. function testFootnoteReferences() {
  362. global $base_path, $base_url;
  363. $source = '<a href="http://www.example.com/node/1">Host and path</a>'
  364. . '<br /><a href="http://www.example.com">Host, no path</a>'
  365. . '<br /><a href="' . $base_path . 'node/1">Path, no host</a>'
  366. . '<br /><a href="node/1">Relative path</a>';
  367. // @todo Footnote URLs should be absolute.
  368. $tt = "Host and path [1]"
  369. . "\nHost, no path [2]"
  370. // @todo The following two references should be combined.
  371. . "\nPath, no host [3]"
  372. . "\nRelative path [4]"
  373. . "\n"
  374. . "\n[1] http://www.example.com/node/1"
  375. . "\n[2] http://www.example.com"
  376. // @todo The following two references should be combined.
  377. . "\n[3] $base_url/node/1"
  378. . "\n[4] node/1\n";
  379. $this->assertHtmlToText($source, $tt, 'Footnotes');
  380. }
  381. /**
  382. * Test that combinations of paragraph breaks, line breaks, linefeeds,
  383. * and spaces are properly handled.
  384. */
  385. function testDrupalHtmlToTextParagraphs() {
  386. $tests = array();
  387. $tests[] = array(
  388. 'html' => "<p>line 1<br />\nline 2<br />line 3\n<br />line 4</p><p>paragraph</p>",
  389. // @todo Trailing line breaks should be trimmed.
  390. 'text' => "line 1\nline 2\nline 3\nline 4\n\nparagraph\n\n",
  391. );
  392. $tests[] = array(
  393. 'html' => "<p>line 1<br /> line 2</p> <p>line 4<br /> line 5</p> <p>0</p>",
  394. // @todo Trailing line breaks should be trimmed.
  395. 'text' => "line 1\nline 2\n\nline 4\nline 5\n\n0\n\n",
  396. );
  397. foreach ($tests as $test) {
  398. $this->assertHtmlToText($test['html'], $test['text'], 'Paragraph breaks');
  399. }
  400. }
  401. /**
  402. * Tests that drupal_html_to_text() wraps before 1000 characters.
  403. *
  404. * RFC 3676 says, "The Text/Plain media type is the lowest common
  405. * denominator of Internet email, with lines of no more than 998 characters."
  406. *
  407. * RFC 2046 says, "SMTP [RFC-821] allows a maximum of 998 octets before the
  408. * next CRLF sequence."
  409. *
  410. * RFC 821 says, "The maximum total length of a text line including the
  411. * <CRLF> is 1000 characters."
  412. */
  413. function testVeryLongLineWrap() {
  414. $input = 'Drupal<br /><p>' . str_repeat('x', 2100) . '</><br />Drupal';
  415. $output = drupal_html_to_text($input);
  416. // This awkward construct comes from includes/mail.inc lines 8-13.
  417. $eol = variable_get('mail_line_endings', MAIL_LINE_ENDINGS);
  418. // We must use strlen() rather than drupal_strlen() in order to count
  419. // octets rather than characters.
  420. $line_length_limit = 1000 - drupal_strlen($eol);
  421. $maximum_line_length = 0;
  422. foreach (explode($eol, $output) as $line) {
  423. // We must use strlen() rather than drupal_strlen() in order to count
  424. // octets rather than characters.
  425. $maximum_line_length = max($maximum_line_length, strlen($line . $eol));
  426. }
  427. $verbose = 'Maximum line length found was ' . $maximum_line_length . ' octets.';
  428. // @todo This should assert that $maximum_line_length <= 1000.
  429. $this->pass($verbose);
  430. }
  431. }