function TextSummaryTest::testLength

Test various summary length edge cases.

File

drupal/core/modules/field/modules/text/lib/Drupal/text/Tests/TextSummaryTest.php, line 60
Definition of Drupal\text\TextSummaryTest.

Class

TextSummaryTest
Tests the text field summary.

Namespace

Drupal\text\Tests

Code

function testLength() {

  // This string tests a number of edge cases.
  $text = "<p>\nHi\n</p>\n<p>\nfolks\n<br />\n!\n</p>";

  // The summaries we expect text_summary() to return when $size is the index
  // of each array item.
  // Using no text format:
  $expected = array(
    "<p>\nHi\n</p>\n<p>\nfolks\n<br />\n!\n</p>",
    "<",
    "<p",
    "<p>",
    "<p>\n",
    "<p>\nH",
    "<p>\nHi",
    "<p>\nHi\n",
    "<p>\nHi\n<",
    "<p>\nHi\n</",
    "<p>\nHi\n</p",
    "<p>\nHi\n</p>",
    "<p>\nHi\n</p>",
    "<p>\nHi\n</p>",
    "<p>\nHi\n</p>",
    "<p>\nHi\n</p>",
    "<p>\nHi\n</p>",
    "<p>\nHi\n</p>",
    "<p>\nHi\n</p>",
    "<p>\nHi\n</p>",
    "<p>\nHi\n</p>",
    "<p>\nHi\n</p>",
    "<p>\nHi\n</p>",
    "<p>\nHi\n</p>",
    "<p>\nHi\n</p>",
    "<p>\nHi\n</p>",
    "<p>\nHi\n</p>",
    "<p>\nHi\n</p>",
    "<p>\nHi\n</p>",
    "<p>\nHi\n</p>",
    "<p>\nHi\n</p>",
    "<p>\nHi\n</p>",
    "<p>\nHi\n</p>",
    "<p>\nHi\n</p>",
    "<p>\nHi\n</p>",
    "<p>\nHi\n</p>\n<p>\nfolks\n<br />\n!\n</p>",
    "<p>\nHi\n</p>\n<p>\nfolks\n<br />\n!\n</p>",
    "<p>\nHi\n</p>\n<p>\nfolks\n<br />\n!\n</p>",
  );

  // And using a text format WITH the line-break and htmlcorrector filters.
  $expected_lb = array(
    "<p>\nHi\n</p>\n<p>\nfolks\n<br />\n!\n</p>",
    "",
    "<p></p>",
    "<p></p>",
    "<p></p>",
    "<p></p>",
    "<p></p>",
    "<p>\nHi</p>",
    "<p>\nHi</p>",
    "<p>\nHi</p>",
    "<p>\nHi</p>",
    "<p>\nHi\n</p>",
    "<p>\nHi\n</p>",
    "<p>\nHi\n</p>",
    "<p>\nHi\n</p>",
    "<p>\nHi\n</p>",
    "<p>\nHi\n</p>",
    "<p>\nHi\n</p>",
    "<p>\nHi\n</p>",
    "<p>\nHi\n</p>",
    "<p>\nHi\n</p>",
    "<p>\nHi\n</p>",
    "<p>\nHi\n</p>",
    "<p>\nHi\n</p>",
    "<p>\nHi\n</p>",
    "<p>\nHi\n</p>",
    "<p>\nHi\n</p>",
    "<p>\nHi\n</p>",
    "<p>\nHi\n</p>",
    "<p>\nHi\n</p>",
    "<p>\nHi\n</p>",
    "<p>\nHi\n</p>",
    "<p>\nHi\n</p>",
    "<p>\nHi\n</p>",
    "<p>\nHi\n</p>",
    "<p>\nHi\n</p>\n<p>\nfolks\n<br />\n!\n</p>",
    "<p>\nHi\n</p>\n<p>\nfolks\n<br />\n!\n</p>",
    "<p>\nHi\n</p>\n<p>\nfolks\n<br />\n!\n</p>",
  );

  // Test text_summary() for different sizes.
  for ($i = 0; $i <= 37; $i++) {
    $this
      ->callTextSummary($text, $expected[$i], NULL, $i);
    $this
      ->callTextSummary($text, $expected_lb[$i], 'plain_text', $i);
    $this
      ->callTextSummary($text, $expected_lb[$i], 'filtered_html', $i);
  }
}