You are here

public function AccessGrantInterfaceTest::testGrantOverviewFilters in Access Control Kit 7

Test the grant overview filters.

File

./access.test, line 858
Tests for the access control kit module.

Class

AccessGrantInterfaceTest
Tests the access grant interface.

Code

public function testGrantOverviewFilters() {

  // Create two schemes for testing the scheme filter.
  $schemes = array(
    'good' => $this
      ->createScheme(),
    'bad' => $this
      ->createScheme(),
  );

  // Create two roles for testing the role filter.
  $rid = $this
    ->drupalCreateRole(array(
    'access content',
  ));
  $roles = array(
    'good' => $this->ackRole,
    'bad' => user_role_load($rid),
  );

  // Enable both roles for both schemes.
  $scheme_roles = array(
    $roles['good']->rid => $roles['good']->name,
    $roles['bad']->rid => $roles['bad']->name,
  );
  variable_set('access_scheme_roles_' . $schemes['good']->machine_name, $scheme_roles);
  variable_set('access_scheme_roles_' . $schemes['bad']->machine_name, $scheme_roles);

  // Create two users for testing the user filter.
  $users = array(
    'good' => $this->ackUser,
    'bad' => $this
      ->drupalCreateUser(array(
      'access content',
    )),
  );

  // Create two grants for filtering.
  $good = $this
    ->createGrant($schemes['good'], $roles['good'], $users['good']);
  $bad = $this
    ->createGrant($schemes['bad'], $roles['bad'], $users['bad']);
  $this
    ->drupalGet('admin/access');
  foreach (array(
    'role',
    'scheme',
    'username',
  ) as $filter) {

    // Make sure that both grants are displayed on the unfiltered display.
    $this
      ->assertLinkByHref('admin/access/grant/' . $good->gid, 0);
    $this
      ->assertLinkByHref('admin/access/grant/' . $bad->gid, 0);

    // Filter the display.
    $edit = array();
    $value = '';
    switch ($filter) {
      case 'role':
        $edit = array(
          'rid' => $roles['good']->rid,
        );
        $value = $roles['good']->name;
        break;
      case 'scheme':
        $edit = array(
          'scheme' => $schemes['good']->machine_name,
        );
        $value = $schemes['good']->name;
        break;
      case 'username':
        $edit = array(
          'username' => $users['good']->name,
        );
        $value = $users['good']->name;
        break;
    }
    $this
      ->drupalPost(NULL, $edit, 'Filter');
    $this
      ->assertText(t('where @filter is @value', array(
      '@filter' => $filter,
      '@value' => $value,
    )), 'Applied filter for ' . $filter . '.');

    // Make sure that only the matching grant is displayed.
    $this
      ->assertLinkByHref('admin/access/grant/' . $good->gid, 0, 'Matching grants were displayed.');
    $this
      ->assertNoLinkByHref('admin/access/grant/' . $bad->gid, 'Non-matching grants were not displayed.');

    // Clear the filters.
    $this
      ->drupalPost(NULL, array(), 'Reset');
    $this
      ->assertNoText(t('where @filter is @value', array(
      '@filter' => $filter,
      '@value' => $value,
    )), 'Filter text was removed on reset.');
  }

  // Test multiple filters.
  $this
    ->drupalPost(NULL, array(
    'username' => $users['good']->name,
  ), 'Filter');
  $this
    ->drupalPost(NULL, array(
    'scheme' => $schemes['good']->machine_name,
  ), 'Refine');
  $this
    ->assertText(t('where @filter is @value', array(
    '@filter' => 'username',
    '@value' => $users['good']->name,
  )), 'Applied filter for username.');
  $this
    ->assertText(t('and where @filter is @value', array(
    '@filter' => 'scheme',
    '@value' => $schemes['good']->name,
  )), 'Applied filter for scheme.');
  $this
    ->assertLinkByHref('admin/access/grant/' . $good->gid, 0, 'Matching grants were displayed.');
  $this
    ->assertNoLinkByHref('admin/access/grant/' . $bad->gid, 'Non-matching grants were not displayed.');
  $this
    ->drupalPost(NULL, array(), 'Undo');
  $this
    ->assertText(t('where @filter is @value', array(
    '@filter' => 'username',
    '@value' => $users['good']->name,
  )), 'Retained filter for username.');
  $this
    ->assertNoText(t('and where @filter is @value', array(
    '@filter' => 'scheme',
    '@value' => $schemes['good']->name,
  )), 'Removed filter for scheme.');
  $this
    ->assertLinkByHref('admin/access/grant/' . $good->gid, 0, 'Matching grants were displayed.');
  $this
    ->assertNoLinkByHref('admin/access/grant/' . $bad->gid, 'Non-matching grants were not displayed.');
  $this
    ->drupalPost(NULL, array(
    'scheme' => $schemes['bad']->machine_name,
  ), 'Refine');
  $this
    ->assertNoLinkByHref('admin/access/grant/' . $good->gid, 'Non-matching grants were not displayed.');
  $this
    ->assertNoLinkByHref('admin/access/grant/' . $bad->gid, 'Non-matching grants were not displayed.');
  $this
    ->assertText(t('No access grants available.'));

  // Clear all filters.
  $this
    ->drupalPost(NULL, array(), 'Reset');
  $this
    ->assertLinkByHref('admin/access/grant/' . $good->gid, 0);
  $this
    ->assertLinkByHref('admin/access/grant/' . $bad->gid, 0);
}