/**
  * Test for maintaining composite relationship.
  *
  * Tests that the referenced entity saves the parent type and id when saving.
  */
 public function testEntityReferenceRevisionsCompositeRelationship()
 {
     // Create the reference to the composite entity test.
     $field_storage = FieldStorageConfig::create(array('field_name' => 'composite_reference', 'entity_type' => 'node', 'type' => 'entity_reference_revisions', 'settings' => array('target_type' => 'entity_test_composite')));
     $field_storage->save();
     $field = FieldConfig::create(array('field_storage' => $field_storage, 'bundle' => 'article'));
     $field->save();
     // Create the test composite entity.
     $composite = EntityTestCompositeRelationship::create(array('uuid' => $this->randomMachineName(), 'name' => $this->randomMachineName()));
     $composite->save();
     // Create a node with a reference to the test composite entity.
     $node = $this->createNode(array('title' => $this->randomMachineName(), 'type' => 'article', 'composite_reference' => $composite));
     // Verify the value of parent type and id after create a node.
     $composite_after = EntityTestCompositeRelationship::load($composite->id())->toArray();
     $this->assertEqual($composite_after['parent_type'][0]['value'], $node->getEntityTypeId());
     $this->assertEqual($composite_after['parent_id'][0]['value'], $node->id());
     $this->assertEqual($composite_after['parent_field_name'][0]['value'], 'composite_reference');
     // Test that the composite entity is deleted when its parent is deleted.
     $node->delete();
     $this->assertNull(EntityTestCompositeRelationship::load($composite->id()));
 }
 public function testFormatterWithDeletedReference()
 {
     // Create the test composite entity.
     $text = 'Dummy text';
     $entity_test = EntityTestCompositeRelationship::create(['uuid' => $text, 'name' => $text]);
     $entity_test->save();
     $text = 'Clever text';
     // Set the name to a new text.
     /** @var \Drupal\entity_composite_relationship_test\Entity\EntityTestCompositeRelationship $entity_test */
     $entity_test->name = $text;
     $entity_test->setNeedsSave(TRUE);
     $node = Node::create(['title' => $this->randomMachineName(), 'type' => 'article', 'composite_reference' => $entity_test]);
     $node->save();
     // entity_reference_revisions_entity_view
     $result = $node->composite_reference->view(['type' => 'entity_reference_revisions_entity_view']);
     $this->setRawContent($this->render($result));
     $this->assertText('Clever text');
     // Remove the referenced entity.
     $entity_test->delete();
     $node = Node::load($node->id());
     $result = $node->composite_reference->view(['type' => 'entity_reference_revisions_entity_view']);
     $this->render($result);
     $this->assertNoText('Clever text');
 }
 /**
  * Test for NeedsSaveInterface implementation.
  *
  * Tests that the fields in the parent are properly updated.
  */
 public function testSaveNewEntity()
 {
     // Add the entity_reference_revisions field to article.
     $field_storage = FieldStorageConfig::create(array('field_name' => 'composite_reference', 'entity_type' => 'node', 'type' => 'entity_reference_revisions', 'settings' => array('target_type' => 'entity_test_composite')));
     $field_storage->save();
     $field = FieldConfig::create(array('field_storage' => $field_storage, 'bundle' => 'article'));
     $field->save();
     $text = 'Dummy text';
     // Create the test entity.
     $entity_test = EntityTestCompositeRelationship::create(array('uuid' => $text, 'name' => $text));
     // Create a node with a reference to the test entity and save.
     $node = Node::create(['title' => $this->randomMachineName(), 'type' => 'article', 'composite_reference' => $entity_test]);
     $validate = $node->validate();
     $this->assertEmpty($validate);
     $node->save();
     // Test that the fields on node are properly set.
     $node_after = Node::load($node->id());
     static::assertEquals($node_after->composite_reference[0]->target_id, $entity_test->id());
     static::assertEquals($node_after->composite_reference[0]->target_revision_id, $entity_test->getRevisionId());
     // Check that the entity is not new after save parent.
     $this->assertFalse($entity_test->isNew());
     // Create a new test entity.
     $text = 'Smart text';
     $second_entity_test = EntityTestCompositeRelationship::create(array('uuid' => $text, 'name' => $text));
     $second_entity_test->save();
     // Set the new test entity to the node field.
     $node_after->composite_reference = $second_entity_test;
     // Check the fields have been updated.
     static::assertEquals($node_after->composite_reference[0]->target_id, $second_entity_test->id());
     static::assertEquals($node_after->composite_reference[0]->target_revision_id, $second_entity_test->getRevisionId());
 }