StorageTest.php 28 KB

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