public function EntityReferenceRevisionsCompositeTest::testEntityReferenceRevisionsCompositeRelationship in Entity Reference Revisions 8
Test for maintaining composite relationship.
Tests that the referenced entity saves the parent type and id when saving.
File
- tests/
src/ Kernel/ EntityReferenceRevisionsCompositeTest.php, line 100
Class
- EntityReferenceRevisionsCompositeTest
- Tests the entity_reference_revisions composite relationship.
Namespace
Drupal\Tests\entity_reference_revisions\KernelCode
public function testEntityReferenceRevisionsCompositeRelationship() {
// Create the test composite entity.
$composite = EntityTestCompositeRelationship::create(array(
'uuid' => $this
->randomMachineName(),
'name' => $this
->randomMachineName(),
));
$composite
->save();
// Assert that there is only 1 revision of the composite entity.
$composite_revisions_count = \Drupal::entityQuery('entity_test_composite')
->condition('uuid', $composite
->uuid())
->allRevisions()
->count()
->execute();
$this
->assertEquals(1, $composite_revisions_count);
// Create a node with a reference to the test composite entity.
/** @var \Drupal\node\NodeInterface $node */
$node = Node::create(array(
'title' => $this
->randomMachineName(),
'type' => 'article',
));
$node
->save();
$node
->set('composite_reference', $composite);
$this
->assertTrue($node
->hasTranslationChanges());
$node
->save();
// Assert that there is only 1 revision when creating a node.
$node_revisions_count = \Drupal::entityQuery('node')
->condition('nid', $node
->id())
->allRevisions()
->count()
->execute();
$this
->assertEquals(1, $node_revisions_count);
// Assert there is no new composite revision after creating a host entity.
$composite_revisions_count = \Drupal::entityQuery('entity_test_composite')
->condition('uuid', $composite
->uuid())
->allRevisions()
->count()
->execute();
$this
->assertEquals(1, $composite_revisions_count);
// Verify the value of parent type and id after create a node.
$composite = EntityTestCompositeRelationship::load($composite
->id());
$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);
// Create second revision of the node.
$original_composite_revision = $node->composite_reference[0]->target_revision_id;
$original_node_revision = $node
->getRevisionId();
$node
->setTitle('2nd revision');
$node
->setNewRevision();
$node
->save();
$node = Node::load($node
->id());
// Check the revision of the node.
$this
->assertEquals('2nd revision', $node
->getTitle(), 'New node revision has changed data.');
$this
->assertNotEquals($original_composite_revision, $node->composite_reference[0]->target_revision_id, 'Composite entity got new revision when its host did.');
// Make sure that there are only 2 revisions.
$node_revisions_count = \Drupal::entityQuery('node')
->condition('nid', $node
->id())
->allRevisions()
->count()
->execute();
$this
->assertEquals(2, $node_revisions_count);
// Revert to first revision of the node.
$node = $this->entityTypeManager
->getStorage('node')
->loadRevision($original_node_revision);
$node
->setNewRevision();
$node
->isDefaultRevision(TRUE);
$node
->save();
$node = Node::load($node
->id());
// Check the revision of the node.
$this
->assertNotEquals('2nd revision', $node
->getTitle(), 'Node did not keep changed title after reversion.');
$this
->assertNotEquals($original_composite_revision, $node->composite_reference[0]->target_revision_id, 'Composite entity got new revision when its host reverted to an old revision.');
$node_storage = $this->entityTypeManager
->getStorage('node');
// Test that removing composite references results in translation changes.
$node
->set('composite_reference', []);
$this
->assertTrue($node
->hasTranslationChanges());
// Test that changing composite reference results in translation changes.
$changed_composite_reference = $composite;
$changed_composite_reference
->set('name', 'Changing composite reference');
$this
->assertTrue((bool) $changed_composite_reference
->isRevisionTranslationAffected());
$node
->set('composite_reference', $changed_composite_reference);
$node
->setNewRevision();
$this
->assertTrue($node
->hasTranslationChanges());
$node
->save();
$nid = $node
->id();
$node_storage
->resetCache([
$nid,
]);
/** @var \Drupal\node\NodeInterface $node */
$node = $node_storage
->load($nid);
// Check the composite has changed.
$this
->assertEquals('Changing composite reference', $node
->get('composite_reference')->entity
->getName());
// Make sure the node has 4 revisions.
$node_revisions_count = $node_storage
->getQuery()
->condition('nid', $nid)
->allRevisions()
->count()
->execute();
$this
->assertEqual($node_revisions_count, 4);
// Make sure the node has no revision with revision translation affected
// flag set to NULL.
$node_revisions_count = $node_storage
->getQuery()
->condition('nid', $nid)
->allRevisions()
->condition('revision_translation_affected', NULL, 'IS NULL')
->count()
->execute();
$this
->assertEqual($node_revisions_count, 0, 'Node has a revision with revision translation affected set to NULL');
// Revert the changes to avoid interfering with the delete test.
$node
->set('composite_reference', $composite);
// Test that the composite entity is deleted when its parent is deleted.
$node
->delete();
$this
->assertNotNull(EntityTestCompositeRelationship::load($composite
->id()));
$this->cron
->run();
$this
->assertNull(EntityTestCompositeRelationship::load($composite
->id()));
// Test that the deleting composite entity does not break the parent entity
// when creating a new revision.
$composite = EntityTestCompositeRelationship::create([
'name' => $this
->randomMachineName(),
]);
$composite
->save();
// Create a node with a reference to the test composite entity.
/** @var \Drupal\node\NodeInterface $node */
$node = Node::create([
'title' => $this
->randomMachineName(),
'type' => 'article',
'composite_reference' => $composite,
]);
$node
->save();
// Delete the composite entity.
$composite
->delete();
// Re-apply the field item values to unset the computed "entity" property.
$field_item = $node
->get('composite_reference')
->get(0);
$field_item
->setValue($field_item
->getValue(), FALSE);
$new_revision = $this->entityTypeManager
->getStorage('node')
->createRevision($node);
$this
->assertTrue($new_revision
->get('composite_reference')
->isEmpty());
}