You are here

function EntityReferenceRevisionsCompositeTest::testCompositeRelationshipDuplicatedRevisions in Entity Reference Revisions 8

Tests that the composite revision is not deleted if it is still in use.

File

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

Class

EntityReferenceRevisionsCompositeTest
Tests the entity_reference_revisions composite relationship.

Namespace

Drupal\Tests\entity_reference_revisions\Kernel

Code

function testCompositeRelationshipDuplicatedRevisions() {

  // 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);

  // Set the new node revision to reference to the original composite
  // revision as well to test this composite revision will not be deleted.
  $this->database
    ->update('node__composite_reference')
    ->fields([
    'composite_reference_target_revision_id' => $composite_original_revision_id,
  ])
    ->condition('revision_id', $node
    ->getRevisionId())
    ->execute();
  $this->database
    ->update('node_revision__composite_reference')
    ->fields([
    'composite_reference_target_revision_id' => $composite_original_revision_id,
  ])
    ->condition('revision_id', $node
    ->getRevisionId())
    ->execute();

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

  // Ensure the original composite revision is not deleted because it is
  // still referenced by the second node revision.
  $composite_original_revision = $this->entityTypeManager
    ->getStorage('entity_test_composite')
    ->loadRevision($composite_original_revision_id);
  $this
    ->assertNotNull($composite_original_revision);

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

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