You are here

public function RelationAPITest::testRelationQuery in Relation 8.2

Same name and namespace in other branches
  1. 8 src/Tests/RelationAPITest.php \Drupal\relation\Tests\RelationAPITest::testRelationQuery()

Tests all available methods in RelationQuery.

Creates some nodes, add some relations and checks if they are related.

File

src/Tests/RelationAPITest.php, line 83

Class

RelationAPITest
Test general API for Relation.

Namespace

Drupal\relation\Tests

Code

public function testRelationQuery() {
  $relations = Relation::loadMultiple(array_keys(relation_query('node', $this->node1
    ->id())
    ->execute()));

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

  // Symmetric relation is Article 1 <--> Page 4
  // @see https://drupal.org/node/1760026
  $endpoints = [
    [
      'target_type' => 'node',
      'target_id' => $this->node4
        ->id(),
    ],
    [
      'target_type' => 'node',
      'target_id' => $this->node4
        ->id(),
    ],
  ];
  $exists = $this->container
    ->get('entity.repository.relation')
    ->relationExists($endpoints, 'symmetric');
  $this
    ->assertTrue(empty($exists), '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
    ->id())
    ->count()
    ->execute();
  $this
    ->assertEqual($count, 6);

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

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

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

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

  // Check that we have the correct relations.
  $this
    ->assertEqual(isset($relations[$this->relation_id_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
    ->id());
  $or_condition = $relations
    ->orConditionGroup()
    ->condition('relation_type', $this->relation_types['directional']['id'])
    ->condition('relation_type', $this->relation_types['octopus']['id']);
  $relations = $relations
    ->condition($or_condition)
    ->execute();
  $count = count($relations);
  $this
    ->assertEqual($count, 2);

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

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

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

  // Get all relations on node 1 and sort them in reverse created order.
  $relations = relation_query('node', $this->node1
    ->id())
    ->sort('created', 'DESC')
    ->execute();
  $this
    ->assertEqual($relations, [
    $this->relation_id_octopus => $this->relation_id_octopus,
    $this->relation_id_directional => $this->relation_id_directional,
    $this->relation_id_symmetric => $this->relation_id_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
    ->id())
    ->count()
    ->execute();
  $this
    ->assertEqual($count, 16);
}