diff options
author | Vincent Petry <pvince81@owncloud.com> | 2015-01-16 20:11:13 +0300 |
---|---|---|
committer | Vincent Petry <pvince81@owncloud.com> | 2015-01-21 00:07:21 +0300 |
commit | 18d46dfdeb2facc9f5c5d9f1827b27714acb96dd (patch) | |
tree | cba9f1e6489be99eced609b569e137a7343425c0 /tests | |
parent | 5f70ed188b7d661929ec04b687f11fcb305d8334 (diff) |
Fix getItemSharedWithUser for groups
Fixed SQL query for whenever a user has more than one group.
Added missing $owner where clause for group lookup.
Added unit tests for the group cases.
Backport of 40931a8b0d5d10c0f711756a4e8a423ff055621e from master
Diffstat (limited to 'tests')
-rw-r--r-- | tests/lib/share/share.php | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/tests/lib/share/share.php b/tests/lib/share/share.php index c3264a8637d..d2825f320ca 100644 --- a/tests/lib/share/share.php +++ b/tests/lib/share/share.php @@ -27,6 +27,7 @@ class Test_Share extends Test\TestCase { protected $user2; protected $user3; protected $user4; + protected $user5; protected $groupAndUser; protected $groupBackend; protected $group1; @@ -43,11 +44,13 @@ class Test_Share extends Test\TestCase { $this->user2 = $this->getUniqueID('user2_'); $this->user3 = $this->getUniqueID('user3_'); $this->user4 = $this->getUniqueID('user4_'); + $this->user5 = $this->getUniqueID('user5_'); $this->groupAndUser = $this->getUniqueID('groupAndUser_'); OC_User::createUser($this->user1, 'pass'); OC_User::createUser($this->user2, 'pass'); OC_User::createUser($this->user3, 'pass'); OC_User::createUser($this->user4, 'pass'); + OC_User::createUser($this->user5, 'pass'); // no group OC_User::createUser($this->groupAndUser, 'pass'); OC_User::setUserId($this->user1); OC_Group::clearBackends(); @@ -81,6 +84,18 @@ class Test_Share extends Test\TestCase { $query = OC_DB::prepare('DELETE FROM `*PREFIX*share` WHERE `item_type` = ?'); $query->execute(array('test')); OC_Appconfig::setValue('core', 'shareapi_allow_resharing', $this->resharing); + + OC_User::deleteUser($this->user1); + OC_User::deleteUser($this->user2); + OC_User::deleteUser($this->user3); + OC_User::deleteUser($this->user4); + OC_User::deleteUser($this->user5); + OC_User::deleteUser($this->groupAndUser); + + OC_Group::deleteGroup($this->group1); + OC_Group::deleteGroup($this->group2); + OC_Group::deleteGroup($this->groupAndUser); + parent::tearDown(); } @@ -632,6 +647,81 @@ class Test_Share extends Test\TestCase { return $row; } + public function testGetItemSharedWithUser() { + OC_User::setUserId($this->user1); + + //add dummy values to the share table + $query = \OC_DB::prepare('INSERT INTO `*PREFIX*share` (' + .' `item_type`, `item_source`, `item_target`, `share_type`,' + .' `share_with`, `uid_owner`) VALUES (?,?,?,?,?,?)'); + $args = array('test', 99, 'target1', OCP\Share::SHARE_TYPE_USER, $this->user2, $this->user1); + $query->execute($args); + $args = array('test', 99, 'target2', OCP\Share::SHARE_TYPE_USER, $this->user4, $this->user1); + $query->execute($args); + $args = array('test', 99, 'target3', OCP\Share::SHARE_TYPE_USER, $this->user3, $this->user2); + $query->execute($args); + $args = array('test', 99, 'target4', OCP\Share::SHARE_TYPE_USER, $this->user3, $this->user4); + $query->execute($args); + $args = array('test', 99, 'target4', OCP\Share::SHARE_TYPE_USER, $this->user5, $this->user4); + $query->execute($args); + + + $result1 = \OCP\Share::getItemSharedWithUser('test', 99, $this->user2); + $this->assertSame(1, count($result1)); + $this->verifyResult($result1, array('target1')); + + $result2 = \OCP\Share::getItemSharedWithUser('test', 99, null); + $this->assertSame(5, count($result2)); + $this->verifyResult($result2, array('target1', 'target2')); + + $result3 = \OCP\Share::getItemSharedWithUser('test', 99, null); + $this->assertSame(5, count($result3)); // 5 because target4 appears twice + $this->verifyResult($result3, array('target1', 'target2', 'target3', 'target4')); + + $result5 = \OCP\Share::getItemSharedWithUser('test', 99, $this->user5); + $this->assertSame(1, count($result5)); + $this->verifyResult($result5, array('target4')); + } + + public function testGetItemSharedWithUserFromGroupShare() { + OC_User::setUserId($this->user1); + + //add dummy values to the share table + $query = \OC_DB::prepare('INSERT INTO `*PREFIX*share` (' + .' `item_type`, `item_source`, `item_target`, `share_type`,' + .' `share_with`, `uid_owner`) VALUES (?,?,?,?,?,?)'); + $args = array('test', 99, 'target1', OCP\Share::SHARE_TYPE_GROUP, $this->group1, $this->user1); + $query->execute($args); + $args = array('test', 99, 'target2', OCP\Share::SHARE_TYPE_GROUP, $this->group2, $this->user1); + $query->execute($args); + $args = array('test', 99, 'target3', OCP\Share::SHARE_TYPE_GROUP, $this->group1, $this->user2); + $query->execute($args); + $args = array('test', 99, 'target4', OCP\Share::SHARE_TYPE_GROUP, $this->group1, $this->user4); + $query->execute($args); + + // user2 is in group1 and group2 + $result1 = \OCP\Share::getItemSharedWithUser('test', 99, $this->user2); + $this->assertSame(4, count($result1)); + $this->verifyResult($result1, array('target1', 'target2', 'target3', 'target4')); + + $result2 = \OCP\Share::getItemSharedWithUser('test', 99, null); + $this->assertSame(4, count($result2)); + $this->verifyResult($result2, array('target1', 'target2', 'target3', 'target4')); + + $result3 = \OCP\Share::getItemSharedWithUser('test', 99, $this->user5); + $this->assertSame(0, count($result3)); + } + + public function verifyResult($result, $expected) { + foreach ($result as $r) { + if (in_array($r['item_target'], $expected)) { + $key = array_search($r['item_target'], $expected); + unset($expected[$key]); + } + } + $this->assertEmpty($expected, 'did not found all expected values'); + } + public function testShareItemWithLink() { OC_User::setUserId($this->user1); $token = OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_LINK, null, OCP\PERMISSION_READ); |