1
0

ShareTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Michael Gapczynski
  6. * @copyright 2012 Michael Gapczynski mtgap@owncloud.com
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library 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
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. namespace Test\Share;
  22. use OC\Share\Share;
  23. use OCP\DB\QueryBuilder\IQueryBuilder;
  24. use OCP\IDBConnection;
  25. use OCP\IGroup;
  26. use OCP\IGroupManager;
  27. use OCP\IUser;
  28. use OCP\IUserManager;
  29. use OCP\Share\IShare;
  30. /**
  31. * Class Test_Share
  32. *
  33. * @group DB
  34. */
  35. class ShareTest extends \Test\TestCase {
  36. protected $itemType;
  37. protected IUser $user1;
  38. protected IUser $user2;
  39. protected IUser $user3;
  40. protected IUser $user4;
  41. protected IUser $user5;
  42. protected IUser $user6;
  43. protected IUser $groupAndUser_user;
  44. protected IGroup $group1;
  45. protected IGroup $group2;
  46. protected IGroup $groupAndUser_group;
  47. protected string $resharing;
  48. protected string $dateInFuture;
  49. protected string $dateInPast;
  50. protected IGroupManager $groupManager;
  51. protected IUserManager $userManager;
  52. private IDBConnection $connection;
  53. protected function setUp(): void {
  54. parent::setUp();
  55. $this->groupManager = \OC::$server->getGroupManager();
  56. $this->userManager = \OC::$server->getUserManager();
  57. $this->userManager->clearBackends();
  58. $this->userManager->registerBackend(new \Test\Util\User\Dummy());
  59. $this->user1 = $this->userManager->createUser($this->getUniqueID('user1_'), 'pass');
  60. $this->user2 = $this->userManager->createUser($this->getUniqueID('user2_'), 'pass');
  61. $this->user3 = $this->userManager->createUser($this->getUniqueID('user3_'), 'pass');
  62. $this->user4 = $this->userManager->createUser($this->getUniqueID('user4_'), 'pass');
  63. $this->user5 = $this->userManager->createUser($this->getUniqueID('user5_'), 'pass');
  64. $this->user6 = $this->userManager->createUser($this->getUniqueID('user6_'), 'pass');
  65. $groupAndUserId = $this->getUniqueID('groupAndUser_');
  66. $this->groupAndUser_user = $this->userManager->createUser($groupAndUserId, 'pass');
  67. \OC_User::setUserId($this->user1->getUID());
  68. $this->groupManager->clearBackends();
  69. $this->groupManager->addBackend(new \Test\Util\Group\Dummy());
  70. $this->group1 = $this->groupManager->createGroup($this->getUniqueID('group1_'));
  71. $this->group2 = $this->groupManager->createGroup($this->getUniqueID('group2_'));
  72. $this->groupAndUser_group = $this->groupManager->createGroup($groupAndUserId);
  73. $this->connection = \OC::$server->get(IDBConnection::class);
  74. $this->group1->addUser($this->user1);
  75. $this->group1->addUser($this->user2);
  76. $this->group1->addUser($this->user3);
  77. $this->group2->addUser($this->user2);
  78. $this->group2->addUser($this->user4);
  79. $this->groupAndUser_group->addUser($this->user2);
  80. $this->groupAndUser_group->addUser($this->user3);
  81. Share::registerBackend('test', 'Test\Share\Backend');
  82. \OC_Hook::clear('OCP\\Share');
  83. \OC::registerShareHooks(\OC::$server->getSystemConfig());
  84. $this->resharing = \OC::$server->getConfig()->getAppValue('core', 'shareapi_allow_resharing', 'yes');
  85. \OC::$server->getConfig()->setAppValue('core', 'shareapi_allow_resharing', 'yes');
  86. // 20 Minutes in the past, 20 minutes in the future.
  87. $now = time();
  88. $dateFormat = 'Y-m-d H:i:s';
  89. $this->dateInPast = date($dateFormat, $now - 20 * 60);
  90. $this->dateInFuture = date($dateFormat, $now + 20 * 60);
  91. }
  92. protected function tearDown(): void {
  93. $query = $this->connection->getQueryBuilder();
  94. $query->delete('share')->andWhere($query->expr()->eq('item_type', $query->createNamedParameter('test')));
  95. $query->executeStatement();
  96. \OC::$server->getConfig()->setAppValue('core', 'shareapi_allow_resharing', $this->resharing);
  97. $this->user1->delete();
  98. $this->user2->delete();
  99. $this->user3->delete();
  100. $this->user4->delete();
  101. $this->user5->delete();
  102. $this->user6->delete();
  103. $this->groupAndUser_user->delete();
  104. $this->group1->delete();
  105. $this->group2->delete();
  106. $this->groupAndUser_group->delete();
  107. $this->logout();
  108. parent::tearDown();
  109. }
  110. public function testGetItemSharedWithUser() {
  111. \OC_User::setUserId($this->user1->getUID());
  112. // add dummy values to the share table
  113. $query = $this->connection->getQueryBuilder();
  114. $query->insert('share')
  115. ->values([
  116. 'item_type' => $query->createParameter('itemType'),
  117. 'item_source' => $query->createParameter('itemSource'),
  118. 'item_target' => $query->createParameter('itemTarget'),
  119. 'share_type' => $query->createParameter('shareType'),
  120. 'share_with' => $query->createParameter('shareWith'),
  121. 'uid_owner' => $query->createParameter('uidOwner')
  122. ]);
  123. $args = [
  124. ['test', 99, 'target1', IShare::TYPE_USER, $this->user2->getUID(), $this->user1->getUID()],
  125. ['test', 99, 'target2', IShare::TYPE_USER, $this->user4->getUID(), $this->user1->getUID()],
  126. ['test', 99, 'target3', IShare::TYPE_USER, $this->user3->getUID(), $this->user2->getUID()],
  127. ['test', 99, 'target4', IShare::TYPE_USER, $this->user3->getUID(), $this->user4->getUID()],
  128. ['test', 99, 'target4', IShare::TYPE_USER, $this->user6->getUID(), $this->user4->getUID()],
  129. ];
  130. foreach ($args as $row) {
  131. $query->setParameter('itemType', $row[0]);
  132. $query->setParameter('itemSource', $row[1], IQueryBuilder::PARAM_INT);
  133. $query->setParameter('itemTarget', $row[2]);
  134. $query->setParameter('shareType', $row[3], IQueryBuilder::PARAM_INT);
  135. $query->setParameter('shareWith', $row[4]);
  136. $query->setParameter('uidOwner', $row[5]);
  137. $query->executeStatement();
  138. }
  139. $result1 = Share::getItemSharedWithUser('test', 99, $this->user2->getUID(), $this->user1->getUID());
  140. $this->assertSame(1, count($result1));
  141. $this->verifyResult($result1, ['target1']);
  142. $result2 = Share::getItemSharedWithUser('test', 99, null, $this->user1->getUID());
  143. $this->assertSame(2, count($result2));
  144. $this->verifyResult($result2, ['target1', 'target2']);
  145. $result3 = Share::getItemSharedWithUser('test', 99, $this->user3->getUID());
  146. $this->assertSame(2, count($result3));
  147. $this->verifyResult($result3, ['target3', 'target4']);
  148. $result4 = Share::getItemSharedWithUser('test', 99, null, null);
  149. $this->assertSame(5, count($result4)); // 5 because target4 appears twice
  150. $this->verifyResult($result4, ['target1', 'target2', 'target3', 'target4']);
  151. $result6 = Share::getItemSharedWithUser('test', 99, $this->user6->getUID(), null);
  152. $this->assertSame(1, count($result6));
  153. $this->verifyResult($result6, ['target4']);
  154. }
  155. public function testGetItemSharedWithUserFromGroupShare() {
  156. \OC_User::setUserId($this->user1->getUID());
  157. // add dummy values to the share table
  158. $query = $this->connection->getQueryBuilder();
  159. $query->insert('share')
  160. ->values([
  161. 'item_type' => $query->createParameter('itemType'),
  162. 'item_source' => $query->createParameter('itemSource'),
  163. 'item_target' => $query->createParameter('itemTarget'),
  164. 'share_type' => $query->createParameter('shareType'),
  165. 'share_with' => $query->createParameter('shareWith'),
  166. 'uid_owner' => $query->createParameter('uidOwner')
  167. ]);
  168. $args = [
  169. ['test', 99, 'target1', IShare::TYPE_GROUP, $this->group1->getGID(), $this->user1->getUID()],
  170. ['test', 99, 'target2', IShare::TYPE_GROUP, $this->group2->getGID(), $this->user1->getUID()],
  171. ['test', 99, 'target3', IShare::TYPE_GROUP, $this->group1->getGID(), $this->user2->getUID()],
  172. ['test', 99, 'target4', IShare::TYPE_GROUP, $this->group1->getGID(), $this->user4->getUID()],
  173. ];
  174. foreach ($args as $row) {
  175. $query->setParameter('itemType', $row[0]);
  176. $query->setParameter('itemSource', $row[1], IQueryBuilder::PARAM_INT);
  177. $query->setParameter('itemTarget', $row[2]);
  178. $query->setParameter('shareType', $row[3], IQueryBuilder::PARAM_INT);
  179. $query->setParameter('shareWith', $row[4]);
  180. $query->setParameter('uidOwner', $row[5]);
  181. $query->executeStatement();
  182. }
  183. // user2 is in group1 and group2
  184. $result1 = Share::getItemSharedWithUser('test', 99, $this->user2->getUID(), $this->user1->getUID());
  185. $this->assertSame(2, count($result1));
  186. $this->verifyResult($result1, ['target1', 'target2']);
  187. $result2 = Share::getItemSharedWithUser('test', 99, null, $this->user1->getUID());
  188. $this->assertSame(2, count($result2));
  189. $this->verifyResult($result2, ['target1', 'target2']);
  190. // user3 is in group1 and group2
  191. $result3 = Share::getItemSharedWithUser('test', 99, $this->user3->getUID());
  192. $this->assertSame(3, count($result3));
  193. $this->verifyResult($result3, ['target1', 'target3', 'target4']);
  194. $result4 = Share::getItemSharedWithUser('test', 99, null, null);
  195. $this->assertSame(4, count($result4));
  196. $this->verifyResult($result4, ['target1', 'target2', 'target3', 'target4']);
  197. $result6 = Share::getItemSharedWithUser('test', 99, $this->user6->getUID(), null);
  198. $this->assertSame(0, count($result6));
  199. }
  200. public function verifyResult($result, $expected) {
  201. foreach ($result as $r) {
  202. if (in_array($r['item_target'], $expected)) {
  203. $key = array_search($r['item_target'], $expected);
  204. unset($expected[$key]);
  205. }
  206. }
  207. $this->assertEmpty($expected, 'did not found all expected values');
  208. }
  209. /**
  210. * @dataProvider urls
  211. * @param string $url
  212. * @param string $expectedResult
  213. */
  214. public function testRemoveProtocolFromUrl($url, $expectedResult) {
  215. $share = new Share();
  216. $result = self::invokePrivate($share, 'removeProtocolFromUrl', [$url]);
  217. $this->assertSame($expectedResult, $result);
  218. }
  219. public function urls() {
  220. return [
  221. ['http://owncloud.org', 'owncloud.org'],
  222. ['https://owncloud.org', 'owncloud.org'],
  223. ['owncloud.org', 'owncloud.org'],
  224. ];
  225. }
  226. /**
  227. * @dataProvider dataProviderTestGroupItems
  228. * @param array $ungrouped
  229. * @param array $grouped
  230. */
  231. public function testGroupItems($ungrouped, $grouped) {
  232. $result = DummyShareClass::groupItemsTest($ungrouped);
  233. $this->compareArrays($grouped, $result);
  234. }
  235. public function compareArrays($result, $expectedResult) {
  236. foreach ($expectedResult as $key => $value) {
  237. if (is_array($value)) {
  238. $this->compareArrays($result[$key], $value);
  239. } else {
  240. $this->assertSame($value, $result[$key]);
  241. }
  242. }
  243. }
  244. public function dataProviderTestGroupItems() {
  245. return [
  246. // one array with one share
  247. [
  248. [ // input
  249. ['item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_ALL, 'item_target' => 't1']],
  250. [ // expected result
  251. ['item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_ALL, 'item_target' => 't1']]],
  252. // two shares both point to the same source
  253. [
  254. [ // input
  255. ['item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_READ, 'item_target' => 't1'],
  256. ['item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_UPDATE, 'item_target' => 't1'],
  257. ],
  258. [ // expected result
  259. ['item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE, 'item_target' => 't1',
  260. 'grouped' => [
  261. ['item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_READ, 'item_target' => 't1'],
  262. ['item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_UPDATE, 'item_target' => 't1'],
  263. ]
  264. ],
  265. ]
  266. ],
  267. // two shares both point to the same source but with different targets
  268. [
  269. [ // input
  270. ['item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_READ, 'item_target' => 't1'],
  271. ['item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_UPDATE, 'item_target' => 't2'],
  272. ],
  273. [ // expected result
  274. ['item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_READ, 'item_target' => 't1'],
  275. ['item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_UPDATE, 'item_target' => 't2'],
  276. ]
  277. ],
  278. // three shares two point to the same source
  279. [
  280. [ // input
  281. ['item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_READ, 'item_target' => 't1'],
  282. ['item_source' => 2, 'permissions' => \OCP\Constants::PERMISSION_CREATE, 'item_target' => 't2'],
  283. ['item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_UPDATE, 'item_target' => 't1'],
  284. ],
  285. [ // expected result
  286. ['item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE, 'item_target' => 't1',
  287. 'grouped' => [
  288. ['item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_READ, 'item_target' => 't1'],
  289. ['item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_UPDATE, 'item_target' => 't1'],
  290. ]
  291. ],
  292. ['item_source' => 2, 'permissions' => \OCP\Constants::PERMISSION_CREATE, 'item_target' => 't2'],
  293. ]
  294. ],
  295. ];
  296. }
  297. }
  298. class DummyShareClass extends Share {
  299. public static function groupItemsTest($items) {
  300. return parent::groupItems($items, 'test');
  301. }
  302. }
  303. class DummyHookListener {
  304. public static $shareType = null;
  305. public static function listen($params) {
  306. self::$shareType = $params['shareType'];
  307. }
  308. }