1
0

TrashbinTest.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. use OC\AppFramework\Bootstrap\BootContext;
  8. use OC\AppFramework\DependencyInjection\DIContainer;
  9. use OCA\Files_Sharing\AppInfo\Application;
  10. use OCA\Files_Trashbin\AppInfo\Application as TrashbinApplication;
  11. use OCP\Share\IShare;
  12. /**
  13. * Class Test_Encryption
  14. *
  15. * @group DB
  16. */
  17. class TrashbinTest extends \Test\TestCase {
  18. public const TEST_TRASHBIN_USER1 = "test-trashbin-user1";
  19. public const TEST_TRASHBIN_USER2 = "test-trashbin-user2";
  20. private $trashRoot1;
  21. private $trashRoot2;
  22. private static $rememberRetentionObligation;
  23. /**
  24. * @var bool
  25. */
  26. private static $trashBinStatus;
  27. /**
  28. * @var \OC\Files\View
  29. */
  30. private $rootView;
  31. public static function setUpBeforeClass(): void {
  32. parent::setUpBeforeClass();
  33. $appManager = \OC::$server->getAppManager();
  34. self::$trashBinStatus = $appManager->isEnabledForUser('files_trashbin');
  35. // reset backend
  36. \OC_User::clearBackends();
  37. \OC_User::useBackend('database');
  38. // clear share hooks
  39. \OC_Hook::clear('OCP\\Share');
  40. \OC::registerShareHooks(\OC::$server->getSystemConfig());
  41. // init files sharing
  42. new Application();
  43. //disable encryption
  44. \OC::$server->getAppManager()->disableApp('encryption');
  45. $config = \OC::$server->getConfig();
  46. //configure trashbin
  47. self::$rememberRetentionObligation = $config->getSystemValue('trashbin_retention_obligation', \OCA\Files_Trashbin\Expiration::DEFAULT_RETENTION_OBLIGATION);
  48. /** @var \OCA\Files_Trashbin\Expiration $expiration */
  49. $expiration = \OC::$server->query(\OCA\Files_Trashbin\Expiration::class);
  50. $expiration->setRetentionObligation('auto, 2');
  51. // register trashbin hooks
  52. $trashbinApp = new TrashbinApplication();
  53. $trashbinApp->boot(new BootContext(new DIContainer('', [], \OC::$server)));
  54. // create test user
  55. self::loginHelper(self::TEST_TRASHBIN_USER2, true);
  56. self::loginHelper(self::TEST_TRASHBIN_USER1, true);
  57. }
  58. public static function tearDownAfterClass(): void {
  59. // cleanup test user
  60. $user = \OC::$server->getUserManager()->get(self::TEST_TRASHBIN_USER1);
  61. if ($user !== null) {
  62. $user->delete();
  63. }
  64. /** @var \OCA\Files_Trashbin\Expiration $expiration */
  65. $expiration = \OC::$server->query(\OCA\Files_Trashbin\Expiration::class);
  66. $expiration->setRetentionObligation(self::$rememberRetentionObligation);
  67. \OC_Hook::clear();
  68. \OC\Files\Filesystem::getLoader()->removeStorageWrapper('oc_trashbin');
  69. if (self::$trashBinStatus) {
  70. \OC::$server->getAppManager()->enableApp('files_trashbin');
  71. }
  72. parent::tearDownAfterClass();
  73. }
  74. protected function setUp(): void {
  75. parent::setUp();
  76. \OC::$server->getAppManager()->enableApp('files_trashbin');
  77. $config = \OC::$server->getConfig();
  78. $mockConfig = $this->createMock(\OCP\IConfig::class);
  79. $mockConfig
  80. ->method('getSystemValue')
  81. ->willReturnCallback(static 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. $mockConfig
  89. ->method('getUserValue')
  90. ->willReturnCallback(static function ($userId, $appName, $key, $default = '') use ($config) {
  91. return $config->getUserValue($userId, $appName, $key, $default);
  92. });
  93. $mockConfig
  94. ->method('getAppValue')
  95. ->willReturnCallback(static function ($appName, $key, $default = '') use ($config) {
  96. return $config->getAppValue($appName, $key, $default);
  97. });
  98. $this->overwriteService(\OC\AllConfig::class, $mockConfig);
  99. $this->trashRoot1 = '/' . self::TEST_TRASHBIN_USER1 . '/files_trashbin';
  100. $this->trashRoot2 = '/' . self::TEST_TRASHBIN_USER2 . '/files_trashbin';
  101. $this->rootView = new \OC\Files\View();
  102. self::loginHelper(self::TEST_TRASHBIN_USER1);
  103. }
  104. protected function tearDown(): void {
  105. $this->restoreService(\OC\AllConfig::class);
  106. // disable trashbin to be able to properly clean up
  107. \OC::$server->getAppManager()->disableApp('files_trashbin');
  108. $this->rootView->deleteAll('/' . self::TEST_TRASHBIN_USER1 . '/files');
  109. $this->rootView->deleteAll('/' . self::TEST_TRASHBIN_USER2 . '/files');
  110. $this->rootView->deleteAll($this->trashRoot1);
  111. $this->rootView->deleteAll($this->trashRoot2);
  112. // clear trash table
  113. $connection = \OC::$server->getDatabaseConnection();
  114. $connection->executeUpdate('DELETE FROM `*PREFIX*files_trash`');
  115. parent::tearDown();
  116. }
  117. /**
  118. * test expiration of files older then the max storage time defined for the trash
  119. */
  120. public function testExpireOldFiles() {
  121. /** @var \OCP\AppFramework\Utility\ITimeFactory $time */
  122. $time = \OC::$server->query(\OCP\AppFramework\Utility\ITimeFactory::class);
  123. $currentTime = $time->getTime();
  124. $expireAt = $currentTime - 2 * 24 * 60 * 60;
  125. $expiredDate = $currentTime - 3 * 24 * 60 * 60;
  126. // create some files
  127. \OC\Files\Filesystem::file_put_contents('file1.txt', 'file1');
  128. \OC\Files\Filesystem::file_put_contents('file2.txt', 'file2');
  129. \OC\Files\Filesystem::file_put_contents('file3.txt', 'file3');
  130. // delete them so that they end up in the trash bin
  131. \OC\Files\Filesystem::unlink('file1.txt');
  132. \OC\Files\Filesystem::unlink('file2.txt');
  133. \OC\Files\Filesystem::unlink('file3.txt');
  134. //make sure that files are in the trash bin
  135. $filesInTrash = OCA\Files_Trashbin\Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1, 'name');
  136. $this->assertSame(3, count($filesInTrash));
  137. // every second file will get a date in the past so that it will get expired
  138. $manipulatedList = $this->manipulateDeleteTime($filesInTrash, $this->trashRoot1, $expiredDate);
  139. $testClass = new TrashbinForTesting();
  140. [$sizeOfDeletedFiles, $count] = $testClass->dummyDeleteExpiredFiles($manipulatedList, $expireAt);
  141. $this->assertSame(10, $sizeOfDeletedFiles);
  142. $this->assertSame(2, $count);
  143. // only file2.txt should be left
  144. $remainingFiles = array_slice($manipulatedList, $count);
  145. $this->assertSame(1, count($remainingFiles));
  146. $remainingFile = reset($remainingFiles);
  147. // TODO: failing test
  148. #$this->assertSame('file2.txt', $remainingFile['name']);
  149. // check that file1.txt and file3.txt was really deleted
  150. $newTrashContent = OCA\Files_Trashbin\Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1);
  151. $this->assertSame(1, count($newTrashContent));
  152. $element = reset($newTrashContent);
  153. // TODO: failing test
  154. #$this->assertSame('file2.txt', $element['name']);
  155. }
  156. /**
  157. * test expiration of files older then the max storage time defined for the trash
  158. * in this test we delete a shared file and check if both trash bins, the one from
  159. * the owner of the file and the one from the user who deleted the file get expired
  160. * correctly
  161. */
  162. public function testExpireOldFilesShared() {
  163. $currentTime = time();
  164. $folder = "trashTest-" . $currentTime . '/';
  165. $expiredDate = $currentTime - 3 * 24 * 60 * 60;
  166. // create some files
  167. \OC\Files\Filesystem::mkdir($folder);
  168. \OC\Files\Filesystem::file_put_contents($folder . 'user1-1.txt', 'file1');
  169. \OC\Files\Filesystem::file_put_contents($folder . 'user1-2.txt', 'file2');
  170. \OC\Files\Filesystem::file_put_contents($folder . 'user1-3.txt', 'file3');
  171. \OC\Files\Filesystem::file_put_contents($folder . 'user1-4.txt', 'file4');
  172. //share user1-4.txt with user2
  173. $node = \OC::$server->getUserFolder(self::TEST_TRASHBIN_USER1)->get($folder);
  174. $share = \OC::$server->getShareManager()->newShare();
  175. $share->setShareType(IShare::TYPE_USER)
  176. ->setNode($node)
  177. ->setSharedBy(self::TEST_TRASHBIN_USER1)
  178. ->setSharedWith(self::TEST_TRASHBIN_USER2)
  179. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  180. $share = \OC::$server->getShareManager()->createShare($share);
  181. \OC::$server->getShareManager()->acceptShare($share, self::TEST_TRASHBIN_USER2);
  182. // delete them so that they end up in the trash bin
  183. \OC\Files\Filesystem::unlink($folder . 'user1-1.txt');
  184. \OC\Files\Filesystem::unlink($folder . 'user1-2.txt');
  185. \OC\Files\Filesystem::unlink($folder . 'user1-3.txt');
  186. $filesInTrash = OCA\Files_Trashbin\Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1, 'name');
  187. $this->assertSame(3, count($filesInTrash));
  188. // every second file will get a date in the past so that it will get expired
  189. $this->manipulateDeleteTime($filesInTrash, $this->trashRoot1, $expiredDate);
  190. // login as user2
  191. self::loginHelper(self::TEST_TRASHBIN_USER2);
  192. $this->assertTrue(\OC\Files\Filesystem::file_exists($folder . "user1-4.txt"));
  193. // create some files
  194. \OC\Files\Filesystem::file_put_contents('user2-1.txt', 'file1');
  195. \OC\Files\Filesystem::file_put_contents('user2-2.txt', 'file2');
  196. // delete them so that they end up in the trash bin
  197. \OC\Files\Filesystem::unlink('user2-1.txt');
  198. \OC\Files\Filesystem::unlink('user2-2.txt');
  199. $filesInTrashUser2 = OCA\Files_Trashbin\Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER2, 'name');
  200. $this->assertSame(2, count($filesInTrashUser2));
  201. // every second file will get a date in the past so that it will get expired
  202. $this->manipulateDeleteTime($filesInTrashUser2, $this->trashRoot2, $expiredDate);
  203. \OC\Files\Filesystem::unlink($folder . 'user1-4.txt');
  204. $this->runCommands();
  205. $filesInTrashUser2AfterDelete = OCA\Files_Trashbin\Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER2);
  206. // user2-1.txt should have been expired
  207. $this->verifyArray($filesInTrashUser2AfterDelete, ['user2-2.txt', 'user1-4.txt']);
  208. self::loginHelper(self::TEST_TRASHBIN_USER1);
  209. // user1-1.txt and user1-3.txt should have been expired
  210. $filesInTrashUser1AfterDelete = OCA\Files_Trashbin\Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1);
  211. $this->verifyArray($filesInTrashUser1AfterDelete, ['user1-2.txt', 'user1-4.txt']);
  212. }
  213. /**
  214. * verify that the array contains the expected results
  215. *
  216. * @param OCP\Files\FileInfo[] $result
  217. * @param string[] $expected
  218. */
  219. private function verifyArray($result, $expected) {
  220. $this->assertSame(count($expected), count($result));
  221. foreach ($expected as $expectedFile) {
  222. $found = false;
  223. foreach ($result as $fileInTrash) {
  224. if ($expectedFile === $fileInTrash['name']) {
  225. $found = true;
  226. break;
  227. }
  228. }
  229. if (!$found) {
  230. // if we didn't found the expected file, something went wrong
  231. $this->assertTrue(false, "can't find expected file '" . $expectedFile . "' in trash bin");
  232. }
  233. }
  234. }
  235. /**
  236. * @param OCP\Files\FileInfo[] $files
  237. * @param string $trashRoot
  238. * @param integer $expireDate
  239. */
  240. private function manipulateDeleteTime($files, $trashRoot, $expireDate) {
  241. $counter = 0;
  242. foreach ($files as &$file) {
  243. // modify every second file
  244. $counter = ($counter + 1) % 2;
  245. if ($counter === 1) {
  246. $source = $trashRoot . '/files/' . $file['name'] . '.d' . $file['mtime'];
  247. $target = \OC\Files\Filesystem::normalizePath($trashRoot . '/files/' . $file['name'] . '.d' . $expireDate);
  248. $this->rootView->rename($source, $target);
  249. $file['mtime'] = $expireDate;
  250. }
  251. }
  252. return \OCA\Files\Helper::sortFiles($files, 'mtime');
  253. }
  254. /**
  255. * test expiration of old files in the trash bin until the max size
  256. * of the trash bin is met again
  257. */
  258. public function testExpireOldFilesUtilLimitsAreMet() {
  259. // create some files
  260. \OC\Files\Filesystem::file_put_contents('file1.txt', 'file1');
  261. \OC\Files\Filesystem::file_put_contents('file2.txt', 'file2');
  262. \OC\Files\Filesystem::file_put_contents('file3.txt', 'file3');
  263. // delete them so that they end up in the trash bin
  264. \OC\Files\Filesystem::unlink('file3.txt');
  265. sleep(1); // make sure that every file has a unique mtime
  266. \OC\Files\Filesystem::unlink('file2.txt');
  267. sleep(1); // make sure that every file has a unique mtime
  268. \OC\Files\Filesystem::unlink('file1.txt');
  269. //make sure that files are in the trash bin
  270. $filesInTrash = OCA\Files_Trashbin\Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1, 'mtime');
  271. $this->assertSame(3, count($filesInTrash));
  272. $testClass = new TrashbinForTesting();
  273. $sizeOfDeletedFiles = $testClass->dummyDeleteFiles($filesInTrash, -8);
  274. // the two oldest files (file3.txt and file2.txt) should be deleted
  275. $this->assertSame(10, $sizeOfDeletedFiles);
  276. $newTrashContent = OCA\Files_Trashbin\Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1);
  277. $this->assertSame(1, count($newTrashContent));
  278. $element = reset($newTrashContent);
  279. $this->assertSame('file1.txt', $element['name']);
  280. }
  281. /**
  282. * Test restoring a file
  283. */
  284. public function testRestoreFileInRoot() {
  285. $userFolder = \OC::$server->getUserFolder();
  286. $file = $userFolder->newFile('file1.txt');
  287. $file->putContent('foo');
  288. $this->assertTrue($userFolder->nodeExists('file1.txt'));
  289. $file->delete();
  290. $this->assertFalse($userFolder->nodeExists('file1.txt'));
  291. $filesInTrash = OCA\Files_Trashbin\Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1, 'mtime');
  292. $this->assertCount(1, $filesInTrash);
  293. /** @var \OCP\Files\FileInfo */
  294. $trashedFile = $filesInTrash[0];
  295. $this->assertTrue(
  296. OCA\Files_Trashbin\Trashbin::restore(
  297. 'file1.txt.d' . $trashedFile->getMtime(),
  298. $trashedFile->getName(),
  299. $trashedFile->getMtime()
  300. )
  301. );
  302. $file = $userFolder->get('file1.txt');
  303. $this->assertEquals('foo', $file->getContent());
  304. }
  305. /**
  306. * Test restoring a file in subfolder
  307. */
  308. public function testRestoreFileInSubfolder() {
  309. $userFolder = \OC::$server->getUserFolder();
  310. $folder = $userFolder->newFolder('folder');
  311. $file = $folder->newFile('file1.txt');
  312. $file->putContent('foo');
  313. $this->assertTrue($userFolder->nodeExists('folder/file1.txt'));
  314. $file->delete();
  315. $this->assertFalse($userFolder->nodeExists('folder/file1.txt'));
  316. $filesInTrash = OCA\Files_Trashbin\Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1, 'mtime');
  317. $this->assertCount(1, $filesInTrash);
  318. /** @var \OCP\Files\FileInfo */
  319. $trashedFile = $filesInTrash[0];
  320. $this->assertTrue(
  321. OCA\Files_Trashbin\Trashbin::restore(
  322. 'file1.txt.d' . $trashedFile->getMtime(),
  323. $trashedFile->getName(),
  324. $trashedFile->getMtime()
  325. )
  326. );
  327. $file = $userFolder->get('folder/file1.txt');
  328. $this->assertEquals('foo', $file->getContent());
  329. }
  330. /**
  331. * Test restoring a folder
  332. */
  333. public function testRestoreFolder() {
  334. $userFolder = \OC::$server->getUserFolder();
  335. $folder = $userFolder->newFolder('folder');
  336. $file = $folder->newFile('file1.txt');
  337. $file->putContent('foo');
  338. $this->assertTrue($userFolder->nodeExists('folder'));
  339. $folder->delete();
  340. $this->assertFalse($userFolder->nodeExists('folder'));
  341. $filesInTrash = OCA\Files_Trashbin\Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1, 'mtime');
  342. $this->assertCount(1, $filesInTrash);
  343. /** @var \OCP\Files\FileInfo */
  344. $trashedFolder = $filesInTrash[0];
  345. $this->assertTrue(
  346. OCA\Files_Trashbin\Trashbin::restore(
  347. 'folder.d' . $trashedFolder->getMtime(),
  348. $trashedFolder->getName(),
  349. $trashedFolder->getMtime()
  350. )
  351. );
  352. $file = $userFolder->get('folder/file1.txt');
  353. $this->assertEquals('foo', $file->getContent());
  354. }
  355. /**
  356. * Test restoring a file from inside a trashed folder
  357. */
  358. public function testRestoreFileFromTrashedSubfolder() {
  359. $userFolder = \OC::$server->getUserFolder();
  360. $folder = $userFolder->newFolder('folder');
  361. $file = $folder->newFile('file1.txt');
  362. $file->putContent('foo');
  363. $this->assertTrue($userFolder->nodeExists('folder'));
  364. $folder->delete();
  365. $this->assertFalse($userFolder->nodeExists('folder'));
  366. $filesInTrash = OCA\Files_Trashbin\Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1, 'mtime');
  367. $this->assertCount(1, $filesInTrash);
  368. /** @var \OCP\Files\FileInfo */
  369. $trashedFile = $filesInTrash[0];
  370. $this->assertTrue(
  371. OCA\Files_Trashbin\Trashbin::restore(
  372. 'folder.d' . $trashedFile->getMtime() . '/file1.txt',
  373. 'file1.txt',
  374. $trashedFile->getMtime()
  375. )
  376. );
  377. $file = $userFolder->get('file1.txt');
  378. $this->assertEquals('foo', $file->getContent());
  379. }
  380. /**
  381. * Test restoring a file whenever the source folder was removed.
  382. * The file should then land in the root.
  383. */
  384. public function testRestoreFileWithMissingSourceFolder() {
  385. $userFolder = \OC::$server->getUserFolder();
  386. $folder = $userFolder->newFolder('folder');
  387. $file = $folder->newFile('file1.txt');
  388. $file->putContent('foo');
  389. $this->assertTrue($userFolder->nodeExists('folder/file1.txt'));
  390. $file->delete();
  391. $this->assertFalse($userFolder->nodeExists('folder/file1.txt'));
  392. $filesInTrash = OCA\Files_Trashbin\Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1, 'mtime');
  393. $this->assertCount(1, $filesInTrash);
  394. /** @var \OCP\Files\FileInfo */
  395. $trashedFile = $filesInTrash[0];
  396. // delete source folder
  397. $folder->delete();
  398. $this->assertTrue(
  399. OCA\Files_Trashbin\Trashbin::restore(
  400. 'file1.txt.d' . $trashedFile->getMtime(),
  401. $trashedFile->getName(),
  402. $trashedFile->getMtime()
  403. )
  404. );
  405. $file = $userFolder->get('file1.txt');
  406. $this->assertEquals('foo', $file->getContent());
  407. }
  408. /**
  409. * Test restoring a file in the root folder whenever there is another file
  410. * with the same name in the root folder
  411. */
  412. public function testRestoreFileDoesNotOverwriteExistingInRoot() {
  413. $userFolder = \OC::$server->getUserFolder();
  414. $file = $userFolder->newFile('file1.txt');
  415. $file->putContent('foo');
  416. $this->assertTrue($userFolder->nodeExists('file1.txt'));
  417. $file->delete();
  418. $this->assertFalse($userFolder->nodeExists('file1.txt'));
  419. $filesInTrash = OCA\Files_Trashbin\Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1, 'mtime');
  420. $this->assertCount(1, $filesInTrash);
  421. /** @var \OCP\Files\FileInfo */
  422. $trashedFile = $filesInTrash[0];
  423. // create another file
  424. $file = $userFolder->newFile('file1.txt');
  425. $file->putContent('bar');
  426. $this->assertTrue(
  427. OCA\Files_Trashbin\Trashbin::restore(
  428. 'file1.txt.d' . $trashedFile->getMtime(),
  429. $trashedFile->getName(),
  430. $trashedFile->getMtime()
  431. )
  432. );
  433. $anotherFile = $userFolder->get('file1.txt');
  434. $this->assertEquals('bar', $anotherFile->getContent());
  435. $restoredFile = $userFolder->get('file1 (restored).txt');
  436. $this->assertEquals('foo', $restoredFile->getContent());
  437. }
  438. /**
  439. * Test restoring a file whenever there is another file
  440. * with the same name in the source folder
  441. */
  442. public function testRestoreFileDoesNotOverwriteExistingInSubfolder() {
  443. $userFolder = \OC::$server->getUserFolder();
  444. $folder = $userFolder->newFolder('folder');
  445. $file = $folder->newFile('file1.txt');
  446. $file->putContent('foo');
  447. $this->assertTrue($userFolder->nodeExists('folder/file1.txt'));
  448. $file->delete();
  449. $this->assertFalse($userFolder->nodeExists('folder/file1.txt'));
  450. $filesInTrash = OCA\Files_Trashbin\Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1, 'mtime');
  451. $this->assertCount(1, $filesInTrash);
  452. /** @var \OCP\Files\FileInfo */
  453. $trashedFile = $filesInTrash[0];
  454. // create another file
  455. $file = $folder->newFile('file1.txt');
  456. $file->putContent('bar');
  457. $this->assertTrue(
  458. OCA\Files_Trashbin\Trashbin::restore(
  459. 'file1.txt.d' . $trashedFile->getMtime(),
  460. $trashedFile->getName(),
  461. $trashedFile->getMtime()
  462. )
  463. );
  464. $anotherFile = $userFolder->get('folder/file1.txt');
  465. $this->assertEquals('bar', $anotherFile->getContent());
  466. $restoredFile = $userFolder->get('folder/file1 (restored).txt');
  467. $this->assertEquals('foo', $restoredFile->getContent());
  468. }
  469. /**
  470. * Test restoring a non-existing file from trashbin, returns false
  471. */
  472. public function testRestoreUnexistingFile() {
  473. $this->assertFalse(
  474. OCA\Files_Trashbin\Trashbin::restore(
  475. 'unexist.txt.d123456',
  476. 'unexist.txt',
  477. '123456'
  478. )
  479. );
  480. }
  481. /**
  482. * Test restoring a file into a read-only folder, will restore
  483. * the file to root instead
  484. */
  485. public function testRestoreFileIntoReadOnlySourceFolder() {
  486. $userFolder = \OC::$server->getUserFolder();
  487. $folder = $userFolder->newFolder('folder');
  488. $file = $folder->newFile('file1.txt');
  489. $file->putContent('foo');
  490. $this->assertTrue($userFolder->nodeExists('folder/file1.txt'));
  491. $file->delete();
  492. $this->assertFalse($userFolder->nodeExists('folder/file1.txt'));
  493. $filesInTrash = OCA\Files_Trashbin\Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1, 'mtime');
  494. $this->assertCount(1, $filesInTrash);
  495. /** @var \OCP\Files\FileInfo */
  496. $trashedFile = $filesInTrash[0];
  497. // delete source folder
  498. [$storage, $internalPath] = $this->rootView->resolvePath('/' . self::TEST_TRASHBIN_USER1 . '/files/folder');
  499. if ($storage instanceof \OC\Files\Storage\Local) {
  500. $folderAbsPath = $storage->getSourcePath($internalPath);
  501. // make folder read-only
  502. chmod($folderAbsPath, 0555);
  503. $this->assertTrue(
  504. OCA\Files_Trashbin\Trashbin::restore(
  505. 'file1.txt.d' . $trashedFile->getMtime(),
  506. $trashedFile->getName(),
  507. $trashedFile->getMtime()
  508. )
  509. );
  510. $file = $userFolder->get('file1.txt');
  511. $this->assertEquals('foo', $file->getContent());
  512. chmod($folderAbsPath, 0755);
  513. }
  514. }
  515. /**
  516. * @param string $user
  517. * @param bool $create
  518. */
  519. public static function loginHelper($user, $create = false) {
  520. if ($create) {
  521. try {
  522. \OC::$server->getUserManager()->createUser($user, $user);
  523. } catch (\Exception $e) { // catch username is already being used from previous aborted runs
  524. }
  525. }
  526. \OC_Util::tearDownFS();
  527. \OC_User::setUserId('');
  528. \OC\Files\Filesystem::tearDown();
  529. \OC_User::setUserId($user);
  530. \OC_Util::setupFS($user);
  531. \OC::$server->getUserFolder($user);
  532. }
  533. }
  534. // just a dummy class to make protected methods available for testing
  535. class TrashbinForTesting extends \OCA\Files_Trashbin\Trashbin {
  536. /**
  537. * @param OCP\Files\FileInfo[] $files
  538. * @param integer $limit
  539. */
  540. public function dummyDeleteExpiredFiles($files) {
  541. // dummy value for $retention_obligation because it is not needed here
  542. return parent::deleteExpiredFiles($files, TrashbinTest::TEST_TRASHBIN_USER1);
  543. }
  544. /**
  545. * @param OCP\Files\FileInfo[] $files
  546. * @param integer $availableSpace
  547. */
  548. public function dummyDeleteFiles($files, $availableSpace) {
  549. return parent::deleteFiles($files, TrashbinTest::TEST_TRASHBIN_USER1, $availableSpace);
  550. }
  551. }