You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CacheTest.php 18KB

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