You are here

public function AccessPluginTest::testHanderInfo in Access Control Kit 7

Check that handlers are defined correctly.

File

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

Class

AccessPluginTest
Tests the bundled scheme types and handlers.

Code

public function testHanderInfo() {
  $info = access_handler_info();

  // We need schemes and a representative entity to test the handlers.
  $scheme_list = $this
    ->createListScheme('integer');
  $scheme_term = $this
    ->createTaxonomyTermScheme('vocabulary_taxonomy_term');
  $content_type = $this
    ->drupalCreateContentType();

  // Add a list field to the content type.
  $instance = array(
    'field_name' => 'field_integer',
    'entity_type' => 'node',
    'bundle' => $content_type->type,
  );
  field_create_instance($instance);

  // Add a term reference field to the content type.
  $field = array(
    'field_name' => 'field_term',
    'type' => 'taxonomy_term_reference',
    'settings' => array(
      'allowed_values' => array(
        array(
          'vocabulary' => 'vocabulary_taxonomy_term',
          'parent' => '0',
        ),
      ),
    ),
  );
  field_create_field($field);
  $instance = array(
    'field_name' => 'field_term',
    'entity_type' => 'node',
    'bundle' => $content_type->type,
    'required' => TRUE,
  );
  field_create_instance($instance);

  // Create the test node.
  $node = $this
    ->drupalCreateNode(array(
    'type' => $content_type->type,
  ));
  $node = node_load($node->nid);
  $node->field_integer[LANGUAGE_NONE][]['value'] = 7;
  $node->field_term[LANGUAGE_NONE][]['tid'] = 3;
  node_save($node);

  // Get the node form for later reference.
  $node_form_state = array();
  $node_form_state['build_info']['args'] = array(
    $node,
  );
  module_load_include('inc', 'node', 'node.pages');
  $node_form = drupal_build_form($node->type . '_node_form', $node_form_state);

  // Test the generic field handler.
  $this
    ->assertTrue(isset($info['ACKEntityField']), 'Found the generic field handler.');
  $handler = new ACKEntityField($scheme_list);
  $description = $handler
    ->description();
  $this
    ->assertIdentical($description, t('The value of %field_name will determine realm membership.', array(
    '%field_name' => 'field_integer',
  )), 'Generic handler has the correct description.');
  $form = $handler
    ->settingsForm();
  $this
    ->assertIdentical($form, array(), 'Generic handler has no settings.');
  $realms = $handler
    ->objectRealms('node', $node);
  $this
    ->assertIdentical($realms, array(
    7,
  ), 'Generic handler can find realm values on a node.');

  // Test altering the node form.
  $this
    ->assertTrue(empty($node_form['field_integer'][LANGUAGE_NONE]['#disabled']), 'List field is accessible before handler is applied.');
  $handler
    ->objectFormAlter('node', $node, $node_form, $node_form_state, $node->type . '_node_form');
  $this
    ->assertTrue($node_form['field_integer'][LANGUAGE_NONE]['#disabled'], 'List field is locked after handler is applied.');
  $this
    ->assertTrue(isset($node_form['field_integer'][LANGUAGE_NONE]['#options']['_none']), 'List field contains the _none option.');
  $this
    ->assertTrue(isset($node_form['field_integer'][LANGUAGE_NONE]['#options']['1']), 'List field contains option 1.');
  $this
    ->assertTrue(isset($node_form['field_integer'][LANGUAGE_NONE]['#options']['7']), 'List field contains option 7.');
  $this
    ->assertTrue(isset($node_form['field_integer'][LANGUAGE_NONE]['#options']['31']), 'List field contains option 31.');
  $handler
    ->objectFormAlter('node', $node, $node_form, $node_form_state, $node->type . '_node_form', array(
    '7',
    '31',
    '365',
  ));
  $this
    ->assertTrue(isset($node_form['field_integer'][LANGUAGE_NONE]['#options']['_none']), 'The _none option was not removed from the list.');
  $this
    ->assertFalse(isset($node_form['field_integer'][LANGUAGE_NONE]['#options']['1']), 'Option 1 was removed from the the list.');
  $this
    ->assertTrue(isset($node_form['field_integer'][LANGUAGE_NONE]['#options']['7']), 'Option 7 remains in the list.');
  $this
    ->assertTrue(isset($node_form['field_integer'][LANGUAGE_NONE]['#options']['31']), 'Option 31 remains in the list.');
  $this
    ->assertFalse(isset($node_form['field_integer'][LANGUAGE_NONE]['#options']['365']), 'Option 365 was not added to the list.');

  // Test the taxonomy term reference handler.
  $this
    ->assertTrue(isset($info['ACKEntityTaxonomyTermReference']), 'Found the term reference field handler.');
  $handler = new ACKEntityTaxonomyTermReference($scheme_term, array(
    'field_name' => 'field_term',
  ));
  $description = $handler
    ->description();
  $this
    ->assertIdentical($description, t('The value of the selected term reference field will determine realm membership.'), 'Term handler has the correct description.');
  $form = $handler
    ->settingsForm();
  $this
    ->assertTrue(isset($form['field_name']['#default_value']), 'Term handler has settings.');
  $this
    ->assertIdentical($form['field_name']['#default_value'], 'field_term', 'Term handler settings default to the saved value.');
  $realms = $handler
    ->objectRealms('node', $node);
  $this
    ->assertIdentical($realms, array(
    3,
  ), 'Term handler can find realm values on a node.');
  $this
    ->assertTrue(empty($node_form['field_term'][LANGUAGE_NONE]['#disabled']), 'Term field is accessible before handler is applied.');
  $this
    ->assertTrue(isset($node_form['field_term'][LANGUAGE_NONE]['#options']['1']), 'Term field contains option 1.');
  $this
    ->assertTrue(isset($node_form['field_term'][LANGUAGE_NONE]['#options']['2']), 'Term field contains option 2.');
  $this
    ->assertTrue(isset($node_form['field_term'][LANGUAGE_NONE]['#options']['3']), 'Term field contains option 3.');
  $this
    ->assertEqual($node_form['field_term'][LANGUAGE_NONE]['#default_value'], array(
    '3',
  ), 'Term field defaults to the saved value.');
  $handler
    ->objectFormAlter('node', $node, $node_form, $node_form_state, $node->type . '_node_form', array(
    '2',
  ));
  $this
    ->assertFalse(isset($node_form['field_term'][LANGUAGE_NONE]['#options']['_none']), 'The _none option was not added to the term field.');
  $this
    ->assertFalse(isset($node_form['field_term'][LANGUAGE_NONE]['#options']['1']), 'Option 1 was removed from the term field.');
  $this
    ->assertTrue(isset($node_form['field_term'][LANGUAGE_NONE]['#options']['2']), 'Option 2 remains in the term field.');
  $this
    ->assertFalse(isset($node_form['field_term'][LANGUAGE_NONE]['#options']['3']), 'Option 3 was removed from the term field.');
  $this
    ->assertTrue($node_form['field_term'][LANGUAGE_NONE]['#disabled'], 'Term field is locked after the handler is applied.');
  $this
    ->assertEqual($node_form['field_term'][LANGUAGE_NONE]['#default_value'], array(
    '2',
  ), 'Only allowable term value is preselected by the handler.');
}