VersioningTest.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_Versions\Tests;
  8. use OC\AllConfig;
  9. use OC\Files\Cache\Watcher;
  10. use OC\Files\Filesystem;
  11. use OC\Files\Storage\Temporary;
  12. use OC\Files\View;
  13. use OC\User\NoUserException;
  14. use OCA\Files_Sharing\AppInfo\Application;
  15. use OCA\Files_Versions\Db\VersionEntity;
  16. use OCA\Files_Versions\Db\VersionsMapper;
  17. use OCA\Files_Versions\Storage;
  18. use OCA\Files_Versions\Versions\IVersionManager;
  19. use OCP\Constants;
  20. use OCP\Files\IMimeTypeLoader;
  21. use OCP\IConfig;
  22. use OCP\IUser;
  23. use OCP\Server;
  24. use OCP\Share\IShare;
  25. use OCP\Util;
  26. /**
  27. * Class Test_Files_versions
  28. * this class provide basic files versions test
  29. *
  30. * @group DB
  31. */
  32. class VersioningTest extends \Test\TestCase {
  33. public const TEST_VERSIONS_USER = 'test-versions-user';
  34. public const TEST_VERSIONS_USER2 = 'test-versions-user2';
  35. public const USERS_VERSIONS_ROOT = '/test-versions-user/files_versions';
  36. /**
  37. * @var View
  38. */
  39. private $rootView;
  40. /**
  41. * @var VersionsMapper
  42. */
  43. private $versionsMapper;
  44. /**
  45. * @var IMimeTypeLoader
  46. */
  47. private $mimeTypeLoader;
  48. private $user1;
  49. private $user2;
  50. public static function setUpBeforeClass(): void {
  51. parent::setUpBeforeClass();
  52. $application = new Application();
  53. // create test user
  54. self::loginHelper(self::TEST_VERSIONS_USER2, true);
  55. self::loginHelper(self::TEST_VERSIONS_USER, true);
  56. }
  57. public static function tearDownAfterClass(): void {
  58. // cleanup test user
  59. $user = \OC::$server->getUserManager()->get(self::TEST_VERSIONS_USER);
  60. if ($user !== null) {
  61. $user->delete();
  62. }
  63. $user = \OC::$server->getUserManager()->get(self::TEST_VERSIONS_USER2);
  64. if ($user !== null) {
  65. $user->delete();
  66. }
  67. parent::tearDownAfterClass();
  68. }
  69. protected function setUp(): void {
  70. parent::setUp();
  71. $config = \OC::$server->getConfig();
  72. $mockConfig = $this->createMock(IConfig::class);
  73. $mockConfig->expects($this->any())
  74. ->method('getSystemValue')
  75. ->willReturnCallback(function ($key, $default) use ($config) {
  76. if ($key === 'filesystem_check_changes') {
  77. return Watcher::CHECK_ONCE;
  78. } else {
  79. return $config->getSystemValue($key, $default);
  80. }
  81. });
  82. $this->overwriteService(AllConfig::class, $mockConfig);
  83. // clear hooks
  84. \OC_Hook::clear();
  85. \OC::registerShareHooks(\OC::$server->getSystemConfig());
  86. \OC::$server->boot();
  87. self::loginHelper(self::TEST_VERSIONS_USER);
  88. $this->rootView = new View();
  89. if (!$this->rootView->file_exists(self::USERS_VERSIONS_ROOT)) {
  90. $this->rootView->mkdir(self::USERS_VERSIONS_ROOT);
  91. }
  92. $this->versionsMapper = Server::get(VersionsMapper::class);
  93. $this->mimeTypeLoader = Server::get(IMimeTypeLoader::class);
  94. $this->user1 = $this->createMock(IUser::class);
  95. $this->user1->method('getUID')
  96. ->willReturn(self::TEST_VERSIONS_USER);
  97. $this->user2 = $this->createMock(IUser::class);
  98. $this->user2->method('getUID')
  99. ->willReturn(self::TEST_VERSIONS_USER2);
  100. }
  101. protected function tearDown(): void {
  102. $this->restoreService(AllConfig::class);
  103. if ($this->rootView) {
  104. $this->rootView->deleteAll(self::TEST_VERSIONS_USER . '/files/');
  105. $this->rootView->deleteAll(self::TEST_VERSIONS_USER2 . '/files/');
  106. $this->rootView->deleteAll(self::TEST_VERSIONS_USER . '/files_versions/');
  107. $this->rootView->deleteAll(self::TEST_VERSIONS_USER2 . '/files_versions/');
  108. }
  109. \OC_Hook::clear();
  110. parent::tearDown();
  111. }
  112. /**
  113. * @medium
  114. * test expire logic
  115. * @dataProvider versionsProvider
  116. */
  117. public function testGetExpireList($versions, $sizeOfAllDeletedFiles): void {
  118. // last interval end at 2592000
  119. $startTime = 5000000;
  120. $testClass = new VersionStorageToTest();
  121. [$deleted, $size] = $testClass->callProtectedGetExpireList($startTime, $versions);
  122. // we should have deleted 16 files each of the size 1
  123. $this->assertEquals($sizeOfAllDeletedFiles, $size);
  124. // the deleted array should only contain versions which should be deleted
  125. foreach ($deleted as $key => $path) {
  126. unset($versions[$key]);
  127. $this->assertEquals('delete', substr($path, 0, strlen('delete')));
  128. }
  129. // the versions array should only contain versions which should be kept
  130. foreach ($versions as $version) {
  131. $this->assertEquals('keep', $version['path']);
  132. }
  133. }
  134. public function versionsProvider() {
  135. return [
  136. // first set of versions uniformly distributed versions
  137. [
  138. [
  139. // first slice (10sec) keep one version every 2 seconds
  140. ['version' => 4999999, 'path' => 'keep', 'size' => 1],
  141. ['version' => 4999998, 'path' => 'delete', 'size' => 1],
  142. ['version' => 4999997, 'path' => 'keep', 'size' => 1],
  143. ['version' => 4999995, 'path' => 'keep', 'size' => 1],
  144. ['version' => 4999994, 'path' => 'delete', 'size' => 1],
  145. //next slice (60sec) starts at 4999990 keep one version every 10 secons
  146. ['version' => 4999988, 'path' => 'keep', 'size' => 1],
  147. ['version' => 4999978, 'path' => 'keep', 'size' => 1],
  148. ['version' => 4999975, 'path' => 'delete', 'size' => 1],
  149. ['version' => 4999972, 'path' => 'delete', 'size' => 1],
  150. ['version' => 4999967, 'path' => 'keep', 'size' => 1],
  151. ['version' => 4999958, 'path' => 'delete', 'size' => 1],
  152. ['version' => 4999957, 'path' => 'keep', 'size' => 1],
  153. //next slice (3600sec) start at 4999940 keep one version every 60 seconds
  154. ['version' => 4999900, 'path' => 'keep', 'size' => 1],
  155. ['version' => 4999841, 'path' => 'delete', 'size' => 1],
  156. ['version' => 4999840, 'path' => 'keep', 'size' => 1],
  157. ['version' => 4999780, 'path' => 'keep', 'size' => 1],
  158. ['version' => 4996401, 'path' => 'keep', 'size' => 1],
  159. // next slice (86400sec) start at 4996400 keep one version every 3600 seconds
  160. ['version' => 4996350, 'path' => 'delete', 'size' => 1],
  161. ['version' => 4992800, 'path' => 'keep', 'size' => 1],
  162. ['version' => 4989800, 'path' => 'delete', 'size' => 1],
  163. ['version' => 4989700, 'path' => 'delete', 'size' => 1],
  164. ['version' => 4989200, 'path' => 'keep', 'size' => 1],
  165. // next slice (2592000sec) start at 4913600 keep one version every 86400 seconds
  166. ['version' => 4913600, 'path' => 'keep', 'size' => 1],
  167. ['version' => 4852800, 'path' => 'delete', 'size' => 1],
  168. ['version' => 4827201, 'path' => 'delete', 'size' => 1],
  169. ['version' => 4827200, 'path' => 'keep', 'size' => 1],
  170. ['version' => 4777201, 'path' => 'delete', 'size' => 1],
  171. ['version' => 4777501, 'path' => 'delete', 'size' => 1],
  172. ['version' => 4740000, 'path' => 'keep', 'size' => 1],
  173. // final slice starts at 2408000 keep one version every 604800 secons
  174. ['version' => 2408000, 'path' => 'keep', 'size' => 1],
  175. ['version' => 1803201, 'path' => 'delete', 'size' => 1],
  176. ['version' => 1803200, 'path' => 'keep', 'size' => 1],
  177. ['version' => 1800199, 'path' => 'delete', 'size' => 1],
  178. ['version' => 1800100, 'path' => 'delete', 'size' => 1],
  179. ['version' => 1198300, 'path' => 'keep', 'size' => 1],
  180. ],
  181. 16 // size of all deleted files (every file has the size 1)
  182. ],
  183. // second set of versions, here we have only really old versions
  184. [
  185. [
  186. // first slice (10sec) keep one version every 2 seconds
  187. // next slice (60sec) starts at 4999990 keep one version every 10 secons
  188. // next slice (3600sec) start at 4999940 keep one version every 60 seconds
  189. // next slice (86400sec) start at 4996400 keep one version every 3600 seconds
  190. ['version' => 4996400, 'path' => 'keep', 'size' => 1],
  191. ['version' => 4996350, 'path' => 'delete', 'size' => 1],
  192. ['version' => 4996350, 'path' => 'delete', 'size' => 1],
  193. ['version' => 4992800, 'path' => 'keep', 'size' => 1],
  194. ['version' => 4989800, 'path' => 'delete', 'size' => 1],
  195. ['version' => 4989700, 'path' => 'delete', 'size' => 1],
  196. ['version' => 4989200, 'path' => 'keep', 'size' => 1],
  197. // next slice (2592000sec) start at 4913600 keep one version every 86400 seconds
  198. ['version' => 4913600, 'path' => 'keep', 'size' => 1],
  199. ['version' => 4852800, 'path' => 'delete', 'size' => 1],
  200. ['version' => 4827201, 'path' => 'delete', 'size' => 1],
  201. ['version' => 4827200, 'path' => 'keep', 'size' => 1],
  202. ['version' => 4777201, 'path' => 'delete', 'size' => 1],
  203. ['version' => 4777501, 'path' => 'delete', 'size' => 1],
  204. ['version' => 4740000, 'path' => 'keep', 'size' => 1],
  205. // final slice starts at 2408000 keep one version every 604800 secons
  206. ['version' => 2408000, 'path' => 'keep', 'size' => 1],
  207. ['version' => 1803201, 'path' => 'delete', 'size' => 1],
  208. ['version' => 1803200, 'path' => 'keep', 'size' => 1],
  209. ['version' => 1800199, 'path' => 'delete', 'size' => 1],
  210. ['version' => 1800100, 'path' => 'delete', 'size' => 1],
  211. ['version' => 1198300, 'path' => 'keep', 'size' => 1],
  212. ],
  213. 11 // size of all deleted files (every file has the size 1)
  214. ],
  215. // third set of versions, with some gaps between
  216. [
  217. [
  218. // first slice (10sec) keep one version every 2 seconds
  219. ['version' => 4999999, 'path' => 'keep', 'size' => 1],
  220. ['version' => 4999998, 'path' => 'delete', 'size' => 1],
  221. ['version' => 4999997, 'path' => 'keep', 'size' => 1],
  222. ['version' => 4999995, 'path' => 'keep', 'size' => 1],
  223. ['version' => 4999994, 'path' => 'delete', 'size' => 1],
  224. //next slice (60sec) starts at 4999990 keep one version every 10 secons
  225. ['version' => 4999988, 'path' => 'keep', 'size' => 1],
  226. ['version' => 4999978, 'path' => 'keep', 'size' => 1],
  227. //next slice (3600sec) start at 4999940 keep one version every 60 seconds
  228. // next slice (86400sec) start at 4996400 keep one version every 3600 seconds
  229. ['version' => 4989200, 'path' => 'keep', 'size' => 1],
  230. // next slice (2592000sec) start at 4913600 keep one version every 86400 seconds
  231. ['version' => 4913600, 'path' => 'keep', 'size' => 1],
  232. ['version' => 4852800, 'path' => 'delete', 'size' => 1],
  233. ['version' => 4827201, 'path' => 'delete', 'size' => 1],
  234. ['version' => 4827200, 'path' => 'keep', 'size' => 1],
  235. ['version' => 4777201, 'path' => 'delete', 'size' => 1],
  236. ['version' => 4777501, 'path' => 'delete', 'size' => 1],
  237. ['version' => 4740000, 'path' => 'keep', 'size' => 1],
  238. // final slice starts at 2408000 keep one version every 604800 secons
  239. ['version' => 2408000, 'path' => 'keep', 'size' => 1],
  240. ['version' => 1803201, 'path' => 'delete', 'size' => 1],
  241. ['version' => 1803200, 'path' => 'keep', 'size' => 1],
  242. ['version' => 1800199, 'path' => 'delete', 'size' => 1],
  243. ['version' => 1800100, 'path' => 'delete', 'size' => 1],
  244. ['version' => 1198300, 'path' => 'keep', 'size' => 1],
  245. ],
  246. 9 // size of all deleted files (every file has the size 1)
  247. ],
  248. // fourth set of versions: empty (see issue #19066)
  249. [
  250. [],
  251. 0
  252. ]
  253. ];
  254. }
  255. public function testRename(): void {
  256. Filesystem::file_put_contents('test.txt', 'test file');
  257. $t1 = time();
  258. // second version is two weeks older, this way we make sure that no
  259. // version will be expired
  260. $t2 = $t1 - 60 * 60 * 24 * 14;
  261. // create some versions
  262. $v1 = self::USERS_VERSIONS_ROOT . '/test.txt.v' . $t1;
  263. $v2 = self::USERS_VERSIONS_ROOT . '/test.txt.v' . $t2;
  264. $v1Renamed = self::USERS_VERSIONS_ROOT . '/test2.txt.v' . $t1;
  265. $v2Renamed = self::USERS_VERSIONS_ROOT . '/test2.txt.v' . $t2;
  266. $this->rootView->file_put_contents($v1, 'version1');
  267. $this->rootView->file_put_contents($v2, 'version2');
  268. // execute rename hook of versions app
  269. Filesystem::rename('test.txt', 'test2.txt');
  270. $this->runCommands();
  271. $this->assertFalse($this->rootView->file_exists($v1), 'version 1 of old file does not exist');
  272. $this->assertFalse($this->rootView->file_exists($v2), 'version 2 of old file does not exist');
  273. $this->assertTrue($this->rootView->file_exists($v1Renamed), 'version 1 of renamed file exists');
  274. $this->assertTrue($this->rootView->file_exists($v2Renamed), 'version 2 of renamed file exists');
  275. }
  276. public function testRenameInSharedFolder(): void {
  277. Filesystem::mkdir('folder1');
  278. Filesystem::mkdir('folder1/folder2');
  279. Filesystem::file_put_contents('folder1/test.txt', 'test file');
  280. $t1 = time();
  281. // second version is two weeks older, this way we make sure that no
  282. // version will be expired
  283. $t2 = $t1 - 60 * 60 * 24 * 14;
  284. $this->rootView->mkdir(self::USERS_VERSIONS_ROOT . '/folder1');
  285. // create some versions
  286. $v1 = self::USERS_VERSIONS_ROOT . '/folder1/test.txt.v' . $t1;
  287. $v2 = self::USERS_VERSIONS_ROOT . '/folder1/test.txt.v' . $t2;
  288. $v1Renamed = self::USERS_VERSIONS_ROOT . '/folder1/folder2/test.txt.v' . $t1;
  289. $v2Renamed = self::USERS_VERSIONS_ROOT . '/folder1/folder2/test.txt.v' . $t2;
  290. $this->rootView->file_put_contents($v1, 'version1');
  291. $this->rootView->file_put_contents($v2, 'version2');
  292. $node = \OC::$server->getUserFolder(self::TEST_VERSIONS_USER)->get('folder1');
  293. $share = \OC::$server->getShareManager()->newShare();
  294. $share->setNode($node)
  295. ->setShareType(IShare::TYPE_USER)
  296. ->setSharedBy(self::TEST_VERSIONS_USER)
  297. ->setSharedWith(self::TEST_VERSIONS_USER2)
  298. ->setPermissions(Constants::PERMISSION_ALL);
  299. $share = \OC::$server->getShareManager()->createShare($share);
  300. \OC::$server->getShareManager()->acceptShare($share, self::TEST_VERSIONS_USER2);
  301. self::loginHelper(self::TEST_VERSIONS_USER2);
  302. $this->assertTrue(Filesystem::file_exists('folder1/test.txt'));
  303. // execute rename hook of versions app
  304. Filesystem::rename('/folder1/test.txt', '/folder1/folder2/test.txt');
  305. $this->runCommands();
  306. self::loginHelper(self::TEST_VERSIONS_USER);
  307. $this->assertFalse($this->rootView->file_exists($v1), 'version 1 of old file does not exist');
  308. $this->assertFalse($this->rootView->file_exists($v2), 'version 2 of old file does not exist');
  309. $this->assertTrue($this->rootView->file_exists($v1Renamed), 'version 1 of renamed file exists');
  310. $this->assertTrue($this->rootView->file_exists($v2Renamed), 'version 2 of renamed file exists');
  311. \OC::$server->getShareManager()->deleteShare($share);
  312. }
  313. public function testMoveFolder(): void {
  314. Filesystem::mkdir('folder1');
  315. Filesystem::mkdir('folder2');
  316. Filesystem::file_put_contents('folder1/test.txt', 'test file');
  317. $t1 = time();
  318. // second version is two weeks older, this way we make sure that no
  319. // version will be expired
  320. $t2 = $t1 - 60 * 60 * 24 * 14;
  321. // create some versions
  322. $this->rootView->mkdir(self::USERS_VERSIONS_ROOT . '/folder1');
  323. $v1 = self::USERS_VERSIONS_ROOT . '/folder1/test.txt.v' . $t1;
  324. $v2 = self::USERS_VERSIONS_ROOT . '/folder1/test.txt.v' . $t2;
  325. $v1Renamed = self::USERS_VERSIONS_ROOT . '/folder2/folder1/test.txt.v' . $t1;
  326. $v2Renamed = self::USERS_VERSIONS_ROOT . '/folder2/folder1/test.txt.v' . $t2;
  327. $this->rootView->file_put_contents($v1, 'version1');
  328. $this->rootView->file_put_contents($v2, 'version2');
  329. // execute rename hook of versions app
  330. Filesystem::rename('folder1', 'folder2/folder1');
  331. $this->runCommands();
  332. $this->assertFalse($this->rootView->file_exists($v1));
  333. $this->assertFalse($this->rootView->file_exists($v2));
  334. $this->assertTrue($this->rootView->file_exists($v1Renamed));
  335. $this->assertTrue($this->rootView->file_exists($v2Renamed));
  336. }
  337. public function testMoveFileIntoSharedFolderAsRecipient(): void {
  338. Filesystem::mkdir('folder1');
  339. $fileInfo = Filesystem::getFileInfo('folder1');
  340. $node = \OC::$server->getUserFolder(self::TEST_VERSIONS_USER)->get('folder1');
  341. $share = \OC::$server->getShareManager()->newShare();
  342. $share->setNode($node)
  343. ->setShareType(IShare::TYPE_USER)
  344. ->setSharedBy(self::TEST_VERSIONS_USER)
  345. ->setSharedWith(self::TEST_VERSIONS_USER2)
  346. ->setPermissions(Constants::PERMISSION_ALL);
  347. $share = \OC::$server->getShareManager()->createShare($share);
  348. \OC::$server->getShareManager()->acceptShare($share, self::TEST_VERSIONS_USER2);
  349. self::loginHelper(self::TEST_VERSIONS_USER2);
  350. $versionsFolder2 = '/' . self::TEST_VERSIONS_USER2 . '/files_versions';
  351. Filesystem::file_put_contents('test.txt', 'test file');
  352. $t1 = time();
  353. // second version is two weeks older, this way we make sure that no
  354. // version will be expired
  355. $t2 = $t1 - 60 * 60 * 24 * 14;
  356. $this->rootView->mkdir($versionsFolder2);
  357. // create some versions
  358. $v1 = $versionsFolder2 . '/test.txt.v' . $t1;
  359. $v2 = $versionsFolder2 . '/test.txt.v' . $t2;
  360. $this->rootView->file_put_contents($v1, 'version1');
  361. $this->rootView->file_put_contents($v2, 'version2');
  362. // move file into the shared folder as recipient
  363. Filesystem::rename('/test.txt', '/folder1/test.txt');
  364. $this->assertFalse($this->rootView->file_exists($v1));
  365. $this->assertFalse($this->rootView->file_exists($v2));
  366. self::loginHelper(self::TEST_VERSIONS_USER);
  367. $versionsFolder1 = '/' . self::TEST_VERSIONS_USER . '/files_versions';
  368. $v1Renamed = $versionsFolder1 . '/folder1/test.txt.v' . $t1;
  369. $v2Renamed = $versionsFolder1 . '/folder1/test.txt.v' . $t2;
  370. $this->assertTrue($this->rootView->file_exists($v1Renamed));
  371. $this->assertTrue($this->rootView->file_exists($v2Renamed));
  372. \OC::$server->getShareManager()->deleteShare($share);
  373. }
  374. public function testMoveFolderIntoSharedFolderAsRecipient(): void {
  375. Filesystem::mkdir('folder1');
  376. $node = \OC::$server->getUserFolder(self::TEST_VERSIONS_USER)->get('folder1');
  377. $share = \OC::$server->getShareManager()->newShare();
  378. $share->setNode($node)
  379. ->setShareType(IShare::TYPE_USER)
  380. ->setSharedBy(self::TEST_VERSIONS_USER)
  381. ->setSharedWith(self::TEST_VERSIONS_USER2)
  382. ->setPermissions(Constants::PERMISSION_ALL);
  383. $share = \OC::$server->getShareManager()->createShare($share);
  384. \OC::$server->getShareManager()->acceptShare($share, self::TEST_VERSIONS_USER2);
  385. self::loginHelper(self::TEST_VERSIONS_USER2);
  386. $versionsFolder2 = '/' . self::TEST_VERSIONS_USER2 . '/files_versions';
  387. Filesystem::mkdir('folder2');
  388. Filesystem::file_put_contents('folder2/test.txt', 'test file');
  389. $t1 = time();
  390. // second version is two weeks older, this way we make sure that no
  391. // version will be expired
  392. $t2 = $t1 - 60 * 60 * 24 * 14;
  393. $this->rootView->mkdir($versionsFolder2);
  394. $this->rootView->mkdir($versionsFolder2 . '/folder2');
  395. // create some versions
  396. $v1 = $versionsFolder2 . '/folder2/test.txt.v' . $t1;
  397. $v2 = $versionsFolder2 . '/folder2/test.txt.v' . $t2;
  398. $this->rootView->file_put_contents($v1, 'version1');
  399. $this->rootView->file_put_contents($v2, 'version2');
  400. // move file into the shared folder as recipient
  401. Filesystem::rename('/folder2', '/folder1/folder2');
  402. $this->assertFalse($this->rootView->file_exists($v1));
  403. $this->assertFalse($this->rootView->file_exists($v2));
  404. self::loginHelper(self::TEST_VERSIONS_USER);
  405. $versionsFolder1 = '/' . self::TEST_VERSIONS_USER . '/files_versions';
  406. $v1Renamed = $versionsFolder1 . '/folder1/folder2/test.txt.v' . $t1;
  407. $v2Renamed = $versionsFolder1 . '/folder1/folder2/test.txt.v' . $t2;
  408. $this->assertTrue($this->rootView->file_exists($v1Renamed));
  409. $this->assertTrue($this->rootView->file_exists($v2Renamed));
  410. \OC::$server->getShareManager()->deleteShare($share);
  411. }
  412. public function testRenameSharedFile(): void {
  413. Filesystem::file_put_contents('test.txt', 'test file');
  414. $t1 = time();
  415. // second version is two weeks older, this way we make sure that no
  416. // version will be expired
  417. $t2 = $t1 - 60 * 60 * 24 * 14;
  418. $this->rootView->mkdir(self::USERS_VERSIONS_ROOT);
  419. // create some versions
  420. $v1 = self::USERS_VERSIONS_ROOT . '/test.txt.v' . $t1;
  421. $v2 = self::USERS_VERSIONS_ROOT . '/test.txt.v' . $t2;
  422. // the renamed versions should not exist! Because we only moved the mount point!
  423. $v1Renamed = self::USERS_VERSIONS_ROOT . '/test2.txt.v' . $t1;
  424. $v2Renamed = self::USERS_VERSIONS_ROOT . '/test2.txt.v' . $t2;
  425. $this->rootView->file_put_contents($v1, 'version1');
  426. $this->rootView->file_put_contents($v2, 'version2');
  427. $node = \OC::$server->getUserFolder(self::TEST_VERSIONS_USER)->get('test.txt');
  428. $share = \OC::$server->getShareManager()->newShare();
  429. $share->setNode($node)
  430. ->setShareType(IShare::TYPE_USER)
  431. ->setSharedBy(self::TEST_VERSIONS_USER)
  432. ->setSharedWith(self::TEST_VERSIONS_USER2)
  433. ->setPermissions(Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE | Constants::PERMISSION_SHARE);
  434. $share = \OC::$server->getShareManager()->createShare($share);
  435. \OC::$server->getShareManager()->acceptShare($share, self::TEST_VERSIONS_USER2);
  436. self::loginHelper(self::TEST_VERSIONS_USER2);
  437. $this->assertTrue(Filesystem::file_exists('test.txt'));
  438. // execute rename hook of versions app
  439. Filesystem::rename('test.txt', 'test2.txt');
  440. self::loginHelper(self::TEST_VERSIONS_USER);
  441. $this->runCommands();
  442. $this->assertTrue($this->rootView->file_exists($v1));
  443. $this->assertTrue($this->rootView->file_exists($v2));
  444. $this->assertFalse($this->rootView->file_exists($v1Renamed));
  445. $this->assertFalse($this->rootView->file_exists($v2Renamed));
  446. \OC::$server->getShareManager()->deleteShare($share);
  447. }
  448. public function testCopy(): void {
  449. Filesystem::file_put_contents('test.txt', 'test file');
  450. $t1 = time();
  451. // second version is two weeks older, this way we make sure that no
  452. // version will be expired
  453. $t2 = $t1 - 60 * 60 * 24 * 14;
  454. // create some versions
  455. $v1 = self::USERS_VERSIONS_ROOT . '/test.txt.v' . $t1;
  456. $v2 = self::USERS_VERSIONS_ROOT . '/test.txt.v' . $t2;
  457. $v1Copied = self::USERS_VERSIONS_ROOT . '/test2.txt.v' . $t1;
  458. $v2Copied = self::USERS_VERSIONS_ROOT . '/test2.txt.v' . $t2;
  459. $this->rootView->file_put_contents($v1, 'version1');
  460. $this->rootView->file_put_contents($v2, 'version2');
  461. // execute copy hook of versions app
  462. Filesystem::copy('test.txt', 'test2.txt');
  463. $this->runCommands();
  464. $this->assertTrue($this->rootView->file_exists($v1), 'version 1 of original file exists');
  465. $this->assertTrue($this->rootView->file_exists($v2), 'version 2 of original file exists');
  466. $this->assertTrue($this->rootView->file_exists($v1Copied), 'version 1 of copied file exists');
  467. $this->assertTrue($this->rootView->file_exists($v2Copied), 'version 2 of copied file exists');
  468. }
  469. /**
  470. * test if we find all versions and if the versions array contain
  471. * the correct 'path' and 'name'
  472. */
  473. public function testGetVersions(): void {
  474. $t1 = time();
  475. // second version is two weeks older, this way we make sure that no
  476. // version will be expired
  477. $t2 = $t1 - 60 * 60 * 24 * 14;
  478. // create some versions
  479. $v1 = self::USERS_VERSIONS_ROOT . '/subfolder/test.txt.v' . $t1;
  480. $v2 = self::USERS_VERSIONS_ROOT . '/subfolder/test.txt.v' . $t2;
  481. $this->rootView->mkdir(self::USERS_VERSIONS_ROOT . '/subfolder/');
  482. $this->rootView->file_put_contents($v1, 'version1');
  483. $this->rootView->file_put_contents($v2, 'version2');
  484. // execute copy hook of versions app
  485. $versions = Storage::getVersions(self::TEST_VERSIONS_USER, '/subfolder/test.txt');
  486. $this->assertCount(2, $versions);
  487. foreach ($versions as $version) {
  488. $this->assertSame('/subfolder/test.txt', $version['path']);
  489. $this->assertSame('test.txt', $version['name']);
  490. }
  491. //cleanup
  492. $this->rootView->deleteAll(self::USERS_VERSIONS_ROOT . '/subfolder');
  493. }
  494. /**
  495. * test if we find all versions and if the versions array contain
  496. * the correct 'path' and 'name'
  497. */
  498. public function testGetVersionsEmptyFile(): void {
  499. // execute copy hook of versions app
  500. $versions = Storage::getVersions(self::TEST_VERSIONS_USER, '');
  501. $this->assertCount(0, $versions);
  502. $versions = Storage::getVersions(self::TEST_VERSIONS_USER, null);
  503. $this->assertCount(0, $versions);
  504. }
  505. public function testExpireNonexistingFile(): void {
  506. $this->logout();
  507. // needed to have a FS setup (the background job does this)
  508. \OC_Util::setupFS(self::TEST_VERSIONS_USER);
  509. $this->assertFalse(Storage::expire('/void/unexist.txt', self::TEST_VERSIONS_USER));
  510. }
  511. public function testExpireNonexistingUser(): void {
  512. $this->expectException(NoUserException::class);
  513. $this->logout();
  514. // needed to have a FS setup (the background job does this)
  515. \OC_Util::setupFS(self::TEST_VERSIONS_USER);
  516. Filesystem::file_put_contents('test.txt', 'test file');
  517. $this->assertFalse(Storage::expire('test.txt', 'unexist'));
  518. }
  519. public function testRestoreSameStorage(): void {
  520. Filesystem::mkdir('sub');
  521. $this->doTestRestore();
  522. }
  523. public function testRestoreCrossStorage(): void {
  524. $storage2 = new Temporary([]);
  525. Filesystem::mount($storage2, [], self::TEST_VERSIONS_USER . '/files/sub');
  526. $this->doTestRestore();
  527. }
  528. public function testRestoreNoPermission(): void {
  529. $this->loginAsUser(self::TEST_VERSIONS_USER);
  530. $userHome = \OC::$server->getUserFolder(self::TEST_VERSIONS_USER);
  531. $node = $userHome->newFolder('folder');
  532. $file = $node->newFile('test.txt');
  533. $share = \OC::$server->getShareManager()->newShare();
  534. $share->setNode($node)
  535. ->setShareType(IShare::TYPE_USER)
  536. ->setSharedBy(self::TEST_VERSIONS_USER)
  537. ->setSharedWith(self::TEST_VERSIONS_USER2)
  538. ->setPermissions(Constants::PERMISSION_READ);
  539. $share = \OC::$server->getShareManager()->createShare($share);
  540. \OC::$server->getShareManager()->acceptShare($share, self::TEST_VERSIONS_USER2);
  541. $versions = $this->createAndCheckVersions(
  542. Filesystem::getView(),
  543. 'folder/test.txt'
  544. );
  545. $file->putContent('test file');
  546. $this->loginAsUser(self::TEST_VERSIONS_USER2);
  547. $firstVersion = current($versions);
  548. $this->assertFalse(Storage::rollback('folder/test.txt', $firstVersion['version'], $this->user2), 'Revert did not happen');
  549. $this->loginAsUser(self::TEST_VERSIONS_USER);
  550. \OC::$server->getShareManager()->deleteShare($share);
  551. $this->assertEquals('test file', $file->getContent(), 'File content has not changed');
  552. }
  553. public function testRestoreMovedShare(): void {
  554. $this->markTestSkipped('Unreliable test');
  555. $this->loginAsUser(self::TEST_VERSIONS_USER);
  556. $userHome = \OC::$server->getUserFolder(self::TEST_VERSIONS_USER);
  557. $node = $userHome->newFolder('folder');
  558. $file = $node->newFile('test.txt');
  559. $userHome2 = \OC::$server->getUserFolder(self::TEST_VERSIONS_USER2);
  560. $userHome2->newFolder('subfolder');
  561. $share = \OC::$server->getShareManager()->newShare();
  562. $share->setNode($node)
  563. ->setShareType(IShare::TYPE_USER)
  564. ->setSharedBy(self::TEST_VERSIONS_USER)
  565. ->setSharedWith(self::TEST_VERSIONS_USER2)
  566. ->setPermissions(Constants::PERMISSION_ALL);
  567. $share = \OC::$server->getShareManager()->createShare($share);
  568. $shareManager = \OC::$server->getShareManager();
  569. $shareManager->acceptShare($share, self::TEST_VERSIONS_USER2);
  570. $share->setTarget('subfolder/folder');
  571. $shareManager->moveShare($share, self::TEST_VERSIONS_USER2);
  572. $versions = $this->createAndCheckVersions(
  573. Filesystem::getView(),
  574. 'folder/test.txt'
  575. );
  576. $file->putContent('test file');
  577. $this->loginAsUser(self::TEST_VERSIONS_USER2);
  578. $firstVersion = current($versions);
  579. $this->assertTrue(Storage::rollback('folder/test.txt', $firstVersion['version'], $this->user1));
  580. $this->loginAsUser(self::TEST_VERSIONS_USER);
  581. \OC::$server->getShareManager()->deleteShare($share);
  582. $this->assertEquals('version 2', $file->getContent(), 'File content has not changed');
  583. }
  584. /**
  585. * @param string $hookName name of hook called
  586. * @param string $params variable to receive parameters provided by hook
  587. */
  588. private function connectMockHooks($hookName, &$params) {
  589. if ($hookName === null) {
  590. return;
  591. }
  592. $eventHandler = $this->getMockBuilder(\stdclass::class)
  593. ->setMethods(['callback'])
  594. ->getMock();
  595. $eventHandler->expects($this->any())
  596. ->method('callback')
  597. ->willReturnCallback(
  598. function ($p) use (&$params): void {
  599. $params = $p;
  600. }
  601. );
  602. Util::connectHook(
  603. '\OCP\Versions',
  604. $hookName,
  605. $eventHandler,
  606. 'callback'
  607. );
  608. }
  609. private function doTestRestore() {
  610. $filePath = self::TEST_VERSIONS_USER . '/files/sub/test.txt';
  611. $this->rootView->file_put_contents($filePath, 'test file');
  612. $fileInfo = $this->rootView->getFileInfo($filePath);
  613. $t0 = $this->rootView->filemtime($filePath);
  614. // not exactly the same timestamp as the file
  615. $t1 = time() - 60;
  616. // second version is two weeks older
  617. $t2 = $t1 - 60 * 60 * 24 * 14;
  618. // create some versions
  619. $v1 = self::USERS_VERSIONS_ROOT . '/sub/test.txt.v' . $t1;
  620. $v2 = self::USERS_VERSIONS_ROOT . '/sub/test.txt.v' . $t2;
  621. $this->rootView->mkdir(self::USERS_VERSIONS_ROOT . '/sub');
  622. $this->rootView->file_put_contents($v1, 'version1');
  623. $fileInfoV1 = $this->rootView->getFileInfo($v1);
  624. $versionEntity = new VersionEntity();
  625. $versionEntity->setFileId($fileInfo->getId());
  626. $versionEntity->setTimestamp($t1);
  627. $versionEntity->setSize($fileInfoV1->getSize());
  628. $versionEntity->setMimetype($this->mimeTypeLoader->getId($fileInfoV1->getMimetype()));
  629. $versionEntity->setMetadata([]);
  630. $this->versionsMapper->insert($versionEntity);
  631. $this->rootView->file_put_contents($v2, 'version2');
  632. $fileInfoV2 = $this->rootView->getFileInfo($v2);
  633. $versionEntity = new VersionEntity();
  634. $versionEntity->setFileId($fileInfo->getId());
  635. $versionEntity->setTimestamp($t2);
  636. $versionEntity->setSize($fileInfoV2->getSize());
  637. $versionEntity->setMimetype($this->mimeTypeLoader->getId($fileInfoV2->getMimetype()));
  638. $versionEntity->setMetadata([]);
  639. $this->versionsMapper->insert($versionEntity);
  640. $oldVersions = Storage::getVersions(
  641. self::TEST_VERSIONS_USER, '/sub/test.txt'
  642. );
  643. $this->assertCount(2, $oldVersions);
  644. $this->assertEquals('test file', $this->rootView->file_get_contents($filePath));
  645. $info1 = $this->rootView->getFileInfo($filePath);
  646. $params = [];
  647. $this->connectMockHooks('rollback', $params);
  648. $versionManager = Server::get(IVersionManager::class);
  649. $versions = $versionManager->getVersionsForFile($this->user1, $info1);
  650. $version = array_filter($versions, function ($version) use ($t2) {
  651. return $version->getRevisionId() === $t2;
  652. });
  653. $this->assertTrue($versionManager->rollback(current($version)));
  654. $expectedParams = [
  655. 'path' => '/sub/test.txt',
  656. ];
  657. $this->assertEquals($expectedParams['path'], $params['path']);
  658. $this->assertTrue(array_key_exists('revision', $params));
  659. $this->assertTrue($params['revision'] > 0);
  660. $this->assertEquals('version2', $this->rootView->file_get_contents($filePath));
  661. $info2 = $this->rootView->getFileInfo($filePath);
  662. $this->assertNotEquals(
  663. $info2['etag'],
  664. $info1['etag'],
  665. 'Etag must change after rolling back version'
  666. );
  667. $this->assertEquals(
  668. $info2['fileid'],
  669. $info1['fileid'],
  670. 'File id must not change after rolling back version'
  671. );
  672. $this->assertEquals(
  673. $info2['mtime'],
  674. $t2,
  675. 'Restored file has mtime from version'
  676. );
  677. $newVersions = Storage::getVersions(
  678. self::TEST_VERSIONS_USER, '/sub/test.txt'
  679. );
  680. $this->assertTrue(
  681. $this->rootView->file_exists(self::USERS_VERSIONS_ROOT . '/sub/test.txt.v' . $t0),
  682. 'A version file was created for the file before restoration'
  683. );
  684. $this->assertTrue(
  685. $this->rootView->file_exists($v1),
  686. 'Untouched version file is still there'
  687. );
  688. $this->assertFalse(
  689. $this->rootView->file_exists($v2),
  690. 'Restored version file gone from files_version folder'
  691. );
  692. $this->assertCount(2, $newVersions, 'Additional version created');
  693. $this->assertTrue(
  694. isset($newVersions[$t0 . '#' . 'test.txt']),
  695. 'A version was created for the file before restoration'
  696. );
  697. $this->assertTrue(
  698. isset($newVersions[$t1 . '#' . 'test.txt']),
  699. 'Untouched version is still there'
  700. );
  701. $this->assertFalse(
  702. isset($newVersions[$t2 . '#' . 'test.txt']),
  703. 'Restored version is not in the list any more'
  704. );
  705. }
  706. /**
  707. * Test whether versions are created when overwriting as owner
  708. */
  709. public function testStoreVersionAsOwner(): void {
  710. $this->loginAsUser(self::TEST_VERSIONS_USER);
  711. $this->createAndCheckVersions(
  712. Filesystem::getView(),
  713. 'test.txt'
  714. );
  715. }
  716. /**
  717. * Test whether versions are created when overwriting as share recipient
  718. */
  719. public function testStoreVersionAsRecipient(): void {
  720. $this->loginAsUser(self::TEST_VERSIONS_USER);
  721. Filesystem::mkdir('folder');
  722. Filesystem::file_put_contents('folder/test.txt', 'test file');
  723. $node = \OC::$server->getUserFolder(self::TEST_VERSIONS_USER)->get('folder');
  724. $share = \OC::$server->getShareManager()->newShare();
  725. $share->setNode($node)
  726. ->setShareType(IShare::TYPE_USER)
  727. ->setSharedBy(self::TEST_VERSIONS_USER)
  728. ->setSharedWith(self::TEST_VERSIONS_USER2)
  729. ->setPermissions(Constants::PERMISSION_ALL);
  730. $share = \OC::$server->getShareManager()->createShare($share);
  731. \OC::$server->getShareManager()->acceptShare($share, self::TEST_VERSIONS_USER2);
  732. $this->loginAsUser(self::TEST_VERSIONS_USER2);
  733. $this->createAndCheckVersions(
  734. Filesystem::getView(),
  735. 'folder/test.txt'
  736. );
  737. \OC::$server->getShareManager()->deleteShare($share);
  738. }
  739. /**
  740. * Test whether versions are created when overwriting anonymously.
  741. *
  742. * When uploading through a public link or publicwebdav, no user
  743. * is logged in. File modification must still be able to find
  744. * the owner and create versions.
  745. */
  746. public function testStoreVersionAsAnonymous(): void {
  747. $this->logout();
  748. // note: public link upload does this,
  749. // needed to make the hooks fire
  750. \OC_Util::setupFS(self::TEST_VERSIONS_USER);
  751. $userView = new View('/' . self::TEST_VERSIONS_USER . '/files');
  752. $this->createAndCheckVersions(
  753. $userView,
  754. 'test.txt'
  755. );
  756. }
  757. /**
  758. * @param View $view
  759. * @param string $path
  760. */
  761. private function createAndCheckVersions(View $view, $path) {
  762. $view->file_put_contents($path, 'test file');
  763. $view->file_put_contents($path, 'version 1');
  764. $view->file_put_contents($path, 'version 2');
  765. $this->loginAsUser(self::TEST_VERSIONS_USER);
  766. // need to scan for the versions
  767. [$rootStorage,] = $this->rootView->resolvePath(self::TEST_VERSIONS_USER . '/files_versions');
  768. $rootStorage->getScanner()->scan('files_versions');
  769. $versions = Storage::getVersions(
  770. self::TEST_VERSIONS_USER, '/' . $path
  771. );
  772. // note: we cannot predict how many versions are created due to
  773. // test run timing
  774. $this->assertGreaterThan(0, count($versions));
  775. return $versions;
  776. }
  777. /**
  778. * @param string $user
  779. * @param bool $create
  780. */
  781. public static function loginHelper($user, $create = false) {
  782. if ($create) {
  783. $backend = new \Test\Util\User\Dummy();
  784. $backend->createUser($user, $user);
  785. \OC::$server->getUserManager()->registerBackend($backend);
  786. }
  787. \OC_Util::tearDownFS();
  788. \OC_User::setUserId('');
  789. Filesystem::tearDown();
  790. \OC_User::setUserId($user);
  791. \OC_Util::setupFS($user);
  792. \OC::$server->getUserFolder($user);
  793. }
  794. }
  795. // extend the original class to make it possible to test protected methods
  796. class VersionStorageToTest extends Storage {
  797. /**
  798. * @param integer $time
  799. */
  800. public function callProtectedGetExpireList($time, $versions) {
  801. return self::getExpireList($time, $versions);
  802. }
  803. }