1 entityreference.handlers.test | public EntityReferenceHandlersTestCase::testNodeHandler() |
Test the node-specific overrides of the entity handler.
File
- core/
modules/ entityreference/ 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.
$build_nodes = array(
'published1' => array(
'type' => 'post',
'status' => 1,
'title' => 'Node published1 (<&>)',
'uid' => 1,
),
'published2' => array(
'type' => 'post',
'status' => 1,
'title' => 'Node published2 (<&>)',
'uid' => 1,
),
'unpublished' => array(
'type' => 'post',
'status' => 0,
'title' => 'Node unpublished (<&>)',
'uid' => 1,
),
// Title purposefully starts with same characters as published1 and
// published2 above but contains a slash.
'published_withslash' => array(
'type' => 'post',
'status' => 1,
'title' => 'Node pub/lished1',
'uid' => 1,
),
);
$node_labels = array();
foreach ($build_nodes as $key => $settings) {
$node = $this->backdropCreateNode($settings);
$nodes[$key] = $node;
$node_labels[$key] = check_plain($node->title);
}
// Test as a non-admin.
$normal_user = $this->backdropCreateUser(array('access content'));
$GLOBALS['user'] = $normal_user;
$referenceable_tests = array(
array(
'arguments' => array(
array(NULL, 'CONTAINS'),
),
'result' => array(
'post' => 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(
'post' => array(
$nodes['published1']->nid => $node_labels['published1'],
),
),
),
array(
'arguments' => array(
array('published2', 'CONTAINS'),
array('Published2', 'CONTAINS'),
),
'result' => array(
'post' => 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(
'post' => array(
$nodes['published_withslash']->nid => $node_labels['published_withslash'],
),
),
),
);
$this->assertReferenceable($field, $referenceable_tests, 'Node handler');
// Test as an admin.
$admin_user = $this->backdropCreateUser(array('access content', 'bypass node access'));
$GLOBALS['user'] = $admin_user;
$referenceable_tests = array(
array(
'arguments' => array(
array(NULL, 'CONTAINS'),
),
'result' => array(
'post' => 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(
'post' => array(
$nodes['unpublished']->nid => $node_labels['unpublished'],
),
),
),
);
$this->assertReferenceable($field, $referenceable_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)));
}