You are here

public function EmailActivationTest::testActivationMail in Search API Saved Searches 8

Tests whether activation mails are sent correctly.

@dataProvider activationMailDataProvider

Parameters

int $current_user: The index in $this->users of the user to set as the current user.

int $owner: The index in $this->users of the user to set as the owner of the created saved search.

string|null $mail_address: The mail address to set for the saved search, or NULL to use the mail address of the owner.

bool $status: The status to set for the saved search.

bool $expected_status: The expected status of the saved search after saving.

bool $mail_expected: Whether an activation mail is expected to be sent.

File

tests/src/Kernel/EmailActivationTest.php, line 129

Class

EmailActivationTest
Tests whether activation mails are sent correctly.

Namespace

Drupal\Tests\search_api_saved_searches\Kernel

Code

public function testActivationMail($current_user, $owner, $mail_address, $status, $expected_status, $mail_expected) {
  $current_user = $this->users[$current_user];
  $owner = $this->users[$owner];
  $this
    ->setCurrentUser($current_user);
  if ($mail_address === NULL) {
    $mail_address = $owner
      ->getEmail();
  }
  $search = SavedSearch::create([
    'type' => 'default',
    'status' => $status,
    'uid' => $owner
      ->id(),
    'label' => 'Test search 1',
    'mail' => $mail_address,
  ]);
  $this
    ->assertEquals(SAVED_NEW, $search
    ->save());
  $this
    ->sendMails();
  $this
    ->assertEquals($expected_status, $search
    ->get('status')->value);
  $activation_url = $search
    ->toUrl('activate', [
    'absolute' => TRUE,
  ])
    ->toString();
  $this
    ->assertNotEmpty(preg_match('#/saved-search/(\\d+)/activate\\?token=([^&]+)$#', $activation_url, $match));
  $this
    ->assertEquals($search
    ->id(), $match[1]);
  $this
    ->assertEquals(urlencode($search
    ->getAccessToken('activate')), $match[2]);
  $captured_emails = \Drupal::state()
    ->get('system.test_mail_collector');
  if (!$mail_expected) {
    $this
      ->assertEmpty($captured_emails);
    return;
  }
  $this
    ->assertNotEmpty($captured_emails);
  $mail = end($captured_emails);
  $title = '[user:display-name], activate your saved search "[search-api-saved-search:label]" at [site:name]';
  $body = <<<END
[user:display-name],

A saved search on [site:name] with this e-mail address was created.
To activate this saved search, click the following link:

[search-api-saved-search:activate-url]
[foo:bar]

--  [site:name] team
END;
  $this
    ->assertEquals("Saved Searches Test <admin@example.net>", $mail['headers']['From'], 'Message is sent from the site email account.');
  $this
    ->assertEquals($mail_address, $mail['to'], 'Message sent to correct address.');
  $replacements = [
    '[user:display-name]' => $owner
      ->getDisplayName(),
    '[site:name]' => 'Saved Searches Test',
    '[search-api-saved-search:label]' => 'Test search 1',
    '[search-api-saved-search:activate-url]' => $activation_url,
    '[foo:bar]' => '',
  ];
  $title = strtr($title, $replacements);
  $body = strtr($body, $replacements);
  $this
    ->assertEquals($title, $mail['subject']);
  $this
    ->assertEquals($body, trim($mail['body']));
}