Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

SharesPluginTest.php 7.0KB

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