You are here

public function AccessGrantInterfaceTest::testGrantDelete in Access Control Kit 7

Delete an access grant via the user interface.

File

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

Class

AccessGrantInterfaceTest
Tests the access grant interface.

Code

public function testGrantDelete() {

  // Create an access scheme that uses the test role.
  $scheme = $this
    ->createScheme();
  $scheme = access_scheme_load($scheme->sid, TRUE);
  $scheme->roles = array(
    $this->ackRole->rid => $this->ackRole->name,
  );
  variable_set('access_scheme_roles_' . $scheme->machine_name, $scheme->roles);

  // Add the test user to the test role.
  $this->ackUser->original = clone $this->ackUser;
  $roles = $this->ackUser->roles + array(
    $this->ackRole->rid => $this->ackRole->name,
  );
  user_save($this->ackUser, array(
    'roles' => $roles,
  ));
  $this->ackUser = user_load($this->ackUser->uid, TRUE);

  // Create an access grant.
  $grant = $this
    ->createGrant($scheme, $this->ackRole, $this->ackUser);
  $field_name = $scheme->realm_field['field_name'];
  $grant->{$field_name} = array(
    'und' => array(
      array(
        'value' => 1,
      ),
    ),
  );
  access_grant_save($grant);
  $grant = access_grant_load($grant->gid, TRUE);
  $this
    ->assertTrue($grant, 'Access grant found in the database.');

  // Check deleting from the overview page.
  $this
    ->drupalGet('admin/access');
  $this
    ->clickLink(t('delete'));
  $this
    ->assertRaw(t("Are you sure you want to revoke all %scheme for %user's access as %role?", array(
    '%scheme' => $scheme->name,
    '%user' => $this->ackUser->name,
    '%role' => $this->ackRole->name,
  )), '[confirm deletion] Asks for confirmation.');

  // Delete the grant without removing the user's role.
  $edit = array();
  $this
    ->drupalPost('admin/access/grant/' . $grant->gid . '/edit', $edit, t('Delete'));
  $this
    ->assertRaw(t("Are you sure you want to revoke all %scheme for %user's access as %role?", array(
    '%scheme' => $scheme->name,
    '%user' => $this->ackUser->name,
    '%role' => $this->ackRole->name,
  )), '[confirm deletion] Asks for confirmation.');
  $this
    ->assertNoRaw(t("Also revoke %user's membership in the %role role?", array(
    '%user' => $this->ackUser->name,
    '%role' => $this->ackRole->name,
  )), 'Prompt to revoke the role is not shown when user lacks user admin access.');

  // Give the admin user access to administer users and try again.
  $this->adminUser->original = clone $this->adminUser;
  $rid = $this
    ->drupalCreateRole(array(
    'administer users',
  ));
  $role = user_role_load($rid);
  $roles = $this->adminUser->roles + array(
    $role->rid => $role->name,
  );
  user_save($this->adminUser, array(
    'roles' => $roles,
  ));
  $this
    ->drupalGet('admin/access/grant/' . $grant->gid . '/delete');
  $this
    ->assertRaw(t("Also revoke %user's membership in the %role role?", array(
    '%user' => $this->ackUser->name,
    '%role' => $this->ackRole->name,
  )), 'Prompt to revoke the role is shown when user has user admin access.');
  $this
    ->assertText(t('This action cannot be undone.'), '[confirm deletion] Informs that deletion is permanent.');
  $this
    ->drupalPost(NULL, NULL, t('Delete'));

  // Confirm deletion.
  $this
    ->assertRaw(t("Deleted %scheme for %user's access as %role.", array(
    '%scheme' => $scheme->name,
    '%user' => $this->ackUser->name,
    '%role' => $this->ackRole->name,
  )), 'Access grant deleted.');
  $this
    ->assertFalse(access_grant_load($grant->gid, TRUE), 'Access grant is not found in the database.');
  $this->ackUser = user_load($this->ackUser->uid, TRUE);
  $this
    ->assertTrue(isset($this->ackUser->roles[$this->ackRole->rid]), 'User is still a member of the role.');

  // Repeat with a new grant and the revoke role option checked.
  $grant = $this
    ->createGrant($scheme, $this->ackRole, $this->ackUser);
  $grant->{$field_name} = array(
    'und' => array(
      array(
        'value' => 1,
      ),
    ),
  );
  access_grant_save($grant);
  $grant = access_grant_load($grant->gid, TRUE);
  $this
    ->assertTrue($grant, 'Access grant found in the database.');
  $edit = array();
  $edit['revoke_role'] = TRUE;
  $this
    ->drupalPost('admin/access/grant/' . $grant->gid . '/delete', $edit, t('Delete'));

  // Confirm deletion and role removal.
  $this
    ->assertRaw(t("Deleted %scheme for %user's access as %role.", array(
    '%scheme' => $scheme->name,
    '%user' => $this->ackUser->name,
    '%role' => $this->ackRole->name,
  )), 'Access grant deleted.');
  $this
    ->assertFalse(access_grant_load($grant->gid, TRUE), 'Access grant is not found in the database.');
  $this->ackUser = user_load($this->ackUser->uid, TRUE);
  $this
    ->assertFalse(isset($this->ackUser->roles[$this->ackRole->rid]), 'User was removed from the role.');

  // Check multiple deletion.
  $grant_a = $this
    ->createGrant($scheme);
  $grant_b = $this
    ->createGrant($scheme);
  $edit = array();
  $this
    ->drupalPost('admin/access', $edit, t('Delete selected grants'));
  $this
    ->assertText(t('No items selected'));
  $key = 'grants[' . $grant_a->gid . ']';
  $edit[$key] = TRUE;
  $this
    ->drupalPost('admin/access', $edit, t('Delete selected grants'));
  $this
    ->assertText('Are you sure you want to delete this access grant?', '[confirm deletion] Asks for confirmation.');
  $this
    ->assertText(access_grant_label($grant_a));
  $this
    ->clickLink(t('Cancel'));
  $this
    ->assertLinkByHref('admin/access/grant/' . $grant_a->gid);
  $this
    ->assertLinkByHref('admin/access/grant/' . $grant_b->gid);
  $key = 'grants[' . $grant_b->gid . ']';
  $edit[$key] = TRUE;
  $this
    ->drupalPost('admin/access', $edit, t('Delete selected grants'));
  $this
    ->assertText('Are you sure you want to delete these access grants?', '[confirm deletion] Asks for confirmation.');
  $this
    ->assertText(access_grant_label($grant_a));
  $this
    ->assertText(access_grant_label($grant_b));
  $this
    ->assertText(t('This action cannot be undone.'), '[confirm deletion] Informs that deletion is permanent.');
  $this
    ->drupalPost(NULL, array(), t('Delete'));
  $this
    ->assertText('Deleted 2 access grants.');
  $this
    ->assertNoLinkByHref('admin/access/grant/' . $grant_a->gid);
  $this
    ->assertNoLinkByHref('admin/access/grant/' . $grant_b->gid);
  $this
    ->assertFalse(access_grant_load($grant_a->gid, TRUE), 'Access grant is not found in the database.');
  $this
    ->assertFalse(access_grant_load($grant_b->gid, TRUE), 'Access grant is not found in the database.');
}