1
0

VersioningTest.php 36 KB

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