ShareTest.php 12 KB

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