function NotificationHandlerTest::testUserMailsWithoutAccountEmail

Same name and namespace in other branches
  1. 11.x core/modules/user/tests/src/Kernel/NotificationHandlerTest.php \Drupal\Tests\user\Kernel\NotificationHandlerTest::testUserMailsWithoutAccountEmail()

Tests mails are not sent when the account has no email address.

Attributes

#[DataProvider('userMailsProvider')]

Parameters

string $method: The method to call on the mail handler.

string $configKey: The config key to test.

array $emailKeys: The mail keys to test for.

File

core/modules/user/tests/src/Kernel/NotificationHandlerTest.php, line 143

Class

NotificationHandlerTest
Tests NotificationHandler use of user.settings.notify.*.

Namespace

Drupal\Tests\user\Kernel

Code

public function testUserMailsWithoutAccountEmail(string $method, string $configKey, array $emailKeys) : void {
  $this->installConfig('user');
  $this->config('system.site')
    ->set('mail', 'test@example.com')
    ->save();
  $this->config('user.settings')
    ->set($configKey, TRUE)
    ->save();
  $logger = $this->createMock(LoggerInterface::class);
  $logger->expects($this->once())
    ->method('log');
  /** @var \Drupal\Core\Logger\LoggerChannelFactory $logger_factory */
  $logger_factory = $this->container
    ->get('logger.factory');
  $logger_factory->get('user')
    ->addLogger($logger);
  $notificationHandler = \Drupal::service(NotificationHandler::class);
  $return = $notificationHandler->{$method}($this->createUser([], NULL, FALSE, [
    'mail' => NULL,
  ]));
  $this->assertFalse($return);
  if ($configKey == 'notify.register_pending_approval') {
    // The register_pending_approval op will cause an email to be sent to the
    // site address.
    $this->assertCount(1, $this->getMails());
  }
  else {
    $this->assertEmpty($this->getMails());
  }
}

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