You are here

access_filter.test in Access Filter 7

File

access_filter.test
View source
<?php

/**
 * @file
 * Tests for access_filter.module
 */
class AccessFilterTestCase extends DrupalWebTestCase {
  protected $privilegedUser;
  protected $normalUser;

  /**
   * Give display information to the SimpleTest system.
   */
  public static function getInfo() {
    return array(
      'name' => 'Standard operations',
      'description' => 'Tests that add, edit and delete operations.',
      'group' => 'Access Filter',
    );
  }

  /**
   * Set up the test environment.
   *
   * @see DrupalWebTestCase::setUp()
   */
  public function setUp() {
    parent::setUp('access_filter');
    $this->privilegedUser = $this
      ->drupalCreateUser(array(
      'administer access filter rules',
    ));
    $this->normalUser = $this
      ->drupalCreateUser();
  }

  /**
   * Test standard operations.
   */
  public function testOperations() {

    // Test user permissions.
    $this
      ->drupalLogin($this->normalUser);
    $this
      ->drupalGet('admin/config/people/access_filter');
    $this
      ->assertResponse(403);
    $this
      ->drupalLogin($this->privilegedUser);
    $this
      ->drupalGet('admin/config/people/access_filter');
    $this
      ->assertResponse(200);

    // Create a filter.
    $edit = array();
    $edit['name'] = $this
      ->randomName(16);
    $edit['status'] = 1;
    $edit['paths'] = 'D:node/1';
    $edit['rules'] = 'A:*';
    $this
      ->drupalPost('admin/config/people/access_filter/add', $edit, t('Save'));
    $this
      ->assertText($edit['name']);

    // Load the created filter.
    $filter = $this
      ->loadFilterByName($edit['name']);
    $this
      ->assertTrue($filter, 'Access filter has created surely.');

    // Edit the filter.
    $edit['rules'] = 'D:*';
    $this
      ->drupalPost('admin/config/people/access_filter/' . $filter->fid . '/edit', $edit, t('Save'));
    $this
      ->assertText($edit['name']);

    // Delete the filter.
    $this
      ->drupalPost('admin/config/people/access_filter/' . $filter->fid . '/delete', array(), t('Delete'));
    $this
      ->assertText($edit['name']);
    $this
      ->assertFalse($this
      ->loadFilterByName($edit['name']), 'Access filter has been deleted surely.');
  }

  /**
   * Load an access filter by name.
   *
   * @param string $name
   *   A string of name.
   *
   * @return object
   *   An object of access filter.
   */
  protected function loadFilterByName($name) {
    return db_query('SELECT * FROM {access_filter} WHERE name = :name', array(
      ':name' => $name,
    ))
      ->fetchObject();
  }

}
class AccessFilterResrictionTestCase extends DrupalWebTestCase {
  protected $user;

  /**
   * Give display information to the SimpleTest system.
   */
  public static function getInfo() {
    return array(
      'name' => 'Restriction functions',
      'description' => 'Tests that access filter restriction functions.',
      'group' => 'Access Filter',
    );
  }

  /**
   * Set up the test environment.
   *
   * @see DrupalWebTestCase::setUp()
   */
  public function setUp() {
    parent::setUp('access_filter');
    $this->user = $this
      ->drupalCreateUser(array(
      'access content',
    ));
    $this
      ->drupalLogin($this->user);
  }

  /**
   * Test IP address matching functions.
   */
  public function testIpAddressMatching() {
    $ip = '192.168.0.10';
    $this
      ->assertIpAddressMatching('192.168.0.10', $ip);
    $this
      ->assertIpAddressMatching('192.168.0.0-192.168.0.20', $ip);
    $this
      ->assertIpAddressMatching('192.168.0.0/24', $ip);
    $this
      ->assertIpAddressMatching('10.20.0.10', $ip, FALSE);
    $this
      ->assertIpAddressMatching('10.20.0.0-10.20.0.20', $ip, FALSE);
    $this
      ->assertIpAddressMatching('10.20.0.0/24', $ip, FALSE);
  }

  /**
   * Check IP address matching works correctly.
   *
   * @param string $pattern
   *   A string of pattern.
   * @param string $ip
   *   A string of IP address.
   * @param bool $status
   *   Boolean of desired result.
   *
   * @return bool
   *   TRUE if the assertion succeeded, FALSE otherwise.
   */
  protected function assertIpAddressMatching($pattern, $ip, $status = TRUE) {
    return $this
      ->assert($status == access_filter_ip_match($pattern, $ip), format_string('IP address %ip is' . ($status ? '' : ' not') . ' matched to %pattern', array(
      '%ip' => $ip,
      '%pattern' => $pattern,
    )));
  }

  /**
   * Test filter restriction functions.
   */
  public function testRestrictionAccess() {

    // Create node with random alias.
    $settings['path']['alias'] = strtolower($this
      ->randomName(8)) . '/' . strtolower($this
      ->randomName(8));
    $node = $this
      ->drupalCreateNode($settings);
    $path = 'node/' . $node->nid;
    $alias = $node->path['alias'];

    // Create filter.
    $filter = $this
      ->createFilter('Deny');

    // Test for deny action.
    $deny_actions = array(
      ACCESS_FILTER_DENY_ACTION_403,
      ACCESS_FILTER_DENY_ACTION_404,
      ACCESS_FILTER_DENY_ACTION_301,
      ACCESS_FILTER_DENY_ACTION_302,
      ACCESS_FILTER_DENY_ACTION_200,
    );
    $filter->paths = 'D:' . $path;
    $filter->rules = 'D:*';
    foreach ($deny_actions as $deny_action) {
      $filter->deny_action_settings->type = $deny_action;
      if ($this
        ->assertSaveFilter($filter)) {
        $this
          ->assertAccess($path, $deny_action, format_string("Deny action %code worked correctly.", array(
          '%code' => $deny_action,
        )));
      }
    }

    // Test for deny specified path only.
    $filter->paths = 'D+B:' . $path;
    $filter->rules = 'D:*';
    $filter->deny_action_settings->type = ACCESS_FILTER_DENY_ACTION_403;
    if ($this
      ->assertSaveFilter($filter)) {
      $this
        ->assertAccess($path, $filter->deny_action_settings->type, format_string('Denying access to %path on blind mode worked correctly.', array(
        '%path' => $path,
      )));
      $this
        ->assertAccess($alias, 200, format_string('Allowing access to %path on blind mode worked correctly.', array(
        '%path' => $alias,
      )));
    }

    // Test for deny specified path and aliases.
    $filter->paths = 'D:' . $path;
    $filter->rules = 'D:*';
    $filter->deny_action_settings->type = ACCESS_FILTER_DENY_ACTION_403;
    if ($this
      ->assertSaveFilter($filter)) {
      $this
        ->assertAccess($path, $filter->deny_action_settings->type, format_string('Denying access to %path worked correctly.', array(
        '%path' => $path,
      )));
      $this
        ->assertAccess($alias, $filter->deny_action_settings->type, format_string('Denying access to %path worked correctly.', array(
        '%path' => $alias,
      )));
    }

    // Test for allow specified path and aliases.
    $filter->paths = 'D:' . $path;
    $filter->rules = "D:*\nA:*";
    if ($this
      ->assertSaveFilter($filter)) {
      $this
        ->assertAccess($path, 200, format_string('Allowing access to %path worked correctly.', array(
        '%path' => $path,
      )));
      $this
        ->assertAccess($alias, 200, format_string('Allowing access to %path worked correctly.', array(
        '%path' => $alias,
      )));
    }

    // Test for using regex for path.
    if (!empty($GLOBALS['conf']['clean_url'])) {
      $filter->paths = 'D+R:^node\\/[0-9]+$';
    }
    else {
      $filter->paths = 'D+R:^\\?q=node\\/[0-9]+$';
    }
    $filter->rules = 'D:*';
    $filter->deny_action_settings->type = ACCESS_FILTER_DENY_ACTION_403;
    if ($this
      ->assertSaveFilter($filter)) {
      $this
        ->assertAccess($path, $filter->deny_action_settings->type, format_string('Using regex for path worked correctly.', array(
        '%path' => $path,
      )));
    }

    // Test for using request uri.
    if (!empty($GLOBALS['conf']['clean_url'])) {
      $filter->paths = 'R:node/' . $node->nid;
    }
    else {
      $filter->paths = 'R:?q=node/' . $node->nid;
    }
    $filter->rules = 'D:*';
    $filter->deny_action_settings->type = ACCESS_FILTER_DENY_ACTION_403;
    if ($this
      ->assertSaveFilter($filter)) {
      $this
        ->assertAccess($path, $filter->deny_action_settings->type, format_string('Using request uri worked correctly.', array(
        '%path' => $path,
      )));
    }

    // Test for using regex for request uri.
    if (!empty($GLOBALS['conf']['clean_url'])) {
      $filter->paths = 'R+R:^node\\/[0-9]+$';
    }
    else {
      $filter->paths = 'R+R:^\\?q=node\\/[0-9]+$';
    }
    $filter->rules = 'D:*';
    $filter->deny_action_settings->type = ACCESS_FILTER_DENY_ACTION_403;
    if ($this
      ->assertSaveFilter($filter)) {
      $this
        ->assertAccess($path, $filter->deny_action_settings->type, format_string('Using regex for request uri worked correctly.', array(
        '%path' => $path,
      )));
    }

    // Test for force logout.
    $filter->paths = 'D:' . $path;
    $filter->rules = "D:*";
    $filter->deny_action_settings->type = ACCESS_FILTER_DENY_ACTION_403;
    $filter->deny_action_settings->force_logout = TRUE;
    if ($this
      ->assertSaveFilter($filter)) {
      $this
        ->assertAccess($path, $filter->deny_action_settings->type, format_string('Denying access to %path worked correctly.', array(
        '%path' => $path,
      )));

      // Check user is logged out.
      // Copied from DrupalTestCase::drupalLogout().
      $this
        ->drupalGet('user');
      $pass = $this
        ->assertField('name', t('Username field found.'), t('Logout'));
      $pass = $pass && $this
        ->assertField('pass', t('Password field found.'), t('Logout'));
      $this
        ->assertTrue($pass, 'Forcing logout worked correctly.');
    }
  }

  /**
   * Create a filter.
   *
   * @param string $name
   *   A string of filter name.
   *
   * @return object
   *   An object of filter.
   */
  protected function createFilter($name) {
    $filter = new stdClass();
    $filter->status = 1;
    $filter->name = $name;
    $filter->deny_action_settings = new stdClass();
    $filter->deny_action_settings->type = ACCESS_FILTER_DENY_ACTION_403;
    $filter->deny_action_settings->redirect_destination = 'http://drupal.org/';
    $filter->deny_action_settings->force_logout = FALSE;
    $filter->weight = 1;
    return $filter;
  }

  /**
   * Check update filter works correctly.
   *
   * @param object $filter
   *   An object of filter.
   *
   * @return bool
   *   TRUE if the assertion succeeded, FALSE otherwise.
   */
  protected function assertSaveFilter($filter) {
    return $this
      ->assertTrue(access_filter_save($filter), 'Access filter saved.');
  }

  /**
   * Check restriction to access works correctly.
   *
   * @param string $path
   *   A string of path to access.
   * @param int $code
   *   An integer of HTTP status code.
   * @param string $message
   *   A string of assert message.
   *
   * @return bool
   *   TRUE if the assertion succeeded, FALSE otherwise.
   */
  protected function assertAccess($path, $code, $message) {

    // Build absolute url to avoid auto looking up aliases.
    global $base_url;
    $url = $base_url . '/';
    if (!empty($GLOBALS['conf']['clean_url'])) {
      $url .= $path;
    }
    else {
      $url .= '?q=' . $path;
    }

    // Disable redirection to assert 301/302 codes.
    $max_redirects = variable_get('simpletest_maximum_redirects', 5);
    variable_set('simpletest_maximum_redirects', 0);
    $this
      ->drupalGet($url);
    variable_set('simpletest_maximum_redirects', $max_redirects);
    return $this
      ->assertResponse($code, $message);
  }

}