public function entityReferenceSelectionAccessTest::testNodeHandler in Entity reference 8
Test the node-specific overrides of the entity handler.
File
- lib/
Drupal/ entityreference/ Tests/ entityReferenceSelectionAccessTest.php, line 44 - Contains Drupal\entityreference\Tests\entityReferenceSelectionAccessTest.
Class
- entityReferenceSelectionAccessTest
- Test for Entity Reference handlers.
Namespace
Drupal\entityreference\TestsCode
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.
$node_values = array(
'published1' => array(
'type' => 'article',
'status' => NODE_PUBLISHED,
'title' => 'Node published1 (<&>)',
'uid' => 1,
),
'published2' => array(
'type' => 'article',
'status' => NODE_PUBLISHED,
'title' => 'Node published2 (<&>)',
'uid' => 1,
),
'unpublished' => array(
'type' => 'article',
'status' => NODE_NOT_PUBLISHED,
'title' => 'Node unpublished (<&>)',
'uid' => 1,
),
);
$nodes = array();
$node_labels = array();
foreach ($node_values as $key => $values) {
$node = entity_create('node', $values);
$node
->save();
$nodes[$key] = $node;
$node_labels[$key] = check_plain($node
->label());
}
// 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(
$nodes['published1']->nid => $node_labels['published1'],
$nodes['published2']->nid => $node_labels['published2'],
),
),
array(
'arguments' => array(
array(
'published1',
'CONTAINS',
),
array(
'Published1',
'CONTAINS',
),
),
'result' => array(
$nodes['published1']->nid => $node_labels['published1'],
),
),
array(
'arguments' => array(
array(
'published2',
'CONTAINS',
),
array(
'Published2',
'CONTAINS',
),
),
'result' => 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(),
),
);
$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(
$nodes['published1']->nid => $node_labels['published1'],
$nodes['published2']->nid => $node_labels['published2'],
$nodes['unpublished']->nid => $node_labels['unpublished'],
),
),
array(
'arguments' => array(
array(
'Node unpublished',
'CONTAINS',
),
),
'result' => array(
$nodes['unpublished']->nid => $node_labels['unpublished'],
),
),
);
$this
->assertReferencable($field, $referencable_tests, 'Node handler (admin)');
}