You are here

function EntityReferenceRevisionsCompositeTest::testCompositeRelationshipWithRevisions in Entity Reference Revisions 8

Tests composite relationship with revisions.

File

tests/src/Kernel/EntityReferenceRevisionsCompositeTest.php, line 330

Class

EntityReferenceRevisionsCompositeTest
Tests the entity_reference_revisions composite relationship.

Namespace

Drupal\Tests\entity_reference_revisions\Kernel

Code

function testCompositeRelationshipWithRevisions() {

  // Create the test composite entity with a translation.
  $composite = EntityTestCompositeRelationship::create(array(
    'uuid' => $this
      ->randomMachineName(),
    'name' => $this
      ->randomMachineName(),
  ));
  $composite
    ->save();

  // Create a node with a reference to the test composite entity.
  $node = Node::create(array(
    'title' => $this
      ->randomMachineName(),
    'type' => 'article',
    'composite_reference' => $composite,
  ));
  $node
    ->save();

  // Verify the value of parent type and id after create a node.
  $composite = EntityTestCompositeRelationship::load($composite
    ->id());
  $composite_original_revision_id = $composite
    ->getRevisionId();
  $node_original_revision_id = $node
    ->getRevisionId();
  $this
    ->assertEquals($node
    ->getEntityTypeId(), $composite->parent_type->value);
  $this
    ->assertEquals($node
    ->id(), $composite->parent_id->value);
  $this
    ->assertEquals('composite_reference', $composite->parent_field_name->value);
  $node
    ->setNewRevision(TRUE);
  $node
    ->save();

  // Ensure that we saved a new revision ID.
  $composite = EntityTestCompositeRelationship::load($composite
    ->id());
  $this
    ->assertNotEquals($composite_original_revision_id, $composite
    ->getRevisionId());

  // Test that deleting the first revision does not delete the composite.
  $this->entityTypeManager
    ->getStorage('node')
    ->deleteRevision($node_original_revision_id);
  $composite = EntityTestCompositeRelationship::load($composite
    ->id());
  $this
    ->assertNotNull($composite);

  // Ensure that the composite revision was deleted as well.
  $composite_revision = $this->entityTypeManager
    ->getStorage('entity_test_composite')
    ->loadRevision($composite_original_revision_id);
  $this
    ->assertNull($composite_revision);

  // Test that the composite entity is deleted when its parent is deleted.
  $node
    ->delete();
  $this->cron
    ->run();
  $composite = EntityTestCompositeRelationship::load($composite
    ->id());
  $this
    ->assertNull($composite);
}