You are here

public function AccessWebTestCase::createGrant in Access Control Kit 7

Creates and returns a new access grant for the given scheme.

Parameters

object $scheme: An access scheme.

object $role: (optional) A user role. If omitted, uses the first available scheme role. If no role is available, creates one using drupalCreateRole().

object $account: (optional) A user account. If omitted, uses drupalCreateUser() and adds the user to the role.

Return value

object An access grant.

9 calls to AccessWebTestCase::createGrant()
AccessAPITest::testHandlerInterface in ./access.test
Attach a handler to the test scheme through the UI.
AccessAPITest::testHandlerMethods in ./access.test
Test access arbitration through our dummy handler.
AccessGrantFunctionTest::setUp in ./access.test
Overrides DrupalWebTestCase::setUp().
AccessGrantFunctionTest::testGrantCRUD in ./access.test
Test basic create, read, update, and delete functions.
AccessGrantInterfaceTest::testGrantDelete in ./access.test
Delete an access grant via the user interface.

... See full list

File

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

Class

AccessWebTestCase
Provides common helper methods for access control kit module tests.

Code

public function createGrant($scheme, $role = NULL, $account = NULL) {

  // Make sure we have a user role to assign.
  if (!isset($role)) {

    // Get the list of roles enabled for this scheme.
    $scheme_roles = variable_get('access_scheme_roles_' . $scheme->machine_name, array());

    // If the scheme has no enabled roles, create one for it.
    if (empty($scheme_roles)) {
      $rid = $this
        ->drupalCreateRole(array(
        'access content',
      ));
      $role = user_role_load($rid);
      $scheme_roles = array(
        $role->rid => $role->name,
      );
      variable_set('access_scheme_roles_' . $scheme->machine_name, $scheme_roles);
    }
    else {
      $rid = key($scheme_roles);
      $role = user_role_load($rid);
    }
  }

  // Make sure we have a user to assign.
  if (!isset($account)) {

    // Create an account and add it to the role.
    $account = $this
      ->drupalCreateUser(array(
      'access content',
    ));
    $account->original = clone $account;
    $user_roles = $account->roles + array(
      $role->rid => $role->name,
    );
    user_save($account, array(
      'roles' => $user_roles,
    ));
  }

  // Create an access grant.
  $grant = entity_get_controller('access_grant')
    ->create(array(
    'scheme' => $scheme->machine_name,
    'rid' => $role->rid,
    'uid' => $account->uid,
  ));
  $this
    ->assertNull($grant->gid);

  // Save the grant and confirm that values come back correct.
  $this
    ->assertIdentical(access_grant_save($grant), SAVED_NEW, 'New grant saved.');
  $this
    ->assertNotNull($grant->gid);
  $this
    ->assertIdentical($grant->scheme, $scheme->machine_name);
  $this
    ->assertIdentical($grant->rid, $role->rid);
  $this
    ->assertIdentical($grant->uid, $account->uid);
  return $grant;
}