SharedStorageTest.php 20 KB

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