ShareTest.php 12 KB

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