You are here

function OgEntityFieldQueryTestCase::testEntityFieldQuery in Organic groups 7.2

Test the following query scenarios:

  • Single group audience.
  • Multiple group audience.
  • Single group audience first, with another non-audience field.
  • Non-audience field first, with single group audience.
  • Multiple entity types in entityCondition().
  • No entity property.
  • Non-node entity without revision table (e.g. entity_test).
  • Non-node entity without revision table and without bundles (e.g. user).
  • Count query.

File

./og.test, line 1534

Class

OgEntityFieldQueryTestCase
Test queying group-audience fields using entityFieldQuery.

Code

function testEntityFieldQuery() {
  $group1 = $this->group1;
  $group2 = $this->group2;
  $node = $this->node;
  $user1 = $this->user1;
  $entity_test = $this->entity_test;

  // Single group audience.
  $query = new EntityFieldQuery();
  $result = $query
    ->entityCondition('entity_type', 'node')
    ->propertyCondition('type', $node->type)
    ->fieldCondition('og_node', 'target_id', $group2->nid)
    ->execute();
  $this
    ->assertEqual(array_keys($result['node']), array(
    $node->nid,
  ), 'Single group audience query is correct.');

  // Multiple group audience.
  $query = new EntityFieldQuery();
  $result = $query
    ->entityCondition('entity_type', 'node')
    ->propertyCondition('type', $node->type)
    ->fieldCondition('og_node', 'target_id', $group2->nid)
    ->fieldCondition('og_entity_test', 'target_id', $group1->pid)
    ->execute();
  $this
    ->assertEqual(array_keys($result['node']), array(
    $node->nid,
  ), 'Multiple group audience query is correct.');

  // Single group audience first, with another non-audience field.
  $query = new EntityFieldQuery();
  $result = $query
    ->entityCondition('entity_type', 'node')
    ->propertyCondition('type', $node->type)
    ->fieldCondition('og_node', 'target_id', $group2->nid)
    ->fieldCondition('node_reference', 'target_id', $group2->nid)
    ->execute();
  $this
    ->assertEqual(array_keys($result['node']), array(
    $node->nid,
  ), 'Single group audience first, with another non-audience field query is correct.');

  // Non-audience field first, with single group audience.
  $query = new EntityFieldQuery();
  $result = $query
    ->entityCondition('entity_type', 'node')
    ->propertyCondition('type', $node->type)
    ->fieldCondition('node_reference', 'target_id', $group2->nid)
    ->fieldCondition('og_node', 'target_id', $group2->nid)
    ->execute();
  $this
    ->assertEqual(array_keys($result['node']), array(
    $node->nid,
  ), 'Non-audience field first, with single group audience query is correct.');

  // Multiple entity types in entityCondition().
  $query = new EntityFieldQuery();
  $result = $query
    ->entityCondition('entity_type', array(
    'node',
    'user',
  ), 'IN')
    ->fieldCondition('node_reference', 'target_id', $group2->nid)
    ->fieldCondition('og_node', 'target_id', $group2->nid)
    ->execute();
  $this
    ->assertEqual(array_keys($result['node']), array(
    $node->nid,
  ), 'Multiple entity types in entityCondition() query is correct.');

  // No entity property.
  $query = new EntityFieldQuery();
  $result = $query
    ->entityCondition('entity_type', 'node')
    ->fieldCondition('og_node', 'target_id', $group2->nid)
    ->execute();
  $this
    ->assertEqual(array_keys($result['node']), array(
    $node->nid,
  ), 'No entity property query is correct.');

  // Non-node entity without revision table.
  $query = new EntityFieldQuery();
  $result = $query
    ->entityCondition('entity_type', 'entity_test')
    ->fieldCondition('og_node', 'target_id', $group2->nid)
    ->execute();
  $this
    ->assertEqual(array_keys($result['entity_test']), array(
    $entity_test->pid,
  ), 'Non-node entity without revision table query is correct.');

  // Non-node entity without revision table and without bundles.
  $query = new EntityFieldQuery();
  $result = $query
    ->entityCondition('entity_type', 'user')
    ->fieldCondition('og_entity_test', 'target_id', $group2->nid)
    ->execute();
  $expected_values = array(
    $this->user1->uid,
    $this->user2->uid,
  );
  $this
    ->assertEqual(array_keys($result['user']), $expected_values, 'Non-node entity without revision table and without bundles query is correct.');

  // Count query.
  $query = new EntityFieldQuery();
  $result = $query
    ->entityCondition('entity_type', 'node')
    ->propertyCondition('type', $node->type)
    ->fieldCondition('og_node', 'target_id', $group2->nid)
    ->count()
    ->execute();
  $this
    ->assertEqual($result, 1, 'Count query is correct.');
}