VersioningTest.php 35 KB

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