1
0

StorageTest.php 27 KB

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