ShareTest.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test\Share;
  8. use OC\Share\Share;
  9. use OCP\IDBConnection;
  10. use OCP\IGroup;
  11. use OCP\IGroupManager;
  12. use OCP\IUser;
  13. use OCP\IUserManager;
  14. /**
  15. * Class Test_Share
  16. *
  17. * @group DB
  18. */
  19. class ShareTest extends \Test\TestCase {
  20. protected $itemType;
  21. protected IUser $user1;
  22. protected IUser $user2;
  23. protected IUser $user3;
  24. protected IUser $user4;
  25. protected IUser $user5;
  26. protected IUser $user6;
  27. protected IUser $groupAndUser_user;
  28. protected IGroup $group1;
  29. protected IGroup $group2;
  30. protected IGroup $groupAndUser_group;
  31. protected string $resharing;
  32. protected string $dateInFuture;
  33. protected string $dateInPast;
  34. protected IGroupManager $groupManager;
  35. protected IUserManager $userManager;
  36. private IDBConnection $connection;
  37. protected function setUp(): void {
  38. parent::setUp();
  39. $this->groupManager = \OC::$server->getGroupManager();
  40. $this->userManager = \OC::$server->getUserManager();
  41. $this->userManager->clearBackends();
  42. $this->userManager->registerBackend(new \Test\Util\User\Dummy());
  43. $this->user1 = $this->userManager->createUser($this->getUniqueID('user1_'), 'pass');
  44. $this->user2 = $this->userManager->createUser($this->getUniqueID('user2_'), 'pass');
  45. $this->user3 = $this->userManager->createUser($this->getUniqueID('user3_'), 'pass');
  46. $this->user4 = $this->userManager->createUser($this->getUniqueID('user4_'), 'pass');
  47. $this->user5 = $this->userManager->createUser($this->getUniqueID('user5_'), 'pass');
  48. $this->user6 = $this->userManager->createUser($this->getUniqueID('user6_'), 'pass');
  49. $groupAndUserId = $this->getUniqueID('groupAndUser_');
  50. $this->groupAndUser_user = $this->userManager->createUser($groupAndUserId, 'pass');
  51. \OC_User::setUserId($this->user1->getUID());
  52. $this->groupManager->clearBackends();
  53. $this->groupManager->addBackend(new \Test\Util\Group\Dummy());
  54. $this->group1 = $this->groupManager->createGroup($this->getUniqueID('group1_'));
  55. $this->group2 = $this->groupManager->createGroup($this->getUniqueID('group2_'));
  56. $this->groupAndUser_group = $this->groupManager->createGroup($groupAndUserId);
  57. $this->connection = \OC::$server->get(IDBConnection::class);
  58. $this->group1->addUser($this->user1);
  59. $this->group1->addUser($this->user2);
  60. $this->group1->addUser($this->user3);
  61. $this->group2->addUser($this->user2);
  62. $this->group2->addUser($this->user4);
  63. $this->groupAndUser_group->addUser($this->user2);
  64. $this->groupAndUser_group->addUser($this->user3);
  65. Share::registerBackend('test', 'Test\Share\Backend');
  66. \OC_Hook::clear('OCP\\Share');
  67. \OC::registerShareHooks(\OC::$server->getSystemConfig());
  68. $this->resharing = \OC::$server->getConfig()->getAppValue('core', 'shareapi_allow_resharing', 'yes');
  69. \OC::$server->getConfig()->setAppValue('core', 'shareapi_allow_resharing', 'yes');
  70. // 20 Minutes in the past, 20 minutes in the future.
  71. $now = time();
  72. $dateFormat = 'Y-m-d H:i:s';
  73. $this->dateInPast = date($dateFormat, $now - 20 * 60);
  74. $this->dateInFuture = date($dateFormat, $now + 20 * 60);
  75. }
  76. protected function tearDown(): void {
  77. $query = $this->connection->getQueryBuilder();
  78. $query->delete('share')->andWhere($query->expr()->eq('item_type', $query->createNamedParameter('test')));
  79. $query->executeStatement();
  80. \OC::$server->getConfig()->setAppValue('core', 'shareapi_allow_resharing', $this->resharing);
  81. $this->user1->delete();
  82. $this->user2->delete();
  83. $this->user3->delete();
  84. $this->user4->delete();
  85. $this->user5->delete();
  86. $this->user6->delete();
  87. $this->groupAndUser_user->delete();
  88. $this->group1->delete();
  89. $this->group2->delete();
  90. $this->groupAndUser_group->delete();
  91. $this->logout();
  92. parent::tearDown();
  93. }
  94. public function verifyResult($result, $expected) {
  95. foreach ($result as $r) {
  96. if (in_array($r['item_target'], $expected)) {
  97. $key = array_search($r['item_target'], $expected);
  98. unset($expected[$key]);
  99. }
  100. }
  101. $this->assertEmpty($expected, 'did not found all expected values');
  102. }
  103. /**
  104. * @dataProvider urls
  105. * @param string $url
  106. * @param string $expectedResult
  107. */
  108. public function testRemoveProtocolFromUrl($url, $expectedResult) {
  109. $share = new Share();
  110. $result = self::invokePrivate($share, 'removeProtocolFromUrl', [$url]);
  111. $this->assertSame($expectedResult, $result);
  112. }
  113. public function urls() {
  114. return [
  115. ['http://owncloud.org', 'owncloud.org'],
  116. ['https://owncloud.org', 'owncloud.org'],
  117. ['owncloud.org', 'owncloud.org'],
  118. ];
  119. }
  120. /**
  121. * @dataProvider dataProviderTestGroupItems
  122. * @param array $ungrouped
  123. * @param array $grouped
  124. */
  125. public function testGroupItems($ungrouped, $grouped) {
  126. $result = DummyShareClass::groupItemsTest($ungrouped);
  127. $this->compareArrays($grouped, $result);
  128. }
  129. public function compareArrays($result, $expectedResult) {
  130. foreach ($expectedResult as $key => $value) {
  131. if (is_array($value)) {
  132. $this->compareArrays($result[$key], $value);
  133. } else {
  134. $this->assertSame($value, $result[$key]);
  135. }
  136. }
  137. }
  138. public function dataProviderTestGroupItems() {
  139. return [
  140. // one array with one share
  141. [
  142. [ // input
  143. ['item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_ALL, 'item_target' => 't1']],
  144. [ // expected result
  145. ['item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_ALL, 'item_target' => 't1']]],
  146. // two shares both point to the same source
  147. [
  148. [ // input
  149. ['item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_READ, 'item_target' => 't1'],
  150. ['item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_UPDATE, 'item_target' => 't1'],
  151. ],
  152. [ // expected result
  153. ['item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE, 'item_target' => 't1',
  154. 'grouped' => [
  155. ['item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_READ, 'item_target' => 't1'],
  156. ['item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_UPDATE, 'item_target' => 't1'],
  157. ]
  158. ],
  159. ]
  160. ],
  161. // two shares both point to the same source but with different targets
  162. [
  163. [ // input
  164. ['item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_READ, 'item_target' => 't1'],
  165. ['item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_UPDATE, 'item_target' => 't2'],
  166. ],
  167. [ // expected result
  168. ['item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_READ, 'item_target' => 't1'],
  169. ['item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_UPDATE, 'item_target' => 't2'],
  170. ]
  171. ],
  172. // three shares two point to the same source
  173. [
  174. [ // input
  175. ['item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_READ, 'item_target' => 't1'],
  176. ['item_source' => 2, 'permissions' => \OCP\Constants::PERMISSION_CREATE, 'item_target' => 't2'],
  177. ['item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_UPDATE, 'item_target' => 't1'],
  178. ],
  179. [ // expected result
  180. ['item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE, 'item_target' => 't1',
  181. 'grouped' => [
  182. ['item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_READ, 'item_target' => 't1'],
  183. ['item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_UPDATE, 'item_target' => 't1'],
  184. ]
  185. ],
  186. ['item_source' => 2, 'permissions' => \OCP\Constants::PERMISSION_CREATE, 'item_target' => 't2'],
  187. ]
  188. ],
  189. ];
  190. }
  191. }
  192. class DummyShareClass extends Share {
  193. public static function groupItemsTest($items) {
  194. return parent::groupItems($items, 'test');
  195. }
  196. }
  197. class DummyHookListener {
  198. public static $shareType = null;
  199. public static function listen($params) {
  200. self::$shareType = $params['shareType'];
  201. }
  202. }