SharedStorageTest.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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 <pvince81@owncloud.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. list($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. $result = \OC\Share\Share::unshare('folder', $fileinfoFolder['fileid'], IShare::TYPE_USER,
  240. self::TEST_FILES_SHARING_API_USER2);
  241. $this->assertTrue($result);
  242. }
  243. public function testFopenWithUpdateOnlyPermission() {
  244. $this->view->file_put_contents($this->folder . '/existing.txt', 'foo');
  245. $share = $this->share(
  246. IShare::TYPE_USER,
  247. $this->folder,
  248. self::TEST_FILES_SHARING_API_USER1,
  249. self::TEST_FILES_SHARING_API_USER2,
  250. \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE
  251. );
  252. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  253. $user2View = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files');
  254. // create part file allowed
  255. $handle = $user2View->fopen($this->folder . '/test.txt.part', 'w');
  256. $this->assertNotFalse($handle);
  257. fclose($handle);
  258. // create regular file not allowed
  259. $handle = $user2View->fopen($this->folder . '/test-create.txt', 'w');
  260. $this->assertFalse($handle);
  261. // rename part file not allowed to non-existing file
  262. $this->assertFalse($user2View->rename($this->folder . '/test.txt.part', $this->folder . '/nonexist.txt'));
  263. // rename part file allowed to target existing file
  264. $this->assertTrue($user2View->rename($this->folder . '/test.txt.part', $this->folder . '/existing.txt'));
  265. $this->assertTrue($user2View->file_exists($this->folder . '/existing.txt'));
  266. // rename regular file allowed
  267. $this->assertTrue($user2View->rename($this->folder . '/existing.txt', $this->folder . '/existing-renamed.txt'));
  268. $this->assertTrue($user2View->file_exists($this->folder . '/existing-renamed.txt'));
  269. // overwriting file directly is allowed
  270. $handle = $user2View->fopen($this->folder . '/existing-renamed.txt', 'w');
  271. $this->assertNotFalse($handle);
  272. fclose($handle);
  273. // delete forbidden
  274. $this->assertFalse($user2View->unlink($this->folder . '/existing-renamed.txt'));
  275. //cleanup
  276. $this->shareManager->deleteShare($share);
  277. }
  278. public function testFopenWithDeleteOnlyPermission() {
  279. $this->view->file_put_contents($this->folder . '/existing.txt', 'foo');
  280. $share = $this->share(
  281. IShare::TYPE_USER,
  282. $this->folder,
  283. self::TEST_FILES_SHARING_API_USER1,
  284. self::TEST_FILES_SHARING_API_USER2,
  285. \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_DELETE
  286. );
  287. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  288. $user2View = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files');
  289. // part file should be forbidden
  290. $handle = $user2View->fopen($this->folder . '/test.txt.part', 'w');
  291. $this->assertFalse($handle);
  292. // regular file forbidden
  293. $handle = $user2View->fopen($this->folder . '/test.txt', 'w');
  294. $this->assertFalse($handle);
  295. // rename forbidden
  296. $this->assertFalse($user2View->rename($this->folder . '/existing.txt', $this->folder . '/existing2.txt'));
  297. // delete allowed
  298. $this->assertTrue($user2View->unlink($this->folder . '/existing.txt'));
  299. //cleanup
  300. $this->shareManager->deleteShare($share);
  301. }
  302. public function testMountSharesOtherUser() {
  303. $rootView = new \OC\Files\View('');
  304. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  305. // share 2 different files with 2 different users
  306. $share1 = $this->share(
  307. IShare::TYPE_USER,
  308. $this->folder,
  309. self::TEST_FILES_SHARING_API_USER1,
  310. self::TEST_FILES_SHARING_API_USER2,
  311. \OCP\Constants::PERMISSION_ALL
  312. );
  313. $share2 = $this->share(
  314. IShare::TYPE_USER,
  315. $this->filename,
  316. self::TEST_FILES_SHARING_API_USER1,
  317. self::TEST_FILES_SHARING_API_USER3,
  318. \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_SHARE
  319. );
  320. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  321. $this->assertTrue($rootView->file_exists('/' . self::TEST_FILES_SHARING_API_USER2 . '/files/' . $this->folder));
  322. $mountConfigManager = \OC::$server->getMountProviderCollection();
  323. $mounts = $mountConfigManager->getMountsForUser(\OC::$server->getUserManager()->get(self::TEST_FILES_SHARING_API_USER3));
  324. array_walk($mounts, [\OC\Files\Filesystem::getMountManager(), 'addMount']);
  325. $this->assertTrue($rootView->file_exists('/' . self::TEST_FILES_SHARING_API_USER3 . '/files/' . $this->filename));
  326. // make sure we didn't double setup shares for user 2 or mounted the shares for user 3 in user's 2 home
  327. $this->assertFalse($rootView->file_exists('/' . self::TEST_FILES_SHARING_API_USER2 . '/files/' . $this->folder . ' (2)'));
  328. $this->assertFalse($rootView->file_exists('/' . self::TEST_FILES_SHARING_API_USER2 . '/files/' . $this->filename));
  329. //cleanup
  330. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  331. $this->view->unlink($this->folder);
  332. $this->shareManager->deleteShare($share1);
  333. $this->shareManager->deleteShare($share2);
  334. }
  335. public function testCopyFromStorage() {
  336. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  337. $share = $this->share(
  338. IShare::TYPE_USER,
  339. $this->folder,
  340. self::TEST_FILES_SHARING_API_USER1,
  341. self::TEST_FILES_SHARING_API_USER2,
  342. \OCP\Constants::PERMISSION_ALL
  343. );
  344. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  345. $view = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files');
  346. $this->assertTrue($view->file_exists($this->folder));
  347. /**
  348. * @var \OCP\Files\Storage $sharedStorage
  349. */
  350. list($sharedStorage,) = $view->resolvePath($this->folder);
  351. $this->assertTrue($sharedStorage->instanceOfStorage('OCA\Files_Sharing\ISharedStorage'));
  352. $sourceStorage = new \OC\Files\Storage\Temporary([]);
  353. $sourceStorage->file_put_contents('foo.txt', 'asd');
  354. $sharedStorage->copyFromStorage($sourceStorage, 'foo.txt', 'bar.txt');
  355. $this->assertTrue($sharedStorage->file_exists('bar.txt'));
  356. $this->assertEquals('asd', $sharedStorage->file_get_contents('bar.txt'));
  357. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  358. $this->view->unlink($this->folder);
  359. $this->shareManager->deleteShare($share);
  360. }
  361. public function testMoveFromStorage() {
  362. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  363. $share = $this->share(
  364. IShare::TYPE_USER,
  365. $this->folder,
  366. self::TEST_FILES_SHARING_API_USER1,
  367. self::TEST_FILES_SHARING_API_USER2,
  368. \OCP\Constants::PERMISSION_ALL
  369. );
  370. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  371. $view = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files');
  372. $this->assertTrue($view->file_exists($this->folder));
  373. /**
  374. * @var \OCP\Files\Storage $sharedStorage
  375. */
  376. list($sharedStorage,) = $view->resolvePath($this->folder);
  377. $this->assertTrue($sharedStorage->instanceOfStorage('OCA\Files_Sharing\ISharedStorage'));
  378. $sourceStorage = new \OC\Files\Storage\Temporary([]);
  379. $sourceStorage->file_put_contents('foo.txt', 'asd');
  380. $sharedStorage->moveFromStorage($sourceStorage, 'foo.txt', 'bar.txt');
  381. $this->assertTrue($sharedStorage->file_exists('bar.txt'));
  382. $this->assertEquals('asd', $sharedStorage->file_get_contents('bar.txt'));
  383. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  384. $this->view->unlink($this->folder);
  385. $this->shareManager->deleteShare($share);
  386. }
  387. public function testNameConflict() {
  388. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  389. $view1 = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER1 . '/files');
  390. $view1->mkdir('foo');
  391. self::loginHelper(self::TEST_FILES_SHARING_API_USER3);
  392. $view3 = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER3 . '/files');
  393. $view3->mkdir('foo');
  394. // share a folder with the same name from two different users to the same user
  395. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  396. $share1 = $this->share(
  397. IShare::TYPE_GROUP,
  398. 'foo',
  399. self::TEST_FILES_SHARING_API_USER1,
  400. self::TEST_FILES_SHARING_API_GROUP1,
  401. \OCP\Constants::PERMISSION_ALL
  402. );
  403. $this->shareManager->acceptShare($share1, self::TEST_FILES_SHARING_API_USER2);
  404. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  405. self::loginHelper(self::TEST_FILES_SHARING_API_USER3);
  406. $share2 = $this->share(
  407. IShare::TYPE_GROUP,
  408. 'foo',
  409. self::TEST_FILES_SHARING_API_USER3,
  410. self::TEST_FILES_SHARING_API_GROUP1,
  411. \OCP\Constants::PERMISSION_ALL
  412. );
  413. $this->shareManager->acceptShare($share2, self::TEST_FILES_SHARING_API_USER2);
  414. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  415. $view2 = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files');
  416. $this->assertTrue($view2->file_exists('/foo'));
  417. $this->assertTrue($view2->file_exists('/foo (2)'));
  418. $mount = $view2->getMount('/foo');
  419. $this->assertInstanceOf('\OCA\Files_Sharing\SharedMount', $mount);
  420. /** @var \OCA\Files_Sharing\SharedStorage $storage */
  421. $storage = $mount->getStorage();
  422. $this->assertEquals(self::TEST_FILES_SHARING_API_USER1, $storage->getOwner(''));
  423. $this->shareManager->deleteShare($share1);
  424. $this->shareManager->deleteShare($share2);
  425. }
  426. public function testOwnerPermissions() {
  427. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  428. $share = $this->share(
  429. IShare::TYPE_USER,
  430. $this->folder,
  431. self::TEST_FILES_SHARING_API_USER1,
  432. self::TEST_FILES_SHARING_API_USER2,
  433. \OCP\Constants::PERMISSION_ALL - \OCP\Constants::PERMISSION_DELETE
  434. );
  435. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  436. $view = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files');
  437. $this->assertTrue($view->file_exists($this->folder));
  438. $view->file_put_contents($this->folder . '/newfile.txt', 'asd');
  439. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  440. $this->assertTrue($this->view->file_exists($this->folder . '/newfile.txt'));
  441. $this->assertEquals(\OCP\Constants::PERMISSION_ALL - \OCP\Constants::PERMISSION_CREATE,
  442. $this->view->getFileInfo($this->folder . '/newfile.txt')->getPermissions());
  443. $this->view->unlink($this->folder);
  444. $this->shareManager->deleteShare($share);
  445. }
  446. public function testInitWithNonExistingUser() {
  447. $share = $this->createMock(IShare::class);
  448. $share->method('getShareOwner')->willReturn('unexist');
  449. $ownerView = $this->createMock(View::class);
  450. $storage = new SharedStorage([
  451. 'ownerView' => $ownerView,
  452. 'superShare' => $share,
  453. 'groupedShares' => [$share],
  454. 'user' => 'user1',
  455. ]);
  456. // trigger init
  457. $this->assertInstanceOf(\OC\Files\Storage\FailedStorage::class, $storage->getSourceStorage());
  458. $this->assertInstanceOf(\OC\Files\Cache\FailedCache::class, $storage->getCache());
  459. }
  460. public function testInitWithNotFoundSource() {
  461. $share = $this->createMock(IShare::class);
  462. $share->method('getShareOwner')->willReturn(self::TEST_FILES_SHARING_API_USER1);
  463. $ownerView = $this->createMock(View::class);
  464. $ownerView->method('getPath')->will($this->throwException(new NotFoundException()));
  465. $storage = new SharedStorage([
  466. 'ownerView' => $ownerView,
  467. 'superShare' => $share,
  468. 'groupedShares' => [$share],
  469. 'user' => 'user1',
  470. ]);
  471. // trigger init
  472. $this->assertInstanceOf(\OC\Files\Storage\FailedStorage::class, $storage->getSourceStorage());
  473. $this->assertInstanceOf(\OC\Files\Cache\FailedCache::class, $storage->getCache());
  474. }
  475. }