<?php
-
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
/**
* Unit tests for /lib/filestorage/file_storage.php
*
- * @package core
- * @category test
+ * @package core_files
+ * @category phpunit
* @copyright 2012 David Mudrak <david@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
$this->assertFileExists($location);
-
// Verify the dir placeholder files are created.
$this->assertEquals(3, $DB->count_records('files', array()));
$this->assertTrue($DB->record_exists('files', array('pathnamehash'=>sha1('/'.$filerecord['contextid'].'/'.$filerecord['component'].'/'.$filerecord['filearea'].'/'.$filerecord['itemid'].'/.'))));
$this->assertTrue($DB->record_exists('files', array('pathnamehash'=>sha1('/'.$filerecord['contextid'].'/'.$filerecord['component'].'/'.$filerecord['filearea'].'/'.$filerecord['itemid'].$filerecord['filepath'].'.'))));
-
// Tests that missing content file is recreated.
unlink($location);
$this->assertEquals(4, $DB->count_records('files', array()));
-
// Test that borked content file is recreated.
$this->assertSame(2, file_put_contents($location, 'xx'));
$this->assertFileExists($location);
-
// Verify the dir placeholder files are created.
$this->assertEquals(3, $DB->count_records('files', array()));
$this->assertTrue($DB->record_exists('files', array('pathnamehash'=>sha1('/'.$filerecord['contextid'].'/'.$filerecord['component'].'/'.$filerecord['filearea'].'/'.$filerecord['itemid'].'/.'))));
$this->assertTrue($DB->record_exists('files', array('pathnamehash'=>sha1('/'.$filerecord['contextid'].'/'.$filerecord['component'].'/'.$filerecord['filearea'].'/'.$filerecord['itemid'].$filerecord['filepath'].'.'))));
-
// Tests that missing content file is recreated.
unlink($location);
$this->assertEquals(4, $DB->count_records('files', array()));
-
// Test that borked content file is recreated.
$this->assertSame(2, file_put_contents($location, 'xx'));
public function test_get_file_preview(stored_file $file) {
global $CFG;
- $this->resetAfterTest(true);
+ $this->resetAfterTest();
$fs = get_file_storage();
$previewtinyicon = $fs->get_file_preview($file, 'tinyicon');
public function test_file_renaming() {
global $CFG;
- $this->resetAfterTest(true);
+ $this->resetAfterTest();
$fs = get_file_storage();
$syscontext = context_system::instance();
$component = 'core';
$newpath = '/test/';
$newname = 'newtest.txt';
- // this should work
+ // 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
+ // Try break it.
$this->setExpectedException('file_exception');
- // this shall throw exception
+ // This shall throw exception.
$originalfile->rename($newpath, $newname);
}
public function test_create_file_from_reference() {
global $CFG, $DB;
- $this->resetAfterTest(true);
- // create user
+ $this->resetAfterTest();
+ // Create user.
$generator = $this->getDataGenerator();
$user = $generator->create_user();
$this->setUser($user);
$fs = get_file_storage();
$repositorypluginname = 'user';
- // override repository permission
+ // Override repository permission.
$capability = 'repository/' . $repositorypluginname . ':view';
$allroles = $DB->get_records_menu('role', array(), 'id', 'archetype, id');
assign_capability($capability, CAP_ALLOW, $allroles['guest'], $syscontext->id, true);
-
$args = array();
$args['type'] = $repositorypluginname;
$repos = repository::get_instances($args);
$this->assertEquals($originalfile->get_filesize(), $newstoredfile->get_filesize());
$this->assertRegExp('#' . $filename. '$#', $newstoredfile->get_reference_details());
- // Test looking for references
+ // Test looking for references.
$count = $fs->get_references_count_by_storedfile($originalfile);
$this->assertEquals(1, $count);
$files = $fs->get_references_by_storedfile($originalfile);
$file = reset($files);
$this->assertEquals($file, $newstoredfile);
- // Look for references by repository ID
+ // Look for references by repository ID.
$files = $fs->get_external_files($userrepository->id);
$file = reset($files);
$this->assertEquals($file, $newstoredfile);
- // Try convert reference to local file
+ // Try convert reference to local file.
$importedfile = $fs->import_external_file($newstoredfile);
$this->assertFalse($importedfile->is_external_file());
$this->assertInstanceOf('stored_file', $importedfile);
- // still readable?
+ // Still readable?
$this->assertEquals($content, $importedfile->get_content());
}
private function setup_three_private_files() {
- $this->resetAfterTest(true);
+ $this->resetAfterTest();
$generator = $this->getDataGenerator();
$user = $generator->create_user();
$this->setUser($user->id);
$usercontext = context_user::instance($user->id);
- // create a user private file
+ // Create a user private file.
$file1 = new stdClass;
$file1->contextid = $usercontext->id;
$file1->component = 'user';
$this->assertEquals($key, $file->get_pathnamehash());
}
- // Test with an itemid with no files
+ // Test with an itemid with no files.
$areafiles = $fs->get_area_files($user->ctxid, 'user', 'private', 666, 'sortorder', false);
// Should be none.
$this->assertEmpty($areafiles);
$user = $this->setup_three_private_files();
$fs = get_file_storage();
-
// Get area files with default options.
$areatree = $fs->get_area_tree($user->ctxid, 'user', 'private', 0);
$this->assertEmpty($areatree['subdirs']);
$fs = get_file_storage();
$areafiles = $fs->get_area_files($user->ctxid, 'user', 'private');
- // Test get_file_by_hash
+ // Test get_file_by_hash.
$filebyhash = reset($areafiles);
$shouldbesame = $fs->get_file_by_hash($filebyhash->get_pathnamehash());
$this->assertEquals($filebyhash->get_id(), $shouldbesame->get_id());
$userrepository = reset($repos);
$this->assertInstanceOf('repository', $userrepository);
- // no aliases yet
+ // No aliases yet.
$exfiles = $fs->get_external_files($userrepository->id, 'id');
$this->assertEquals(array(), $exfiles);
- // create three aliases linking the same original: $aliasfile1 and $aliasfile2 are
- // created via create_file_from_reference(), $aliasfile3 created from $aliasfile2
+ // Create three aliases linking the same original: $aliasfile1 and $aliasfile2 are
+ // created via create_file_from_reference(), $aliasfile3 created from $aliasfile2.
$originalfile = null;
foreach ($fs->get_area_files($user->ctxid, 'user', 'private') as $areafile) {
if (!$areafile->is_directory()) {
$aliasrecord->filepath = '/bar/';
$aliasrecord->filename = 'uno.txt';
- // change the order of the items in the array to make sure that it does not matter
+ // Change the order of the items in the array to make sure that it does not matter.
ksort($originalrecord);
$ref = $fs->pack_reference($originalrecord);
$aliasfile2 = $fs->create_file_from_reference($aliasrecord, $userrepository->id, $ref);
$aliasrecord->filename = 'jedna.txt';
$aliasfile3 = $fs->create_file_from_storedfile($aliasrecord, $aliasfile2);
- // make sure we get three aliases now
+ // Make sure we get three aliases now.
$exfiles = $fs->get_external_files($userrepository->id, 'id');
$this->assertEquals(3, count($exfiles));
foreach ($exfiles as $exfile) {
$this->assertTrue($exfile->is_external_file());
}
- // make sure they all link the same original (thence that all are linked with the same
- // record in {files_reference})
+ // Make sure they all link the same original (thence that all are linked with the same
+ // record in {files_reference}).
$this->assertEquals($aliasfile1->get_referencefileid(), $aliasfile2->get_referencefileid());
$this->assertEquals($aliasfile3->get_referencefileid(), $aliasfile2->get_referencefileid());
}
$directoryfile = $fs->create_file_from_string($filerecord, 'Test content');
$this->assertInstanceOf('stored_file', $directoryfile);
- // Don't recurse without dirs
+ // Don't recurse without dirs.
$files = $fs->get_directory_files($user->ctxid, 'user', 'private', 0, '/', false, false, 'id');
// 3 files only.
$this->assertCount(3, $files);
$result = $fs->search_references_count($reference);
$this->assertSame($result, 2);
- // The method can't be used for references to files outside the filepool
+ // 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');
$repos = repository::get_instances(array('type'=>'user'));
$repo = reset($repos);
- // create two aliases linking the same original
+ // Create two aliases linking the same original.
$originalfile = null;
foreach ($fs->get_area_files($user->ctxid, 'user', 'private') as $areafile) {
}
$this->assertInstanceOf('stored_file', $originalfile);
- // calling delete_reference() on a non-reference file
+ // Calling delete_reference() on a non-reference file.
$originalrecord = array(
'contextid' => $originalfile->get_contextid(),
$ref = $fs->pack_reference($originalrecord);
$aliasfile2 = $fs->create_file_from_reference($aliasrecord, $repo->id, $ref);
- // refetch A/symlink.txt
+ // Refetch A/symlink.txt file.
$symlink1 = $fs->get_file($aliasrecord->contextid, $aliasrecord->component,
$aliasrecord->filearea, $aliasrecord->itemid, '/A/', 'symlink.txt');
$this->assertTrue($symlink1->is_external_file());
- // unlink the A/symlink.txt
+ // Unlink the A/symlink.txt file.
$symlink1->delete_reference();
$this->assertFalse($symlink1->is_external_file());
- // make sure that B/symlink.txt has not been affected
+ // Make sure that B/symlink.txt has not been affected.
$symlink2 = $fs->get_file($aliasrecord->contextid, $aliasrecord->component,
$aliasrecord->filearea, $aliasrecord->itemid, '/B/', 'symlink.txt');
$this->assertTrue($symlink2->is_external_file());