StorageTest.php 23 KB

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