Skip to content

Commit

Permalink
Merge branch 'MDL-33453-reference' of git://github.com/mudrd8mz/moodl…
Browse files Browse the repository at this point in the history
…e into MOODLE_23_STABLE
  • Loading branch information
stronk7 committed Jul 5, 2012
2 parents 4539b33 + d81d6ea commit 353580b
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 20 deletions.
27 changes: 24 additions & 3 deletions lib/filestorage/file_storage.php
Expand Up @@ -1656,6 +1656,7 @@ public static function pack_reference($params) {
*
* @param string $str
* @param bool $cleanparams if set to true, array elements will be passed through {@link clean_param()}
* @throws file_reference_exception if the $str does not have the expected format
* @return array
*/
public static function unpack_reference($str, $cleanparams = false) {
Expand All @@ -1681,7 +1682,13 @@ public static function unpack_reference($str, $cleanparams = false) {
}

/**
* Returns all aliases that link to an external file identified by the given reference
* Returns all aliases that refer to some stored_file via the given reference
*
* All repositories that provide access to a stored_file are expected to use
* {@link self::pack_reference()}. This method can't be used if the given reference
* does not use this format or if you are looking for references to an external file
* (for example it can't be used to search for all aliases that refer to a given
* Dropbox or Box.net file).
*
* Aliases in user draft areas are excluded from the returned list.
*
Expand All @@ -1695,6 +1702,10 @@ public function search_references($reference) {
throw new coding_exception('NULL is not a valid reference to an external file');
}

// Give {@link self::unpack_reference()} a chance to throw exception if the
// reference is not in a valid format.
self::unpack_reference($reference);

$referencehash = sha1($reference);

$sql = "SELECT ".self::instance_sql_fields('f', 'r')."
Expand All @@ -1714,7 +1725,13 @@ public function search_references($reference) {
}

/**
* Returns the number of aliases that link to an external file identified by the given reference
* Returns the number of aliases that refer to some stored_file via the given reference
*
* All repositories that provide access to a stored_file are expected to use
* {@link self::pack_reference()}. This method can't be used if the given reference
* does not use this format or if you are looking for references to an external file
* (for example it can't be used to count aliases that refer to a given Dropbox or
* Box.net file).
*
* Aliases in user draft areas are not counted.
*
Expand All @@ -1728,6 +1745,10 @@ public function search_references_count($reference) {
throw new coding_exception('NULL is not a valid reference to an external file');
}

// Give {@link self::unpack_reference()} a chance to throw exception if the
// reference is not in a valid format.
self::unpack_reference($reference);

$referencehash = sha1($reference);

$sql = "SELECT COUNT(f.id)
Expand All @@ -1737,7 +1758,7 @@ public function search_references_count($reference) {
WHERE r.referencehash = ?
AND (f.component <> ? OR f.filearea <> ?)";

return $DB->count_records_sql($sql, array($referencehash, 'user', 'draft'));
return (int)$DB->count_records_sql($sql, array($referencehash, 'user', 'draft'));
}

/**
Expand Down
84 changes: 67 additions & 17 deletions lib/filestorage/tests/file_storage_test.php
Expand Up @@ -400,16 +400,6 @@ public function test_get_file_by_hash() {
$this->assertFalse($doesntexist);
}

public function test_get_references_by_storedfile() {
$user = $this->setup_three_private_files();
$fs = get_file_storage();

$areafiles = $fs->get_area_files($user->ctxid, 'user', 'private');
$testfile = reset($areafiles);
$references = $fs->get_references_by_storedfile($testfile);
// TODO MDL-33368 Verify result!!
}

public function test_get_external_files() {
$user = $this->setup_three_private_files();
$fs = get_file_storage();
Expand Down Expand Up @@ -583,15 +573,75 @@ public function test_get_directory_files() {
}

public function test_search_references() {
$user = $this->setup_three_private_files();
$fs = get_file_storage();
$references = $fs->search_references('testsearch');
// TODO MDL-33368 Verify result!!
}
$repos = repository::get_instances(array('type'=>'user'));
$repo = reset($repos);

public function test_search_references_count() {
$fs = get_file_storage();
$references = $fs->search_references_count('testsearch');
// TODO MDL-33368 Verify result!!
$alias1 = array(
'contextid' => $user->ctxid,
'component' => 'user',
'filearea' => 'private',
'itemid' => 0,
'filepath' => '/aliases/',
'filename' => 'alias-to-1.txt'
);

$alias2 = array(
'contextid' => $user->ctxid,
'component' => 'user',
'filearea' => 'private',
'itemid' => 0,
'filepath' => '/aliases/',
'filename' => 'another-alias-to-1.txt'
);

$reference = file_storage::pack_reference(array(
'contextid' => $user->ctxid,
'component' => 'user',
'filearea' => 'private',
'itemid' => 0,
'filepath' => '/',
'filename' => '1.txt'
));

// There are no aliases now.
$result = $fs->search_references($reference);
$this->assertEquals(array(), $result);

$result = $fs->search_references_count($reference);
$this->assertSame($result, 0);

// Create two aliases and make sure they are returned.
$fs->create_file_from_reference($alias1, $repo->id, $reference);
$fs->create_file_from_reference($alias2, $repo->id, $reference);

$result = $fs->search_references($reference);
$this->assertTrue(is_array($result));
$this->assertEquals(count($result), 2);
foreach ($result as $alias) {
$this->assertTrue($alias instanceof stored_file);
}

$result = $fs->search_references_count($reference);
$this->assertSame($result, 2);

// The method can't be used for references to files outside the filepool
$exceptionthrown = false;
try {
$fs->search_references('http://dl.dropbox.com/download/1234567/naked-dougiamas.jpg');
} catch (file_reference_exception $e) {
$exceptionthrown = true;
}
$this->assertTrue($exceptionthrown);

$exceptionthrown = false;
try {
$fs->search_references_count('http://dl.dropbox.com/download/1234567/naked-dougiamas.jpg');
} catch (file_reference_exception $e) {
$exceptionthrown = true;
}
$this->assertTrue($exceptionthrown);
}

public function test_delete_area_files() {
Expand Down

0 comments on commit 353580b

Please sign in to comment.