cache.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. <?php
  2. /**
  3. * @author Björn Schießle <schiessle@owncloud.com>
  4. * @author Joas Schilling <nickvergessen@owncloud.com>
  5. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Robin Appelman <icewind@owncloud.com>
  8. * @author Thomas Müller <thomas.mueller@tmit.eu>
  9. * @author Vincent Petry <pvince81@owncloud.com>
  10. *
  11. * @copyright Copyright (c) 2015, ownCloud, Inc.
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. use OCA\Files_sharing\Tests\TestCase;
  28. /**
  29. * ownCloud
  30. *
  31. * @author Vincent Petry, Bjoern Schiessle
  32. * @copyright 2014 Vincent Petry <pvince81@owncloud.com>
  33. * 2014 Bjoern Schiessle <schiessle@owncloud.com>
  34. *
  35. * This library is free software; you can redistribute it and/or
  36. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  37. * License as published by the Free Software Foundation; either
  38. * version 3 of the License, or any later version.
  39. *
  40. * This library is distributed in the hope that it will be useful,
  41. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  42. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  43. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  44. *
  45. * You should have received a copy of the GNU Affero General Public
  46. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  47. *
  48. */
  49. class Test_Files_Sharing_Cache extends TestCase {
  50. /**
  51. * @var OC\Files\View
  52. */
  53. public $user2View;
  54. /** @var \OC\Files\Cache\Cache */
  55. protected $ownerCache;
  56. /** @var \OC\Files\Cache\Cache */
  57. protected $sharedCache;
  58. /** @var \OC\Files\Storage\Storage */
  59. protected $ownerStorage;
  60. /** @var \OC\Files\Storage\Storage */
  61. protected $sharedStorage;
  62. protected function setUp() {
  63. parent::setUp();
  64. \OC_User::setDisplayName(self::TEST_FILES_SHARING_API_USER1, 'User One');
  65. \OC_User::setDisplayName(self::TEST_FILES_SHARING_API_USER2, 'User Two');
  66. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  67. $this->user2View = new \OC\Files\View('/'. self::TEST_FILES_SHARING_API_USER2 . '/files');
  68. // prepare user1's dir structure
  69. $this->view->mkdir('container');
  70. $this->view->mkdir('container/shareddir');
  71. $this->view->mkdir('container/shareddir/subdir');
  72. $this->view->mkdir('container/shareddir/emptydir');
  73. $textData = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  74. $this->view->file_put_contents('container/not shared.txt', $textData);
  75. $this->view->file_put_contents('container/shared single file.txt', $textData);
  76. $this->view->file_put_contents('container/shareddir/bar.txt', $textData);
  77. $this->view->file_put_contents('container/shareddir/subdir/another.txt', $textData);
  78. $this->view->file_put_contents('container/shareddir/subdir/another too.txt', $textData);
  79. $this->view->file_put_contents('container/shareddir/subdir/not a text file.xml', '<xml></xml>');
  80. list($this->ownerStorage,) = $this->view->resolvePath('');
  81. $this->ownerCache = $this->ownerStorage->getCache();
  82. $this->ownerStorage->getScanner()->scan('');
  83. // share "shareddir" with user2
  84. $fileinfo = $this->view->getFileInfo('container/shareddir');
  85. \OCP\Share::shareItem('folder', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  86. self::TEST_FILES_SHARING_API_USER2, 31);
  87. $fileinfo = $this->view->getFileInfo('container/shared single file.txt');
  88. \OCP\Share::shareItem('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  89. self::TEST_FILES_SHARING_API_USER2, 31);
  90. // login as user2
  91. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  92. // retrieve the shared storage
  93. $secondView = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2);
  94. list($this->sharedStorage,) = $secondView->resolvePath('files/shareddir');
  95. $this->sharedCache = $this->sharedStorage->getCache();
  96. }
  97. protected function tearDown() {
  98. if($this->sharedCache) {
  99. $this->sharedCache->clear();
  100. }
  101. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  102. $fileinfo = $this->view->getFileInfo('container/shareddir');
  103. \OCP\Share::unshare('folder', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  104. self::TEST_FILES_SHARING_API_USER2);
  105. $fileinfo = $this->view->getFileInfo('container/shared single file.txt');
  106. \OCP\Share::unshare('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  107. self::TEST_FILES_SHARING_API_USER2);
  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('%nonexistant%',
  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', null, null, $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', null, null, $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. $fileinfo = $this->view->getFileInfo('container/shareddir/subdir');
  353. \OCP\Share::shareItem('folder', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  354. self::TEST_FILES_SHARING_API_USER3, 31);
  355. self::loginHelper(self::TEST_FILES_SHARING_API_USER3);
  356. $thirdView = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER3 . '/files');
  357. $results = $thirdView->getDirectoryContent('/subdir');
  358. $this->verifyFiles(
  359. array(
  360. array(
  361. 'name' => 'another too.txt',
  362. 'path' => 'another too.txt',
  363. 'mimetype' => 'text/plain',
  364. 'uid_owner' => self::TEST_FILES_SHARING_API_USER1,
  365. 'displayname_owner' => 'User One',
  366. ),
  367. array(
  368. 'name' => 'another.txt',
  369. 'path' => 'another.txt',
  370. 'mimetype' => 'text/plain',
  371. 'uid_owner' => self::TEST_FILES_SHARING_API_USER1,
  372. 'displayname_owner' => 'User One',
  373. ),
  374. array(
  375. 'name' => 'not a text file.xml',
  376. 'path' => 'not a text file.xml',
  377. 'mimetype' => 'application/xml',
  378. 'uid_owner' => self::TEST_FILES_SHARING_API_USER1,
  379. 'displayname_owner' => 'User One',
  380. ),
  381. ),
  382. $results
  383. );
  384. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  385. \OCP\Share::unshare('folder', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  386. self::TEST_FILES_SHARING_API_USER3);
  387. }
  388. /**
  389. * Check if 'results' contains the expected 'examples' only.
  390. *
  391. * @param array $examples array of example files
  392. * @param array $results array of files
  393. */
  394. private function verifyFiles($examples, $results) {
  395. $this->assertEquals(count($examples), count($results));
  396. foreach ($examples as $example) {
  397. foreach ($results as $key => $result) {
  398. if ($result['name'] === $example['name']) {
  399. $this->verifyKeys($example, $result);
  400. unset($results[$key]);
  401. break;
  402. }
  403. }
  404. }
  405. $this->assertEquals(array(), $results);
  406. }
  407. /**
  408. * verify if each value from the result matches the expected result
  409. * @param array $example array with the expected results
  410. * @param array $result array with the results
  411. */
  412. private function verifyKeys($example, $result) {
  413. foreach ($example as $key => $value) {
  414. $this->assertEquals($value, $result[$key]);
  415. }
  416. }
  417. public function testGetPathByIdDirectShare() {
  418. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  419. \OC\Files\Filesystem::file_put_contents('test.txt', 'foo');
  420. $info = \OC\Files\Filesystem::getFileInfo('test.txt');
  421. \OCP\Share::shareItem('file', $info->getId(), \OCP\Share::SHARE_TYPE_USER, self::TEST_FILES_SHARING_API_USER2, \OCP\Constants::PERMISSION_ALL);
  422. \OC_Util::tearDownFS();
  423. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  424. $this->assertTrue(\OC\Files\Filesystem::file_exists('/test.txt'));
  425. list($sharedStorage) = \OC\Files\Filesystem::resolvePath('/' . self::TEST_FILES_SHARING_API_USER2 . '/files/test.txt');
  426. /**
  427. * @var \OC\Files\Storage\Shared $sharedStorage
  428. */
  429. $sharedCache = $sharedStorage->getCache();
  430. $this->assertEquals('', $sharedCache->getPathById($info->getId()));
  431. }
  432. public function testGetPathByIdShareSubFolder() {
  433. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  434. \OC\Files\Filesystem::mkdir('foo');
  435. \OC\Files\Filesystem::mkdir('foo/bar');
  436. \OC\Files\Filesystem::touch('foo/bar/test.txt');
  437. $folderInfo = \OC\Files\Filesystem::getFileInfo('foo');
  438. $fileInfo = \OC\Files\Filesystem::getFileInfo('foo/bar/test.txt');
  439. \OCP\Share::shareItem('folder', $folderInfo->getId(), \OCP\Share::SHARE_TYPE_USER, self::TEST_FILES_SHARING_API_USER2, \OCP\Constants::PERMISSION_ALL);
  440. \OC_Util::tearDownFS();
  441. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  442. $this->assertTrue(\OC\Files\Filesystem::file_exists('/foo'));
  443. list($sharedStorage) = \OC\Files\Filesystem::resolvePath('/' . self::TEST_FILES_SHARING_API_USER2 . '/files/foo');
  444. /**
  445. * @var \OC\Files\Storage\Shared $sharedStorage
  446. */
  447. $sharedCache = $sharedStorage->getCache();
  448. $this->assertEquals('', $sharedCache->getPathById($folderInfo->getId()));
  449. $this->assertEquals('bar/test.txt', $sharedCache->getPathById($fileInfo->getId()));
  450. }
  451. }