- Patch #331013 by drewish: remove file_set_status in favor of file_save().
parent
0290031d45
commit
4447e45ac4
|
@ -780,8 +780,8 @@ function file_space_used($uid = NULL, $status = FILE_STATUS_PERMANENT) {
|
|||
* Saves a file upload to a new location.
|
||||
*
|
||||
* The file will be added to the files table as a temporary file. Temporary
|
||||
* files are periodically cleaned. To make the file permanent file call
|
||||
* file_set_status() to change its status.
|
||||
* files are periodically cleaned. To make the file a permanent file call
|
||||
* assign the status and use file_save() to save it.
|
||||
*
|
||||
* @param $source
|
||||
* A string specifying the name of the upload field to save.
|
||||
|
@ -1175,40 +1175,6 @@ function file_unmanaged_save_data($data, $destination = NULL, $replace = FILE_EX
|
|||
return file_unmanaged_move($temp_name, $destination, $replace);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the status of a file.
|
||||
*
|
||||
* @param $file
|
||||
* A Drupal file object.
|
||||
* @param $status
|
||||
* A status value to set the file to.
|
||||
* - FILE_STATUS_TEMPORARY - A temporary file that Drupal's garbage
|
||||
* collection will remove.
|
||||
* - FILE_STATUS_PERMANENT - A permanent file that Drupal's garbage
|
||||
* collection will not remove.
|
||||
* @return
|
||||
* File object if the change is successful, or FALSE in the event of an
|
||||
* error.
|
||||
*
|
||||
* @see hook_file_status()
|
||||
*/
|
||||
function file_set_status($file, $status = FILE_STATUS_PERMANENT) {
|
||||
$file = (object)$file;
|
||||
|
||||
$num_updated = db_update('files')
|
||||
->fields(array('status' => $status))
|
||||
->condition('fid', $file->fid)
|
||||
->execute();
|
||||
|
||||
if ($num_updated) {
|
||||
$file->status = $status;
|
||||
// Notify other modules that the file's status has changed.
|
||||
module_invoke_all('file_status', $file);
|
||||
return $file;
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transfer file using HTTP to client. Pipes a file through Drupal to the
|
||||
* client.
|
||||
|
|
|
@ -974,18 +974,22 @@ class FileSaveTest extends FileHookTestCase {
|
|||
$this->assertFileHookCalled('insert');
|
||||
$this->assertNotNull($saved_file, t("Saving the file should give us back a file object."), 'File');
|
||||
$this->assertTrue($saved_file->fid > 0, t("A new file ID is set when saving a new file to the database."), 'File');
|
||||
$this->assertEqual(db_result(db_query('SELECT COUNT(*) FROM {files} f WHERE f.fid = %d', array($saved_file->fid))), 1, t("Record exists in the database."));
|
||||
$loaded_file = db_query('SELECT * FROM {files} f WHERE f.fid = :fid', array(':fid' => $saved_file->fid))->fetch(PDO::FETCH_OBJ);
|
||||
$this->assertNotNull($loaded_file, t("Record exists in the database."));
|
||||
$this->assertEqual($loaded_file->status, $file->status, t("Status was saved correctly."));
|
||||
$this->assertEqual($saved_file->filesize, filesize($file->filepath), t("File size was set correctly."), 'File');
|
||||
$this->assertTrue($saved_file->timestamp > 1, t("File size was set correctly."), 'File');
|
||||
|
||||
// Resave the file, updating the existing record.
|
||||
file_test_reset();
|
||||
$saved_file->status = 7;
|
||||
$resaved_file = file_save($saved_file);
|
||||
$this->assertFileHookCalled('update');
|
||||
$this->assertEqual($resaved_file->fid, $saved_file->fid, t("The file ID of an existing file is not changed when updating the database."), 'File');
|
||||
$this->assertTrue($resaved_file->timestamp >= $saved_file->timestamp, t("Timestamp didn't go backwards."), 'File');
|
||||
$count = db_result(db_query('SELECT COUNT(*) FROM {files} f WHERE f.fid = %d', array($saved_file->fid)));
|
||||
$this->assertEqual($count, 1, t("Record still exists in the database."), 'File');
|
||||
$loaded_file = db_query('SELECT * FROM {files} f WHERE f.fid = :fid', array(':fid' => $saved_file->fid))->fetch(PDO::FETCH_OBJ);
|
||||
$this->assertNotNull($loaded_file, t("Record still exists in the database."), 'File');
|
||||
$this->assertEqual($loaded_file->status, $saved_file->status, t("Status was saved correctly."));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1032,66 +1036,6 @@ class FileValidateTest extends FileHookTestCase {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the file_set_status() function.
|
||||
*/
|
||||
class FileSetStatusTest extends FileHookTestCase {
|
||||
/**
|
||||
* Implementation of getInfo().
|
||||
*/
|
||||
function getInfo() {
|
||||
return array(
|
||||
'name' => t('File set status'),
|
||||
'description' => t('Tests the file set status functions.'),
|
||||
'group' => t('File'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the file_set_status() function.
|
||||
*/
|
||||
function testFileSetStatus() {
|
||||
// Create a new file object.
|
||||
$file = array(
|
||||
'uid' => 1,
|
||||
'filename' => 'druplicon.png',
|
||||
'filepath' => 'misc/druplicon.png',
|
||||
'filemime' => 'image/png',
|
||||
'timestamp' => 1,
|
||||
'status' => FILE_STATUS_TEMPORARY,
|
||||
);
|
||||
$file = file_save($file);
|
||||
// Just a couple of sanity checks before we start the real testing.
|
||||
$this->assertTrue($file->fid, t("Make sure the file saved correctly."));
|
||||
$this->assertEqual($file->status, FILE_STATUS_TEMPORARY, t("Status was set during save."));
|
||||
|
||||
// Change the status and make sure everything works
|
||||
file_test_reset();
|
||||
$returned = file_set_status($file);
|
||||
$this->assertEqual(count(file_test_get_calls('status')), 1, t('hook_file_status was called.'));
|
||||
$this->assertNotIdentical($returned, FALSE, t("file_set_status() worked and returned a non-false value."));
|
||||
$this->assertEqual($returned->fid, $file->fid, t("Returned the correct file."));
|
||||
$this->assertEqual($returned->status, FILE_STATUS_PERMANENT, t("File's status was changed."));
|
||||
|
||||
// Try it resetting it to the same value.
|
||||
file_test_reset();
|
||||
$returned = file_set_status($file, FILE_STATUS_PERMANENT);
|
||||
$this->assertEqual(count(file_test_get_calls('status')), 0, t('hook_file_status was not called.'));
|
||||
$this->assertIdentical($returned, FALSE, t("file_set_status() failed since there was no change."));
|
||||
$test_file = file_load($file->fid);
|
||||
$this->assertEqual($test_file->fid, $file->fid , t("Loaded the correct file."));
|
||||
$this->assertEqual($test_file->status, FILE_STATUS_PERMANENT, t("File's status is correct."));
|
||||
|
||||
// Now switch it.
|
||||
file_test_reset();
|
||||
$returned = file_set_status($file, FILE_STATUS_TEMPORARY);
|
||||
$this->assertEqual(count(file_test_get_calls('status')), 1, t('hook_file_status was called.'));
|
||||
$this->assertNotIdentical($returned, FALSE, t("file_set_status() worked and returned a non-false value."));
|
||||
$this->assertEqual($returned->fid, $file->fid , t("Returned the correct file."));
|
||||
$this->assertEqual($returned->status, FILE_STATUS_TEMPORARY, t("File's status is correct."));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the file_save_data() function.
|
||||
*/
|
||||
|
|
|
@ -67,7 +67,6 @@ function file_test_reset() {
|
|||
'validate' => array(),
|
||||
'download' => array(),
|
||||
'references' => array(),
|
||||
'status' => array(),
|
||||
'insert' => array(),
|
||||
'update' => array(),
|
||||
'copy' => array(),
|
||||
|
@ -91,7 +90,7 @@ function file_test_reset() {
|
|||
*
|
||||
* @param $op
|
||||
* One of the hook_file_* operations: 'load', 'validate', 'download',
|
||||
* 'references', 'status', 'insert', 'update', 'copy', 'move', 'delete'.
|
||||
* 'references', 'insert', 'update', 'copy', 'move', 'delete'.
|
||||
* @returns
|
||||
* Array of the parameters passed to each call.
|
||||
* @see _file_test_log_call() and file_test_reset()
|
||||
|
@ -106,7 +105,7 @@ function file_test_get_calls($op) {
|
|||
*
|
||||
* @param $op
|
||||
* One of the hook_file_* operations: 'load', 'validate', 'download',
|
||||
* 'references', 'status', 'insert', 'update', 'copy', 'move', 'delete'.
|
||||
* 'references', 'insert', 'update', 'copy', 'move', 'delete'.
|
||||
* @param $args
|
||||
* Values passed to hook.
|
||||
* @see file_test_get_calls() and file_test_reset()
|
||||
|
@ -164,13 +163,6 @@ function file_test_file_validate($file) {
|
|||
return _file_test_get_return('validate');
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of hook_file_status().
|
||||
*/
|
||||
function file_test_file_status($file) {
|
||||
_file_test_log_call('status', array($file));
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of hook_file_download().
|
||||
*/
|
||||
|
|
|
@ -479,7 +479,8 @@ function upload_save(&$node) {
|
|||
else {
|
||||
db_query("UPDATE {upload} SET list = %d, description = '%s', weight = %d WHERE fid = %d AND vid = %d", $file->list, $file->description, $file->weight, $file->fid, $node->vid);
|
||||
}
|
||||
file_set_status($file, FILE_STATUS_PERMANENT);
|
||||
$file->status &= FILE_STATUS_PERMANENT;
|
||||
$file = file_save($file);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue