mail.test 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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. * Checks for the site name in an auto-generated From: header.
  52. */
  53. function testFromHeader() {
  54. global $language;
  55. $default_from = variable_get('site_mail', ini_get('sendmail_from'));
  56. $site_name = variable_get('site_name', 'Drupal');
  57. // Reset the class variable holding a copy of the last sent message.
  58. self::$sent_message = NULL;
  59. // Send an e-mail with a sender address specified.
  60. $from_email = 'someone_else@example.com';
  61. $message = drupal_mail('simpletest', 'from_test', 'from_test@example.com', $language, array(), $from_email);
  62. // Test that the from e-mail is just the e-mail and not the site name and
  63. // default sender e-mail.
  64. $this->assertEqual($from_email, self::$sent_message['headers']['From']);
  65. // Check default behavior is only email in FROM header.
  66. self::$sent_message = NULL;
  67. // Send an e-mail and check that the From-header contains only default mail address.
  68. variable_del('mail_display_name_site_name');
  69. $message = drupal_mail('simpletest', 'from_test', 'from_test@example.com', $language);
  70. $this->assertEqual($default_from, self::$sent_message['headers']['From']);
  71. self::$sent_message = NULL;
  72. // Send an e-mail and check that the From-header contains the site name.
  73. variable_set('mail_display_name_site_name', TRUE);
  74. $message = drupal_mail('simpletest', 'from_test', 'from_test@example.com', $language);
  75. $this->assertEqual($site_name . ' <' . $default_from . '>', self::$sent_message['headers']['From']);
  76. }
  77. /**
  78. * Checks for the site name in an auto-generated From: header.
  79. */
  80. function testFromHeaderRfc2822Compliant() {
  81. global $language;
  82. $default_from = variable_get('site_mail', ini_get('sendmail_from'));
  83. // Enable adding a site name to From.
  84. variable_set('mail_display_name_site_name', TRUE);
  85. $site_names = array(
  86. // Simple ASCII characters.
  87. 'Test site' => 'Test site',
  88. // ASCII with html entity.
  89. 'Test &amp; site' => 'Test & site',
  90. // Non-ASCII characters.
  91. 'Tést site' => '=?UTF-8?B?VMOpc3Qgc2l0ZQ==?=',
  92. // Non-ASCII with special characters.
  93. 'Tést; site' => '=?UTF-8?B?VMOpc3Q7IHNpdGU=?=',
  94. // Non-ASCII with html entity.
  95. 'T&eacute;st; site' => '=?UTF-8?B?VMOpc3Q7IHNpdGU=?=',
  96. // ASCII with special characters.
  97. 'Test; site' => '"Test; site"',
  98. // ASCII with special characters as html entity.
  99. 'Test &lt; site' => '"Test < site"',
  100. // ASCII with special characters and '\'.
  101. 'Test; \ "site"' => '"Test; \\\\ \"site\""',
  102. // String already RFC-2822 compliant.
  103. '"Test; site"' => '"Test; site"',
  104. // String already RFC-2822 compliant.
  105. '"Test; \\\\ \"site\""' => '"Test; \\\\ \"site\""',
  106. );
  107. foreach ($site_names as $original_name => $safe_string) {
  108. variable_set('site_name', $original_name);
  109. // Reset the class variable holding a copy of the last sent message.
  110. self::$sent_message = NULL;
  111. // Send an e-mail and check that the From-header contains is RFC-2822 compliant.
  112. drupal_mail('simpletest', 'from_test', 'from_test@example.com', $language);
  113. $this->assertEqual($safe_string . ' <' . $default_from . '>', self::$sent_message['headers']['From']);
  114. }
  115. }
  116. /**
  117. * Concatenate and wrap the e-mail body for plain-text mails.
  118. *
  119. * @see DefaultMailSystem
  120. */
  121. public function format(array $message) {
  122. // Join the body array into one string.
  123. $message['body'] = implode("\n\n", $message['body']);
  124. // Convert any HTML to plain-text.
  125. $message['body'] = drupal_html_to_text($message['body']);
  126. // Wrap the mail body for sending.
  127. $message['body'] = drupal_wrap_mail($message['body']);
  128. return $message;
  129. }
  130. /**
  131. * Send function that is called through the mail system.
  132. */
  133. public function mail(array $message) {
  134. self::$sent_message = $message;
  135. }
  136. }
  137. /**
  138. * Unit tests for drupal_html_to_text().
  139. */
  140. class DrupalHtmlToTextTestCase extends DrupalWebTestCase {
  141. public static function getInfo() {
  142. return array(
  143. 'name' => 'HTML to text conversion',
  144. 'description' => 'Tests drupal_html_to_text().',
  145. 'group' => 'Mail',
  146. );
  147. }
  148. /**
  149. * Converts a string to its PHP source equivalent for display in test messages.
  150. *
  151. * @param $text
  152. * The text string to convert.
  153. *
  154. * @return
  155. * An HTML representation of the text string that, when displayed in a
  156. * browser, represents the PHP source code equivalent of $text.
  157. */
  158. function stringToHtml($text) {
  159. return '"' .
  160. str_replace(
  161. array("\n", ' '),
  162. array('\n', '&nbsp;'),
  163. check_plain($text)
  164. ) . '"';
  165. }
  166. /**
  167. * Helper function for testing drupal_html_to_text().
  168. *
  169. * @param $html
  170. * The source HTML string to be converted.
  171. * @param $text
  172. * The expected result of converting $html to text.
  173. * @param $message
  174. * A text message to display in the assertion message.
  175. * @param $allowed_tags
  176. * (optional) An array of allowed tags, or NULL to default to the full
  177. * set of tags supported by drupal_html_to_text().
  178. */
  179. function assertHtmlToText($html, $text, $message, $allowed_tags = NULL) {
  180. preg_match_all('/<([a-z0-6]+)/', drupal_strtolower($html), $matches);
  181. $tested_tags = implode(', ', array_unique($matches[1]));
  182. $message .= ' (' . $tested_tags . ')';
  183. $result = drupal_html_to_text($html, $allowed_tags);
  184. $pass = $this->assertEqual($result, $text, check_plain($message));
  185. $verbose = 'html = <pre>' . $this->stringToHtml($html)
  186. . '</pre><br />' . 'result = <pre>' . $this->stringToHtml($result)
  187. . '</pre><br />' . 'expected = <pre>' . $this->stringToHtml($text)
  188. . '</pre>';
  189. $this->verbose($verbose);
  190. if (!$pass) {
  191. $this->pass("Previous test verbose info:<br />$verbose");
  192. }
  193. }
  194. /**
  195. * Test all supported tags of drupal_html_to_text().
  196. */
  197. function testTags() {
  198. global $base_path, $base_url;
  199. $tests = array(
  200. // @todo Trailing linefeeds should be trimmed.
  201. '<a href = "http://drupal.org">Drupal.org</a>' => "Drupal.org [1]\n\n[1] http://drupal.org\n",
  202. // @todo Footer URLs should be absolute.
  203. "<a href = \"$base_path\">Homepage</a>" => "Homepage [1]\n\n[1] $base_url/\n",
  204. '<address>Drupal</address>' => "Drupal\n",
  205. // @todo The <address> tag is currently not supported.
  206. '<address>Drupal</address><address>Drupal</address>' => "DrupalDrupal\n",
  207. '<b>Drupal</b>' => "*Drupal*\n",
  208. // @todo There should be a space between the '>' and the text.
  209. '<blockquote>Drupal</blockquote>' => ">Drupal\n",
  210. '<blockquote>Drupal</blockquote><blockquote>Drupal</blockquote>' => ">Drupal\n>Drupal\n",
  211. '<br />Drupal<br />Drupal<br /><br />Drupal' => "Drupal\nDrupal\nDrupal\n",
  212. '<br/>Drupal<br/>Drupal<br/><br/>Drupal' => "Drupal\nDrupal\nDrupal\n",
  213. // @todo There should be two line breaks before the paragraph.
  214. '<br/>Drupal<br/>Drupal<br/><br/>Drupal<p>Drupal</p>' => "Drupal\nDrupal\nDrupal\nDrupal\n\n",
  215. '<div>Drupal</div>' => "Drupal\n",
  216. // @todo The <div> tag is currently not supported.
  217. '<div>Drupal</div><div>Drupal</div>' => "DrupalDrupal\n",
  218. '<em>Drupal</em>' => "/Drupal/\n",
  219. '<h1>Drupal</h1>' => "======== DRUPAL ==============================================================\n\n",
  220. '<h1>Drupal</h1><p>Drupal</p>' => "======== DRUPAL ==============================================================\n\nDrupal\n\n",
  221. '<h2>Drupal</h2>' => "-------- DRUPAL --------------------------------------------------------------\n\n",
  222. '<h2>Drupal</h2><p>Drupal</p>' => "-------- DRUPAL --------------------------------------------------------------\n\nDrupal\n\n",
  223. '<h3>Drupal</h3>' => ".... Drupal\n\n",
  224. '<h3>Drupal</h3><p>Drupal</p>' => ".... Drupal\n\nDrupal\n\n",
  225. '<h4>Drupal</h4>' => ".. Drupal\n\n",
  226. '<h4>Drupal</h4><p>Drupal</p>' => ".. Drupal\n\nDrupal\n\n",
  227. '<h5>Drupal</h5>' => "Drupal\n\n",
  228. '<h5>Drupal</h5><p>Drupal</p>' => "Drupal\n\nDrupal\n\n",
  229. '<h6>Drupal</h6>' => "Drupal\n\n",
  230. '<h6>Drupal</h6><p>Drupal</p>' => "Drupal\n\nDrupal\n\n",
  231. '<hr />Drupal<hr />' => "------------------------------------------------------------------------------\nDrupal\n------------------------------------------------------------------------------\n",
  232. '<hr/>Drupal<hr/>' => "------------------------------------------------------------------------------\nDrupal\n------------------------------------------------------------------------------\n",
  233. '<hr/>Drupal<hr/><p>Drupal</p>' => "------------------------------------------------------------------------------\nDrupal\n------------------------------------------------------------------------------\nDrupal\n\n",
  234. '<i>Drupal</i>' => "/Drupal/\n",
  235. '<p>Drupal</p>' => "Drupal\n\n",
  236. '<p>Drupal</p><p>Drupal</p>' => "Drupal\n\nDrupal\n\n",
  237. '<strong>Drupal</strong>' => "*Drupal*\n",
  238. // @todo Tables are currently not supported.
  239. '<table><tr><td>Drupal</td><td>Drupal</td></tr><tr><td>Drupal</td><td>Drupal</td></tr></table>' => "DrupalDrupalDrupalDrupal\n",
  240. '<table><tr><td>Drupal</td></tr></table><p>Drupal</p>' => "Drupal\nDrupal\n\n",
  241. // @todo The <u> tag is currently not supported.
  242. '<u>Drupal</u>' => "Drupal\n",
  243. '<ul><li>Drupal</li></ul>' => " * Drupal\n\n",
  244. '<ul><li>Drupal <em>Drupal</em> Drupal</li></ul>' => " * Drupal /Drupal/ Drupal\n\n",
  245. // @todo Lines containing nothing but spaces should be trimmed.
  246. '<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",
  247. '<ul><li>Drupal</li><li><ol><li>Drupal</li></ol></li><li>Drupal</li></ul>' => " * Drupal\n * 1) Drupal\n \n * Drupal\n\n",
  248. '<ul><li>Drupal</li><li>Drupal</li></ul>' => " * Drupal\n * Drupal\n\n",
  249. '<ul><li>Drupal</li></ul><p>Drupal</p>' => " * Drupal\n\nDrupal\n\n",
  250. '<ol><li>Drupal</li></ol>' => " 1) Drupal\n\n",
  251. '<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",
  252. '<ol><li>Drupal</li><li>Drupal</li></ol>' => " 1) Drupal\n 2) Drupal\n\n",
  253. '<ol>Drupal</ol>' => "Drupal\n\n",
  254. '<ol><li>Drupal</li></ol><p>Drupal</p>' => " 1) Drupal\n\nDrupal\n\n",
  255. '<dl><dt>Drupal</dt></dl>' => "Drupal\n\n",
  256. '<dl><dt>Drupal</dt><dd>Drupal</dd></dl>' => "Drupal\n Drupal\n\n",
  257. '<dl><dt>Drupal</dt><dd>Drupal</dd><dt>Drupal</dt><dd>Drupal</dd></dl>' => "Drupal\n Drupal\nDrupal\n Drupal\n\n",
  258. '<dl><dt>Drupal</dt><dd>Drupal</dd></dl><p>Drupal</p>' => "Drupal\n Drupal\n\nDrupal\n\n",
  259. '<dl><dt>Drupal<dd>Drupal</dl>' => "Drupal\n Drupal\n\n",
  260. '<dl><dt>Drupal</dt></dl><p>Drupal</p>' => "Drupal\n\nDrupal\n\n",
  261. // @todo Again, lines containing only spaces should be trimmed.
  262. '<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",
  263. // Tests malformed HTML tags.
  264. '<br>Drupal<br>Drupal' => "Drupal\nDrupal\n",
  265. '<hr>Drupal<hr>Drupal' => "------------------------------------------------------------------------------\nDrupal\n------------------------------------------------------------------------------\nDrupal\n",
  266. '<ol><li>Drupal<li>Drupal</ol>' => " 1) Drupal\n 2) Drupal\n\n",
  267. '<ul><li>Drupal <em>Drupal</em> Drupal</ul></ul>' => " * Drupal /Drupal/ Drupal\n\n",
  268. '<ul><li>Drupal<li>Drupal</ol>' => " * Drupal\n * Drupal\n\n",
  269. '<ul><li>Drupal<li>Drupal</ul>' => " * Drupal\n * Drupal\n\n",
  270. '<ul>Drupal</ul>' => "Drupal\n\n",
  271. 'Drupal</ul></ol></dl><li>Drupal' => "Drupal\n * Drupal\n",
  272. '<dl>Drupal</dl>' => "Drupal\n\n",
  273. '<dl>Drupal</dl><p>Drupal</p>' => "Drupal\n\nDrupal\n\n",
  274. '<dt>Drupal</dt>' => "Drupal\n",
  275. // Tests some unsupported HTML tags.
  276. '<html>Drupal</html>' => "Drupal\n",
  277. // @todo Perhaps the contents of <script> tags should be dropped.
  278. '<script type="text/javascript">Drupal</script>' => "Drupal\n",
  279. );
  280. foreach ($tests as $html => $text) {
  281. $this->assertHtmlToText($html, $text, 'Supported tags');
  282. }
  283. }
  284. /**
  285. * Test $allowed_tags argument of drupal_html_to_text().
  286. */
  287. function testDrupalHtmlToTextArgs() {
  288. // The second parameter of drupal_html_to_text() overrules the allowed tags.
  289. $this->assertHtmlToText(
  290. 'Drupal <b>Drupal</b> Drupal',
  291. "Drupal *Drupal* Drupal\n",
  292. 'Allowed <b> tag found',
  293. array('b')
  294. );
  295. $this->assertHtmlToText(
  296. 'Drupal <h1>Drupal</h1> Drupal',
  297. "Drupal Drupal Drupal\n",
  298. 'Disallowed <h1> tag not found',
  299. array('b')
  300. );
  301. $this->assertHtmlToText(
  302. 'Drupal <p><em><b>Drupal</b></em><p> Drupal',
  303. "Drupal Drupal Drupal\n",
  304. 'Disallowed <p>, <em>, and <b> tags not found',
  305. array('a', 'br', 'h1')
  306. );
  307. $this->assertHtmlToText(
  308. '<html><body>Drupal</body></html>',
  309. "Drupal\n",
  310. 'Unsupported <html> and <body> tags not found',
  311. array('html', 'body')
  312. );
  313. }
  314. /**
  315. * Tests that drupal_wrap_mail() removes trailing whitespace before newlines.
  316. */
  317. function testDrupalHtmltoTextRemoveTrailingWhitespace() {
  318. $text = "Hi there! \nHerp Derp";
  319. $mail_lines = explode("\n", drupal_wrap_mail($text));
  320. $this->assertNotEqual(" ", substr($mail_lines[0], -1), 'Trailing whitespace removed.');
  321. }
  322. /**
  323. * Tests drupal_wrap_mail() retains whitespace from Usenet style signatures.
  324. *
  325. * RFC 3676 says, "This is a special case; an (optionally quoted or quoted and
  326. * stuffed) line consisting of DASH DASH SP is neither fixed nor flowed."
  327. */
  328. function testDrupalHtmltoTextUsenetSignature() {
  329. $text = "Hi there!\n-- \nHerp Derp";
  330. $mail_lines = explode("\n", drupal_wrap_mail($text));
  331. $this->assertEqual("-- ", $mail_lines[1], 'Trailing whitespace not removed for dash-dash-space signatures.');
  332. $text = "Hi there!\n-- \nHerp Derp";
  333. $mail_lines = explode("\n", drupal_wrap_mail($text));
  334. $this->assertEqual("--", $mail_lines[1], 'Trailing whitespace removed for incorrect dash-dash-space signatures.');
  335. }
  336. /**
  337. * Test that whitespace is collapsed.
  338. */
  339. function testDrupalHtmltoTextCollapsesWhitespace() {
  340. $input = "<p>Drupal Drupal\n\nDrupal<pre>Drupal Drupal\n\nDrupal</pre>Drupal Drupal\n\nDrupal</p>";
  341. // @todo The whitespace should be collapsed.
  342. $collapsed = "Drupal Drupal\n\nDrupalDrupal Drupal\n\nDrupalDrupal Drupal\n\nDrupal\n\n";
  343. $this->assertHtmlToText(
  344. $input,
  345. $collapsed,
  346. 'Whitespace is collapsed',
  347. array('p')
  348. );
  349. }
  350. /**
  351. * Test that text separated by block-level tags in HTML get separated by
  352. * (at least) a newline in the plaintext version.
  353. */
  354. function testDrupalHtmlToTextBlockTagToNewline() {
  355. $input = '[text]'
  356. . '<blockquote>[blockquote]</blockquote>'
  357. . '<br />[br]'
  358. . '<dl><dt>[dl-dt]</dt>'
  359. . '<dt>[dt]</dt>'
  360. . '<dd>[dd]</dd>'
  361. . '<dd>[dd-dl]</dd></dl>'
  362. . '<h1>[h1]</h1>'
  363. . '<h2>[h2]</h2>'
  364. . '<h3>[h3]</h3>'
  365. . '<h4>[h4]</h4>'
  366. . '<h5>[h5]</h5>'
  367. . '<h6>[h6]</h6>'
  368. . '<hr />[hr]'
  369. . '<ol><li>[ol-li]</li>'
  370. . '<li>[li]</li>'
  371. . '<li>[li-ol]</li></ol>'
  372. . '<p>[p]</p>'
  373. . '<ul><li>[ul-li]</li>'
  374. . '<li>[li-ul]</li></ul>'
  375. . '[text]';
  376. $output = drupal_html_to_text($input);
  377. $pass = $this->assertFalse(
  378. preg_match('/\][^\n]*\[/s', $output),
  379. 'Block-level HTML tags should force newlines'
  380. );
  381. if (!$pass) {
  382. $this->verbose($this->stringToHtml($output));
  383. }
  384. $output_upper = drupal_strtoupper($output);
  385. $upper_input = drupal_strtoupper($input);
  386. $upper_output = drupal_html_to_text($upper_input);
  387. $pass = $this->assertEqual(
  388. $upper_output,
  389. $output_upper,
  390. 'Tag recognition should be case-insensitive'
  391. );
  392. if (!$pass) {
  393. $this->verbose(
  394. $upper_output
  395. . '<br />should be equal to <br />'
  396. . $output_upper
  397. );
  398. }
  399. }
  400. /**
  401. * Test that headers are properly separated from surrounding text.
  402. */
  403. function testHeaderSeparation() {
  404. $html = 'Drupal<h1>Drupal</h1>Drupal';
  405. // @todo There should be more space above the header than below it.
  406. $text = "Drupal\n======== DRUPAL ==============================================================\n\nDrupal\n";
  407. $this->assertHtmlToText($html, $text,
  408. 'Text before and after <h1> tag');
  409. $html = '<p>Drupal</p><h1>Drupal</h1>Drupal';
  410. // @todo There should be more space above the header than below it.
  411. $text = "Drupal\n\n======== DRUPAL ==============================================================\n\nDrupal\n";
  412. $this->assertHtmlToText($html, $text,
  413. 'Paragraph before and text after <h1> tag');
  414. $html = 'Drupal<h1>Drupal</h1><p>Drupal</p>';
  415. // @todo There should be more space above the header than below it.
  416. $text = "Drupal\n======== DRUPAL ==============================================================\n\nDrupal\n\n";
  417. $this->assertHtmlToText($html, $text,
  418. 'Text before and paragraph after <h1> tag');
  419. $html = '<p>Drupal</p><h1>Drupal</h1><p>Drupal</p>';
  420. $text = "Drupal\n\n======== DRUPAL ==============================================================\n\nDrupal\n\n";
  421. $this->assertHtmlToText($html, $text,
  422. 'Paragraph before and after <h1> tag');
  423. }
  424. /**
  425. * Test that footnote references are properly generated.
  426. */
  427. function testFootnoteReferences() {
  428. global $base_path, $base_url;
  429. $source = '<a href="http://www.example.com/node/1">Host and path</a>'
  430. . '<br /><a href="http://www.example.com">Host, no path</a>'
  431. . '<br /><a href="' . $base_path . 'node/1">Path, no host</a>'
  432. . '<br /><a href="node/1">Relative path</a>';
  433. // @todo Footnote URLs should be absolute.
  434. $tt = "Host and path [1]"
  435. . "\nHost, no path [2]"
  436. // @todo The following two references should be combined.
  437. . "\nPath, no host [3]"
  438. . "\nRelative path [4]"
  439. . "\n"
  440. . "\n[1] http://www.example.com/node/1"
  441. . "\n[2] http://www.example.com"
  442. // @todo The following two references should be combined.
  443. . "\n[3] $base_url/node/1"
  444. . "\n[4] node/1\n";
  445. $this->assertHtmlToText($source, $tt, 'Footnotes');
  446. }
  447. /**
  448. * Test that combinations of paragraph breaks, line breaks, linefeeds,
  449. * and spaces are properly handled.
  450. */
  451. function testDrupalHtmlToTextParagraphs() {
  452. $tests = array();
  453. $tests[] = array(
  454. 'html' => "<p>line 1<br />\nline 2<br />line 3\n<br />line 4</p><p>paragraph</p>",
  455. // @todo Trailing line breaks should be trimmed.
  456. 'text' => "line 1\nline 2\nline 3\nline 4\n\nparagraph\n\n",
  457. );
  458. $tests[] = array(
  459. 'html' => "<p>line 1<br /> line 2</p> <p>line 4<br /> line 5</p> <p>0</p>",
  460. // @todo Trailing line breaks should be trimmed.
  461. 'text' => "line 1\nline 2\n\nline 4\nline 5\n\n0\n\n",
  462. );
  463. foreach ($tests as $test) {
  464. $this->assertHtmlToText($test['html'], $test['text'], 'Paragraph breaks');
  465. }
  466. }
  467. /**
  468. * Tests that drupal_html_to_text() wraps before 1000 characters.
  469. *
  470. * RFC 3676 says, "The Text/Plain media type is the lowest common
  471. * denominator of Internet email, with lines of no more than 998 characters."
  472. *
  473. * RFC 2046 says, "SMTP [RFC-821] allows a maximum of 998 octets before the
  474. * next CRLF sequence."
  475. *
  476. * RFC 821 says, "The maximum total length of a text line including the
  477. * <CRLF> is 1000 characters."
  478. */
  479. function testVeryLongLineWrap() {
  480. $input = 'Drupal<br /><p>' . str_repeat('x', 2100) . '</p><br />Drupal';
  481. $output = drupal_html_to_text($input);
  482. // This awkward construct comes from includes/mail.inc lines 8-13.
  483. $eol = variable_get('mail_line_endings', MAIL_LINE_ENDINGS);
  484. // We must use strlen() rather than drupal_strlen() in order to count
  485. // octets rather than characters.
  486. $line_length_limit = 1000 - drupal_strlen($eol);
  487. $maximum_line_length = 0;
  488. foreach (explode($eol, $output) as $line) {
  489. // We must use strlen() rather than drupal_strlen() in order to count
  490. // octets rather than characters.
  491. $maximum_line_length = max($maximum_line_length, strlen($line . $eol));
  492. }
  493. $verbose = 'Maximum line length found was ' . $maximum_line_length . ' octets.';
  494. $this->assertTrue($maximum_line_length <= 1000, $verbose);
  495. }
  496. }