Skip to content

Commit

Permalink
Merge branch 'MDL-62482_35' of git://github.com/timhunt/moodle into M…
Browse files Browse the repository at this point in the history
…OODLE_35_STABLE
  • Loading branch information
andrewnicols committed May 22, 2018
2 parents ab7694d + deb358f commit 30cbfbd
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/filestorage/file_storage.php
Expand Up @@ -1575,7 +1575,7 @@ public function create_file_from_reference($filerecord, $repositoryid, $referenc

$existingfile = null;
if (isset($filerecord->contenthash)) {
$existingfile = $DB->get_record('files', array('contenthash' => $filerecord->contenthash));
$existingfile = $DB->get_record('files', array('contenthash' => $filerecord->contenthash), '*', IGNORE_MULTIPLE);
}
if (!empty($existingfile)) {
// There is an existing file already available.
Expand Down
72 changes: 72 additions & 0 deletions lib/filestorage/tests/file_storage_test.php
Expand Up @@ -397,6 +397,78 @@ public function test_create_file_from_reference() {
$this->assertEquals($content, $importedfile->get_content());
}

/**
* Create file from reference tests
*
* @copyright 2012 Dongsheng Cai {@link http://dongsheng.org}
*/
public function test_create_file_from_reference_with_content_hash() {
global $CFG, $DB;

$this->resetAfterTest();
// Create user.
$generator = $this->getDataGenerator();
$user = $generator->create_user();
$this->setUser($user);
$usercontext = context_user::instance($user->id);
$syscontext = context_system::instance();

$fs = get_file_storage();

$repositorypluginname = 'user';
// Override repository permission.
$capability = 'repository/' . $repositorypluginname . ':view';
$guestroleid = $DB->get_field('role', 'id', array('shortname' => 'guest'));
assign_capability($capability, CAP_ALLOW, $guestroleid, $syscontext->id, true);

$args = array();
$args['type'] = $repositorypluginname;
$repos = repository::get_instances($args);
$userrepository = reset($repos);
$this->assertInstanceOf('repository', $userrepository);

$component = 'user';
$filearea = 'private';
$itemid = 0;
$filepath = '/';
$filename = 'userfile.txt';

$filerecord = array(
'contextid' => $usercontext->id,
'component' => $component,
'filearea' => $filearea,
'itemid' => $itemid,
'filepath' => $filepath,
'filename' => $filename,
);

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

$otherfilerecord = $filerecord;
$otherfilerecord['filename'] = 'other-filename.txt';
$otherfilewithsamecontents = $fs->create_file_from_string($otherfilerecord, $content);
$this->assertInstanceOf('stored_file', $otherfilewithsamecontents);

$newfilerecord = array(
'contextid' => $syscontext->id,
'component' => 'core',
'filearea' => 'phpunit',
'itemid' => 0,
'filepath' => $filepath,
'filename' => $filename,
'contenthash' => $originalfile->get_contenthash(),
);
$ref = $fs->pack_reference($filerecord);
$newstoredfile = $fs->create_file_from_reference($newfilerecord, $userrepository->id, $ref);
$this->assertInstanceOf('stored_file', $newstoredfile);
$this->assertEquals($userrepository->id, $newstoredfile->get_repository_id());
$this->assertEquals($originalfile->get_contenthash(), $newstoredfile->get_contenthash());
$this->assertEquals($originalfile->get_filesize(), $newstoredfile->get_filesize());
$this->assertRegExp('#' . $filename . '$#', $newstoredfile->get_reference_details());
}

private function setup_three_private_files() {

$this->resetAfterTest();
Expand Down

0 comments on commit 30cbfbd

Please sign in to comment.