1 filter.test | FilterUnitTestCase::testLineBreakFilter() |
Tests the line break filter.
File
- core/
modules/ filter/ tests/ filter.test, line 990 - Tests for filter.module.
Class
- FilterUnitTestCase
- Unit tests for core filters.
Code
function testLineBreakFilter() {
// Set up dummy filter object.
$filter = new stdClass();
$filter->callback = '_filter_autop';
// Since the line break filter naturally needs plenty of newlines in test
// strings and expectations, we're using "\n" instead of regular newlines
// here.
$tests = array(
// Single line breaks should be changed to <br /> tags, while paragraphs
// separated with double line breaks should be enclosed with <p></p> tags.
"aaa\nbbb\n\nccc" => array(
"<p>aaa<br />\nbbb</p>\n<p>ccc</p>" => TRUE,
),
// Skip contents of certain block tags entirely.
"<script>aaa\nbbb\n\nccc</script>
<style>aaa\nbbb\n\nccc</style>
<pre>aaa\nbbb\n\nccc</pre>
<object>aaa\nbbb\n\nccc</object>
<iframe>aaa\nbbb\n\nccc</iframe>
" => array(
"<script>aaa\nbbb\n\nccc</script>" => TRUE,
"<style>aaa\nbbb\n\nccc</style>" => TRUE,
"<pre>aaa\nbbb\n\nccc</pre>" => TRUE,
"<object>aaa\nbbb\n\nccc</object>" => TRUE,
"<iframe>aaa\nbbb\n\nccc</iframe>" => TRUE,
),
// Skip comments entirely.
"One. <!-- comment --> Two.\n<!--\nThree.\n-->\n" => array(
'<!-- comment -->' => TRUE,
"<!--\nThree.\n-->" => TRUE,
),
// Resulting HTML should produce matching paragraph tags.
'<p><div> </div></p>' => array(
"<p>\n<div> </div>\n</p>" => TRUE,
),
'<div><p> </p></div>' => array(
"<div>\n</div>" => TRUE,
),
'<blockquote><pre>aaa</pre></blockquote>' => array(
"<blockquote><pre>aaa</pre></blockquote>" => TRUE,
),
"<pre>aaa\nbbb\nccc</pre>\nddd\neee" => array(
"<pre>aaa\nbbb\nccc</pre>" => TRUE,
"<p>ddd<br />\neee</p>" => TRUE,
),
// Comments remain unchanged and subsequent lines/paragraphs are
// transformed normally.
"aaa<!--comment-->\n\nbbb\n\nccc\n\nddd<!--comment\nwith line break-->\n\neee\n\nfff" => array(
"<p>aaa</p>\n<!--comment--><p>\nbbb</p>\n<p>ccc</p>\n<p>ddd</p>" => TRUE,
"<!--comment\nwith line break--><p>\neee</p>\n<p>fff</p>" => TRUE,
),
// Check that a comment in a PRE will result that the text after
// the comment, but still in PRE, is not transformed.
"<pre>aaa\nbbb<!-- comment -->\n\nccc</pre>\nddd" => array(
"<pre>aaa\nbbb<!-- comment -->\n\nccc</pre>" => TRUE,
),
// Bug 810824, paragraphs were appearing around iframe tags.
"<iframe>aaa</iframe>\n\n" => array(
"<p><iframe>aaa</iframe></p>" => FALSE,
),
// Check that tags that close on a separate line do not get an extra
// <br /> before the closing tag. This is the default formatting output by
// CKEditor 5 in Backdrop.
"<h3>\n indented line\n</h3>" => array(
"<h3>\n indented line\n</h3>" => TRUE,
"<h3>\n indented line<br />\n</h3>" => FALSE,
),
// Check that new lines within block elements have <br /> tags added, but
// not before the closing tag on a new line.
"<p>\n indented line\n second indented line\n</p>" => array(
"<p>\n indented line<br />\n second indented line\n</p>" => TRUE,
"<p>\n indented line<br />\n second indented line<br />\n</p>" => FALSE,
"<p>\n indented line\n second indented line\n</p>" => FALSE,
),
// Check that manually added <br> tags at end of lines don't duplicate
// another <br /> tag after them. Line breaks are normalized from <br>
// to <br /> as part of this filter.
"<p>\n line before break<br>\n line after break</p>" => array(
"<p>\n line before break<br />\n line after break</p>" => TRUE,
"<p>\n line before break<br><br />\n line after break</p>" => FALSE,
"<p>\n line before break<br /><br />\n line after break</p>" => FALSE,
),
);
$this->assertFilteredString($filter, $tests);
// Very long string hitting PCRE limits.
$limit = max(ini_get('pcre.backtrack_limit'), ini_get('pcre.recursion_limit'));
$source = $this->randomName($limit);
$result = _filter_autop($source);
$success = $this->assertEqual($result, '<p>' . $source . "</p>\n", 'Line break filter can process very long strings.');
if (!$success) {
$this->verbose("\n" . $source . "\n<hr />\n" . $result);
}
}