diff --git a/core/modules/rest/src/Plugin/rest/resource/UserPasswordResetResource.php b/core/modules/rest/src/Plugin/rest/resource/UserPasswordResetResource.php index fd13d55..a960fe3 100644 --- a/core/modules/rest/src/Plugin/rest/resource/UserPasswordResetResource.php +++ b/core/modules/rest/src/Plugin/rest/resource/UserPasswordResetResource.php @@ -3,6 +3,8 @@ namespace Drupal\rest\Plugin\rest\resource; use Drupal\Component\Render\FormattableMarkup; +use Drupal\Core\Render\RenderContext; +use Drupal\Core\Render\RendererInterface; use Drupal\Core\Session\AccountInterface; use Drupal\rest\Plugin\ResourceBase; use Drupal\rest\ResourceResponse; @@ -33,6 +35,13 @@ class UserPasswordResetResource extends ResourceBase { protected $userStorage; /** + * The renderer. + * + * @var \Drupal\Core\Render\RendererInterface + */ + protected $renderer; + + /** * Constructs a new UserPasswordResetResource object. * * @param array $configuration @@ -47,11 +56,14 @@ class UserPasswordResetResource extends ResourceBase { * A logger instance. * @param \Drupal\user\UserStorageInterface $user_storage * The user storage. + * @param \Drupal\Core\Render\RendererInterface $renderer + * The renderer. */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, array $serializer_formats, LoggerInterface $logger, UserStorageInterface $user_storage) { + public function __construct(array $configuration, $plugin_id, $plugin_definition, array $serializer_formats, LoggerInterface $logger, UserStorageInterface $user_storage, RendererInterface $renderer) { parent::__construct($configuration, $plugin_id, $plugin_definition, $serializer_formats, $logger); $this->userStorage = $user_storage; + $this->renderer = $renderer; } /** @@ -64,7 +76,8 @@ public static function create(ContainerInterface $container, array $configuratio $plugin_definition, $container->getParameter('serializer.formats'), $container->get('logger.channel.rest'), - $container->get('entity_type.manager')->getStorage('user') + $container->get('entity_type.manager')->getStorage('user'), + $container->get('renderer') ); } @@ -97,7 +110,7 @@ public function post($langcode, $name) { $name = trim($name); if (!$account = $this->loadUserByNameOrEmail($name)) { - throw new BadRequestHttpException(new FormattableMarkup('%name is not recognized as a username or an email address.', ['%name' => $name])); + throw new BadRequestHttpException(sprintf('%s is not recognized as a username or an email address.', $name)); } $mail = $this->userMailNotify($account, $langcode); @@ -124,7 +137,10 @@ public function post($langcode, $name) { * See \Drupal\Core\Mail\MailManagerInterface::mail() for details. */ protected function userMailNotify(AccountInterface $account, $langcode = NULL) { - return _user_mail_notify('password_reset', $account, $langcode); + $render_context = new RenderContext(); + return $this->renderer->executeInRenderContext($render_context, function() use ($account, $langcode) { + return _user_mail_notify('password_reset', $account, $langcode); + }); } /** diff --git a/core/modules/rest/src/Tests/RESTTestBase.php b/core/modules/rest/src/Tests/RESTTestBase.php index f07afd7..ad290a6 100644 --- a/core/modules/rest/src/Tests/RESTTestBase.php +++ b/core/modules/rest/src/Tests/RESTTestBase.php @@ -260,11 +260,13 @@ protected function entityValues($entity_type) { * (Optional) The serialization format, e.g. hal_json. * @param array $auth * (Optional) The list of valid authentication methods. + * @param bool $append + * (Optional) Append to the existing settings. */ - protected function enableService($resource_type, $method = 'GET', $format = NULL, $auth = NULL) { + protected function enableService($resource_type, $method = 'GET', $format = NULL, $auth = NULL, $append = FALSE) { // Enable REST API for this entity type. $config = $this->config('rest.settings'); - $settings = $config->get('resources') ?: []; + $settings = $append ? $config->get('resources') : []; if ($resource_type) { if ($format == NULL) { diff --git a/core/modules/rest/src/Tests/UserLoginTest.php b/core/modules/rest/src/Tests/UserLoginTest.php index 8e6a8f3..b712937 100644 --- a/core/modules/rest/src/Tests/UserLoginTest.php +++ b/core/modules/rest/src/Tests/UserLoginTest.php @@ -24,9 +24,9 @@ public function testLogin() { $auth = $this->defaultAuth; $this->enableService('user_login', 'POST', $format, $auth); - $this->enableService('user_login_status', 'GET', $format, $auth); - $this->enableService('user_logout', 'POST', $format, $auth); - $this->enableService('user_password_reset', 'POST', $format, $auth); + $this->enableService('user_login_status', 'GET', $format, $auth, TRUE); + $this->enableService('user_logout', 'POST', $format, $auth, TRUE); + $this->enableService('user_password_reset', 'POST', $format, $auth, TRUE); $permissions[] = 'restful post user_login'; $account = $this->drupalCreateUser($permissions); @@ -135,12 +135,12 @@ public function testLogin() { $url = Url::fromRoute('rest.user_password_reset.POST', ['name' => $random_name]); $this->httpRequest($url, 'POST', NULL, 'application/json'); $this->assertResponse(400); - $this->assertResponseBody('{"error":"' . new FormattableMarkup('%name is not recognized as a username or an email address.', ['%name' => $random_name]) . '"}'); + $this->assertResponseBody('{"error":"' . sprintf('%s is not recognized as a username or an email address.', $random_name) . '"}'); $url = Url::fromRoute('rest.user_password_reset.POST', ['name' => $name]); $this->httpRequest($url, 'POST', NULL, 'application/json'); $this->assertResponse(200); - $this->assertResponseBody('Further instructions have been sent to your email address.'); + $this->assertResponseBody('"Further instructions have been sent to your email address."'); } }