MDL-33032 Fixed issues for file integration

1. Added tracker issue number in comment
2. Fixed stored_file::delete_reference()
3. repository::send_file() will throw exception if not implemented by subclass
4. Fixed renaming unit test, added one unit test for deleting original file
5. Fixed copyright statement for googledoc and picasa repository plugin
6. Implemented stored_file::set_filesize()
This commit is contained in:
Dongsheng Cai 2012-05-17 13:55:03 +08:00 committed by Marina Glancy
parent 4712676dfa
commit 61506a0af9
10 changed files with 145 additions and 13 deletions

View File

@ -324,6 +324,7 @@ $string['invalidsection'] = 'Course module record contains invalid section';
$string['invalidsesskey'] = 'Incorrect sesskey submitted, form not accepted!';
$string['invalidshortname'] = 'That\'s an invalid short course name';
$string['invalidstatedetected'] = 'Something has gone wrong: {$a}. This should never normally happen.';
$string['invalidsourcefield'] = 'Draft file\'s source field is invalid';
$string['invalidurl'] = 'Invalid URL';
$string['invaliduser'] = 'Invalid user';
$string['invaliduserid'] = 'Invalid user id';

View File

@ -375,7 +375,7 @@ function file_prepare_draft_area(&$draftitemid, $contextid, $component, $fileare
continue;
}
$draftfile = $fs->create_file_from_storedfile($file_record, $file);
// XXX: This is a hack for file manager
// XXX: This is a hack for file manager (MDL-28666)
// File manager needs to know the original file information before copying
// to draft area, so we append these information in mdl_files.source field
// {@link file_storage::search_references()}
@ -679,9 +679,13 @@ function file_get_submitted_draft_itemid($elname) {
*/
function file_restore_source_field_from_draft_file($storedfile) {
$source = unserialize($storedfile->get_source());
if (!empty($source) && is_object($source)) {
$restoredsource = $source->source;
$storedfile->set_source($restoredsource);
if (!empty($source)) {
if (is_object($source)) {
$restoredsource = $source->source;
$storedfile->set_source($restoredsource);
} else {
throw new moodle_exception('invalidsourcefield', 'error');
}
}
return $storedfile;
}
@ -813,6 +817,11 @@ function file_save_draft_area_files($draftitemid, $contextid, $component, $filea
$oldfile->set_sortorder($newfile->get_sortorder());
}
// Update file size
if ($oldfile->get_filesize() != $newfile->get_filesize()) {
$oldfile->set_filesize($newfile->get_filesize());
}
// unchanged file or directory - we keep it as is
unset($newhashes[$oldhash]);
if (!$oldfile->is_directory()) {

View File

@ -191,15 +191,27 @@ class stored_file {
*/
public function delete_reference() {
global $DB;
// Remove repository info.
$this->repository = null;
// Remove reference info from DB.
$DB->delete_records('files_reference', array('id'=>$this->file_record->referencefileid));
// Must refresh $this->file_record form DB
$filerecord = $DB->get_record('files', array('id'=>$this->get_id()));
// Update DB
$filerecord->referencelastsync = null;
$filerecord->referencelifetime = null;
$filerecord->referencefileid = null;
$this->update($filerecord);
// unset object variable
unset($this->file_record->repositoryid);
unset($this->file_record->reference);
unset($this->file_record->referencelastsync);
unset($this->file_record->referencelifetime);
// Remove reference info from DB.
$DB->delete_records('files_reference', array('id'=>$this->file_record->referencefileid));
unset($this->file_record->referencefileid);
}
/**
@ -560,6 +572,17 @@ class stored_file {
return $this->file_record->filesize;
}
/**
* Returns the size of file in bytes.
*
* @param int $filesize bytes
*/
public function set_filesize($filesize) {
$filerecord = new stdClass;
$filerecord->filesize = $filesize;
$this->update($filerecord);
}
/**
* Returns mime type of file.
*

View File

@ -109,15 +109,17 @@ class filestoragelib_testcase extends advanced_testcase {
$contenthash = $originalfile->get_contenthash();
$newpath = '/test/';
$newname = 'newtest.txt';
// try break it
$this->setExpectedException('file_exception');
// this shall throw exception
$originalfile->rename($filepath, $filename);
// this should work
$originalfile->rename($newpath, $newname);
$file = $fs->get_file($syscontext->id, $component, $filearea, $itemid, $newpath, $newname);
$this->assertInstanceOf('stored_file', $file);
$this->assertEquals($contenthash, $file->get_contenthash());
// try break it
$this->setExpectedException('file_exception');
// this shall throw exception
$originalfile->rename($newpath, $newname);
}
/**

View File

@ -195,4 +195,78 @@ class filelib_testcase extends advanced_testcase {
$this->assertEquals($license, $file->get_license());
$this->assertEquals($newsourcefield, $file->get_source());
}
/**
* Testing deleting original files
*
* @copyright 2012 Dongsheng Cai {@link http://dongsheng.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
public function test_delete_original_file_from_draft() {
global $USER, $DB;
$this->resetAfterTest(true);
$generator = $this->getDataGenerator();
$user = $generator->create_user();
$usercontext = context_user::instance($user->id);
$USER = $DB->get_record('user', array('id'=>$user->id));
$repositorypluginname = 'user';
$args = array();
$args['type'] = $repositorypluginname;
$repos = repository::get_instances($args);
$userrepository = reset($repos);
$this->assertInstanceOf('repository', $userrepository);
$fs = get_file_storage();
$syscontext = context_system::instance();
$filecontent = 'User file content';
// create a user private file
$userfilerecord = new stdClass;
$userfilerecord->contextid = $usercontext->id;
$userfilerecord->component = 'user';
$userfilerecord->filearea = 'private';
$userfilerecord->itemid = 0;
$userfilerecord->filepath = '/';
$userfilerecord->filename = 'userfile.txt';
$userfilerecord->source = 'test';
$userfile = $fs->create_file_from_string($userfilerecord, $filecontent);
$userfileref = $fs->pack_reference($userfilerecord);
$contenthash = $userfile->get_contenthash();
$filerecord = array(
'contextid' => $syscontext->id,
'component' => 'core',
'filearea' => 'phpunit',
'itemid' => 0,
'filepath' => '/',
'filename' => 'test.txt',
);
// create a file reference
$fileref = $fs->create_file_from_reference($filerecord, $userrepository->id, $userfileref);
$this->assertInstanceOf('stored_file', $fileref);
$this->assertEquals($userrepository->id, $fileref->repository->id);
$this->assertEquals($userfile->get_contenthash(), $fileref->get_contenthash());
$this->assertEquals($userfile->get_filesize(), $fileref->get_filesize());
$this->assertRegExp('#' . $userfile->get_filename(). '$#', $fileref->get_reference_details());
$draftitemid = 0;
file_prepare_draft_area($draftitemid, $usercontext->id, 'user', 'private', 0);
$draftfiles = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid);
$this->assertEquals(2, count($draftfiles));
$draftfile = $fs->get_file($usercontext->id, 'user', 'draft', $draftitemid, $userfilerecord->filepath, $userfilerecord->filename);
$draftfile->delete();
// Save changed file
file_save_draft_area_files($draftitemid, $usercontext->id, 'user', 'private', 0);
// The file reference should be a regular moodle file now
$fileref = $fs->get_file($syscontext->id, 'core', 'phpunit', 0, '/', 'test.txt');
$this->assertEquals(false, $fileref->is_external_file());
$this->assertEquals($contenthash, $fileref->get_contenthash());
$this->assertEquals($filecontent, $fileref->get_content());
}
}

View File

@ -279,6 +279,17 @@ class repository_coursefiles extends repository {
return $coursename . ': ' . $params['filepath'] . $params['filename'];
}
/**
* Return reference file life time
*
* @param string $ref
* @return int
*/
public function get_reference_file_lifetime($ref) {
// this should be realtime
return 0;
}
/**
* Repository method to serve file
*

View File

@ -19,7 +19,7 @@
*
* @since 2.0
* @package repository_googledocs
* @copyright 2010 Dongsheng Cai {@link http://dongsheng.org}
* @copyright 2009 Dan Poltawski <talktodan@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once($CFG->dirroot . '/repository/lib.php');

View File

@ -1066,6 +1066,7 @@ abstract class repository {
* @param array $options additional options affecting the file serving
*/
public function send_file($storedfile, $lifetime=86400 , $filter=0, $forcedownload=false, array $options = null) {
throw new coding_exception("Repository plugin must implement send_file() method.");
}
/**

View File

@ -19,7 +19,7 @@
*
* @since 2.0
* @package repository_picasa
* @copyright 2010 Dongsheng Cai {@link http://dongsheng.org}
* @copyright 2009 Dan Poltawski <talktodan@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once($CFG->dirroot . '/repository/lib.php');

View File

@ -225,6 +225,17 @@ class repository_user extends repository {
return $this->get_name() . ': ' . $params['filepath'] . $params['filename'];
}
/**
* Return reference file life time
*
* @param string $ref
* @return int
*/
public function get_reference_file_lifetime($ref) {
// this should be realtime
return 0;
}
/**
* Repository method to serve file
*