SharesPluginTest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Daniel Calviño Sánchez <danxuliu@gmail.com>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Maxence Lange <maxence@nextcloud.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Robin Appelman <robin@icewind.nl>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Vincent Petry <pvince81@owncloud.com>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OCA\DAV\Tests\unit\Connector\Sabre;
  29. use OCA\DAV\Connector\Sabre\Directory;
  30. use OCA\DAV\Connector\Sabre\File;
  31. use OCA\DAV\Connector\Sabre\Node;
  32. use OCP\Files\Folder;
  33. use OCP\IUser;
  34. use OCP\IUserSession;
  35. use OCP\Share\IManager;
  36. use OCP\Share\IShare;
  37. use Sabre\DAV\Tree;
  38. class SharesPluginTest extends \Test\TestCase {
  39. const SHARETYPES_PROPERTYNAME = \OCA\DAV\Connector\Sabre\SharesPlugin::SHARETYPES_PROPERTYNAME;
  40. /**
  41. * @var \Sabre\DAV\Server
  42. */
  43. private $server;
  44. /**
  45. * @var \Sabre\DAV\Tree
  46. */
  47. private $tree;
  48. /**
  49. * @var \OCP\Share\IManager
  50. */
  51. private $shareManager;
  52. /**
  53. * @var \OCP\Files\Folder
  54. */
  55. private $userFolder;
  56. /**
  57. * @var \OCA\DAV\Connector\Sabre\SharesPlugin
  58. */
  59. private $plugin;
  60. protected function setUp(): void {
  61. parent::setUp();
  62. $this->server = new \Sabre\DAV\Server();
  63. $this->tree = $this->createMock(Tree::class);
  64. $this->shareManager = $this->createMock(IManager::class);
  65. $user = $this->createMock(IUser::class);
  66. $user->expects($this->once())
  67. ->method('getUID')
  68. ->willReturn('user1');
  69. $userSession = $this->createMock(IUserSession::class);
  70. $userSession->expects($this->once())
  71. ->method('getUser')
  72. ->willReturn($user);
  73. $this->userFolder = $this->createMock(Folder::class);
  74. $this->plugin = new \OCA\DAV\Connector\Sabre\SharesPlugin(
  75. $this->tree,
  76. $userSession,
  77. $this->userFolder,
  78. $this->shareManager
  79. );
  80. $this->plugin->initialize($this->server);
  81. }
  82. /**
  83. * @dataProvider sharesGetPropertiesDataProvider
  84. */
  85. public function testGetProperties($shareTypes) {
  86. $sabreNode = $this->getMockBuilder(Node::class)
  87. ->disableOriginalConstructor()
  88. ->getMock();
  89. $sabreNode->expects($this->any())
  90. ->method('getId')
  91. ->willReturn(123);
  92. $sabreNode->expects($this->any())
  93. ->method('getPath')
  94. ->willReturn('/subdir');
  95. // node API nodes
  96. $node = $this->getMockBuilder(Folder::class)
  97. ->disableOriginalConstructor()
  98. ->getMock();
  99. $this->userFolder->expects($this->once())
  100. ->method('get')
  101. ->with('/subdir')
  102. ->willReturn($node);
  103. $this->shareManager->expects($this->any())
  104. ->method('getSharesBy')
  105. ->with(
  106. $this->equalTo('user1'),
  107. $this->anything(),
  108. $this->anything(),
  109. $this->equalTo(false),
  110. $this->equalTo(-1)
  111. )
  112. ->willReturnCallback(function($userId, $requestedShareType, $node, $flag, $limit) use ($shareTypes){
  113. if (in_array($requestedShareType, $shareTypes)) {
  114. $share = $this->createMock(IShare::class);
  115. $share->method('getShareType')
  116. ->willReturn($requestedShareType);
  117. return [$share];
  118. }
  119. return [];
  120. });
  121. $propFind = new \Sabre\DAV\PropFind(
  122. '/dummyPath',
  123. [self::SHARETYPES_PROPERTYNAME],
  124. 0
  125. );
  126. $this->plugin->handleGetProperties(
  127. $propFind,
  128. $sabreNode
  129. );
  130. $result = $propFind->getResultForMultiStatus();
  131. $this->assertEmpty($result[404]);
  132. unset($result[404]);
  133. $this->assertEquals($shareTypes, $result[200][self::SHARETYPES_PROPERTYNAME]->getShareTypes());
  134. }
  135. /**
  136. * @dataProvider sharesGetPropertiesDataProvider
  137. */
  138. public function testPreloadThenGetProperties($shareTypes) {
  139. $sabreNode1 = $this->createMock(File::class);
  140. $sabreNode1->method('getId')
  141. ->willReturn(111);
  142. $sabreNode2 = $this->createMock(File::class);
  143. $sabreNode2->method('getId')
  144. ->willReturn(222);
  145. $sabreNode2->method('getPath')
  146. ->willReturn('/subdir/foo');
  147. $sabreNode = $this->createMock(Directory::class);
  148. $sabreNode->method('getId')
  149. ->willReturn(123);
  150. // never, because we use getDirectoryListing from the Node API instead
  151. $sabreNode->expects($this->never())
  152. ->method('getChildren');
  153. $sabreNode->expects($this->any())
  154. ->method('getPath')
  155. ->willReturn('/subdir');
  156. // node API nodes
  157. $node = $this->createMock(Folder::class);
  158. $node->method('getId')
  159. ->willReturn(123);
  160. $node1 = $this->createMock(File::class);
  161. $node1->method('getId')
  162. ->willReturn(111);
  163. $node2 = $this->createMock(File::class);
  164. $node2->method('getId')
  165. ->willReturn(222);
  166. $this->userFolder->method('get')
  167. ->with('/subdir')
  168. ->willReturn($node);
  169. $dummyShares = array_map(function($type) {
  170. $share = $this->getMockBuilder(IShare::class)->getMock();
  171. $share->expects($this->any())
  172. ->method('getShareType')
  173. ->willReturn($type);
  174. return $share;
  175. }, $shareTypes);
  176. $this->shareManager->expects($this->any())
  177. ->method('getSharesBy')
  178. ->with(
  179. $this->equalTo('user1'),
  180. $this->anything(),
  181. $this->anything(),
  182. $this->equalTo(false),
  183. $this->equalTo(-1)
  184. )
  185. ->willReturnCallback(function($userId, $requestedShareType, $node, $flag, $limit) use ($shareTypes, $dummyShares){
  186. if ($node->getId() === 111 && in_array($requestedShareType, $shareTypes)) {
  187. foreach ($dummyShares as $dummyShare) {
  188. if ($dummyShare->getShareType() === $requestedShareType) {
  189. return [$dummyShare];
  190. }
  191. }
  192. }
  193. return [];
  194. });
  195. $this->shareManager->expects($this->any())
  196. ->method('getSharesInFolder')
  197. ->with(
  198. $this->equalTo('user1'),
  199. $this->anything(),
  200. $this->equalTo(true)
  201. )
  202. ->willReturnCallback(function ($userId, $node, $flag) use ($shareTypes, $dummyShares) {
  203. return [111 => $dummyShares];
  204. });
  205. // simulate sabre recursive PROPFIND traversal
  206. $propFindRoot = new \Sabre\DAV\PropFind(
  207. '/subdir',
  208. [self::SHARETYPES_PROPERTYNAME],
  209. 1
  210. );
  211. $propFind1 = new \Sabre\DAV\PropFind(
  212. '/subdir/test.txt',
  213. [self::SHARETYPES_PROPERTYNAME],
  214. 0
  215. );
  216. $propFind2 = new \Sabre\DAV\PropFind(
  217. '/subdir/test2.txt',
  218. [self::SHARETYPES_PROPERTYNAME],
  219. 0
  220. );
  221. $this->plugin->handleGetProperties(
  222. $propFindRoot,
  223. $sabreNode
  224. );
  225. $this->plugin->handleGetProperties(
  226. $propFind1,
  227. $sabreNode1
  228. );
  229. $this->plugin->handleGetProperties(
  230. $propFind2,
  231. $sabreNode2
  232. );
  233. $result = $propFind1->getResultForMultiStatus();
  234. $this->assertEmpty($result[404]);
  235. unset($result[404]);
  236. $this->assertEquals($shareTypes, $result[200][self::SHARETYPES_PROPERTYNAME]->getShareTypes());
  237. }
  238. function sharesGetPropertiesDataProvider() {
  239. return [
  240. [[]],
  241. [[\OCP\Share::SHARE_TYPE_USER]],
  242. [[\OCP\Share::SHARE_TYPE_GROUP]],
  243. [[\OCP\Share::SHARE_TYPE_LINK]],
  244. [[\OCP\Share::SHARE_TYPE_REMOTE]],
  245. [[\OCP\Share::SHARE_TYPE_ROOM]],
  246. [[\OCP\Share::SHARE_TYPE_USER, \OCP\Share::SHARE_TYPE_GROUP]],
  247. [[\OCP\Share::SHARE_TYPE_USER, \OCP\Share::SHARE_TYPE_GROUP, \OCP\Share::SHARE_TYPE_LINK]],
  248. [[\OCP\Share::SHARE_TYPE_USER, \OCP\Share::SHARE_TYPE_LINK]],
  249. [[\OCP\Share::SHARE_TYPE_GROUP, \OCP\Share::SHARE_TYPE_LINK]],
  250. [[\OCP\Share::SHARE_TYPE_USER, \OCP\Share::SHARE_TYPE_REMOTE]],
  251. ];
  252. }
  253. }