123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575 |
- <?php
- /**
- * @file
- * Contains EntityReferenceHandlersTestCase
- */
- /**
- * Test for Entity Reference handlers.
- */
- class EntityReferenceHandlersTestCase extends DrupalWebTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'Entity Reference Handlers',
- 'description' => 'Tests for the base handlers provided by Entity Reference.',
- 'group' => 'Entity Reference',
- );
- }
- public function setUp() {
- parent::setUp('entityreference');
- }
- protected function assertReferencable($field, $tests, $handler_name) {
- $handler = entityreference_get_selection_handler($field);
- foreach ($tests as $test) {
- foreach ($test['arguments'] as $arguments) {
- $result = call_user_func_array(array($handler, 'getReferencableEntities'), $arguments);
- $this->assertEqual($result, $test['result'], format_string('Valid result set returned by @handler.', array('@handler' => $handler_name)));
- $result = call_user_func_array(array($handler, 'countReferencableEntities'), $arguments);
- if (!empty($test['result'])) {
- $bundle = key($test['result']);
- $count = count($test['result'][$bundle]);
- }
- else {
- $count = 0;
- }
- $this->assertEqual($result, $count, format_string('Valid count returned by @handler.', array('@handler' => $handler_name)));
- }
- }
- }
- /**
- * Test the node-specific overrides of the entity handler.
- */
- 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,
- ),
- );
- $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'],
- ),
- ),
- ),
- 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(),
- ),
- );
- $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['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)');
- }
- /**
- * Test the user-specific overrides of the entity handler.
- */
- public function testUserHandler() {
- // Build a fake field instance.
- $field = array(
- 'translatable' => FALSE,
- 'entity_types' => array(),
- 'settings' => array(
- 'handler' => 'base',
- 'target_type' => 'user',
- 'handler_settings' => array(
- 'target_bundles' => array(),
- ),
- ),
- 'field_name' => 'test_field',
- 'type' => 'entityreference',
- 'cardinality' => '1',
- );
- // Build a set of test data.
- $users = array(
- 'anonymous' => user_load(0),
- 'admin' => user_load(1),
- 'non_admin' => (object) array(
- 'name' => 'non_admin <&>',
- 'mail' => 'non_admin@example.com',
- 'roles' => array(),
- 'pass' => user_password(),
- 'status' => 1,
- ),
- 'blocked' => (object) array(
- 'name' => 'blocked <&>',
- 'mail' => 'blocked@example.com',
- 'roles' => array(),
- 'pass' => user_password(),
- 'status' => 0,
- ),
- );
- // The label of the anonymous user is variable_get('anonymous').
- $users['anonymous']->name = variable_get('anonymous', t('Anonymous'));
- $user_labels = array();
- foreach ($users as $key => $user) {
- if (!isset($user->uid)) {
- $users[$key] = $user = user_save(drupal_anonymous_user(), (array) $user);
- }
- $user_labels[$key] = check_plain($user->name);
- }
- // Test as a non-admin.
- $GLOBALS['user'] = $users['non_admin'];
- $referencable_tests = array(
- array(
- 'arguments' => array(
- array(NULL, 'CONTAINS'),
- ),
- 'result' => array(
- 'user' => array(
- $users['admin']->uid => $user_labels['admin'],
- $users['non_admin']->uid => $user_labels['non_admin'],
- ),
- ),
- ),
- array(
- 'arguments' => array(
- array('non_admin', 'CONTAINS'),
- array('NON_ADMIN', 'CONTAINS'),
- ),
- 'result' => array(
- 'user' => array(
- $users['non_admin']->uid => $user_labels['non_admin'],
- ),
- ),
- ),
- array(
- 'arguments' => array(
- array('invalid user', 'CONTAINS'),
- ),
- 'result' => array(),
- ),
- array(
- 'arguments' => array(
- array('blocked', 'CONTAINS'),
- ),
- 'result' => array(),
- ),
- );
- $this->assertReferencable($field, $referencable_tests, 'User handler');
- $GLOBALS['user'] = $users['admin'];
- $referencable_tests = array(
- array(
- 'arguments' => array(
- array(NULL, 'CONTAINS'),
- ),
- 'result' => array(
- 'user' => array(
- $users['anonymous']->uid => $user_labels['anonymous'],
- $users['admin']->uid => $user_labels['admin'],
- $users['non_admin']->uid => $user_labels['non_admin'],
- $users['blocked']->uid => $user_labels['blocked'],
- ),
- ),
- ),
- array(
- 'arguments' => array(
- array('blocked', 'CONTAINS'),
- ),
- 'result' => array(
- 'user' => array(
- $users['blocked']->uid => $user_labels['blocked'],
- ),
- ),
- ),
- array(
- 'arguments' => array(
- array('Anonymous', 'CONTAINS'),
- array('anonymous', 'CONTAINS'),
- ),
- 'result' => array(
- 'user' => array(
- $users['anonymous']->uid => $user_labels['anonymous'],
- ),
- ),
- ),
- );
- $this->assertReferencable($field, $referencable_tests, 'User handler (admin)');
- }
- /**
- * Test the comment-specific overrides of the entity handler.
- */
- public function testCommentHandler() {
- // Build a fake field instance.
- $field = array(
- 'translatable' => FALSE,
- 'entity_types' => array(),
- 'settings' => array(
- 'handler' => 'base',
- 'target_type' => 'comment',
- 'handler_settings' => array(
- 'target_bundles' => array(),
- ),
- ),
- 'field_name' => 'test_field',
- 'type' => 'entityreference',
- 'cardinality' => '1',
- );
- // Build a set of test data.
- $nodes = array(
- 'published' => (object) array(
- 'type' => 'article',
- 'status' => 1,
- 'title' => 'Node published',
- 'uid' => 1,
- ),
- 'unpublished' => (object) array(
- 'type' => 'article',
- 'status' => 0,
- 'title' => 'Node unpublished',
- 'uid' => 1,
- ),
- );
- foreach ($nodes as $node) {
- node_save($node);
- }
- $comments = array(
- 'published_published' => (object) array(
- 'nid' => $nodes['published']->nid,
- 'uid' => 1,
- 'cid' => NULL,
- 'pid' => 0,
- 'status' => COMMENT_PUBLISHED,
- 'subject' => 'Comment Published <&>',
- 'hostname' => ip_address(),
- 'language' => LANGUAGE_NONE,
- ),
- 'published_unpublished' => (object) array(
- 'nid' => $nodes['published']->nid,
- 'uid' => 1,
- 'cid' => NULL,
- 'pid' => 0,
- 'status' => COMMENT_NOT_PUBLISHED,
- 'subject' => 'Comment Unpublished <&>',
- 'hostname' => ip_address(),
- 'language' => LANGUAGE_NONE,
- ),
- 'unpublished_published' => (object) array(
- 'nid' => $nodes['unpublished']->nid,
- 'uid' => 1,
- 'cid' => NULL,
- 'pid' => 0,
- 'status' => COMMENT_NOT_PUBLISHED,
- 'subject' => 'Comment Published on Unpublished node <&>',
- 'hostname' => ip_address(),
- 'language' => LANGUAGE_NONE,
- ),
- );
- $comment_labels = array();
- foreach ($comments as $key => $comment) {
- comment_save($comment);
- $comment_labels[$key] = check_plain($comment->subject);
- }
- // Test as a non-admin.
- $normal_user = $this->drupalCreateUser(array('access content', 'access comments'));
- $GLOBALS['user'] = $normal_user;
- $referencable_tests = array(
- array(
- 'arguments' => array(
- array(NULL, 'CONTAINS'),
- ),
- 'result' => array(
- 'comment_node_article' => array(
- $comments['published_published']->cid => $comment_labels['published_published'],
- ),
- ),
- ),
- array(
- 'arguments' => array(
- array('Published', 'CONTAINS'),
- ),
- 'result' => array(
- 'comment_node_article' => array(
- $comments['published_published']->cid => $comment_labels['published_published'],
- ),
- ),
- ),
- array(
- 'arguments' => array(
- array('invalid comment', 'CONTAINS'),
- ),
- 'result' => array(),
- ),
- array(
- 'arguments' => array(
- array('Comment Unpublished', 'CONTAINS'),
- ),
- 'result' => array(),
- ),
- );
- $this->assertReferencable($field, $referencable_tests, 'Comment handler');
- // Test as a comment admin.
- $admin_user = $this->drupalCreateUser(array('access content', 'access comments', 'administer comments'));
- $GLOBALS['user'] = $admin_user;
- $referencable_tests = array(
- array(
- 'arguments' => array(
- array(NULL, 'CONTAINS'),
- ),
- 'result' => array(
- 'comment_node_article' => array(
- $comments['published_published']->cid => $comment_labels['published_published'],
- $comments['published_unpublished']->cid => $comment_labels['published_unpublished'],
- ),
- ),
- ),
- );
- $this->assertReferencable($field, $referencable_tests, 'Comment handler (comment admin)');
- // Test as a node and comment admin.
- $admin_user = $this->drupalCreateUser(array('access content', 'access comments', 'administer comments', 'bypass node access'));
- $GLOBALS['user'] = $admin_user;
- $referencable_tests = array(
- array(
- 'arguments' => array(
- array(NULL, 'CONTAINS'),
- ),
- 'result' => array(
- 'comment_node_article' => array(
- $comments['published_published']->cid => $comment_labels['published_published'],
- $comments['published_unpublished']->cid => $comment_labels['published_unpublished'],
- $comments['unpublished_published']->cid => $comment_labels['unpublished_published'],
- ),
- ),
- ),
- );
- $this->assertReferencable($field, $referencable_tests, 'Comment handler (comment + node admin)');
- }
- /**
- * Assert sorting by field works for non-admins.
- *
- * Since we are sorting on a field, we need to make sure the base-table
- * is added, and access-control is behaving as expected.
- */
- public function testSortByField() {
- // Add text field to entity, to sort by.
- $field_info = array(
- 'field_name' => 'field_text',
- 'type' => 'text',
- 'entity_types' => array('node'),
- );
- field_create_field($field_info);
- $instance = array(
- 'label' => 'Text Field',
- 'field_name' => 'field_text',
- 'entity_type' => 'node',
- 'bundle' => 'article',
- 'settings' => array(),
- 'required' => FALSE,
- );
- field_create_instance($instance);
- // 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(),
- // Add sorting.
- 'sort' => array(
- 'type' => 'field',
- 'field' => 'field_text:value',
- 'direction' => 'DESC',
- ),
- ),
- ),
- 'field_name' => 'test_field',
- 'type' => 'entityreference',
- 'cardinality' => '1',
- );
- // Build a set of test data.
- $nodes = array(
- 'published1' => (object) array(
- 'type' => 'article',
- 'status' => 1,
- 'title' => 'Node published1 (<&>)',
- 'uid' => 1,
- 'field_text' => array(
- LANGUAGE_NONE => array(
- array(
- 'value' => 1,
- ),
- ),
- ),
- ),
- 'published2' => (object) array(
- 'type' => 'article',
- 'status' => 1,
- 'title' => 'Node published2 (<&>)',
- 'uid' => 1,
- 'field_text' => array(
- LANGUAGE_NONE => array(
- array(
- 'value' => 2,
- ),
- ),
- ),
- ),
- 'unpublished' => (object) array(
- 'type' => 'article',
- 'status' => 0,
- 'title' => 'Node unpublished (<&>)',
- 'uid' => 1,
- 'field_text' => array(
- LANGUAGE_NONE => array(
- array(
- 'value' => 3,
- ),
- ),
- ),
- ),
- );
- $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;
- $handler = entityreference_get_selection_handler($field);
- // Not only assert the result, but make sure the keys are sorted as
- // expected.
- $result = $handler->getReferencableEntities();
- $expected_result = array(
- $nodes['published2']->nid => $node_labels['published2'],
- $nodes['published1']->nid => $node_labels['published1'],
- );
- $this->assertIdentical($result['article'], $expected_result, 'Query sorted by field returned expected values for non-admin.');
- }
- }
|