You are here

public function EntityReferenceHandlersTestCase::testNodeHandler in Entity reference 7

Test the node-specific overrides of the entity handler.

File

tests/entityreference.handlers.test, line 49
Contains EntityReferenceHandlersTestCase

Class

EntityReferenceHandlersTestCase
Test for Entity Reference handlers.

Code

public function testNodeHandler() {

  // Build a fake field instance.
  $field = array(
    'translatable' => FALSE,
    'entity_types' => array(),
    'settings' => array(
      'handler' => 'base',
      'target_type' => 'node',
      'handler_settings' => array(
        'target_bundles' => array(),
      ),
    ),
    'field_name' => 'test_field',
    'type' => 'entityreference',
    'cardinality' => '1',
  );

  // Build a set of test data.
  // Titles contain HTML-special characters to test escaping.
  $nodes = array(
    'published1' => (object) array(
      'type' => 'article',
      'status' => 1,
      'title' => 'Node published1 (<&>)',
      'uid' => 1,
    ),
    'published2' => (object) array(
      'type' => 'article',
      'status' => 1,
      'title' => 'Node published2 (<&>)',
      'uid' => 1,
    ),
    'unpublished' => (object) array(
      'type' => 'article',
      'status' => 0,
      'title' => 'Node unpublished (<&>)',
      'uid' => 1,
    ),
    // Title purposefully starts with same characters as published1 and
    // published2 above but contains a slash.
    'published_withslash' => (object) array(
      'type' => 'article',
      'status' => 1,
      'title' => 'Node pub/lished1',
      'uid' => 1,
    ),
  );
  $node_labels = array();
  foreach ($nodes as $key => $node) {
    node_save($node);
    $node_labels[$key] = check_plain($node->title);
  }

  // Test as a non-admin.
  $normal_user = $this
    ->drupalCreateUser(array(
    'access content',
  ));
  $GLOBALS['user'] = $normal_user;
  $referencable_tests = array(
    array(
      'arguments' => array(
        array(
          NULL,
          'CONTAINS',
        ),
      ),
      'result' => array(
        'article' => array(
          $nodes['published1']->nid => $node_labels['published1'],
          $nodes['published2']->nid => $node_labels['published2'],
          $nodes['published_withslash']->nid => $node_labels['published_withslash'],
        ),
      ),
    ),
    array(
      'arguments' => array(
        array(
          'published1',
          'CONTAINS',
        ),
        array(
          'Published1',
          'CONTAINS',
        ),
      ),
      'result' => array(
        'article' => array(
          $nodes['published1']->nid => $node_labels['published1'],
        ),
      ),
    ),
    array(
      'arguments' => array(
        array(
          'published2',
          'CONTAINS',
        ),
        array(
          'Published2',
          'CONTAINS',
        ),
      ),
      'result' => array(
        'article' => array(
          $nodes['published2']->nid => $node_labels['published2'],
        ),
      ),
    ),
    array(
      'arguments' => array(
        array(
          'invalid node',
          'CONTAINS',
        ),
      ),
      'result' => array(),
    ),
    array(
      'arguments' => array(
        array(
          'Node unpublished',
          'CONTAINS',
        ),
      ),
      'result' => array(),
    ),
    // Searching for "Node pub/" should return only the published_withslash node
    // and not published1 and published2 from above.
    array(
      'arguments' => array(
        array(
          'Node pub/',
          'CONTAINS',
        ),
      ),
      'result' => array(
        'article' => array(
          $nodes['published_withslash']->nid => $node_labels['published_withslash'],
        ),
      ),
    ),
  );
  $this
    ->assertReferencable($field, $referencable_tests, 'Node handler');

  // Test as an admin.
  $admin_user = $this
    ->drupalCreateUser(array(
    'access content',
    'bypass node access',
  ));
  $GLOBALS['user'] = $admin_user;
  $referencable_tests = array(
    array(
      'arguments' => array(
        array(
          NULL,
          'CONTAINS',
        ),
      ),
      'result' => array(
        'article' => array(
          $nodes['published1']->nid => $node_labels['published1'],
          $nodes['published2']->nid => $node_labels['published2'],
          $nodes['published_withslash']->nid => $node_labels['published_withslash'],
          $nodes['unpublished']->nid => $node_labels['unpublished'],
        ),
      ),
    ),
    array(
      'arguments' => array(
        array(
          'Node unpublished',
          'CONTAINS',
        ),
      ),
      'result' => array(
        'article' => array(
          $nodes['unpublished']->nid => $node_labels['unpublished'],
        ),
      ),
    ),
  );
  $this
    ->assertReferencable($field, $referencable_tests, 'Node handler (admin)');

  // Verify autocomplete input validation.
  $handler = entityreference_get_selection_handler($field);
  $element = array(
    '#parents' => array(
      'element_name',
    ),
  );
  $form_state = array();
  $form = array();
  $value = $handler
    ->validateAutocompleteInput($nodes['published1']->title, $element, $form_state, $form);
  $this
    ->assertEqual($value, $nodes['published1']->nid);
  $invalid_input = $this
    ->randomName();
  $value = $handler
    ->validateAutocompleteInput($invalid_input, $element, $form_state, $form);
  $this
    ->assertNull($value);
  $this
    ->assertEqual(form_get_error($element), t('There are no entities matching "%value"', array(
    '%value' => $invalid_input,
  )));
}