You are here

protected function TestBase::getMessages in Double Field 4.x

Same name and namespace in other branches
  1. 8.3 tests/src/Functional/TestBase.php \Drupal\Tests\double_field\Functional\TestBase::getMessages()

Finds Drupal messages on the page.

Parameters

string $type: A message type (e.g. status, warning, error).

Return value

array List of found messages.

4 calls to TestBase::getMessages()
FieldTypeTest::testAllowedValuesValidation in tests/src/Functional/FieldTypeTest.php
Test allowed values validation.
TestBase::assertErrorMessage in tests/src/Functional/TestBase.php
Passes if a given error message was found on the page.
TestBase::assertStatusMessage in tests/src/Functional/TestBase.php
Passes if a given status message was found on the page.
TestBase::assertWarningMessage in tests/src/Functional/TestBase.php
Passes if a given warning message was found on the page.

File

tests/src/Functional/TestBase.php, line 243

Class

TestBase
Tests the creation of text fields.

Namespace

Drupal\Tests\double_field\Functional

Code

protected function getMessages(string $type) : array {
  $messages = [];
  $get_message = function (NodeElement $element) : string {

    // Remove hidden heading.
    $message = preg_replace('#<h2[^>]*>.*</h2>#', '', $element
      ->getHtml());
    $message = strip_tags($message, '<em>');
    return trim(preg_replace('#\\s+#', ' ', $message));
  };
  $xpath = sprintf('//div[@aria-label = "%s message"]', ucfirst($type));

  // Error messages have one more wrapper.
  if ($type == 'error') {
    $xpath .= '/div[@role = "alert"]';
  }
  $wrapper = $this
    ->xpath($xpath);
  if (!empty($wrapper[0])) {
    unset($wrapper[0]->h2);
    $items = $wrapper[0]
      ->findAll('xpath', '/ul/li');

    // Multiple messages are rendered with an HTML list.
    if ($items) {
      foreach ($items as $item) {
        $messages[] = $get_message($item);
      }
    }
    else {
      $messages[] = $get_message($wrapper[0]);
    }
  }
  return $messages;
}