<?php

/**
 * @file
 * Contains EmailRegistrationTestCase.
 */

class EmailRegistrationTestCase extends DrupalWebTestCase {

  /**
   * Implement getInfo().
   */
  public static function getInfo() {
    return array(
      'name' => 'Email registration.',
      'description' => 'Test the email registration module.',
      'group' => 'Email registration',
    );
  }

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp('email_registration');

    // Allow user registration.
    variable_set('user_register', USER_REGISTER_VISITORS);
    // Configure to allow set password.
    variable_set('user_email_verification', FALSE);
  }

  /**
   * Test various behaviors for anonymous users.
   */
  public function testRegistration() {
    // Try to register a user.
    $name = $this->randomName();
    $pass = $this->randomName(10);
    $register = array(
      'mail' => $name . '@example.com',
      'pass[pass1]' => $pass,
      'pass[pass2]' => $pass,
    );
    $this->drupalPost('/user/register', $register, t('Create new account'));
    $this->drupalLogout();

    $login = array(
      'name' => $name . '@example.com',
      'pass' => $pass,
    );
    $this->drupalPost('user/login', $login, t('Log in'));

    // Get the account to get uid.
    $new_user = user_load_by_mail($name . '@example.com');
    $new_name = $name . '_' . $new_user->uid;

    // Confirm the user was created and logged in with expected username.
    $this->assertTitle($new_name . ' | Drupal', 'User properly created, logged in.');

    // Now try the immediate login.
    $this->drupalLogout();
    $name = $this->randomName();
    $pass = $this->randomName(10);
    $register = array(
      'mail' => $name . '@example.com',
      'pass[pass1]' => $pass,
      'pass[pass2]' => $pass,
    );
    $this->drupalPost('/user/register', $register, t('Create new account'));
    $this->assertRaw('Registration successful. You are now logged in.', 'User properly created, immediately logged in.');

    // Try to login with just username, should succeed by default.
    $this->drupalLogout();

    // User name appended with UID see email_registration_cleanup_username().
    $new_user = user_load_by_mail($name . '@example.com');
    $new_name = $name . '_' . $new_user->uid;
    $login = array(
      'name' => $new_name,
      'pass' => $pass,
    );
    $this->drupalPost('user/login', $login, t('Log in'));

    $this->assertTitle($new_name . ' | Drupal', 'By default, username can log in.');
    $this->drupalLogout();

    // Disallow logins with username and try to login with just username, should fail.
    variable_set('email_registration_login_with_username', FALSE);
    $this->drupalPost('user/login', $login, t('Log in'));
    $this->assertTitle('User account | Drupal', 'When disabled, a user cannot login with just their username.');
  }

}
