/** * Test for NeedsSaveInterface implementation. * * Tests that the referenced entity is saved when needsSave() is TRUE. */ public function testNeedsSave() { // 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 composite entity. $entity_test = EntityTestCompositeRelationship::create(array('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); // Create a node with a reference to the test entity and save. $node = Node::create(['title' => $this->randomMachineName(), 'type' => 'article', 'composite_reference' => $entity_test]); // Check the name is properly set. $values = $node->composite_reference->getValue(); $this->assertTrue(isset($values[0]['entity'])); static::assertEquals($values[0]['entity']->name->value, $text); $node->composite_reference->setValue($values); static::assertEquals($node->composite_reference->entity->name->value, $text); $node->save(); // Check that the name has been updated when the parent has been saved. /** @var \Drupal\entity_composite_relationship_test\Entity\EntityTestCompositeRelationship $entity_test_after */ $entity_test_after = EntityTestCompositeRelationship::load($entity_test->id()); static::assertEquals($entity_test_after->name->value, $text); $new_text = 'Dummy text again'; // Set the name again. $entity_test->name = $new_text; $entity_test->setNeedsSave(FALSE); // Load the Node and check the composite reference field is not set. $node = Node::load($node->id()); $values = $node->composite_reference->getValue(); $this->assertFalse(isset($values[0]['entity'])); $node->composite_reference = $entity_test; $node->save(); // Check the name is not updated. $entity_test_after = EntityTestCompositeRelationship::load($entity_test->id()); static::assertEquals($entity_test_after->name->value, $text); // Test if after delete the referenced entity there are no problems setting // the referencing values to the parent. $entity_test->delete(); $node = Node::load($node->id()); $node->save(); }
/** * 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())); }