StorageTest.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_Trashbin\Tests;
  8. use OC\Files\Filesystem;
  9. use OC\Files\Storage\Common;
  10. use OC\Files\Storage\Local;
  11. use OC\Files\Storage\Temporary;
  12. use OCA\Files_Trashbin\AppInfo\Application;
  13. use OCA\Files_Trashbin\Events\MoveToTrashEvent;
  14. use OCA\Files_Trashbin\Storage;
  15. use OCA\Files_Trashbin\Trash\ITrashManager;
  16. use OCP\AppFramework\Bootstrap\IBootContext;
  17. use OCP\AppFramework\Utility\ITimeFactory;
  18. use OCP\EventDispatcher\IEventDispatcher;
  19. use OCP\Files\Cache\ICache;
  20. use OCP\Files\Folder;
  21. use OCP\Files\IRootFolder;
  22. use OCP\Files\Node;
  23. use OCP\Files\Storage\IStorage;
  24. use OCP\IUserManager;
  25. use OCP\Lock\ILockingProvider;
  26. use OCP\Share\IShare;
  27. use Psr\Log\LoggerInterface;
  28. use Test\Traits\MountProviderTrait;
  29. class TemporaryNoCross extends Temporary {
  30. public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime = null) {
  31. return Common::copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime);
  32. }
  33. public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
  34. return Common::moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
  35. }
  36. }
  37. /**
  38. * Class Storage
  39. *
  40. * @group DB
  41. *
  42. * @package OCA\Files_Trashbin\Tests
  43. */
  44. class StorageTest extends \Test\TestCase {
  45. use MountProviderTrait;
  46. /**
  47. * @var string
  48. */
  49. private $user;
  50. /**
  51. * @var \OC\Files\View
  52. */
  53. private $rootView;
  54. /**
  55. * @var \OC\Files\View
  56. */
  57. private $userView;
  58. // 239 chars so appended timestamp of 12 chars will exceed max length of 250 chars
  59. private const LONG_FILENAME = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.txt';
  60. // 250 chars
  61. private const MAX_FILENAME = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.txt';
  62. protected function setUp(): void {
  63. parent::setUp();
  64. \OC_Hook::clear();
  65. \OC::$server->boot();
  66. // register trashbin hooks
  67. $trashbinApp = new Application();
  68. $trashbinApp->boot($this->createMock(IBootContext::class));
  69. $this->user = $this->getUniqueId('user');
  70. \OC::$server->getUserManager()->createUser($this->user, $this->user);
  71. // this will setup the FS
  72. $this->loginAsUser($this->user);
  73. \OCA\Files_Trashbin\Storage::setupStorage();
  74. $this->rootView = new \OC\Files\View('/');
  75. $this->userView = new \OC\Files\View('/' . $this->user . '/files/');
  76. $this->userView->file_put_contents('test.txt', 'foo');
  77. $this->userView->file_put_contents(static::LONG_FILENAME, 'foo');
  78. $this->userView->file_put_contents(static::MAX_FILENAME, 'foo');
  79. $this->userView->mkdir('folder');
  80. $this->userView->file_put_contents('folder/inside.txt', 'bar');
  81. }
  82. protected function tearDown(): void {
  83. \OC\Files\Filesystem::getLoader()->removeStorageWrapper('oc_trashbin');
  84. $this->logout();
  85. $user = \OC::$server->getUserManager()->get($this->user);
  86. if ($user !== null) {
  87. $user->delete();
  88. }
  89. \OC_Hook::clear();
  90. parent::tearDown();
  91. }
  92. /**
  93. * Test that deleting a file puts it into the trashbin.
  94. */
  95. public function testSingleStorageDeleteFile() {
  96. $this->assertTrue($this->userView->file_exists('test.txt'));
  97. $this->userView->unlink('test.txt');
  98. [$storage,] = $this->userView->resolvePath('test.txt');
  99. $storage->getScanner()->scan(''); // make sure we check the storage
  100. $this->assertFalse($this->userView->getFileInfo('test.txt'));
  101. // check if file is in trashbin
  102. $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/files/');
  103. $this->assertEquals(1, count($results));
  104. $name = $results[0]->getName();
  105. $this->assertEquals('test.txt', substr($name, 0, strrpos($name, '.')));
  106. }
  107. /**
  108. * Test that deleting a folder puts it into the trashbin.
  109. */
  110. public function testSingleStorageDeleteFolder() {
  111. $this->assertTrue($this->userView->file_exists('folder/inside.txt'));
  112. $this->userView->rmdir('folder');
  113. [$storage,] = $this->userView->resolvePath('folder/inside.txt');
  114. $storage->getScanner()->scan(''); // make sure we check the storage
  115. $this->assertFalse($this->userView->getFileInfo('folder'));
  116. // check if folder is in trashbin
  117. $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/files/');
  118. $this->assertEquals(1, count($results));
  119. $name = $results[0]->getName();
  120. $this->assertEquals('folder', substr($name, 0, strrpos($name, '.')));
  121. $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/files/' . $name . '/');
  122. $this->assertEquals(1, count($results));
  123. $name = $results[0]->getName();
  124. $this->assertEquals('inside.txt', $name);
  125. }
  126. /**
  127. * Test that deleting a file with a long filename puts it into the trashbin.
  128. */
  129. public function testSingleStorageDeleteLongFilename() {
  130. $truncatedFilename = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.txt';
  131. $this->assertTrue($this->userView->file_exists(static::LONG_FILENAME));
  132. $this->userView->unlink(static::LONG_FILENAME);
  133. [$storage,] = $this->userView->resolvePath(static::LONG_FILENAME);
  134. $storage->getScanner()->scan(''); // make sure we check the storage
  135. $this->assertFalse($this->userView->getFileInfo(static::LONG_FILENAME));
  136. // check if file is in trashbin
  137. $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/files/');
  138. $this->assertEquals(1, count($results));
  139. $name = $results[0]->getName();
  140. $this->assertEquals($truncatedFilename, substr($name, 0, strrpos($name, '.')));
  141. }
  142. /**
  143. * Test that deleting a file with the max filename length puts it into the trashbin.
  144. */
  145. public function testSingleStorageDeleteMaxLengthFilename() {
  146. $truncatedFilename = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.txt';
  147. $this->assertTrue($this->userView->file_exists(static::MAX_FILENAME));
  148. $this->userView->unlink(static::MAX_FILENAME);
  149. [$storage,] = $this->userView->resolvePath(static::MAX_FILENAME);
  150. $storage->getScanner()->scan(''); // make sure we check the storage
  151. $this->assertFalse($this->userView->getFileInfo(static::MAX_FILENAME));
  152. // check if file is in trashbin
  153. $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/files/');
  154. $this->assertEquals(1, count($results));
  155. $name = $results[0]->getName();
  156. $this->assertEquals($truncatedFilename, substr($name, 0, strrpos($name, '.')));
  157. }
  158. /**
  159. * Test that deleting a file from another mounted storage properly
  160. * lands in the trashbin. This is a cross-storage situation because
  161. * the trashbin folder is in the root storage while the mounted one
  162. * isn't.
  163. */
  164. public function testCrossStorageDeleteFile() {
  165. $storage2 = new Temporary([]);
  166. \OC\Files\Filesystem::mount($storage2, [], $this->user . '/files/substorage');
  167. $this->userView->file_put_contents('substorage/subfile.txt', 'foo');
  168. $storage2->getScanner()->scan('');
  169. $this->assertTrue($storage2->file_exists('subfile.txt'));
  170. $this->userView->unlink('substorage/subfile.txt');
  171. $storage2->getScanner()->scan('');
  172. $this->assertFalse($this->userView->getFileInfo('substorage/subfile.txt'));
  173. $this->assertFalse($storage2->file_exists('subfile.txt'));
  174. // check if file is in trashbin
  175. $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/files');
  176. $this->assertEquals(1, count($results));
  177. $name = $results[0]->getName();
  178. $this->assertEquals('subfile.txt', substr($name, 0, strrpos($name, '.')));
  179. }
  180. /**
  181. * Test that deleting a folder from another mounted storage properly
  182. * lands in the trashbin. This is a cross-storage situation because
  183. * the trashbin folder is in the root storage while the mounted one
  184. * isn't.
  185. */
  186. public function testCrossStorageDeleteFolder() {
  187. $storage2 = new Temporary([]);
  188. \OC\Files\Filesystem::mount($storage2, [], $this->user . '/files/substorage');
  189. $this->userView->mkdir('substorage/folder');
  190. $this->userView->file_put_contents('substorage/folder/subfile.txt', 'bar');
  191. $storage2->getScanner()->scan('');
  192. $this->assertTrue($storage2->file_exists('folder/subfile.txt'));
  193. $this->userView->rmdir('substorage/folder');
  194. $storage2->getScanner()->scan('');
  195. $this->assertFalse($this->userView->getFileInfo('substorage/folder'));
  196. $this->assertFalse($storage2->file_exists('folder/subfile.txt'));
  197. // check if folder is in trashbin
  198. $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/files');
  199. $this->assertEquals(1, count($results));
  200. $name = $results[0]->getName();
  201. $this->assertEquals('folder', substr($name, 0, strrpos($name, '.')));
  202. $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/files/' . $name . '/');
  203. $this->assertEquals(1, count($results));
  204. $name = $results[0]->getName();
  205. $this->assertEquals('subfile.txt', $name);
  206. }
  207. /**
  208. * Test that deleted versions properly land in the trashbin.
  209. */
  210. public function testDeleteVersionsOfFile() {
  211. // trigger a version (multiple would not work because of the expire logic)
  212. $this->userView->file_put_contents('test.txt', 'v1');
  213. $results = $this->rootView->getDirectoryContent($this->user . '/files_versions/');
  214. $this->assertEquals(1, count($results));
  215. $this->userView->unlink('test.txt');
  216. // rescan trash storage
  217. [$rootStorage,] = $this->rootView->resolvePath($this->user . '/files_trashbin');
  218. $rootStorage->getScanner()->scan('');
  219. // check if versions are in trashbin
  220. $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/versions');
  221. $this->assertEquals(1, count($results));
  222. $name = $results[0]->getName();
  223. $this->assertEquals('test.txt.v', substr($name, 0, strlen('test.txt.v')));
  224. // versions deleted
  225. $results = $this->rootView->getDirectoryContent($this->user . '/files_versions/');
  226. $this->assertEquals(0, count($results));
  227. }
  228. /**
  229. * Test that deleted versions properly land in the trashbin.
  230. */
  231. public function testDeleteVersionsOfFolder() {
  232. // trigger a version (multiple would not work because of the expire logic)
  233. $this->userView->file_put_contents('folder/inside.txt', 'v1');
  234. $results = $this->rootView->getDirectoryContent($this->user . '/files_versions/folder/');
  235. $this->assertEquals(1, count($results));
  236. $this->userView->rmdir('folder');
  237. // rescan trash storage
  238. [$rootStorage,] = $this->rootView->resolvePath($this->user . '/files_trashbin');
  239. $rootStorage->getScanner()->scan('');
  240. // check if versions are in trashbin
  241. $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/versions');
  242. $this->assertEquals(1, count($results));
  243. $name = $results[0]->getName();
  244. $this->assertEquals('folder.d', substr($name, 0, strlen('folder.d')));
  245. // check if versions are in trashbin
  246. $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/versions/' . $name . '/');
  247. $this->assertEquals(1, count($results));
  248. $name = $results[0]->getName();
  249. $this->assertEquals('inside.txt.v', substr($name, 0, strlen('inside.txt.v')));
  250. // versions deleted
  251. $results = $this->rootView->getDirectoryContent($this->user . '/files_versions/folder/');
  252. $this->assertEquals(0, count($results));
  253. }
  254. /**
  255. * Test that deleted versions properly land in the trashbin when deleting as share recipient.
  256. */
  257. public function testDeleteVersionsOfFileAsRecipient() {
  258. $this->userView->mkdir('share');
  259. // trigger a version (multiple would not work because of the expire logic)
  260. $this->userView->file_put_contents('share/test.txt', 'v1');
  261. $this->userView->file_put_contents('share/test.txt', 'v2');
  262. $results = $this->rootView->getDirectoryContent($this->user . '/files_versions/share/');
  263. $this->assertEquals(1, count($results));
  264. $recipientUser = $this->getUniqueId('recipient_');
  265. \OC::$server->getUserManager()->createUser($recipientUser, $recipientUser);
  266. $node = \OC::$server->getUserFolder($this->user)->get('share');
  267. $share = \OC::$server->getShareManager()->newShare();
  268. $share->setNode($node)
  269. ->setShareType(IShare::TYPE_USER)
  270. ->setSharedBy($this->user)
  271. ->setSharedWith($recipientUser)
  272. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  273. $share = \OC::$server->getShareManager()->createShare($share);
  274. \OC::$server->getShareManager()->acceptShare($share, $recipientUser);
  275. $this->loginAsUser($recipientUser);
  276. // delete as recipient
  277. $recipientView = new \OC\Files\View('/' . $recipientUser . '/files');
  278. $recipientView->unlink('share/test.txt');
  279. // rescan trash storage for both users
  280. [$rootStorage,] = $this->rootView->resolvePath($this->user . '/files_trashbin');
  281. $rootStorage->getScanner()->scan('');
  282. // check if versions are in trashbin for both users
  283. $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/versions');
  284. $this->assertEquals(1, count($results), 'Versions in owner\'s trashbin');
  285. $name = $results[0]->getName();
  286. $this->assertEquals('test.txt.v', substr($name, 0, strlen('test.txt.v')));
  287. $results = $this->rootView->getDirectoryContent($recipientUser . '/files_trashbin/versions');
  288. $this->assertEquals(1, count($results), 'Versions in recipient\'s trashbin');
  289. $name = $results[0]->getName();
  290. $this->assertEquals('test.txt.v', substr($name, 0, strlen('test.txt.v')));
  291. // versions deleted
  292. $results = $this->rootView->getDirectoryContent($this->user . '/files_versions/share/');
  293. $this->assertEquals(0, count($results));
  294. }
  295. /**
  296. * Test that deleted versions properly land in the trashbin when deleting as share recipient.
  297. */
  298. public function testDeleteVersionsOfFolderAsRecipient() {
  299. $this->userView->mkdir('share');
  300. $this->userView->mkdir('share/folder');
  301. // trigger a version (multiple would not work because of the expire logic)
  302. $this->userView->file_put_contents('share/folder/test.txt', 'v1');
  303. $this->userView->file_put_contents('share/folder/test.txt', 'v2');
  304. $results = $this->rootView->getDirectoryContent($this->user . '/files_versions/share/folder/');
  305. $this->assertEquals(1, count($results));
  306. $recipientUser = $this->getUniqueId('recipient_');
  307. \OC::$server->getUserManager()->createUser($recipientUser, $recipientUser);
  308. $node = \OC::$server->getUserFolder($this->user)->get('share');
  309. $share = \OC::$server->getShareManager()->newShare();
  310. $share->setNode($node)
  311. ->setShareType(IShare::TYPE_USER)
  312. ->setSharedBy($this->user)
  313. ->setSharedWith($recipientUser)
  314. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  315. $share = \OC::$server->getShareManager()->createShare($share);
  316. \OC::$server->getShareManager()->acceptShare($share, $recipientUser);
  317. $this->loginAsUser($recipientUser);
  318. // delete as recipient
  319. $recipientView = new \OC\Files\View('/' . $recipientUser . '/files');
  320. $recipientView->rmdir('share/folder');
  321. // rescan trash storage
  322. [$rootStorage,] = $this->rootView->resolvePath($this->user . '/files_trashbin');
  323. $rootStorage->getScanner()->scan('');
  324. // check if versions are in trashbin for owner
  325. $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/versions');
  326. $this->assertEquals(1, count($results));
  327. $name = $results[0]->getName();
  328. $this->assertEquals('folder.d', substr($name, 0, strlen('folder.d')));
  329. // check if file versions are in trashbin for owner
  330. $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/versions/' . $name . '/');
  331. $this->assertEquals(1, count($results));
  332. $name = $results[0]->getName();
  333. $this->assertEquals('test.txt.v', substr($name, 0, strlen('test.txt.v')));
  334. // check if versions are in trashbin for recipient
  335. $results = $this->rootView->getDirectoryContent($recipientUser . '/files_trashbin/versions');
  336. $this->assertEquals(1, count($results));
  337. $name = $results[0]->getName();
  338. $this->assertEquals('folder.d', substr($name, 0, strlen('folder.d')));
  339. // check if file versions are in trashbin for recipient
  340. $results = $this->rootView->getDirectoryContent($recipientUser . '/files_trashbin/versions/' . $name . '/');
  341. $this->assertEquals(1, count($results));
  342. $name = $results[0]->getName();
  343. $this->assertEquals('test.txt.v', substr($name, 0, strlen('test.txt.v')));
  344. // versions deleted
  345. $results = $this->rootView->getDirectoryContent($recipientUser . '/files_versions/share/folder/');
  346. $this->assertEquals(0, count($results));
  347. }
  348. /**
  349. * Test that versions are not auto-trashed when moving a file between
  350. * storages. This is because rename() between storages would call
  351. * unlink() which should NOT trigger the version deletion logic.
  352. */
  353. public function testKeepFileAndVersionsWhenMovingFileBetweenStorages() {
  354. $storage2 = new Temporary([]);
  355. \OC\Files\Filesystem::mount($storage2, [], $this->user . '/files/substorage');
  356. // trigger a version (multiple would not work because of the expire logic)
  357. $this->userView->file_put_contents('test.txt', 'v1');
  358. $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/files');
  359. $this->assertEquals(0, count($results));
  360. $results = $this->rootView->getDirectoryContent($this->user . '/files_versions/');
  361. $this->assertEquals(1, count($results));
  362. // move to another storage
  363. $this->userView->rename('test.txt', 'substorage/test.txt');
  364. $this->assertTrue($this->userView->file_exists('substorage/test.txt'));
  365. // rescan trash storage
  366. [$rootStorage,] = $this->rootView->resolvePath($this->user . '/files_trashbin');
  367. $rootStorage->getScanner()->scan('');
  368. // versions were moved too
  369. $results = $this->rootView->getDirectoryContent($this->user . '/files_versions/substorage');
  370. $this->assertEquals(1, count($results));
  371. // check that nothing got trashed by the rename's unlink() call
  372. $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/files');
  373. $this->assertEquals(0, count($results));
  374. // check that versions were moved and not trashed
  375. $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/versions/');
  376. $this->assertEquals(0, count($results));
  377. }
  378. /**
  379. * Test that versions are not auto-trashed when moving a file between
  380. * storages. This is because rename() between storages would call
  381. * unlink() which should NOT trigger the version deletion logic.
  382. */
  383. public function testKeepFileAndVersionsWhenMovingFolderBetweenStorages() {
  384. $storage2 = new Temporary([]);
  385. \OC\Files\Filesystem::mount($storage2, [], $this->user . '/files/substorage');
  386. // trigger a version (multiple would not work because of the expire logic)
  387. $this->userView->file_put_contents('folder/inside.txt', 'v1');
  388. $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/files');
  389. $this->assertEquals(0, count($results));
  390. $results = $this->rootView->getDirectoryContent($this->user . '/files_versions/folder/');
  391. $this->assertEquals(1, count($results));
  392. // move to another storage
  393. $this->userView->rename('folder', 'substorage/folder');
  394. $this->assertTrue($this->userView->file_exists('substorage/folder/inside.txt'));
  395. // rescan trash storage
  396. [$rootStorage,] = $this->rootView->resolvePath($this->user . '/files_trashbin');
  397. $rootStorage->getScanner()->scan('');
  398. // versions were moved too
  399. $results = $this->rootView->getDirectoryContent($this->user . '/files_versions/substorage/folder/');
  400. $this->assertEquals(1, count($results));
  401. // check that nothing got trashed by the rename's unlink() call
  402. $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/files');
  403. $this->assertEquals(0, count($results));
  404. // check that versions were moved and not trashed
  405. $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/versions/');
  406. $this->assertEquals(0, count($results));
  407. }
  408. /**
  409. * Delete should fail if the source file can't be deleted.
  410. */
  411. public function testSingleStorageDeleteFileFail() {
  412. /**
  413. * @var \OC\Files\Storage\Temporary | \PHPUnit\Framework\MockObject\MockObject $storage
  414. */
  415. $storage = $this->getMockBuilder('\OC\Files\Storage\Temporary')
  416. ->setConstructorArgs([[]])
  417. ->setMethods(['rename', 'unlink', 'moveFromStorage'])
  418. ->getMock();
  419. $storage->expects($this->any())
  420. ->method('rename')
  421. ->willReturn(false);
  422. $storage->expects($this->any())
  423. ->method('moveFromStorage')
  424. ->willReturn(false);
  425. $storage->expects($this->any())
  426. ->method('unlink')
  427. ->willReturn(false);
  428. $cache = $storage->getCache();
  429. Filesystem::mount($storage, [], '/' . $this->user);
  430. $storage->mkdir('files');
  431. $this->userView->file_put_contents('test.txt', 'foo');
  432. $this->assertTrue($storage->file_exists('files/test.txt'));
  433. $this->assertFalse($this->userView->unlink('test.txt'));
  434. $this->assertTrue($storage->file_exists('files/test.txt'));
  435. $this->assertTrue($cache->inCache('files/test.txt'));
  436. // file should not be in the trashbin
  437. $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/files/');
  438. $this->assertEquals(0, count($results));
  439. }
  440. /**
  441. * Delete should fail if the source folder can't be deleted.
  442. */
  443. public function testSingleStorageDeleteFolderFail() {
  444. /**
  445. * @var \OC\Files\Storage\Temporary | \PHPUnit\Framework\MockObject\MockObject $storage
  446. */
  447. $storage = $this->getMockBuilder('\OC\Files\Storage\Temporary')
  448. ->setConstructorArgs([[]])
  449. ->setMethods(['rename', 'unlink', 'rmdir'])
  450. ->getMock();
  451. $storage->expects($this->any())
  452. ->method('rmdir')
  453. ->willReturn(false);
  454. $cache = $storage->getCache();
  455. Filesystem::mount($storage, [], '/' . $this->user);
  456. $storage->mkdir('files');
  457. $this->userView->mkdir('folder');
  458. $this->userView->file_put_contents('folder/test.txt', 'foo');
  459. $this->assertTrue($storage->file_exists('files/folder/test.txt'));
  460. $this->assertFalse($this->userView->rmdir('files/folder'));
  461. $this->assertTrue($storage->file_exists('files/folder'));
  462. $this->assertTrue($storage->file_exists('files/folder/test.txt'));
  463. $this->assertTrue($cache->inCache('files/folder'));
  464. $this->assertTrue($cache->inCache('files/folder/test.txt'));
  465. // file should not be in the trashbin
  466. $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/files/');
  467. $this->assertEquals(0, count($results));
  468. }
  469. /**
  470. * @dataProvider dataTestShouldMoveToTrash
  471. */
  472. public function testShouldMoveToTrash($mountPoint, $path, $userExists, $appDisablesTrash, $expected) {
  473. $fileID = 1;
  474. $cache = $this->createMock(ICache::class);
  475. $cache->expects($this->any())->method('getId')->willReturn($fileID);
  476. $tmpStorage = $this->getMockBuilder('\OC\Files\Storage\Temporary')
  477. ->disableOriginalConstructor()->getMock($cache);
  478. $tmpStorage->expects($this->any())->method('getCache')->willReturn($cache);
  479. $userManager = $this->getMockBuilder(IUserManager::class)
  480. ->disableOriginalConstructor()->getMock();
  481. $userManager->expects($this->any())
  482. ->method('userExists')->willReturn($userExists);
  483. $logger = $this->getMockBuilder(LoggerInterface::class)->getMock();
  484. $eventDispatcher = $this->createMock(IEventDispatcher::class);
  485. $rootFolder = $this->createMock(IRootFolder::class);
  486. $userFolder = $this->createMock(Folder::class);
  487. $node = $this->getMockBuilder(Node::class)->disableOriginalConstructor()->getMock();
  488. $trashManager = $this->createMock(ITrashManager::class);
  489. $event = $this->getMockBuilder(MoveToTrashEvent::class)->disableOriginalConstructor()->getMock();
  490. $event->expects($this->any())->method('shouldMoveToTrashBin')->willReturn(!$appDisablesTrash);
  491. $userFolder->expects($this->any())->method('getById')->with($fileID)->willReturn([$node]);
  492. $rootFolder->expects($this->any())->method('getUserFolder')->willReturn($userFolder);
  493. $storage = $this->getMockBuilder(Storage::class)
  494. ->setConstructorArgs(
  495. [
  496. ['mountPoint' => $mountPoint, 'storage' => $tmpStorage],
  497. $trashManager,
  498. $userManager,
  499. $logger,
  500. $eventDispatcher,
  501. $rootFolder
  502. ]
  503. )->setMethods(['createMoveToTrashEvent'])->getMock();
  504. $storage->expects($this->any())->method('createMoveToTrashEvent')->with($node)
  505. ->willReturn($event);
  506. $this->assertSame($expected,
  507. $this->invokePrivate($storage, 'shouldMoveToTrash', [$path])
  508. );
  509. }
  510. public function dataTestShouldMoveToTrash() {
  511. return [
  512. ['/schiesbn/', '/files/test.txt', true, false, true],
  513. ['/schiesbn/', '/files/test.txt', false, false, false],
  514. ['/schiesbn/', '/test.txt', true, false, false],
  515. ['/schiesbn/', '/test.txt', false, false, false],
  516. // other apps disables the trashbin
  517. ['/schiesbn/', '/files/test.txt', true, true, false],
  518. ['/schiesbn/', '/files/test.txt', false, true, false],
  519. ];
  520. }
  521. /**
  522. * Test that deleting a file doesn't error when nobody is logged in
  523. */
  524. public function testSingleStorageDeleteFileLoggedOut() {
  525. $this->logout();
  526. if (!$this->userView->file_exists('test.txt')) {
  527. $this->markTestSkipped('Skipping since the current home storage backend requires the user to logged in');
  528. } else {
  529. $this->userView->unlink('test.txt');
  530. $this->addToAssertionCount(1);
  531. }
  532. }
  533. public function testTrashbinCollision() {
  534. $this->userView->file_put_contents('test.txt', 'foo');
  535. $this->userView->file_put_contents('folder/test.txt', 'bar');
  536. $timeFactory = $this->createMock(ITimeFactory::class);
  537. $timeFactory->method('getTime')
  538. ->willReturn(1000);
  539. $lockingProvider = \OC::$server->getLockingProvider();
  540. $this->overwriteService(ITimeFactory::class, $timeFactory);
  541. $this->userView->unlink('test.txt');
  542. $this->assertTrue($this->rootView->file_exists('/' . $this->user . '/files_trashbin/files/test.txt.d1000'));
  543. /** @var \OC\Files\Storage\Storage $trashStorage */
  544. [$trashStorage, $trashInternalPath] = $this->rootView->resolvePath('/' . $this->user . '/files_trashbin/files/test.txt.d1000');
  545. /// simulate a concurrent delete
  546. $trashStorage->acquireLock($trashInternalPath, ILockingProvider::LOCK_EXCLUSIVE, $lockingProvider);
  547. $this->userView->unlink('folder/test.txt');
  548. $trashStorage->releaseLock($trashInternalPath, ILockingProvider::LOCK_EXCLUSIVE, $lockingProvider);
  549. $this->assertTrue($this->rootView->file_exists($this->user . '/files_trashbin/files/test.txt.d1001'));
  550. $this->assertEquals('foo', $this->rootView->file_get_contents($this->user . '/files_trashbin/files/test.txt.d1000'));
  551. $this->assertEquals('bar', $this->rootView->file_get_contents($this->user . '/files_trashbin/files/test.txt.d1001'));
  552. }
  553. public function testMoveFromStoragePreserveFileId() {
  554. if (!$this->userView->getMount('')->getStorage()->instanceOfStorage(Local::class)) {
  555. $this->markTestSkipped("Skipping on non-local users storage");
  556. }
  557. $this->userView->file_put_contents('test.txt', 'foo');
  558. $fileId = $this->userView->getFileInfo('test.txt')->getId();
  559. $externalStorage = new TemporaryNoCross([]);
  560. $externalStorage->getScanner()->scan('');
  561. Filesystem::mount($externalStorage, [], "/" . $this->user . "/files/storage");
  562. $this->assertTrue($this->userView->rename('test.txt', 'storage/test.txt'));
  563. $this->assertTrue($externalStorage->file_exists('test.txt'));
  564. $this->assertEquals($fileId, $this->userView->getFileInfo('storage/test.txt')->getId());
  565. }
  566. }