Skip to content
Snippets Groups Projects
Commit 1abfe54b authored by Theresa Grannum's avatar Theresa Grannum Committed by Ted Bowman
Browse files

Issue #3299101 by Theresa.Grannum, phenaproxima, tedbow: For cron update email...

Issue #3299101 by Theresa.Grannum, phenaproxima, tedbow: For cron update email use the user's preferred language if available
parent ae5ae0c4
No related branches found
No related tags found
No related merge requests found
...@@ -38,15 +38,20 @@ function automatic_updates_help($route_name, RouteMatchInterface $route_match) { ...@@ -38,15 +38,20 @@ function automatic_updates_help($route_name, RouteMatchInterface $route_match) {
* Implements hook_mail(). * Implements hook_mail().
*/ */
function automatic_updates_mail(string $key, array &$message, array $params): void { function automatic_updates_mail(string $key, array &$message, array $params): void {
// Explicitly pass the language code to all translated strings.
$options = [
'langcode' => $message['langcode'],
];
if ($key === 'cron_successful') { if ($key === 'cron_successful') {
$message['subject'] = t("Drupal core was successfully updated"); $message['subject'] = t("Drupal core was successfully updated", [], $options);
$message['body'][] = t('Congratulations!'); $message['body'][] = t('Congratulations!', [], $options);
$message['body'][] = t('Drupal core was automatically updated from @previous_version to @updated_version.', [ $message['body'][] = t('Drupal core was automatically updated from @previous_version to @updated_version.', [
'@previous_version' => $params['previous_version'], '@previous_version' => $params['previous_version'],
'@updated_version' => $params['updated_version'], '@updated_version' => $params['updated_version'],
]); ], $options);
$message['body'][] = t('This e-mail was sent by the Automatic Updates module. Unattended updates are not yet fully supported.'); $message['body'][] = t('This e-mail was sent by the Automatic Updates module. Unattended updates are not yet fully supported.', [], $options);
$message['body'][] = t('If you are using this feature in production, it is strongly recommended for you to visit your site and ensure that everything still looks good.'); $message['body'][] = t('If you are using this feature in production, it is strongly recommended for you to visit your site and ensure that everything still looks good.', [], $options);
} }
} }
......
...@@ -278,7 +278,7 @@ class CronUpdater extends Updater { ...@@ -278,7 +278,7 @@ class CronUpdater extends Updater {
$recipients = $this->configFactory->get('update.settings') $recipients = $this->configFactory->get('update.settings')
->get('notification.emails'); ->get('notification.emails');
foreach ($recipients as $recipient) { foreach ($recipients as $recipient) {
$this->mailManager->mail('automatic_updates', 'cron_successful', $recipient, $this->languageManager->getDefaultLanguage()->getId(), $mail_params); $this->mailManager->mail('automatic_updates', 'cron_successful', $recipient, $this->getEmailLangcode($recipient), $mail_params);
} }
} }
catch (\Throwable $e) { catch (\Throwable $e) {
...@@ -299,6 +299,23 @@ class CronUpdater extends Updater { ...@@ -299,6 +299,23 @@ class CronUpdater extends Updater {
return new Response(); return new Response();
} }
/**
* Retrieves preferred language to send email.
*
* @param string $recipient
* The email address of the recipient.
*
* @return string
* The preferred language of the recipient.
*/
protected function getEmailLangcode(string $recipient): string {
$user = user_load_by_mail($recipient);
if ($user) {
return $user->getPreferredLangcode();
}
return $this->languageManager->getDefaultLanguage()->getId();
}
/** /**
* Gets the cron update mode. * Gets the cron update mode.
* *
......
<?php
/**
* @file
* Contains hook implementations for testing Automatic Updates.
*/
use Drupal\Core\StringTranslation\TranslatableMarkup;
/**
* Implements hook_mail_alter().
*/
function automatic_updates_test_mail_alter(array &$message): void {
if (str_starts_with($message['id'], 'automatic_updates_')) {
$line_langcodes = [];
// Get the langcode of every translated line in the mssage, including the
// subject line.
$lines = array_merge($message['body'], [
$message['subject'],
]);
foreach ($lines as $line) {
if (!($line instanceof TranslatableMarkup)) {
// All lines need to be translated.
return;
}
$line_langcodes[] = $line->getOption('langcode');
}
$message['line_langcodes'] = array_unique($line_langcodes);
}
}
...@@ -19,6 +19,7 @@ use Drupal\package_manager\Event\PreCreateEvent; ...@@ -19,6 +19,7 @@ use Drupal\package_manager\Event\PreCreateEvent;
use Drupal\package_manager\Exception\StageValidationException; use Drupal\package_manager\Exception\StageValidationException;
use Drupal\package_manager\ValidationResult; use Drupal\package_manager\ValidationResult;
use Drupal\Tests\package_manager\Traits\PackageManagerBypassTestTrait; use Drupal\Tests\package_manager\Traits\PackageManagerBypassTestTrait;
use Drupal\Tests\user\Traits\UserCreationTrait;
use Drupal\update\UpdateSettingsForm; use Drupal\update\UpdateSettingsForm;
use Psr\Log\Test\TestLogger; use Psr\Log\Test\TestLogger;
use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface;
...@@ -33,6 +34,7 @@ class CronUpdaterTest extends AutomaticUpdatesKernelTestBase { ...@@ -33,6 +34,7 @@ class CronUpdaterTest extends AutomaticUpdatesKernelTestBase {
use AssertMailTrait; use AssertMailTrait;
use PackageManagerBypassTestTrait; use PackageManagerBypassTestTrait;
use UserCreationTrait;
/** /**
* {@inheritdoc} * {@inheritdoc}
...@@ -60,6 +62,8 @@ class CronUpdaterTest extends AutomaticUpdatesKernelTestBase { ...@@ -60,6 +62,8 @@ class CronUpdaterTest extends AutomaticUpdatesKernelTestBase {
$this->container->get('logger.factory') $this->container->get('logger.factory')
->get('automatic_updates') ->get('automatic_updates')
->addLogger($this->logger); ->addLogger($this->logger);
$this->installEntitySchema('user');
$this->installSchema('user', ['users_data']);
} }
/** /**
...@@ -348,18 +352,48 @@ class CronUpdaterTest extends AutomaticUpdatesKernelTestBase { ...@@ -348,18 +352,48 @@ class CronUpdaterTest extends AutomaticUpdatesKernelTestBase {
* Tests that user 1 is emailed when an unattended update succeeds. * Tests that user 1 is emailed when an unattended update succeeds.
*/ */
public function testEmailOnSuccess(): void { public function testEmailOnSuccess(): void {
$default_language = $this->container->get('language_manager')
->getDefaultLanguage()
->getId();
$this->assertNotSame('fr', $default_language);
$account = $this->createUser([], NULL, FALSE, [
'preferred_langcode' => 'fr',
]);
$recipients = [
'emissary@deep.space',
$account->getEmail(),
];
$languages = [
$default_language,
$account->getPreferredLangcode(),
];
$this->config('update.settings') $this->config('update.settings')
->set('notification.emails', [ ->set('notification.emails', $recipients)
'emissary@deep.space',
])
->save(); ->save();
$this->container->get('cron')->run(); $this->container->get('cron')->run();
// Check that we actually sent a success email to the right person. // Ensure we sent a success message to all recipients.
$this->assertMail('to', 'emissary@deep.space'); $sent_messages = $this->getMails([
$this->assertMail('subject', "Drupal core was successfully updated"); 'subject' => "Drupal core was successfully updated",
$this->assertMailString('body', "Congratulations!\n\nDrupal core was automatically updated from 9.8.0 to 9.8.1.\n", 1); ]);
$this->assertSame(count($recipients), count($sent_messages));
// Ensure the messages had the correct body text, and were sent to the right
// people.
foreach ($sent_messages as $i => $message) {
$this->assertSame($message['to'], $recipients[$i]);
$this->assertStringStartsWith("Congratulations!\n\nDrupal core was automatically updated from 9.8.0 to 9.8.1.\n", $message['body']);
// The message, and every line in it, should have been sent in the
// expected language.
$langcode = $message['langcode'];
$this->assertSame($langcode, $languages[$i]);
// @see automatic_updates_test_mail_alter()
$this->assertArrayHasKey('line_langcodes', $message);
$this->assertSame([$langcode], $message['line_langcodes']);
}
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment