You are here

public function AccessGrantFunctionTest::testGrantCRUD in Access Control Kit 7

Test basic create, read, update, and delete functions.

File

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

Class

AccessGrantFunctionTest
Tests for access grant functions.

Code

public function testGrantCRUD() {
  $loaded_grants = access_grant_load_multiple(FALSE, TRUE);
  $this
    ->assertTrue(count($loaded_grants), 'Access grants exist in the database.');
  $i = 0;
  $count = count($this->grants);
  $field_name = $this->scheme->realm_field['field_name'];
  foreach ($this->grants as $grant) {
    $i++;

    // Confirm that the loaded grant matches expected values.
    $loaded_grant = $loaded_grants[$grant->gid];
    $this
      ->assertEqual($grant->gid, $loaded_grant->gid, "[grant {$i} of {$count}] ID is OK.");
    $this
      ->assertEqual($grant->scheme, $loaded_grant->scheme, "[grant {$i} of {$count}] Scheme is OK.");
    $this
      ->assertEqual($grant->uid, $loaded_grant->uid, "[grant {$i} of {$count}] User reference is OK.");
    $this
      ->assertEqual($grant->rid, $loaded_grant->rid, "[grant {$i} of {$count}] Role reference is OK.");

    // Confirm that the realm field was attached.
    $this
      ->assertTrue(isset($loaded_grant->{$field_name}), "[grant {$i} of {$count}] Realm field is attached.");
  }

  // Attempt to load a grant that doesn't exist.
  $gid = count($loaded_grants) + 1;
  $grant = access_grant_load($gid);
  $this
    ->assertFalse($grant, 'Invalid grant ID does not load.');

  // Create a new grant, which should end up with $gid as its ID.
  $this
    ->createGrant($this->scheme);

  // Make sure that the previously invalid ID now loads properly.
  $grant = access_grant_load($gid);
  $this
    ->assertTrue(!empty($grant) && is_object($grant), 'Newly created access grant loads properly.');
  $this
    ->assertEqual($grant->gid, $gid, 'Loaded grant ID is the same as the previously invalid ID.');

  // Confirm that the grant can be updated.
  if (!empty($grant) && is_object($grant)) {
    $grant->{$field_name} = array(
      'und' => array(
        array(
          'value' => 1,
        ),
      ),
    );
    $this
      ->assertEqual(access_grant_save($grant), SAVED_UPDATED, 'Grant updated.');
    $loaded_grant = access_grant_load($grant->gid, TRUE);
    $field = $loaded_grant->{$field_name};
    $this
      ->assertEqual($field['und'][0]['value'], 1);
    $realms = $loaded_grant->realms;
    $this
      ->assertFalse(isset($realms[0]));
    $this
      ->assertTrue(isset($realms[1]), 'Grant realms found.');

    // Confirm that the grant can be deleted.
    $this
      ->assertEqual(access_grant_delete($grant->gid), SAVED_DELETED, 'Grant deleted.');
    $this
      ->assertFalse(access_grant_load($grant->gid));

    // Confirm that the grant can be recreated.
    unset($grant->gid);
    $this
      ->assertEqual(access_grant_save($grant), SAVED_NEW, 'Grant created.');
  }

  // Test conditional loading.
  $scheme = $this
    ->createListScheme('integer');
  $field_name = $scheme->realm_field['field_name'];
  $grant = $this
    ->createGrant($scheme);
  $grant->{$field_name} = array(
    'und' => array(
      array(
        'value' => 7,
      ),
    ),
  );
  access_grant_save($grant);
  $grant = $this
    ->createGrant($scheme);
  $grant->{$field_name} = array(
    'und' => array(
      array(
        'value' => 31,
      ),
    ),
  );
  access_grant_save($grant);
  $conditions = array(
    // Load by scheme.
    'scheme' => array(
      'condition' => $scheme->machine_name,
      'count' => 2,
    ),
    // Load by user.
    'uid' => array(
      'condition' => $grant->uid,
      'count' => 1,
    ),
    // Load by role.
    'rid' => array(
      'condition' => $grant->rid,
      'count' => 2,
    ),
  );
  foreach ($conditions as $key => $tests) {
    $match = TRUE;
    $loaded_grants = access_grant_load_by_condition(array(
      $key => $tests['condition'],
    ));
    foreach ($loaded_grants as $loaded_grant) {
      $match = $match && $loaded_grant->{$key} == $tests['condition'];
    }
    $this
      ->assertTrue($match && count($loaded_grants) == $tests['count'], 'Loaded all grants for ' . $key . ' ' . $tests['condition']);
  }

  // Load by realms.
  $conditions = array(
    'scheme' => $scheme->machine_name,
    'realms' => array(
      31,
    ),
  );
  $loaded_grants = access_grant_load_by_condition($conditions);
  $loaded_grant = reset($loaded_grants);
  $this
    ->assertTrue(count($loaded_grants) == 1 && $loaded_grant->gid == $grant->gid, 'Loaded only one grant with matching realms.');
  $conditions['realms'] = array(
    1,
  );
  $loaded_grants = access_grant_load_by_condition($conditions);
  $this
    ->assertTrue(count($loaded_grants) == 0, 'Did not load grants with mismatching realms.');
}