@@ -27,10 +27,18 @@ class EntityReferenceHandlersTestCase extends DrupalWebTestCase {
|
||||
foreach ($tests as $test) {
|
||||
foreach ($test['arguments'] as $arguments) {
|
||||
$result = call_user_func_array(array($handler, 'getReferencableEntities'), $arguments);
|
||||
$this->assertEqual($result, $test['result'], t('Valid result set returned by @handler.', array('@handler' => $handler_name)));
|
||||
$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);
|
||||
$this->assertEqual($result, count($test['result']), t('Valid count returned by @handler.', array('@handler' => $handler_name)));
|
||||
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)));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -93,8 +101,10 @@ class EntityReferenceHandlersTestCase extends DrupalWebTestCase {
|
||||
array(NULL, 'CONTAINS'),
|
||||
),
|
||||
'result' => array(
|
||||
$nodes['published1']->nid => $node_labels['published1'],
|
||||
$nodes['published2']->nid => $node_labels['published2'],
|
||||
'article' => array(
|
||||
$nodes['published1']->nid => $node_labels['published1'],
|
||||
$nodes['published2']->nid => $node_labels['published2'],
|
||||
),
|
||||
),
|
||||
),
|
||||
array(
|
||||
@@ -103,7 +113,9 @@ class EntityReferenceHandlersTestCase extends DrupalWebTestCase {
|
||||
array('Published1', 'CONTAINS'),
|
||||
),
|
||||
'result' => array(
|
||||
$nodes['published1']->nid => $node_labels['published1'],
|
||||
'article' => array(
|
||||
$nodes['published1']->nid => $node_labels['published1'],
|
||||
),
|
||||
),
|
||||
),
|
||||
array(
|
||||
@@ -112,7 +124,9 @@ class EntityReferenceHandlersTestCase extends DrupalWebTestCase {
|
||||
array('Published2', 'CONTAINS'),
|
||||
),
|
||||
'result' => array(
|
||||
$nodes['published2']->nid => $node_labels['published2'],
|
||||
'article' => array(
|
||||
$nodes['published2']->nid => $node_labels['published2'],
|
||||
),
|
||||
),
|
||||
),
|
||||
array(
|
||||
@@ -139,9 +153,11 @@ class EntityReferenceHandlersTestCase extends DrupalWebTestCase {
|
||||
array(NULL, 'CONTAINS'),
|
||||
),
|
||||
'result' => array(
|
||||
$nodes['published1']->nid => $node_labels['published1'],
|
||||
$nodes['published2']->nid => $node_labels['published2'],
|
||||
$nodes['unpublished']->nid => $node_labels['unpublished'],
|
||||
'article' => array(
|
||||
$nodes['published1']->nid => $node_labels['published1'],
|
||||
$nodes['published2']->nid => $node_labels['published2'],
|
||||
$nodes['unpublished']->nid => $node_labels['unpublished'],
|
||||
),
|
||||
),
|
||||
),
|
||||
array(
|
||||
@@ -149,7 +165,9 @@ class EntityReferenceHandlersTestCase extends DrupalWebTestCase {
|
||||
array('Node unpublished', 'CONTAINS'),
|
||||
),
|
||||
'result' => array(
|
||||
$nodes['unpublished']->nid => $node_labels['unpublished'],
|
||||
'article' => array(
|
||||
$nodes['unpublished']->nid => $node_labels['unpublished'],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
@@ -215,8 +233,10 @@ class EntityReferenceHandlersTestCase extends DrupalWebTestCase {
|
||||
array(NULL, 'CONTAINS'),
|
||||
),
|
||||
'result' => array(
|
||||
$users['admin']->uid => $user_labels['admin'],
|
||||
$users['non_admin']->uid => $user_labels['non_admin'],
|
||||
'user' => array(
|
||||
$users['admin']->uid => $user_labels['admin'],
|
||||
$users['non_admin']->uid => $user_labels['non_admin'],
|
||||
),
|
||||
),
|
||||
),
|
||||
array(
|
||||
@@ -225,7 +245,9 @@ class EntityReferenceHandlersTestCase extends DrupalWebTestCase {
|
||||
array('NON_ADMIN', 'CONTAINS'),
|
||||
),
|
||||
'result' => array(
|
||||
$users['non_admin']->uid => $user_labels['non_admin'],
|
||||
'user' => array(
|
||||
$users['non_admin']->uid => $user_labels['non_admin'],
|
||||
),
|
||||
),
|
||||
),
|
||||
array(
|
||||
@@ -250,10 +272,12 @@ class EntityReferenceHandlersTestCase extends DrupalWebTestCase {
|
||||
array(NULL, 'CONTAINS'),
|
||||
),
|
||||
'result' => 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'],
|
||||
'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(
|
||||
@@ -261,7 +285,9 @@ class EntityReferenceHandlersTestCase extends DrupalWebTestCase {
|
||||
array('blocked', 'CONTAINS'),
|
||||
),
|
||||
'result' => array(
|
||||
$users['blocked']->uid => $user_labels['blocked'],
|
||||
'user' => array(
|
||||
$users['blocked']->uid => $user_labels['blocked'],
|
||||
),
|
||||
),
|
||||
),
|
||||
array(
|
||||
@@ -270,7 +296,9 @@ class EntityReferenceHandlersTestCase extends DrupalWebTestCase {
|
||||
array('anonymous', 'CONTAINS'),
|
||||
),
|
||||
'result' => array(
|
||||
$users['anonymous']->uid => $user_labels['anonymous'],
|
||||
'user' => array(
|
||||
$users['anonymous']->uid => $user_labels['anonymous'],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
@@ -364,7 +392,9 @@ class EntityReferenceHandlersTestCase extends DrupalWebTestCase {
|
||||
array(NULL, 'CONTAINS'),
|
||||
),
|
||||
'result' => array(
|
||||
$comments['published_published']->cid => $comment_labels['published_published'],
|
||||
'comment_node_article' => array(
|
||||
$comments['published_published']->cid => $comment_labels['published_published'],
|
||||
),
|
||||
),
|
||||
),
|
||||
array(
|
||||
@@ -372,7 +402,9 @@ class EntityReferenceHandlersTestCase extends DrupalWebTestCase {
|
||||
array('Published', 'CONTAINS'),
|
||||
),
|
||||
'result' => array(
|
||||
$comments['published_published']->cid => $comment_labels['published_published'],
|
||||
'comment_node_article' => array(
|
||||
$comments['published_published']->cid => $comment_labels['published_published'],
|
||||
),
|
||||
),
|
||||
),
|
||||
array(
|
||||
@@ -399,8 +431,10 @@ class EntityReferenceHandlersTestCase extends DrupalWebTestCase {
|
||||
array(NULL, 'CONTAINS'),
|
||||
),
|
||||
'result' => array(
|
||||
$comments['published_published']->cid => $comment_labels['published_published'],
|
||||
$comments['published_unpublished']->cid => $comment_labels['published_unpublished'],
|
||||
'comment_node_article' => array(
|
||||
$comments['published_published']->cid => $comment_labels['published_published'],
|
||||
$comments['published_unpublished']->cid => $comment_labels['published_unpublished'],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
@@ -415,12 +449,127 @@ class EntityReferenceHandlersTestCase extends DrupalWebTestCase {
|
||||
array(NULL, 'CONTAINS'),
|
||||
),
|
||||
'result' => 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'],
|
||||
'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.');
|
||||
}
|
||||
}
|
||||
|
115
tests/entityreference.taxonomy.test
Normal file
115
tests/entityreference.taxonomy.test
Normal file
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Test for Entity Reference taxonomy integration.
|
||||
*/
|
||||
class EntityReferenceTaxonomyTestCase extends DrupalWebTestCase {
|
||||
public static function getInfo() {
|
||||
return array(
|
||||
'name' => 'Entity Reference Taxonomy',
|
||||
'description' => 'Tests nodes with reference to terms as indexed.',
|
||||
'group' => 'Entity Reference',
|
||||
);
|
||||
}
|
||||
|
||||
public function setUp() {
|
||||
parent::setUp('entityreference', 'taxonomy');
|
||||
|
||||
// Create an entity reference field.
|
||||
$field = array(
|
||||
'entity_types' => array('node'),
|
||||
'settings' => array(
|
||||
'handler' => 'base',
|
||||
'target_type' => 'taxonomy_term',
|
||||
'handler_settings' => array(
|
||||
'target_bundles' => array(),
|
||||
),
|
||||
),
|
||||
'field_name' => 'field_entityreference_term',
|
||||
'type' => 'entityreference',
|
||||
);
|
||||
$field = field_create_field($field);
|
||||
$instance = array(
|
||||
'field_name' => 'field_entityreference_term',
|
||||
'bundle' => 'article',
|
||||
'entity_type' => 'node',
|
||||
);
|
||||
|
||||
// Enable the taxonomy-index behavior.
|
||||
$instance['settings']['behaviors']['taxonomy-index']['status'] = TRUE;
|
||||
field_create_instance($instance);
|
||||
|
||||
// Create a term reference field.
|
||||
$field = array(
|
||||
'translatable' => FALSE,
|
||||
'entity_types' => array('node'),
|
||||
'settings' => array(
|
||||
'allowed_values' => array(
|
||||
array(
|
||||
'vocabulary' => 'terms',
|
||||
'parent' => 0,
|
||||
),
|
||||
),
|
||||
),
|
||||
'field_name' => 'field_taxonomy_term',
|
||||
'type' => 'taxonomy_term_reference',
|
||||
);
|
||||
$field = field_create_field($field);
|
||||
$instance = array(
|
||||
'field_name' => 'field_taxonomy_term',
|
||||
'bundle' => 'article',
|
||||
'entity_type' => 'node',
|
||||
);
|
||||
field_create_instance($instance);
|
||||
|
||||
// Create a terms vocobulary.
|
||||
$vocabulary = new stdClass();
|
||||
$vocabulary->name = 'Terms';
|
||||
$vocabulary->machine_name = 'terms';
|
||||
taxonomy_vocabulary_save($vocabulary);
|
||||
|
||||
// Create term.
|
||||
for ($i = 1; $i <= 2; $i++) {
|
||||
$term = new stdClass();
|
||||
$term->name = "term $i";
|
||||
$term->vid = 1;
|
||||
taxonomy_term_save($term);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test referencing a term using entity reference field.
|
||||
*/
|
||||
public function testNodeIndex() {
|
||||
// Asert node insert with reference to term.
|
||||
$settings = array();
|
||||
$settings['type'] = 'article';
|
||||
$settings['field_entityreference_term'][LANGUAGE_NONE][0]['target_id'] = 1;
|
||||
$node = $this->drupalCreateNode($settings);
|
||||
|
||||
$this->assertEqual(taxonomy_select_nodes(1), array($node->nid));
|
||||
|
||||
// Asert node update with reference to term.
|
||||
node_save($node);
|
||||
$this->assertEqual(taxonomy_select_nodes(1), array($node->nid));
|
||||
|
||||
// Assert node update with reference to term and taxonomy reference to
|
||||
// another term.
|
||||
$wrapper = entity_metadata_wrapper('node', $node);
|
||||
$wrapper->field_taxonomy_term->set(2);
|
||||
$wrapper->save();
|
||||
|
||||
$this->assertEqual(taxonomy_select_nodes(1), array($node->nid));
|
||||
$this->assertEqual(taxonomy_select_nodes(2), array($node->nid));
|
||||
|
||||
// Assert node update with reference to term and taxonomy reference to
|
||||
// same term.
|
||||
$wrapper->field_taxonomy_term->set(1);
|
||||
$wrapper->save();
|
||||
$this->assertEqual(taxonomy_select_nodes(1), array($node->nid));
|
||||
|
||||
$wrapper->delete();
|
||||
$this->assertFalse(taxonomy_select_nodes(1));
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user