1
0

ShareTest.php 8.3 KB

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