StorageTest.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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 Joas Schilling <coding@schilljs.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Robin Appelman <robin@icewind.nl>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Stefan Weil <sw@weilnetz.de>
  12. * @author Thomas Müller <thomas.mueller@tmit.eu>
  13. * @author Vincent Petry <pvince81@owncloud.com>
  14. *
  15. * @license AGPL-3.0
  16. *
  17. * This code is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU Affero General Public License, version 3,
  19. * as published by the Free Software Foundation.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License, version 3,
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>
  28. *
  29. */
  30. namespace OCA\Files_Trashbin\Tests;
  31. use OC\Files\Storage\Temporary;
  32. use OC\Files\Filesystem;
  33. use OCA\Files_Trashbin\Events\MoveToTrashEvent;
  34. use OCA\Files_Trashbin\Storage;
  35. use OCA\Files_Trashbin\Trash\ITrashManager;
  36. use OCP\Files\Cache\ICache;
  37. use OCP\Files\Folder;
  38. use OCP\Files\IRootFolder;
  39. use OCP\Files\Node;
  40. use OCP\ILogger;
  41. use OCP\IUserManager;
  42. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  43. /**
  44. * Class Storage
  45. *
  46. * @group DB
  47. *
  48. * @package OCA\Files_Trashbin\Tests
  49. */
  50. class StorageTest extends \Test\TestCase {
  51. /**
  52. * @var string
  53. */
  54. private $user;
  55. /**
  56. * @var \OC\Files\View
  57. */
  58. private $rootView;
  59. /**
  60. * @var \OC\Files\View
  61. */
  62. private $userView;
  63. protected function setUp() {
  64. parent::setUp();
  65. \OC_Hook::clear();
  66. \OCA\Files_Trashbin\Trashbin::registerHooks();
  67. $this->user = $this->getUniqueId('user');
  68. \OC::$server->getUserManager()->createUser($this->user, $this->user);
  69. // this will setup the FS
  70. $this->loginAsUser($this->user);
  71. \OCA\Files_Trashbin\Storage::setupStorage();
  72. $this->rootView = new \OC\Files\View('/');
  73. $this->userView = new \OC\Files\View('/' . $this->user . '/files/');
  74. $this->userView->file_put_contents('test.txt', 'foo');
  75. $this->userView->mkdir('folder');
  76. $this->userView->file_put_contents('folder/inside.txt', 'bar');
  77. }
  78. protected function tearDown() {
  79. \OC\Files\Filesystem::getLoader()->removeStorageWrapper('oc_trashbin');
  80. $this->logout();
  81. $user = \OC::$server->getUserManager()->get($this->user);
  82. if ($user !== null) { $user->delete(); }
  83. \OC_Hook::clear();
  84. parent::tearDown();
  85. }
  86. /**
  87. * Test that deleting a file puts it into the trashbin.
  88. */
  89. public function testSingleStorageDeleteFile() {
  90. $this->assertTrue($this->userView->file_exists('test.txt'));
  91. $this->userView->unlink('test.txt');
  92. list($storage,) = $this->userView->resolvePath('test.txt');
  93. $storage->getScanner()->scan(''); // make sure we check the storage
  94. $this->assertFalse($this->userView->getFileInfo('test.txt'));
  95. // check if file is in trashbin
  96. $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/files/');
  97. $this->assertEquals(1, count($results));
  98. $name = $results[0]->getName();
  99. $this->assertEquals('test.txt', substr($name, 0, strrpos($name, '.')));
  100. }
  101. /**
  102. * Test that deleting a folder puts it into the trashbin.
  103. */
  104. public function testSingleStorageDeleteFolder() {
  105. $this->assertTrue($this->userView->file_exists('folder/inside.txt'));
  106. $this->userView->rmdir('folder');
  107. list($storage,) = $this->userView->resolvePath('folder/inside.txt');
  108. $storage->getScanner()->scan(''); // make sure we check the storage
  109. $this->assertFalse($this->userView->getFileInfo('folder'));
  110. // check if folder is in trashbin
  111. $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/files/');
  112. $this->assertEquals(1, count($results));
  113. $name = $results[0]->getName();
  114. $this->assertEquals('folder', substr($name, 0, strrpos($name, '.')));
  115. $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/files/' . $name . '/');
  116. $this->assertEquals(1, count($results));
  117. $name = $results[0]->getName();
  118. $this->assertEquals('inside.txt', $name);
  119. }
  120. /**
  121. * Test that deleting a file from another mounted storage properly
  122. * lands in the trashbin. This is a cross-storage situation because
  123. * the trashbin folder is in the root storage while the mounted one
  124. * isn't.
  125. */
  126. public function testCrossStorageDeleteFile() {
  127. $storage2 = new Temporary(array());
  128. \OC\Files\Filesystem::mount($storage2, array(), $this->user . '/files/substorage');
  129. $this->userView->file_put_contents('substorage/subfile.txt', 'foo');
  130. $storage2->getScanner()->scan('');
  131. $this->assertTrue($storage2->file_exists('subfile.txt'));
  132. $this->userView->unlink('substorage/subfile.txt');
  133. $storage2->getScanner()->scan('');
  134. $this->assertFalse($this->userView->getFileInfo('substorage/subfile.txt'));
  135. $this->assertFalse($storage2->file_exists('subfile.txt'));
  136. // check if file is in trashbin
  137. $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/files');
  138. $this->assertEquals(1, count($results));
  139. $name = $results[0]->getName();
  140. $this->assertEquals('subfile.txt', substr($name, 0, strrpos($name, '.')));
  141. }
  142. /**
  143. * Test that deleting a folder from another mounted storage properly
  144. * lands in the trashbin. This is a cross-storage situation because
  145. * the trashbin folder is in the root storage while the mounted one
  146. * isn't.
  147. */
  148. public function testCrossStorageDeleteFolder() {
  149. $storage2 = new Temporary(array());
  150. \OC\Files\Filesystem::mount($storage2, array(), $this->user . '/files/substorage');
  151. $this->userView->mkdir('substorage/folder');
  152. $this->userView->file_put_contents('substorage/folder/subfile.txt', 'bar');
  153. $storage2->getScanner()->scan('');
  154. $this->assertTrue($storage2->file_exists('folder/subfile.txt'));
  155. $this->userView->rmdir('substorage/folder');
  156. $storage2->getScanner()->scan('');
  157. $this->assertFalse($this->userView->getFileInfo('substorage/folder'));
  158. $this->assertFalse($storage2->file_exists('folder/subfile.txt'));
  159. // check if folder is in trashbin
  160. $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/files');
  161. $this->assertEquals(1, count($results));
  162. $name = $results[0]->getName();
  163. $this->assertEquals('folder', substr($name, 0, strrpos($name, '.')));
  164. $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/files/' . $name . '/');
  165. $this->assertEquals(1, count($results));
  166. $name = $results[0]->getName();
  167. $this->assertEquals('subfile.txt', $name);
  168. }
  169. /**
  170. * Test that deleted versions properly land in the trashbin.
  171. */
  172. public function testDeleteVersionsOfFile() {
  173. \OCA\Files_Versions\Hooks::connectHooks();
  174. // trigger a version (multiple would not work because of the expire logic)
  175. $this->userView->file_put_contents('test.txt', 'v1');
  176. $results = $this->rootView->getDirectoryContent($this->user . '/files_versions/');
  177. $this->assertEquals(1, count($results));
  178. $this->userView->unlink('test.txt');
  179. // rescan trash storage
  180. list($rootStorage,) = $this->rootView->resolvePath($this->user . '/files_trashbin');
  181. $rootStorage->getScanner()->scan('');
  182. // check if versions are in trashbin
  183. $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/versions');
  184. $this->assertEquals(1, count($results));
  185. $name = $results[0]->getName();
  186. $this->assertEquals('test.txt.v', substr($name, 0, strlen('test.txt.v')));
  187. // versions deleted
  188. $results = $this->rootView->getDirectoryContent($this->user . '/files_versions/');
  189. $this->assertEquals(0, count($results));
  190. }
  191. /**
  192. * Test that deleted versions properly land in the trashbin.
  193. */
  194. public function testDeleteVersionsOfFolder() {
  195. \OCA\Files_Versions\Hooks::connectHooks();
  196. // trigger a version (multiple would not work because of the expire logic)
  197. $this->userView->file_put_contents('folder/inside.txt', 'v1');
  198. $results = $this->rootView->getDirectoryContent($this->user . '/files_versions/folder/');
  199. $this->assertEquals(1, count($results));
  200. $this->userView->rmdir('folder');
  201. // rescan trash storage
  202. list($rootStorage,) = $this->rootView->resolvePath($this->user . '/files_trashbin');
  203. $rootStorage->getScanner()->scan('');
  204. // check if versions are in trashbin
  205. $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/versions');
  206. $this->assertEquals(1, count($results));
  207. $name = $results[0]->getName();
  208. $this->assertEquals('folder.d', substr($name, 0, strlen('folder.d')));
  209. // check if versions are in trashbin
  210. $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/versions/' . $name . '/');
  211. $this->assertEquals(1, count($results));
  212. $name = $results[0]->getName();
  213. $this->assertEquals('inside.txt.v', substr($name, 0, strlen('inside.txt.v')));
  214. // versions deleted
  215. $results = $this->rootView->getDirectoryContent($this->user . '/files_versions/folder/');
  216. $this->assertEquals(0, count($results));
  217. }
  218. /**
  219. * Test that deleted versions properly land in the trashbin when deleting as share recipient.
  220. */
  221. public function testDeleteVersionsOfFileAsRecipient() {
  222. \OCA\Files_Versions\Hooks::connectHooks();
  223. $this->userView->mkdir('share');
  224. // trigger a version (multiple would not work because of the expire logic)
  225. $this->userView->file_put_contents('share/test.txt', 'v1');
  226. $this->userView->file_put_contents('share/test.txt', 'v2');
  227. $results = $this->rootView->getDirectoryContent($this->user . '/files_versions/share/');
  228. $this->assertEquals(1, count($results));
  229. $recipientUser = $this->getUniqueId('recipient_');
  230. \OC::$server->getUserManager()->createUser($recipientUser, $recipientUser);
  231. $node = \OC::$server->getUserFolder($this->user)->get('share');
  232. $share = \OC::$server->getShareManager()->newShare();
  233. $share->setNode($node)
  234. ->setShareType(\OCP\Share::SHARE_TYPE_USER)
  235. ->setSharedBy($this->user)
  236. ->setSharedWith($recipientUser)
  237. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  238. \OC::$server->getShareManager()->createShare($share);
  239. $this->loginAsUser($recipientUser);
  240. // delete as recipient
  241. $recipientView = new \OC\Files\View('/' . $recipientUser . '/files');
  242. $recipientView->unlink('share/test.txt');
  243. // rescan trash storage for both users
  244. list($rootStorage,) = $this->rootView->resolvePath($this->user . '/files_trashbin');
  245. $rootStorage->getScanner()->scan('');
  246. // check if versions are in trashbin for both users
  247. $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/versions');
  248. $this->assertEquals(1, count($results), 'Versions in owner\'s trashbin');
  249. $name = $results[0]->getName();
  250. $this->assertEquals('test.txt.v', substr($name, 0, strlen('test.txt.v')));
  251. $results = $this->rootView->getDirectoryContent($recipientUser . '/files_trashbin/versions');
  252. $this->assertEquals(1, count($results), 'Versions in recipient\'s trashbin');
  253. $name = $results[0]->getName();
  254. $this->assertEquals('test.txt.v', substr($name, 0, strlen('test.txt.v')));
  255. // versions deleted
  256. $results = $this->rootView->getDirectoryContent($this->user . '/files_versions/share/');
  257. $this->assertEquals(0, count($results));
  258. }
  259. /**
  260. * Test that deleted versions properly land in the trashbin when deleting as share recipient.
  261. */
  262. public function testDeleteVersionsOfFolderAsRecipient() {
  263. \OCA\Files_Versions\Hooks::connectHooks();
  264. $this->userView->mkdir('share');
  265. $this->userView->mkdir('share/folder');
  266. // trigger a version (multiple would not work because of the expire logic)
  267. $this->userView->file_put_contents('share/folder/test.txt', 'v1');
  268. $this->userView->file_put_contents('share/folder/test.txt', 'v2');
  269. $results = $this->rootView->getDirectoryContent($this->user . '/files_versions/share/folder/');
  270. $this->assertEquals(1, count($results));
  271. $recipientUser = $this->getUniqueId('recipient_');
  272. \OC::$server->getUserManager()->createUser($recipientUser, $recipientUser);
  273. $node = \OC::$server->getUserFolder($this->user)->get('share');
  274. $share = \OC::$server->getShareManager()->newShare();
  275. $share->setNode($node)
  276. ->setShareType(\OCP\Share::SHARE_TYPE_USER)
  277. ->setSharedBy($this->user)
  278. ->setSharedWith($recipientUser)
  279. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  280. \OC::$server->getShareManager()->createShare($share);
  281. $this->loginAsUser($recipientUser);
  282. // delete as recipient
  283. $recipientView = new \OC\Files\View('/' . $recipientUser . '/files');
  284. $recipientView->rmdir('share/folder');
  285. // rescan trash storage
  286. list($rootStorage,) = $this->rootView->resolvePath($this->user . '/files_trashbin');
  287. $rootStorage->getScanner()->scan('');
  288. // check if versions are in trashbin for owner
  289. $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/versions');
  290. $this->assertEquals(1, count($results));
  291. $name = $results[0]->getName();
  292. $this->assertEquals('folder.d', substr($name, 0, strlen('folder.d')));
  293. // check if file versions are in trashbin for owner
  294. $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/versions/' . $name . '/');
  295. $this->assertEquals(1, count($results));
  296. $name = $results[0]->getName();
  297. $this->assertEquals('test.txt.v', substr($name, 0, strlen('test.txt.v')));
  298. // check if versions are in trashbin for recipient
  299. $results = $this->rootView->getDirectoryContent($recipientUser . '/files_trashbin/versions');
  300. $this->assertEquals(1, count($results));
  301. $name = $results[0]->getName();
  302. $this->assertEquals('folder.d', substr($name, 0, strlen('folder.d')));
  303. // check if file versions are in trashbin for recipient
  304. $results = $this->rootView->getDirectoryContent($recipientUser . '/files_trashbin/versions/' . $name . '/');
  305. $this->assertEquals(1, count($results));
  306. $name = $results[0]->getName();
  307. $this->assertEquals('test.txt.v', substr($name, 0, strlen('test.txt.v')));
  308. // versions deleted
  309. $results = $this->rootView->getDirectoryContent($recipientUser . '/files_versions/share/folder/');
  310. $this->assertEquals(0, count($results));
  311. }
  312. /**
  313. * Test that versions are not auto-trashed when moving a file between
  314. * storages. This is because rename() between storages would call
  315. * unlink() which should NOT trigger the version deletion logic.
  316. */
  317. public function testKeepFileAndVersionsWhenMovingFileBetweenStorages() {
  318. \OCA\Files_Versions\Hooks::connectHooks();
  319. $storage2 = new Temporary(array());
  320. \OC\Files\Filesystem::mount($storage2, array(), $this->user . '/files/substorage');
  321. // trigger a version (multiple would not work because of the expire logic)
  322. $this->userView->file_put_contents('test.txt', 'v1');
  323. $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/files');
  324. $this->assertEquals(0, count($results));
  325. $results = $this->rootView->getDirectoryContent($this->user . '/files_versions/');
  326. $this->assertEquals(1, count($results));
  327. // move to another storage
  328. $this->userView->rename('test.txt', 'substorage/test.txt');
  329. $this->assertTrue($this->userView->file_exists('substorage/test.txt'));
  330. // rescan trash storage
  331. list($rootStorage,) = $this->rootView->resolvePath($this->user . '/files_trashbin');
  332. $rootStorage->getScanner()->scan('');
  333. // versions were moved too
  334. $results = $this->rootView->getDirectoryContent($this->user . '/files_versions/substorage');
  335. $this->assertEquals(1, count($results));
  336. // check that nothing got trashed by the rename's unlink() call
  337. $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/files');
  338. $this->assertEquals(0, count($results));
  339. // check that versions were moved and not trashed
  340. $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/versions/');
  341. $this->assertEquals(0, count($results));
  342. }
  343. /**
  344. * Test that versions are not auto-trashed when moving a file between
  345. * storages. This is because rename() between storages would call
  346. * unlink() which should NOT trigger the version deletion logic.
  347. */
  348. public function testKeepFileAndVersionsWhenMovingFolderBetweenStorages() {
  349. \OCA\Files_Versions\Hooks::connectHooks();
  350. $storage2 = new Temporary(array());
  351. \OC\Files\Filesystem::mount($storage2, array(), $this->user . '/files/substorage');
  352. // trigger a version (multiple would not work because of the expire logic)
  353. $this->userView->file_put_contents('folder/inside.txt', 'v1');
  354. $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/files');
  355. $this->assertEquals(0, count($results));
  356. $results = $this->rootView->getDirectoryContent($this->user . '/files_versions/folder/');
  357. $this->assertEquals(1, count($results));
  358. // move to another storage
  359. $this->userView->rename('folder', 'substorage/folder');
  360. $this->assertTrue($this->userView->file_exists('substorage/folder/inside.txt'));
  361. // rescan trash storage
  362. list($rootStorage,) = $this->rootView->resolvePath($this->user . '/files_trashbin');
  363. $rootStorage->getScanner()->scan('');
  364. // versions were moved too
  365. $results = $this->rootView->getDirectoryContent($this->user . '/files_versions/substorage/folder/');
  366. $this->assertEquals(1, count($results));
  367. // check that nothing got trashed by the rename's unlink() call
  368. $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/files');
  369. $this->assertEquals(0, count($results));
  370. // check that versions were moved and not trashed
  371. $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/versions/');
  372. $this->assertEquals(0, count($results));
  373. }
  374. /**
  375. * Delete should fail if the source file can't be deleted.
  376. */
  377. public function testSingleStorageDeleteFileFail() {
  378. /**
  379. * @var \OC\Files\Storage\Temporary | \PHPUnit_Framework_MockObject_MockObject $storage
  380. */
  381. $storage = $this->getMockBuilder('\OC\Files\Storage\Temporary')
  382. ->setConstructorArgs([[]])
  383. ->setMethods(['rename', 'unlink', 'moveFromStorage'])
  384. ->getMock();
  385. $storage->expects($this->any())
  386. ->method('rename')
  387. ->will($this->returnValue(false));
  388. $storage->expects($this->any())
  389. ->method('moveFromStorage')
  390. ->will($this->returnValue(false));
  391. $storage->expects($this->any())
  392. ->method('unlink')
  393. ->will($this->returnValue(false));
  394. $cache = $storage->getCache();
  395. Filesystem::mount($storage, [], '/' . $this->user);
  396. $storage->mkdir('files');
  397. $this->userView->file_put_contents('test.txt', 'foo');
  398. $this->assertTrue($storage->file_exists('files/test.txt'));
  399. $this->assertFalse($this->userView->unlink('test.txt'));
  400. $this->assertTrue($storage->file_exists('files/test.txt'));
  401. $this->assertTrue($cache->inCache('files/test.txt'));
  402. // file should not be in the trashbin
  403. $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/files/');
  404. $this->assertEquals(0, count($results));
  405. }
  406. /**
  407. * Delete should fail if the source folder can't be deleted.
  408. */
  409. public function testSingleStorageDeleteFolderFail() {
  410. /**
  411. * @var \OC\Files\Storage\Temporary | \PHPUnit_Framework_MockObject_MockObject $storage
  412. */
  413. $storage = $this->getMockBuilder('\OC\Files\Storage\Temporary')
  414. ->setConstructorArgs([[]])
  415. ->setMethods(['rename', 'unlink', 'rmdir'])
  416. ->getMock();
  417. $storage->expects($this->any())
  418. ->method('rmdir')
  419. ->will($this->returnValue(false));
  420. $cache = $storage->getCache();
  421. Filesystem::mount($storage, [], '/' . $this->user);
  422. $storage->mkdir('files');
  423. $this->userView->mkdir('folder');
  424. $this->userView->file_put_contents('folder/test.txt', 'foo');
  425. $this->assertTrue($storage->file_exists('files/folder/test.txt'));
  426. $this->assertFalse($this->userView->rmdir('files/folder'));
  427. $this->assertTrue($storage->file_exists('files/folder'));
  428. $this->assertTrue($storage->file_exists('files/folder/test.txt'));
  429. $this->assertTrue($cache->inCache('files/folder'));
  430. $this->assertTrue($cache->inCache('files/folder/test.txt'));
  431. // file should not be in the trashbin
  432. $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/files/');
  433. $this->assertEquals(0, count($results));
  434. }
  435. /**
  436. * @dataProvider dataTestShouldMoveToTrash
  437. */
  438. public function testShouldMoveToTrash($mountPoint, $path, $userExists, $appDisablesTrash, $expected) {
  439. $fileID = 1;
  440. $cache = $this->createMock(ICache::class);
  441. $cache->expects($this->any())->method('getId')->willReturn($fileID);
  442. $tmpStorage = $this->getMockBuilder('\OC\Files\Storage\Temporary')
  443. ->disableOriginalConstructor()->getMock($cache);
  444. $tmpStorage->expects($this->any())->method('getCache')->willReturn($cache);
  445. $userManager = $this->getMockBuilder(IUserManager::class)
  446. ->disableOriginalConstructor()->getMock();
  447. $userManager->expects($this->any())
  448. ->method('userExists')->willReturn($userExists);
  449. $logger = $this->getMockBuilder(ILogger::class)->getMock();
  450. $eventDispatcher = $this->createMock(EventDispatcherInterface::class);
  451. $rootFolder = $this->createMock(IRootFolder::class);
  452. $userFolder = $this->createMock(Folder::class);
  453. $node = $this->getMockBuilder(Node::class)->disableOriginalConstructor()->getMock();
  454. $trashManager = $this->createMock(ITrashManager::class);
  455. $event = $this->getMockBuilder(MoveToTrashEvent::class)->disableOriginalConstructor()->getMock();
  456. $event->expects($this->any())->method('shouldMoveToTrashBin')->willReturn(!$appDisablesTrash);
  457. $userFolder->expects($this->any())->method('getById')->with($fileID)->willReturn([$node]);
  458. $rootFolder->expects($this->any())->method('getUserFolder')->willReturn($userFolder);
  459. $storage = $this->getMockBuilder(Storage::class)
  460. ->setConstructorArgs(
  461. [
  462. ['mountPoint' => $mountPoint, 'storage' => $tmpStorage],
  463. $trashManager,
  464. $userManager,
  465. $logger,
  466. $eventDispatcher,
  467. $rootFolder
  468. ]
  469. )->setMethods(['createMoveToTrashEvent'])->getMock();
  470. $storage->expects($this->any())->method('createMoveToTrashEvent')->with($node)
  471. ->willReturn($event);
  472. $this->assertSame($expected,
  473. $this->invokePrivate($storage, 'shouldMoveToTrash', [$path])
  474. );
  475. }
  476. public function dataTestShouldMoveToTrash() {
  477. return [
  478. ['/schiesbn/', '/files/test.txt', true, false, true],
  479. ['/schiesbn/', '/files/test.txt', false, false, false],
  480. ['/schiesbn/', '/test.txt', true, false, false],
  481. ['/schiesbn/', '/test.txt', false, false, false],
  482. // other apps disables the trashbin
  483. ['/schiesbn/', '/files/test.txt', true, true, false],
  484. ['/schiesbn/', '/files/test.txt', false, true, false],
  485. ];
  486. }
  487. /**
  488. * Test that deleting a file doesn't error when nobody is logged in
  489. */
  490. public function testSingleStorageDeleteFileLoggedOut() {
  491. $this->logout();
  492. if (!$this->userView->file_exists('test.txt')) {
  493. $this->markTestSkipped('Skipping since the current home storage backend requires the user to logged in');
  494. } else {
  495. $this->userView->unlink('test.txt');
  496. $this->addToAssertionCount(1);
  497. }
  498. }
  499. }