You are here

function RelationAPITestCase::testRelationQuery in Relation 7

Tests all available methods in RelationQuery. Creates some nodes, add some relations and checks if they are related.

File

tests/relation.test, line 249
Tests for Relation module.

Class

RelationAPITestCase
Tests Relation API.

Code

function testRelationQuery() {
  $relations = entity_load('relation', array_keys(relation_query('node', $this->node1->nid)
    ->execute()));

  // Check that symmetric relation is correctly related to node 4.
  $this
    ->assertEqual($relations[$this->rid_symmetric]->endpoints[LANGUAGE_NONE][1]['entity_id'], $this->node4->nid, 'Correct entity is related: ' . $relations[$this->rid_symmetric]->endpoints[LANGUAGE_NONE][1]['entity_id'] . '==' . $this->node4->nid);

  // Symmetric relation is Article 1 <--> Page 4
  $entity_keys = array(
    array(
      'entity_type' => 'node',
      'entity_id' => $this->node4->nid,
    ),
    array(
      'entity_type' => 'node',
      'entity_id' => $this->node4->nid,
    ),
  );
  $this
    ->assertFalse(relation_relation_exists($entity_keys, 'symmetric'), 'node4 is not related to node4.');

  // Get relations for node 1, should return 3 relations.
  $count = count($relations);
  $this
    ->assertEqual($count, 3);

  // Get number of relations for node 4, should return 6 relations.
  $count = relation_query('node', $this->node4->nid)
    ->count()
    ->execute();
  $this
    ->assertEqual($count, 6);

  // Get number of relations for node 5, should return 2 relations.
  $count = relation_query('node', $this->node5->nid)
    ->count()
    ->execute();
  $this
    ->assertEqual($count, 2);

  // Get relations between entities 2 and 5 (none).
  $count = relation_query('node', $this->node2->nid)
    ->related('node', $this->node5->nid)
    ->count()
    ->execute();
  $this
    ->assertFalse($count);

  // Get directed relations for node 3 using index, should return 2 relations.
  // The other node 3 relation has an r_index 0.
  $relations = relation_query('node', $this->node3->nid, 1)
    ->execute();
  $this
    ->assertEqual(count($relations), 3);
  $this
    ->assertTrue(isset($relations[$this->rid_directional]), 'Got the correct directional relation for nid=3.');

  // Get relations between entities 2 and 3 (octopus).
  $relations = relation_query('node', $this->node2->nid)
    ->related('node', $this->node3->nid)
    ->execute();
  $count = count($relations);
  $this
    ->assertEqual($count, 1);

  // Check that we have the correct relations
  $this
    ->assertEqual(isset($relations[$this->rid_octopus]), 'Got one correct relation.');

  // Get relations for node 1 (symmetric, directional, octopus), limit to
  // directional and octopus with relation_type().
  $relations = relation_query('node', $this->node1->nid)
    ->propertyCondition('relation_type', array(
    $this->relation_types['directional']['relation_type'],
    $this->relation_types['octopus']['relation_type'],
  ))
    ->execute();
  $count = count($relations);
  $this
    ->assertEqual($count, 2);

  // Check that we have the correct relations.
  $this
    ->assertTrue(isset($relations[$this->rid_directional]), 'Got one correct relation.');
  $this
    ->assertTrue(isset($relations[$this->rid_octopus]), 'Got a second one.');

  // Get last two relations for node 1.
  $relations = relation_query('node', $this->node1->nid)
    ->range(1, 2)
    ->propertyOrderBy('rid', 'ASC')
    ->execute();
  $count = count($relations);
  $this
    ->assertEqual($count, 2);

  // Check that we have the correct relations.
  $this
    ->assertTrue(isset($relations[$this->rid_directional]), 'Got one correct relation.');
  $this
    ->assertTrue(isset($relations[$this->rid_octopus]), 'Got a second one.');

  // Get all relations on node 1 and sort them in reverse created order.
  $relations = relation_query('node', $this->node1->nid)
    ->propertyOrderBy('created', 'DESC')
    ->execute();
  $this
    ->assertEqual(array_keys($relations), array(
    $this->rid_octopus,
    $this->rid_directional,
    $this->rid_symmetric,
  ));

  // Create 10 more symmetric relations and verify that the count works with
  // double digit counts as well.
  for ($i = 0; $i < 10; $i++) {
    $this
      ->createRelationSymmetric();
  }
  $count = relation_query('node', $this->node4->nid)
    ->count()
    ->execute();
  $this
    ->assertEqual($count, 16);
}