summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Rothstein <drothstein@gmail.com>2014-07-24 16:38:03 -0400
committerDavid Rothstein <drothstein@gmail.com>2014-07-24 16:38:03 -0400
commitb90a53201d7c3ce0dc3a240d5377cf9d99365c53 (patch)
tree31f6c85c51ad1158e0be1259776fd1092971cd1e
parent9b6bd6d3191963686346d39540370edc0a7d3a86 (diff)
downloadbrdo-b90a53201d7c3ce0dc3a240d5377cf9d99365c53.tar.gz
brdo-b90a53201d7c3ce0dc3a240d5377cf9d99365c53.tar.bz2
Issue #2305017 by David_Rothstein, pwolanin | beech: Fixed Regression: Files or images attached to certain core and non-core entities are lost when the entity is edited and saved.
-rw-r--r--CHANGELOG.txt3
-rw-r--r--modules/file/file.module22
-rw-r--r--modules/file/tests/file.test122
3 files changed, 141 insertions, 6 deletions
diff --git a/CHANGELOG.txt b/CHANGELOG.txt
index c118da47c..1d85fd5f6 100644
--- a/CHANGELOG.txt
+++ b/CHANGELOG.txt
@@ -1,6 +1,9 @@
Drupal 7.30, xxxx-xx-xx (development version)
-----------------------
+- Fixed a regression introduced in Drupal 7.29 that caused files or images
+ attached to taxonomy terms to be deleted when the taxonomy term was edited
+ and resaved (and other related bugs with contributed and custom modules).
- Added a warning on the permissions page to recommend restricting access to
the "View site reports" permission to trusted administrators. See
DRUPAL-PSA-2014-002.
diff --git a/modules/file/file.module b/modules/file/file.module
index ed165368b..a4a7f03d9 100644
--- a/modules/file/file.module
+++ b/modules/file/file.module
@@ -478,6 +478,7 @@ function file_managed_file_process($element, &$form_state, $form) {
*/
function file_managed_file_value(&$element, $input = FALSE, $form_state = NULL) {
$fid = 0;
+ $force_default = FALSE;
// Find the current value of this field from the form state.
$form_state_fid = $form_state['values'];
@@ -510,16 +511,25 @@ function file_managed_file_value(&$element, $input = FALSE, $form_state = NULL)
$callback($element, $input, $form_state);
}
}
- // Load file and check access if the FID has changed, to confirm it
- // exists and that the current user has access to it.
- if (isset($input['fid']) && ($file = file_load($input['fid'])) && file_download_access($file->uri)) {
- $fid = $file->fid;
+ // If a FID was submitted, load the file (and check access if it's not a
+ // public file) to confirm it exists and that the current user has access
+ // to it.
+ if (isset($input['fid']) && ($file = file_load($input['fid']))) {
+ if (file_uri_scheme($file->uri) == 'public' || file_download_access($file->uri)) {
+ $fid = $file->fid;
+ }
+ // If the current user doesn't have access, don't let the file be
+ // changed.
+ else {
+ $force_default = TRUE;
+ }
}
}
}
- // If there is no input, set the default value.
- else {
+ // If there is no input or if the default value was requested above, use the
+ // default value.
+ if ($input === FALSE || $force_default) {
if ($element['#extended']) {
$default_fid = isset($element['#default_value']['fid']) ? $element['#default_value']['fid'] : 0;
$return = isset($element['#default_value']) ? $element['#default_value'] : array('fid' => 0);
diff --git a/modules/file/tests/file.test b/modules/file/tests/file.test
index 4d53d747f..0f6a578d9 100644
--- a/modules/file/tests/file.test
+++ b/modules/file/tests/file.test
@@ -221,6 +221,128 @@ class FileFieldTestCase extends DrupalWebTestCase {
}
/**
+ * Tests adding a file to a non-node entity.
+ */
+class FileTaxonomyTermTestCase extends DrupalWebTestCase {
+ protected $admin_user;
+
+ public static function getInfo() {
+ return array(
+ 'name' => 'Taxonomy term file test',
+ 'description' => 'Tests adding a file to a non-node entity.',
+ 'group' => 'File',
+ );
+ }
+
+ public function setUp() {
+ $modules[] = 'file';
+ $modules[] = 'taxonomy';
+ parent::setUp($modules);
+ $this->admin_user = $this->drupalCreateUser(array('access content', 'access administration pages', 'administer site configuration', 'administer taxonomy'));
+ $this->drupalLogin($this->admin_user);
+ }
+
+ /**
+ * Creates a file field and attaches it to the "Tags" taxonomy vocabulary.
+ *
+ * @param $name
+ * The field name of the file field to create.
+ * @param $uri_scheme
+ * The URI scheme to use for the file field (for example, "private" to
+ * create a field that stores private files or "public" to create a field
+ * that stores public files).
+ */
+ protected function createAttachFileField($name, $uri_scheme) {
+ $field = array(
+ 'field_name' => $name,
+ 'type' => 'file',
+ 'settings' => array(
+ 'uri_scheme' => $uri_scheme,
+ ),
+ 'cardinality' => 1,
+ );
+ field_create_field($field);
+ // Attach an instance of it.
+ $instance = array(
+ 'field_name' => $name,
+ 'label' => 'File',
+ 'entity_type' => 'taxonomy_term',
+ 'bundle' => 'tags',
+ 'required' => FALSE,
+ 'settings' => array(),
+ 'widget' => array(
+ 'type' => 'file_generic',
+ 'settings' => array(),
+ ),
+ );
+ field_create_instance($instance);
+ }
+
+ /**
+ * Tests that a public file can be attached to a taxonomy term.
+ *
+ * This is a regression test for https://www.drupal.org/node/2305017.
+ */
+ public function testTermFilePublic() {
+ $this->_testTermFile('public');
+ }
+
+ /**
+ * Tests that a private file can be attached to a taxonomy term.
+ *
+ * This is a regression test for https://www.drupal.org/node/2305017.
+ */
+ public function testTermFilePrivate() {
+ $this->_testTermFile('private');
+ }
+
+ /**
+ * Runs tests for attaching a file field to a taxonomy term.
+ *
+ * @param $uri_scheme
+ * The URI scheme to use for the file field, either "public" or "private".
+ */
+ protected function _testTermFile($uri_scheme) {
+ $field_name = strtolower($this->randomName());
+ $this->createAttachFileField($field_name, $uri_scheme);
+ // Get a file to upload.
+ $file = current($this->drupalGetTestFiles('text'));
+ // Add a filesize property to files as would be read by file_load().
+ $file->filesize = filesize($file->uri);
+ $langcode = LANGUAGE_NONE;
+ $edit = array(
+ "name" => $this->randomName(),
+ );
+ // Attach a file to the term.
+ $edit['files[' . $field_name . '_' . $langcode . '_0]'] = drupal_realpath($file->uri);
+ $this->drupalPost("admin/structure/taxonomy/tags/add", $edit, t('Save'));
+ // Find the term ID we just created.
+ $tid = db_query_range('SELECT tid FROM {taxonomy_term_data} ORDER BY tid DESC', 0, 1)->fetchField();
+ $terms = entity_load('taxonomy_term', array($tid));
+ $term = $terms[$tid];
+ $fid = $term->{$field_name}[LANGUAGE_NONE][0]['fid'];
+ // Check that the uploaded file is present on the edit form.
+ $this->drupalGet("taxonomy/term/$tid/edit");
+ $file_input_name = $field_name . '[' . LANGUAGE_NONE . '][0][fid]';
+ $this->assertFieldByXpath('//input[@type="hidden" and @name="' . $file_input_name . '"]', $fid, 'File is attached on edit form.');
+ // Edit the term and change name without changing the file.
+ $edit = array(
+ "name" => $this->randomName(),
+ );
+ $this->drupalPost("taxonomy/term/$tid/edit", $edit, t('Save'));
+ // Check that the uploaded file is still present on the edit form.
+ $this->drupalGet("taxonomy/term/$tid/edit");
+ $file_input_name = $field_name . '[' . LANGUAGE_NONE . '][0][fid]';
+ $this->assertFieldByXpath('//input[@type="hidden" and @name="' . $file_input_name . '"]', $fid, 'File is attached on edit form.');
+ // Load term while resetting the cache.
+ $terms = entity_load('taxonomy_term', array($tid), array(), TRUE);
+ $term = $terms[$tid];
+ $this->assertTrue(!empty($term->{$field_name}[LANGUAGE_NONE]), 'Term has attached files.');
+ $this->assertEqual($term->{$field_name}[LANGUAGE_NONE][0]['fid'], $fid, 'Same File ID is attached to the term.');
+ }
+}
+
+/**
* Tests the 'managed_file' element type.
*
* @todo Create a FileTestCase base class and move FileFieldTestCase methods