You are here

protected function NameAdminTest::normalizeOutput in Name Field 8

Helper function to normalize output for testing results.

Normalizes text by:

  • gets complete HTML structure of the child nodes.
  • ensures whitespace around any HTML tags.
  • removes newlines and ensures single whitespaces.
  • trims the string for trailing whitespace.

Parameters

array|false $elements: Raw results from the XML Path lookup.

Return value

string A normalized string.

2 calls to NameAdminTest::normalizeOutput()
NameAdminTest::assertRow in tests/src/Functional/NameAdminTest.php
Helper function to test a table cell via its expected value.
NameAdminTest::assertRowContains in tests/src/Functional/NameAdminTest.php
Helper function to test a table cell via its expected value.

File

tests/src/Functional/NameAdminTest.php, line 384

Class

NameAdminTest
Tests for the admin settings and custom format page.

Namespace

Drupal\Tests\name\Functional

Code

protected function normalizeOutput($elements = []) {
  if (!is_array($elements)) {
    return '__MISSING__';
  }
  $results = '';
  foreach ($elements as $element) {
    $results .= $element
      ->getHtml();
  }

  // Insert a single whitespace in front of all opening HTML tags.
  $results = preg_replace('/<(?!\\/)/', ' <', $results);

  // Normalize any newlines.
  $results = str_replace([
    "\r",
    "\n",
  ], [
    "\n",
    " ",
  ], $results);
  $results = strip_tags($results);

  // Normalize any remaining whitespaces into a single space.
  $results = preg_replace('/\\s+/', ' ', $results);
  return trim($results);
}