You are here

protected function FileUrlWidgetTest::assertOrderInPageText in File URL 8

Same name and namespace in other branches
  1. 2.0.x tests/src/FunctionalJavascript/FileUrlWidgetTest.php \Drupal\Tests\file_url\FunctionalJavascript\FileUrlWidgetTest::assertOrderInPageText()

Asserts that several pieces of text are in a given order in the page.

@todo Remove this method when https://www.drupal.org/node/2817657 lands.

Parameters

string[] $items: An ordered list of strings.

Throws

\Behat\Mink\Exception\ExpectationException When any of the given string is not found.

1 call to FileUrlWidgetTest::assertOrderInPageText()
FileUrlWidgetTest::testFileUrlWidget in tests/src/FunctionalJavascript/FileUrlWidgetTest.php
Tests the file URL widget.

File

tests/src/FunctionalJavascript/FileUrlWidgetTest.php, line 256

Class

FileUrlWidgetTest
Tests the file URL widget.

Namespace

Drupal\Tests\file_url\FunctionalJavascript

Code

protected function assertOrderInPageText(array $items) {
  $session = $this
    ->getSession();
  $text = $session
    ->getPage()
    ->getText();
  $strings = $not_found = [];
  foreach ($items as $item) {
    if (($pos = strpos($text, $item)) === FALSE) {
      if (!in_array($item, $not_found)) {
        $not_found[] = $item;
      }
    }
    else {
      $strings[$pos] = $item;
    }
  }
  $quote_string_list = function (array $list) {
    return implode(', ', array_map(function ($string) {
      return "'{$string}'";
    }, $list));
  };
  if ($not_found) {
    $not_found = $quote_string_list($not_found);
    throw new ElementNotFoundException($session
      ->getDriver(), "Cannot find item(s): {$not_found}.");
  }
  ksort($strings);
  $ordered = $quote_string_list($items);
  $this
    ->assertTrue($items === array_values($strings), "Strings correctly ordered as: {$ordered}.");
}