VersioningTest.php 34 KB

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