From 55286952b6cb45d6540ddf2ad6ef3d84931f74f1 Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Wed, 5 Nov 2014 09:22:25 +0000 Subject: [PATCH] Issue #2358999 by rpayanm, JeroenT, er.pushpinderrana, javivf, Ec1ipsis, aczietlow | Les Lim: Remove usage of drupal_html_to_text(). --- .../Drupal/Core/Mail/Plugin/Mail/PhpMail.php | 3 ++- .../contact/src/MessageViewBuilder.php | 6 +++-- .../contact/src/Tests/ContactSitewideTest.php | 5 ++-- .../system/src/Tests/Mail/HtmlToTextTest.php | 25 +++++++++++-------- core/modules/system/system.api.php | 3 ++- 5 files changed, 25 insertions(+), 17 deletions(-) diff --git a/core/lib/Drupal/Core/Mail/Plugin/Mail/PhpMail.php b/core/lib/Drupal/Core/Mail/Plugin/Mail/PhpMail.php index caa2af37540..1a97b661ad1 100644 --- a/core/lib/Drupal/Core/Mail/Plugin/Mail/PhpMail.php +++ b/core/lib/Drupal/Core/Mail/Plugin/Mail/PhpMail.php @@ -8,6 +8,7 @@ namespace Drupal\Core\Mail\Plugin\Mail; use Drupal\Component\Utility\Unicode; +use Drupal\Core\Mail\MailFormatHelper; use Drupal\Core\Mail\MailInterface; use Drupal\Core\Site\Settings; @@ -35,7 +36,7 @@ class PhpMail implements MailInterface { // Join the body array into one string. $message['body'] = implode("\n\n", $message['body']); // Convert any HTML to plain-text. - $message['body'] = drupal_html_to_text($message['body']); + $message['body'] = MailFormatHelper::htmlToText($message['body']); // Wrap the mail body for sending. $message['body'] = drupal_wrap_mail($message['body']); diff --git a/core/modules/contact/src/MessageViewBuilder.php b/core/modules/contact/src/MessageViewBuilder.php index 23e9b430043..47173386bf1 100644 --- a/core/modules/contact/src/MessageViewBuilder.php +++ b/core/modules/contact/src/MessageViewBuilder.php @@ -10,6 +10,7 @@ namespace Drupal\contact; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityViewBuilder; use Drupal\Component\Utility\String; +use Drupal\Core\Mail\MailFormatHelper; use Drupal\Core\Render\Element; /** @@ -55,7 +56,8 @@ class MessageViewBuilder extends EntityViewBuilder { if ($view_mode == 'mail') { // Convert field labels into headings. - // @todo Improve drupal_html_to_text() to convert DIVs correctly. + // @todo Improve \Drupal\Core\Mail\MailFormatHelper::htmlToText() to + // convert DIVs correctly. foreach (Element::children($build) as $key) { if (isset($build[$key]['#label_display']) && $build[$key]['#label_display'] == 'above') { $build[$key] += array('#prefix' => ''); @@ -64,7 +66,7 @@ class MessageViewBuilder extends EntityViewBuilder { } } $build = array( - '#markup' => drupal_html_to_text(drupal_render($build)), + '#markup' => MailFormatHelper::htmlToText(drupal_render($build)), ); } return $build; diff --git a/core/modules/contact/src/Tests/ContactSitewideTest.php b/core/modules/contact/src/Tests/ContactSitewideTest.php index 257468de1ec..2f108663822 100644 --- a/core/modules/contact/src/Tests/ContactSitewideTest.php +++ b/core/modules/contact/src/Tests/ContactSitewideTest.php @@ -8,6 +8,7 @@ namespace Drupal\contact\Tests; use Drupal\Component\Utility\Unicode; +use Drupal\Core\Mail\MailFormatHelper; use Drupal\simpletest\WebTestBase; use Drupal\Core\Entity\EntityTypeInterface; @@ -299,7 +300,7 @@ class ContactSitewideTest extends WebTestBase { // We are testing the auto-reply, so there should be one email going to the sender. $captured_emails = $this->drupalGetMails(array('id' => 'contact_page_autoreply', 'to' => $email)); $this->assertEqual(count($captured_emails), 1); - $this->assertEqual(trim($captured_emails[0]['body']), trim(drupal_html_to_text($foo_autoreply))); + $this->assertEqual(trim($captured_emails[0]['body']), trim(MailFormatHelper::htmlToText($foo_autoreply))); // Test the auto-reply for form 'bar'. $email = $this->randomMachineName(32) . '@example.com'; @@ -308,7 +309,7 @@ class ContactSitewideTest extends WebTestBase { // Auto-reply for form 'bar' should result in one auto-reply email to the sender. $captured_emails = $this->drupalGetMails(array('id' => 'contact_page_autoreply', 'to' => $email)); $this->assertEqual(count($captured_emails), 1); - $this->assertEqual(trim($captured_emails[0]['body']), trim(drupal_html_to_text($bar_autoreply))); + $this->assertEqual(trim($captured_emails[0]['body']), trim(MailFormatHelper::htmlToText($bar_autoreply))); // Verify that no auto-reply is sent when the auto-reply field is left blank. $email = $this->randomMachineName(32) . '@example.com'; diff --git a/core/modules/system/src/Tests/Mail/HtmlToTextTest.php b/core/modules/system/src/Tests/Mail/HtmlToTextTest.php index e95e0b5d924..f26b49810d4 100644 --- a/core/modules/system/src/Tests/Mail/HtmlToTextTest.php +++ b/core/modules/system/src/Tests/Mail/HtmlToTextTest.php @@ -9,11 +9,12 @@ namespace Drupal\system\Tests\Mail; use Drupal\Component\Utility\String; use Drupal\Component\Utility\Unicode; +use Drupal\Core\Mail\MailFormatHelper; use Drupal\Core\Site\Settings; use Drupal\simpletest\WebTestBase; /** - * Tests for drupal_html_to_text(). + * Tests for \Drupal\Core\Mail\MailFormatHelper::htmlToText(). * * @group Mail */ @@ -38,7 +39,7 @@ class HtmlToTextTest extends WebTestBase { } /** - * Helper function for testing drupal_html_to_text(). + * Helper function to test \Drupal\Core\Mail\MailFormatHelper::htmlToText(). * * @param $html * The source HTML string to be converted. @@ -48,13 +49,14 @@ class HtmlToTextTest extends WebTestBase { * A text message to display in the assertion message. * @param $allowed_tags * (optional) An array of allowed tags, or NULL to default to the full - * set of tags supported by drupal_html_to_text(). + * set of tags supported by + * \Drupal\Core\Mail\MailFormatHelper::htmlToText(). */ protected function assertHtmlToText($html, $text, $message, $allowed_tags = NULL) { preg_match_all('/<([a-z0-6]+)/', Unicode::strtolower($html), $matches); $tested_tags = implode(', ', array_unique($matches[1])); $message .= ' (' . $tested_tags . ')'; - $result = drupal_html_to_text($html, $allowed_tags); + $result = MailFormatHelper::htmlToText($html, $allowed_tags); $pass = $this->assertEqual($result, $text, String::checkPlain($message)); $verbose = 'html =
' . $this->stringToHtml($html)
       . '

' . 'result =
' . $this->stringToHtml($result)
@@ -67,7 +69,7 @@ class HtmlToTextTest extends WebTestBase {
   }
 
   /**
-   * Test all supported tags of drupal_html_to_text().
+   * Test supported tags of \Drupal\Core\Mail\MailFormatHelper::htmlToText().
    */
   public function testTags() {
     global $base_path, $base_url;
@@ -162,10 +164,11 @@ class HtmlToTextTest extends WebTestBase {
   }
 
   /**
-   * Test $allowed_tags argument of drupal_html_to_text().
+   * Tests allowing tags in \Drupal\Core\Mail\MailFormatHelper::htmlToText().
    */
   public function testDrupalHtmlToTextArgs() {
-    // The second parameter of drupal_html_to_text() overrules the allowed tags.
+    // The second parameter of \Drupal\Core\Mail\MailFormatHelper::htmlToText()
+    // overrules the allowed tags.
     $this->assertHtmlToText(
       'Drupal Drupal Drupal',
       "Drupal *Drupal* Drupal\n",
@@ -235,7 +238,7 @@ class HtmlToTextTest extends WebTestBase {
       . ''
       . '[text]';
-    $output = drupal_html_to_text($input);
+    $output = MailFormatHelper::htmlToText($input);
     $pass = $this->assertFalse(
       preg_match('/\][^\n]*\[/s', $output),
       'Block-level HTML tags should force newlines'
@@ -245,7 +248,7 @@ class HtmlToTextTest extends WebTestBase {
     }
     $output_upper = Unicode::strtoupper($output);
     $upper_input = Unicode::strtoupper($input);
-    $upper_output = drupal_html_to_text($upper_input);
+    $upper_output = MailFormatHelper::htmlToText($upper_input);
     $pass = $this->assertEqual(
       $upper_output,
       $output_upper,
@@ -331,7 +334,7 @@ class HtmlToTextTest extends WebTestBase {
   }
 
   /**
-   * Tests that drupal_html_to_text() wraps before 1000 characters.
+   * Tests \Drupal\Core\Mail\MailFormatHelper::htmlToText() wrapping.
    *
    * RFC 3676 says, "The Text/Plain media type is the lowest common
    * denominator of Internet email, with lines of no more than 998 characters."
@@ -344,7 +347,7 @@ class HtmlToTextTest extends WebTestBase {
    */
   public function testVeryLongLineWrap() {
     $input = 'Drupal

' . str_repeat('x', 2100) . '
Drupal'; - $output = drupal_html_to_text($input); + $output = MailFormatHelper::htmlToText($input); $eol = Settings::get('mail_line_endings', PHP_EOL); $maximum_line_length = 0; diff --git a/core/modules/system/system.api.php b/core/modules/system/system.api.php index e306b0920cd..4e98a643d40 100644 --- a/core/modules/system/system.api.php +++ b/core/modules/system/system.api.php @@ -6,6 +6,7 @@ */ use Drupal\Component\Utility\String; +use Drupal\Core\Mail\MailFormatHelper; use Drupal\Core\Url; /** @@ -482,7 +483,7 @@ function hook_mail($key, &$message, $params) { $subject = strtr($context['subject'], $variables); $body = strtr($context['message'], $variables); $message['subject'] .= str_replace(array("\r", "\n"), '', $subject); - $message['body'][] = drupal_html_to_text($body); + $message['body'][] = MailFormatHelper::htmlToText($body); } /**