function AssertMailTrait::assertMailPattern

Same name in other branches
  1. 9 core/lib/Drupal/Core/Test/AssertMailTrait.php \Drupal\Core\Test\AssertMailTrait::assertMailPattern()
  2. 8.9.x core/lib/Drupal/Core/Test/AssertMailTrait.php \Drupal\Core\Test\AssertMailTrait::assertMailPattern()
  3. 11.x core/lib/Drupal/Core/Test/AssertMailTrait.php \Drupal\Core\Test\AssertMailTrait::assertMailPattern()

Asserts that the most recently sent email message has the pattern in it.

Parameters

string $field_name: Name of field or message property to assert: subject, body, id, ...

string $regex: Pattern to search for.

string $message: (optional) A message to display with the assertion. Do not translate messages with t(). Use double quotes and embed variables directly in message text, or use sprintf() if necessary. Avoid the use of \Drupal\Component\Render\FormattableMarkup unless you cast the object to a string. If left blank, a default message will be displayed.

1 call to AssertMailTrait::assertMailPattern()
AssertMailTraitTest::testAssertMailTrait in core/tests/Drupal/KernelTests/Core/Test/AssertMailTraitTest.php
Tests that the maintenance theme initializes the theme and its base themes.

File

core/lib/Drupal/Core/Test/AssertMailTrait.php, line 119

Class

AssertMailTrait
Provides methods for testing emails sent during test runs.

Namespace

Drupal\Core\Test

Code

protected function assertMailPattern($field_name, $regex, $message = '') {
    $mails = $this->getMails();
    $mail = end($mails);
    $regex_found = preg_match("/{$regex}/", $mail[$field_name]);
    if (!$message) {
        $message = new FormattableMarkup('Expected text found in @field of email message: "@expected".', [
            '@field' => $field_name,
            '@expected' => $regex,
        ]);
    }
    $this->assertTrue((bool) $regex_found, $message);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.