You are here

public function AccessPluginTest::testSchemeInfo in Access Control Kit 7

Check that scheme types are defined correctly.

File

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

Class

AccessPluginTest
Tests the bundled scheme types and handlers.

Code

public function testSchemeInfo() {
  $info = access_scheme_info();

  // The boolean type should always be available.
  $this
    ->assertTrue(isset($info['boolean']), 'The boolean scheme type is defined.');
  $scheme = $this
    ->createScheme('boolean');
  $scheme = access_scheme_load($scheme->sid);
  $realms = array(
    0 => t('False'),
    1 => t('True'),
  );
  $diff_a = array_diff_assoc($scheme->realms, $realms);
  $diff_b = array_diff_assoc($realms, $scheme->realms);
  $this
    ->assertTrue(empty($diff_a) && empty($diff_b), 'The boolean realms were found.');

  // The user type should always be available.
  $this
    ->assertTrue(isset($info['user']), 'The user scheme type is defined.');
  $scheme = $this
    ->createScheme('user');
  $scheme = access_scheme_load($scheme->sid);
  $realms = db_query('SELECT uid, name FROM {users} WHERE uid > 0')
    ->fetchAllKeyed();
  $diff_a = array_diff_assoc($scheme->realms, $realms);
  $diff_b = array_diff_assoc($realms, $scheme->realms);
  $this
    ->assertTrue(empty($diff_a) && empty($diff_b), 'The user realms were found.');

  // The list types should not be available until a field exists.
  foreach (array(
    'integer',
    'float',
    'text',
  ) as $type) {
    $list = 'list_' . $type;
    $field_name = 'field_' . $type;
    $this
      ->assertFalse(isset($info[$list]), 'The ' . $list . ' scheme type is not available without a usable field.');
    $scheme = $this
      ->createListScheme($type);
    $info = access_scheme_info();
    $this
      ->assertTrue(isset($info[$list]), 'The ' . $list . ' scheme type is available when a usable field exists.');
    $field = field_info_field($field_name);
    $realms = list_allowed_values($field);
    $diff_a = array_diff_assoc($scheme->realms, $realms);
    $diff_b = array_diff_assoc($realms, $scheme->realms);
    $this
      ->assertTrue(empty($diff_a) && empty($diff_b), 'The ' . $list . ' realms were found.');
    $settings_form = call_user_func_array($scheme->info['settings callback'], array(
      $scheme,
      FALSE,
    ));
    $this
      ->assertTrue(isset($settings_form['field_name']), 'The ' . $list . ' settings form was found.');
    $this
      ->assertTrue(isset($settings_form['field_name']['#options'][$field_name]), 'The usable ' . $list . ' field appears as an option.');
  }

  // The taxonomy type should not be available until a vocabulary exists.
  $this
    ->assertFalse(isset($info['taxonomy_term']), 'The taxonomy_term scheme type is not available without a usable vocabulary.');
  $scheme = $this
    ->createTaxonomyTermScheme('vocabulary_taxonomy_term');
  $info = access_scheme_info();
  $this
    ->assertTrue(isset($info['taxonomy_term']), 'The taxonomy_term scheme type is available when a usable vocabulary exists.');
  $vocabulary = taxonomy_vocabulary_machine_name_load('vocabulary_taxonomy_term');
  $realms = db_query('SELECT tid, name FROM {taxonomy_term_data} WHERE vid = :vid', array(
    ':vid' => $vocabulary->vid,
  ))
    ->fetchAllKeyed();
  $diff_a = array_diff_assoc($scheme->realms, $realms);
  $diff_b = array_diff_assoc($realms, $scheme->realms);
  $this
    ->assertTrue(empty($diff_a) && empty($diff_b), 'The taxonomy_term realms were found.');
  $settings_form = call_user_func_array($scheme->info['settings callback'], array(
    $scheme,
    FALSE,
  ));
  $this
    ->assertTrue(isset($settings_form['vocabulary']), 'The taxonomy_term settings form was found.');
  $this
    ->assertTrue(isset($settings_form['vocabulary']['#options'][$vocabulary->machine_name]), 'The usable vocabulary appears as an option.');
}