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.

cache.php 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Vincent Petry, Bjoern Schiessle
  6. * @copyright 2014 Vincent Petry <pvince81@owncloud.com>
  7. * 2014 Bjoern Schiessle <schiessle@owncloud.com>
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  11. * License as published by the Free Software Foundation; either
  12. * version 3 of the License, or any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public
  20. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. require_once __DIR__ . '/base.php';
  24. class Test_Files_Sharing_Cache extends Test_Files_Sharing_Base {
  25. /**
  26. * @var OC_FilesystemView
  27. */
  28. public $user2View;
  29. function setUp() {
  30. parent::setUp();
  31. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  32. $this->user2View = new \OC\Files\View('/'. self::TEST_FILES_SHARING_API_USER2 . '/files');
  33. // prepare user1's dir structure
  34. $this->view->mkdir('container');
  35. $this->view->mkdir('container/shareddir');
  36. $this->view->mkdir('container/shareddir/subdir');
  37. $this->view->mkdir('container/shareddir/emptydir');
  38. $textData = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  39. $this->view->file_put_contents('container/not shared.txt', $textData);
  40. $this->view->file_put_contents('container/shared single file.txt', $textData);
  41. $this->view->file_put_contents('container/shareddir/bar.txt', $textData);
  42. $this->view->file_put_contents('container/shareddir/subdir/another.txt', $textData);
  43. $this->view->file_put_contents('container/shareddir/subdir/another too.txt', $textData);
  44. $this->view->file_put_contents('container/shareddir/subdir/not a text file.xml', '<xml></xml>');
  45. list($this->ownerStorage, $internalPath) = $this->view->resolvePath('');
  46. $this->ownerCache = $this->ownerStorage->getCache();
  47. $this->ownerStorage->getScanner()->scan('');
  48. // share "shareddir" with user2
  49. $fileinfo = $this->view->getFileInfo('container/shareddir');
  50. \OCP\Share::shareItem('folder', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  51. self::TEST_FILES_SHARING_API_USER2, 31);
  52. $fileinfo = $this->view->getFileInfo('container/shared single file.txt');
  53. \OCP\Share::shareItem('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  54. self::TEST_FILES_SHARING_API_USER2, 31);
  55. // login as user2
  56. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  57. // retrieve the shared storage
  58. $secondView = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2);
  59. list($this->sharedStorage, $internalPath) = $secondView->resolvePath('files/shareddir');
  60. $this->sharedCache = $this->sharedStorage->getCache();
  61. }
  62. function tearDown() {
  63. $this->sharedCache->clear();
  64. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  65. $fileinfo = $this->view->getFileInfo('container/shareddir');
  66. \OCP\Share::unshare('folder', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  67. self::TEST_FILES_SHARING_API_USER2);
  68. $fileinfo = $this->view->getFileInfo('container/shared single file.txt');
  69. \OCP\Share::unshare('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  70. self::TEST_FILES_SHARING_API_USER2);
  71. $this->view->deleteAll('container');
  72. $this->ownerCache->clear();
  73. parent::tearDown();
  74. }
  75. /**
  76. * Test searching by mime type
  77. */
  78. function testSearchByMime() {
  79. $results = $this->sharedStorage->getCache()->searchByMime('text');
  80. $check = array(
  81. array(
  82. 'name' => 'bar.txt',
  83. 'path' => 'bar.txt'
  84. ),
  85. array(
  86. 'name' => 'another too.txt',
  87. 'path' => 'subdir/another too.txt'
  88. ),
  89. array(
  90. 'name' => 'another.txt',
  91. 'path' => 'subdir/another.txt'
  92. ),
  93. );
  94. $this->verifyFiles($check, $results);
  95. $this->verifyFiles($check, $results);
  96. }
  97. function testGetFolderContentsInRoot() {
  98. $results = $this->user2View->getDirectoryContent('/');
  99. // we should get the shared items "shareddir" and "shared single file.txt"
  100. // additional root will always contain the example file "welcome.txt",
  101. // so this will be part of the result
  102. $this->verifyFiles(
  103. array(
  104. array(
  105. 'name' => 'welcome.txt',
  106. 'path' => 'files/welcome.txt',
  107. 'mimetype' => 'text/plain',
  108. ),
  109. array(
  110. 'name' => 'shareddir',
  111. 'path' => 'files/shareddir',
  112. 'mimetype' => 'httpd/unix-directory',
  113. ),
  114. array(
  115. 'name' => 'shared single file.txt',
  116. 'path' => 'files/shared single file.txt',
  117. 'mimetype' => 'text/plain',
  118. ),
  119. ),
  120. $results
  121. );
  122. }
  123. function testGetFolderContentsInSubdir() {
  124. $results = $this->user2View->getDirectoryContent('/shareddir');
  125. $this->verifyFiles(
  126. array(
  127. array(
  128. 'name' => 'bar.txt',
  129. 'path' => 'bar.txt',
  130. 'mimetype' => 'text/plain',
  131. ),
  132. array(
  133. 'name' => 'emptydir',
  134. 'path' => 'emptydir',
  135. 'mimetype' => 'httpd/unix-directory',
  136. ),
  137. array(
  138. 'name' => 'subdir',
  139. 'path' => 'subdir',
  140. 'mimetype' => 'httpd/unix-directory',
  141. ),
  142. ),
  143. $results
  144. );
  145. }
  146. function testGetFolderContentsWhenSubSubdirShared() {
  147. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  148. $fileinfo = $this->view->getFileInfo('container/shareddir/subdir');
  149. \OCP\Share::shareItem('folder', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  150. self::TEST_FILES_SHARING_API_USER3, 31);
  151. self::loginHelper(self::TEST_FILES_SHARING_API_USER3);
  152. $thirdView = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER3 . '/files');
  153. $results = $thirdView->getDirectoryContent('/subdir');
  154. $this->verifyFiles(
  155. array(
  156. array(
  157. 'name' => 'another too.txt',
  158. 'path' => 'another too.txt',
  159. 'mimetype' => 'text/plain',
  160. ),
  161. array(
  162. 'name' => 'another.txt',
  163. 'path' => 'another.txt',
  164. 'mimetype' => 'text/plain',
  165. ),
  166. array(
  167. 'name' => 'not a text file.xml',
  168. 'path' => 'not a text file.xml',
  169. 'mimetype' => 'application/xml',
  170. ),
  171. ),
  172. $results
  173. );
  174. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  175. \OCP\Share::unshare('folder', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  176. self::TEST_FILES_SHARING_API_USER3);
  177. }
  178. /**
  179. * Check if 'results' contains the expected 'examples' only.
  180. *
  181. * @param array $examples array of example files
  182. * @param array $results array of files
  183. */
  184. private function verifyFiles($examples, $results) {
  185. $this->assertEquals(count($examples), count($results));
  186. foreach ($examples as $example) {
  187. foreach ($results as $key => $result) {
  188. if ($result['name'] === $example['name']) {
  189. $this->verifyKeys($example, $result);
  190. unset($results[$key]);
  191. break;
  192. }
  193. }
  194. }
  195. $this->assertTrue(empty($results));
  196. }
  197. /**
  198. * @brief verify if each value from the result matches the expected result
  199. * @param array $example array with the expected results
  200. * @param array $result array with the results
  201. */
  202. private function verifyKeys($example, $result) {
  203. foreach ($example as $key => $value) {
  204. $this->assertEquals($value, $result[$key]);
  205. }
  206. }
  207. public function testGetPathByIdDirectShare() {
  208. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  209. \OC\Files\Filesystem::file_put_contents('test.txt', 'foo');
  210. $info = \OC\Files\Filesystem::getFileInfo('test.txt');
  211. \OCP\Share::shareItem('file', $info->getId(), \OCP\Share::SHARE_TYPE_USER, self::TEST_FILES_SHARING_API_USER2, \OCP\PERMISSION_ALL);
  212. \OC_Util::tearDownFS();
  213. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  214. $this->assertTrue(\OC\Files\Filesystem::file_exists('/test.txt'));
  215. list($sharedStorage) = \OC\Files\Filesystem::resolvePath('/' . self::TEST_FILES_SHARING_API_USER2 . '/files/test.txt');
  216. /**
  217. * @var \OC\Files\Storage\Shared $sharedStorage
  218. */
  219. $sharedCache = $sharedStorage->getCache();
  220. $this->assertEquals('', $sharedCache->getPathById($info->getId()));
  221. }
  222. public function testGetPathByIdShareSubFolder() {
  223. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  224. \OC\Files\Filesystem::mkdir('foo');
  225. \OC\Files\Filesystem::mkdir('foo/bar');
  226. \OC\Files\Filesystem::touch('foo/bar/test.txt', 'bar');
  227. $folderInfo = \OC\Files\Filesystem::getFileInfo('foo');
  228. $fileInfo = \OC\Files\Filesystem::getFileInfo('foo/bar/test.txt');
  229. \OCP\Share::shareItem('folder', $folderInfo->getId(), \OCP\Share::SHARE_TYPE_USER, self::TEST_FILES_SHARING_API_USER2, \OCP\PERMISSION_ALL);
  230. \OC_Util::tearDownFS();
  231. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  232. $this->assertTrue(\OC\Files\Filesystem::file_exists('/foo'));
  233. list($sharedStorage) = \OC\Files\Filesystem::resolvePath('/' . self::TEST_FILES_SHARING_API_USER2 . '/files/foo');
  234. /**
  235. * @var \OC\Files\Storage\Shared $sharedStorage
  236. */
  237. $sharedCache = $sharedStorage->getCache();
  238. $this->assertEquals('', $sharedCache->getPathById($folderInfo->getId()));
  239. $this->assertEquals('bar/test.txt', $sharedCache->getPathById($fileInfo->getId()));
  240. }
  241. }