You are here

public function EntityReferenceRevisionsCompositeTest::testCompositeDeleteAfterChangingParent in Entity Reference Revisions 8

Tests the composite entity is not deleted when changing parents.

Includes revisions on the host entity.

File

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

Class

EntityReferenceRevisionsCompositeTest
Tests the entity_reference_revisions composite relationship.

Namespace

Drupal\Tests\entity_reference_revisions\Kernel

Code

public function testCompositeDeleteAfterChangingParent() {
  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();
  $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 new parent is
  // deleted, since it is still being used in a previous revision with a
  // different parent.
  $second_node
    ->delete();
  $this->cron
    ->run();
  $composite = EntityTestCompositeRelationship::load($composite
    ->id());
  $this
    ->assertNotNull($composite);

  // Delete the parent of the previous revision.
  $node
    ->delete();

  // Verify that the composite entity is removed after running cron.
  $this->cron
    ->run();
  $composite = EntityTestCompositeRelationship::load($composite
    ->id());
  $this
    ->assertNull($composite);
}