You are here

public function MessageDigestSubContext::assertDigestContains in Message Digest 8

Checks that the digest for a user contains a certain message.

Example table: @codingStandardsIgnoreStart | mail_subject | The content titled "My content" has been deleted | | mail_body | The "My content" page was deleted by an administrator. | @codingStandardsIgnoreEnd

@Then the :interval digest for :username should contain the following message for the :label :entity_type: @Then the :interval digest for :username should contain the following message:

Parameters

\Behat\Gherkin\Node\TableNode $table: A table containing the expected content of the different view modes for the message.

string $interval: The digest interval, e.g. 'daily' or 'weekly'.

string $username: The name of the user for which the message is intended.

string $label: Optional label of an entity that is related to the message.

string $entity_type: Optional entity type of an entity that is related to the message.

File

./message_digest.behat.inc, line 46
Contains \MessageDigestSubContext.

Class

MessageDigestSubContext
Behat step definitions for the Message Digest module.

Code

public function assertDigestContains(TableNode $table, $interval, $username, $label = NULL, $entity_type = NULL) {

  // Check that the passed table contains valid view modes.
  $rows = $table
    ->getRowsHash();
  $view_modes = array_keys($rows);
  $notifier = $this
    ->getMessageDigestNotifierForInterval($interval);
  $this
    ->assertNotifierViewModes($notifier, $view_modes);
  $user = user_load_by_name($username);
  $messages = $this
    ->getUserMessagesByNotifier($notifier, $user
    ->id(), $entity_type, $label);
  if (empty($messages)) {
    throw new \RuntimeException("The {$interval} digest for {$username} does not contain any messages related to the '{$label}' {$entity_type}.");
  }
  foreach ($messages as $message) {
    $found_view_modes = [];
    foreach ($rows as $view_mode => $expected_content) {
      $actual_content = $this
        ->getRenderedMessage($message, $view_mode);
      if (strpos($actual_content, $expected_content) !== FALSE) {
        $found_view_modes[] = $view_mode;
      }
    }
    if (empty(array_diff($view_modes, $found_view_modes))) {

      // We found the message.
      return;
    }
  }
  $exception_message = !empty($entity_type) && !empty($label) ? "The expected message for the '{$label}' {$entity_type} was not found in the {$interval} digest for {$username}." : "The expected message was not found in the {$interval} digest for {$username}.";
  throw new \RuntimeException($exception_message);
}