diff --git a/entityreference.module b/entityreference.module index 6d8c95d..328cd26 100644 --- a/entityreference.module +++ b/entityreference.module @@ -246,22 +246,9 @@ function entityreference_field_presave($entity_type, $entity, $field, $instance, // Reference the right revision(s) if revision locking is enabled. if (!empty($field['settings']['handler_settings']['lock_revision'])) { $target_type = $field['settings']['target_type']; - $original_items = array(); + // If the entity is being updated, retrieve the original field items. - if (isset($entity->original)) { - $original_items = field_get_items($entity_type, $entity->original, $field['field_name']); - } - // If $entity->original was not populated, retrieve it from the database. - // @todo Check if this is really necessary. - else { - list($id) = entity_extract_ids($entity_type, $entity); - if ($id) { - $original = entity_load_unchanged($entity_type, $id); - if ($original) { - $original_items = field_get_items($entity_type, $original, $field['field_name']); - } - } - } + $original_items = isset($entity->original) ? field_get_items($entity_type, $entity->original, $field['field_name']) : array(); foreach ($items as $key => $item) { // Load the referenced entity revision if it was explicitly set. diff --git a/tests/entityreference.revisions.test b/tests/entityreference.revisions.test index f470114..4a4e2a6 100644 --- a/tests/entityreference.revisions.test +++ b/tests/entityreference.revisions.test @@ -90,31 +90,41 @@ class EntityReferenceRevisionsTestCase extends DrupalWebTestCase { * Test referencing a revision of an entity. */ public function testReferencingRevisions() { - // Get the original revision that was referenced. - $vid = $this->getReferencedRevisionId(); + // Check that the referencing of specific revisions is disabled by default. + $this->assertFalse($this->isReferencingRevision(), 'Referencing of a specific revision is disabled by default.'); - // By default the referencing of specific revisions is disabled and the - // latest revision should always be referenced. - $this->createNewRevision(); - $this->assertNotEqual($vid, $vid = $this->getReferencedRevisionId(), 'When revision referencing is disabled, the latest revision is referenced.'); + // Check that it is still disabled after updating the referencing node. + node_save($this->referencing_node); + $this->assertFalse($this->isReferencingRevision(), 'Referencing of a specific revision is still disabled after the referencing node is updated.'); - // Enable revision referencing. + // Enable revision referencing and update the referencing node so it picks + // up the change. $this->instance['settings']['handler_settings']['reference_revisions'] = TRUE; field_update_instance($this->instance); + node_save($this->referencing_node); - // Check that the referenced revision stays the same when a new revision is - // made. - $this->createNewRevision(); - $this->assertEqual($vid, $this->getReferencedRevisionId(), 'When revision referencing is enabled and a new revision is created, the original revision is still referenced.'); + // Check that the field is now referencing revisions. Retrieve the revision + // ID for comparison. + $this->assertTrue($this->isReferencingRevision(), 'The field is referencing revisions after this has been enabled.'); + $vid = $this->getReferencedRevisionId(); // Update the referencing node. The revision is not locked, so it should // change to the current revision. - node_save($this->referencing_node); $this->assertNotEqual($vid, $vid = $this->getReferencedRevisionId(), 'When revision referencing is enabled and the revision is not locked, the current revision is referenced when the referencing entity is updated.'); - // Enable revision locking. + // Enable revision locking and update the referencing node so it picks up + // the change. $this->instance['settings']['handler_settings']['lock_revision'] = TRUE; field_update_instance($this->instance); + node_save($this->referencing_node); + + $this->createNewRevision(); + $this->assertEqual($vid, $this->getReferencedRevisionId(), 'When revision locking is enabled the original revision is still referenced.'); + + // Update the referencing node. The revision is now locked, so it should + // still reference the original revision. + node_save($this->referencing_node); + } /** @@ -132,7 +142,21 @@ class EntityReferenceRevisionsTestCase extends DrupalWebTestCase { * The referenced revision. */ protected function getReferencedRevisionId() { - $wrapper = entity_metadata_wrapper('node', $this->referencing_node); - return $wrapper->field_entityreference->vid->value(); + $items = field_get_items('node', $this->referencing_node, 'field_entityreference'); + if (empty($items[0]['revision_id'])) { + throw new Exception('The field is not referencing a revision.'); + } + return $items[0]['revision_id']; + } + + /** + * Returns whether or not the field is referencing a revision. + * + * @return bool + * TRUE if the field is referencing a revision, FALSE otherwise. + */ + protected function isReferencingRevision() { + $items = field_get_items('node', $this->referencing_node, 'field_entityreference'); + return !empty($items[0]['revision_id']); } }