function UpdateMailTest::testUpdateEmail

Test the subject and body of update text.

Attributes

#[DataProvider('providerTestUpdateEmail')]

File

core/modules/update/tests/src/Unit/UpdateMailTest.php, line 79

Class

UpdateMailTest
Tests text of update email.

Namespace

Drupal\Tests\update\Unit

Code

public function testUpdateEmail($notification_threshold, $params, array $expected_body) : void {
  $langcode = 'en';
  $available_updates_url = 'https://example.com/admin/reports/updates';
  $update_settings_url = 'https://example.com/admin/reports/updates/settings';
  $site_name = 'Test site';
  // Initialize update_mail input parameters.
  $key = NULL;
  $message = [
    'langcode' => $langcode,
    'subject' => '',
    'message' => '',
    'body' => [],
  ];
  // Language manager just returns the language.
  $this->languageManager
    ->expects($this->once())
    ->method('getLanguage')
    ->willReturn($langcode);
  // Create three config entities.
  $config_site_name = $this->createMock('Drupal\\Core\\Config\\Config');
  $config_site_name->expects($this->once())
    ->method('get')
    ->with('name')
    ->willReturn($site_name);
  $config_notification = $this->createMock('Drupal\\Core\\Config\\Config');
  $config_notification->expects($this->once())
    ->method('get')
    ->with('notification.threshold')
    ->willReturn($notification_threshold);
  $this->configFactory
    ->expects($this->exactly(2))
    ->method('get')
    ->willReturnMap([
    [
      'system.site',
      $config_site_name,
    ],
    [
      'update.settings',
      $config_notification,
    ],
  ]);
  $this->urlGenerator
    ->expects($this->exactly(2))
    ->method('generateFromRoute')
    ->willReturnMap([
    [
      'update.status',
      [],
      [
        'absolute' => TRUE,
        'language' => $langcode,
      ],
      FALSE,
      $available_updates_url,
    ],
    [
      'update.settings',
      [],
      [
        'absolute' => TRUE,
      ],
      FALSE,
      $update_settings_url,
    ],
  ]);
  // Set the container.
  $this->container
    ->set('language_manager', $this->languageManager);
  $this->container
    ->set('url_generator', $this->urlGenerator);
  $this->container
    ->set('config.factory', $this->configFactory);
  $this->container
    ->set('current_user', $this->currentUser);
  \Drupal::setContainer($this->container);
  // Generate the email message.
  $updateMail = new UpdateHooks();
  $updateMail->mail($key, $message, $params);
  // Confirm the subject.
  $this->assertSame("New release(s) available for {$site_name}", $message['subject']);
  // Confirm each part of the body.
  for ($i = 0; $i < count($expected_body); $i++) {
    $body_part = is_string($message['body'][$i]) ? $message['body'][$i] : $message['body'][$i]->render();
    $this->assertSame($expected_body[$i], $body_part);
  }
}

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