versions.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. <?php
  2. /**
  3. * @author Björn Schießle <schiessle@owncloud.com>
  4. * @author Joas Schilling <nickvergessen@owncloud.com>
  5. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Thomas Müller <thomas.mueller@tmit.eu>
  8. *
  9. * @copyright Copyright (c) 2015, ownCloud, Inc.
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. require_once __DIR__ . '/../appinfo/app.php';
  26. /**
  27. * Class Test_Files_versions
  28. * this class provide basic files versions test
  29. */
  30. class Test_Files_Versioning extends \Test\TestCase {
  31. const TEST_VERSIONS_USER = 'test-versions-user';
  32. const TEST_VERSIONS_USER2 = 'test-versions-user2';
  33. const USERS_VERSIONS_ROOT = '/test-versions-user/files_versions';
  34. /**
  35. * @var \OC\Files\View
  36. */
  37. private $rootView;
  38. public static function setUpBeforeClass() {
  39. parent::setUpBeforeClass();
  40. // clear share hooks
  41. \OC_Hook::clear('OCP\\Share');
  42. \OC::registerShareHooks();
  43. \OCA\Files_Versions\Hooks::connectHooks();
  44. \OCP\Util::connectHook('OC_Filesystem', 'setup', '\OC\Files\Storage\Shared', 'setup');
  45. // create test user
  46. self::loginHelper(self::TEST_VERSIONS_USER2, true);
  47. self::loginHelper(self::TEST_VERSIONS_USER, true);
  48. }
  49. public static function tearDownAfterClass() {
  50. // cleanup test user
  51. \OC_User::deleteUser(self::TEST_VERSIONS_USER);
  52. \OC_User::deleteUser(self::TEST_VERSIONS_USER2);
  53. parent::tearDownAfterClass();
  54. }
  55. protected function setUp() {
  56. parent::setUp();
  57. self::loginHelper(self::TEST_VERSIONS_USER);
  58. $this->rootView = new \OC\Files\View();
  59. if (!$this->rootView->file_exists(self::USERS_VERSIONS_ROOT)) {
  60. $this->rootView->mkdir(self::USERS_VERSIONS_ROOT);
  61. }
  62. }
  63. protected function tearDown() {
  64. $this->rootView->deleteAll(self::USERS_VERSIONS_ROOT);
  65. parent::tearDown();
  66. }
  67. /**
  68. * @medium
  69. * test expire logic
  70. * @dataProvider versionsProvider
  71. */
  72. public function testGetExpireList($versions, $sizeOfAllDeletedFiles) {
  73. // last interval end at 2592000
  74. $startTime = 5000000;
  75. $testClass = new VersionStorageToTest();
  76. list($deleted, $size) = $testClass->callProtectedGetExpireList($startTime, $versions);
  77. // we should have deleted 16 files each of the size 1
  78. $this->assertEquals($sizeOfAllDeletedFiles, $size);
  79. // the deleted array should only contain versions which should be deleted
  80. foreach($deleted as $key => $path) {
  81. unset($versions[$key]);
  82. $this->assertEquals("delete", substr($path, 0, strlen("delete")));
  83. }
  84. // the versions array should only contain versions which should be kept
  85. foreach ($versions as $version) {
  86. $this->assertEquals("keep", $version['path']);
  87. }
  88. }
  89. public function versionsProvider() {
  90. return array(
  91. // first set of versions uniformly distributed versions
  92. array(
  93. array(
  94. // first slice (10sec) keep one version every 2 seconds
  95. array("version" => 4999999, "path" => "keep", "size" => 1),
  96. array("version" => 4999998, "path" => "delete", "size" => 1),
  97. array("version" => 4999997, "path" => "keep", "size" => 1),
  98. array("version" => 4999995, "path" => "keep", "size" => 1),
  99. array("version" => 4999994, "path" => "delete", "size" => 1),
  100. //next slice (60sec) starts at 4999990 keep one version every 10 secons
  101. array("version" => 4999988, "path" => "keep", "size" => 1),
  102. array("version" => 4999978, "path" => "keep", "size" => 1),
  103. array("version" => 4999975, "path" => "delete", "size" => 1),
  104. array("version" => 4999972, "path" => "delete", "size" => 1),
  105. array("version" => 4999967, "path" => "keep", "size" => 1),
  106. array("version" => 4999958, "path" => "delete", "size" => 1),
  107. array("version" => 4999957, "path" => "keep", "size" => 1),
  108. //next slice (3600sec) start at 4999940 keep one version every 60 seconds
  109. array("version" => 4999900, "path" => "keep", "size" => 1),
  110. array("version" => 4999841, "path" => "delete", "size" => 1),
  111. array("version" => 4999840, "path" => "keep", "size" => 1),
  112. array("version" => 4999780, "path" => "keep", "size" => 1),
  113. array("version" => 4996401, "path" => "keep", "size" => 1),
  114. // next slice (86400sec) start at 4996400 keep one version every 3600 seconds
  115. array("version" => 4996350, "path" => "delete", "size" => 1),
  116. array("version" => 4992800, "path" => "keep", "size" => 1),
  117. array("version" => 4989800, "path" => "delete", "size" => 1),
  118. array("version" => 4989700, "path" => "delete", "size" => 1),
  119. array("version" => 4989200, "path" => "keep", "size" => 1),
  120. // next slice (2592000sec) start at 4913600 keep one version every 86400 seconds
  121. array("version" => 4913600, "path" => "keep", "size" => 1),
  122. array("version" => 4852800, "path" => "delete", "size" => 1),
  123. array("version" => 4827201, "path" => "delete", "size" => 1),
  124. array("version" => 4827200, "path" => "keep", "size" => 1),
  125. array("version" => 4777201, "path" => "delete", "size" => 1),
  126. array("version" => 4777501, "path" => "delete", "size" => 1),
  127. array("version" => 4740000, "path" => "keep", "size" => 1),
  128. // final slice starts at 2408000 keep one version every 604800 secons
  129. array("version" => 2408000, "path" => "keep", "size" => 1),
  130. array("version" => 1803201, "path" => "delete", "size" => 1),
  131. array("version" => 1803200, "path" => "keep", "size" => 1),
  132. array("version" => 1800199, "path" => "delete", "size" => 1),
  133. array("version" => 1800100, "path" => "delete", "size" => 1),
  134. array("version" => 1198300, "path" => "keep", "size" => 1),
  135. ),
  136. 16 // size of all deleted files (every file has the size 1)
  137. ),
  138. // second set of versions, here we have only really old versions
  139. array(
  140. array(
  141. // first slice (10sec) keep one version every 2 seconds
  142. // next slice (60sec) starts at 4999990 keep one version every 10 secons
  143. // next slice (3600sec) start at 4999940 keep one version every 60 seconds
  144. // next slice (86400sec) start at 4996400 keep one version every 3600 seconds
  145. array("version" => 4996400, "path" => "keep", "size" => 1),
  146. array("version" => 4996350, "path" => "delete", "size" => 1),
  147. array("version" => 4996350, "path" => "delete", "size" => 1),
  148. array("version" => 4992800, "path" => "keep", "size" => 1),
  149. array("version" => 4989800, "path" => "delete", "size" => 1),
  150. array("version" => 4989700, "path" => "delete", "size" => 1),
  151. array("version" => 4989200, "path" => "keep", "size" => 1),
  152. // next slice (2592000sec) start at 4913600 keep one version every 86400 seconds
  153. array("version" => 4913600, "path" => "keep", "size" => 1),
  154. array("version" => 4852800, "path" => "delete", "size" => 1),
  155. array("version" => 4827201, "path" => "delete", "size" => 1),
  156. array("version" => 4827200, "path" => "keep", "size" => 1),
  157. array("version" => 4777201, "path" => "delete", "size" => 1),
  158. array("version" => 4777501, "path" => "delete", "size" => 1),
  159. array("version" => 4740000, "path" => "keep", "size" => 1),
  160. // final slice starts at 2408000 keep one version every 604800 secons
  161. array("version" => 2408000, "path" => "keep", "size" => 1),
  162. array("version" => 1803201, "path" => "delete", "size" => 1),
  163. array("version" => 1803200, "path" => "keep", "size" => 1),
  164. array("version" => 1800199, "path" => "delete", "size" => 1),
  165. array("version" => 1800100, "path" => "delete", "size" => 1),
  166. array("version" => 1198300, "path" => "keep", "size" => 1),
  167. ),
  168. 11 // size of all deleted files (every file has the size 1)
  169. ),
  170. // third set of versions, with some gaps inbetween
  171. array(
  172. array(
  173. // first slice (10sec) keep one version every 2 seconds
  174. array("version" => 4999999, "path" => "keep", "size" => 1),
  175. array("version" => 4999998, "path" => "delete", "size" => 1),
  176. array("version" => 4999997, "path" => "keep", "size" => 1),
  177. array("version" => 4999995, "path" => "keep", "size" => 1),
  178. array("version" => 4999994, "path" => "delete", "size" => 1),
  179. //next slice (60sec) starts at 4999990 keep one version every 10 secons
  180. array("version" => 4999988, "path" => "keep", "size" => 1),
  181. array("version" => 4999978, "path" => "keep", "size" => 1),
  182. //next slice (3600sec) start at 4999940 keep one version every 60 seconds
  183. // next slice (86400sec) start at 4996400 keep one version every 3600 seconds
  184. array("version" => 4989200, "path" => "keep", "size" => 1),
  185. // next slice (2592000sec) start at 4913600 keep one version every 86400 seconds
  186. array("version" => 4913600, "path" => "keep", "size" => 1),
  187. array("version" => 4852800, "path" => "delete", "size" => 1),
  188. array("version" => 4827201, "path" => "delete", "size" => 1),
  189. array("version" => 4827200, "path" => "keep", "size" => 1),
  190. array("version" => 4777201, "path" => "delete", "size" => 1),
  191. array("version" => 4777501, "path" => "delete", "size" => 1),
  192. array("version" => 4740000, "path" => "keep", "size" => 1),
  193. // final slice starts at 2408000 keep one version every 604800 secons
  194. array("version" => 2408000, "path" => "keep", "size" => 1),
  195. array("version" => 1803201, "path" => "delete", "size" => 1),
  196. array("version" => 1803200, "path" => "keep", "size" => 1),
  197. array("version" => 1800199, "path" => "delete", "size" => 1),
  198. array("version" => 1800100, "path" => "delete", "size" => 1),
  199. array("version" => 1198300, "path" => "keep", "size" => 1),
  200. ),
  201. 9 // size of all deleted files (every file has the size 1)
  202. ),
  203. );
  204. }
  205. public function testRename() {
  206. \OC\Files\Filesystem::file_put_contents("test.txt", "test file");
  207. $t1 = time();
  208. // second version is two weeks older, this way we make sure that no
  209. // version will be expired
  210. $t2 = $t1 - 60 * 60 * 24 * 14;
  211. // create some versions
  212. $v1 = self::USERS_VERSIONS_ROOT . '/test.txt.v' . $t1;
  213. $v2 = self::USERS_VERSIONS_ROOT . '/test.txt.v' . $t2;
  214. $v1Renamed = self::USERS_VERSIONS_ROOT . '/test2.txt.v' . $t1;
  215. $v2Renamed = self::USERS_VERSIONS_ROOT . '/test2.txt.v' . $t2;
  216. $this->rootView->file_put_contents($v1, 'version1');
  217. $this->rootView->file_put_contents($v2, 'version2');
  218. // execute rename hook of versions app
  219. \OC\Files\Filesystem::rename("test.txt", "test2.txt");
  220. $this->runCommands();
  221. $this->assertFalse($this->rootView->file_exists($v1));
  222. $this->assertFalse($this->rootView->file_exists($v2));
  223. $this->assertTrue($this->rootView->file_exists($v1Renamed));
  224. $this->assertTrue($this->rootView->file_exists($v2Renamed));
  225. //cleanup
  226. \OC\Files\Filesystem::unlink('test2.txt');
  227. }
  228. public function testRenameInSharedFolder() {
  229. \OC\Files\Filesystem::mkdir('folder1');
  230. \OC\Files\Filesystem::mkdir('folder1/folder2');
  231. \OC\Files\Filesystem::file_put_contents("folder1/test.txt", "test file");
  232. $fileInfo = \OC\Files\Filesystem::getFileInfo('folder1');
  233. $t1 = time();
  234. // second version is two weeks older, this way we make sure that no
  235. // version will be expired
  236. $t2 = $t1 - 60 * 60 * 24 * 14;
  237. $this->rootView->mkdir(self::USERS_VERSIONS_ROOT . '/folder1');
  238. // create some versions
  239. $v1 = self::USERS_VERSIONS_ROOT . '/folder1/test.txt.v' . $t1;
  240. $v2 = self::USERS_VERSIONS_ROOT . '/folder1/test.txt.v' . $t2;
  241. $v1Renamed = self::USERS_VERSIONS_ROOT . '/folder1/folder2/test.txt.v' . $t1;
  242. $v2Renamed = self::USERS_VERSIONS_ROOT . '/folder1/folder2/test.txt.v' . $t2;
  243. $this->rootView->file_put_contents($v1, 'version1');
  244. $this->rootView->file_put_contents($v2, 'version2');
  245. \OCP\Share::shareItem('folder', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER, self::TEST_VERSIONS_USER2, \OCP\Constants::PERMISSION_ALL);
  246. self::loginHelper(self::TEST_VERSIONS_USER2);
  247. $this->assertTrue(\OC\Files\Filesystem::file_exists('folder1/test.txt'));
  248. // execute rename hook of versions app
  249. \OC\Files\Filesystem::rename('/folder1/test.txt', '/folder1/folder2/test.txt');
  250. self::loginHelper(self::TEST_VERSIONS_USER2);
  251. $this->runCommands();
  252. $this->assertFalse($this->rootView->file_exists($v1));
  253. $this->assertFalse($this->rootView->file_exists($v2));
  254. $this->assertTrue($this->rootView->file_exists($v1Renamed));
  255. $this->assertTrue($this->rootView->file_exists($v2Renamed));
  256. //cleanup
  257. \OC\Files\Filesystem::unlink('/folder1/folder2/test.txt');
  258. }
  259. public function testRenameSharedFile() {
  260. \OC\Files\Filesystem::file_put_contents("test.txt", "test file");
  261. $fileInfo = \OC\Files\Filesystem::getFileInfo('test.txt');
  262. $t1 = time();
  263. // second version is two weeks older, this way we make sure that no
  264. // version will be expired
  265. $t2 = $t1 - 60 * 60 * 24 * 14;
  266. $this->rootView->mkdir(self::USERS_VERSIONS_ROOT);
  267. // create some versions
  268. $v1 = self::USERS_VERSIONS_ROOT . '/test.txt.v' . $t1;
  269. $v2 = self::USERS_VERSIONS_ROOT . '/test.txt.v' . $t2;
  270. // the renamed versions should not exist! Because we only moved the mount point!
  271. $v1Renamed = self::USERS_VERSIONS_ROOT . '/test2.txt.v' . $t1;
  272. $v2Renamed = self::USERS_VERSIONS_ROOT . '/test2.txt.v' . $t2;
  273. $this->rootView->file_put_contents($v1, 'version1');
  274. $this->rootView->file_put_contents($v2, 'version2');
  275. \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER, self::TEST_VERSIONS_USER2, \OCP\Constants::PERMISSION_ALL);
  276. self::loginHelper(self::TEST_VERSIONS_USER2);
  277. $this->assertTrue(\OC\Files\Filesystem::file_exists('test.txt'));
  278. // execute rename hook of versions app
  279. \OC\Files\Filesystem::rename('test.txt', 'test2.txt');
  280. self::loginHelper(self::TEST_VERSIONS_USER);
  281. $this->runCommands();
  282. $this->assertTrue($this->rootView->file_exists($v1));
  283. $this->assertTrue($this->rootView->file_exists($v2));
  284. $this->assertFalse($this->rootView->file_exists($v1Renamed));
  285. $this->assertFalse($this->rootView->file_exists($v2Renamed));
  286. //cleanup
  287. \OC\Files\Filesystem::unlink('/test.txt');
  288. }
  289. public function testCopy() {
  290. \OC\Files\Filesystem::file_put_contents("test.txt", "test file");
  291. $t1 = time();
  292. // second version is two weeks older, this way we make sure that no
  293. // version will be expired
  294. $t2 = $t1 - 60 * 60 * 24 * 14;
  295. // create some versions
  296. $v1 = self::USERS_VERSIONS_ROOT . '/test.txt.v' . $t1;
  297. $v2 = self::USERS_VERSIONS_ROOT . '/test.txt.v' . $t2;
  298. $v1Copied = self::USERS_VERSIONS_ROOT . '/test2.txt.v' . $t1;
  299. $v2Copied = self::USERS_VERSIONS_ROOT . '/test2.txt.v' . $t2;
  300. $this->rootView->file_put_contents($v1, 'version1');
  301. $this->rootView->file_put_contents($v2, 'version2');
  302. // execute copy hook of versions app
  303. \OC\Files\Filesystem::copy("test.txt", "test2.txt");
  304. $this->runCommands();
  305. $this->assertTrue($this->rootView->file_exists($v1));
  306. $this->assertTrue($this->rootView->file_exists($v2));
  307. $this->assertTrue($this->rootView->file_exists($v1Copied));
  308. $this->assertTrue($this->rootView->file_exists($v2Copied));
  309. //cleanup
  310. \OC\Files\Filesystem::unlink('test.txt');
  311. \OC\Files\Filesystem::unlink('test2.txt');
  312. }
  313. /**
  314. * test if we find all versions and if the versions array contain
  315. * the correct 'path' and 'name'
  316. */
  317. public function testGetVersions() {
  318. $t1 = time();
  319. // second version is two weeks older, this way we make sure that no
  320. // version will be expired
  321. $t2 = $t1 - 60 * 60 * 24 * 14;
  322. // create some versions
  323. $v1 = self::USERS_VERSIONS_ROOT . '/subfolder/test.txt.v' . $t1;
  324. $v2 = self::USERS_VERSIONS_ROOT . '/subfolder/test.txt.v' . $t2;
  325. $this->rootView->mkdir(self::USERS_VERSIONS_ROOT . '/subfolder/');
  326. $this->rootView->file_put_contents($v1, 'version1');
  327. $this->rootView->file_put_contents($v2, 'version2');
  328. // execute copy hook of versions app
  329. $versions = \OCA\Files_Versions\Storage::getVersions(self::TEST_VERSIONS_USER, '/subfolder/test.txt');
  330. $this->assertSame(2, count($versions));
  331. foreach ($versions as $version) {
  332. $this->assertSame('/subfolder/test.txt', $version['path']);
  333. $this->assertSame('test.txt', $version['name']);
  334. }
  335. //cleanup
  336. $this->rootView->deleteAll(self::USERS_VERSIONS_ROOT . '/subfolder');
  337. }
  338. /**
  339. * @param string $user
  340. * @param bool $create
  341. * @param bool $password
  342. */
  343. public static function loginHelper($user, $create = false) {
  344. if ($create) {
  345. $backend = new \OC_User_Dummy();
  346. $backend->createUser($user, $user);
  347. \OC::$server->getUserManager()->registerBackend($backend);
  348. }
  349. $storage = new \ReflectionClass('\OC\Files\Storage\Shared');
  350. $isInitialized = $storage->getProperty('isInitialized');
  351. $isInitialized->setAccessible(true);
  352. $isInitialized->setValue(array());
  353. $isInitialized->setAccessible(false);
  354. \OC_Util::tearDownFS();
  355. \OC_User::setUserId('');
  356. \OC\Files\Filesystem::tearDown();
  357. \OC_User::setUserId($user);
  358. \OC_Util::setupFS($user);
  359. }
  360. }
  361. // extend the original class to make it possible to test protected methods
  362. class VersionStorageToTest extends \OCA\Files_Versions\Storage {
  363. /**
  364. * @param integer $time
  365. */
  366. public function callProtectedGetExpireList($time, $versions) {
  367. return self::getExpireList($time, $versions);
  368. }
  369. }