You are here

public function EntityReferenceRevisionsCompositeTest::testCompositeDeleteRevisionAfterChangingParent in Entity Reference Revisions 8

Composite entity with revisions isn't deleted when changing parents.

Includes revisions on the host entity.

File

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

Class

EntityReferenceRevisionsCompositeTest
Tests the entity_reference_revisions composite relationship.

Namespace

Drupal\Tests\entity_reference_revisions\Kernel

Code

public function testCompositeDeleteRevisionAfterChangingParent() {
  list($composite, $node) = $this
    ->assignCompositeToNode();

  // Remove reference to the composite entity from the node.
  $node
    ->set('composite_reference', NULL);
  $node
    ->setNewRevision();
  $node
    ->save();

  // Setting a new revision of the composite entity in the second node.
  $composite = EntityTestCompositeRelationship::load($composite
    ->id());
  $composite
    ->setNewRevision(TRUE);
  $composite
    ->save();
  $composite = EntityTestCompositeRelationship::load($composite
    ->id());
  $second_node = Node::create([
    'title' => 'Second node',
    'type' => 'article',
    'composite_reference' => $composite,
  ]);
  $second_node
    ->save();

  // Remove reference to the composite entity from the node.
  $second_node
    ->set('composite_reference', NULL);
  $second_node
    ->setNewRevision(TRUE);
  $second_node
    ->save();

  // Verify the composite entity is not removed on nodes with revisions.
  $composite = EntityTestCompositeRelationship::load($composite
    ->id());
  $this
    ->assertNotNull($composite);

  // Verify the amount of revisions of each entity.
  $this
    ->assertRevisionCount(2, 'entity_test_composite', $composite
    ->id());
  $this
    ->assertRevisionCount(2, 'node', $node
    ->id());
  $this
    ->assertRevisionCount(2, 'node', $second_node
    ->id());

  // Test that the composite entity is not deleted when its old parent is
  // deleted.
  $node
    ->delete();
  $composite = EntityTestCompositeRelationship::load($composite
    ->id());
  $this
    ->assertNotNull($composite);

  // Verify that the composite entity is not removed after running cron but
  // the previous unused revision is deleted.
  $this->cron
    ->run();
  $composite = EntityTestCompositeRelationship::load($composite
    ->id());
  $this
    ->assertNotNull($composite);
  $this
    ->assertRevisionCount(1, 'entity_test_composite', $composite
    ->id());
}