Skip to content

Commit

Permalink
MDL-28666 Files API and db changes
Browse files Browse the repository at this point in the history
1. Fix db regression
2. Unit tests for is_external_file() and rename exception
3. Searching reference method exclude draft files
  • Loading branch information
Dongsheng Cai authored and marinaglancy committed May 21, 2012
1 parent 6dd299b commit 7051415
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 22 deletions.
20 changes: 11 additions & 9 deletions lib/db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -594,26 +594,29 @@ function xmldb_main_upgrade($oldversion) {

if ($oldversion < 2012052100.00) {

// Define field referencefileid to be added to files
// Define field referencefileid to be added to files.
$table = new xmldb_table('files');

// Define field referencefileid to be added to files
// Define field referencefileid to be added to files.
$field = new xmldb_field('referencefileid', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'sortorder');
// Conditionally launch add field referencefileid

// Conditionally launch add field referencefileid.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}

// Define field referencelastsync to be added to files
// Define field referencelastsync to be added to files.
$field = new xmldb_field('referencelastsync', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'referencefileid');
// Conditionally launch add field referencelastsync

// Conditionally launch add field referencelastsync.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}

// Define field referencelifetime to be added to files
$field = new xmldb_field('referencelastsync', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'referencefileid');
// Conditionally launch add field referencelifetime
// Define field referencelifetime to be added to files.
$field = new xmldb_field('referencelifetime', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'referencelastsync');

// Conditionally launch add field referencelifetime.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
Expand All @@ -627,7 +630,6 @@ function xmldb_main_upgrade($oldversion) {

// Adding fields to table files_reference.
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('fileid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
$table->add_field('repositoryid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
$table->add_field('lastsync', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
$table->add_field('lifetime', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
Expand Down
16 changes: 8 additions & 8 deletions lib/filestorage/file_storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -1665,9 +1665,9 @@ public function search_references($str) {
FROM {files} f
LEFT JOIN {files_reference} r
ON f.referencefileid = r.id
WHERE r.reference = ?";
WHERE r.reference = ? AND (f.component <> ? OR f.filearea <> ?)";

$rs = $DB->get_recordset_sql($sql, array($str));
$rs = $DB->get_recordset_sql($sql, array($str, 'user', 'draft'));
$files = array();
foreach ($rs as $filerecord) {
$file = $this->get_file_instance($filerecord);
Expand All @@ -1691,9 +1691,9 @@ public function search_references_count($str) {
FROM {files} f
LEFT JOIN {files_reference} r
ON f.referencefileid = r.id
WHERE r.reference = ?";
WHERE r.reference = ? AND (f.component <> ? OR f.filearea <> ?)";

$count = $DB->count_records_sql($sql, array($str));
$count = $DB->count_records_sql($sql, array($str, 'user', 'draft'));
return $count;
}

Expand Down Expand Up @@ -1722,9 +1722,9 @@ public function get_references_by_storedfile($storedfile) {
FROM {files} f
LEFT JOIN {files_reference} r
ON f.referencefileid = r.id
WHERE r.reference = ?";
WHERE r.reference = ? AND (f.component <> ? OR f.filearea <> ?)";

$rs = $DB->get_recordset_sql($sql, array($reference));
$rs = $DB->get_recordset_sql($sql, array($reference, 'user', 'draft'));
$files = array();
foreach ($rs as $filerecord) {
$file = $this->get_file_instance($filerecord);
Expand Down Expand Up @@ -1761,9 +1761,9 @@ public function get_references_count_by_storedfile($storedfile) {
FROM {files} f
LEFT JOIN {files_reference} r
ON f.referencefileid = r.id
WHERE r.reference = ?";
WHERE r.reference = ? AND (f.component <> ? OR f.filearea <> ?)";

$count = $DB->count_records_sql($sql, array($reference));
$count = $DB->count_records_sql($sql, array($reference, 'user', 'draft'));
return $count;
}

Expand Down
3 changes: 3 additions & 0 deletions lib/filestorage/stored_file.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ protected function update($dataobject) {
* @param string $filename file name
*/
public function rename($filepath, $filename) {
if ($this->fs->file_exists($this->get_contextid(), $this->get_component(), $this->get_filearea(), $this->get_itemid(), $filepath, $filename)) {
throw new file_exception('storedfilenotcreated', '', 'file exists, cannot rename');
}
$filerecord = new stdClass;
$filerecord->filepath = $filepath;
$filerecord->filename = $filename;
Expand Down
6 changes: 5 additions & 1 deletion lib/filestorage/tests/file_storage_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,12 @@ public function test_file_renaming() {
$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());
Expand Down
39 changes: 35 additions & 4 deletions lib/tests/filelib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,16 @@ public function test_prepare_draft_area() {
$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();
$component = 'core';
$filearea = 'unittest';
Expand All @@ -122,28 +130,51 @@ public function test_prepare_draft_area() {
'source' => $sourcefield,
);
$ref = $fs->pack_reference($filerecord);

$originalfile = $fs->create_file_from_string($filerecord, 'Test content');

$fileid = $originalfile->get_id();
$this->assertInstanceOf('stored_file', $originalfile);

// 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, 'User file content');
$userfileref = $fs->pack_reference($userfilerecord);

$filerefrecord = clone((object)$filerecord);
$filerefrecord->filename = 'testref.txt';
// create a file reference
$fileref = $fs->create_file_from_reference($filerefrecord, $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, $syscontext->id, $component, $filearea, $itemid);

$draftfiles = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid);
$this->assertEquals(2, count($draftfiles));
$this->assertEquals(3, count($draftfiles));

$draftfile = $fs->get_file($usercontext->id, 'user', 'draft', $draftitemid, $filepath, $filename);
$source = unserialize($draftfile->get_source());
$this->assertEquals($ref, $source->original);
$this->assertEquals($sourcefield, $source->source);

$draftfileref = $fs->get_file($usercontext->id, 'user', 'draft', $draftitemid, $filepath, $filerefrecord->filename);
$this->assertInstanceOf('stored_file', $draftfileref);
$this->assertEquals(true, $draftfileref->is_external_file());

// change some information
$author = 'Dongsheng Cai';
$draftfile->set_author($author);
$newsourcefield = 'Get from flickr';
$newsourcefield = 'Get from Flickr';
$license = 'GPLv3';
$draftfile->set_license($license);
// if you want to really just change source field, do this:
Expand Down

0 comments on commit 7051415

Please sign in to comment.