You are here

public function AccessFilterResrictionTestCase::testRestrictionAccess in Access Filter 7

Test filter restriction functions.

File

./access_filter.test, line 150
Tests for access_filter.module

Class

AccessFilterResrictionTestCase

Code

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.');
  }
}