Permalink
Browse files

MDL-37641 files: Tests for draftfile_exists and get_unused_filename

  • Loading branch information...
1 parent d7d6939 commit 3385a89c38ff979866dd5fdc80664739ea335ec7 @FMCorz FMCorz committed Feb 14, 2013
Showing with 159 additions and 0 deletions.
  1. +63 −0 lib/filestorage/tests/file_storage_test.php
  2. +96 −0 repository/tests/repository_test.php
@@ -1352,4 +1352,67 @@ public function test_delete_reference_one_symlink_does_not_rule_them_all() {
$aliasrecord->filearea, $aliasrecord->itemid, '/B/', 'symlink.txt');
$this->assertTrue($symlink2->is_external_file());
}
+
+ public function test_get_unused_filename() {
+ global $USER;
+ $this->resetAfterTest(true);
+
+ $fs = get_file_storage();
+ $this->setAdminUser();
+ $contextid = context_user::instance($USER->id)->id;
+ $component = 'user';
+ $filearea = 'private';
+ $itemid = 0;
+ $filepath = '/';
+
+ // Create some private files.
+ $file = new stdClass;
+ $file->contextid = $contextid;
+ $file->component = 'user';
+ $file->filearea = 'private';
+ $file->itemid = 0;
+ $file->filepath = '/';
+ $file->source = 'test';
+ $filenames = array('foo.txt', 'foo (1).txt', 'foo (20).txt', 'foo (999)', 'bar.jpg', 'What (a cool file).jpg',
+ 'Hurray! (1).php', 'Hurray! (2).php', 'Hurray! (9a).php', 'Hurray! (abc).php');
+ foreach ($filenames as $key => $filename) {
+ $file->filename = $filename;
+ $userfile = $fs->create_file_from_string($file, "file $key $filename content");
+ $this->assertInstanceOf('stored_file', $userfile);
+ }
+
+ // Asserting new generated names.
+ $newfilename = $fs->get_unused_filename($contextid, $component, $filearea, $itemid, $filepath, 'unused.txt');
+ $this->assertEquals('unused.txt', $newfilename);
+ $newfilename = $fs->get_unused_filename($contextid, $component, $filearea, $itemid, $filepath, 'foo.txt');
+ $this->assertEquals('foo (21).txt', $newfilename);
+ $newfilename = $fs->get_unused_filename($contextid, $component, $filearea, $itemid, $filepath, 'foo (1).txt');
+ $this->assertEquals('foo (21).txt', $newfilename);
+ $newfilename = $fs->get_unused_filename($contextid, $component, $filearea, $itemid, $filepath, 'foo (2).txt');
+ $this->assertEquals('foo (2).txt', $newfilename);
+ $newfilename = $fs->get_unused_filename($contextid, $component, $filearea, $itemid, $filepath, 'foo (20).txt');
+ $this->assertEquals('foo (21).txt', $newfilename);
+ $newfilename = $fs->get_unused_filename($contextid, $component, $filearea, $itemid, $filepath, 'foo');
+ $this->assertEquals('foo', $newfilename);
+ $newfilename = $fs->get_unused_filename($contextid, $component, $filearea, $itemid, $filepath, 'foo (123)');
+ $this->assertEquals('foo (123)', $newfilename);
+ $newfilename = $fs->get_unused_filename($contextid, $component, $filearea, $itemid, $filepath, 'foo (999)');
+ $this->assertEquals('foo (1000)', $newfilename);
+ $newfilename = $fs->get_unused_filename($contextid, $component, $filearea, $itemid, $filepath, 'bar.png');
+ $this->assertEquals('bar.png', $newfilename);
+ $newfilename = $fs->get_unused_filename($contextid, $component, $filearea, $itemid, $filepath, 'bar (12).png');
+ $this->assertEquals('bar (12).png', $newfilename);
+ $newfilename = $fs->get_unused_filename($contextid, $component, $filearea, $itemid, $filepath, 'bar.jpg');
+ $this->assertEquals('bar (1).jpg', $newfilename);
+ $newfilename = $fs->get_unused_filename($contextid, $component, $filearea, $itemid, $filepath, 'bar (1).jpg');
+ $this->assertEquals('bar (1).jpg', $newfilename);
+ $newfilename = $fs->get_unused_filename($contextid, $component, $filearea, $itemid, $filepath, 'What (a cool file).jpg');
+ $this->assertEquals('What (a cool file) (1).jpg', $newfilename);
+ $newfilename = $fs->get_unused_filename($contextid, $component, $filearea, $itemid, $filepath, 'Hurray! (1).php');
+ $this->assertEquals('Hurray! (3).php', $newfilename);
+
+ $this->setExpectedException('coding_exception');
+ $fs->get_unused_filename($contextid, $component, $filearea, $itemid, $filepath, '');
+ }
+
}
@@ -58,4 +58,100 @@ public function test_install_repository() {
$info = $repository->get_meta();
$this->assertEquals($repositorypluginname, $info->type);
}
+
+ public function test_get_unused_filename() {
+ global $USER;
+
+ $this->resetAfterTest(true);
+
+ $this->setAdminUser();
+ $fs = get_file_storage();
+
+ $draftitemid = null;
+ $context = context_user::instance($USER->id);
+ file_prepare_draft_area($draftitemid, $context->id, 'phpunit', 'test_get_unused_filename', 1);
+
+ $dummy = array(
+ 'contextid' => $context->id,
+ 'component' => 'user',
+ 'filearea' => 'draft',
+ 'itemid' => $draftitemid,
+ 'filepath' => '/',
+ 'filename' => ''
+ );
+
+ // Create some files.
+ $existingfiles = array(
+ 'test',
+ 'test.txt',
+ 'test (1).txt',
+ 'test1.txt',
+ 'test1 (1).txt',
+ 'test1 (2).txt',
+ 'test1 (3).txt',
+ 'test1 (My name is Bob).txt',
+ 'test2 (555).txt',
+ 'test3 (1000).txt',
+ 'test3 (1000MB).txt',
+ );
+ foreach ($existingfiles as $filename) {
+ $dummy['filename'] = $filename;
+ $file = $fs->create_file_from_string($dummy, 'blah! ' . $filename);
+ $this->assertTrue(repository::draftfile_exists($draftitemid, '/', $filename));
+ }
+
+ // Actual testing.
+ $this->assertEquals('free.txt', repository::get_unused_filename($draftitemid, '/', 'free.txt'));
+ $this->assertEquals('test (1)', repository::get_unused_filename($draftitemid, '/', 'test'));
+ $this->assertEquals('test (2).txt', repository::get_unused_filename($draftitemid, '/', 'test.txt'));
+ $this->assertEquals('test1 (4).txt', repository::get_unused_filename($draftitemid, '/', 'test1.txt'));
+ $this->assertEquals('test1 (8).txt', repository::get_unused_filename($draftitemid, '/', 'test1 (8).txt'));
+ $this->assertEquals('test1 ().txt', repository::get_unused_filename($draftitemid, '/', 'test1 ().txt'));
+ $this->assertEquals('test2 (556).txt', repository::get_unused_filename($draftitemid, '/', 'test2 (555).txt'));
+ $this->assertEquals('test3 (1001).txt', repository::get_unused_filename($draftitemid, '/', 'test3 (1000).txt'));
+ $this->assertEquals('test3 (1000MB) (1).txt', repository::get_unused_filename($draftitemid, '/', 'test3 (1000MB).txt'));
+ $this->assertEquals('test4 (1).txt', repository::get_unused_filename($draftitemid, '/', 'test4 (1).txt'));
+ }
+
+ public function test_draftfile_exists() {
+ global $USER;
+
+ $this->resetAfterTest(true);
+
+ $this->setAdminUser();
+ $fs = get_file_storage();
+
+ $draftitemid = file_get_unused_draft_itemid();
+ $context = context_user::instance($USER->id);
+
+ $dummy = array(
+ 'contextid' => $context->id,
+ 'component' => 'user',
+ 'filearea' => 'draft',
+ 'itemid' => $draftitemid,
+ 'filepath' => '/',
+ 'filename' => ''
+ );
+
+ // Create some files.
+ $existingfiles = array(
+ 'The Matrix.movie',
+ 'Astalavista.txt',
+ 'foobar',
+ );
+ foreach ($existingfiles as $filename) {
+ $dummy['filename'] = $filename;
+ $file = $fs->create_file_from_string($dummy, 'Content of ' . $filename);
+ $this->assertInstanceOf('stored_file', $file);
+ }
+
+ // Doing the text.
+ foreach ($existingfiles as $filename) {
+ $this->assertTrue(repository::draftfile_exists($draftitemid, '/', $filename));
+ }
+ foreach (array('Terminator.movie', 'Where is Wally?', 'barfoo') as $filename) {
+ $this->assertFalse(repository::draftfile_exists($draftitemid, '/', $filename));
+ }
+ }
+
}

0 comments on commit 3385a89

Please sign in to comment.