diff options
author | David Rothstein <drothstein@gmail.com> | 2014-11-03 12:49:23 -0500 |
---|---|---|
committer | David Rothstein <drothstein@gmail.com> | 2014-11-03 12:49:23 -0500 |
commit | e7b2772c2583c36b506627bf86aaf3a51b34f9a0 (patch) | |
tree | f2bf470637a58bfe2e5a775813526e268df04630 /modules | |
parent | 8cf521c61c62a30d3d2636502f70e6ea26d3200a (diff) | |
download | brdo-e7b2772c2583c36b506627bf86aaf3a51b34f9a0.tar.gz brdo-e7b2772c2583c36b506627bf86aaf3a51b34f9a0.tar.bz2 |
Issue #2278583 by nlisgo, Berdir, joshi.rohit100 | Fabianx: Fixed field_has_data looks at current data instead of revisioning data: this can lead to data loss.
Diffstat (limited to 'modules')
-rw-r--r-- | modules/field/field.module | 9 | ||||
-rw-r--r-- | modules/field/tests/field.test | 60 |
2 files changed, 66 insertions, 3 deletions
diff --git a/modules/field/field.module b/modules/field/field.module index eef78a350..a593e5111 100644 --- a/modules/field/field.module +++ b/modules/field/field.module @@ -936,14 +936,17 @@ function field_get_items($entity_type, $entity, $field_name, $langcode = NULL) { */ function field_has_data($field) { $query = new EntityFieldQuery(); - return (bool) $query - ->fieldCondition($field) + $query = $query->fieldCondition($field) ->range(0, 1) ->count() // Neutralize the 'entity_field_access' query tag added by // field_sql_storage_field_storage_query(). The result cannot depend on the // access grants of the current user. - ->addTag('DANGEROUS_ACCESS_CHECK_OPT_OUT') + ->addTag('DANGEROUS_ACCESS_CHECK_OPT_OUT'); + + return (bool) $query + ->execute() || (bool) $query + ->age(FIELD_LOAD_REVISION) ->execute(); } diff --git a/modules/field/tests/field.test b/modules/field/tests/field.test index 1e5931548..b279d6a8f 100644 --- a/modules/field/tests/field.test +++ b/modules/field/tests/field.test @@ -485,6 +485,66 @@ class FieldAttachStorageTestCase extends FieldAttachTestCase { } /** + * Test field_has_data(). + */ + function testFieldHasData() { + $entity_type = 'test_entity'; + $langcode = LANGUAGE_NONE; + + $field_name = 'field_1'; + $field = array('field_name' => $field_name, 'type' => 'test_field'); + $field = field_create_field($field); + + $this->assertFalse(field_has_data($field), "No data should be detected."); + + $instance = array( + 'field_name' => $field_name, + 'entity_type' => 'test_entity', + 'bundle' => 'test_bundle' + ); + $instance = field_create_instance($instance); + $table = _field_sql_storage_tablename($field); + $revision_table = _field_sql_storage_revision_tablename($field); + + $columns = array('entity_type', 'entity_id', 'revision_id', 'delta', 'language', $field_name . '_value'); + + $eid = 0; + + // Insert values into the field revision table. + $query = db_insert($revision_table)->fields($columns); + $query->values(array($entity_type, $eid, 0, 0, $langcode, 1)); + $query->execute(); + + $this->assertTrue(field_has_data($field), "Revision data only should be detected."); + + $field_name = 'field_2'; + $field = array('field_name' => $field_name, 'type' => 'test_field'); + $field = field_create_field($field); + + $this->assertFalse(field_has_data($field), "No data should be detected."); + + $instance = array( + 'field_name' => $field_name, + 'entity_type' => 'test_entity', + 'bundle' => 'test_bundle' + ); + $instance = field_create_instance($instance); + $table = _field_sql_storage_tablename($field); + $revision_table = _field_sql_storage_revision_tablename($field); + + $columns = array('entity_type', 'entity_id', 'revision_id', 'delta', 'language', $field_name . '_value'); + + $eid = 1; + + // Insert values into the field table. + $query = db_insert($table)->fields($columns); + $query->values(array($entity_type, $eid, 0, 0, $langcode, 1)); + $query->execute(); + + $this->assertTrue(field_has_data($field), "Values only in field table should be detected."); + } + + /** * Test field_attach_delete(). */ function testFieldAttachDelete() { |