diff options
author | Thomas Müller <thomas.mueller@tmit.eu> | 2015-08-12 13:13:45 +0200 |
---|---|---|
committer | Thomas Müller <thomas.mueller@tmit.eu> | 2015-08-12 13:13:45 +0200 |
commit | 326de6f9b4473f68e4c0237f0b6fb43111cda0c5 (patch) | |
tree | 1a81bd91892227ac071385c80bda86aea02c8530 /apps/files_trashbin/tests | |
parent | abd3d5c6a5803827e54f769e5677cdfa7ea4350b (diff) | |
parent | b9f655d8ada528086eb623705adb683fcb951ebe (diff) | |
download | nextcloud-server-326de6f9b4473f68e4c0237f0b6fb43111cda0c5.tar.gz nextcloud-server-326de6f9b4473f68e4c0237f0b6fb43111cda0c5.zip |
Merge pull request #18065 from owncloud/new-trashbin-retention
New trashbin retention
Diffstat (limited to 'apps/files_trashbin/tests')
-rw-r--r-- | apps/files_trashbin/tests/expiration.php | 200 | ||||
-rw-r--r-- | apps/files_trashbin/tests/trashbin.php | 15 |
2 files changed, 202 insertions, 13 deletions
diff --git a/apps/files_trashbin/tests/expiration.php b/apps/files_trashbin/tests/expiration.php new file mode 100644 index 00000000000..7bd51dccddd --- /dev/null +++ b/apps/files_trashbin/tests/expiration.php @@ -0,0 +1,200 @@ +<?php +/** + * @author Victor Dubiniuk <dubiniuk@owncloud.com> + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +use \OCA\Files_Trashbin\Expiration; + +class Expiration_Test extends \PHPUnit_Framework_TestCase { + const SECONDS_PER_DAY = 86400; //60*60*24 + + public function expirationData(){ + $today = 100*self::SECONDS_PER_DAY; + $back10Days = (100-10)*self::SECONDS_PER_DAY; + $back20Days = (100-20)*self::SECONDS_PER_DAY; + $back30Days = (100-30)*self::SECONDS_PER_DAY; + $back35Days = (100-35)*self::SECONDS_PER_DAY; + + // it should never happen, but who knows :/ + $ahead100Days = (100+100)*self::SECONDS_PER_DAY; + + return [ + // Expiration is disabled - always should return false + [ 'disabled', $today, $back10Days, false, false], + [ 'disabled', $today, $back10Days, true, false], + [ 'disabled', $today, $ahead100Days, true, false], + + // Default: expire in 30 days or earlier when quota requirements are met + [ 'auto', $today, $back10Days, false, false], + [ 'auto', $today, $back35Days, false, false], + [ 'auto', $today, $back10Days, true, true], + [ 'auto', $today, $back35Days, true, true], + [ 'auto', $today, $ahead100Days, true, true], + + // The same with 'auto' + [ 'auto, auto', $today, $back10Days, false, false], + [ 'auto, auto', $today, $back35Days, false, false], + [ 'auto, auto', $today, $back10Days, true, true], + [ 'auto, auto', $today, $back35Days, true, true], + + // Keep for 15 days but expire anytime if space needed + [ '15, auto', $today, $back10Days, false, false], + [ '15, auto', $today, $back20Days, false, false], + [ '15, auto', $today, $back10Days, true, true], + [ '15, auto', $today, $back20Days, true, true], + [ '15, auto', $today, $ahead100Days, true, true], + + // Expire anytime if space needed, Expire all older than max + [ 'auto, 15', $today, $back10Days, false, false], + [ 'auto, 15', $today, $back20Days, false, true], + [ 'auto, 15', $today, $back10Days, true, true], + [ 'auto, 15', $today, $back20Days, true, true], + [ 'auto, 15', $today, $ahead100Days, true, true], + + // Expire all older than max OR older than min if space needed + [ '15, 25', $today, $back10Days, false, false], + [ '15, 25', $today, $back20Days, false, false], + [ '15, 25', $today, $back30Days, false, true], + [ '15, 25', $today, $back10Days, false, false], + [ '15, 25', $today, $back20Days, true, true], + [ '15, 25', $today, $back30Days, true, true], + [ '15, 25', $today, $ahead100Days, true, false], + + // Expire all older than max OR older than min if space needed + // Max<Min case + [ '25, 15', $today, $back10Days, false, false], + [ '25, 15', $today, $back20Days, false, false], + [ '25, 15', $today, $back30Days, false, true], + [ '25, 15', $today, $back10Days, false, false], + [ '25, 15', $today, $back20Days, true, false], + [ '25, 15', $today, $back30Days, true, true], + [ '25, 15', $today, $ahead100Days, true, false], + ]; + } + + /** + * @dataProvider expirationData + * + * @param string $retentionObligation + * @param int $timeNow + * @param int $timestamp + * @param bool $quotaExceeded + * @param string $expectedResult + */ + public function testExpiration($retentionObligation, $timeNow, $timestamp, $quotaExceeded, $expectedResult){ + $mockedConfig = $this->getMockedConfig($retentionObligation); + $mockedTimeFactory = $this->getMockedTimeFactory($timeNow); + + $expiration = new Expiration($mockedConfig, $mockedTimeFactory); + $actualResult = $expiration->isExpired($timestamp, $quotaExceeded); + + $this->assertEquals($expectedResult, $actualResult); + } + + + public function configData(){ + return [ + [ 'disabled', null, null, null], + [ 'auto', Expiration::DEFAULT_RETENTION_OBLIGATION, Expiration::NO_OBLIGATION, true ], + [ 'auto,auto', Expiration::DEFAULT_RETENTION_OBLIGATION, Expiration::NO_OBLIGATION, true ], + [ 'auto, auto', Expiration::DEFAULT_RETENTION_OBLIGATION, Expiration::NO_OBLIGATION, true ], + [ 'auto, 3', Expiration::NO_OBLIGATION, 3, true ], + [ '5, auto', 5, Expiration::NO_OBLIGATION, true ], + [ '3, 5', 3, 5, false ], + [ '10, 3', 10, 10, false ], + ]; + } + + + /** + * @dataProvider configData + * + * @param string $configValue + * @param int $expectedMinAge + * @param int $expectedMaxAge + * @param bool $expectedCanPurgeToSaveSpace + */ + public function testParseRetentionObligation($configValue, $expectedMinAge, $expectedMaxAge, $expectedCanPurgeToSaveSpace){ + $mockedConfig = $this->getMockedConfig($configValue); + $mockedTimeFactory = $this->getMockedTimeFactory( + time() + ); + + $expiration = new Expiration($mockedConfig, $mockedTimeFactory); + $this->assertAttributeEquals($expectedMinAge, 'minAge', $expiration); + $this->assertAttributeEquals($expectedMaxAge, 'maxAge', $expiration); + $this->assertAttributeEquals($expectedCanPurgeToSaveSpace, 'canPurgeToSaveSpace', $expiration); + } + + /** + * + * @param int $time + * @return \OCP\AppFramework\Utility\ITimeFactory + */ + private function getMockedTimeFactory($time){ + $mockedTimeFactory = $this->getMockBuilder('\OCP\AppFramework\Utility\ITimeFactory') + ->disableOriginalConstructor() + ->setMethods(['getTime']) + ->getMock() + ; + $mockedTimeFactory->expects($this->any())->method('getTime')->will( + $this->returnValue($time) + ); + + return $mockedTimeFactory; + } + + /** + * + * @param string $returnValue + * @return \OCP\IConfig + */ + private function getMockedConfig($returnValue){ + $mockedConfig = $this->getMockBuilder('\OCP\IConfig') + ->disableOriginalConstructor() + ->setMethods( + [ + 'setSystemValues', + 'setSystemValue', + 'getSystemValue', + 'deleteSystemValue', + 'getAppKeys', + 'setAppValue', + 'getAppValue', + 'deleteAppValue', + 'deleteAppValues', + 'setUserValue', + 'getUserValue', + 'getUserValueForUsers', + 'getUserKeys', + 'deleteUserValue', + 'deleteAllUserValues', + 'deleteAppFromAllUsers', + 'getUsersForUserValue' + ] + ) + ->getMock() + ; + $mockedConfig->expects($this->any())->method('getSystemValue')->will( + $this->returnValue($returnValue) + ); + + return $mockedConfig; + } +} diff --git a/apps/files_trashbin/tests/trashbin.php b/apps/files_trashbin/tests/trashbin.php index 299c45b19a7..f60367a77e2 100644 --- a/apps/files_trashbin/tests/trashbin.php +++ b/apps/files_trashbin/tests/trashbin.php @@ -38,7 +38,6 @@ class Test_Trashbin extends \Test\TestCase { private $trashRoot2; private static $rememberRetentionObligation; - private static $rememberAutoExpire; /** * @var bool @@ -71,11 +70,8 @@ class Test_Trashbin extends \Test\TestCase { \OC_App::disable('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); - + self::$rememberRetentionObligation = \OC_Config::getValue('trashbin_retention_obligation', Files_Trashbin\Expiration::DEFAULT_RETENTION_OBLIGATION); + \OC_Config::setValue('trashbin_retention_obligation', 'auto, 2'); // register hooks Files_Trashbin\Trashbin::registerHooks(); @@ -92,7 +88,6 @@ class Test_Trashbin extends \Test\TestCase { \OC_User::deleteUser(self::TEST_TRASHBIN_USER1); \OC_Config::setValue('trashbin_retention_obligation', self::$rememberRetentionObligation); - \OC_Config::setValue('trashbin_auto_expire', self::$rememberAutoExpire); \OC_Hook::clear(); @@ -636,12 +631,6 @@ class Test_Trashbin extends \Test\TestCase { } } - $storage = new \ReflectionClass('\OC\Files\Storage\Shared'); - $isInitialized = $storage->getProperty('isInitialized'); - $isInitialized->setAccessible(true); - $isInitialized->setValue(array()); - $isInitialized->setAccessible(false); - \OC_Util::tearDownFS(); \OC_User::setUserId(''); \OC\Files\Filesystem::tearDown(); |