ShareTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. /**
  118. * @param boolean|string $token
  119. * @return array
  120. */
  121. protected function getShareByValidToken($token) {
  122. $row = \OCP\Share::getShareByToken($token);
  123. $this->assertInternalType(
  124. 'array',
  125. $row,
  126. "Failed asserting that a share for token $token exists."
  127. );
  128. return $row;
  129. }
  130. public function testGetItemSharedWithUser() {
  131. \OC_User::setUserId($this->user1->getUID());
  132. //add dummy values to the share table
  133. $query = \OC_DB::prepare('INSERT INTO `*PREFIX*share` ('
  134. .' `item_type`, `item_source`, `item_target`, `share_type`,'
  135. .' `share_with`, `uid_owner`) VALUES (?,?,?,?,?,?)');
  136. $args = array('test', 99, 'target1', \OCP\Share::SHARE_TYPE_USER, $this->user2->getUID(), $this->user1->getUID());
  137. $query->execute($args);
  138. $args = array('test', 99, 'target2', \OCP\Share::SHARE_TYPE_USER, $this->user4->getUID(), $this->user1->getUID());
  139. $query->execute($args);
  140. $args = array('test', 99, 'target3', \OCP\Share::SHARE_TYPE_USER, $this->user3->getUID(), $this->user2->getUID());
  141. $query->execute($args);
  142. $args = array('test', 99, 'target4', \OCP\Share::SHARE_TYPE_USER, $this->user3->getUID(), $this->user4->getUID());
  143. $query->execute($args);
  144. $args = array('test', 99, 'target4', \OCP\Share::SHARE_TYPE_USER, $this->user6->getUID(), $this->user4->getUID());
  145. $query->execute($args);
  146. $result1 = \OCP\Share::getItemSharedWithUser('test', 99, $this->user2->getUID(), $this->user1->getUID());
  147. $this->assertSame(1, count($result1));
  148. $this->verifyResult($result1, array('target1'));
  149. $result2 = \OCP\Share::getItemSharedWithUser('test', 99, null, $this->user1->getUID());
  150. $this->assertSame(2, count($result2));
  151. $this->verifyResult($result2, array('target1', 'target2'));
  152. $result3 = \OCP\Share::getItemSharedWithUser('test', 99, $this->user3->getUID());
  153. $this->assertSame(2, count($result3));
  154. $this->verifyResult($result3, array('target3', 'target4'));
  155. $result4 = \OCP\Share::getItemSharedWithUser('test', 99, null, null);
  156. $this->assertSame(5, count($result4)); // 5 because target4 appears twice
  157. $this->verifyResult($result4, array('target1', 'target2', 'target3', 'target4'));
  158. $result6 = \OCP\Share::getItemSharedWithUser('test', 99, $this->user6->getUID(), null);
  159. $this->assertSame(1, count($result6));
  160. $this->verifyResult($result6, array('target4'));
  161. }
  162. public function testGetItemSharedWithUserFromGroupShare() {
  163. \OC_User::setUserId($this->user1->getUID());
  164. //add dummy values to the share table
  165. $query = \OC_DB::prepare('INSERT INTO `*PREFIX*share` ('
  166. .' `item_type`, `item_source`, `item_target`, `share_type`,'
  167. .' `share_with`, `uid_owner`) VALUES (?,?,?,?,?,?)');
  168. $args = array('test', 99, 'target1', \OCP\Share::SHARE_TYPE_GROUP, $this->group1->getGID(), $this->user1->getUID());
  169. $query->execute($args);
  170. $args = array('test', 99, 'target2', \OCP\Share::SHARE_TYPE_GROUP, $this->group2->getGID(), $this->user1->getUID());
  171. $query->execute($args);
  172. $args = array('test', 99, 'target3', \OCP\Share::SHARE_TYPE_GROUP, $this->group1->getGID(), $this->user2->getUID());
  173. $query->execute($args);
  174. $args = array('test', 99, 'target4', \OCP\Share::SHARE_TYPE_GROUP, $this->group1->getGID(), $this->user4->getUID());
  175. $query->execute($args);
  176. // user2 is in group1 and group2
  177. $result1 = \OCP\Share::getItemSharedWithUser('test', 99, $this->user2->getUID(), $this->user1->getUID());
  178. $this->assertSame(2, count($result1));
  179. $this->verifyResult($result1, array('target1', 'target2'));
  180. $result2 = \OCP\Share::getItemSharedWithUser('test', 99, null, $this->user1->getUID());
  181. $this->assertSame(2, count($result2));
  182. $this->verifyResult($result2, array('target1', 'target2'));
  183. // user3 is in group1 and group2
  184. $result3 = \OCP\Share::getItemSharedWithUser('test', 99, $this->user3->getUID());
  185. $this->assertSame(3, count($result3));
  186. $this->verifyResult($result3, array('target1', 'target3', 'target4'));
  187. $result4 = \OCP\Share::getItemSharedWithUser('test', 99, null, null);
  188. $this->assertSame(4, count($result4));
  189. $this->verifyResult($result4, array('target1', 'target2', 'target3', 'target4'));
  190. $result6 = \OCP\Share::getItemSharedWithUser('test', 99, $this->user6->getUID(), null);
  191. $this->assertSame(0, count($result6));
  192. }
  193. public function verifyResult($result, $expected) {
  194. foreach ($result as $r) {
  195. if (in_array($r['item_target'], $expected)) {
  196. $key = array_search($r['item_target'], $expected);
  197. unset($expected[$key]);
  198. }
  199. }
  200. $this->assertEmpty($expected, 'did not found all expected values');
  201. }
  202. /**
  203. * @dataProvider checkPasswordProtectedShareDataProvider
  204. * @param $expected
  205. * @param $item
  206. */
  207. public function testCheckPasswordProtectedShare($expected, $item) {
  208. \OC::$server->getSession()->set('public_link_authenticated', '100');
  209. $result = \OC\Share\Share::checkPasswordProtectedShare($item);
  210. $this->assertEquals($expected, $result);
  211. }
  212. function checkPasswordProtectedShareDataProvider() {
  213. return array(
  214. array(true, array()),
  215. array(true, array('share_with' => null)),
  216. array(true, array('share_with' => '')),
  217. array(true, array('share_with' => '1234567890', 'share_type' => '1')),
  218. array(true, array('share_with' => '1234567890', 'share_type' => 1)),
  219. array(true, array('share_with' => '1234567890', 'share_type' => '3', 'id' => '100')),
  220. array(true, array('share_with' => '1234567890', 'share_type' => 3, 'id' => '100')),
  221. array(true, array('share_with' => '1234567890', 'share_type' => '3', 'id' => 100)),
  222. array(true, array('share_with' => '1234567890', 'share_type' => 3, 'id' => 100)),
  223. array(false, array('share_with' => '1234567890', 'share_type' => '3', 'id' => '101')),
  224. array(false, array('share_with' => '1234567890', 'share_type' => 3, 'id' => '101')),
  225. array(false, array('share_with' => '1234567890', 'share_type' => '3', 'id' => 101)),
  226. array(false, array('share_with' => '1234567890', 'share_type' => 3, 'id' => 101)),
  227. );
  228. }
  229. /**
  230. * @dataProvider urls
  231. * @param string $url
  232. * @param string $expectedResult
  233. */
  234. function testRemoveProtocolFromUrl($url, $expectedResult) {
  235. $share = new \OC\Share\Share();
  236. $result = self::invokePrivate($share, 'removeProtocolFromUrl', array($url));
  237. $this->assertSame($expectedResult, $result);
  238. }
  239. function urls() {
  240. return array(
  241. array('http://owncloud.org', 'owncloud.org'),
  242. array('https://owncloud.org', 'owncloud.org'),
  243. array('owncloud.org', 'owncloud.org'),
  244. );
  245. }
  246. /**
  247. * @dataProvider dataProviderTestGroupItems
  248. * @param array $ungrouped
  249. * @param array $grouped
  250. */
  251. function testGroupItems($ungrouped, $grouped) {
  252. $result = DummyShareClass::groupItemsTest($ungrouped);
  253. $this->compareArrays($grouped, $result);
  254. }
  255. function compareArrays($result, $expectedResult) {
  256. foreach ($expectedResult as $key => $value) {
  257. if (is_array($value)) {
  258. $this->compareArrays($result[$key], $value);
  259. } else {
  260. $this->assertSame($value, $result[$key]);
  261. }
  262. }
  263. }
  264. function dataProviderTestGroupItems() {
  265. return array(
  266. // one array with one share
  267. array(
  268. array( // input
  269. array('item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_ALL, 'item_target' => 't1')),
  270. array( // expected result
  271. array('item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_ALL, 'item_target' => 't1'))),
  272. // two shares both point to the same source
  273. array(
  274. array( // input
  275. array('item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_READ, 'item_target' => 't1'),
  276. array('item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_UPDATE, 'item_target' => 't1'),
  277. ),
  278. array( // expected result
  279. array('item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE, 'item_target' => 't1',
  280. 'grouped' => array(
  281. array('item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_READ, 'item_target' => 't1'),
  282. array('item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_UPDATE, 'item_target' => 't1'),
  283. )
  284. ),
  285. )
  286. ),
  287. // two shares both point to the same source but with different targets
  288. array(
  289. array( // input
  290. array('item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_READ, 'item_target' => 't1'),
  291. array('item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_UPDATE, 'item_target' => 't2'),
  292. ),
  293. array( // expected result
  294. array('item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_READ, 'item_target' => 't1'),
  295. array('item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_UPDATE, 'item_target' => 't2'),
  296. )
  297. ),
  298. // three shares two point to the same source
  299. array(
  300. array( // input
  301. array('item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_READ, 'item_target' => 't1'),
  302. array('item_source' => 2, 'permissions' => \OCP\Constants::PERMISSION_CREATE, 'item_target' => 't2'),
  303. array('item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_UPDATE, 'item_target' => 't1'),
  304. ),
  305. array( // expected result
  306. array('item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE, 'item_target' => 't1',
  307. 'grouped' => array(
  308. array('item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_READ, 'item_target' => 't1'),
  309. array('item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_UPDATE, 'item_target' => 't1'),
  310. )
  311. ),
  312. array('item_source' => 2, 'permissions' => \OCP\Constants::PERMISSION_CREATE, 'item_target' => 't2'),
  313. )
  314. ),
  315. );
  316. }
  317. }
  318. class DummyShareClass extends \OC\Share\Share {
  319. public static function groupItemsTest($items) {
  320. return parent::groupItems($items, 'test');
  321. }
  322. }
  323. class DummyHookListener {
  324. static $shareType = null;
  325. public static function listen($params) {
  326. self::$shareType = $params['shareType'];
  327. }
  328. }