function UserAdminTest::testNotificationEmailAddress

Same name in other branches
  1. 9 core/modules/user/tests/src/Functional/UserAdminTest.php \Drupal\Tests\user\Functional\UserAdminTest::testNotificationEmailAddress()
  2. 10 core/modules/user/tests/src/Functional/UserAdminTest.php \Drupal\Tests\user\Functional\UserAdminTest::testNotificationEmailAddress()
  3. 11.x core/modules/user/tests/src/Functional/UserAdminTest.php \Drupal\Tests\user\Functional\UserAdminTest::testNotificationEmailAddress()

Tests the alternate notification email address for user mails.

File

core/modules/user/tests/src/Functional/UserAdminTest.php, line 163

Class

UserAdminTest
Tests user administration page functionality.

Namespace

Drupal\Tests\user\Functional

Code

public function testNotificationEmailAddress() {
    // Test that the Notification Email address field is on the config page.
    $admin_user = $this->drupalCreateUser([
        'administer users',
        'administer account settings',
    ]);
    $this->drupalLogin($admin_user);
    $this->drupalGet('admin/config/people/accounts');
    $this->assertRaw('id="edit-mail-notification-address"', 'Notification Email address field exists');
    $this->drupalLogout();
    // Test custom user registration approval email address(es).
    $config = $this->config('user.settings');
    // Allow users to register with admin approval.
    $config->set('verify_mail', TRUE)
        ->set('register', UserInterface::REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL)
        ->save();
    // Set the site and notification email addresses.
    $system = $this->config('system.site');
    $server_address = $this->randomMachineName() . '@example.com';
    $notify_address = $this->randomMachineName() . '@example.com';
    $system->set('mail', $server_address)
        ->set('mail_notification', $notify_address)
        ->save();
    // Register a new user account.
    $edit = [];
    $edit['name'] = $this->randomMachineName();
    $edit['mail'] = $edit['name'] . '@example.com';
    $this->drupalPostForm('user/register', $edit, t('Create new account'));
    $subject = 'Account details for ' . $edit['name'] . ' at ' . $system->get('name') . ' (pending admin approval)';
    // Ensure that admin notification mail is sent to the configured
    // Notification Email address.
    $admin_mail = $this->drupalGetMails([
        'to' => $notify_address,
        'from' => $server_address,
        'subject' => $subject,
    ]);
    $this->assertCount(1, $admin_mail, 'New user mail to admin is sent to configured Notification Email address');
    // Ensure that user notification mail is sent from the configured
    // Notification Email address.
    $user_mail = $this->drupalGetMails([
        'to' => $edit['mail'],
        'from' => $server_address,
        'reply-to' => $notify_address,
        'subject' => $subject,
    ]);
    $this->assertCount(1, $user_mail, 'New user mail to user is sent from configured Notification Email address');
}

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