<?php

/**
 * @file
 * Tests for Legal module.
 */

/**
 * Legal module base test class.
 */
class LegalTestCase extends DrupalWebTestCase {
  function setUp() {
    // Enable any modules required for the test.
    parent::setUp('legal');
  }

  /**
   * Set module settings. This can only be called once from each test case
   * instance.
   */
  function setSettings($edit) {
    // Had to move to creating an user for every change in settings, since
    // creating this in setUp() was not working.
    $admin_user = $this->drupalCreateUser(array('administer Terms and Conditions'));

    $this->drupalLogin($admin_user);

    $this->drupalPost('admin/config/people/legal', $edit, t('Save'));

    // Check account wasn't created.
    $this->assertText(t('Terms &amp; Conditions have been saved.'), 'T&C text setup correctly');

    // Log user out.
    $this->drupalLogout();
  }
}

/**
 * Test registering as new user and creating an account.
 */
class LegalRegisterTestCase extends LegalTestCase {

  function getInfo() {
    return array(
      'name' => 'Create Account',
      'description' => 'Register as new user and create account.',
      'group' => 'Legal',
    );
  }

  function setUp() {
    parent::setUp();

    // Set basic module settings.
    $conditions = $this->randomName();
    $edit = array(
      'conditions' => $conditions,
    );
    $this->setSettings($edit);
  }

  /**
   * Accept T&C to successfully create an account.
   */
  function testRegisterSuccessful() {
    // Prepare a user to do testing.
    $name = $this->randomName();
    $mail = "$name@example.com";
    $edit = array(
      'name' => $name,
      'mail' => $mail,
      'legal_accept' => TRUE,
    );

    $this->drupalPost('user/register', $edit, t('Create new account'));

     // Check account was created.
    $this->assertText(t('Thank you for applying for an account. Your account is currently pending approval by the site administrator.'), 'Account created');
  }

  /**
   * Don't accept T&C no account created.
   */
  function testRegisterUnsuccessful() {
    // Prepare a user to do testing.
    $name = $this->randomName();
    $mail = "$name@example.com";
    $edit = array(
      'name' => $name,
      'mail' => $mail,
    );

    $this->drupalPost('user/register', $edit, t('Create new account'));

     // Check account wasn't created.
    $this->assertText(t('Accept Terms &amp; Conditions of Use field is required.'), 'T&C not accepted, account not created');
  }

}

/**
 * Test registering as new user and creating an account.
 */
class LegalPasswordResetTestCase extends LegalTestCase {

  function getInfo() {
    return array(
      'name' => 'Password reset',
      'description' => 'Password reset workflow when T&Cs need to be accepted.',
      'group' => 'Legal',
    );
  }

  function setUp() {
    parent::setUp();

    // Set basic module settings.
    $conditions = $this->randomName();
    $edit = array(
      'conditions' => $conditions,
    );
    $this->setSettings($edit);

    // Create an ordinary user.
    $this->user = $this->drupalCreateUser();
  }

  /**
   * Accept T&C to successfully create an account.
   */
  function testPasswordReset() {
    // Create a log in link for the user, and go to that URL.
    // Borrowed from testUserPasswordResetExpired().
    $timestamp = REQUEST_TIME;
    $this->drupalGet("user/reset/{$this->user->uid}/$timestamp/" . user_pass_rehash($this->user->pass, $timestamp, $this->user->login));

    $this->assertText(t('Reset password'), "The reset password form is shown.");

    // Use the one-time login link.
    $this->drupalPost(NULL, array(), t('Log in'));

    $this->assertTitle(t('Terms and Conditions | Drupal'), "The user is redirected to the terms and conditions approval page.");
    $this->assertText(t('Terms and Conditions'), "The Terms and Conditions form is shown.");
    // assertText() doesn't handle an '&' properly.
    $this->assertText(t('To continue to use this site please read the Terms &amp; Conditions below'), "The Terms and Conditions form is shown.");

    $edit = array(
      'legal_accept' => TRUE,
    );

    // Accept the T&Cs.
    $this->drupalPost(NULL, $edit, t('Confirm'));

    // Don't use assertUrl() as that requires us to match up the tokens in the
    // query too.
    $url = $this->getUrl();
    $path = parse_url($url, PHP_URL_PATH);
    $expected_path = url("user/{$this->user->uid}/edit");
    $this->assertEqual($path, $expected_path, "The user is redirected to the user edit page.");

    $this->assertText(t("You have just used your one-time login link. It is no longer necessary to use this link to log in. Please change your password."), "The user is shown the message to reset their password.");
  }

}

/**
 * Test Scroll Box Display.
 */
class LegalScrollBoxDisplayTestCase extends LegalTestCase {
  var $conditions;

  function getInfo() {
    return array(
      'name' => 'Scroll Box Display',
      'description' => 'Change display option to Scroll Box and check if display is behaves correctly.',
      'group' => 'Legal',
    );
  }

  function setUp() {
    parent::setUp();

    // Set basic module settings.
    $this->conditions = $this->randomName();
    $edit = array(
      'conditions' => $this->conditions,
      'display' => 0,
    );
    $this->setSettings($edit);
  }

  /**
   * Test "Scroll Box" display.
   */
  function testScrollBoxDisplay() {
    $this->drupalGet('user/register');

     // Check presence of T&C text.
    $this->assertText($this->conditions, 'T&C text displayed');
  }
}

/**
 * Test Scroll Box CSS Display.
 */
class LegalScrollBoxCSSDisplayTestCase extends LegalTestCase {
  var $conditions;

  function getInfo() {
    return array(
      'name' => 'Scroll Box CSS Display',
      'description' => 'Change display option to Scroll Box CSS and check if display is behaves correctly.',
      'group' => 'Legal',
    );
  }

  function setUp() {
    parent::setUp();

    // Set basic module settings.
    $this->conditions = $this->randomName();
    $edit = array(
      'conditions' => $this->conditions,
      'display' => 1,
    );
    $this->setSettings($edit);
  }

  /**
   * Test "Scroll Box CSS" display.
   */
  function testScrollBoxCSSDisplay() {
    $this->drupalGet('user/register');

     // Check presence of T&C text.
    $this->assertText($this->conditions, 'T&C text displayed');
  }
}

/**
 * Test HTML Text display.
 */
class LegalHTMLTextDisplayTestCase extends LegalTestCase {
  var $conditions;

  function getInfo() {
    return array(
      'name' => 'HTML Text display',
      'description' => 'Change display option to HTML Text and check if display is behaves correctly.',
      'group' => 'Legal',
    );
  }

  function setUp() {
    parent::setUp();

    // Set basic module settings.
    $this->conditions = $this->randomName();
    $edit = array(
      'conditions' => $this->conditions,
      'display' => 2,
    );
    $this->setSettings($edit);
  }

  /**
   * Test "HTML Text" display.
   */
  function testHTMLTextDisplay() {
    $this->drupalGet('user/register');

     // Check presence of T&C text.
    $this->assertText($this->conditions, 'T&C text displayed');
  }
}

/**
 * Test "Page Link" display.
 */
class LegalLinkDisplayTestCase extends LegalTestCase {
  var $conditions;

  function getInfo() {
    return array(
      'name' => 'Page Link display',
      'description' => 'Change display option to Page Link and check if display is behaves correctly.',
      'group' => 'Legal',
    );
  }

  function setUp() {
    parent::setUp();

    // Set basic module settings.
    $this->conditions = $this->randomName();
    $edit = array(
      'conditions' => $this->conditions,
      'display' => 3,
    );
    $this->setSettings($edit);
  }

  /**
   * Test "Page Link" display.
   */
  function testPageLinkDisplay() {
    $this->drupalGet('user/register');

     // Check presence of T&C link.
    $this->assertLink('Terms & Conditions', 0, 'T&C link found');
  }

}
