From fd4592bbbcbd65a69311addd06541f38206973ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20S=CC=8Ckoda?= Date: Sat, 20 Apr 2013 21:47:41 +0200 Subject: [PATCH] MDL-36959 test reworked adding of files to sha1 content pool --- lib/filestorage/tests/file_storage_test.php | 177 +++++++++++++++++++- 1 file changed, 173 insertions(+), 4 deletions(-) diff --git a/lib/filestorage/tests/file_storage_test.php b/lib/filestorage/tests/file_storage_test.php index b895e578bb5..709e886fc82 100644 --- a/lib/filestorage/tests/file_storage_test.php +++ b/lib/filestorage/tests/file_storage_test.php @@ -29,13 +29,168 @@ defined('MOODLE_INTERNAL') || die(); global $CFG; require_once($CFG->libdir . '/filelib.php'); require_once($CFG->dirroot . '/repository/lib.php'); +require_once($CFG->libdir . '/filestorage/stored_file.php'); class filestoragelib_testcase extends advanced_testcase { + /** + * Files can be created from strings. + */ + public function test_create_file_from_string() { + global $DB; + + $this->resetAfterTest(true); + + $this->assertEquals(0, $DB->count_records('files', array())); + + $content = 'abcd'; + $syscontext = context_system::instance(); + $filerecord = array( + 'contextid' => $syscontext->id, + 'component' => 'core', + 'filearea' => 'unittest', + 'itemid' => 0, + 'filepath' => '/images/', + 'filename' => 'testfile.txt', + ); + $pathhash = sha1('/'.$filerecord['contextid'].'/'.$filerecord['component'].'/'.$filerecord['filearea'].'/'.$filerecord['itemid'].$filerecord['filepath'].$filerecord['filename']); + + $fs = get_file_storage(); + $file = $fs->create_file_from_string($filerecord, $content); + + $this->assertInstanceOf('stored_file', $file); + $this->assertSame(sha1($content), $file->get_contenthash()); + $this->assertSame($pathhash, $file->get_pathnamehash()); + + $this->assertTrue($DB->record_exists('files', array('pathnamehash'=>$pathhash))); + + $location = test_stored_file_inspection::get_pretected_pathname($file); + + $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->assertFileNotExists($location); + + $filerecord['filename'] = 'testfile2.txt'; + $file2 = $fs->create_file_from_string($filerecord, $content); + $this->assertInstanceOf('stored_file', $file2); + $this->assertSame($file->get_contenthash(), $file2->get_contenthash()); + $this->assertFileExists($location); + + $this->assertEquals(4, $DB->count_records('files', array())); + + + // Test that borked content file is recreated. + + $this->assertSame(2, file_put_contents($location, 'xx')); + + $filerecord['filename'] = 'testfile3.txt'; + $file3 = $fs->create_file_from_string($filerecord, $content); + $this->assertInstanceOf('stored_file', $file3); + $this->assertSame($file->get_contenthash(), $file3->get_contenthash()); + $this->assertFileExists($location); + + $this->assertSame($content, file_get_contents($location)); + $this->assertDebuggingCalled(); + + $this->assertEquals(5, $DB->count_records('files', array())); + } + /** * Local files can be added to the filepool */ public function test_create_file_from_pathname() { + global $CFG, $DB; + + $this->resetAfterTest(true); + + $filecount = $DB->count_records('files', array()); + $this->assertEquals(0, $filecount); + + $filepath = $CFG->dirroot.'/lib/filestorage/tests/fixtures/testimage.jpg'; + $syscontext = context_system::instance(); + $filerecord = array( + 'contextid' => $syscontext->id, + 'component' => 'core', + 'filearea' => 'unittest', + 'itemid' => 0, + 'filepath' => '/images/', + 'filename' => 'testimage.jpg', + ); + $pathhash = sha1('/'.$filerecord['contextid'].'/'.$filerecord['component'].'/'.$filerecord['filearea'].'/'.$filerecord['itemid'].$filerecord['filepath'].$filerecord['filename']); + + $fs = get_file_storage(); + $file = $fs->create_file_from_pathname($filerecord, $filepath); + + $this->assertInstanceOf('stored_file', $file); + $this->assertSame(sha1_file($filepath), $file->get_contenthash()); + + $this->assertTrue($DB->record_exists('files', array('pathnamehash'=>$pathhash))); + + $location = test_stored_file_inspection::get_pretected_pathname($file); + + $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->assertFileNotExists($location); + + $filerecord['filename'] = 'testfile2.jpg'; + $file2 = $fs->create_file_from_pathname($filerecord, $filepath); + $this->assertInstanceOf('stored_file', $file2); + $this->assertSame($file->get_contenthash(), $file2->get_contenthash()); + $this->assertFileExists($location); + + $this->assertEquals(4, $DB->count_records('files', array())); + + + // Test that borked content file is recreated. + + $this->assertSame(2, file_put_contents($location, 'xx')); + + $filerecord['filename'] = 'testfile3.jpg'; + $file3 = $fs->create_file_from_pathname($filerecord, $filepath); + $this->assertInstanceOf('stored_file', $file3); + $this->assertSame($file->get_contenthash(), $file3->get_contenthash()); + $this->assertFileExists($location); + + $this->assertSame(file_get_contents($filepath), file_get_contents($location)); + $this->assertDebuggingCalled(); + + $this->assertEquals(5, $DB->count_records('files', array())); + + // Test invalid file creation. + + $filerecord['filename'] = 'testfile4.jpg'; + try { + $fs->create_file_from_pathname($filerecord, $filepath.'nonexistent'); + $this->fail('Exception expected when trying to add non-existent stored file.'); + } catch (Exception $e) { + $this->assertInstanceOf('file_exception', $e); + } + } + + /** + * Tests get get file. + */ + public function test_get_file() { global $CFG; $this->resetAfterTest(false); @@ -50,19 +205,28 @@ class filestoragelib_testcase extends advanced_testcase { 'filepath' => '/images/', 'filename' => 'testimage.jpg', ); + $pathhash = sha1('/'.$filerecord['contextid'].'/'.$filerecord['component'].'/'.$filerecord['filearea'].'/'.$filerecord['itemid'].$filerecord['filepath'].$filerecord['filename']); $fs = get_file_storage(); - $fs->create_file_from_pathname($filerecord, $filepath); + $file = $fs->create_file_from_pathname($filerecord, $filepath); - $this->assertTrue($fs->file_exists($syscontext->id, 'core', 'unittest', 0, '/images/', 'testimage.jpg')); + $this->assertInstanceOf('stored_file', $file); + $this->assertEquals($syscontext->id, $file->get_contextid()); + $this->assertEquals('core', $file->get_component()); + $this->assertEquals('unittest', $file->get_filearea()); + $this->assertEquals(0, $file->get_itemid()); + $this->assertEquals('/images/', $file->get_filepath()); + $this->assertEquals('testimage.jpg', $file->get_filename()); + $this->assertEquals(filesize($filepath), $file->get_filesize()); + $this->assertEquals($pathhash, $file->get_pathnamehash()); - return $fs->get_file($syscontext->id, 'core', 'unittest', 0, '/images/', 'testimage.jpg'); + return $file; } /** * Local images can be added to the filepool and their preview can be obtained * - * @depends test_create_file_from_pathname + * @depends test_get_file */ public function test_get_file_preview(stored_file $file) { global $CFG; @@ -1421,5 +1585,10 @@ class filestoragelib_testcase extends advanced_testcase { $this->setExpectedException('coding_exception'); $fs->get_unused_filename($contextid, $component, $filearea, $itemid, $filepath, ''); } +} +class test_stored_file_inspection extends stored_file { + public static function get_pretected_pathname(stored_file $file) { + return $file->get_pathname_by_contenthash(); + } }