Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
MDL-33587 Add unit tests for stored_file::delete_reference()
The first unit test forces the File API to be more picky as any sane
core API should be. The second one illustrates the buggy behaviour of
the current implementation of delete_reference(). They both fail at the
moment, of course...
  • Loading branch information
mudrd8mz committed Jun 7, 2012
1 parent 620ecd3 commit 2d2c194
Showing 1 changed file with 86 additions and 0 deletions.
86 changes: 86 additions & 0 deletions lib/filestorage/tests/file_storage_test.php
Expand Up @@ -1212,4 +1212,90 @@ public function test_create_file_from_pathname_duplicate_file() {
$this->setExpectedException('stored_file_creation_exception');
$file2 = $fs->create_file_from_pathname($filerecord, $path);
}

/**
* Calling stored_file::delete_reference() on a non-reference file throws coding_exception
*/
public function test_delete_reference_on_nonreference() {

$this->resetAfterTest(true);
$user = $this->setup_three_private_files();
$fs = get_file_storage();
$repos = repository::get_instances(array('type'=>'user'));
$repo = reset($repos);

$file = null;
foreach ($fs->get_area_files($user->ctxid, 'user', 'private') as $areafile) {
if (!$areafile->is_directory()) {
$file = $areafile;
break;
}
}
$this->assertInstanceOf('stored_file', $file);
$this->assertFalse($file->is_external_file());

$this->setExpectedException('coding_exception');
$file->delete_reference();
}

/**
* Calling stored_file::delete_reference() on a reference file does not affect other
* symlinks to the same original
*/
public function test_delete_reference_one_symlink_does_not_rule_them_all() {

$this->resetAfterTest(true);
$user = $this->setup_three_private_files();
$fs = get_file_storage();
$repos = repository::get_instances(array('type'=>'user'));
$repo = reset($repos);

// create two aliases linking the same original

$originalfile = null;
foreach ($fs->get_area_files($user->ctxid, 'user', 'private') as $areafile) {
if (!$areafile->is_directory()) {
$originalfile = $areafile;
break;
}
}
$this->assertInstanceOf('stored_file', $originalfile);

// calling delete_reference() on a non-reference file

$originalrecord = array(
'contextid' => $originalfile->get_contextid(),
'component' => $originalfile->get_component(),
'filearea' => $originalfile->get_filearea(),
'itemid' => $originalfile->get_itemid(),
'filepath' => $originalfile->get_filepath(),
'filename' => $originalfile->get_filename(),
);

$aliasrecord = $this->generate_file_record();
$aliasrecord->filepath = '/A/';
$aliasrecord->filename = 'symlink.txt';

$ref = $fs->pack_reference($originalrecord);
$aliasfile1 = $fs->create_file_from_reference($aliasrecord, $repo->id, $ref);

$aliasrecord->filepath = '/B/';
$aliasrecord->filename = 'symlink.txt';
$ref = $fs->pack_reference($originalrecord);
$aliasfile2 = $fs->create_file_from_reference($aliasrecord, $repo->id, $ref);

// refetch A/symlink.txt
$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
$symlink1->delete_reference();
$this->assertFalse($symlink1->is_external_file());

// 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());
}
}

0 comments on commit 2d2c194

Please sign in to comment.