SharedStorageTest.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  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 Thomas Müller <thomas.mueller@tmit.eu>
  12. * @author Vincent Petry <vincent@nextcloud.com>
  13. *
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. namespace OCA\Files_Sharing\Tests;
  30. use OC\Files\View;
  31. use OCA\Files_Sharing\SharedStorage;
  32. use OCA\Files_Trashbin\AppInfo\Application;
  33. use OCP\AppFramework\Bootstrap\IBootContext;
  34. use OCP\Files\NotFoundException;
  35. use OCP\Share\IShare;
  36. /**
  37. * Class SharedStorageTest
  38. *
  39. * @group DB
  40. */
  41. class SharedStorageTest extends TestCase {
  42. protected function setUp(): void {
  43. parent::setUp();
  44. // register trashbin hooks
  45. $trashbinApp = new Application();
  46. $trashbinApp->boot($this->createMock(IBootContext::class));
  47. $this->folder = '/folder_share_storage_test';
  48. $this->filename = '/share-api-storage.txt';
  49. $this->view->mkdir($this->folder);
  50. // save file with content
  51. $this->view->file_put_contents($this->filename, "root file");
  52. $this->view->file_put_contents($this->folder . $this->filename, "file in subfolder");
  53. }
  54. protected function tearDown(): void {
  55. if ($this->view) {
  56. if ($this->view->file_exists($this->folder)) {
  57. $this->view->unlink($this->folder);
  58. }
  59. if ($this->view->file_exists($this->filename)) {
  60. $this->view->unlink($this->filename);
  61. }
  62. }
  63. \OC\Files\Filesystem::getLoader()->removeStorageWrapper('oc_trashbin');
  64. parent::tearDown();
  65. }
  66. /**
  67. * if the parent of the mount point is gone then the mount point should move up
  68. *
  69. * @medium
  70. */
  71. public function testParentOfMountPointIsGone() {
  72. // share to user
  73. $share = $this->share(
  74. IShare::TYPE_USER,
  75. $this->folder,
  76. self::TEST_FILES_SHARING_API_USER1,
  77. self::TEST_FILES_SHARING_API_USER2,
  78. \OCP\Constants::PERMISSION_ALL
  79. );
  80. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  81. $user2View = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files');
  82. $this->assertTrue($user2View->file_exists($this->folder));
  83. // create a local folder
  84. $result = $user2View->mkdir('localfolder');
  85. $this->assertTrue($result);
  86. // move mount point to local folder
  87. $result = $user2View->rename($this->folder, '/localfolder/' . $this->folder);
  88. $this->assertTrue($result);
  89. // mount point in the root folder should no longer exist
  90. $this->assertFalse($user2View->is_dir($this->folder));
  91. // delete the local folder
  92. /** @var \OC\Files\Storage\Storage $storage */
  93. [$storage, $internalPath] = \OC\Files\Filesystem::resolvePath('/' . self::TEST_FILES_SHARING_API_USER2 . '/files/localfolder');
  94. $storage->rmdir($internalPath);
  95. //enforce reload of the mount points
  96. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  97. //mount point should be back at the root
  98. $this->assertTrue($user2View->is_dir($this->folder));
  99. //cleanup
  100. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  101. $this->view->unlink($this->folder);
  102. }
  103. /**
  104. * @medium
  105. */
  106. public function testRenamePartFile() {
  107. // share to user
  108. $share = $this->share(
  109. IShare::TYPE_USER,
  110. $this->folder,
  111. self::TEST_FILES_SHARING_API_USER1,
  112. self::TEST_FILES_SHARING_API_USER2,
  113. \OCP\Constants::PERMISSION_ALL
  114. );
  115. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  116. $user2View = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files');
  117. $this->assertTrue($user2View->file_exists($this->folder));
  118. // create part file
  119. $result = $user2View->file_put_contents($this->folder . '/foo.txt.part', 'some test data');
  120. $this->assertTrue(is_int($result));
  121. // rename part file to real file
  122. $result = $user2View->rename($this->folder . '/foo.txt.part', $this->folder . '/foo.txt');
  123. $this->assertTrue($result);
  124. // check if the new file really exists
  125. $this->assertTrue($user2View->file_exists($this->folder . '/foo.txt'));
  126. // check if the rename also affected the owner
  127. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  128. $this->assertTrue($this->view->file_exists($this->folder . '/foo.txt'));
  129. //cleanup
  130. $this->shareManager->deleteShare($share);
  131. }
  132. public function testFilesize() {
  133. $folderSize = $this->view->filesize($this->folder);
  134. $file1Size = $this->view->filesize($this->folder . $this->filename);
  135. $file2Size = $this->view->filesize($this->filename);
  136. $share1 = $this->share(
  137. IShare::TYPE_USER,
  138. $this->folder,
  139. self::TEST_FILES_SHARING_API_USER1,
  140. self::TEST_FILES_SHARING_API_USER2,
  141. \OCP\Constants::PERMISSION_ALL
  142. );
  143. $share2 = $this->share(
  144. IShare::TYPE_USER,
  145. $this->filename,
  146. self::TEST_FILES_SHARING_API_USER1,
  147. self::TEST_FILES_SHARING_API_USER2,
  148. \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_SHARE
  149. );
  150. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  151. // compare file size between user1 and user2, should always be the same
  152. $this->assertSame($folderSize, \OC\Files\Filesystem::filesize($this->folder));
  153. $this->assertSame($file1Size, \OC\Files\Filesystem::filesize($this->folder . $this->filename));
  154. $this->assertSame($file2Size, \OC\Files\Filesystem::filesize($this->filename));
  155. //cleanup
  156. $this->shareManager->deleteShare($share1);
  157. $this->shareManager->deleteShare($share2);
  158. }
  159. public function testGetPermissions() {
  160. $share = $this->share(
  161. IShare::TYPE_USER,
  162. $this->folder,
  163. self::TEST_FILES_SHARING_API_USER1,
  164. self::TEST_FILES_SHARING_API_USER2,
  165. \OCP\Constants::PERMISSION_READ
  166. );
  167. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  168. $this->assertTrue(\OC\Files\Filesystem::is_dir($this->folder));
  169. // for the share root we expect:
  170. // the read permissions (1)
  171. // the delete permission (8), to enable unshare
  172. $rootInfo = \OC\Files\Filesystem::getFileInfo($this->folder);
  173. $this->assertSame(9, $rootInfo->getPermissions());
  174. // for the file within the shared folder we expect:
  175. // the read permissions (1)
  176. $subfileInfo = \OC\Files\Filesystem::getFileInfo($this->folder . $this->filename);
  177. $this->assertSame(1, $subfileInfo->getPermissions());
  178. //cleanup
  179. $this->shareManager->deleteShare($share);
  180. }
  181. public function testFopenWithReadOnlyPermission() {
  182. $this->view->file_put_contents($this->folder . '/existing.txt', 'foo');
  183. $share = $this->share(
  184. IShare::TYPE_USER,
  185. $this->folder,
  186. self::TEST_FILES_SHARING_API_USER1,
  187. self::TEST_FILES_SHARING_API_USER2,
  188. \OCP\Constants::PERMISSION_READ
  189. );
  190. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  191. $user2View = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files');
  192. // part file should be forbidden
  193. $handle = $user2View->fopen($this->folder . '/test.txt.part', 'w');
  194. $this->assertFalse($handle);
  195. // regular file forbidden
  196. $handle = $user2View->fopen($this->folder . '/test.txt', 'w');
  197. $this->assertFalse($handle);
  198. // rename forbidden
  199. $this->assertFalse($user2View->rename($this->folder . '/existing.txt', $this->folder . '/existing2.txt'));
  200. // delete forbidden
  201. $this->assertFalse($user2View->unlink($this->folder . '/existing.txt'));
  202. //cleanup
  203. $this->shareManager->deleteShare($share);
  204. }
  205. public function testFopenWithCreateOnlyPermission() {
  206. $this->view->file_put_contents($this->folder . '/existing.txt', 'foo');
  207. $fileinfoFolder = $this->view->getFileInfo($this->folder);
  208. $share = $this->share(
  209. IShare::TYPE_USER,
  210. $this->folder,
  211. self::TEST_FILES_SHARING_API_USER1,
  212. self::TEST_FILES_SHARING_API_USER2,
  213. \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE
  214. );
  215. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  216. $user2View = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files');
  217. // create part file allowed
  218. $handle = $user2View->fopen($this->folder . '/test.txt.part', 'w');
  219. $this->assertNotFalse($handle);
  220. fclose($handle);
  221. // create regular file allowed
  222. $handle = $user2View->fopen($this->folder . '/test-create.txt', 'w');
  223. $this->assertNotFalse($handle);
  224. fclose($handle);
  225. // rename file never allowed
  226. $this->assertFalse($user2View->rename($this->folder . '/test-create.txt', $this->folder . '/newtarget.txt'));
  227. $this->assertFalse($user2View->file_exists($this->folder . '/newtarget.txt'));
  228. // rename file not allowed if target exists
  229. $this->assertFalse($user2View->rename($this->folder . '/newtarget.txt', $this->folder . '/existing.txt'));
  230. // overwriting file not allowed
  231. $handle = $user2View->fopen($this->folder . '/existing.txt', 'w');
  232. $this->assertFalse($handle);
  233. // overwrite forbidden (no update permission)
  234. $this->assertFalse($user2View->rename($this->folder . '/test.txt.part', $this->folder . '/existing.txt'));
  235. // delete forbidden
  236. $this->assertFalse($user2View->unlink($this->folder . '/existing.txt'));
  237. //cleanup
  238. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  239. $this->shareManager->deleteShare($share);
  240. }
  241. public function testFopenWithUpdateOnlyPermission() {
  242. $this->view->file_put_contents($this->folder . '/existing.txt', 'foo');
  243. $share = $this->share(
  244. IShare::TYPE_USER,
  245. $this->folder,
  246. self::TEST_FILES_SHARING_API_USER1,
  247. self::TEST_FILES_SHARING_API_USER2,
  248. \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE
  249. );
  250. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  251. $user2View = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files');
  252. // create part file allowed
  253. $handle = $user2View->fopen($this->folder . '/test.txt.part', 'w');
  254. $this->assertNotFalse($handle);
  255. fclose($handle);
  256. // create regular file not allowed
  257. $handle = $user2View->fopen($this->folder . '/test-create.txt', 'w');
  258. $this->assertFalse($handle);
  259. // rename part file not allowed to non-existing file
  260. $this->assertFalse($user2View->rename($this->folder . '/test.txt.part', $this->folder . '/nonexist.txt'));
  261. // rename part file allowed to target existing file
  262. $this->assertTrue($user2View->rename($this->folder . '/test.txt.part', $this->folder . '/existing.txt'));
  263. $this->assertTrue($user2View->file_exists($this->folder . '/existing.txt'));
  264. // rename regular file allowed
  265. $this->assertTrue($user2View->rename($this->folder . '/existing.txt', $this->folder . '/existing-renamed.txt'));
  266. $this->assertTrue($user2View->file_exists($this->folder . '/existing-renamed.txt'));
  267. // overwriting file directly is allowed
  268. $handle = $user2View->fopen($this->folder . '/existing-renamed.txt', 'w');
  269. $this->assertNotFalse($handle);
  270. fclose($handle);
  271. // delete forbidden
  272. $this->assertFalse($user2View->unlink($this->folder . '/existing-renamed.txt'));
  273. //cleanup
  274. $this->shareManager->deleteShare($share);
  275. }
  276. public function testFopenWithDeleteOnlyPermission() {
  277. $this->view->file_put_contents($this->folder . '/existing.txt', 'foo');
  278. $share = $this->share(
  279. IShare::TYPE_USER,
  280. $this->folder,
  281. self::TEST_FILES_SHARING_API_USER1,
  282. self::TEST_FILES_SHARING_API_USER2,
  283. \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_DELETE
  284. );
  285. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  286. $user2View = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files');
  287. // part file should be forbidden
  288. $handle = $user2View->fopen($this->folder . '/test.txt.part', 'w');
  289. $this->assertFalse($handle);
  290. // regular file forbidden
  291. $handle = $user2View->fopen($this->folder . '/test.txt', 'w');
  292. $this->assertFalse($handle);
  293. // rename forbidden
  294. $this->assertFalse($user2View->rename($this->folder . '/existing.txt', $this->folder . '/existing2.txt'));
  295. // delete allowed
  296. $this->assertTrue($user2View->unlink($this->folder . '/existing.txt'));
  297. //cleanup
  298. $this->shareManager->deleteShare($share);
  299. }
  300. public function testMountSharesOtherUser() {
  301. $rootView = new \OC\Files\View('');
  302. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  303. // share 2 different files with 2 different users
  304. $share1 = $this->share(
  305. IShare::TYPE_USER,
  306. $this->folder,
  307. self::TEST_FILES_SHARING_API_USER1,
  308. self::TEST_FILES_SHARING_API_USER2,
  309. \OCP\Constants::PERMISSION_ALL
  310. );
  311. $share2 = $this->share(
  312. IShare::TYPE_USER,
  313. $this->filename,
  314. self::TEST_FILES_SHARING_API_USER1,
  315. self::TEST_FILES_SHARING_API_USER3,
  316. \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_SHARE
  317. );
  318. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  319. $this->assertTrue($rootView->file_exists('/' . self::TEST_FILES_SHARING_API_USER2 . '/files/' . $this->folder));
  320. $mountConfigManager = \OC::$server->getMountProviderCollection();
  321. $mounts = $mountConfigManager->getMountsForUser(\OC::$server->getUserManager()->get(self::TEST_FILES_SHARING_API_USER3));
  322. array_walk($mounts, [\OC\Files\Filesystem::getMountManager(), 'addMount']);
  323. $this->assertTrue($rootView->file_exists('/' . self::TEST_FILES_SHARING_API_USER3 . '/files/' . $this->filename));
  324. // make sure we didn't double setup shares for user 2 or mounted the shares for user 3 in user's 2 home
  325. $this->assertFalse($rootView->file_exists('/' . self::TEST_FILES_SHARING_API_USER2 . '/files/' . $this->folder . ' (2)'));
  326. $this->assertFalse($rootView->file_exists('/' . self::TEST_FILES_SHARING_API_USER2 . '/files/' . $this->filename));
  327. //cleanup
  328. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  329. $this->view->unlink($this->folder);
  330. $this->shareManager->deleteShare($share1);
  331. $this->shareManager->deleteShare($share2);
  332. }
  333. public function testCopyFromStorage() {
  334. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  335. $share = $this->share(
  336. IShare::TYPE_USER,
  337. $this->folder,
  338. self::TEST_FILES_SHARING_API_USER1,
  339. self::TEST_FILES_SHARING_API_USER2,
  340. \OCP\Constants::PERMISSION_ALL
  341. );
  342. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  343. $view = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files');
  344. $this->assertTrue($view->file_exists($this->folder));
  345. /**
  346. * @var \OCP\Files\Storage $sharedStorage
  347. */
  348. [$sharedStorage,] = $view->resolvePath($this->folder);
  349. $this->assertTrue($sharedStorage->instanceOfStorage('OCA\Files_Sharing\ISharedStorage'));
  350. $sourceStorage = new \OC\Files\Storage\Temporary([]);
  351. $sourceStorage->file_put_contents('foo.txt', 'asd');
  352. $sharedStorage->copyFromStorage($sourceStorage, 'foo.txt', 'bar.txt');
  353. $this->assertTrue($sharedStorage->file_exists('bar.txt'));
  354. $this->assertEquals('asd', $sharedStorage->file_get_contents('bar.txt'));
  355. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  356. $this->view->unlink($this->folder);
  357. $this->shareManager->deleteShare($share);
  358. }
  359. public function testMoveFromStorage() {
  360. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  361. $share = $this->share(
  362. IShare::TYPE_USER,
  363. $this->folder,
  364. self::TEST_FILES_SHARING_API_USER1,
  365. self::TEST_FILES_SHARING_API_USER2,
  366. \OCP\Constants::PERMISSION_ALL
  367. );
  368. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  369. $view = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files');
  370. $this->assertTrue($view->file_exists($this->folder));
  371. /**
  372. * @var \OCP\Files\Storage $sharedStorage
  373. */
  374. [$sharedStorage,] = $view->resolvePath($this->folder);
  375. $this->assertTrue($sharedStorage->instanceOfStorage('OCA\Files_Sharing\ISharedStorage'));
  376. $sourceStorage = new \OC\Files\Storage\Temporary([]);
  377. $sourceStorage->file_put_contents('foo.txt', 'asd');
  378. $sharedStorage->moveFromStorage($sourceStorage, 'foo.txt', 'bar.txt');
  379. $this->assertTrue($sharedStorage->file_exists('bar.txt'));
  380. $this->assertEquals('asd', $sharedStorage->file_get_contents('bar.txt'));
  381. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  382. $this->view->unlink($this->folder);
  383. $this->shareManager->deleteShare($share);
  384. }
  385. public function testNameConflict() {
  386. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  387. $view1 = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER1 . '/files');
  388. $view1->mkdir('foo');
  389. self::loginHelper(self::TEST_FILES_SHARING_API_USER3);
  390. $view3 = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER3 . '/files');
  391. $view3->mkdir('foo');
  392. // share a folder with the same name from two different users to the same user
  393. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  394. $share1 = $this->share(
  395. IShare::TYPE_GROUP,
  396. 'foo',
  397. self::TEST_FILES_SHARING_API_USER1,
  398. self::TEST_FILES_SHARING_API_GROUP1,
  399. \OCP\Constants::PERMISSION_ALL
  400. );
  401. $this->shareManager->acceptShare($share1, self::TEST_FILES_SHARING_API_USER2);
  402. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  403. self::loginHelper(self::TEST_FILES_SHARING_API_USER3);
  404. $share2 = $this->share(
  405. IShare::TYPE_GROUP,
  406. 'foo',
  407. self::TEST_FILES_SHARING_API_USER3,
  408. self::TEST_FILES_SHARING_API_GROUP1,
  409. \OCP\Constants::PERMISSION_ALL
  410. );
  411. $this->shareManager->acceptShare($share2, self::TEST_FILES_SHARING_API_USER2);
  412. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  413. $view2 = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files');
  414. $this->assertTrue($view2->file_exists('/foo'));
  415. $this->assertTrue($view2->file_exists('/foo (2)'));
  416. $mount = $view2->getMount('/foo');
  417. $this->assertInstanceOf('\OCA\Files_Sharing\SharedMount', $mount);
  418. /** @var \OCA\Files_Sharing\SharedStorage $storage */
  419. $storage = $mount->getStorage();
  420. $this->assertEquals(self::TEST_FILES_SHARING_API_USER1, $storage->getOwner(''));
  421. $this->shareManager->deleteShare($share1);
  422. $this->shareManager->deleteShare($share2);
  423. }
  424. public function testOwnerPermissions() {
  425. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  426. $share = $this->share(
  427. IShare::TYPE_USER,
  428. $this->folder,
  429. self::TEST_FILES_SHARING_API_USER1,
  430. self::TEST_FILES_SHARING_API_USER2,
  431. \OCP\Constants::PERMISSION_ALL - \OCP\Constants::PERMISSION_DELETE
  432. );
  433. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  434. $view = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files');
  435. $this->assertTrue($view->file_exists($this->folder));
  436. $view->file_put_contents($this->folder . '/newfile.txt', 'asd');
  437. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  438. $this->assertTrue($this->view->file_exists($this->folder . '/newfile.txt'));
  439. $this->assertEquals(\OCP\Constants::PERMISSION_ALL - \OCP\Constants::PERMISSION_CREATE,
  440. $this->view->getFileInfo($this->folder . '/newfile.txt')->getPermissions());
  441. $this->view->unlink($this->folder);
  442. $this->shareManager->deleteShare($share);
  443. }
  444. public function testInitWithNonExistingUser() {
  445. $share = $this->createMock(IShare::class);
  446. $share->method('getShareOwner')->willReturn('unexist');
  447. $ownerView = $this->createMock(View::class);
  448. $storage = new SharedStorage([
  449. 'ownerView' => $ownerView,
  450. 'superShare' => $share,
  451. 'groupedShares' => [$share],
  452. 'user' => 'user1',
  453. ]);
  454. // trigger init
  455. $this->assertInstanceOf(\OC\Files\Storage\FailedStorage::class, $storage->getSourceStorage());
  456. $this->assertInstanceOf(\OC\Files\Cache\FailedCache::class, $storage->getCache());
  457. }
  458. public function testInitWithNotFoundSource() {
  459. $share = $this->createMock(IShare::class);
  460. $share->method('getShareOwner')->willReturn(self::TEST_FILES_SHARING_API_USER1);
  461. $share->method('getNodeId')->willReturn(1);
  462. $ownerView = $this->createMock(View::class);
  463. $ownerView->method('getPath')->will($this->throwException(new NotFoundException()));
  464. $storage = new SharedStorage([
  465. 'ownerView' => $ownerView,
  466. 'superShare' => $share,
  467. 'groupedShares' => [$share],
  468. 'user' => 'user1',
  469. ]);
  470. // trigger init
  471. $this->assertInstanceOf(\OC\Files\Storage\FailedStorage::class, $storage->getSourceStorage());
  472. $this->assertInstanceOf(\OC\Files\Cache\FailedCache::class, $storage->getCache());
  473. }
  474. }