TrashbinTest.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Clark Tomlinson <fallen013@gmail.com>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Robin Appelman <robin@icewind.nl>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Thomas Müller <thomas.mueller@tmit.eu>
  12. * @author Vincent Petry <pvince81@owncloud.com>
  13. *
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. use OCA\Files_Trashbin\Tests;
  30. /**
  31. * Class Test_Encryption
  32. *
  33. * @group DB
  34. */
  35. class TrashbinTest extends \Test\TestCase {
  36. const TEST_TRASHBIN_USER1 = "test-trashbin-user1";
  37. const TEST_TRASHBIN_USER2 = "test-trashbin-user2";
  38. private $trashRoot1;
  39. private $trashRoot2;
  40. private static $rememberRetentionObligation;
  41. /**
  42. * @var bool
  43. */
  44. private static $trashBinStatus;
  45. /**
  46. * @var \OC\Files\View
  47. */
  48. private $rootView;
  49. public static function setUpBeforeClass() {
  50. parent::setUpBeforeClass();
  51. $appManager = \OC::$server->getAppManager();
  52. self::$trashBinStatus = $appManager->isEnabledForUser('files_trashbin');
  53. // reset backend
  54. \OC_User::clearBackends();
  55. \OC_User::useBackend('database');
  56. // clear share hooks
  57. \OC_Hook::clear('OCP\\Share');
  58. \OC::registerShareHooks();
  59. $application = new \OCA\Files_Sharing\AppInfo\Application();
  60. $application->registerMountProviders();
  61. //disable encryption
  62. \OC::$server->getAppManager()->disableApp('encryption');
  63. $config = \OC::$server->getConfig();
  64. //configure trashbin
  65. self::$rememberRetentionObligation = $config->getSystemValue('trashbin_retention_obligation', \OCA\Files_Trashbin\Expiration::DEFAULT_RETENTION_OBLIGATION);
  66. $config->setSystemValue('trashbin_retention_obligation', 'auto, 2');
  67. // register hooks
  68. \OCA\Files_Trashbin\Trashbin::registerHooks();
  69. // create test user
  70. self::loginHelper(self::TEST_TRASHBIN_USER2, true);
  71. self::loginHelper(self::TEST_TRASHBIN_USER1, true);
  72. }
  73. public static function tearDownAfterClass() {
  74. // cleanup test user
  75. $user = \OC::$server->getUserManager()->get(self::TEST_TRASHBIN_USER1);
  76. if ($user !== null) {
  77. $user->delete();
  78. }
  79. \OC::$server->getConfig()->setSystemValue('trashbin_retention_obligation', self::$rememberRetentionObligation);
  80. \OC_Hook::clear();
  81. \OC\Files\Filesystem::getLoader()->removeStorageWrapper('oc_trashbin');
  82. if (self::$trashBinStatus) {
  83. \OC::$server->getAppManager()->enableApp('files_trashbin');
  84. }
  85. parent::tearDownAfterClass();
  86. }
  87. protected function setUp() {
  88. parent::setUp();
  89. \OC::$server->getAppManager()->enableApp('files_trashbin');
  90. $config = \OC::$server->getConfig();
  91. $mockConfig = $this->createMock(\OCP\IConfig::class);
  92. $mockConfig->expects($this->any())
  93. ->method('getSystemValue')
  94. ->will($this->returnCallback(function ($key, $default) use ($config) {
  95. if ($key === 'filesystem_check_changes') {
  96. return \OC\Files\Cache\Watcher::CHECK_ONCE;
  97. } else {
  98. return $config->getSystemValue($key, $default);
  99. }
  100. }));
  101. $this->overwriteService('AllConfig', $mockConfig);
  102. $this->trashRoot1 = '/' . self::TEST_TRASHBIN_USER1 . '/files_trashbin';
  103. $this->trashRoot2 = '/' . self::TEST_TRASHBIN_USER2 . '/files_trashbin';
  104. $this->rootView = new \OC\Files\View();
  105. self::loginHelper(self::TEST_TRASHBIN_USER1);
  106. }
  107. protected function tearDown() {
  108. $this->restoreService('AllConfig');
  109. // disable trashbin to be able to properly clean up
  110. \OC::$server->getAppManager()->disableApp('files_trashbin');
  111. $this->rootView->deleteAll('/' . self::TEST_TRASHBIN_USER1 . '/files');
  112. $this->rootView->deleteAll('/' . self::TEST_TRASHBIN_USER2 . '/files');
  113. $this->rootView->deleteAll($this->trashRoot1);
  114. $this->rootView->deleteAll($this->trashRoot2);
  115. // clear trash table
  116. $connection = \OC::$server->getDatabaseConnection();
  117. $connection->executeUpdate('DELETE FROM `*PREFIX*files_trash`');
  118. parent::tearDown();
  119. }
  120. /**
  121. * test expiration of files older then the max storage time defined for the trash
  122. */
  123. public function testExpireOldFiles() {
  124. $currentTime = time();
  125. $expireAt = $currentTime - 2 * 24 * 60 * 60;
  126. $expiredDate = $currentTime - 3 * 24 * 60 * 60;
  127. // create some files
  128. \OC\Files\Filesystem::file_put_contents('file1.txt', 'file1');
  129. \OC\Files\Filesystem::file_put_contents('file2.txt', 'file2');
  130. \OC\Files\Filesystem::file_put_contents('file3.txt', 'file3');
  131. // delete them so that they end up in the trash bin
  132. \OC\Files\Filesystem::unlink('file1.txt');
  133. \OC\Files\Filesystem::unlink('file2.txt');
  134. \OC\Files\Filesystem::unlink('file3.txt');
  135. //make sure that files are in the trash bin
  136. $filesInTrash = OCA\Files_Trashbin\Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1, 'name');
  137. $this->assertSame(3, count($filesInTrash));
  138. // every second file will get a date in the past so that it will get expired
  139. $manipulatedList = $this->manipulateDeleteTime($filesInTrash, $this->trashRoot1, $expiredDate);
  140. $testClass = new TrashbinForTesting();
  141. list($sizeOfDeletedFiles, $count) = $testClass->dummyDeleteExpiredFiles($manipulatedList, $expireAt);
  142. $this->assertSame(10, $sizeOfDeletedFiles);
  143. $this->assertSame(2, $count);
  144. // only file2.txt should be left
  145. $remainingFiles = array_slice($manipulatedList, $count);
  146. $this->assertSame(1, count($remainingFiles));
  147. $remainingFile = reset($remainingFiles);
  148. // TODO: failing test
  149. #$this->assertSame('file2.txt', $remainingFile['name']);
  150. // check that file1.txt and file3.txt was really deleted
  151. $newTrashContent = OCA\Files_Trashbin\Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1);
  152. $this->assertSame(1, count($newTrashContent));
  153. $element = reset($newTrashContent);
  154. // TODO: failing test
  155. #$this->assertSame('file2.txt', $element['name']);
  156. }
  157. /**
  158. * test expiration of files older then the max storage time defined for the trash
  159. * in this test we delete a shared file and check if both trash bins, the one from
  160. * the owner of the file and the one from the user who deleted the file get expired
  161. * correctly
  162. */
  163. public function testExpireOldFilesShared() {
  164. $currentTime = time();
  165. $folder = "trashTest-" . $currentTime . '/';
  166. $expiredDate = $currentTime - 3 * 24 * 60 * 60;
  167. // create some files
  168. \OC\Files\Filesystem::mkdir($folder);
  169. \OC\Files\Filesystem::file_put_contents($folder . 'user1-1.txt', 'file1');
  170. \OC\Files\Filesystem::file_put_contents($folder . 'user1-2.txt', 'file2');
  171. \OC\Files\Filesystem::file_put_contents($folder . 'user1-3.txt', 'file3');
  172. \OC\Files\Filesystem::file_put_contents($folder . 'user1-4.txt', 'file4');
  173. //share user1-4.txt with user2
  174. $node = \OC::$server->getUserFolder(self::TEST_TRASHBIN_USER1)->get($folder);
  175. $share = \OC::$server->getShareManager()->newShare();
  176. $share->setShareType(\OCP\Share::SHARE_TYPE_USER)
  177. ->setNode($node)
  178. ->setSharedBy(self::TEST_TRASHBIN_USER1)
  179. ->setSharedWith(self::TEST_TRASHBIN_USER2)
  180. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  181. \OC::$server->getShareManager()->createShare($share);
  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, array('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, array('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. list($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, $limit) {
  541. // dummy value for $retention_obligation because it is not needed here
  542. return parent::deleteExpiredFiles($files, TrashbinTest::TEST_TRASHBIN_USER1, $limit, 0);
  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. }