StorageTest.php 24 KB

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