function UserRegistrationRestTest::testRegisterUser

Same name in other branches
  1. 10 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 72

Class

UserRegistrationRestTest
Tests registration of user using REST.

Namespace

Drupal\Tests\user\Functional

Code

public function testRegisterUser() {
    $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('Rick.Deckard', FALSE);
    $this->assertResourceErrorResponse(422, "No password provided.", $response);
    // Attempt to register with a password when e-mail verification is on.
    $config->set('register', UserInterface::REGISTER_VISITORS);
    $config->set('verify_mail', 1);
    $config->save();
    $response = $this->registerRequest('Estraven');
    $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->assertEmpty($user->getPassword());
    $this->assertTrue($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 = 'Argaven';
    $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', 'Argaven has applied for an account', 2);
    // Allow visitors to register with Admin approval and e-mail verification.
    $config->set('register', UserInterface::REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL);
    $config->set('verify_mail', 1);
    $config->save();
    $name = 'Bob.Arctor';
    $user = $this->registerUser($name, FALSE);
    $this->resetAll();
    $this->assertEmpty($user->getPassword());
    $this->assertTrue($user->isBlocked());
    $this->assertMailString('body', 'Your application for an account is', 2);
    $this->assertMailString('body', 'Bob.Arctor 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.