You are here

public function UserLimitUserTestCase::testProtectRegisterUserNormal in User Limit 7

Test user registration (anonymous users) and message on register page.

File

./user_limit.test, line 30
Tests for user_limit.module.

Class

UserLimitUserTestCase
Test the functionality of the Honeypot module for an admin user.

Code

public function testProtectRegisterUserNormal() {
  $edit = array();

  // Set up required variables.
  variable_set('user_limit', 2);

  // Only allow two users (uid 1 + one extra).
  variable_set('user_limit_uid1', 1);

  // Count uid 1 towards limit.
  variable_set('user_limit_active', 1);

  // Count only active users towards limit.
  // Allow anonymous users to register without email verification.
  variable_set('user_register', USER_REGISTER_VISITORS);
  variable_set('user_email_verification', FALSE);

  // Make sure message doesn't appear on registration page (default).
  $this
    ->drupalGet('user/register');
  $this
    ->assertNoText('User limit (active/allowed)', 'User limit message hidden.');

  // Enable message and make sure message appears.
  variable_set('user_limit_show_counts', 1);
  $this
    ->drupalGet('user/register');
  $this
    ->assertText('User limit (active/allowed): 1 / 2', 'User limit message displayed.');

  // Set up user/register form and submit it.
  $edit = array();
  $edit['name'] = $name = $this
    ->randomName();
  $edit['mail'] = $mail = $edit['name'] . '@example.com';
  $edit['pass[pass1]'] = $pass = '999999';
  $edit['pass[pass2]'] = $pass;
  $this
    ->drupalPost('user/register', $edit, t('Create new account'));
  $accounts = user_load_multiple(array(), array(
    'name' => $name,
    'mail' => $mail,
  ));
  $new_user = reset($accounts);
  $this
    ->assertText(t('Registration successful. You are now logged in.'), 'User registered successfully.');
  $this
    ->drupalLogout();

  // Visit 'user/register' page again, make sure error message appears.
  $this
    ->drupalGet('user/register');
  $this
    ->assertText(USER_LIMIT_DEFAULT_MESSAGE, 'User limit enforced on registration form.');
  $this
    ->assertNoField('name', '', 'Registration form not displayed when over limit.');

  // De-activate user 2.
  $account = user_load(2);
  $account->status = 0;
  user_save($account);

  // Try registering another user, and make sure it works.
  $edit = array();
  $edit['name'] = $name = $this
    ->randomName();
  $edit['mail'] = $mail = $edit['name'] . '@example.com';
  $edit['pass[pass1]'] = $pass = '999999';
  $edit['pass[pass2]'] = $pass;
  $this
    ->drupalPost('user/register', $edit, t('Create new account'));
  $accounts = user_load_multiple(array(), array(
    'name' => $name,
    'mail' => $mail,
  ));
  $new_user = reset($accounts);
  $this
    ->assertText(t('Registration successful. You are now logged in.'), 'User registered successfully with one deactivated user.');
  $this
    ->drupalLogout();

  // Count all users (active and not) towards limit.
  variable_set('user_limit_active', 0);

  // Make sure current count is now 3 (two active, one not).
  $this
    ->assertTrue(user_limit_count_users() == 3, 'Active user limit works properly.');

  // Visit 'user/register' page again, make sure error message appears.
  $this
    ->drupalGet('user/register');
  $this
    ->assertText(USER_LIMIT_DEFAULT_MESSAGE, 'User limit enforced on registration form.');
}