You are here

function EntityReferenceRevisionsCompositeTest::testCompositeRelationshipDefaultRevision in Entity Reference Revisions 8

Tests that the composite revision is not deleted if it is the default one.

File

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

Class

EntityReferenceRevisionsCompositeTest
Tests the entity_reference_revisions composite relationship.

Namespace

Drupal\Tests\entity_reference_revisions\Kernel

Code

function testCompositeRelationshipDefaultRevision() {

  // Create a node with a reference to a test composite entity.
  $composite = EntityTestCompositeRelationship::create([
    'uuid' => $this
      ->randomMachineName(),
    'name' => $this
      ->randomMachineName(),
  ]);
  $composite
    ->save();
  $node = Node::create([
    'title' => $this
      ->randomMachineName(),
    'type' => 'article',
    'composite_reference' => $composite,
  ]);
  $node
    ->save();
  $composite = EntityTestCompositeRelationship::load($composite
    ->id());
  $composite_original_revision_id = $composite
    ->getRevisionId();
  $node_original_revision_id = $node
    ->getRevisionId();

  // Set a new revision, composite entity should have a new revision as well.
  $node
    ->setNewRevision(TRUE);
  $node
    ->save();

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

  // Revert default composite entity revision to the original revision.
  $composite_original = $this->entityTypeManager
    ->getStorage('entity_test_composite')
    ->loadRevision($composite_original_revision_id);
  $composite_original
    ->isDefaultRevision(TRUE);
  $composite_original
    ->save();

  // Check the default composite revision is the original composite revision.
  $this
    ->assertEquals($composite_original_revision_id, $composite_original
    ->getrevisionId());

  // Test deleting the first node revision, referencing to the default
  // composite revision, does not delete the default composite revision.
  $this->entityTypeManager
    ->getStorage('node')
    ->deleteRevision($node_original_revision_id);
  $composite_default = EntityTestCompositeRelationship::load($composite_original
    ->id());
  $this
    ->assertNotNull($composite_default);
  $composite_default_revision = $this->entityTypeManager
    ->getStorage('entity_test_composite')
    ->loadRevision($composite_original
    ->getrevisionId());
  $this
    ->assertNotNull($composite_default_revision);

  // Ensure the second revision still exists.
  $composite2_revision = $this->entityTypeManager
    ->getStorage('entity_test_composite')
    ->loadRevision($composite2_rev_id);
  $this
    ->assertNotNull($composite2_revision);
}