You are here

public function RelationAPITest::testRelationHelpers in Relation 8

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

Test relation helper functions.

File

src/Tests/RelationAPITest.php, line 46

Class

RelationAPITest
Test general API for Relation.

Namespace

Drupal\relation\Tests

Code

public function testRelationHelpers() {

  // ## Test relationExists() method of entity repository relation service.
  // Where relation type is set.
  $exists = $this->container
    ->get('entity.repository.relation')
    ->relationExists($this->endpoints, $this->relation_type_symmetric);
  $this
    ->verbose(print_r($exists, TRUE));
  $this
    ->assertTrue(isset($exists[$this->relation_id_symmetric]), 'Relation exists.');

  // Where relation type is not set.
  $exists = $this->container
    ->get('entity.repository.relation')
    ->relationExists($this->endpoints_4);
  $this
    ->assertTrue(isset($exists[$this->relation_id_octopus]), 'Relation exists.');

  // Where endpoints does not exist.
  $endpoints_do_not_exist = $this->endpoints;
  $endpoints_do_not_exist[1]['entity_type'] = $this
    ->randomMachineName();
  $this
    ->assertEqual(array(), $this->container
    ->get('entity.repository.relation')
    ->relationExists($endpoints_do_not_exist, $this->relation_type_symmetric), 'Relation with non-existant endpoint not found.');

  // Where there are too many endpoints.
  $endpoints_excessive = $this->endpoints;
  $endpoints_excessive[] = [
    'entity_type' => $this
      ->randomMachineName(),
    'entity_id' => 1000,
  ];
  $this
    ->assertEqual(array(), $this->container
    ->get('entity.repository.relation')
    ->relationExists($endpoints_do_not_exist, $this->relation_type_symmetric), 'Relation with too many endpoints not found.');

  // Where relation type is invalid.
  $this
    ->assertEqual(array(), $this->container
    ->get('entity.repository.relation')
    ->relationExists($this->endpoints, $this
    ->randomMachineName()), 'Relation with invalid relation type not found.');
}