You are here

public function AccessSchemeFunctionTest::testSchemeCRUD in Access Control Kit 7

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

File

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

Class

AccessSchemeFunctionTest
Tests for access scheme functions.

Code

public function testSchemeCRUD() {
  $loaded_schemes = access_scheme_load_multiple(FALSE, TRUE);
  $this
    ->assertTrue(count($loaded_schemes), 'Access schemes exist in the database.');
  $i = 0;
  $count = count($this->schemes);
  $names = access_scheme_names();
  $bundles = field_info_bundles('access_grant');
  foreach ($this->schemes as $scheme) {
    $i++;

    // Find the test scheme in the name list.
    $this
      ->assertTrue(isset($names[$scheme->machine_name]) && $names[$scheme->machine_name] == $scheme->name, "[scheme {$i} of {$count}] Found the scheme in the name list.");

    // Find the test scheme in the bundle list.
    $this
      ->assertTrue(isset($bundles[$scheme->machine_name]), "[scheme {$i} of {$count}] Found the scheme in the bundle list.");

    // Confirm that the loaded scheme matches expected values.
    $loaded_scheme = $loaded_schemes[$scheme->sid];
    $this
      ->assertEqual($scheme->sid, $loaded_scheme->sid, "[scheme {$i} of {$count}] ID is OK.");
    $this
      ->assertEqual($scheme->machine_name, $loaded_scheme->machine_name, "[scheme {$i} of {$count}] Machine name is OK.");
    $this
      ->assertEqual($scheme->name, $loaded_scheme->name, "[scheme {$i} of {$count}] Human-readable name is OK.");
    $this
      ->assertEqual($scheme->type, $loaded_scheme->type, "[scheme {$i} of {$count}] Scheme type is OK.");
    $this
      ->assertEqual($scheme->description, $loaded_scheme->description, "[scheme {$i} of {$count}] Description is OK.");

    // Confirm that the scheme's access realm field exists.
    $this
      ->assertTrue(isset($loaded_scheme->realm_field) && isset($loaded_scheme->realm_field['field_name']) && $loaded_scheme->realm_field['field_name'] == $scheme->realm_field_name, "[scheme {$i} of {$count}] Realm field exists.");

    // Confirm that scheme type and realm information were attached.
    $this
      ->assertTrue(isset($loaded_scheme->info) && isset($loaded_scheme->info['type']) && $loaded_scheme->info['type'] == 'boolean', "[scheme {$i} of {$count}] Scheme type info was attached.");
    $this
      ->assertTrue(isset($loaded_scheme->realms) && is_array($loaded_scheme->realms) && $loaded_scheme->realms[0] == t('False') && $loaded_scheme->realms[1] == t('True'), "[scheme {$i} of {$count}] Realm values were attached.");

    // Confirm that role information was attached.
    $this
      ->assertTrue(isset($loaded_scheme->roles) && is_array($loaded_scheme->roles), "[scheme {$i} of {$count}] Roles were attached.");

    // Confirm that handlers were attached.
    $this
      ->assertTrue(isset($loaded_scheme->handlers) && isset($loaded_scheme->handlers['node']), "[scheme {$i} of {$count}] Handlers were attached.");
    $this
      ->assertEqual(get_class($loaded_scheme->handlers['node']), 'ACKEntityField');
  }

  // Attempt to load a scheme that doesn't exist.
  $sid = count($loaded_schemes) + 1;
  $scheme = access_scheme_load($sid);
  $this
    ->assertFalse($scheme, 'Invalid scheme ID does not load.');

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

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

  // Confirm that schemes can be loaded by machine name.
  $loaded_scheme = access_scheme_machine_name_load(str_replace('_', '-', $scheme->machine_name));
  $this
    ->assertEqual($scheme->sid, $loaded_scheme->sid, 'Scheme loads by machine name.');

  // Confirm that the scheme can be updated.
  $new_name = $this
    ->randomName();
  $loaded_scheme->name = $new_name;
  $this
    ->assertEqual(access_scheme_save($loaded_scheme), SAVED_UPDATED, 'Scheme updated.');
  $loaded_scheme = access_scheme_load($scheme->sid, TRUE);
  $this
    ->assertEqual($loaded_scheme->name, $new_name);

  // Confirm that the scheme can be deleted.
  entity_get_controller('access_scheme')
    ->attachHandler($loaded_scheme, 'node', 'ACKEntityField');
  access_scheme_save($loaded_scheme);
  $handlers = db_query('SELECT * FROM {access_handler} WHERE scheme = :scheme', array(
    ':scheme' => $loaded_scheme->machine_name,
  ));
  $handler = $handlers
    ->fetch();
  $this
    ->assertFalse(empty($handler), 'Handlers were attached.');
  $this
    ->assertEqual(access_scheme_delete($loaded_scheme->sid), SAVED_DELETED, 'Scheme deleted.');
  $this
    ->assertFalse(access_scheme_load($loaded_scheme->sid));
  $handlers = db_query('SELECT * FROM {access_handler} WHERE scheme = :scheme', array(
    ':scheme' => $loaded_scheme->machine_name,
  ));
  $this
    ->assertFalse($handlers
    ->fetch(), 'Handlers were detached.');
  $this
    ->assertNull(variable_get('access_scheme_roles_' . $loaded_scheme->machine_name), 'Role configuration was deleted.');
  $bundles = field_info_bundles('access_grant');
  $this
    ->assertFalse(isset($bundles[$loaded_scheme->machine_name]), 'Bundle was deleted.');

  // Confirm that the scheme can be recreated.
  unset($loaded_scheme->sid);
  $this
    ->assertEqual(access_scheme_save($loaded_scheme), SAVED_NEW, 'Scheme created.');
  $this
    ->assertNotNull($loaded_scheme->sid);
}