CacheTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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 Jörn Friedrich Dreyer <jfd@butonic.de>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Robin Appelman <robin@icewind.nl>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Stefan Weil <sw@weilnetz.de>
  12. * @author Thomas Müller <thomas.mueller@tmit.eu>
  13. * @author Vincent Petry <pvince81@owncloud.com>
  14. *
  15. * @license AGPL-3.0
  16. *
  17. * This code is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU Affero General Public License, version 3,
  19. * as published by the Free Software Foundation.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License, version 3,
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>
  28. *
  29. */
  30. namespace OCA\Files_Sharing\Tests;
  31. use OC\Files\Storage\Temporary;
  32. use OC\Files\Storage\Wrapper\Jail;
  33. use OCA\Files_Sharing\SharedStorage;
  34. /**
  35. * Class CacheTest
  36. *
  37. * @group DB
  38. */
  39. class CacheTest extends TestCase {
  40. /**
  41. * @var \OC\Files\View
  42. */
  43. public $user2View;
  44. /** @var \OC\Files\Cache\Cache */
  45. protected $ownerCache;
  46. /** @var \OC\Files\Cache\Cache */
  47. protected $sharedCache;
  48. /** @var \OC\Files\Storage\Storage */
  49. protected $ownerStorage;
  50. /** @var \OC\Files\Storage\Storage */
  51. protected $sharedStorage;
  52. /** @var \OCP\Share\IManager */
  53. protected $shareManager;
  54. protected function setUp() {
  55. parent::setUp();
  56. $this->shareManager = \OC::$server->getShareManager();
  57. $userManager = \OC::$server->getUserManager();
  58. $userManager->get(self::TEST_FILES_SHARING_API_USER1)->setDisplayName('User One');
  59. $userManager->get(self::TEST_FILES_SHARING_API_USER2)->setDisplayName('User Two');
  60. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  61. $this->user2View = new \OC\Files\View('/'. self::TEST_FILES_SHARING_API_USER2 . '/files');
  62. // prepare user1's dir structure
  63. $this->view->mkdir('container');
  64. $this->view->mkdir('container/shareddir');
  65. $this->view->mkdir('container/shareddir/subdir');
  66. $this->view->mkdir('container/shareddir/emptydir');
  67. $textData = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  68. $this->view->file_put_contents('container/not shared.txt', $textData);
  69. $this->view->file_put_contents('container/shared single file.txt', $textData);
  70. $this->view->file_put_contents('container/shareddir/bar.txt', $textData);
  71. $this->view->file_put_contents('container/shareddir/subdir/another.txt', $textData);
  72. $this->view->file_put_contents('container/shareddir/subdir/another too.txt', $textData);
  73. $this->view->file_put_contents('container/shareddir/subdir/not a text file.xml', '<xml></xml>');
  74. list($this->ownerStorage,) = $this->view->resolvePath('');
  75. $this->ownerCache = $this->ownerStorage->getCache();
  76. $this->ownerStorage->getScanner()->scan('');
  77. // share "shareddir" with user2
  78. $rootFolder = \OC::$server->getUserFolder(self::TEST_FILES_SHARING_API_USER1);
  79. $node = $rootFolder->get('container/shareddir');
  80. $share = $this->shareManager->newShare();
  81. $share->setNode($node)
  82. ->setShareType(\OCP\Share::SHARE_TYPE_USER)
  83. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  84. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  85. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  86. $this->shareManager->createShare($share);
  87. $node = $rootFolder->get('container/shared single file.txt');
  88. $share = $this->shareManager->newShare();
  89. $share->setNode($node)
  90. ->setShareType(\OCP\Share::SHARE_TYPE_USER)
  91. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  92. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  93. ->setPermissions(\OCP\Constants::PERMISSION_ALL & ~(\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_DELETE));
  94. $this->shareManager->createShare($share);
  95. // login as user2
  96. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  97. // retrieve the shared storage
  98. $secondView = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2);
  99. list($this->sharedStorage,) = $secondView->resolvePath('files/shareddir');
  100. $this->sharedCache = $this->sharedStorage->getCache();
  101. }
  102. protected function tearDown() {
  103. if($this->sharedCache) {
  104. $this->sharedCache->clear();
  105. }
  106. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  107. $shares = $this->shareManager->getSharesBy(self::TEST_FILES_SHARING_API_USER1, \OCP\Share::SHARE_TYPE_USER);
  108. foreach ($shares as $share) {
  109. $this->shareManager->deleteShare($share);
  110. }
  111. $this->view->deleteAll('container');
  112. $this->ownerCache->clear();
  113. parent::tearDown();
  114. }
  115. function searchDataProvider() {
  116. return array(
  117. array('%another%',
  118. array(
  119. array('name' => 'another too.txt', 'path' => 'subdir/another too.txt'),
  120. array('name' => 'another.txt', 'path' => 'subdir/another.txt'),
  121. )
  122. ),
  123. array('%Another%',
  124. array(
  125. array('name' => 'another too.txt', 'path' => 'subdir/another too.txt'),
  126. array('name' => 'another.txt', 'path' => 'subdir/another.txt'),
  127. )
  128. ),
  129. array('%dir%',
  130. array(
  131. array('name' => 'emptydir', 'path' => 'emptydir'),
  132. array('name' => 'subdir', 'path' => 'subdir'),
  133. array('name' => 'shareddir', 'path' => ''),
  134. )
  135. ),
  136. array('%Dir%',
  137. array(
  138. array('name' => 'emptydir', 'path' => 'emptydir'),
  139. array('name' => 'subdir', 'path' => 'subdir'),
  140. array('name' => 'shareddir', 'path' => ''),
  141. )
  142. ),
  143. array('%txt%',
  144. array(
  145. array('name' => 'bar.txt', 'path' => 'bar.txt'),
  146. array('name' => 'another too.txt', 'path' => 'subdir/another too.txt'),
  147. array('name' => 'another.txt', 'path' => 'subdir/another.txt'),
  148. )
  149. ),
  150. array('%Txt%',
  151. array(
  152. array('name' => 'bar.txt', 'path' => 'bar.txt'),
  153. array('name' => 'another too.txt', 'path' => 'subdir/another too.txt'),
  154. array('name' => 'another.txt', 'path' => 'subdir/another.txt'),
  155. )
  156. ),
  157. array('%',
  158. array(
  159. array('name' => 'bar.txt', 'path' => 'bar.txt'),
  160. array('name' => 'emptydir', 'path' => 'emptydir'),
  161. array('name' => 'subdir', 'path' => 'subdir'),
  162. array('name' => 'another too.txt', 'path' => 'subdir/another too.txt'),
  163. array('name' => 'another.txt', 'path' => 'subdir/another.txt'),
  164. array('name' => 'not a text file.xml', 'path' => 'subdir/not a text file.xml'),
  165. array('name' => 'shareddir', 'path' => ''),
  166. )
  167. ),
  168. array('%nonexistent%',
  169. array(
  170. )
  171. ),
  172. );
  173. }
  174. /**
  175. * we cannot use a dataProvider because that would cause the stray hook detection to remove the hooks
  176. * that were added in setUpBeforeClass.
  177. */
  178. function testSearch() {
  179. foreach ($this->searchDataProvider() as $data) {
  180. list($pattern, $expectedFiles) = $data;
  181. $results = $this->sharedStorage->getCache()->search($pattern);
  182. $this->verifyFiles($expectedFiles, $results);
  183. }
  184. }
  185. /**
  186. * Test searching by mime type
  187. */
  188. function testSearchByMime() {
  189. $results = $this->sharedStorage->getCache()->searchByMime('text');
  190. $check = array(
  191. array(
  192. 'name' => 'bar.txt',
  193. 'path' => 'bar.txt'
  194. ),
  195. array(
  196. 'name' => 'another too.txt',
  197. 'path' => 'subdir/another too.txt'
  198. ),
  199. array(
  200. 'name' => 'another.txt',
  201. 'path' => 'subdir/another.txt'
  202. ),
  203. );
  204. $this->verifyFiles($check, $results);
  205. }
  206. /**
  207. * Test searching by tag
  208. */
  209. function testSearchByTag() {
  210. $userId = \OC::$server->getUserSession()->getUser()->getUId();
  211. $id1 = $this->sharedCache->get('bar.txt')['fileid'];
  212. $id2 = $this->sharedCache->get('subdir/another too.txt')['fileid'];
  213. $id3 = $this->sharedCache->get('subdir/not a text file.xml')['fileid'];
  214. $id4 = $this->sharedCache->get('subdir/another.txt')['fileid'];
  215. $tagManager = \OC::$server->getTagManager()->load('files', [], false, $userId);
  216. $tagManager->tagAs($id1, 'tag1');
  217. $tagManager->tagAs($id1, 'tag2');
  218. $tagManager->tagAs($id2, 'tag1');
  219. $tagManager->tagAs($id3, 'tag1');
  220. $tagManager->tagAs($id4, 'tag2');
  221. $results = $this->sharedStorage->getCache()->searchByTag('tag1', $userId);
  222. $check = array(
  223. array(
  224. 'name' => 'bar.txt',
  225. 'path' => 'bar.txt'
  226. ),
  227. array(
  228. 'name' => 'another too.txt',
  229. 'path' => 'subdir/another too.txt'
  230. ),
  231. array(
  232. 'name' => 'not a text file.xml',
  233. 'path' => 'subdir/not a text file.xml'
  234. ),
  235. );
  236. $this->verifyFiles($check, $results);
  237. $tagManager->delete(array('tag1', 'tag2'));
  238. }
  239. /**
  240. * Test searching by tag for multiple sections of the tree
  241. */
  242. function testSearchByTagTree() {
  243. $userId = \OC::$server->getUserSession()->getUser()->getUId();
  244. $this->sharedStorage->mkdir('subdir/emptydir');
  245. $this->sharedStorage->mkdir('subdir/emptydir2');
  246. $this->ownerStorage->getScanner()->scan('');
  247. $allIds = array(
  248. $this->sharedCache->get('')['fileid'],
  249. $this->sharedCache->get('bar.txt')['fileid'],
  250. $this->sharedCache->get('subdir/another too.txt')['fileid'],
  251. $this->sharedCache->get('subdir/not a text file.xml')['fileid'],
  252. $this->sharedCache->get('subdir/another.txt')['fileid'],
  253. $this->sharedCache->get('subdir/emptydir')['fileid'],
  254. $this->sharedCache->get('subdir/emptydir2')['fileid'],
  255. );
  256. $tagManager = \OC::$server->getTagManager()->load('files', [], false, $userId);
  257. foreach ($allIds as $id) {
  258. $tagManager->tagAs($id, 'tag1');
  259. }
  260. $results = $this->sharedStorage->getCache()->searchByTag('tag1', $userId);
  261. $check = array(
  262. array(
  263. 'name' => 'shareddir',
  264. 'path' => ''
  265. ),
  266. array(
  267. 'name' => 'bar.txt',
  268. 'path' => 'bar.txt'
  269. ),
  270. array(
  271. 'name' => 'another.txt',
  272. 'path' => 'subdir/another.txt'
  273. ),
  274. array(
  275. 'name' => 'another too.txt',
  276. 'path' => 'subdir/another too.txt'
  277. ),
  278. array(
  279. 'name' => 'emptydir',
  280. 'path' => 'subdir/emptydir'
  281. ),
  282. array(
  283. 'name' => 'emptydir2',
  284. 'path' => 'subdir/emptydir2'
  285. ),
  286. array(
  287. 'name' => 'not a text file.xml',
  288. 'path' => 'subdir/not a text file.xml'
  289. ),
  290. );
  291. $this->verifyFiles($check, $results);
  292. $tagManager->delete(array('tag1'));
  293. }
  294. function testGetFolderContentsInRoot() {
  295. $results = $this->user2View->getDirectoryContent('/');
  296. // we should get the shared items "shareddir" and "shared single file.txt"
  297. // additional root will always contain the example file "welcome.txt",
  298. // so this will be part of the result
  299. $this->verifyFiles(
  300. array(
  301. array(
  302. 'name' => 'welcome.txt',
  303. 'path' => 'files/welcome.txt',
  304. 'mimetype' => 'text/plain',
  305. ),
  306. array(
  307. 'name' => 'shareddir',
  308. 'path' => 'files/shareddir',
  309. 'mimetype' => 'httpd/unix-directory',
  310. 'uid_owner' => self::TEST_FILES_SHARING_API_USER1,
  311. 'displayname_owner' => 'User One',
  312. ),
  313. array(
  314. 'name' => 'shared single file.txt',
  315. 'path' => 'files/shared single file.txt',
  316. 'mimetype' => 'text/plain',
  317. 'uid_owner' => self::TEST_FILES_SHARING_API_USER1,
  318. 'displayname_owner' => 'User One',
  319. ),
  320. ),
  321. $results
  322. );
  323. }
  324. function testGetFolderContentsInSubdir() {
  325. $results = $this->user2View->getDirectoryContent('/shareddir');
  326. $this->verifyFiles(
  327. array(
  328. array(
  329. 'name' => 'bar.txt',
  330. 'path' => 'bar.txt',
  331. 'mimetype' => 'text/plain',
  332. 'uid_owner' => self::TEST_FILES_SHARING_API_USER1,
  333. 'displayname_owner' => 'User One',
  334. ),
  335. array(
  336. 'name' => 'emptydir',
  337. 'path' => 'emptydir',
  338. 'mimetype' => 'httpd/unix-directory',
  339. 'uid_owner' => self::TEST_FILES_SHARING_API_USER1,
  340. 'displayname_owner' => 'User One',
  341. ),
  342. array(
  343. 'name' => 'subdir',
  344. 'path' => 'subdir',
  345. 'mimetype' => 'httpd/unix-directory',
  346. 'uid_owner' => self::TEST_FILES_SHARING_API_USER1,
  347. 'displayname_owner' => 'User One',
  348. ),
  349. ),
  350. $results
  351. );
  352. }
  353. function testGetFolderContentsWhenSubSubdirShared() {
  354. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  355. $rootFolder = \OC::$server->getUserFolder(self::TEST_FILES_SHARING_API_USER1);
  356. $node = $rootFolder->get('container/shareddir/subdir');
  357. $share = $this->shareManager->newShare();
  358. $share->setNode($node)
  359. ->setShareType(\OCP\Share::SHARE_TYPE_USER)
  360. ->setSharedWith(self::TEST_FILES_SHARING_API_USER3)
  361. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  362. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  363. $share = $this->shareManager->createShare($share);
  364. self::loginHelper(self::TEST_FILES_SHARING_API_USER3);
  365. $thirdView = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER3 . '/files');
  366. $results = $thirdView->getDirectoryContent('/subdir');
  367. $this->verifyFiles(
  368. array(
  369. array(
  370. 'name' => 'another too.txt',
  371. 'path' => 'another too.txt',
  372. 'mimetype' => 'text/plain',
  373. 'uid_owner' => self::TEST_FILES_SHARING_API_USER1,
  374. 'displayname_owner' => 'User One',
  375. ),
  376. array(
  377. 'name' => 'another.txt',
  378. 'path' => 'another.txt',
  379. 'mimetype' => 'text/plain',
  380. 'uid_owner' => self::TEST_FILES_SHARING_API_USER1,
  381. 'displayname_owner' => 'User One',
  382. ),
  383. array(
  384. 'name' => 'not a text file.xml',
  385. 'path' => 'not a text file.xml',
  386. 'mimetype' => 'application/xml',
  387. 'uid_owner' => self::TEST_FILES_SHARING_API_USER1,
  388. 'displayname_owner' => 'User One',
  389. ),
  390. ),
  391. $results
  392. );
  393. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  394. $this->shareManager->deleteShare($share);
  395. }
  396. /**
  397. * Check if 'results' contains the expected 'examples' only.
  398. *
  399. * @param array $examples array of example files
  400. * @param array $results array of files
  401. */
  402. private function verifyFiles($examples, $results) {
  403. $this->assertEquals(count($examples), count($results));
  404. foreach ($examples as $example) {
  405. foreach ($results as $key => $result) {
  406. if ($result['name'] === $example['name']) {
  407. $this->verifyKeys($example, $result);
  408. unset($results[$key]);
  409. break;
  410. }
  411. }
  412. }
  413. $this->assertEquals(array(), $results);
  414. }
  415. /**
  416. * verify if each value from the result matches the expected result
  417. * @param array $example array with the expected results
  418. * @param array $result array with the results
  419. */
  420. private function verifyKeys($example, $result) {
  421. foreach ($example as $key => $value) {
  422. $this->assertEquals($value, $result[$key]);
  423. }
  424. }
  425. public function testGetPathByIdDirectShare() {
  426. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  427. \OC\Files\Filesystem::file_put_contents('test.txt', 'foo');
  428. $info = \OC\Files\Filesystem::getFileInfo('test.txt');
  429. $rootFolder = \OC::$server->getUserFolder(self::TEST_FILES_SHARING_API_USER1);
  430. $node = $rootFolder->get('test.txt');
  431. $share = $this->shareManager->newShare();
  432. $share->setNode($node)
  433. ->setShareType(\OCP\Share::SHARE_TYPE_USER)
  434. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  435. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  436. ->setPermissions(\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_SHARE);
  437. $this->shareManager->createShare($share);
  438. \OC_Util::tearDownFS();
  439. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  440. $this->assertTrue(\OC\Files\Filesystem::file_exists('/test.txt'));
  441. list($sharedStorage) = \OC\Files\Filesystem::resolvePath('/' . self::TEST_FILES_SHARING_API_USER2 . '/files/test.txt');
  442. /**
  443. * @var \OCA\Files_Sharing\SharedStorage $sharedStorage
  444. */
  445. $sharedCache = $sharedStorage->getCache();
  446. $this->assertEquals('', $sharedCache->getPathById($info->getId()));
  447. }
  448. public function testGetPathByIdShareSubFolder() {
  449. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  450. \OC\Files\Filesystem::mkdir('foo');
  451. \OC\Files\Filesystem::mkdir('foo/bar');
  452. \OC\Files\Filesystem::touch('foo/bar/test.txt');
  453. $folderInfo = \OC\Files\Filesystem::getFileInfo('foo');
  454. $fileInfo = \OC\Files\Filesystem::getFileInfo('foo/bar/test.txt');
  455. $rootFolder = \OC::$server->getUserFolder(self::TEST_FILES_SHARING_API_USER1);
  456. $node = $rootFolder->get('foo');
  457. $share = $this->shareManager->newShare();
  458. $share->setNode($node)
  459. ->setShareType(\OCP\Share::SHARE_TYPE_USER)
  460. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  461. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  462. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  463. $this->shareManager->createShare($share);
  464. \OC_Util::tearDownFS();
  465. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  466. $this->assertTrue(\OC\Files\Filesystem::file_exists('/foo'));
  467. list($sharedStorage) = \OC\Files\Filesystem::resolvePath('/' . self::TEST_FILES_SHARING_API_USER2 . '/files/foo');
  468. /**
  469. * @var \OCA\Files_Sharing\SharedStorage $sharedStorage
  470. */
  471. $sharedCache = $sharedStorage->getCache();
  472. $this->assertEquals('', $sharedCache->getPathById($folderInfo->getId()));
  473. $this->assertEquals('bar/test.txt', $sharedCache->getPathById($fileInfo->getId()));
  474. }
  475. public function testNumericStorageId() {
  476. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  477. \OC\Files\Filesystem::mkdir('foo');
  478. $rootFolder = \OC::$server->getUserFolder(self::TEST_FILES_SHARING_API_USER1);
  479. $node = $rootFolder->get('foo');
  480. $share = $this->shareManager->newShare();
  481. $share->setNode($node)
  482. ->setShareType(\OCP\Share::SHARE_TYPE_USER)
  483. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  484. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  485. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  486. $this->shareManager->createShare($share);
  487. \OC_Util::tearDownFS();
  488. list($sourceStorage) = \OC\Files\Filesystem::resolvePath('/' . self::TEST_FILES_SHARING_API_USER1 . '/files/foo');
  489. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  490. $this->assertTrue(\OC\Files\Filesystem::file_exists('/foo'));
  491. /** @var SharedStorage $sharedStorage */
  492. list($sharedStorage) = \OC\Files\Filesystem::resolvePath('/' . self::TEST_FILES_SHARING_API_USER2 . '/files/foo');
  493. $this->assertEquals($sourceStorage->getCache()->getNumericStorageId(), $sharedStorage->getCache()->getNumericStorageId());
  494. }
  495. public function testShareJailedStorage() {
  496. $sourceStorage = new Temporary();
  497. $sourceStorage->mkdir('jail');
  498. $sourceStorage->mkdir('jail/sub');
  499. $sourceStorage->file_put_contents('jail/sub/foo.txt', 'foo');
  500. $jailedSource = new Jail([
  501. 'storage' => $sourceStorage,
  502. 'root' => 'jail'
  503. ]);
  504. $sourceStorage->getScanner()->scan('');
  505. $this->registerMount(self::TEST_FILES_SHARING_API_USER1, $jailedSource, '/' . self::TEST_FILES_SHARING_API_USER1 . '/files/foo');
  506. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  507. $rootFolder = \OC::$server->getUserFolder(self::TEST_FILES_SHARING_API_USER1);
  508. $node = $rootFolder->get('foo/sub');
  509. $share = $this->shareManager->newShare();
  510. $share->setNode($node)
  511. ->setShareType(\OCP\Share::SHARE_TYPE_USER)
  512. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  513. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  514. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  515. $this->shareManager->createShare($share);
  516. \OC_Util::tearDownFS();
  517. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  518. $this->assertEquals('foo', \OC\Files\Filesystem::file_get_contents('/sub/foo.txt'));
  519. \OC\Files\Filesystem::file_put_contents('/sub/bar.txt', 'bar');
  520. /** @var SharedStorage $sharedStorage */
  521. list($sharedStorage) = \OC\Files\Filesystem::resolvePath('/' . self::TEST_FILES_SHARING_API_USER2 . '/files/sub');
  522. $this->assertTrue($sharedStorage->getCache()->inCache('bar.txt'));
  523. $this->assertTrue($sourceStorage->getCache()->inCache('jail/sub/bar.txt'));
  524. }
  525. }