You are here

site_disclaimer.test in Site Disclaimer 7

Same filename and directory in other branches
  1. 6 tests/site_disclaimer.test

Site Disclaimer module functionality tests.

File

tests/site_disclaimer.test
View source
<?php

/**
 * @file
 * Site Disclaimer module functionality tests.
 */
define('TEST_SITE_DISCLAIMER_SETTINGS_PATH', 'admin/config/people/site_disclaimer');

/**
 * Base class for all terms od use web test cases.
 */
class SiteDisclaimerWebTestCase extends DrupalWebTestCase {
  protected $admin_user;
  protected $node;
  protected $node1;

  // Node linked from the $node
  protected $label;
  protected $label_link;
  protected $label_link_titles;
  protected $title;
  protected $body;
  protected $body1;
  protected $xss;
  public function setUp() {

    // String for XSS Test.
    $this->label = $this
      ->testXssString();
    $this->label_link = $this
      ->testXssString() . ' @link ' . $this
      ->testXssString();
    $this->title = $this
      ->testXssString();
    $this->body1 = $this
      ->randomName();

    // Enable the Site Disclaimer module.
    parent::setUp('site_disclaimer');

    // Don't require e-mail verification.
    variable_set('user_email_verification', FALSE);

    // Allow registration by site visitors without administrator approval.
    variable_set('user_register', USER_REGISTER_VISITORS);

    // Disable clean URLs (on qa.d.o they are disabled!).

    //variable_set('clean_url', 0);
    $this->admin_user = $this
      ->drupalCreateUser(array(
      'administer site disclaimer',
    ));
    $this
      ->drupalLogin($this->admin_user);

    // A node linked from default Site Disclaimer node.
    $this->node1 = $this
      ->drupalCreateNode(array(
      'type' => 'page',
      'body' => array(
        LANGUAGE_NONE => array(
          array(
            'value' => $this->body1,
          ),
        ),
      ),
    ));

    // A label linking to node1 by title
    $this->label_link_titles = $this
      ->testXssString() . ' @"' . $this->node1->title . '" ';

    // Default Site Disclaimer node (contains a link to node1)
    // We are using 'absolute' => TRUE to avoid Drupal problems in dealing with
    // content links when installation is in webserver's subdirectory, i.e. http://example.com/drupal/...
    $this->body = $this
      ->randomName() . ' ' . l($this->node1->title, 'node/' . $this->node1->nid, array(
      'absolute' => TRUE,
    )) . ' ' . $this
      ->randomName();
    $this->node = $this
      ->drupalCreateNode(array(
      'type' => 'page',
      'body' => array(
        LANGUAGE_NONE => array(
          array(
            'value' => $this->body,
          ),
        ),
      ),
    ));
  }
  private function testXssString() {
    return '<a href="/' . $this
      ->randomString() . '">' . $this
      ->randomString() . '</a><script>alert("' . $this
      ->randomString() . '");</script>';
  }

}

/**
 * Test for all simple settings.
 */
class SiteDisclaimerSettingsTestCase extends SiteDisclaimerWebTestCase {
  public static function getInfo() {
    return array(
      'name' => 'Site Disclaimer Basic',
      'description' => 'Basic functionality tests.',
      'group' => 'Site Disclaimer',
    );
  }
  public function testSiteDisclaimerBasic() {

    // Test basic settings
    $edit = array(
      'site_disclaimer_node_title' => $this->node->title,
      'site_disclaimer_checkbox_label' => $this->label,
      'site_disclaimer_title' => $this->title,
      'site_disclaimer_version_details' => $this
        ->randomName(),
    );
    $this
      ->drupalPost(TEST_SITE_DISCLAIMER_SETTINGS_PATH, $edit, t('Save configuration'));
    $this
      ->assertFieldByName('site_disclaimer_node_title', $edit['site_disclaimer_node_title'], 'Node set.');
    $this
      ->assertFieldByName('site_disclaimer_checkbox_label', $edit['site_disclaimer_checkbox_label'], 'Label name set.');
    $this
      ->assertFieldByName('site_disclaimer_title', $edit['site_disclaimer_title'], 'Title set.');
    $this
      ->drupalLogout();

    // Test registration page contains Site Disclaimer with basic settings
    $this
      ->drupalGet('user/register');
    $this
      ->assertRaw(filter_xss_admin($edit['site_disclaimer_checkbox_label']), 'Checkbox label found.');
    $this
      ->assertRaw(check_plain($edit['site_disclaimer_title']), 'Title found.');
    $this
      ->assertRaw($this->body, 'Terms node body text found.');
    $this
      ->assertNoRaw(filter_xss_admin($edit['site_disclaimer_version_details']), 'Site Disclaimer version details not found');

    // Test registration requires the [x] checkbox
    $this
      ->drupalPost('user/register', array(), t('Create new account'));
    $this
      ->assertRaw(t('!name field is required.', array(
      '!name' => filter_xss_admin($edit['site_disclaimer_checkbox_label']),
    )), 'Checkbox label found, required.');

    //D6    $this->assertRaw(t('You must agree with the !terms to get an account.', array('!terms' => check_plain($edit['site_disclaimer_title']))), 'Checkbox lable found, required.');

    // Test successful registration
    $edit = array();
    $edit['name'] = $name = $this
      ->randomName();
    $edit['mail'] = $mail = $this
      ->randomName() . '@example.com';
    $edit['I_agree'] = TRUE;
    $edit['pass[pass1]'] = $pass = $this
      ->randomName();
    $edit['pass[pass2]'] = $pass;
    $this
      ->drupalPost('user/register', $edit, t('Create new account'));

    //    $this->assertText('Thank you for applying for an account.', 'Account creation successfull.');
    $this
      ->assertText(t('Registration successful. You are now logged in.'), t('Users are logged in after registering.'));
    $this
      ->drupalLogout();

    // Test successful login after registration (no Site Disclaimer)
    $auth = array(
      'name' => $name,
      'pass' => $pass,
    );

    // Login after administrator approval.
    $this
      ->drupalPost('user/login', $auth, t('Log in'));
    $this
      ->assertText(t('Member for'), t('User can log in.'));
    $this
      ->drupalLogout();

    // Test settings with link from [x] checkbox label
    $this
      ->drupalLogin($this->admin_user);
    $edit = array(
      'site_disclaimer_checkbox_label' => $this->label_link,
    );
    $label_raw = str_replace('@link', l($this->node->title, 'node/' . $this->node->nid), filter_xss_admin($edit['site_disclaimer_checkbox_label']));
    $this
      ->drupalPost(TEST_SITE_DISCLAIMER_SETTINGS_PATH, $edit, t('Save configuration'));
    $this
      ->assertFieldByName('site_disclaimer_checkbox_label', $edit['site_disclaimer_checkbox_label'], 'Label name with @link set.');
    $this
      ->drupalLogout();

    // Test registration page contains [x] checkbox label but no node shown
    $this
      ->drupalGet('user/register');
    $this
      ->assertRaw($label_raw, 'Checkbox label found.');
    $this
      ->assertNoRaw($this->body, 'Terms node body text not found.');

    // Test Site Disclaimer version update
    $this
      ->drupalLogin($this->admin_user);
    $edit = array(
      'site_disclaimer_checkbox_label' => $this->label_link,
      'site_disclaimer_version' => 2,
      'site_disclaimer_version_details' => $this
        ->randomName(),
    );
    $label_raw = str_replace('@link', l($this->node->title, 'node/' . $this->node->nid), filter_xss_admin($edit['site_disclaimer_checkbox_label']));
    $this
      ->drupalPost(TEST_SITE_DISCLAIMER_SETTINGS_PATH, $edit, t('Save configuration'));
    $this
      ->assertFieldByName('site_disclaimer_checkbox_label', $edit['site_disclaimer_checkbox_label'], 'Label name with @link set.');

    //debug('Inspect base_url=' . print_r($GLOBALS['base_url'], 1) . ' base_root=' . print_r($GLOBALS['base_root'], 1) );

    //debug('Inspect node->body=' . htmlentities(print_r($this->body, 1)));
    debug('Inspect site_disclaimer_allow_nodes=' . print_r(variable_get('site_disclaimer_allow_nodes', array()), 1));
    $this
      ->drupalLogout();

    // Test that a request is shown to accept new version of Site Disclaimer when an old user logs in
    $auth = array(
      'name' => $name,
      'pass' => $pass,
    );
    $this
      ->drupalPost('user/login', $auth, t('Log in'));
    $this
      ->assertNoText(t('Member for'), t('User log in incomplete.'));
    $this
      ->assertRaw(filter_xss_admin($edit['site_disclaimer_version_details']), 'Site Disclaimer version details found');
    $this
      ->assertRaw($label_raw, 'Checkbox label found.');

    // Test that user can open the linked node
    $this
      ->clickLink($this->node->title);
    $this
      ->assertRaw($this->body, 'Site Disclaimer node found');

    // Test that user can also open the node linked from Site Disclaimer node
    $this
      ->clickLink($this->node1->title);
    $this
      ->assertRaw($this->body1, 'node linked from Site Disclaimer node found');

    // Test that other pages are redirected to the Site Disclaimer page
    $this
      ->drupalGet('');
    $this
      ->assertRaw(filter_xss_admin($edit['site_disclaimer_version_details']), 'Site Disclaimer version details found');
    $this
      ->assertRaw($label_raw, 'Checkbox label found.');

    // Test accepting the new Site Disclaimer
    $edit = array(
      'I_agree' => TRUE,
    );
    $this
      ->drupalPost(NULL, $edit, t('Continue'));
    $this
      ->assertText(t('Log out'), t('User can log in.'));
    $this
      ->assertText(t('My account'), t('User can log in.'));
    $this
      ->drupalLogout();

    //@todo:

    // Test settings with quoted links from [x] checkbox label
  }

}

Constants

Namesort descending Description
TEST_SITE_DISCLAIMER_SETTINGS_PATH @file Site Disclaimer module functionality tests.

Classes

Namesort descending Description
SiteDisclaimerSettingsTestCase Test for all simple settings.
SiteDisclaimerWebTestCase Base class for all terms od use web test cases.