From 05cd150fd228a51f0d32218804d6c1cb88837a28 Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Tue, 17 Jun 2014 22:30:11 +0200 Subject: [PATCH] add additional tests for the trash bin --- apps/files_trashbin/lib/trashbin.php | 2 +- apps/files_trashbin/tests/trashbin.php | 139 +++++++++++++++++++++++-- 2 files changed, 132 insertions(+), 9 deletions(-) diff --git a/apps/files_trashbin/lib/trashbin.php b/apps/files_trashbin/lib/trashbin.php index f3d255d179d..d7c07274970 100644 --- a/apps/files_trashbin/lib/trashbin.php +++ b/apps/files_trashbin/lib/trashbin.php @@ -760,7 +760,7 @@ class Trashbin { * @param int $availableSpace available disc space * @return int size of deleted files */ - protected function deleteFiles($files, $user, $availableSpace) { + protected static function deleteFiles($files, $user, $availableSpace) { $size = 0; if ($availableSpace < 0) { diff --git a/apps/files_trashbin/tests/trashbin.php b/apps/files_trashbin/tests/trashbin.php index e94a5011094..6a8955f5d1d 100644 --- a/apps/files_trashbin/tests/trashbin.php +++ b/apps/files_trashbin/tests/trashbin.php @@ -30,8 +30,14 @@ use OCA\Files_Trashbin; class Test_Trashbin extends \PHPUnit_Framework_TestCase { const TEST_TRASHBIN_USER1 = "test-trashbin-user1"; + const TEST_TRASHBIN_USER2 = "test-trashbin-user2"; - private $trashRoot; + private $trashRoot1; + private $trashRoot2; + + private static $encryptionStatus; + private static $rememberRetentionObligation; + private static $rememberAutoExpire; /** * @var \OC\Files\View @@ -43,10 +49,27 @@ class Test_Trashbin extends \PHPUnit_Framework_TestCase { \OC_User::clearBackends(); \OC_User::useBackend('database'); + // clear share hooks + \OC_Hook::clear('OCP\\Share'); + \OC::registerShareHooks(); + \OCP\Util::connectHook('OC_Filesystem', 'setup', '\OC\Files\Storage\Shared', 'setup'); + + //disable encryption + self::$encryptionStatus = \OC_App::isEnabled('files_encryption'); + \OC_App::disable('files_encryption'); + + //configure trashbin + self::$rememberRetentionObligation = \OC_Config::getValue('trashbin_retention_obligation', Files_Trashbin\Trashbin::DEFAULT_RETENTION_OBLIGATION); + \OC_Config::setValue('trashbin_retention_obligation', 2); + self::$rememberAutoExpire = \OC_Config::getValue('trashbin_auto_expire', true); + \OC_Config::setValue('trashbin_auto_expire', true); + + // register hooks Files_Trashbin\Trashbin::registerHooks(); // create test user + self::loginHelper(self::TEST_TRASHBIN_USER2, true); self::loginHelper(self::TEST_TRASHBIN_USER1, true); } @@ -56,16 +79,26 @@ class Test_Trashbin extends \PHPUnit_Framework_TestCase { // cleanup test user \OC_User::deleteUser(self::TEST_TRASHBIN_USER1); + if (self::$encryptionStatus === true) { + \OC_App::enable('files_encryption'); + } + + \OC_Config::setValue('trashbin_retention_obligation', self::$rememberRetentionObligation); + \OC_Config::setValue('trashbin_auto_expire', self::$rememberAutoExpire); + \OC_Hook::clear(); } public function setUp() { - $this->trashRoot = '/' . self::TEST_TRASHBIN_USER1 . '/files_trashbin'; + $this->trashRoot1 = '/' . self::TEST_TRASHBIN_USER1 . '/files_trashbin'; + $this->trashRoot2 = '/' . self::TEST_TRASHBIN_USER2 . '/files_trashbin'; $this->rootView = new \OC\Files\View(); + self::loginHelper(self::TEST_TRASHBIN_USER1); } public function tearDown() { - $this->rootView->deleteAll($this->trashRoot); + $this->rootView->deleteAll($this->trashRoot1); + $this->rootView->deleteAll($this->trashRoot2); } /** @@ -88,10 +121,11 @@ class Test_Trashbin extends \PHPUnit_Framework_TestCase { \OC\Files\Filesystem::unlink('file3.txt'); //make sure that files are in the trash bin - $filesInTrash = OCA\Files_Trashbin\Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1); + $filesInTrash = OCA\Files_Trashbin\Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1, 'name'); $this->assertSame(3, count($filesInTrash)); - $manipulatedList = $this->manipulateDeleteTime($filesInTrash, $expiredDate); + // every second file will get a date in the past so that it will get expired + $manipulatedList = $this->manipulateDeleteTime($filesInTrash, $this->trashRoot1, $expiredDate); $testClass = new TrashbinForTesting(); list($sizeOfDeletedFiles, $count) = $testClass->dummyDeleteExpiredFiles($manipulatedList, $expireAt); @@ -112,14 +146,103 @@ class Test_Trashbin extends \PHPUnit_Framework_TestCase { $this->assertSame('file2.txt', $element['name']); } - private function manipulateDeleteTime($files, $expireDate) { + /** + * test expiration of files older then the max storage time defined for the trash + * in this test we delete a shared file and check if both trash bins, the one from + * the owner of the file and the one from the user who deleted the file get expired + * correctly + */ + public function testExpireOldFilesShared() { + + $currentTime = time(); + $folder = "trashTest-" . $currentTime . '/'; + $expiredDate = $currentTime - 3*24*60*60; + + // create some files + \OC\Files\Filesystem::mkdir($folder); + \OC\Files\Filesystem::file_put_contents($folder . 'user1-1.txt', 'file1'); + \OC\Files\Filesystem::file_put_contents($folder . 'user1-2.txt', 'file2'); + \OC\Files\Filesystem::file_put_contents($folder . 'user1-3.txt', 'file3'); + \OC\Files\Filesystem::file_put_contents($folder . 'user1-4.txt', 'file4'); + + //share user1-4.txt with user2 + $fileInfo = \OC\Files\Filesystem::getFileInfo($folder); + $result = \OCP\Share::shareItem('folder', $fileInfo->getId(), \OCP\Share::SHARE_TYPE_USER, self::TEST_TRASHBIN_USER2, 31); + $this->assertTrue($result); + + // delete them so that they end up in the trash bin + \OC\Files\Filesystem::unlink($folder . 'user1-1.txt'); + \OC\Files\Filesystem::unlink($folder . 'user1-2.txt'); + \OC\Files\Filesystem::unlink($folder . 'user1-3.txt'); + + $filesInTrash = OCA\Files_Trashbin\Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1, 'name'); + $this->assertSame(3, count($filesInTrash)); + + // every second file will get a date in the past so that it will get expired + $this->manipulateDeleteTime($filesInTrash, $this->trashRoot1, $expiredDate); + + // login as user2 + self::loginHelper(self::TEST_TRASHBIN_USER2); + + $this->assertTrue(\OC\Files\Filesystem::file_exists($folder . "user1-4.txt")); + + // create some files + \OC\Files\Filesystem::file_put_contents('user2-1.txt', 'file1'); + \OC\Files\Filesystem::file_put_contents('user2-2.txt', 'file2'); + + // delete them so that they end up in the trash bin + \OC\Files\Filesystem::unlink('user2-1.txt'); + \OC\Files\Filesystem::unlink('user2-2.txt'); + + $filesInTrashUser2 = OCA\Files_Trashbin\Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER2, 'name'); + $this->assertSame(2, count($filesInTrashUser2)); + + // every second file will get a date in the past so that it will get expired + $this->manipulateDeleteTime($filesInTrashUser2, $this->trashRoot2, $expiredDate); + + \OC\Files\Filesystem::unlink($folder . 'user1-4.txt'); + + $filesInTrashUser2AfterDelete = OCA\Files_Trashbin\Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER2); + + // user2-1.txt should have been expired + $this->verifyArray($filesInTrashUser2AfterDelete, array('user2-2.txt', 'user1-4.txt')); + + // user1-1.txt and user1-3.txt should have been expired + $filesInTrashUser1AfterDelete = OCA\Files_Trashbin\Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1); + + $this->verifyArray($filesInTrashUser1AfterDelete, array('user1-2.txt', 'user1-4.txt')); + } + + /** + * verify that the array contains the expected results + * @param array $result + * @param array $expected + */ + private function verifyArray($result, $expected) { + $this->assertSame(count($expected), count($result)); + foreach ($expected as $expectedFile) { + $found = false; + foreach ($result as $fileInTrash) { + if ($expectedFile === $fileInTrash['name']) { + $found = true; + break; + } + } + if (!$found) { + // if we didn't found the expected file, something went wrong + $this->assertTrue(false, "can't find expected file '" . $expectedFile . "' in trash bin"); + } + } + } + + private function manipulateDeleteTime($files, $trashRoot, $expireDate) { $counter = 0; foreach ($files as &$file) { // modify every second file $counter = ($counter + 1) % 2; if ($counter === 1) { - $source = $this->trashRoot . '/files/' . $file['name'].'.d'.$file['mtime']; - $target = \OC\Files\Filesystem::normalizePath($this->trashRoot . '/files/' . $file['name'] . '.d' . $expireDate); + $source = $trashRoot . '/files/' . $file['name'].'.d'.$file['mtime']; + $target = \OC\Files\Filesystem::normalizePath($trashRoot . '/files/' . $file['name'] . '.d' . $expireDate); $this->rootView->rename($source, $target); $file['mtime'] = $expireDate; } -- 2.39.5