CacheTest.php 17 KB

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