You are here

public function RelationAPITest::testRelationTypes in Relation 8.2

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

Tests relation types.

File

src/Tests/RelationAPITest.php, line 186

Class

RelationAPITest
Test general API for Relation.

Namespace

Drupal\relation\Tests

Code

public function testRelationTypes() {

  // Symmetric.
  $related = relation_get_related_entity('node', $this->node1
    ->id());
  $this
    ->assertEqual($this->node4
    ->id(), $related
    ->id());

  // Confirm this works once the related entity has been cached.
  $related = relation_get_related_entity('node', $this->node1
    ->id());
  $this
    ->assertEqual($this->node4
    ->id(), $related
    ->id());

  // Directional.
  // From Parent to Grandparent.
  $directional_relation_type = RelationType::load($this->relation_types['directional']['id']);
  $related = relation_get_related_entity('node', $this->node3
    ->id(), $directional_relation_type
    ->id(), 1);
  $this
    ->assertEqual($this->node1
    ->id(), $related
    ->id());

  // From Parent to Child.
  $related = relation_get_related_entity('node', $this->node3
    ->id(), $directional_relation_type
    ->id(), 0);
  $this
    ->assertEqual($this->node4
    ->id(), $related
    ->id());

  // Test labels.
  $this
    ->assertEqual($directional_relation_type
    ->label(), 'directional');
  $this
    ->assertEqual($directional_relation_type
    ->reverseLabel(), 'reverse_directional');
  $test_relation_type = RelationType::create([
    'id' => 'test_relation_type',
  ]);
  $test_relation_type
    ->save();
  $this
    ->assertEqual($test_relation_type
    ->label(), 'test_relation_type');
  $this
    ->assertEqual($test_relation_type
    ->reverseLabel(), 'test_relation_type');

  // Delete all relations related to node 4, then confirm that these can
  // no longer be found as related entities.
  $relation_ids = relation_query('node', $this->node4
    ->id())
    ->execute();
  foreach (Relation::loadMultiple($relation_ids) as $relation) {
    $relation
      ->delete();
  }
  $this
    ->assertFalse(relation_get_related_entity('node', $this->node4
    ->id()), 'The entity was not loaded after the relation was deleted.');

  // Test get available relation types .
  $available_articles = $this->container
    ->get('entity.repository.relation')
    ->getAvailable('node', 'article');
  $article_labels = [];
  foreach ($available_articles as $relation) {
    $article_labels[] = $relation
      ->label();
  }

  // Expect 3 available relation types for node article.
  $this
    ->assertEqual($article_labels, [
    'directional',
    'octopus',
    'symmetric',
  ]);
  $available_users = $this->container
    ->get('entity.repository.relation')
    ->getAvailable('user', '*');
  $user_labels = [];
  foreach ($available_users as $relation) {
    $user_labels[] = $relation
      ->label();
  }

  // Expect 2 available relation types for user.
  $this
    ->assertEqual($user_labels, [
    'directional_entitydifferent',
    'symmetric',
  ]);
}