mail.test 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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', t('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. * Test that whitespace is collapsed.
  250. */
  251. function testDrupalHtmltoTextCollapsesWhitespace() {
  252. $input = "<p>Drupal Drupal\n\nDrupal<pre>Drupal Drupal\n\nDrupal</pre>Drupal Drupal\n\nDrupal</p>";
  253. // @todo The whitespace should be collapsed.
  254. $collapsed = "Drupal Drupal\n\nDrupalDrupal Drupal\n\nDrupalDrupal Drupal\n\nDrupal\n\n";
  255. $this->assertHtmlToText(
  256. $input,
  257. $collapsed,
  258. 'Whitespace is collapsed',
  259. array('p')
  260. );
  261. }
  262. /**
  263. * Test that text separated by block-level tags in HTML get separated by
  264. * (at least) a newline in the plaintext version.
  265. */
  266. function testDrupalHtmlToTextBlockTagToNewline() {
  267. $input = '[text]'
  268. . '<blockquote>[blockquote]</blockquote>'
  269. . '<br />[br]'
  270. . '<dl><dt>[dl-dt]</dt>'
  271. . '<dt>[dt]</dt>'
  272. . '<dd>[dd]</dd>'
  273. . '<dd>[dd-dl]</dd></dl>'
  274. . '<h1>[h1]</h1>'
  275. . '<h2>[h2]</h2>'
  276. . '<h3>[h3]</h3>'
  277. . '<h4>[h4]</h4>'
  278. . '<h5>[h5]</h5>'
  279. . '<h6>[h6]</h6>'
  280. . '<hr />[hr]'
  281. . '<ol><li>[ol-li]</li>'
  282. . '<li>[li]</li>'
  283. . '<li>[li-ol]</li></ol>'
  284. . '<p>[p]</p>'
  285. . '<ul><li>[ul-li]</li>'
  286. . '<li>[li-ul]</li></ul>'
  287. . '[text]';
  288. $output = drupal_html_to_text($input);
  289. $pass = $this->assertFalse(
  290. preg_match('/\][^\n]*\[/s', $output),
  291. 'Block-level HTML tags should force newlines'
  292. );
  293. if (!$pass) {
  294. $this->verbose($this->stringToHtml($output));
  295. }
  296. $output_upper = drupal_strtoupper($output);
  297. $upper_input = drupal_strtoupper($input);
  298. $upper_output = drupal_html_to_text($upper_input);
  299. $pass = $this->assertEqual(
  300. $upper_output,
  301. $output_upper,
  302. 'Tag recognition should be case-insensitive'
  303. );
  304. if (!$pass) {
  305. $this->verbose(
  306. $upper_output
  307. . '<br />should be equal to <br />'
  308. . $output_upper
  309. );
  310. }
  311. }
  312. /**
  313. * Test that headers are properly separated from surrounding text.
  314. */
  315. function testHeaderSeparation() {
  316. $html = 'Drupal<h1>Drupal</h1>Drupal';
  317. // @todo There should be more space above the header than below it.
  318. $text = "Drupal\n======== DRUPAL ==============================================================\n\nDrupal\n";
  319. $this->assertHtmlToText($html, $text,
  320. 'Text before and after <h1> tag');
  321. $html = '<p>Drupal</p><h1>Drupal</h1>Drupal';
  322. // @todo There should be more space above the header than below it.
  323. $text = "Drupal\n\n======== DRUPAL ==============================================================\n\nDrupal\n";
  324. $this->assertHtmlToText($html, $text,
  325. 'Paragraph before and text after <h1> tag');
  326. $html = 'Drupal<h1>Drupal</h1><p>Drupal</p>';
  327. // @todo There should be more space above the header than below it.
  328. $text = "Drupal\n======== DRUPAL ==============================================================\n\nDrupal\n\n";
  329. $this->assertHtmlToText($html, $text,
  330. 'Text before and paragraph after <h1> tag');
  331. $html = '<p>Drupal</p><h1>Drupal</h1><p>Drupal</p>';
  332. $text = "Drupal\n\n======== DRUPAL ==============================================================\n\nDrupal\n\n";
  333. $this->assertHtmlToText($html, $text,
  334. 'Paragraph before and after <h1> tag');
  335. }
  336. /**
  337. * Test that footnote references are properly generated.
  338. */
  339. function testFootnoteReferences() {
  340. global $base_path, $base_url;
  341. $source = '<a href="http://www.example.com/node/1">Host and path</a>'
  342. . '<br /><a href="http://www.example.com">Host, no path</a>'
  343. . '<br /><a href="' . $base_path . 'node/1">Path, no host</a>'
  344. . '<br /><a href="node/1">Relative path</a>';
  345. // @todo Footnote URLs should be absolute.
  346. $tt = "Host and path [1]"
  347. . "\nHost, no path [2]"
  348. // @todo The following two references should be combined.
  349. . "\nPath, no host [3]"
  350. . "\nRelative path [4]"
  351. . "\n"
  352. . "\n[1] http://www.example.com/node/1"
  353. . "\n[2] http://www.example.com"
  354. // @todo The following two references should be combined.
  355. . "\n[3] $base_url/node/1"
  356. . "\n[4] node/1\n";
  357. $this->assertHtmlToText($source, $tt, 'Footnotes');
  358. }
  359. /**
  360. * Test that combinations of paragraph breaks, line breaks, linefeeds,
  361. * and spaces are properly handled.
  362. */
  363. function testDrupalHtmlToTextParagraphs() {
  364. $tests = array();
  365. $tests[] = array(
  366. 'html' => "<p>line 1<br />\nline 2<br />line 3\n<br />line 4</p><p>paragraph</p>",
  367. // @todo Trailing line breaks should be trimmed.
  368. 'text' => "line 1\nline 2\nline 3\nline 4\n\nparagraph\n\n",
  369. );
  370. $tests[] = array(
  371. 'html' => "<p>line 1<br /> line 2</p> <p>line 4<br /> line 5</p> <p>0</p>",
  372. // @todo Trailing line breaks should be trimmed.
  373. 'text' => "line 1\nline 2\n\nline 4\nline 5\n\n0\n\n",
  374. );
  375. foreach ($tests as $test) {
  376. $this->assertHtmlToText($test['html'], $test['text'], 'Paragraph breaks');
  377. }
  378. }
  379. /**
  380. * Tests that drupal_html_to_text() wraps before 1000 characters.
  381. *
  382. * RFC 3676 says, "The Text/Plain media type is the lowest common
  383. * denominator of Internet email, with lines of no more than 998 characters."
  384. *
  385. * RFC 2046 says, "SMTP [RFC-821] allows a maximum of 998 octets before the
  386. * next CRLF sequence."
  387. *
  388. * RFC 821 says, "The maximum total length of a text line including the
  389. * <CRLF> is 1000 characters."
  390. */
  391. function testVeryLongLineWrap() {
  392. $input = 'Drupal<br /><p>' . str_repeat('x', 2100) . '</><br />Drupal';
  393. $output = drupal_html_to_text($input);
  394. // This awkward construct comes from includes/mail.inc lines 8-13.
  395. $eol = variable_get('mail_line_endings', MAIL_LINE_ENDINGS);
  396. // We must use strlen() rather than drupal_strlen() in order to count
  397. // octets rather than characters.
  398. $line_length_limit = 1000 - drupal_strlen($eol);
  399. $maximum_line_length = 0;
  400. foreach (explode($eol, $output) as $line) {
  401. // We must use strlen() rather than drupal_strlen() in order to count
  402. // octets rather than characters.
  403. $maximum_line_length = max($maximum_line_length, strlen($line . $eol));
  404. }
  405. $verbose = 'Maximum line length found was ' . $maximum_line_length . ' octets.';
  406. // @todo This should assert that $maximum_line_length <= 1000.
  407. $this->pass($verbose);
  408. }
  409. }