SharesPluginTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Daniel Calviño Sánchez <danxuliu@gmail.com>
  7. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Maxence Lange <maxence@nextcloud.com>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. * @author Robin Appelman <robin@icewind.nl>
  12. * @author Roeland Jago Douma <roeland@famdouma.nl>
  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\DAV\Tests\unit\Connector\Sabre;
  31. use OCA\DAV\Connector\Sabre\Directory;
  32. use OCA\DAV\Connector\Sabre\File;
  33. use OCA\DAV\Connector\Sabre\Node;
  34. use OCA\DAV\Upload\UploadFile;
  35. use OCP\Files\Folder;
  36. use OCP\IUser;
  37. use OCP\IUserSession;
  38. use OCP\Share\IManager;
  39. use OCP\Share\IShare;
  40. use Sabre\DAV\Tree;
  41. class SharesPluginTest extends \Test\TestCase {
  42. public const SHARETYPES_PROPERTYNAME = \OCA\DAV\Connector\Sabre\SharesPlugin::SHARETYPES_PROPERTYNAME;
  43. /**
  44. * @var \Sabre\DAV\Server
  45. */
  46. private $server;
  47. /**
  48. * @var \Sabre\DAV\Tree
  49. */
  50. private $tree;
  51. /**
  52. * @var \OCP\Share\IManager
  53. */
  54. private $shareManager;
  55. /**
  56. * @var \OCP\Files\Folder
  57. */
  58. private $userFolder;
  59. /**
  60. * @var \OCA\DAV\Connector\Sabre\SharesPlugin
  61. */
  62. private $plugin;
  63. protected function setUp(): void {
  64. parent::setUp();
  65. $this->server = new \Sabre\DAV\Server();
  66. $this->tree = $this->createMock(Tree::class);
  67. $this->shareManager = $this->createMock(IManager::class);
  68. $user = $this->createMock(IUser::class);
  69. $user->expects($this->once())
  70. ->method('getUID')
  71. ->willReturn('user1');
  72. $userSession = $this->createMock(IUserSession::class);
  73. $userSession->expects($this->once())
  74. ->method('getUser')
  75. ->willReturn($user);
  76. $this->userFolder = $this->createMock(Folder::class);
  77. $this->plugin = new \OCA\DAV\Connector\Sabre\SharesPlugin(
  78. $this->tree,
  79. $userSession,
  80. $this->userFolder,
  81. $this->shareManager
  82. );
  83. $this->plugin->initialize($this->server);
  84. }
  85. /**
  86. * @dataProvider sharesGetPropertiesDataProvider
  87. */
  88. public function testGetProperties($shareTypes) {
  89. $sabreNode = $this->getMockBuilder(Node::class)
  90. ->disableOriginalConstructor()
  91. ->getMock();
  92. $sabreNode->expects($this->any())
  93. ->method('getId')
  94. ->willReturn(123);
  95. $sabreNode->expects($this->any())
  96. ->method('getPath')
  97. ->willReturn('/subdir');
  98. // node API nodes
  99. $node = $this->getMockBuilder(Folder::class)
  100. ->disableOriginalConstructor()
  101. ->getMock();
  102. $this->userFolder->expects($this->once())
  103. ->method('get')
  104. ->with('/subdir')
  105. ->willReturn($node);
  106. $this->shareManager->expects($this->any())
  107. ->method('getSharesBy')
  108. ->with(
  109. $this->equalTo('user1'),
  110. $this->anything(),
  111. $this->anything(),
  112. $this->equalTo(false),
  113. $this->equalTo(-1)
  114. )
  115. ->willReturnCallback(function ($userId, $requestedShareType, $node, $flag, $limit) use ($shareTypes) {
  116. if (in_array($requestedShareType, $shareTypes)) {
  117. $share = $this->createMock(IShare::class);
  118. $share->method('getShareType')
  119. ->willReturn($requestedShareType);
  120. return [$share];
  121. }
  122. return [];
  123. });
  124. $propFind = new \Sabre\DAV\PropFind(
  125. '/dummyPath',
  126. [self::SHARETYPES_PROPERTYNAME],
  127. 0
  128. );
  129. $this->plugin->handleGetProperties(
  130. $propFind,
  131. $sabreNode
  132. );
  133. $result = $propFind->getResultForMultiStatus();
  134. $this->assertEmpty($result[404]);
  135. unset($result[404]);
  136. $this->assertEquals($shareTypes, $result[200][self::SHARETYPES_PROPERTYNAME]->getShareTypes());
  137. }
  138. /**
  139. * @dataProvider sharesGetPropertiesDataProvider
  140. */
  141. public function testPreloadThenGetProperties($shareTypes) {
  142. $sabreNode1 = $this->createMock(File::class);
  143. $sabreNode1->method('getId')
  144. ->willReturn(111);
  145. $sabreNode2 = $this->createMock(File::class);
  146. $sabreNode2->method('getId')
  147. ->willReturn(222);
  148. $sabreNode2->method('getPath')
  149. ->willReturn('/subdir/foo');
  150. $sabreNode = $this->createMock(Directory::class);
  151. $sabreNode->method('getId')
  152. ->willReturn(123);
  153. // never, because we use getDirectoryListing from the Node API instead
  154. $sabreNode->expects($this->never())
  155. ->method('getChildren');
  156. $sabreNode->expects($this->any())
  157. ->method('getPath')
  158. ->willReturn('/subdir');
  159. // node API nodes
  160. $node = $this->createMock(Folder::class);
  161. $node->method('getId')
  162. ->willReturn(123);
  163. $node1 = $this->createMock(File::class);
  164. $node1->method('getId')
  165. ->willReturn(111);
  166. $node2 = $this->createMock(File::class);
  167. $node2->method('getId')
  168. ->willReturn(222);
  169. $this->userFolder->method('get')
  170. ->with('/subdir')
  171. ->willReturn($node);
  172. $dummyShares = array_map(function ($type) {
  173. $share = $this->getMockBuilder(IShare::class)->getMock();
  174. $share->expects($this->any())
  175. ->method('getShareType')
  176. ->willReturn($type);
  177. return $share;
  178. }, $shareTypes);
  179. $this->shareManager->expects($this->any())
  180. ->method('getSharesBy')
  181. ->with(
  182. $this->equalTo('user1'),
  183. $this->anything(),
  184. $this->anything(),
  185. $this->equalTo(false),
  186. $this->equalTo(-1)
  187. )
  188. ->willReturnCallback(function ($userId, $requestedShareType, $node, $flag, $limit) use ($shareTypes, $dummyShares) {
  189. if ($node->getId() === 111 && in_array($requestedShareType, $shareTypes)) {
  190. foreach ($dummyShares as $dummyShare) {
  191. if ($dummyShare->getShareType() === $requestedShareType) {
  192. return [$dummyShare];
  193. }
  194. }
  195. }
  196. return [];
  197. });
  198. $this->shareManager->expects($this->any())
  199. ->method('getSharesInFolder')
  200. ->with(
  201. $this->equalTo('user1'),
  202. $this->anything(),
  203. $this->equalTo(true)
  204. )
  205. ->willReturnCallback(function ($userId, $node, $flag) use ($shareTypes, $dummyShares) {
  206. return [111 => $dummyShares];
  207. });
  208. // simulate sabre recursive PROPFIND traversal
  209. $propFindRoot = new \Sabre\DAV\PropFind(
  210. '/subdir',
  211. [self::SHARETYPES_PROPERTYNAME],
  212. 1
  213. );
  214. $propFind1 = new \Sabre\DAV\PropFind(
  215. '/subdir/test.txt',
  216. [self::SHARETYPES_PROPERTYNAME],
  217. 0
  218. );
  219. $propFind2 = new \Sabre\DAV\PropFind(
  220. '/subdir/test2.txt',
  221. [self::SHARETYPES_PROPERTYNAME],
  222. 0
  223. );
  224. $this->plugin->handleGetProperties(
  225. $propFindRoot,
  226. $sabreNode
  227. );
  228. $this->plugin->handleGetProperties(
  229. $propFind1,
  230. $sabreNode1
  231. );
  232. $this->plugin->handleGetProperties(
  233. $propFind2,
  234. $sabreNode2
  235. );
  236. $result = $propFind1->getResultForMultiStatus();
  237. $this->assertEmpty($result[404]);
  238. unset($result[404]);
  239. $this->assertEquals($shareTypes, $result[200][self::SHARETYPES_PROPERTYNAME]->getShareTypes());
  240. }
  241. public function sharesGetPropertiesDataProvider() {
  242. return [
  243. [[]],
  244. [[\OCP\Share::SHARE_TYPE_USER]],
  245. [[\OCP\Share::SHARE_TYPE_GROUP]],
  246. [[\OCP\Share::SHARE_TYPE_LINK]],
  247. [[\OCP\Share::SHARE_TYPE_REMOTE]],
  248. [[\OCP\Share::SHARE_TYPE_ROOM]],
  249. [[\OCP\Share::SHARE_TYPE_USER, \OCP\Share::SHARE_TYPE_GROUP]],
  250. [[\OCP\Share::SHARE_TYPE_USER, \OCP\Share::SHARE_TYPE_GROUP, \OCP\Share::SHARE_TYPE_LINK]],
  251. [[\OCP\Share::SHARE_TYPE_USER, \OCP\Share::SHARE_TYPE_LINK]],
  252. [[\OCP\Share::SHARE_TYPE_GROUP, \OCP\Share::SHARE_TYPE_LINK]],
  253. [[\OCP\Share::SHARE_TYPE_USER, \OCP\Share::SHARE_TYPE_REMOTE]],
  254. ];
  255. }
  256. public function testGetPropertiesSkipChunks(): void {
  257. $sabreNode = $this->getMockBuilder(UploadFile::class)
  258. ->disableOriginalConstructor()
  259. ->getMock();
  260. $propFind = new \Sabre\DAV\PropFind(
  261. '/dummyPath',
  262. [self::SHARETYPES_PROPERTYNAME],
  263. 0
  264. );
  265. $this->plugin->handleGetProperties(
  266. $propFind,
  267. $sabreNode
  268. );
  269. $result = $propFind->getResultForMultiStatus();
  270. $this->assertCount(1, $result[404]);
  271. }
  272. }