You are here

public function RelationAPITest::testRelationSave in Relation 8.2

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

Tests saving of relations.

File

src/Tests/RelationAPITest.php, line 241

Class

RelationAPITest
Test general API for Relation.

Namespace

Drupal\relation\Tests

Code

public function testRelationSave() {
  foreach ($this->relation_types as $value) {
    $relation_type = $value['id'];
    $endpoints = $this->endpoints;
    if (isset($value['min_arity'])) {
      $endpoints = $value['min_arity'] == 1 ? $this->endpoints_unary : $this->endpoints_4;
    }
    if ($relation_type == 'directional_entitydifferent') {
      $endpoints = $this->endpoints_entitydifferent;
    }
    $relation = Relation::create(array(
      'relation_type' => $relation_type,
    ));
    $relation->endpoints = $endpoints;
    $relation
      ->save();
    $this
      ->assertTrue($relation
      ->id(), 'Relation created.');
    $count = count($relation->endpoints);
    $this
      ->assertEqual($count, count($endpoints));
    $this
      ->assertEqual($relation->arity->value, count($endpoints));
    $this
      ->assertEqual($relation
      ->bundle(), $relation_type);
    foreach ($relation->endpoints as $endpoint) {
      $need_ids[$endpoint->target_id] = TRUE;
    }
    foreach ($relation->endpoints as $delta => $endpoint) {
      $this
        ->assertEqual($endpoint->target_type, $endpoints[$delta]['target_type'], 'The entity type is ' . $endpoints[$delta]['target_type'] . ': ' . $endpoint->target_type);
      $this
        ->assertTrue(isset($need_ids[$endpoint->target_id]), 'The entity ID is correct: ' . $need_ids[$endpoint->target_id]);
      unset($need_ids[$endpoint->target_id]);
    }
    $this
      ->assertFalse($need_ids, 'All ids found.');

    // Confirm the relation_id in revision table.
    $revision = \Drupal::database()
      ->select('relation_revision', 'v')
      ->fields('v', array(
      'relation_id',
    ))
      ->condition('revision_id', $relation
      ->getRevisionId())
      ->execute()
      ->fetchAllAssoc('relation_id');
    $this
      ->assertTrue(array_key_exists($relation
      ->id(), $revision), 'Relation revision created.');
  }
}