Issue #2358999 by rpayanm, JeroenT, er.pushpinderrana, javivf, Ec1ipsis, aczietlow | Les Lim: Remove usage of drupal_html_to_text().

8.0.x
Alex Pott 2014-11-05 09:22:25 +00:00
parent 031d0f6c91
commit 55286952b6
5 changed files with 25 additions and 17 deletions

View File

@ -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']);

View File

@ -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;

View File

@ -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';

View File

@ -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 = <pre>' . $this->stringToHtml($html)
. '</pre><br />' . 'result = <pre>' . $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 <b>Drupal</b> Drupal',
"Drupal *Drupal* Drupal\n",
@ -235,7 +238,7 @@ class HtmlToTextTest extends WebTestBase {
. '<ul><li>[ul-li]</li>'
. '<li>[li-ul]</li></ul>'
. '[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<br /><p>' . str_repeat('x', 2100) . '</><br />Drupal';
$output = drupal_html_to_text($input);
$output = MailFormatHelper::htmlToText($input);
$eol = Settings::get('mail_line_endings', PHP_EOL);
$maximum_line_length = 0;

View File

@ -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);
}
/**