SharesPluginTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Julius Härtl <jus@bitgrid.net>
  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 <vincent@nextcloud.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): void {
  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. $sabreNode->method('getNode')
  103. ->willReturn($node);
  104. $this->shareManager->expects($this->any())
  105. ->method('getSharesBy')
  106. ->with(
  107. $this->equalTo('user1'),
  108. $this->anything(),
  109. $this->anything(),
  110. $this->equalTo(false),
  111. $this->equalTo(-1)
  112. )
  113. ->willReturnCallback(function ($userId, $requestedShareType, $node, $flag, $limit) use ($shareTypes) {
  114. if (in_array($requestedShareType, $shareTypes)) {
  115. $share = $this->createMock(IShare::class);
  116. $share->method('getShareType')
  117. ->willReturn($requestedShareType);
  118. return [$share];
  119. }
  120. return [];
  121. });
  122. $propFind = new \Sabre\DAV\PropFind(
  123. '/dummyPath',
  124. [self::SHARETYPES_PROPERTYNAME],
  125. 0
  126. );
  127. $this->plugin->handleGetProperties(
  128. $propFind,
  129. $sabreNode
  130. );
  131. $result = $propFind->getResultForMultiStatus();
  132. $this->assertEmpty($result[404]);
  133. unset($result[404]);
  134. $this->assertEquals($shareTypes, $result[200][self::SHARETYPES_PROPERTYNAME]->getShareTypes());
  135. }
  136. /**
  137. * @dataProvider sharesGetPropertiesDataProvider
  138. */
  139. public function testPreloadThenGetProperties($shareTypes): void {
  140. $sabreNode1 = $this->createMock(File::class);
  141. $sabreNode1->method('getId')
  142. ->willReturn(111);
  143. $sabreNode2 = $this->createMock(File::class);
  144. $sabreNode2->method('getId')
  145. ->willReturn(222);
  146. $sabreNode2->method('getPath')
  147. ->willReturn('/subdir/foo');
  148. $sabreNode = $this->createMock(Directory::class);
  149. $sabreNode->method('getId')
  150. ->willReturn(123);
  151. // never, because we use getDirectoryListing from the Node API instead
  152. $sabreNode->expects($this->never())
  153. ->method('getChildren');
  154. $sabreNode->expects($this->any())
  155. ->method('getPath')
  156. ->willReturn('/subdir');
  157. // node API nodes
  158. $node = $this->createMock(Folder::class);
  159. $node->method('getId')
  160. ->willReturn(123);
  161. $node1 = $this->createMock(\OC\Files\Node\File::class);
  162. $node1->method('getId')
  163. ->willReturn(111);
  164. $node2 = $this->createMock(\OC\Files\Node\File::class);
  165. $node2->method('getId')
  166. ->willReturn(222);
  167. $sabreNode->method('getNode')
  168. ->willReturn($node);
  169. $sabreNode1->method('getNode')
  170. ->willReturn($node1);
  171. $sabreNode2->method('getNode')
  172. ->willReturn($node2);
  173. $dummyShares = array_map(function ($type) {
  174. $share = $this->getMockBuilder(IShare::class)->getMock();
  175. $share->expects($this->any())
  176. ->method('getShareType')
  177. ->willReturn($type);
  178. return $share;
  179. }, $shareTypes);
  180. $this->shareManager->expects($this->any())
  181. ->method('getSharesBy')
  182. ->with(
  183. $this->equalTo('user1'),
  184. $this->anything(),
  185. $this->anything(),
  186. $this->equalTo(false),
  187. $this->equalTo(-1)
  188. )
  189. ->willReturnCallback(function ($userId, $requestedShareType, $node, $flag, $limit) use ($shareTypes, $dummyShares) {
  190. if ($node->getId() === 111 && in_array($requestedShareType, $shareTypes)) {
  191. foreach ($dummyShares as $dummyShare) {
  192. if ($dummyShare->getShareType() === $requestedShareType) {
  193. return [$dummyShare];
  194. }
  195. }
  196. }
  197. return [];
  198. });
  199. $this->shareManager->expects($this->any())
  200. ->method('getSharesInFolder')
  201. ->with(
  202. $this->equalTo('user1'),
  203. $this->anything(),
  204. $this->equalTo(true)
  205. )
  206. ->willReturnCallback(function ($userId, $node, $flag) use ($shareTypes, $dummyShares) {
  207. return [111 => $dummyShares];
  208. });
  209. // simulate sabre recursive PROPFIND traversal
  210. $propFindRoot = new \Sabre\DAV\PropFind(
  211. '/subdir',
  212. [self::SHARETYPES_PROPERTYNAME],
  213. 1
  214. );
  215. $propFind1 = new \Sabre\DAV\PropFind(
  216. '/subdir/test.txt',
  217. [self::SHARETYPES_PROPERTYNAME],
  218. 0
  219. );
  220. $propFind2 = new \Sabre\DAV\PropFind(
  221. '/subdir/test2.txt',
  222. [self::SHARETYPES_PROPERTYNAME],
  223. 0
  224. );
  225. $this->plugin->handleGetProperties(
  226. $propFindRoot,
  227. $sabreNode
  228. );
  229. $this->plugin->handleGetProperties(
  230. $propFind1,
  231. $sabreNode1
  232. );
  233. $this->plugin->handleGetProperties(
  234. $propFind2,
  235. $sabreNode2
  236. );
  237. $result = $propFind1->getResultForMultiStatus();
  238. $this->assertEmpty($result[404]);
  239. unset($result[404]);
  240. $this->assertEquals($shareTypes, $result[200][self::SHARETYPES_PROPERTYNAME]->getShareTypes());
  241. }
  242. public function sharesGetPropertiesDataProvider() {
  243. return [
  244. [[]],
  245. [[IShare::TYPE_USER]],
  246. [[IShare::TYPE_GROUP]],
  247. [[IShare::TYPE_LINK]],
  248. [[IShare::TYPE_REMOTE]],
  249. [[IShare::TYPE_ROOM]],
  250. [[IShare::TYPE_DECK]],
  251. [[IShare::TYPE_SCIENCEMESH]],
  252. [[IShare::TYPE_USER, IShare::TYPE_GROUP]],
  253. [[IShare::TYPE_USER, IShare::TYPE_GROUP, IShare::TYPE_LINK]],
  254. [[IShare::TYPE_USER, IShare::TYPE_LINK]],
  255. [[IShare::TYPE_GROUP, IShare::TYPE_LINK]],
  256. [[IShare::TYPE_USER, IShare::TYPE_REMOTE]],
  257. ];
  258. }
  259. public function testGetPropertiesSkipChunks(): void {
  260. $sabreNode = $this->getMockBuilder(UploadFile::class)
  261. ->disableOriginalConstructor()
  262. ->getMock();
  263. $propFind = new \Sabre\DAV\PropFind(
  264. '/dummyPath',
  265. [self::SHARETYPES_PROPERTYNAME],
  266. 0
  267. );
  268. $this->plugin->handleGetProperties(
  269. $propFind,
  270. $sabreNode
  271. );
  272. $result = $propFind->getResultForMultiStatus();
  273. $this->assertCount(1, $result[404]);
  274. }
  275. }