aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorRoeland Jago Douma <rullzer@owncloud.com>2016-01-20 14:07:56 +0100
committerRoeland Jago Douma <rullzer@owncloud.com>2016-01-21 16:06:12 +0100
commit9b5ea18ce56fef3de8403be97e0961dc7a082370 (patch)
treec09a47dbb1a0d0e38377b95098e6f92fa7693336 /tests
parent0a9cd91e1d9baa8247fc2bbe1d02ede8340f2906 (diff)
downloadnextcloud-server-9b5ea18ce56fef3de8403be97e0961dc7a082370.tar.gz
nextcloud-server-9b5ea18ce56fef3de8403be97e0961dc7a082370.zip
Add Unit tests for the default share provider
Diffstat (limited to 'tests')
-rw-r--r--tests/lib/share20/defaultshareprovidertest.php438
1 files changed, 395 insertions, 43 deletions
diff --git a/tests/lib/share20/defaultshareprovidertest.php b/tests/lib/share20/defaultshareprovidertest.php
index 039692772a0..812c6ecc27e 100644
--- a/tests/lib/share20/defaultshareprovidertest.php
+++ b/tests/lib/share20/defaultshareprovidertest.php
@@ -37,13 +37,13 @@ class DefaultShareProviderTest extends \Test\TestCase {
/** @var IDBConnection */
protected $dbConn;
- /** @var IUserManager */
+ /** @var IUserManager | \PHPUnit_Framework_MockObject_MockObject */
protected $userManager;
- /** @var IGroupManager */
+ /** @var IGroupManager | \PHPUnit_Framework_MockObject_MockObject */
protected $groupManager;
- /** @var IRootFolder */
+ /** @var IRootFolder | \PHPUnit_Framework_MockObject_MockObject */
protected $rootFolder;
/** @var DefaultShareProvider */
@@ -92,16 +92,7 @@ class DefaultShareProviderTest extends \Test\TestCase {
]);
$qb->execute();
- // Get the id
- $qb = $this->dbConn->getQueryBuilder();
- $cursor = $qb->select('id')
- ->from('share')
- ->setMaxResults(1)
- ->orderBy('id', 'DESC')
- ->execute();
- $id = $cursor->fetch();
- $id = $id['id'];
- $cursor->closeCursor();
+ $id = $qb->getLastInsertId();
$sharedWith = $this->getMock('OCP\IUser');
$sharedBy = $this->getMock('OCP\IUser');
@@ -165,15 +156,7 @@ class DefaultShareProviderTest extends \Test\TestCase {
$this->assertEquals(1, $qb->execute());
// Get the id
- $qb = $this->dbConn->getQueryBuilder();
- $cursor = $qb->select('id')
- ->from('share')
- ->setMaxResults(1)
- ->orderBy('id', 'DESC')
- ->execute();
- $id = $cursor->fetch();
- $id = $id['id'];
- $cursor->closeCursor();
+ $id = $qb->getLastInsertId();
$sharedWith = $this->getMock('OCP\IGroup');
$sharedBy = $this->getMock('OCP\IUser');
@@ -242,16 +225,7 @@ class DefaultShareProviderTest extends \Test\TestCase {
]);
$this->assertEquals(1, $qb->execute());
- // Get the id
- $qb = $this->dbConn->getQueryBuilder();
- $cursor = $qb->select('id')
- ->from('share')
- ->setMaxResults(1)
- ->orderBy('id', 'DESC')
- ->execute();
- $id = $cursor->fetch();
- $id = $id['id'];
- $cursor->closeCursor();
+ $id = $qb->getLastInsertId();
$sharedBy = $this->getMock('OCP\IUser');
$sharedBy->method('getUID')->willReturn('sharedBy');
@@ -311,17 +285,7 @@ class DefaultShareProviderTest extends \Test\TestCase {
]);
$this->assertEquals(1, $qb->execute());
- // Get the id
- $qb = $this->dbConn->getQueryBuilder();
- $cursor = $qb->select('id')
- ->from('share')
- ->setMaxResults(1)
- ->orderBy('id', 'DESC')
- ->execute();
- $id = $cursor->fetch();
- $id = $id['id'];
- $cursor->closeCursor();
-
+ $id = $qb->getLastInsertId();
$share = $this->getMock('OC\Share20\IShare');
$share->method('getId')->willReturn($id);
@@ -771,4 +735,392 @@ class DefaultShareProviderTest extends \Test\TestCase {
public function testGetShareByTokenNotFound() {
$this->provider->getShareByToken('invalidtoken');
}
+
+ public function testGetSharedWithUser() {
+ $qb = $this->dbConn->getQueryBuilder();
+ $qb->insert('share')
+ ->values([
+ 'share_type' => $qb->expr()->literal(\OCP\Share::SHARE_TYPE_USER),
+ 'share_with' => $qb->expr()->literal('sharedWith'),
+ 'uid_owner' => $qb->expr()->literal('shareOwner'),
+ 'uid_initiator' => $qb->expr()->literal('sharedBy'),
+ 'item_type' => $qb->expr()->literal('file'),
+ 'file_source' => $qb->expr()->literal(42),
+ 'file_target' => $qb->expr()->literal('myTarget'),
+ 'permissions' => $qb->expr()->literal(13),
+ ]);
+ $this->assertEquals(1, $qb->execute());
+ $id = $qb->getLastInsertId();
+
+ $qb = $this->dbConn->getQueryBuilder();
+ $qb->insert('share')
+ ->values([
+ 'share_type' => $qb->expr()->literal(\OCP\Share::SHARE_TYPE_USER),
+ 'share_with' => $qb->expr()->literal('sharedWith2'),
+ 'uid_owner' => $qb->expr()->literal('shareOwner2'),
+ 'uid_initiator' => $qb->expr()->literal('sharedBy2'),
+ 'item_type' => $qb->expr()->literal('file2'),
+ 'file_source' => $qb->expr()->literal(43),
+ 'file_target' => $qb->expr()->literal('myTarget2'),
+ 'permissions' => $qb->expr()->literal(14),
+ ]);
+ $this->assertEquals(1, $qb->execute());
+
+ $user = $this->getMock('\OCP\IUser');
+ $user->method('getUID')->willReturn('sharedWith');
+ $owner = $this->getMock('\OCP\IUser');
+ $owner->method('getUID')->willReturn('shareOwner');
+ $initiator = $this->getMock('\OCP\IUser');
+ $initiator->method('getUID')->willReturn('sharedBy');
+
+ $this->userManager->method('get')->willReturnMap([
+ ['sharedWith', $user],
+ ['shareOwner', $owner],
+ ['sharedBy', $initiator],
+ ]);
+
+ $file = $this->getMock('\OCP\Files\File');
+ $this->rootFolder->method('getUserFolder')->with('shareOwner')->will($this->returnSelf());
+ $this->rootFolder->method('getById')->with(42)->willReturn([$file]);
+
+ $share = $this->provider->getSharedWith($user, \OCP\Share::SHARE_TYPE_USER, 1 , 0);
+ $this->assertCount(1, $share);
+
+ $share = $share[0];
+ $this->assertEquals($id, $share->getId());
+ $this->assertEquals($user, $share->getSharedWith());
+ $this->assertEquals($owner, $share->getShareOwner());
+ $this->assertEquals($initiator, $share->getSharedBy());
+ $this->assertEquals(\OCP\Share::SHARE_TYPE_USER, $share->getShareType());
+ }
+
+ public function testGetSharedWithGroup() {
+ $qb = $this->dbConn->getQueryBuilder();
+ $qb->insert('share')
+ ->values([
+ 'share_type' => $qb->expr()->literal(\OCP\Share::SHARE_TYPE_GROUP),
+ 'share_with' => $qb->expr()->literal('sharedWith'),
+ 'uid_owner' => $qb->expr()->literal('shareOwner2'),
+ 'uid_initiator' => $qb->expr()->literal('sharedBy2'),
+ 'item_type' => $qb->expr()->literal('file'),
+ 'file_source' => $qb->expr()->literal(43),
+ 'file_target' => $qb->expr()->literal('myTarget2'),
+ 'permissions' => $qb->expr()->literal(14),
+ ]);
+ $this->assertEquals(1, $qb->execute());
+
+ $qb = $this->dbConn->getQueryBuilder();
+ $qb->insert('share')
+ ->values([
+ 'share_type' => $qb->expr()->literal(\OCP\Share::SHARE_TYPE_GROUP),
+ 'share_with' => $qb->expr()->literal('sharedWith'),
+ 'uid_owner' => $qb->expr()->literal('shareOwner'),
+ 'uid_initiator' => $qb->expr()->literal('sharedBy'),
+ 'item_type' => $qb->expr()->literal('file'),
+ 'file_source' => $qb->expr()->literal(42),
+ 'file_target' => $qb->expr()->literal('myTarget'),
+ 'permissions' => $qb->expr()->literal(13),
+ ]);
+ $this->assertEquals(1, $qb->execute());
+ $id = $qb->getLastInsertId();
+
+ $groups = [];
+ foreach(range(0, 100) as $i) {
+ $group = $this->getMock('\OCP\IGroup');
+ $group->method('getGID')->willReturn('group'.$i);
+ $groups[] = $group;
+ }
+
+ $group = $this->getMock('\OCP\IGroup');
+ $group->method('getGID')->willReturn('sharedWith');
+ $groups[] = $group;
+
+ $user = $this->getMock('\OCP\IUser');
+ $user->method('getUID')->willReturn('sharedWith');
+ $owner = $this->getMock('\OCP\IUser');
+ $owner->method('getUID')->willReturn('shareOwner');
+ $initiator = $this->getMock('\OCP\IUser');
+ $initiator->method('getUID')->willReturn('sharedBy');
+
+ $this->userManager->method('get')->willReturnMap([
+ ['shareOwner', $owner],
+ ['sharedBy', $initiator],
+ ]);
+ $this->groupManager->method('getUserGroups')->with($user)->willReturn($groups);
+ $this->groupManager->method('get')->with('sharedWith')->willReturn($group);
+
+ $file = $this->getMock('\OCP\Files\File');
+ $this->rootFolder->method('getUserFolder')->with('shareOwner')->will($this->returnSelf());
+ $this->rootFolder->method('getById')->with(42)->willReturn([$file]);
+
+ $share = $this->provider->getSharedWith($user, \OCP\Share::SHARE_TYPE_GROUP, 20 , 1);
+ $this->assertCount(1, $share);
+
+ $share = $share[0];
+ $this->assertEquals($id, $share->getId());
+ $this->assertEquals($group, $share->getSharedWith());
+ $this->assertEquals($owner, $share->getShareOwner());
+ $this->assertEquals($initiator, $share->getSharedBy());
+ $this->assertEquals(\OCP\Share::SHARE_TYPE_GROUP, $share->getShareType());
+ }
+
+ public function testGetSharedWithGroupUserModified() {
+ $qb = $this->dbConn->getQueryBuilder();
+ $qb->insert('share')
+ ->values([
+ 'share_type' => $qb->expr()->literal(\OCP\Share::SHARE_TYPE_GROUP),
+ 'share_with' => $qb->expr()->literal('sharedWith'),
+ 'uid_owner' => $qb->expr()->literal('shareOwner'),
+ 'uid_initiator' => $qb->expr()->literal('sharedBy'),
+ 'item_type' => $qb->expr()->literal('file'),
+ 'file_source' => $qb->expr()->literal(42),
+ 'file_target' => $qb->expr()->literal('myTarget'),
+ 'permissions' => $qb->expr()->literal(13),
+ ]);
+ $this->assertEquals(1, $qb->execute());
+ $id = $qb->getLastInsertId();
+
+ $qb = $this->dbConn->getQueryBuilder();
+ $qb->insert('share')
+ ->values([
+ 'share_type' => $qb->expr()->literal(2),
+ 'share_with' => $qb->expr()->literal('user'),
+ 'uid_owner' => $qb->expr()->literal('shareOwner'),
+ 'uid_initiator' => $qb->expr()->literal('sharedBy'),
+ 'item_type' => $qb->expr()->literal('file'),
+ 'file_source' => $qb->expr()->literal(42),
+ 'file_target' => $qb->expr()->literal('userTarget'),
+ 'permissions' => $qb->expr()->literal(0),
+ 'parent' => $qb->expr()->literal($id),
+ ]);
+ $this->assertEquals(1, $qb->execute());
+
+ $group = $this->getMock('\OCP\IGroup');
+ $group->method('getGID')->willReturn('sharedWith');
+ $groups = [$group];
+
+ $user = $this->getMock('\OCP\IUser');
+ $user->method('getUID')->willReturn('user');
+ $owner = $this->getMock('\OCP\IUser');
+ $owner->method('getUID')->willReturn('shareOwner');
+ $initiator = $this->getMock('\OCP\IUser');
+ $initiator->method('getUID')->willReturn('sharedBy');
+
+ $this->userManager->method('get')->willReturnMap([
+ ['shareOwner', $owner],
+ ['sharedBy', $initiator],
+ ]);
+ $this->groupManager->method('getUserGroups')->with($user)->willReturn($groups);
+ $this->groupManager->method('get')->with('sharedWith')->willReturn($group);
+
+ $file = $this->getMock('\OCP\Files\File');
+ $this->rootFolder->method('getUserFolder')->with('shareOwner')->will($this->returnSelf());
+ $this->rootFolder->method('getById')->with(42)->willReturn([$file]);
+
+ $share = $this->provider->getSharedWith($user, \OCP\Share::SHARE_TYPE_GROUP, -1, 0);
+ $this->assertCount(1, $share);
+
+ $share = $share[0];
+ $this->assertEquals($id, $share->getId());
+ $this->assertEquals($group, $share->getSharedWith());
+ $this->assertEquals($owner, $share->getShareOwner());
+ $this->assertEquals($initiator, $share->getSharedBy());
+ $this->assertEquals(\OCP\Share::SHARE_TYPE_GROUP, $share->getShareType());
+ $this->assertEquals(0, $share->getPermissions());
+ $this->assertEquals('userTarget', $share->getTarget());
+ }
+
+ public function testGetSharesBy() {
+ $qb = $this->dbConn->getQueryBuilder();
+ $qb->insert('share')
+ ->values([
+ 'share_type' => $qb->expr()->literal(\OCP\Share::SHARE_TYPE_USER),
+ 'share_with' => $qb->expr()->literal('sharedWith'),
+ 'uid_owner' => $qb->expr()->literal('shareOwner'),
+ 'uid_initiator' => $qb->expr()->literal('sharedBy'),
+ 'item_type' => $qb->expr()->literal('file'),
+ 'file_source' => $qb->expr()->literal(42),
+ 'file_target' => $qb->expr()->literal('myTarget'),
+ 'permissions' => $qb->expr()->literal(13),
+ ]);
+ $this->assertEquals(1, $qb->execute());
+ $id = $qb->getLastInsertId();
+
+ $qb = $this->dbConn->getQueryBuilder();
+ $qb->insert('share')
+ ->values([
+ 'share_type' => $qb->expr()->literal(\OCP\Share::SHARE_TYPE_USER),
+ 'share_with' => $qb->expr()->literal('sharedWith'),
+ 'uid_owner' => $qb->expr()->literal('shareOwner'),
+ 'uid_initiator' => $qb->expr()->literal('sharedBy2'),
+ 'item_type' => $qb->expr()->literal('file'),
+ 'file_source' => $qb->expr()->literal(42),
+ 'file_target' => $qb->expr()->literal('userTarget'),
+ 'permissions' => $qb->expr()->literal(0),
+ 'parent' => $qb->expr()->literal($id),
+ ]);
+ $this->assertEquals(1, $qb->execute());
+
+ $user = $this->getMock('\OCP\IUser');
+ $user->method('getUID')->willReturn('sharedWith');
+ $owner = $this->getMock('\OCP\IUser');
+ $owner->method('getUID')->willReturn('shareOwner');
+ $initiator = $this->getMock('\OCP\IUser');
+ $initiator->method('getUID')->willReturn('sharedBy');
+
+ $this->userManager->method('get')->willReturnMap([
+ ['sharedWith', $user],
+ ['shareOwner', $owner],
+ ['sharedBy', $initiator],
+ ]);
+
+ $file = $this->getMock('\OCP\Files\File');
+ $this->rootFolder->method('getUserFolder')->with('shareOwner')->will($this->returnSelf());
+ $this->rootFolder->method('getById')->with(42)->willReturn([$file]);
+
+ $share = $this->provider->getSharesBy($initiator, \OCP\Share::SHARE_TYPE_USER, null, false, 1, 0);
+ $this->assertCount(1, $share);
+
+ $share = $share[0];
+ $this->assertEquals($id, $share->getId());
+ $this->assertEquals($user, $share->getSharedWith());
+ $this->assertEquals($owner, $share->getShareOwner());
+ $this->assertEquals($initiator, $share->getSharedBy());
+ $this->assertEquals(\OCP\Share::SHARE_TYPE_USER, $share->getShareType());
+ $this->assertEquals(13, $share->getPermissions());
+ $this->assertEquals('myTarget', $share->getTarget());
+ }
+
+ public function testGetSharesNode() {
+ $qb = $this->dbConn->getQueryBuilder();
+ $qb->insert('share')
+ ->values([
+ 'share_type' => $qb->expr()->literal(\OCP\Share::SHARE_TYPE_USER),
+ 'share_with' => $qb->expr()->literal('sharedWith'),
+ 'uid_owner' => $qb->expr()->literal('shareOwner'),
+ 'uid_initiator' => $qb->expr()->literal('sharedBy'),
+ 'item_type' => $qb->expr()->literal('file'),
+ 'file_source' => $qb->expr()->literal(42),
+ 'file_target' => $qb->expr()->literal('myTarget'),
+ 'permissions' => $qb->expr()->literal(13),
+ ]);
+ $this->assertEquals(1, $qb->execute());
+ $id = $qb->getLastInsertId();
+
+ $qb = $this->dbConn->getQueryBuilder();
+ $qb->insert('share')
+ ->values([
+ 'share_type' => $qb->expr()->literal(\OCP\Share::SHARE_TYPE_USER),
+ 'share_with' => $qb->expr()->literal('sharedWith'),
+ 'uid_owner' => $qb->expr()->literal('shareOwner'),
+ 'uid_initiator' => $qb->expr()->literal('sharedBy'),
+ 'item_type' => $qb->expr()->literal('file'),
+ 'file_source' => $qb->expr()->literal(43),
+ 'file_target' => $qb->expr()->literal('userTarget'),
+ 'permissions' => $qb->expr()->literal(0),
+ 'parent' => $qb->expr()->literal($id),
+ ]);
+ $this->assertEquals(1, $qb->execute());
+
+ $user = $this->getMock('\OCP\IUser');
+ $user->method('getUID')->willReturn('sharedWith');
+ $owner = $this->getMock('\OCP\IUser');
+ $owner->method('getUID')->willReturn('shareOwner');
+ $initiator = $this->getMock('\OCP\IUser');
+ $initiator->method('getUID')->willReturn('sharedBy');
+
+ $this->userManager->method('get')->willReturnMap([
+ ['sharedWith', $user],
+ ['shareOwner', $owner],
+ ['sharedBy', $initiator],
+ ]);
+
+ $file = $this->getMock('\OCP\Files\File');
+ $file->method('getId')->willReturn(42);
+ $this->rootFolder->method('getUserFolder')->with('shareOwner')->will($this->returnSelf());
+ $this->rootFolder->method('getById')->with(42)->willReturn([$file]);
+
+ $share = $this->provider->getSharesBy($initiator, \OCP\Share::SHARE_TYPE_USER, $file, false, 1, 0);
+ $this->assertCount(1, $share);
+
+ $share = $share[0];
+ $this->assertEquals($id, $share->getId());
+ $this->assertEquals($user, $share->getSharedWith());
+ $this->assertEquals($owner, $share->getShareOwner());
+ $this->assertEquals($initiator, $share->getSharedBy());
+ $this->assertEquals(\OCP\Share::SHARE_TYPE_USER, $share->getShareType());
+ $this->assertEquals(13, $share->getPermissions());
+ $this->assertEquals('myTarget', $share->getTarget());
+ }
+
+ public function testGetSharesReshare() {
+ $qb = $this->dbConn->getQueryBuilder();
+ $qb->insert('share')
+ ->values([
+ 'share_type' => $qb->expr()->literal(\OCP\Share::SHARE_TYPE_USER),
+ 'share_with' => $qb->expr()->literal('sharedWith'),
+ 'uid_owner' => $qb->expr()->literal('shareOwner'),
+ 'uid_initiator' => $qb->expr()->literal('shareOwner'),
+ 'item_type' => $qb->expr()->literal('file'),
+ 'file_source' => $qb->expr()->literal(42),
+ 'file_target' => $qb->expr()->literal('myTarget'),
+ 'permissions' => $qb->expr()->literal(13),
+ ]);
+ $this->assertEquals(1, $qb->execute());
+ $id1 = $qb->getLastInsertId();
+
+ $qb = $this->dbConn->getQueryBuilder();
+ $qb->insert('share')
+ ->values([
+ 'share_type' => $qb->expr()->literal(\OCP\Share::SHARE_TYPE_USER),
+ 'share_with' => $qb->expr()->literal('sharedWith'),
+ 'uid_owner' => $qb->expr()->literal('shareOwner'),
+ 'uid_initiator' => $qb->expr()->literal('sharedBy'),
+ 'item_type' => $qb->expr()->literal('file'),
+ 'file_source' => $qb->expr()->literal(42),
+ 'file_target' => $qb->expr()->literal('userTarget'),
+ 'permissions' => $qb->expr()->literal(0),
+ ]);
+ $this->assertEquals(1, $qb->execute());
+ $id2 = $qb->getLastInsertId();
+
+ $user = $this->getMock('\OCP\IUser');
+ $user->method('getUID')->willReturn('sharedWith');
+ $owner = $this->getMock('\OCP\IUser');
+ $owner->method('getUID')->willReturn('shareOwner');
+ $initiator = $this->getMock('\OCP\IUser');
+ $initiator->method('getUID')->willReturn('sharedBy');
+
+ $this->userManager->method('get')->willReturnMap([
+ ['sharedWith', $user],
+ ['shareOwner', $owner],
+ ['sharedBy', $initiator],
+ ]);
+
+ $file = $this->getMock('\OCP\Files\File');
+ $file->method('getId')->willReturn(42);
+ $this->rootFolder->method('getUserFolder')->with('shareOwner')->will($this->returnSelf());
+ $this->rootFolder->method('getById')->with(42)->willReturn([$file]);
+
+ $shares = $this->provider->getSharesBy($owner, \OCP\Share::SHARE_TYPE_USER, null, true, -1, 0);
+ $this->assertCount(2, $shares);
+
+ $share = $shares[0];
+ $this->assertEquals($id1, $share->getId());
+ $this->assertSame($user, $share->getSharedWith());
+ $this->assertSame($owner, $share->getShareOwner());
+ $this->assertSame($owner, $share->getSharedBy());
+ $this->assertEquals(\OCP\Share::SHARE_TYPE_USER, $share->getShareType());
+ $this->assertEquals(13, $share->getPermissions());
+ $this->assertEquals('myTarget', $share->getTarget());
+
+ $share = $shares[1];
+ $this->assertEquals($id2, $share->getId());
+ $this->assertSame($user, $share->getSharedWith());
+ $this->assertSame($owner, $share->getShareOwner());
+ $this->assertSame($initiator, $share->getSharedBy());
+ $this->assertEquals(\OCP\Share::SHARE_TYPE_USER, $share->getShareType());
+ $this->assertEquals(0, $share->getPermissions());
+ $this->assertEquals('userTarget', $share->getTarget());
+ }
}