function UserRegistrationRestTest::testRegisterUser

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

Tests that only anonymous users can register users.

File

core/modules/user/tests/src/Functional/UserRegistrationRestTest.php, line 74

Class

UserRegistrationRestTest
Tests registration of user using REST.

Namespace

Drupal\Tests\user\Functional

Code

public function testRegisterUser() : void {
    $config = $this->config('user.settings');
    // Test out different setting User Registration and Email Verification.
    // Allow visitors to register with no email verification.
    $config->set('register', UserInterface::REGISTER_VISITORS);
    $config->set('verify_mail', 0);
    $config->save();
    $user = $this->registerUser('Palmer.Eldritch');
    $this->assertFalse($user->isBlocked());
    $this->assertNotEmpty($user->getPassword());
    $email_count = count($this->drupalGetMails());
    $this->assertEquals(0, $email_count);
    // Attempt to register without sending a password.
    $response = $this->registerRequest('PhilipK.Dick', FALSE);
    $this->assertResourceErrorResponse(422, "No password provided.", $response);
    // Attempt to register with a password when email verification is on.
    $config->set('register', UserInterface::REGISTER_VISITORS);
    $config->set('verify_mail', 1);
    $config->save();
    $response = $this->registerRequest('UrsulaK.LeGuin');
    $this->assertResourceErrorResponse(422, 'A Password cannot be specified. It will be generated on login.', $response);
    // Allow visitors to register with email verification.
    $config->set('register', UserInterface::REGISTER_VISITORS);
    $config->set('verify_mail', 1);
    $config->save();
    $name = 'Jason.Taverner';
    $user = $this->registerUser($name, FALSE);
    $this->assertNotEmpty($user->getPassword());
    $this->assertFalse($user->isBlocked());
    $this->resetAll();
    $this->assertMailString('body', 'You may now log in by clicking this link', 1);
    // Allow visitors to register with Admin approval and no email verification.
    $config->set('register', UserInterface::REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL);
    $config->set('verify_mail', 0);
    $config->save();
    $name = 'Alex';
    $user = $this->registerUser($name);
    $this->resetAll();
    $this->assertNotEmpty($user->getPassword());
    $this->assertTrue($user->isBlocked());
    $this->assertMailString('body', 'Your application for an account is', 2);
    $this->assertMailString('body', 'Alex has applied for an account', 2);
    // Allow visitors to register with Admin approval and email verification.
    $config->set('register', UserInterface::REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL);
    $config->set('verify_mail', 1);
    $config->save();
    $name = 'PhilipK.Dick';
    $user = $this->registerUser($name, FALSE);
    $this->resetAll();
    $this->assertNotEmpty($user->getPassword());
    $this->assertTrue($user->isBlocked());
    $this->assertMailString('body', 'Your application for an account is', 2);
    $this->assertMailString('body', 'PhilipK.Dick has applied for an account', 2);
    // Verify that an authenticated user cannot register a new user, despite
    // being granted permission to do so because only anonymous users can
    // register themselves, authenticated users with the necessary permissions
    // can POST a new user to the "user" REST resource.
    $this->initAuthentication();
    $response = $this->registerRequest($this->account
        ->getAccountName());
    $this->assertResourceErrorResponse(403, "Only anonymous users can register a user.", $response);
}

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