Commit 05f64fd0 authored by joachim's avatar joachim Committed by amitaibu

Issue #2030787 by joachim, DuaelFr: EntityFieldQuery with fieldCondition() on...

Issue #2030787 by joachim, DuaelFr: EntityFieldQuery with fieldCondition() on OG group audience field breaks if other fieldConditions come before the OG field and there are multiple group content entityypes with .
parent 9de6375f
......@@ -1474,7 +1474,28 @@ function og_query_og_membership_alter(QueryAlterableInterface $query) {
$tables = array_merge(array('ogm' => $ogm), $tables);
}
else {
$query->join('og_membership', 'ogm', "ogm.etid = $base_table_alias.entity_id");
// If the original EntityFieldQuery has an entity type entityCondition,
// restrict the join by this. Otherwise, we would bring in the IDs of
// entities of other types if they happen to match on the base table entity
// ID.
$query_base_entity_type = $query->alterMetaData['entity_field_query']->entityConditions['entity_type']['value'];
if (empty($query_base_entity_type)) {
// It's possible to have no entity type specified: join without it.
$query->join('og_membership', 'ogm', "ogm.etid = $base_table_alias.entity_id");
}
else {
if (is_array($query_base_entity_type)) {
// It's also possible for the entity type to be multiple.
$query->join('og_membership', 'ogm', "ogm.etid = $base_table_alias.entity_id AND ogm.entity_type IN (:entity_type)", array(
':entity_type' => $query_base_entity_type,
));
}
else {
$query->join('og_membership', 'ogm', "ogm.etid = $base_table_alias.entity_id AND ogm.entity_type = :entity_type", array(
':entity_type' => $query_base_entity_type,
));
}
}
}
_og_query_og_membership_alter_conditions($conditions, $aliases, $base_table_alias, $entity_info);
......
......@@ -1512,6 +1512,174 @@ class OgEntityFieldQueryTestCase extends DrupalWebTestCase {
}
}
/**
* EntityFieldQuery FieldConditions with multiple group content entity types.
*/
class OgEntityFieldQueryFieldConditionTestCase extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'OG field condition query',
'description' => 'Test querying field condition fields using entityFieldQuery.',
'group' => 'Organic groups',
);
}
function setUp() {
parent::setUp('og', 'entity_feature');
$user1 = $this->drupalCreateUser();
$user2 = $this->drupalCreateUser();
$type = $this->drupalCreateContentType();
$this->group_type = $type->type;
$type = $this->drupalCreateContentType();
$this->group_content_type = $type->type;
og_create_field(OG_GROUP_FIELD, 'node', $this->group_type);
// Add audience field to group content node type.
$og_field = og_fields_info(OG_AUDIENCE_FIELD);
og_create_field(OG_AUDIENCE_FIELD, 'node', $this->group_content_type, $og_field);
// Add audience field to group content test entity type.
$og_field = og_fields_info(OG_AUDIENCE_FIELD);
og_create_field(OG_AUDIENCE_FIELD, 'entity_test', 'main', $og_field);
// Create a simple text list field.
$field = array(
'entity_types' => array('node'),
'settings' => array(
'allowed_values' => array(
'red' => 'red',
'blue' => 'blue',
),
),
'field_name' => 'list_text',
'type' => 'list_text',
'cardinality' => 1,
);
$field = field_create_field($field);
$instance = array(
'field_name' => 'list_text',
'bundle' => $this->group_content_type,
'entity_type' => 'node',
);
field_create_instance($instance);
// Create groups.
$settings = array(
'type' => $this->group_type,
'uid' => $user1->uid,
);
$settings[OG_GROUP_FIELD][LANGUAGE_NONE][0]['value'] = 1;
$group1 = $this->drupalCreateNode($settings);
$settings = array(
'type' => $this->group_type,
'uid' => $user1->uid,
);
$settings[OG_GROUP_FIELD][LANGUAGE_NONE][0]['value'] = 1;
$group2 = $this->drupalCreateNode($settings);
// Create group-content.
// First content node.
$settings = array(
'type' => $this->group_content_type,
'uid' => $user1->uid,
);
$settings['list_text'][LANGUAGE_NONE][0]['value'] = 'red';
$node = $this->drupalCreateNode($settings);
$values = array(
'entity_type' => 'node',
'entity' => $node,
);
og_group('node', $group1, $values);
// Second content node.
$settings = array(
'type' => $this->group_content_type,
'uid' => $user1->uid,
);
$settings['list_text'][LANGUAGE_NONE][0]['value'] = 'red';
$node = $this->drupalCreateNode($settings);
$values = array(
'entity_type' => 'node',
'entity' => $node,
);
og_group('node', $group2, $values);
// Entity test content.
// We need to create enough test entities so that we get some whose IDs are
// the same as the group content nodes.
foreach (range(1, 5) as $i) {
$entity_test = entity_create('entity_test', array(
// Weirdly, 'name' is the bundle key.
'name' => 'main',
'uid' => $user1->uid,
));
$entity_test->save();
$values = array(
'entity_type' => 'entity_test',
'entity' => $entity_test,
);
og_group('node', $group1, $values);
}
$this->group1 = $group1;
$this->group2 = $group2;
}
function testEntityFieldQueryFieldConditions() {
// Query for all nodes with a field value of 'red'.
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node');
$query->entityCondition('bundle', $this->group_content_type);
$query->fieldCondition('list_text', 'value', 'red');
$result = $query->execute();
$this->assertEqual(count($result['node']), 2, "The correct number of nodes was returned for the query with only the plain field.");
// Query for group content nodes.
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node');
$query->entityCondition('bundle', $this->group_content_type);
// Condition on the group audience field.
$query->fieldCondition(OG_AUDIENCE_FIELD, 'target_id', $this->group1->nid);
$result = $query->execute();
$this->assertEqual(count($result['node']), 1, "The correct number of nodes was returned for the query with only the OG field condition.");
// Query for nodes in group 1 with a field value of 'red'.
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node');
$query->entityCondition('bundle', $this->group_content_type);
// Condition on the group audience field.
$query->fieldCondition(OG_AUDIENCE_FIELD, 'target_id', $this->group1->nid);
$query->fieldCondition('list_text', 'value', 'red');
$result = $query->execute();
$this->assertEqual(count($result['node']), 1, "The correct number of nodes was returned for the query with the OG field condition first.");
// Query for nodes in group 1 with a field value of 'red', but this time
// with the field conditions the other way round.
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node');
$query->entityCondition('bundle', $this->group_content_type);
$query->fieldCondition('list_text', 'value', 'red');
// Condition on the group audience field.
$query->fieldCondition(OG_AUDIENCE_FIELD, 'target_id', $this->group1->nid);
$result = $query->execute();
$this->assertEqual(count($result['node']), 1, "The correct number of nodes was returned for the query with the OG field condition second.");
}
}
/**
* Test group-audience field auto-create.
*/
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment