Commit f2faa90d authored by drumm's avatar drumm Committed by Joel Muzzerall
Browse files

Issue #2385985 by drumm, tim.plunkett: Skip saving host when host is being deleted.

parent 6f8ad926
......@@ -433,7 +433,11 @@ class FieldCollectionItemEntity extends Entity {
$delta = $this->delta();
if ($this->item_id && isset($delta)) {
unset($this->hostEntity->{$this->field_name}[$this->langcode][$delta]);
entity_save($this->hostEntityType, $this->hostEntity);
// Do not save when the host entity is being deleted. See
// field_collection_field_delete().
if (empty($this->hostEntity->field_collection_deleting)) {
entity_save($this->hostEntityType, $this->hostEntity);
}
}
}
......
......@@ -539,6 +539,9 @@ function field_collection_field_delete($entity_type, $entity, $field, $instance,
unset($ids[$id_key]);
}
}
// Set a flag to remember that the host entity is being deleted. See
// FieldCollectionItemEntity::deleteHostEntityReference().
$entity->field_collection_deleting = TRUE;
entity_delete_multiple('field_collection_item', $ids);
}
......
......@@ -19,7 +19,7 @@ class FieldCollectionBasicTestCase extends DrupalWebTestCase {
}
function setUp() {
parent::setUp('field_collection');
parent::setUp('field_collection', 'entity_crud_hook_test');
// Create a field_collection field to use for the tests.
$this->field_name = 'field_test_collection';
......@@ -44,6 +44,65 @@ class FieldCollectionBasicTestCase extends DrupalWebTestCase {
$this->instance = field_create_instance($this->instance);
}
/**
* Pass if the message $text was set by one of the CRUD hooks in
* entity_crud_hook_test.module, i.e., if the $text is an element of
* $_SESSION['entity_crud_hook_test'].
*
* @see EntityCrudHookTestCase::assertHookMessage()
* @see FieldCollectionBasicTestCase::assertNoHookMessage()
* @see FieldCollectionBasicTestCase::clearHookMessages()
*
* @param $text
* Plain text to look for.
* @param $message
* Message to display.
* @param $group
* The group this message belongs to, defaults to 'Other'.
* @return
* TRUE on pass, FALSE on fail.
*/
protected function assertHookMessage($text, $message = NULL, $group = 'Other') {
if (!isset($message)) {
$message = $text;
}
return $this->assertTrue(array_search($text, $_SESSION['entity_crud_hook_test']) !== FALSE, $message, $group);
}
/**
* Fail if the message $text was set by one of the CRUD hooks in
* entity_crud_hook_test.module, i.e., if the $text is an element of
* $_SESSION['entity_crud_hook_test'].
*
* @see FieldCollectionBasicTestCase::assertHookMessage()
* @see FieldCollectionBasicTestCase::clearHookMessages()
*
* @param $text
* Plain text to look for.
* @param $message
* Message to display.
* @param $group
* The group this message belongs to, defaults to 'Other'.
* @return
* TRUE on pass, FALSE on fail.
*/
protected function assertNoHookMessage($text, $message = NULL, $group = 'Other') {
if (!isset($message)) {
$message = $text;
}
return $this->assertFalse(array_search($text, $_SESSION['entity_crud_hook_test']) !== FALSE, $message, $group);
}
/**
* Clear hook messages recorded by entity_crud_hook_test.
*
* @see FieldCollectionBasicTestCase::assertHookMessage()
* @see FieldCollectionBasicTestCase::assertNoHookMessage()
*/
protected function clearHookMessages() {
$_SESSION['entity_crud_hook_test'] = array();
}
/**
* Helper for creating a new node with a field collection item.
*/
......@@ -79,12 +138,16 @@ class FieldCollectionBasicTestCase extends DrupalWebTestCase {
$this->assertEqual($entity2->revision_id, $node->{$this->field_name}[LANGUAGE_NONE][1]['revision_id'], 'New field_collection has been properly referenced (revision)');
// Make sure deleting the field_collection removes the reference.
$this->clearHookMessages();
$entity2->delete();
$this->assertHookMessage('entity_crud_hook_test_entity_presave called for type node');
$node = node_load($node->nid, NULL, TRUE);
$this->assertTrue(!isset($node->{$this->field_name}[LANGUAGE_NONE][1]), 'Reference correctly deleted.');
// Make sure field_collections are removed during deletion of the host.
$this->clearHookMessages();
node_delete($node->nid);
$this->assertNoHookMessage('entity_crud_hook_test_entity_presave called for type node');
$this->assertTrue(entity_load('field_collection_item', FALSE) === array(), 'Field collections are deleted when the host is deleted.');
// Try deleting nodes with collections without any values.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment