CacheTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_Sharing\Tests;
  8. use OC\Files\Storage\Temporary;
  9. use OC\Files\Storage\Wrapper\Jail;
  10. use OCA\Files_Sharing\SharedStorage;
  11. use OCP\Share\IShare;
  12. /**
  13. * Class CacheTest
  14. *
  15. * @group DB
  16. */
  17. class CacheTest extends TestCase {
  18. /**
  19. * @var \OC\Files\View
  20. */
  21. public $user2View;
  22. /** @var \OC\Files\Cache\Cache */
  23. protected $ownerCache;
  24. /** @var \OC\Files\Cache\Cache */
  25. protected $sharedCache;
  26. /** @var \OC\Files\Storage\Storage */
  27. protected $ownerStorage;
  28. /** @var \OC\Files\Storage\Storage */
  29. protected $sharedStorage;
  30. /** @var \OCP\Share\IManager */
  31. protected $shareManager;
  32. protected function setUp(): void {
  33. parent::setUp();
  34. $this->shareManager = \OC::$server->getShareManager();
  35. $userManager = \OC::$server->getUserManager();
  36. $userManager->get(self::TEST_FILES_SHARING_API_USER1)->setDisplayName('User One');
  37. $userManager->get(self::TEST_FILES_SHARING_API_USER2)->setDisplayName('User Two');
  38. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  39. $this->user2View = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files');
  40. // prepare user1's dir structure
  41. $this->view->mkdir('container');
  42. $this->view->mkdir('container/shareddir');
  43. $this->view->mkdir('container/shareddir/subdir');
  44. $this->view->mkdir('container/shareddir/emptydir');
  45. $textData = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  46. $this->view->file_put_contents('container/not shared.txt', $textData);
  47. $this->view->file_put_contents('container/shared single file.txt', $textData);
  48. $this->view->file_put_contents('container/shareddir/bar.txt', $textData);
  49. $this->view->file_put_contents('container/shareddir/subdir/another.txt', $textData);
  50. $this->view->file_put_contents('container/shareddir/subdir/another too.txt', $textData);
  51. $this->view->file_put_contents('container/shareddir/subdir/not a text file.xml', '<xml></xml>');
  52. $this->view->file_put_contents('simplefile.txt', $textData);
  53. [$this->ownerStorage,] = $this->view->resolvePath('');
  54. $this->ownerCache = $this->ownerStorage->getCache();
  55. $this->ownerStorage->getScanner()->scan('');
  56. // share "shareddir" with user2
  57. $rootFolder = \OC::$server->getUserFolder(self::TEST_FILES_SHARING_API_USER1);
  58. $node = $rootFolder->get('container/shareddir');
  59. $share = $this->shareManager->newShare();
  60. $share->setNode($node)
  61. ->setShareType(IShare::TYPE_USER)
  62. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  63. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  64. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  65. $share = $this->shareManager->createShare($share);
  66. $share->setStatus(IShare::STATUS_ACCEPTED);
  67. $this->shareManager->updateShare($share);
  68. $node = $rootFolder->get('container/shared single file.txt');
  69. $share = $this->shareManager->newShare();
  70. $share->setNode($node)
  71. ->setShareType(IShare::TYPE_USER)
  72. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  73. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  74. ->setPermissions(\OCP\Constants::PERMISSION_ALL & ~(\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_DELETE));
  75. $share = $this->shareManager->createShare($share);
  76. $share->setStatus(IShare::STATUS_ACCEPTED);
  77. $this->shareManager->updateShare($share);
  78. // login as user2
  79. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  80. // retrieve the shared storage
  81. $secondView = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2);
  82. [$this->sharedStorage,] = $secondView->resolvePath('files/shareddir');
  83. $this->sharedCache = $this->sharedStorage->getCache();
  84. }
  85. protected function tearDown(): void {
  86. if ($this->sharedCache) {
  87. $this->sharedCache->clear();
  88. }
  89. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  90. $shares = $this->shareManager->getSharesBy(self::TEST_FILES_SHARING_API_USER1, IShare::TYPE_USER);
  91. foreach ($shares as $share) {
  92. $this->shareManager->deleteShare($share);
  93. }
  94. $this->view->deleteAll('container');
  95. $this->ownerCache->clear();
  96. parent::tearDown();
  97. }
  98. public function searchDataProvider() {
  99. return [
  100. ['%another%',
  101. [
  102. ['name' => 'another too.txt', 'path' => 'subdir/another too.txt'],
  103. ['name' => 'another.txt', 'path' => 'subdir/another.txt'],
  104. ]
  105. ],
  106. ['%Another%',
  107. [
  108. ['name' => 'another too.txt', 'path' => 'subdir/another too.txt'],
  109. ['name' => 'another.txt', 'path' => 'subdir/another.txt'],
  110. ]
  111. ],
  112. ['%dir%',
  113. [
  114. ['name' => 'emptydir', 'path' => 'emptydir'],
  115. ['name' => 'subdir', 'path' => 'subdir'],
  116. ['name' => 'shareddir', 'path' => ''],
  117. ]
  118. ],
  119. ['%Dir%',
  120. [
  121. ['name' => 'emptydir', 'path' => 'emptydir'],
  122. ['name' => 'subdir', 'path' => 'subdir'],
  123. ['name' => 'shareddir', 'path' => ''],
  124. ]
  125. ],
  126. ['%txt%',
  127. [
  128. ['name' => 'bar.txt', 'path' => 'bar.txt'],
  129. ['name' => 'another too.txt', 'path' => 'subdir/another too.txt'],
  130. ['name' => 'another.txt', 'path' => 'subdir/another.txt'],
  131. ]
  132. ],
  133. ['%Txt%',
  134. [
  135. ['name' => 'bar.txt', 'path' => 'bar.txt'],
  136. ['name' => 'another too.txt', 'path' => 'subdir/another too.txt'],
  137. ['name' => 'another.txt', 'path' => 'subdir/another.txt'],
  138. ]
  139. ],
  140. ['%',
  141. [
  142. ['name' => 'bar.txt', 'path' => 'bar.txt'],
  143. ['name' => 'emptydir', 'path' => 'emptydir'],
  144. ['name' => 'subdir', 'path' => 'subdir'],
  145. ['name' => 'another too.txt', 'path' => 'subdir/another too.txt'],
  146. ['name' => 'another.txt', 'path' => 'subdir/another.txt'],
  147. ['name' => 'not a text file.xml', 'path' => 'subdir/not a text file.xml'],
  148. ['name' => 'shareddir', 'path' => ''],
  149. ]
  150. ],
  151. ['%nonexistent%',
  152. [
  153. ]
  154. ],
  155. ];
  156. }
  157. /**
  158. * we cannot use a dataProvider because that would cause the stray hook detection to remove the hooks
  159. * that were added in setUpBeforeClass.
  160. */
  161. public function testSearch(): void {
  162. foreach ($this->searchDataProvider() as $data) {
  163. [$pattern, $expectedFiles] = $data;
  164. $results = $this->sharedStorage->getCache()->search($pattern);
  165. $this->verifyFiles($expectedFiles, $results);
  166. }
  167. }
  168. /**
  169. * Test searching by mime type
  170. */
  171. public function testSearchByMime(): void {
  172. $results = $this->sharedStorage->getCache()->searchByMime('text');
  173. $check = [
  174. [
  175. 'name' => 'bar.txt',
  176. 'path' => 'bar.txt'
  177. ],
  178. [
  179. 'name' => 'another too.txt',
  180. 'path' => 'subdir/another too.txt'
  181. ],
  182. [
  183. 'name' => 'another.txt',
  184. 'path' => 'subdir/another.txt'
  185. ],
  186. ];
  187. $this->verifyFiles($check, $results);
  188. }
  189. public function testGetFolderContentsInRoot(): void {
  190. $results = $this->user2View->getDirectoryContent('/');
  191. $results = (array_filter($results, function ($file) {
  192. return $file->getName() !== 'welcome.txt';
  193. }));
  194. // we should get the shared items "shareddir" and "shared single file.txt"
  195. // additional root will always contain the example file "welcome.txt",
  196. // so this will be part of the result
  197. $this->verifyFiles(
  198. [
  199. [
  200. 'name' => 'shareddir',
  201. 'path' => 'files/shareddir',
  202. 'mimetype' => 'httpd/unix-directory',
  203. 'uid_owner' => self::TEST_FILES_SHARING_API_USER1,
  204. 'displayname_owner' => 'User One',
  205. ],
  206. [
  207. 'name' => 'shared single file.txt',
  208. 'path' => 'files/shared single file.txt',
  209. 'mimetype' => 'text/plain',
  210. 'uid_owner' => self::TEST_FILES_SHARING_API_USER1,
  211. 'displayname_owner' => 'User One',
  212. ],
  213. ],
  214. $results
  215. );
  216. }
  217. public function testGetFolderContentsInSubdir(): void {
  218. $results = $this->user2View->getDirectoryContent('/shareddir');
  219. $this->verifyFiles(
  220. [
  221. [
  222. 'name' => 'bar.txt',
  223. 'path' => 'bar.txt',
  224. 'mimetype' => 'text/plain',
  225. 'uid_owner' => self::TEST_FILES_SHARING_API_USER1,
  226. 'displayname_owner' => 'User One',
  227. ],
  228. [
  229. 'name' => 'emptydir',
  230. 'path' => 'emptydir',
  231. 'mimetype' => 'httpd/unix-directory',
  232. 'uid_owner' => self::TEST_FILES_SHARING_API_USER1,
  233. 'displayname_owner' => 'User One',
  234. ],
  235. [
  236. 'name' => 'subdir',
  237. 'path' => 'subdir',
  238. 'mimetype' => 'httpd/unix-directory',
  239. 'uid_owner' => self::TEST_FILES_SHARING_API_USER1,
  240. 'displayname_owner' => 'User One',
  241. ],
  242. ],
  243. $results
  244. );
  245. }
  246. /**
  247. * This covers a bug where the share owners name was propagated
  248. * to the recipient in the recent files API response where the
  249. * share recipient has a different target set
  250. *
  251. * https://github.com/nextcloud/server/issues/39879
  252. */
  253. public function testShareRenameOriginalFileInRecentResults(): void {
  254. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  255. $rootFolder = \OC::$server->getUserFolder(self::TEST_FILES_SHARING_API_USER1);
  256. $node = $rootFolder->get('simplefile.txt');
  257. $share = $this->shareManager->newShare();
  258. $share->setNode($node)
  259. ->setShareType(IShare::TYPE_USER)
  260. ->setSharedWith(self::TEST_FILES_SHARING_API_USER3)
  261. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  262. ->setPermissions(\OCP\Constants::PERMISSION_READ);
  263. $share = $this->shareManager->createShare($share);
  264. $share->setStatus(IShare::STATUS_ACCEPTED);
  265. $this->shareManager->updateShare($share);
  266. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  267. $node->move(self::TEST_FILES_SHARING_API_USER1 . '/files/simplefile2.txt');
  268. self::loginHelper(self::TEST_FILES_SHARING_API_USER3);
  269. $rootFolder = \OC::$server->getUserFolder(self::TEST_FILES_SHARING_API_USER3);
  270. $recents = $rootFolder->getRecent(10);
  271. self::assertEquals([
  272. 'welcome.txt',
  273. 'simplefile.txt'
  274. ], array_map(function ($node) {
  275. return $node->getFileInfo()['name'];
  276. }, $recents));
  277. }
  278. public function testGetFolderContentsWhenSubSubdirShared(): void {
  279. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  280. $rootFolder = \OC::$server->getUserFolder(self::TEST_FILES_SHARING_API_USER1);
  281. $node = $rootFolder->get('container/shareddir/subdir');
  282. $share = $this->shareManager->newShare();
  283. $share->setNode($node)
  284. ->setShareType(IShare::TYPE_USER)
  285. ->setSharedWith(self::TEST_FILES_SHARING_API_USER3)
  286. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  287. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  288. $share = $this->shareManager->createShare($share);
  289. $share->setStatus(IShare::STATUS_ACCEPTED);
  290. $this->shareManager->updateShare($share);
  291. self::loginHelper(self::TEST_FILES_SHARING_API_USER3);
  292. $thirdView = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER3 . '/files');
  293. $results = $thirdView->getDirectoryContent('/subdir');
  294. $this->verifyFiles(
  295. [
  296. [
  297. 'name' => 'another too.txt',
  298. 'path' => 'another too.txt',
  299. 'mimetype' => 'text/plain',
  300. 'uid_owner' => self::TEST_FILES_SHARING_API_USER1,
  301. 'displayname_owner' => 'User One',
  302. ],
  303. [
  304. 'name' => 'another.txt',
  305. 'path' => 'another.txt',
  306. 'mimetype' => 'text/plain',
  307. 'uid_owner' => self::TEST_FILES_SHARING_API_USER1,
  308. 'displayname_owner' => 'User One',
  309. ],
  310. [
  311. 'name' => 'not a text file.xml',
  312. 'path' => 'not a text file.xml',
  313. 'mimetype' => 'application/xml',
  314. 'uid_owner' => self::TEST_FILES_SHARING_API_USER1,
  315. 'displayname_owner' => 'User One',
  316. ],
  317. ],
  318. $results
  319. );
  320. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  321. $this->shareManager->deleteShare($share);
  322. }
  323. /**
  324. * Check if 'results' contains the expected 'examples' only.
  325. *
  326. * @param array $examples array of example files
  327. * @param array $results array of files
  328. */
  329. private function verifyFiles($examples, $results) {
  330. $this->assertEquals(count($examples), count($results));
  331. foreach ($examples as $example) {
  332. foreach ($results as $key => $result) {
  333. if ($result['name'] === $example['name']) {
  334. $this->verifyKeys($example, $result);
  335. unset($results[$key]);
  336. break;
  337. }
  338. }
  339. }
  340. $this->assertEquals([], $results);
  341. }
  342. /**
  343. * verify if each value from the result matches the expected result
  344. * @param array $example array with the expected results
  345. * @param array $result array with the results
  346. */
  347. private function verifyKeys($example, $result) {
  348. foreach ($example as $key => $value) {
  349. $this->assertEquals($value, $result[$key]);
  350. }
  351. }
  352. public function testGetPathByIdDirectShare(): void {
  353. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  354. \OC\Files\Filesystem::file_put_contents('test.txt', 'foo');
  355. $info = \OC\Files\Filesystem::getFileInfo('test.txt');
  356. $rootFolder = \OC::$server->getUserFolder(self::TEST_FILES_SHARING_API_USER1);
  357. $node = $rootFolder->get('test.txt');
  358. $share = $this->shareManager->newShare();
  359. $share->setNode($node)
  360. ->setShareType(IShare::TYPE_USER)
  361. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  362. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  363. ->setPermissions(\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_SHARE);
  364. $share = $this->shareManager->createShare($share);
  365. $share->setStatus(IShare::STATUS_ACCEPTED);
  366. $this->shareManager->updateShare($share);
  367. \OC_Util::tearDownFS();
  368. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  369. $this->assertTrue(\OC\Files\Filesystem::file_exists('/test.txt'));
  370. [$sharedStorage] = \OC\Files\Filesystem::resolvePath('/' . self::TEST_FILES_SHARING_API_USER2 . '/files/test.txt');
  371. /**
  372. * @var \OCA\Files_Sharing\SharedStorage $sharedStorage
  373. */
  374. $sharedCache = $sharedStorage->getCache();
  375. $this->assertEquals('', $sharedCache->getPathById($info->getId()));
  376. }
  377. public function testGetPathByIdShareSubFolder(): void {
  378. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  379. \OC\Files\Filesystem::mkdir('foo');
  380. \OC\Files\Filesystem::mkdir('foo/bar');
  381. \OC\Files\Filesystem::touch('foo/bar/test.txt');
  382. $folderInfo = \OC\Files\Filesystem::getFileInfo('foo');
  383. $fileInfo = \OC\Files\Filesystem::getFileInfo('foo/bar/test.txt');
  384. $rootFolder = \OC::$server->getUserFolder(self::TEST_FILES_SHARING_API_USER1);
  385. $node = $rootFolder->get('foo');
  386. $share = $this->shareManager->newShare();
  387. $share->setNode($node)
  388. ->setShareType(IShare::TYPE_USER)
  389. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  390. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  391. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  392. $share = $this->shareManager->createShare($share);
  393. $share->setStatus(IShare::STATUS_ACCEPTED);
  394. $this->shareManager->updateShare($share);
  395. \OC_Util::tearDownFS();
  396. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  397. $this->assertTrue(\OC\Files\Filesystem::file_exists('/foo'));
  398. [$sharedStorage] = \OC\Files\Filesystem::resolvePath('/' . self::TEST_FILES_SHARING_API_USER2 . '/files/foo');
  399. /**
  400. * @var \OCA\Files_Sharing\SharedStorage $sharedStorage
  401. */
  402. $sharedCache = $sharedStorage->getCache();
  403. $this->assertEquals('', $sharedCache->getPathById($folderInfo->getId()));
  404. $this->assertEquals('bar/test.txt', $sharedCache->getPathById($fileInfo->getId()));
  405. }
  406. public function testNumericStorageId(): void {
  407. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  408. \OC\Files\Filesystem::mkdir('foo');
  409. $rootFolder = \OC::$server->getUserFolder(self::TEST_FILES_SHARING_API_USER1);
  410. $node = $rootFolder->get('foo');
  411. $share = $this->shareManager->newShare();
  412. $share->setNode($node)
  413. ->setShareType(IShare::TYPE_USER)
  414. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  415. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  416. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  417. $share = $this->shareManager->createShare($share);
  418. $share->setStatus(IShare::STATUS_ACCEPTED);
  419. $this->shareManager->updateShare($share);
  420. \OC_Util::tearDownFS();
  421. [$sourceStorage] = \OC\Files\Filesystem::resolvePath('/' . self::TEST_FILES_SHARING_API_USER1 . '/files/foo');
  422. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  423. $this->assertTrue(\OC\Files\Filesystem::file_exists('/foo'));
  424. /** @var SharedStorage $sharedStorage */
  425. [$sharedStorage] = \OC\Files\Filesystem::resolvePath('/' . self::TEST_FILES_SHARING_API_USER2 . '/files/foo');
  426. $this->assertEquals($sourceStorage->getCache()->getNumericStorageId(), $sharedStorage->getCache()->getNumericStorageId());
  427. }
  428. public function testShareJailedStorage(): void {
  429. $sourceStorage = new Temporary();
  430. $sourceStorage->mkdir('jail');
  431. $sourceStorage->mkdir('jail/sub');
  432. $sourceStorage->file_put_contents('jail/sub/foo.txt', 'foo');
  433. $jailedSource = new Jail([
  434. 'storage' => $sourceStorage,
  435. 'root' => 'jail'
  436. ]);
  437. $sourceStorage->getScanner()->scan('');
  438. $this->registerMount(self::TEST_FILES_SHARING_API_USER1, $jailedSource, '/' . self::TEST_FILES_SHARING_API_USER1 . '/files/foo');
  439. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  440. $rootFolder = \OC::$server->getUserFolder(self::TEST_FILES_SHARING_API_USER1);
  441. $node = $rootFolder->get('foo/sub');
  442. $share = $this->shareManager->newShare();
  443. $share->setNode($node)
  444. ->setShareType(IShare::TYPE_USER)
  445. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  446. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  447. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  448. $share = $this->shareManager->createShare($share);
  449. $share->setStatus(IShare::STATUS_ACCEPTED);
  450. $this->shareManager->updateShare($share);
  451. \OC_Util::tearDownFS();
  452. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  453. $this->assertEquals('foo', \OC\Files\Filesystem::file_get_contents('/sub/foo.txt'));
  454. \OC\Files\Filesystem::file_put_contents('/sub/bar.txt', 'bar');
  455. /** @var SharedStorage $sharedStorage */
  456. [$sharedStorage] = \OC\Files\Filesystem::resolvePath('/' . self::TEST_FILES_SHARING_API_USER2 . '/files/sub');
  457. $this->assertTrue($sharedStorage->getCache()->inCache('bar.txt'));
  458. $this->assertTrue($sourceStorage->getCache()->inCache('jail/sub/bar.txt'));
  459. }
  460. public function testSearchShareJailedStorage(): void {
  461. $sourceStorage = new Temporary();
  462. $sourceStorage->mkdir('jail');
  463. $sourceStorage->mkdir('jail/sub');
  464. $sourceStorage->file_put_contents('jail/sub/foo.txt', 'foo');
  465. $jailedSource = new Jail([
  466. 'storage' => $sourceStorage,
  467. 'root' => 'jail'
  468. ]);
  469. $sourceStorage->getScanner()->scan('');
  470. $this->registerMount(self::TEST_FILES_SHARING_API_USER1, $jailedSource, '/' . self::TEST_FILES_SHARING_API_USER1 . '/files/foo');
  471. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  472. $rootFolder = \OC::$server->getUserFolder(self::TEST_FILES_SHARING_API_USER1);
  473. $node = $rootFolder->get('foo/sub');
  474. $share = $this->shareManager->newShare();
  475. $share->setNode($node)
  476. ->setShareType(IShare::TYPE_USER)
  477. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  478. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  479. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  480. $share = $this->shareManager->createShare($share);
  481. $share->setStatus(IShare::STATUS_ACCEPTED);
  482. $this->shareManager->updateShare($share);
  483. \OC_Util::tearDownFS();
  484. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  485. /** @var SharedStorage $sharedStorage */
  486. [$sharedStorage] = \OC\Files\Filesystem::resolvePath('/' . self::TEST_FILES_SHARING_API_USER2 . '/files/sub');
  487. $results = $sharedStorage->getCache()->search('foo.txt');
  488. $this->assertCount(1, $results);
  489. }
  490. }