Projet

Général

Profil

Paste
Télécharger (22 ko) Statistiques
| Branche: | Révision:

root / drupal7 / modules / simpletest / tests / mail.test @ 01dfd3b5

1
<?php
2

    
3
/**
4
 * @file
5
 * Test the Drupal mailing system.
6
 */
7
class MailTestCase extends DrupalWebTestCase implements MailSystemInterface {
8
  /**
9
   * The most recent message that was sent through the test case.
10
   *
11
   * We take advantage here of the fact that static variables are shared among
12
   * all instance of the same class.
13
   */
14
  private static $sent_message;
15

    
16
  public static function getInfo() {
17
    return array(
18
      'name' => 'Mail system',
19
      'description' => 'Performs tests on the pluggable mailing framework.',
20
      'group' => 'System',
21
    );
22
  }
23

    
24
  function setUp() {
25
    parent::setUp(array('simpletest'));
26

    
27
    // Set MailTestCase (i.e. this class) as the SMTP library
28
    variable_set('mail_system', array('default-system' => 'MailTestCase'));
29
  }
30

    
31
  /**
32
   * Assert that the pluggable mail system is functional.
33
   */
34
  function testPluggableFramework() {
35
    global $language;
36

    
37
    // Use MailTestCase for sending a message.
38
    $message = drupal_mail('simpletest', 'mail_test', 'testing@example.com', $language);
39

    
40
    // Assert whether the message was sent through the send function.
41
    $this->assertEqual(self::$sent_message['to'], 'testing@example.com', 'Pluggable mail system is extendable.');
42
  }
43

    
44
  /**
45
   * Test that message sending may be canceled.
46
   *
47
   * @see simpletest_mail_alter()
48
   */
49
  function testCancelMessage() {
50
    global $language;
51

    
52
    // Reset the class variable holding a copy of the last sent message.
53
    self::$sent_message = NULL;
54

    
55
    // Send a test message that simpletest_mail_alter should cancel.
56
    $message = drupal_mail('simpletest', 'cancel_test', 'cancel@example.com', $language);
57

    
58
    // Assert that the message was not actually sent.
59
    $this->assertNull(self::$sent_message, 'Message was canceled.');
60
  }
61

    
62
  /**
63
   * Checks for the site name in an auto-generated From: header.
64
   */
65
  function testFromHeader() {
66
    global $language;
67
    $default_from = variable_get('site_mail', ini_get('sendmail_from'));
68
    $site_name = variable_get('site_name', 'Drupal');
69

    
70
    // Reset the class variable holding a copy of the last sent message.
71
    self::$sent_message = NULL;
72
    // Send an e-mail with a sender address specified.
73
    $from_email = 'someone_else@example.com';
74
    $message = drupal_mail('simpletest', 'from_test', 'from_test@example.com', $language, array(), $from_email);
75
    // Test that the from e-mail is just the e-mail and not the site name and
76
    // default sender e-mail.
77
    $this->assertEqual($from_email, self::$sent_message['headers']['From']);
78

    
79
    // Check default behavior is only email in FROM header.
80
    self::$sent_message = NULL;
81
    // Send an e-mail and check that the From-header contains only default mail address.
82
    variable_del('mail_display_name_site_name');
83
    $message = drupal_mail('simpletest', 'from_test', 'from_test@example.com', $language);
84
    $this->assertEqual($default_from, self::$sent_message['headers']['From']);
85

    
86
    self::$sent_message = NULL;
87
    // Send an e-mail and check that the From-header contains the site name.
88
    variable_set('mail_display_name_site_name', TRUE);
89
    $message = drupal_mail('simpletest', 'from_test', 'from_test@example.com', $language);
90
    $this->assertEqual($site_name . ' <' . $default_from . '>', self::$sent_message['headers']['From']);
91
  }
92

    
93
  /**
94
   * Checks for the site name in an auto-generated From: header.
95
   */
96
  function testFromHeaderRfc2822Compliant() {
97
    global $language;
98
    $default_from = variable_get('site_mail', ini_get('sendmail_from'));
99

    
100
    // Enable adding a site name to From.
101
    variable_set('mail_display_name_site_name', TRUE);
102

    
103
    $site_names = array(
104
      // Simple ASCII characters.
105
      'Test site' => 'Test site',
106
      // ASCII with html entity.
107
      'Test &amp; site' => 'Test & site',
108
      // Non-ASCII characters.
109
      'Tést site' => '=?UTF-8?B?VMOpc3Qgc2l0ZQ==?=',
110
      // Non-ASCII with special characters.
111
      'Tést; site' => '=?UTF-8?B?VMOpc3Q7IHNpdGU=?=',
112
      // Non-ASCII with html entity.
113
      'T&eacute;st; site' => '=?UTF-8?B?VMOpc3Q7IHNpdGU=?=',
114
      // ASCII with special characters.
115
      'Test; site' => '"Test; site"',
116
      // ASCII with special characters as html entity.
117
      'Test &lt; site' => '"Test < site"',
118
      // ASCII with special characters and '\'.
119
      'Test; \ "site"' => '"Test; \\\\ \"site\""',
120
      // String already RFC-2822 compliant.
121
      '"Test; site"' => '"Test; site"',
122
      // String already RFC-2822 compliant.
123
      '"Test; \\\\ \"site\""' => '"Test; \\\\ \"site\""',
124
    );
125

    
126
    foreach ($site_names as $original_name => $safe_string) {
127
      variable_set('site_name', $original_name);
128

    
129
      // Reset the class variable holding a copy of the last sent message.
130
      self::$sent_message = NULL;
131
      // Send an e-mail and check that the From-header contains is RFC-2822 compliant.
132
      drupal_mail('simpletest', 'from_test', 'from_test@example.com', $language);
133
      $this->assertEqual($safe_string . ' <' . $default_from . '>', self::$sent_message['headers']['From']);
134
    }
135
  }
136

    
137
  /**
138
   * Concatenate and wrap the e-mail body for plain-text mails.
139
   *
140
   * @see DefaultMailSystem
141
   */
142
  public function format(array $message) {
143
    // Join the body array into one string.
144
    $message['body'] = implode("\n\n", $message['body']);
145
    // Convert any HTML to plain-text.
146
    $message['body'] = drupal_html_to_text($message['body']);
147
    // Wrap the mail body for sending.
148
    $message['body'] = drupal_wrap_mail($message['body']);
149
    return $message;
150
  }
151

    
152
  /**
153
   * Send function that is called through the mail system.
154
   */
155
  public function mail(array $message) {
156
    self::$sent_message = $message;
157
  }
158
}
159

    
160
/**
161
 * Unit tests for drupal_html_to_text().
162
 */
163
class DrupalHtmlToTextTestCase extends DrupalWebTestCase {
164
  public static function getInfo() {
165
    return array(
166
      'name'  => 'HTML to text conversion',
167
      'description' => 'Tests drupal_html_to_text().',
168
      'group' => 'Mail',
169
    );
170
  }
171

    
172
  /**
173
   * Converts a string to its PHP source equivalent for display in test messages.
174
   *
175
   * @param $text
176
   *   The text string to convert.
177
   *
178
   * @return
179
   *   An HTML representation of the text string that, when displayed in a
180
   *   browser, represents the PHP source code equivalent of $text.
181
   */
182
  function stringToHtml($text) {
183
    return '"' .
184
      str_replace(
185
        array("\n", ' '),
186
        array('\n', '&nbsp;'),
187
        check_plain($text)
188
      ) . '"';
189
  }
190

    
191
  /**
192
   * Helper function for testing drupal_html_to_text().
193
   *
194
   * @param $html
195
   *   The source HTML string to be converted.
196
   * @param $text
197
   *   The expected result of converting $html to text.
198
   * @param $message
199
   *   A text message to display in the assertion message.
200
   * @param $allowed_tags
201
   *   (optional) An array of allowed tags, or NULL to default to the full
202
   *   set of tags supported by drupal_html_to_text().
203
   */
204
  function assertHtmlToText($html, $text, $message, $allowed_tags = NULL) {
205
    preg_match_all('/<([a-z0-6]+)/', drupal_strtolower($html), $matches);
206
    $tested_tags = implode(', ', array_unique($matches[1]));
207
    $message .= ' (' . $tested_tags . ')';
208
    $result = drupal_html_to_text($html, $allowed_tags);
209
    $pass = $this->assertEqual($result, $text, check_plain($message));
210
    $verbose = 'html = <pre>' . $this->stringToHtml($html)
211
      . '</pre><br />' . 'result = <pre>' . $this->stringToHtml($result)
212
      . '</pre><br />' . 'expected = <pre>' . $this->stringToHtml($text)
213
      . '</pre>';
214
    $this->verbose($verbose);
215
    if (!$pass) {
216
      $this->pass("Previous test verbose info:<br />$verbose");
217
    }
218
  }
219

    
220
  /**
221
   * Test all supported tags of drupal_html_to_text().
222
   */
223
  function testTags() {
224
    global $base_path, $base_url;
225
    $tests = array(
226
      // @todo Trailing linefeeds should be trimmed.
227
      '<a href = "http://drupal.org">Drupal.org</a>' => "Drupal.org [1]\n\n[1] http://drupal.org\n",
228
      // @todo Footer URLs should be absolute.
229
      "<a href = \"$base_path\">Homepage</a>" => "Homepage [1]\n\n[1] $base_url/\n",
230
      '<address>Drupal</address>' => "Drupal\n",
231
      // @todo The <address> tag is currently not supported.
232
      '<address>Drupal</address><address>Drupal</address>' => "DrupalDrupal\n",
233
      '<b>Drupal</b>' => "*Drupal*\n",
234
      // @todo There should be a space between the '>' and the text.
235
      '<blockquote>Drupal</blockquote>' => ">Drupal\n",
236
      '<blockquote>Drupal</blockquote><blockquote>Drupal</blockquote>' => ">Drupal\n>Drupal\n",
237
      '<br />Drupal<br />Drupal<br /><br />Drupal' => "Drupal\nDrupal\nDrupal\n",
238
      '<br/>Drupal<br/>Drupal<br/><br/>Drupal' => "Drupal\nDrupal\nDrupal\n",
239
      // @todo There should be two line breaks before the paragraph.
240
      '<br/>Drupal<br/>Drupal<br/><br/>Drupal<p>Drupal</p>' => "Drupal\nDrupal\nDrupal\nDrupal\n\n",
241
      '<div>Drupal</div>' => "Drupal\n",
242
      // @todo The <div> tag is currently not supported.
243
      '<div>Drupal</div><div>Drupal</div>' => "DrupalDrupal\n",
244
      '<em>Drupal</em>' => "/Drupal/\n",
245
      '<h1>Drupal</h1>' => "======== DRUPAL ==============================================================\n\n",
246
      '<h1>Drupal</h1><p>Drupal</p>' => "======== DRUPAL ==============================================================\n\nDrupal\n\n",
247
      '<h2>Drupal</h2>' => "-------- DRUPAL --------------------------------------------------------------\n\n",
248
      '<h2>Drupal</h2><p>Drupal</p>' => "-------- DRUPAL --------------------------------------------------------------\n\nDrupal\n\n",
249
      '<h3>Drupal</h3>' => ".... Drupal\n\n",
250
      '<h3>Drupal</h3><p>Drupal</p>' => ".... Drupal\n\nDrupal\n\n",
251
      '<h4>Drupal</h4>' => ".. Drupal\n\n",
252
      '<h4>Drupal</h4><p>Drupal</p>' => ".. Drupal\n\nDrupal\n\n",
253
      '<h5>Drupal</h5>' => "Drupal\n\n",
254
      '<h5>Drupal</h5><p>Drupal</p>' => "Drupal\n\nDrupal\n\n",
255
      '<h6>Drupal</h6>' => "Drupal\n\n",
256
      '<h6>Drupal</h6><p>Drupal</p>' => "Drupal\n\nDrupal\n\n",
257
      '<hr />Drupal<hr />' => "------------------------------------------------------------------------------\nDrupal\n------------------------------------------------------------------------------\n",
258
      '<hr/>Drupal<hr/>' => "------------------------------------------------------------------------------\nDrupal\n------------------------------------------------------------------------------\n",
259
      '<hr/>Drupal<hr/><p>Drupal</p>' => "------------------------------------------------------------------------------\nDrupal\n------------------------------------------------------------------------------\nDrupal\n\n",
260
      '<i>Drupal</i>' => "/Drupal/\n",
261
      '<p>Drupal</p>' => "Drupal\n\n",
262
      '<p>Drupal</p><p>Drupal</p>' => "Drupal\n\nDrupal\n\n",
263
      '<strong>Drupal</strong>' => "*Drupal*\n",
264
      // @todo Tables are currently not supported.
265
      '<table><tr><td>Drupal</td><td>Drupal</td></tr><tr><td>Drupal</td><td>Drupal</td></tr></table>' => "DrupalDrupalDrupalDrupal\n",
266
      '<table><tr><td>Drupal</td></tr></table><p>Drupal</p>' => "Drupal\nDrupal\n\n",
267
      // @todo The <u> tag is currently not supported.
268
      '<u>Drupal</u>' => "Drupal\n",
269
      '<ul><li>Drupal</li></ul>' => " * Drupal\n\n",
270
      '<ul><li>Drupal <em>Drupal</em> Drupal</li></ul>' => " * Drupal /Drupal/ Drupal\n\n",
271
      // @todo Lines containing nothing but spaces should be trimmed.
272
      '<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",
273
      '<ul><li>Drupal</li><li><ol><li>Drupal</li></ol></li><li>Drupal</li></ul>' => " * Drupal\n *  1) Drupal\n   \n * Drupal\n\n",
274
      '<ul><li>Drupal</li><li>Drupal</li></ul>' => " * Drupal\n * Drupal\n\n",
275
      '<ul><li>Drupal</li></ul><p>Drupal</p>' => " * Drupal\n\nDrupal\n\n",
276
      '<ol><li>Drupal</li></ol>' => " 1) Drupal\n\n",
277
      '<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",
278
      '<ol><li>Drupal</li><li>Drupal</li></ol>' => " 1) Drupal\n 2) Drupal\n\n",
279
      '<ol>Drupal</ol>' => "Drupal\n\n",
280
      '<ol><li>Drupal</li></ol><p>Drupal</p>' => " 1) Drupal\n\nDrupal\n\n",
281
      '<dl><dt>Drupal</dt></dl>' => "Drupal\n\n",
282
      '<dl><dt>Drupal</dt><dd>Drupal</dd></dl>' => "Drupal\n    Drupal\n\n",
283
      '<dl><dt>Drupal</dt><dd>Drupal</dd><dt>Drupal</dt><dd>Drupal</dd></dl>' => "Drupal\n    Drupal\nDrupal\n    Drupal\n\n",
284
      '<dl><dt>Drupal</dt><dd>Drupal</dd></dl><p>Drupal</p>' => "Drupal\n    Drupal\n\nDrupal\n\n",
285
      '<dl><dt>Drupal<dd>Drupal</dl>' => "Drupal\n    Drupal\n\n",
286
      '<dl><dt>Drupal</dt></dl><p>Drupal</p>' => "Drupal\n\nDrupal\n\n",
287
      // @todo Again, lines containing only spaces should be trimmed.
288
      '<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",
289
      // Tests malformed HTML tags.
290
      '<br>Drupal<br>Drupal' => "Drupal\nDrupal\n",
291
      '<hr>Drupal<hr>Drupal' => "------------------------------------------------------------------------------\nDrupal\n------------------------------------------------------------------------------\nDrupal\n",
292
      '<ol><li>Drupal<li>Drupal</ol>' => " 1) Drupal\n 2) Drupal\n\n",
293
      '<ul><li>Drupal <em>Drupal</em> Drupal</ul></ul>' => " * Drupal /Drupal/ Drupal\n\n",
294
      '<ul><li>Drupal<li>Drupal</ol>' => " * Drupal\n * Drupal\n\n",
295
      '<ul><li>Drupal<li>Drupal</ul>' => " * Drupal\n * Drupal\n\n",
296
      '<ul>Drupal</ul>' => "Drupal\n\n",
297
      'Drupal</ul></ol></dl><li>Drupal' => "Drupal\n * Drupal\n",
298
      '<dl>Drupal</dl>' => "Drupal\n\n",
299
      '<dl>Drupal</dl><p>Drupal</p>' => "Drupal\n\nDrupal\n\n",
300
      '<dt>Drupal</dt>' => "Drupal\n",
301
      // Tests some unsupported HTML tags.
302
      '<html>Drupal</html>' => "Drupal\n",
303
      // @todo Perhaps the contents of <script> tags should be dropped.
304
      '<script type="text/javascript">Drupal</script>' => "Drupal\n",
305
    );
306

    
307
    foreach ($tests as $html => $text) {
308
      $this->assertHtmlToText($html, $text, 'Supported tags');
309
    }
310
  }
311

    
312
  /**
313
   * Test $allowed_tags argument of drupal_html_to_text().
314
   */
315
  function testDrupalHtmlToTextArgs() {
316
    // The second parameter of drupal_html_to_text() overrules the allowed tags.
317
    $this->assertHtmlToText(
318
      'Drupal <b>Drupal</b> Drupal',
319
      "Drupal *Drupal* Drupal\n",
320
      'Allowed <b> tag found',
321
      array('b')
322
    );
323
    $this->assertHtmlToText(
324
      'Drupal <h1>Drupal</h1> Drupal',
325
      "Drupal Drupal Drupal\n",
326
      'Disallowed <h1> tag not found',
327
      array('b')
328
    );
329

    
330
    $this->assertHtmlToText(
331
      'Drupal <p><em><b>Drupal</b></em><p> Drupal',
332
      "Drupal Drupal Drupal\n",
333
      'Disallowed <p>, <em>, and <b> tags not found',
334
      array('a', 'br', 'h1')
335
    );
336

    
337
    $this->assertHtmlToText(
338
      '<html><body>Drupal</body></html>',
339
      "Drupal\n",
340
      'Unsupported <html> and <body> tags not found',
341
      array('html', 'body')
342
    );
343
  }
344

    
345
  /**
346
   * Tests that drupal_wrap_mail() removes trailing whitespace before newlines.
347
   */
348
  function testDrupalHtmltoTextRemoveTrailingWhitespace() {
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
  /**
355
   * Tests drupal_wrap_mail() retains whitespace from 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
  function testDrupalHtmltoTextUsenetSignature() {
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

    
365
    $text = "Hi there!\n--  \nHerp Derp";
366
    $mail_lines = explode("\n", drupal_wrap_mail($text));
367
    $this->assertEqual("--", $mail_lines[1], 'Trailing whitespace removed for incorrect dash-dash-space signatures.');
368
  }
369

    
370
  /**
371
   * Test that whitespace is collapsed.
372
   */
373
  function testDrupalHtmltoTextCollapsesWhitespace() {
374
    $input = "<p>Drupal  Drupal\n\nDrupal<pre>Drupal  Drupal\n\nDrupal</pre>Drupal  Drupal\n\nDrupal</p>";
375
    // @todo The whitespace should be collapsed.
376
    $collapsed = "Drupal  Drupal\n\nDrupalDrupal  Drupal\n\nDrupalDrupal  Drupal\n\nDrupal\n\n";
377
    $this->assertHtmlToText(
378
      $input,
379
      $collapsed,
380
      'Whitespace is collapsed',
381
      array('p')
382
    );
383
  }
384

    
385
  /**
386
   * Test that text separated by block-level tags in HTML get separated by
387
   * (at least) a newline in the plaintext version.
388
   */
389
  function testDrupalHtmlToTextBlockTagToNewline() {
390
    $input = '[text]'
391
      . '<blockquote>[blockquote]</blockquote>'
392
      . '<br />[br]'
393
      . '<dl><dt>[dl-dt]</dt>'
394
      . '<dt>[dt]</dt>'
395
      . '<dd>[dd]</dd>'
396
      . '<dd>[dd-dl]</dd></dl>'
397
      . '<h1>[h1]</h1>'
398
      . '<h2>[h2]</h2>'
399
      . '<h3>[h3]</h3>'
400
      . '<h4>[h4]</h4>'
401
      . '<h5>[h5]</h5>'
402
      . '<h6>[h6]</h6>'
403
      . '<hr />[hr]'
404
      . '<ol><li>[ol-li]</li>'
405
      . '<li>[li]</li>'
406
      . '<li>[li-ol]</li></ol>'
407
      . '<p>[p]</p>'
408
      . '<ul><li>[ul-li]</li>'
409
      . '<li>[li-ul]</li></ul>'
410
      . '[text]';
411
    $output = drupal_html_to_text($input);
412
    $pass = $this->assertFalse(
413
      preg_match('/\][^\n]*\[/s', $output),
414
      'Block-level HTML tags should force newlines'
415
    );
416
    if (!$pass) {
417
      $this->verbose($this->stringToHtml($output));
418
    }
419
    $output_upper = drupal_strtoupper($output);
420
    $upper_input = drupal_strtoupper($input);
421
    $upper_output = drupal_html_to_text($upper_input);
422
    $pass = $this->assertEqual(
423
      $upper_output,
424
      $output_upper,
425
      'Tag recognition should be case-insensitive'
426
    );
427
    if (!$pass) {
428
      $this->verbose(
429
        $upper_output
430
        . '<br />should  be equal to <br />'
431
        . $output_upper
432
      );
433
    }
434
  }
435

    
436
  /**
437
   * Test that headers are properly separated from surrounding text.
438
   */
439
  function testHeaderSeparation() {
440
    $html = 'Drupal<h1>Drupal</h1>Drupal';
441
    // @todo There should be more space above the header than below it.
442
    $text = "Drupal\n======== DRUPAL ==============================================================\n\nDrupal\n";
443
    $this->assertHtmlToText($html, $text,
444
      'Text before and after <h1> tag');
445
    $html = '<p>Drupal</p><h1>Drupal</h1>Drupal';
446
    // @todo There should be more space above the header than below it.
447
    $text = "Drupal\n\n======== DRUPAL ==============================================================\n\nDrupal\n";
448
    $this->assertHtmlToText($html, $text,
449
      'Paragraph before and text after <h1> tag');
450
    $html = 'Drupal<h1>Drupal</h1><p>Drupal</p>';
451
    // @todo There should be more space above the header than below it.
452
    $text = "Drupal\n======== DRUPAL ==============================================================\n\nDrupal\n\n";
453
    $this->assertHtmlToText($html, $text,
454
      'Text before and paragraph after <h1> tag');
455
    $html = '<p>Drupal</p><h1>Drupal</h1><p>Drupal</p>';
456
    $text = "Drupal\n\n======== DRUPAL ==============================================================\n\nDrupal\n\n";
457
    $this->assertHtmlToText($html, $text,
458
      'Paragraph before and after <h1> tag');
459
  }
460

    
461
  /**
462
   * Test that footnote references are properly generated.
463
   */
464
  function testFootnoteReferences() {
465
    global $base_path, $base_url;
466
    $source = '<a href="http://www.example.com/node/1">Host and path</a>'
467
      . '<br /><a href="http://www.example.com">Host, no path</a>'
468
      . '<br /><a href="' . $base_path . 'node/1">Path, no host</a>'
469
      . '<br /><a href="node/1">Relative path</a>';
470
    // @todo Footnote URLs should be absolute.
471
    $tt = "Host and path [1]"
472
      . "\nHost, no path [2]"
473
      // @todo The following two references should be combined.
474
      . "\nPath, no host [3]"
475
      . "\nRelative path [4]"
476
      . "\n"
477
      . "\n[1] http://www.example.com/node/1"
478
      . "\n[2] http://www.example.com"
479
      // @todo The following two references should be combined.
480
      . "\n[3] $base_url/node/1"
481
      . "\n[4] node/1\n";
482
    $this->assertHtmlToText($source, $tt, 'Footnotes');
483
  }
484

    
485
  /**
486
   * Test that combinations of paragraph breaks, line breaks, linefeeds,
487
   * and spaces are properly handled.
488
   */
489
  function testDrupalHtmlToTextParagraphs() {
490
    $tests = array();
491
    $tests[] = array(
492
        'html' => "<p>line 1<br />\nline 2<br />line 3\n<br />line 4</p><p>paragraph</p>",
493
        // @todo Trailing line breaks should be trimmed.
494
        'text' => "line 1\nline 2\nline 3\nline 4\n\nparagraph\n\n",
495
    );
496
    $tests[] = array(
497
      'html' => "<p>line 1<br /> line 2</p> <p>line 4<br /> line 5</p> <p>0</p>",
498
      // @todo Trailing line breaks should be trimmed.
499
      'text' => "line 1\nline 2\n\nline 4\nline 5\n\n0\n\n",
500
    );
501
    foreach ($tests as $test) {
502
      $this->assertHtmlToText($test['html'], $test['text'], 'Paragraph breaks');
503
    }
504
  }
505

    
506
  /**
507
   * Tests that drupal_html_to_text() wraps before 1000 characters.
508
   *
509
   * RFC 3676 says, "The Text/Plain media type is the lowest common
510
   * denominator of Internet email, with lines of no more than 998 characters."
511
   *
512
   * RFC 2046 says, "SMTP [RFC-821] allows a maximum of 998 octets before the
513
   * next CRLF sequence."
514
   *
515
   * RFC 821 says, "The maximum total length of a text line including the
516
   * <CRLF> is 1000 characters."
517
   */
518
  function testVeryLongLineWrap() {
519
    $input = 'Drupal<br /><p>' . str_repeat('x', 2100) . '</p><br />Drupal';
520
    $output = drupal_html_to_text($input);
521
    // This awkward construct comes from includes/mail.inc lines 8-13.
522
    $eol = variable_get('mail_line_endings', MAIL_LINE_ENDINGS);
523
    // We must use strlen() rather than drupal_strlen() in order to count
524
    // octets rather than characters.
525
    $line_length_limit = 1000 - drupal_strlen($eol);
526
    $maximum_line_length = 0;
527
    foreach (explode($eol, $output) as $line) {
528
      // We must use strlen() rather than drupal_strlen() in order to count
529
      // octets rather than characters.
530
      $maximum_line_length = max($maximum_line_length, strlen($line . $eol));
531
    }
532
    $verbose = 'Maximum line length found was ' . $maximum_line_length . ' octets.';
533
    $this->assertTrue($maximum_line_length <= 1000, $verbose);
534
  }
535
}