You are here

public function RelationAPITest::testRelationHelpers in Relation 8.2

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

Test relation helper functions.

File

src/Tests/RelationAPITest.php, line 52

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]['target_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[] = [
    'target_type' => $this
      ->randomMachineName(),
    'target_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.');
}