VersioningTest.php 35 KB

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