cache.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. <?php
  2. use OCA\Files_sharing\Tests\TestCase;
  3. /**
  4. * ownCloud
  5. *
  6. * @author Vincent Petry, Bjoern Schiessle
  7. * @copyright 2014 Vincent Petry <pvince81@owncloud.com>
  8. * 2014 Bjoern Schiessle <schiessle@owncloud.com>
  9. *
  10. * This library is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  12. * License as published by the Free Software Foundation; either
  13. * version 3 of the License, or any later version.
  14. *
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public
  21. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. class Test_Files_Sharing_Cache extends TestCase {
  25. /**
  26. * @var OC\Files\View
  27. */
  28. public $user2View;
  29. /** @var \OC\Files\Cache\Cache */
  30. protected $ownerCache;
  31. /** @var \OC\Files\Cache\Cache */
  32. protected $sharedCache;
  33. /** @var \OC\Files\Storage\Storage */
  34. protected $ownerStorage;
  35. /** @var \OC\Files\Storage\Storage */
  36. protected $sharedStorage;
  37. protected function setUp() {
  38. parent::setUp();
  39. \OC_User::setDisplayName(self::TEST_FILES_SHARING_API_USER1, 'User One');
  40. \OC_User::setDisplayName(self::TEST_FILES_SHARING_API_USER2, 'User Two');
  41. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  42. $this->user2View = new \OC\Files\View('/'. self::TEST_FILES_SHARING_API_USER2 . '/files');
  43. // prepare user1's dir structure
  44. $this->view->mkdir('container');
  45. $this->view->mkdir('container/shareddir');
  46. $this->view->mkdir('container/shareddir/subdir');
  47. $this->view->mkdir('container/shareddir/emptydir');
  48. $textData = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  49. $this->view->file_put_contents('container/not shared.txt', $textData);
  50. $this->view->file_put_contents('container/shared single file.txt', $textData);
  51. $this->view->file_put_contents('container/shareddir/bar.txt', $textData);
  52. $this->view->file_put_contents('container/shareddir/subdir/another.txt', $textData);
  53. $this->view->file_put_contents('container/shareddir/subdir/another too.txt', $textData);
  54. $this->view->file_put_contents('container/shareddir/subdir/not a text file.xml', '<xml></xml>');
  55. list($this->ownerStorage,) = $this->view->resolvePath('');
  56. $this->ownerCache = $this->ownerStorage->getCache();
  57. $this->ownerStorage->getScanner()->scan('');
  58. // share "shareddir" with user2
  59. $fileinfo = $this->view->getFileInfo('container/shareddir');
  60. \OCP\Share::shareItem('folder', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  61. self::TEST_FILES_SHARING_API_USER2, 31);
  62. $fileinfo = $this->view->getFileInfo('container/shared single file.txt');
  63. \OCP\Share::shareItem('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  64. self::TEST_FILES_SHARING_API_USER2, 31);
  65. // login as user2
  66. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  67. // retrieve the shared storage
  68. $secondView = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2);
  69. list($this->sharedStorage,) = $secondView->resolvePath('files/shareddir');
  70. $this->sharedCache = $this->sharedStorage->getCache();
  71. }
  72. protected function tearDown() {
  73. $this->sharedCache->clear();
  74. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  75. $fileinfo = $this->view->getFileInfo('container/shareddir');
  76. \OCP\Share::unshare('folder', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  77. self::TEST_FILES_SHARING_API_USER2);
  78. $fileinfo = $this->view->getFileInfo('container/shared single file.txt');
  79. \OCP\Share::unshare('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  80. self::TEST_FILES_SHARING_API_USER2);
  81. $this->view->deleteAll('container');
  82. $this->ownerCache->clear();
  83. parent::tearDown();
  84. }
  85. function searchDataProvider() {
  86. return array(
  87. array('%another%',
  88. array(
  89. array('name' => 'another too.txt', 'path' => 'subdir/another too.txt'),
  90. array('name' => 'another.txt', 'path' => 'subdir/another.txt'),
  91. )
  92. ),
  93. array('%Another%',
  94. array(
  95. array('name' => 'another too.txt', 'path' => 'subdir/another too.txt'),
  96. array('name' => 'another.txt', 'path' => 'subdir/another.txt'),
  97. )
  98. ),
  99. array('%dir%',
  100. array(
  101. array('name' => 'emptydir', 'path' => 'emptydir'),
  102. array('name' => 'subdir', 'path' => 'subdir'),
  103. array('name' => 'shareddir', 'path' => ''),
  104. )
  105. ),
  106. array('%Dir%',
  107. array(
  108. array('name' => 'emptydir', 'path' => 'emptydir'),
  109. array('name' => 'subdir', 'path' => 'subdir'),
  110. array('name' => 'shareddir', 'path' => ''),
  111. )
  112. ),
  113. array('%txt%',
  114. array(
  115. array('name' => 'bar.txt', 'path' => 'bar.txt'),
  116. array('name' => 'another too.txt', 'path' => 'subdir/another too.txt'),
  117. array('name' => 'another.txt', 'path' => 'subdir/another.txt'),
  118. )
  119. ),
  120. array('%Txt%',
  121. array(
  122. array('name' => 'bar.txt', 'path' => 'bar.txt'),
  123. array('name' => 'another too.txt', 'path' => 'subdir/another too.txt'),
  124. array('name' => 'another.txt', 'path' => 'subdir/another.txt'),
  125. )
  126. ),
  127. array('%',
  128. array(
  129. array('name' => 'bar.txt', 'path' => 'bar.txt'),
  130. array('name' => 'emptydir', 'path' => 'emptydir'),
  131. array('name' => 'subdir', 'path' => 'subdir'),
  132. array('name' => 'another too.txt', 'path' => 'subdir/another too.txt'),
  133. array('name' => 'another.txt', 'path' => 'subdir/another.txt'),
  134. array('name' => 'not a text file.xml', 'path' => 'subdir/not a text file.xml'),
  135. array('name' => 'shareddir', 'path' => ''),
  136. )
  137. ),
  138. array('%nonexistant%',
  139. array(
  140. )
  141. ),
  142. );
  143. }
  144. /**
  145. * we cannot use a dataProvider because that would cause the stray hook detection to remove the hooks
  146. * that were added in setUpBeforeClass.
  147. */
  148. function testSearch() {
  149. foreach ($this->searchDataProvider() as $data) {
  150. list($pattern, $expectedFiles) = $data;
  151. $results = $this->sharedStorage->getCache()->search($pattern);
  152. $this->verifyFiles($expectedFiles, $results);
  153. }
  154. }
  155. /**
  156. * Test searching by mime type
  157. */
  158. function testSearchByMime() {
  159. $results = $this->sharedStorage->getCache()->searchByMime('text');
  160. $check = array(
  161. array(
  162. 'name' => 'bar.txt',
  163. 'path' => 'bar.txt'
  164. ),
  165. array(
  166. 'name' => 'another too.txt',
  167. 'path' => 'subdir/another too.txt'
  168. ),
  169. array(
  170. 'name' => 'another.txt',
  171. 'path' => 'subdir/another.txt'
  172. ),
  173. );
  174. $this->verifyFiles($check, $results);
  175. }
  176. function testGetFolderContentsInRoot() {
  177. $results = $this->user2View->getDirectoryContent('/');
  178. // we should get the shared items "shareddir" and "shared single file.txt"
  179. // additional root will always contain the example file "welcome.txt",
  180. // so this will be part of the result
  181. $this->verifyFiles(
  182. array(
  183. array(
  184. 'name' => 'welcome.txt',
  185. 'path' => 'files/welcome.txt',
  186. 'mimetype' => 'text/plain',
  187. ),
  188. array(
  189. 'name' => 'shareddir',
  190. 'path' => 'files/shareddir',
  191. 'mimetype' => 'httpd/unix-directory',
  192. 'uid_owner' => self::TEST_FILES_SHARING_API_USER1,
  193. 'displayname_owner' => 'User One',
  194. ),
  195. array(
  196. 'name' => 'shared single file.txt',
  197. 'path' => 'files/shared single file.txt',
  198. 'mimetype' => 'text/plain',
  199. 'uid_owner' => self::TEST_FILES_SHARING_API_USER1,
  200. 'displayname_owner' => 'User One',
  201. ),
  202. ),
  203. $results
  204. );
  205. }
  206. function testGetFolderContentsInSubdir() {
  207. $results = $this->user2View->getDirectoryContent('/shareddir');
  208. $this->verifyFiles(
  209. array(
  210. array(
  211. 'name' => 'bar.txt',
  212. 'path' => 'bar.txt',
  213. 'mimetype' => 'text/plain',
  214. 'uid_owner' => self::TEST_FILES_SHARING_API_USER1,
  215. 'displayname_owner' => 'User One',
  216. ),
  217. array(
  218. 'name' => 'emptydir',
  219. 'path' => 'emptydir',
  220. 'mimetype' => 'httpd/unix-directory',
  221. 'uid_owner' => self::TEST_FILES_SHARING_API_USER1,
  222. 'displayname_owner' => 'User One',
  223. ),
  224. array(
  225. 'name' => 'subdir',
  226. 'path' => 'subdir',
  227. 'mimetype' => 'httpd/unix-directory',
  228. 'uid_owner' => self::TEST_FILES_SHARING_API_USER1,
  229. 'displayname_owner' => 'User One',
  230. ),
  231. ),
  232. $results
  233. );
  234. }
  235. function testGetFolderContentsWhenSubSubdirShared() {
  236. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  237. $fileinfo = $this->view->getFileInfo('container/shareddir/subdir');
  238. \OCP\Share::shareItem('folder', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  239. self::TEST_FILES_SHARING_API_USER3, 31);
  240. self::loginHelper(self::TEST_FILES_SHARING_API_USER3);
  241. $thirdView = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER3 . '/files');
  242. $results = $thirdView->getDirectoryContent('/subdir');
  243. $this->verifyFiles(
  244. array(
  245. array(
  246. 'name' => 'another too.txt',
  247. 'path' => 'another too.txt',
  248. 'mimetype' => 'text/plain',
  249. 'uid_owner' => self::TEST_FILES_SHARING_API_USER1,
  250. 'displayname_owner' => 'User One',
  251. ),
  252. array(
  253. 'name' => 'another.txt',
  254. 'path' => 'another.txt',
  255. 'mimetype' => 'text/plain',
  256. 'uid_owner' => self::TEST_FILES_SHARING_API_USER1,
  257. 'displayname_owner' => 'User One',
  258. ),
  259. array(
  260. 'name' => 'not a text file.xml',
  261. 'path' => 'not a text file.xml',
  262. 'mimetype' => 'application/xml',
  263. 'uid_owner' => self::TEST_FILES_SHARING_API_USER1,
  264. 'displayname_owner' => 'User One',
  265. ),
  266. ),
  267. $results
  268. );
  269. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  270. \OCP\Share::unshare('folder', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  271. self::TEST_FILES_SHARING_API_USER3);
  272. }
  273. /**
  274. * Check if 'results' contains the expected 'examples' only.
  275. *
  276. * @param array $examples array of example files
  277. * @param array $results array of files
  278. */
  279. private function verifyFiles($examples, $results) {
  280. $this->assertEquals(count($examples), count($results));
  281. foreach ($examples as $example) {
  282. foreach ($results as $key => $result) {
  283. if ($result['name'] === $example['name']) {
  284. $this->verifyKeys($example, $result);
  285. unset($results[$key]);
  286. break;
  287. }
  288. }
  289. }
  290. $this->assertEquals(array(), $results);
  291. }
  292. /**
  293. * verify if each value from the result matches the expected result
  294. * @param array $example array with the expected results
  295. * @param array $result array with the results
  296. */
  297. private function verifyKeys($example, $result) {
  298. foreach ($example as $key => $value) {
  299. $this->assertEquals($value, $result[$key]);
  300. }
  301. }
  302. public function testGetPathByIdDirectShare() {
  303. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  304. \OC\Files\Filesystem::file_put_contents('test.txt', 'foo');
  305. $info = \OC\Files\Filesystem::getFileInfo('test.txt');
  306. \OCP\Share::shareItem('file', $info->getId(), \OCP\Share::SHARE_TYPE_USER, self::TEST_FILES_SHARING_API_USER2, \OCP\Constants::PERMISSION_ALL);
  307. \OC_Util::tearDownFS();
  308. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  309. $this->assertTrue(\OC\Files\Filesystem::file_exists('/test.txt'));
  310. list($sharedStorage) = \OC\Files\Filesystem::resolvePath('/' . self::TEST_FILES_SHARING_API_USER2 . '/files/test.txt');
  311. /**
  312. * @var \OC\Files\Storage\Shared $sharedStorage
  313. */
  314. $sharedCache = $sharedStorage->getCache();
  315. $this->assertEquals('', $sharedCache->getPathById($info->getId()));
  316. }
  317. public function testGetPathByIdShareSubFolder() {
  318. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  319. \OC\Files\Filesystem::mkdir('foo');
  320. \OC\Files\Filesystem::mkdir('foo/bar');
  321. \OC\Files\Filesystem::touch('foo/bar/test.txt');
  322. $folderInfo = \OC\Files\Filesystem::getFileInfo('foo');
  323. $fileInfo = \OC\Files\Filesystem::getFileInfo('foo/bar/test.txt');
  324. \OCP\Share::shareItem('folder', $folderInfo->getId(), \OCP\Share::SHARE_TYPE_USER, self::TEST_FILES_SHARING_API_USER2, \OCP\Constants::PERMISSION_ALL);
  325. \OC_Util::tearDownFS();
  326. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  327. $this->assertTrue(\OC\Files\Filesystem::file_exists('/foo'));
  328. list($sharedStorage) = \OC\Files\Filesystem::resolvePath('/' . self::TEST_FILES_SHARING_API_USER2 . '/files/foo');
  329. /**
  330. * @var \OC\Files\Storage\Shared $sharedStorage
  331. */
  332. $sharedCache = $sharedStorage->getCache();
  333. $this->assertEquals('', $sharedCache->getPathById($folderInfo->getId()));
  334. $this->assertEquals('bar/test.txt', $sharedCache->getPathById($fileInfo->getId()));
  335. }
  336. }