function HtmlToTextTest::testDrupalHtmlToTextBlockTagToNewline

Same name and namespace in other branches
  1. 9 core/modules/system/tests/src/Functional/Mail/HtmlToTextTest.php \Drupal\Tests\system\Functional\Mail\HtmlToTextTest::testDrupalHtmlToTextBlockTagToNewline()
  2. 10 core/modules/system/tests/src/Unit/Mail/HtmlToTextTest.php \Drupal\Tests\system\Unit\Mail\HtmlToTextTest::testDrupalHtmlToTextBlockTagToNewline()
  3. 11.x core/modules/system/tests/src/Unit/Mail/HtmlToTextTest.php \Drupal\Tests\system\Unit\Mail\HtmlToTextTest::testDrupalHtmlToTextBlockTagToNewline()

Test that text separated by block-level tags in HTML get separated by (at least) a newline in the plaintext version.

File

core/modules/system/tests/src/Functional/Mail/HtmlToTextTest.php, line 216

Class

HtmlToTextTest
Tests for <a href="/api/drupal/core%21lib%21Drupal%21Core%21Mail%21MailFormatHelper.php/function/MailFormatHelper%3A%3AhtmlToText/8.9.x" title="Transforms an HTML string into plain text, preserving its structure." class="local">\Drupal\Core\Mail\MailFormatHelper::htmlToText</a>().

Namespace

Drupal\Tests\system\Functional\Mail

Code

public function testDrupalHtmlToTextBlockTagToNewline() {
    $input = <<<'EOT'
[text]
<blockquote>[blockquote]</blockquote>
<br />[br]
<dl><dt>[dl-dt]</dt>
<dt>[dt]</dt>
<dd>[dd]</dd>
<dd>[dd-dl]</dd></dl>
<h1>[h1]</h1>
<h2>[h2]</h2>
<h3>[h3]</h3>
<h4>[h4]</h4>
<h5>[h5]</h5>
<h6>[h6]</h6>
<hr />[hr]
<ol><li>[ol-li]</li>
<li>[li]</li>
<li>[li-ol]</li></ol>
<p>[p]</p>
<ul><li>[ul-li]</li>
<li>[li-ul]</li></ul>
[text]
EOT;
    $input = str_replace([
        "\r",
        "\n",
    ], '', $input);
    $output = MailFormatHelper::htmlToText($input);
    $pass = $this->assertNotRegExp('/\\][^\\n]*\\[/s', $output, 'Block-level HTML tags should force newlines');
    if (!$pass) {
        $this->verbose($this->stringToHtml($output));
    }
    $output_upper = mb_strtoupper($output);
    $upper_input = mb_strtoupper($input);
    $upper_output = MailFormatHelper::htmlToText($upper_input);
    $pass = $this->assertEqual($upper_output, $output_upper, 'Tag recognition should be case-insensitive');
    if (!$pass) {
        $this->verbose($upper_output . '<br />should  be equal to <br />' . $output_upper);
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.