summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/lib/autoloader.php52
-rw-r--r--tests/lib/files/storage/wrapper/encryption.php96
-rw-r--r--tests/lib/group/manager.php26
-rw-r--r--tests/lib/share20/defaultshareprovidertest.php654
-rw-r--r--tests/lib/share20/managertest.php198
-rw-r--r--tests/lib/subadmin.php31
6 files changed, 1049 insertions, 8 deletions
diff --git a/tests/lib/autoloader.php b/tests/lib/autoloader.php
index 6d9d3bd8eba..9fb717c4f63 100644
--- a/tests/lib/autoloader.php
+++ b/tests/lib/autoloader.php
@@ -20,35 +20,53 @@ class AutoLoader extends TestCase {
}
public function testLeadingSlashOnClassName() {
- $this->assertEquals(array('private/files/storage/local.php', 'files/storage/local.php'), $this->loader->findClass('\OC\Files\Storage\Local'));
+ $this->assertEquals([
+ \OC::$SERVERROOT . '/lib/private/files/storage/local.php',
+ ], $this->loader->findClass('\OC\Files\Storage\Local'));
}
public function testNoLeadingSlashOnClassName() {
- $this->assertEquals(array('private/files/storage/local.php', 'files/storage/local.php'), $this->loader->findClass('OC\Files\Storage\Local'));
+ $this->assertEquals([
+ \OC::$SERVERROOT . '/lib/private/files/storage/local.php',
+ ], $this->loader->findClass('OC\Files\Storage\Local'));
}
public function testLegacyPath() {
- $this->assertEquals(array('private/legacy/files.php', 'private/files.php'), $this->loader->findClass('OC_Files'));
+ $this->assertEquals([
+ \OC::$SERVERROOT . '/lib/private/legacy/files.php',
+ \OC::$SERVERROOT . '/lib/private/files.php',
+ ], $this->loader->findClass('OC_Files'));
}
public function testLoadTestNamespace() {
- $this->assertEquals(array('tests/lib/foo/bar.php'), $this->loader->findClass('Test\Foo\Bar'));
+ $this->assertEquals([
+ \OC::$SERVERROOT . '/tests/lib/foo/bar.php'
+ ], $this->loader->findClass('Test\Foo\Bar'));
}
public function testLoadTest() {
- $this->assertEquals(array('tests/lib/foo/bar.php'), $this->loader->findClass('Test_Foo_Bar'));
+ $this->assertEquals([
+ \OC::$SERVERROOT . '/tests/lib/foo/bar.php'
+ ], $this->loader->findClass('Test_Foo_Bar'));
}
public function testLoadCoreNamespace() {
- $this->assertEquals(array('private/foo/bar.php', 'foo/bar.php'), $this->loader->findClass('OC\Foo\Bar'));
+ $this->assertEquals([
+ \OC::$SERVERROOT . '/lib/private/foo/bar.php',
+ ], $this->loader->findClass('OC\Foo\Bar'));
}
public function testLoadCore() {
- $this->assertEquals(array('private/legacy/foo/bar.php', 'private/foo/bar.php'), $this->loader->findClass('OC_Foo_Bar'));
+ $this->assertEquals([
+ \OC::$SERVERROOT . '/lib/private/legacy/foo/bar.php',
+ \OC::$SERVERROOT . '/lib/private/foo/bar.php',
+ ], $this->loader->findClass('OC_Foo_Bar'));
}
public function testLoadPublicNamespace() {
- $this->assertEquals(array('public/foo/bar.php'), $this->loader->findClass('OCP\Foo\Bar'));
+ $this->assertEquals([
+ \OC::$SERVERROOT . '/lib/public/foo/bar.php',
+ ], $this->loader->findClass('OCP\Foo\Bar'));
}
public function testLoadAppNamespace() {
@@ -57,4 +75,22 @@ class AutoLoader extends TestCase {
$this->assertStringEndsWith('apps/files/foobar.php', $result[0]);
$this->assertStringEndsWith('apps/files/lib/foobar.php', $result[1]);
}
+
+ public function testLoadCoreNamespaceCore() {
+ $this->assertEquals([
+ \OC::$SERVERROOT . '/core/foo/bar.php',
+ ], $this->loader->findClass('OC\Core\Foo\Bar'));
+ }
+
+ public function testLoadCoreNamespaceSettings() {
+ $this->assertEquals([
+ \OC::$SERVERROOT . '/settings/foo/bar.php',
+ ], $this->loader->findClass('OC\Settings\Foo\Bar'));
+ }
+
+ public function testLoadCoreNamespaceRepair() {
+ $this->assertEquals([
+ \OC::$SERVERROOT . '/lib/repair/foo/bar.php',
+ ], $this->loader->findClass('OC\Repair\Foo\Bar'));
+ }
}
diff --git a/tests/lib/files/storage/wrapper/encryption.php b/tests/lib/files/storage/wrapper/encryption.php
index 095405462df..2b93aa86db0 100644
--- a/tests/lib/files/storage/wrapper/encryption.php
+++ b/tests/lib/files/storage/wrapper/encryption.php
@@ -560,6 +560,102 @@ class Encryption extends \Test\Files\Storage\Storage {
}
/**
+ * @dataProvider dataTestCopyBetweenStorageVersions
+ *
+ * @param string $sourceInternalPath
+ * @param string $targetInternalPath
+ * @param bool $copyResult
+ * @param bool $encrypted
+ */
+ public function testCopyBetweenStorageVersions($sourceInternalPath, $targetInternalPath, $copyResult, $encrypted) {
+
+ $sourceStorage = $this->getMockBuilder('OCP\Files\Storage')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $targetStorage = $this->getMockBuilder('OCP\Files\Storage')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $cache = $this->getMockBuilder('\OC\Files\Cache\Cache')
+ ->disableOriginalConstructor()->getMock();
+
+ $mountPoint = '/mountPoint';
+
+ /** @var \OC\Files\Storage\Wrapper\Encryption |\PHPUnit_Framework_MockObject_MockObject $instance */
+ $instance = $this->getMockBuilder('\OC\Files\Storage\Wrapper\Encryption')
+ ->setConstructorArgs(
+ [
+ [
+ 'storage' => $targetStorage,
+ 'root' => 'foo',
+ 'mountPoint' => $mountPoint,
+ 'mount' => $this->mount
+ ],
+ $this->encryptionManager,
+ $this->util,
+ $this->logger,
+ $this->file,
+ null,
+ $this->keyStore,
+ $this->update,
+ $this->mountManager
+ ]
+ )
+ ->setMethods(['updateUnencryptedSize', 'getCache'])
+ ->getMock();
+
+ $targetStorage->expects($this->once())->method('copyFromStorage')
+ ->with($sourceStorage, $sourceInternalPath, $targetInternalPath)
+ ->willReturn($copyResult);
+
+ if ($copyResult) {
+ $instance->expects($this->once())->method('getCache')
+ ->with('', $sourceStorage)
+ ->willReturn($cache);
+ $cache->expects($this->once())->method('get')
+ ->with($sourceInternalPath)
+ ->willReturn(['encrypted' => $encrypted, 'size' => 42]);
+ if ($encrypted) {
+ $instance->expects($this->once())->method('updateUnencryptedSize')
+ ->with($mountPoint . $targetInternalPath, 42);
+ } else {
+ $instance->expects($this->never())->method('updateUnencryptedSize');
+ }
+ } else {
+ $instance->expects($this->never())->method('updateUnencryptedSize');
+ }
+
+ $result = $this->invokePrivate(
+ $instance,
+ 'copyBetweenStorage',
+ [
+ $sourceStorage,
+ $sourceInternalPath,
+ $targetInternalPath,
+ false,
+ false
+ ]
+ );
+
+ $this->assertSame($copyResult, $result);
+ }
+
+ public function dataTestCopyBetweenStorageVersions() {
+ return [
+ ['/files/foo.txt', '/files_versions/foo.txt.768743', true, true],
+ ['/files/foo.txt', '/files_versions/foo.txt.768743', true, false],
+ ['/files/foo.txt', '/files_versions/foo.txt.768743', false, true],
+ ['/files/foo.txt', '/files_versions/foo.txt.768743', false, false],
+ ['/files_versions/foo.txt.6487634', '/files/foo.txt', true, true],
+ ['/files_versions/foo.txt.6487634', '/files/foo.txt', true, false],
+ ['/files_versions/foo.txt.6487634', '/files/foo.txt', false, true],
+ ['/files_versions/foo.txt.6487634', '/files/foo.txt', false, false],
+
+ ];
+ }
+
+ /**
* @dataProvider dataTestIsVersion
* @param string $path
* @param bool $expected
diff --git a/tests/lib/group/manager.php b/tests/lib/group/manager.php
index e3e2a96e46d..6cf473b9156 100644
--- a/tests/lib/group/manager.php
+++ b/tests/lib/group/manager.php
@@ -304,6 +304,32 @@ class Manager extends \Test\TestCase {
$this->assertEquals('group1', $group1->getGID());
}
+ public function testGetUserGroupIds() {
+ /** @var \PHPUnit_Framework_MockObject_MockObject|\OC\Group\Manager $manager */
+ $manager = $this->getMockBuilder('OC\Group\Manager')
+ ->disableOriginalConstructor()
+ ->setMethods(['getUserGroups'])
+ ->getMock();
+ $manager->expects($this->once())
+ ->method('getUserGroups')
+ ->willReturn([
+ '123' => '123',
+ 'abc' => 'abc',
+ ]);
+
+ /** @var \OC\User\User $user */
+ $user = $this->getMockBuilder('OC\User\User')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $groups = $manager->getUserGroupIds($user);
+ $this->assertEquals(2, count($groups));
+
+ foreach ($groups as $group) {
+ $this->assertInternalType('string', $group);
+ }
+ }
+
public function testInGroup() {
/**
* @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend
diff --git a/tests/lib/share20/defaultshareprovidertest.php b/tests/lib/share20/defaultshareprovidertest.php
new file mode 100644
index 00000000000..e99290f6724
--- /dev/null
+++ b/tests/lib/share20/defaultshareprovidertest.php
@@ -0,0 +1,654 @@
+<?php
+/**
+ * @author Roeland Jago Douma <rullzer@owncloud.com>
+ *
+ * @copyright Copyright (c) 2015, ownCloud, Inc.
+ * @license AGPL-3.0
+ *
+ * This code is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License, version 3,
+ * along with this program. If not, see <http://www.gnu.org/licenses/>
+ *
+ */
+namespace Test\Share20;
+
+use OCP\IDBConnection;
+use OCP\IUserManager;
+use OCP\IGroupManager;
+use OCP\Files\Folder;
+use OC\Share20\DefaultShareProvider;
+
+class DefaultShareProviderTest extends \Test\TestCase {
+
+ /** @var IDBConnection */
+ protected $dbConn;
+
+ /** @var IUserManager */
+ protected $userManager;
+
+ /** @var IGroupManager */
+ protected $groupManager;
+
+ /** @var Folder */
+ protected $userFolder;
+
+ /** @var DefaultShareProvider */
+ protected $provider;
+
+ public function setUp() {
+ $this->dbConn = \OC::$server->getDatabaseConnection();
+ $this->userManager = $this->getMock('OCP\IUserManager');
+ $this->groupManager = $this->getMock('OCP\IGroupManager');
+ $this->userFolder = $this->getMock('OCP\Files\Folder');
+
+ //Empty share table
+ $this->dbConn->getQueryBuilder()->delete('share')->execute();
+
+ $this->provider = new DefaultShareProvider(
+ $this->dbConn,
+ $this->userManager,
+ $this->groupManager,
+ $this->userFolder
+ );
+ }
+
+ public function tearDown() {
+ $this->dbConn->getQueryBuilder()->delete('share')->execute();
+ }
+
+ /**
+ * @expectedException OC\Share20\Exception\ShareNotFound
+ */
+ public function testGetShareByIdNotExist() {
+ $this->provider->getShareById(1);
+ }
+
+ public function testGetShareByIdUserShare() {
+ $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('sharedBy'),
+ 'item_type' => $qb->expr()->literal('file'),
+ 'file_source' => $qb->expr()->literal(42),
+ 'file_target' => $qb->expr()->literal('myTarget'),
+ 'permissions' => $qb->expr()->literal(13),
+ ]);
+ $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();
+
+ $storage = $this->getMock('OC\Files\Storage\Storage');
+ $storage
+ ->expects($this->once())
+ ->method('getOwner')
+ ->willReturn('shareOwner');
+ $path = $this->getMock('OCP\Files\File');
+ $path
+ ->expects($this->once())
+ ->method('getStorage')
+ ->wilLReturn($storage);
+ $this->userFolder
+ ->expects($this->once())
+ ->method('getById')
+ ->with(42)
+ ->willReturn([$path]);
+
+ $sharedWith = $this->getMock('OCP\IUser');
+ $sharedBy = $this->getMock('OCP\IUser');
+ $shareOwner = $this->getMock('OCP\IUser');
+ $this->userManager
+ ->method('get')
+ ->will($this->returnValueMap([
+ ['sharedWith', $sharedWith],
+ ['sharedBy', $sharedBy],
+ ['shareOwner', $shareOwner],
+ ]));
+
+ $share = $this->provider->getShareById($id);
+
+ $this->assertEquals($id, $share->getId());
+ $this->assertEquals(\OCP\Share::SHARE_TYPE_USER, $share->getShareType());
+ $this->assertEquals($sharedWith, $share->getSharedWith());
+ $this->assertEquals($sharedBy, $share->getSharedBy());
+ $this->assertEquals($shareOwner, $share->getShareOwner());
+ $this->assertEquals($path, $share->getPath());
+ $this->assertEquals(13, $share->getPermissions());
+ $this->assertEquals(null, $share->getToken());
+ $this->assertEquals(null, $share->getExpirationDate());
+ $this->assertEquals('myTarget', $share->getTarget());
+ }
+
+ public function testGetShareByIdGroupShare() {
+ $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('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());
+
+ // 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();
+
+ $storage = $this->getMock('OC\Files\Storage\Storage');
+ $storage
+ ->expects($this->once())
+ ->method('getOwner')
+ ->willReturn('shareOwner');
+ $path = $this->getMock('OCP\Files\Folder');
+ $path
+ ->expects($this->once())
+ ->method('getStorage')
+ ->wilLReturn($storage);
+ $this->userFolder
+ ->expects($this->once())
+ ->method('getById')
+ ->with(42)
+ ->willReturn([$path]);
+
+ $sharedWith = $this->getMock('OCP\IGroup');
+ $sharedBy = $this->getMock('OCP\IUser');
+ $shareOwner = $this->getMock('OCP\IUser');
+ $this->userManager
+ ->method('get')
+ ->will($this->returnValueMap([
+ ['sharedBy', $sharedBy],
+ ['shareOwner', $shareOwner],
+ ]));
+ $this->groupManager
+ ->expects($this->once())
+ ->method('get')
+ ->with('sharedWith')
+ ->willReturn($sharedWith);
+
+ $share = $this->provider->getShareById($id);
+
+ $this->assertEquals($id, $share->getId());
+ $this->assertEquals(\OCP\Share::SHARE_TYPE_GROUP, $share->getShareType());
+ $this->assertEquals($sharedWith, $share->getSharedWith());
+ $this->assertEquals($sharedBy, $share->getSharedBy());
+ $this->assertEquals($shareOwner, $share->getShareOwner());
+ $this->assertEquals($path, $share->getPath());
+ $this->assertEquals(13, $share->getPermissions());
+ $this->assertEquals(null, $share->getToken());
+ $this->assertEquals(null, $share->getExpirationDate());
+ $this->assertEquals('myTarget', $share->getTarget());
+ }
+
+ public function testGetShareByIdLinkShare() {
+ $qb = $this->dbConn->getQueryBuilder();
+
+ $qb->insert('share')
+ ->values([
+ 'share_type' => $qb->expr()->literal(\OCP\Share::SHARE_TYPE_LINK),
+ 'share_with' => $qb->expr()->literal('sharedWith'),
+ 'uid_owner' => $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),
+ 'token' => $qb->expr()->literal('token'),
+ 'expiration' => $qb->expr()->literal('2000-01-02 00:00:00'),
+ ]);
+ $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();
+
+ $storage = $this->getMock('OC\Files\Storage\Storage');
+ $storage
+ ->expects($this->once())
+ ->method('getOwner')
+ ->willReturn('shareOwner');
+ $path = $this->getMock('OCP\Files\Node');
+ $path
+ ->expects($this->once())
+ ->method('getStorage')
+ ->wilLReturn($storage);
+ $this->userFolder
+ ->expects($this->once())
+ ->method('getById')
+ ->with(42)
+ ->willReturn([$path]);
+
+ $sharedBy = $this->getMock('OCP\IUser');
+ $shareOwner = $this->getMock('OCP\IUser');
+ $this->userManager
+ ->method('get')
+ ->will($this->returnValueMap([
+ ['sharedBy', $sharedBy],
+ ['shareOwner', $shareOwner],
+ ]));
+
+ $share = $this->provider->getShareById($id);
+
+ $this->assertEquals($id, $share->getId());
+ $this->assertEquals(\OCP\Share::SHARE_TYPE_LINK, $share->getShareType());
+ $this->assertEquals('sharedWith', $share->getSharedWith());
+ $this->assertEquals($sharedBy, $share->getSharedBy());
+ $this->assertEquals($shareOwner, $share->getShareOwner());
+ $this->assertEquals($path, $share->getPath());
+ $this->assertEquals(13, $share->getPermissions());
+ $this->assertEquals('token', $share->getToken());
+ $this->assertEquals(\DateTime::createFromFormat('Y-m-d H:i:s', '2000-01-02 00:00:00'), $share->getExpirationDate());
+ $this->assertEquals('myTarget', $share->getTarget());
+ }
+
+ public function testGetShareByIdRemoteShare() {
+ $qb = $this->dbConn->getQueryBuilder();
+
+ $qb->insert('share')
+ ->values([
+ 'share_type' => $qb->expr()->literal(\OCP\Share::SHARE_TYPE_REMOTE),
+ 'share_with' => $qb->expr()->literal('sharedWith'),
+ 'uid_owner' => $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());
+
+ // 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();
+
+
+ $storage = $this->getMock('OC\Files\Storage\Storage');
+ $storage
+ ->expects($this->once())
+ ->method('getOwner')
+ ->willReturn('shareOwner');
+ $path = $this->getMock('OCP\Files\Node');
+ $path
+ ->expects($this->once())
+ ->method('getStorage')
+ ->wilLReturn($storage);
+ $this->userFolder
+ ->expects($this->once())
+ ->method('getById')
+ ->with(42)
+ ->willReturn([$path]);
+
+ $sharedBy = $this->getMock('OCP\IUser');
+ $shareOwner = $this->getMock('OCP\IUser');
+ $this->userManager
+ ->method('get')
+ ->will($this->returnValueMap([
+ ['sharedBy', $sharedBy],
+ ['shareOwner', $shareOwner],
+ ]));
+
+ $share = $this->provider->getShareById($id);
+
+ $this->assertEquals($id, $share->getId());
+ $this->assertEquals(\OCP\Share::SHARE_TYPE_REMOTE, $share->getShareType());
+ $this->assertEquals('sharedWith', $share->getSharedWith());
+ $this->assertEquals($sharedBy, $share->getSharedBy());
+ $this->assertEquals($shareOwner, $share->getShareOwner());
+ $this->assertEquals($path, $share->getPath());
+ $this->assertEquals(13, $share->getPermissions());
+ $this->assertEquals(null, $share->getToken());
+ $this->assertEquals(null, $share->getExpirationDate());
+ $this->assertEquals('myTarget', $share->getTarget());
+ }
+
+ public function testDeleteSingleShare() {
+ $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('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());
+
+ // 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();
+
+
+ $path = $this->getMock('OCP\Files\File');
+ $path
+ ->expects($this->exactly(2))
+ ->method('getId')
+ ->willReturn(42);
+
+ $sharedWith = $this->getMock('OCP\IUser');
+ $sharedWith
+ ->expects($this->once())
+ ->method('getUID')
+ ->willReturn('sharedWith');
+ $sharedBy = $this->getMock('OCP\IUser');
+ $sharedBy
+ ->expects($this->once())
+ ->method('getUID')
+ ->willReturn('sharedBy');
+
+ $share = $this->getMock('OC\Share20\IShare');
+ $share
+ ->method('getId')
+ ->willReturn($id);
+ $share
+ ->expects($this->once())
+ ->method('getShareType')
+ ->willReturn(\OCP\Share::SHARE_TYPE_USER);
+ $share
+ ->expects($this->exactly(3))
+ ->method('getPath')
+ ->willReturn($path);
+ $share
+ ->expects($this->once())
+ ->method('getSharedWith')
+ ->willReturn($sharedWith);
+ $share
+ ->expects($this->once())
+ ->method('getSharedBy')
+ ->willReturn($sharedBy);
+ $share
+ ->expects($this->once())
+ ->method('getTarget')
+ ->willReturn('myTarget');
+
+ $provider = $this->getMockBuilder('OC\Share20\DefaultShareProvider')
+ ->setConstructorArgs([
+ $this->dbConn,
+ $this->userManager,
+ $this->groupManager,
+ $this->userFolder,
+ ]
+ )
+ ->setMethods(['deleteChildren', 'getShareById'])
+ ->getMock();
+ $provider
+ ->expects($this->once())
+ ->method('deleteChildren');
+ $provider
+ ->expects($this->once())
+ ->method('getShareById')
+ ->willReturn($share);
+
+ $hookListner = $this->getMockBuilder('Dummy')->setMethods(['listen'])->getMock();
+ \OCP\Util::connectHook('OCP\Share', 'pre_unshare', $hookListner, 'listen');
+ \OCP\Util::connectHook('OCP\Share', 'post_unshare', $hookListner, 'listen');
+
+ $hookListnerExpects = [
+ 'id' => $id,
+ 'itemType' => 'file',
+ 'itemSource' => 42,
+ 'shareType' => \OCP\Share::SHARE_TYPE_USER,
+ 'shareWith' => 'sharedWith',
+ 'itemparent' => null,
+ 'uidOwner' => 'sharedBy',
+ 'fileSource' => 42,
+ 'fileTarget' => 'myTarget',
+ ];
+
+ $hookListner
+ ->expects($this->exactly(2))
+ ->method('listen')
+ ->with($hookListnerExpects);
+
+
+ $provider->delete($share);
+
+ $qb = $this->dbConn->getQueryBuilder();
+ $qb->select('*')
+ ->from('share');
+
+ $cursor = $qb->execute();
+ $result = $cursor->fetchAll();
+ $cursor->closeCursor();
+
+ $this->assertEmpty($result);
+ }
+
+ public function testDeleteNestedShares() {
+ $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('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());
+
+ // Get the id
+ $qb = $this->dbConn->getQueryBuilder();
+ $cursor = $qb->select('id')
+ ->from('share')
+ ->setMaxResults(1)
+ ->orderBy('id', 'DESC')
+ ->execute();
+ $id1 = $cursor->fetch();
+ $id1 = $id1['id'];
+ $cursor->closeCursor();
+
+
+ $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('sharedBy'),
+ 'item_type' => $qb->expr()->literal('file'),
+ 'file_source' => $qb->expr()->literal(42),
+ 'file_target' => $qb->expr()->literal('myTarget'),
+ 'permissions' => $qb->expr()->literal(13),
+ 'parent' => $qb->expr()->literal($id1),
+ ]);
+ $this->assertEquals(1, $qb->execute());
+
+ // Get the id
+ $qb = $this->dbConn->getQueryBuilder();
+ $cursor = $qb->select('id')
+ ->from('share')
+ ->setMaxResults(1)
+ ->orderBy('id', 'DESC')
+ ->execute();
+ $id2 = $cursor->fetch();
+ $id2 = $id2['id'];
+ $cursor->closeCursor();
+
+
+ $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('sharedBy'),
+ 'item_type' => $qb->expr()->literal('file'),
+ 'file_source' => $qb->expr()->literal(42),
+ 'file_target' => $qb->expr()->literal('myTarget'),
+ 'permissions' => $qb->expr()->literal(13),
+ 'parent' => $qb->expr()->literal($id2),
+ ]);
+ $this->assertEquals(1, $qb->execute());
+
+ $storage = $this->getMock('OC\Files\Storage\Storage');
+ $storage
+ ->method('getOwner')
+ ->willReturn('shareOwner');
+ $path = $this->getMock('OCP\Files\Node');
+ $path
+ ->method('getStorage')
+ ->wilLReturn($storage);
+ $this->userFolder
+ ->method('getById')
+ ->with(42)
+ ->willReturn([$path]);
+
+ $sharedWith = $this->getMock('OCP\IUser');
+ $sharedWith
+ ->method('getUID')
+ ->willReturn('sharedWith');
+ $sharedBy = $this->getMock('OCP\IUser');
+ $sharedBy
+ ->method('getUID')
+ ->willReturn('sharedBy');
+ $shareOwner = $this->getMock('OCP\IUser');
+ $this->userManager
+ ->method('get')
+ ->will($this->returnValueMap([
+ ['sharedWith', $sharedWith],
+ ['sharedBy', $sharedBy],
+ ['shareOwner', $shareOwner],
+ ]));
+
+ $share = $this->provider->getShareById($id1);
+ $this->provider->delete($share);
+
+ $qb = $this->dbConn->getQueryBuilder();
+ $qb->select('*')
+ ->from('share');
+
+ $cursor = $qb->execute();
+ $result = $cursor->fetchAll();
+ $cursor->closeCursor();
+
+ $this->assertEmpty($result);
+ }
+
+ /**
+ * @expectedException \OC\Share20\Exception\BackendError
+ */
+ public function testDeleteFails() {
+ $share = $this->getMock('OC\Share20\IShare');
+ $share
+ ->method('getId')
+ ->willReturn(42);
+ $share
+ ->expects($this->once())
+ ->method('getShareType')
+ ->willReturn(\OCP\Share::SHARE_TYPE_LINK);
+
+ $path = $this->getMock('OCP\Files\Folder');
+ $path
+ ->expects($this->exactly(2))
+ ->method('getId')
+ ->willReturn(100);
+ $share
+ ->expects($this->exactly(3))
+ ->method('getPath')
+ ->willReturn($path);
+
+ $sharedBy = $this->getMock('OCP\IUser');
+ $sharedBy
+ ->expects($this->once())
+ ->method('getUID');
+ $share
+ ->expects($this->once())
+ ->method('getSharedBy')
+ ->willReturn($sharedBy);
+
+ $expr = $this->getMock('OCP\DB\QueryBuilder\IExpressionBuilder');
+ $qb = $this->getMock('OCP\DB\QueryBuilder\IQueryBuilder');
+ $qb->expects($this->once())
+ ->method('delete')
+ ->will($this->returnSelf());
+ $qb->expects($this->once())
+ ->method('expr')
+ ->willReturn($expr);
+ $qb->expects($this->once())
+ ->method('where')
+ ->will($this->returnSelf());
+ $qb->expects($this->once())
+ ->method('setParameter')
+ ->will($this->returnSelf());
+ $qb->expects($this->once())
+ ->method('execute')
+ ->will($this->throwException(new \Exception));
+
+ $db = $this->getMock('OCP\IDBConnection');
+ $db->expects($this->once())
+ ->method('getQueryBuilder')
+ ->with()
+ ->willReturn($qb);
+
+ $provider = $this->getMockBuilder('OC\Share20\DefaultShareProvider')
+ ->setConstructorArgs([
+ $db,
+ $this->userManager,
+ $this->groupManager,
+ $this->userFolder,
+ ]
+ )
+ ->setMethods(['deleteChildren', 'getShareById'])
+ ->getMock();
+ $provider
+ ->expects($this->once())
+ ->method('deleteChildren')
+ ->with($share);
+ $provider
+ ->expects($this->once())
+ ->method('getShareById')
+ ->with(42)
+ ->willReturn($share);
+
+ $provider->delete($share);
+ }
+}
diff --git a/tests/lib/share20/managertest.php b/tests/lib/share20/managertest.php
new file mode 100644
index 00000000000..fc6c30692f5
--- /dev/null
+++ b/tests/lib/share20/managertest.php
@@ -0,0 +1,198 @@
+<?php
+/**
+ * @author Roeland Jago Douma <rullzer@owncloud.com>
+ *
+ * @copyright Copyright (c) 2015, ownCloud, Inc.
+ * @license AGPL-3.0
+ *
+ * This code is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License, version 3,
+ * along with this program. If not, see <http://www.gnu.org/licenses/>
+ *
+ */
+namespace Test\Share20;
+
+use OC\Share20\Manager;
+use OC\Share20\Exception;
+
+
+use OCP\IUser;
+use OCP\IUserManager;
+use OCP\IGroupManager;
+use OCP\ILogger;
+use OCP\IAppConfig;
+use OCP\Files\Folder;
+use OCP\Share20\IShareProvider;
+
+class ManagerTest extends \Test\TestCase {
+
+ /** @var Manager */
+ protected $manager;
+
+ /** @var IUser */
+ protected $user;
+
+ /** @var IUserManager */
+ protected $userManager;
+
+ /** @var IGroupManager */
+ protected $groupManager;
+
+ /** @var ILogger */
+ protected $logger;
+
+ /** @var IAppConfig */
+ protected $appConfig;
+
+ /** @var Folder */
+ protected $userFolder;
+
+ /** @var IShareProvider */
+ protected $defaultProvider;
+
+ public function setUp() {
+
+ $this->user = $this->getMock('\OCP\IUser');
+ $this->userManager = $this->getMock('\OCP\IUserManager');
+ $this->groupManager = $this->getMock('\OCP\IGroupManager');
+ $this->logger = $this->getMock('\OCP\ILogger');
+ $this->appConfig = $this->getMock('\OCP\IAppConfig');
+ $this->userFolder = $this->getMock('\OCP\Files\Folder');
+ $this->defaultProvider = $this->getMock('\OC\Share20\IShareProvider');
+
+ $this->manager = new Manager(
+ $this->user,
+ $this->userManager,
+ $this->groupManager,
+ $this->logger,
+ $this->appConfig,
+ $this->userFolder,
+ $this->defaultProvider
+ );
+ }
+
+ /**
+ * @expectedException OC\Share20\Exception\ShareNotFound
+ */
+ public function testDeleteNoShareId() {
+ $share = $this->getMock('\OC\Share20\IShare');
+
+ $share
+ ->expects($this->once())
+ ->method('getId')
+ ->with()
+ ->willReturn(null);
+
+ $this->manager->deleteShare($share);
+ }
+
+ public function testDelete() {
+ $share = $this->getMock('\OC\Share20\IShare');
+
+ $share
+ ->expects($this->once())
+ ->method('getId')
+ ->with()
+ ->willReturn(42);
+ $this->defaultProvider
+ ->expects($this->once())
+ ->method('delete')
+ ->with($share);
+
+ $this->manager->deleteShare($share);
+ }
+
+ /**
+ * @expectedException OC\Share20\Exception\ShareNotFound
+ */
+ public function testGetShareByIdNotFoundInBackend() {
+ $this->defaultProvider
+ ->expects($this->once())
+ ->method('getShareById')
+ ->with(42)
+ ->will($this->throwException(new \OC\Share20\Exception\ShareNotFound()));
+
+ $this->manager->getShareById(42);
+ }
+
+ /**
+ * @expectedException OC\Share20\Exception\ShareNotFound
+ */
+ public function testGetShareByIdNotAuthorized() {
+ $otherUser1 = $this->getMock('\OCP\IUser');
+ $otherUser2 = $this->getMock('\OCP\IUser');
+ $otherUser3 = $this->getMock('\OCP\IUser');
+
+ $share = $this->getMock('\OC\Share20\IShare');
+ $share
+ ->expects($this->once())
+ ->method('getSharedWith')
+ ->with()
+ ->willReturn($otherUser1);
+ $share
+ ->expects($this->once())
+ ->method('getSharedBy')
+ ->with()
+ ->willReturn($otherUser2);
+ $share
+ ->expects($this->once())
+ ->method('getShareOwner')
+ ->with()
+ ->willReturn($otherUser3);
+
+ $this->defaultProvider
+ ->expects($this->once())
+ ->method('getShareById')
+ ->with(42)
+ ->willReturn($share);
+
+ $this->manager->getShareById(42);
+ }
+
+ public function dataGetShareById() {
+ return [
+ ['getSharedWith'],
+ ['getSharedBy'],
+ ['getShareOwner'],
+ ];
+ }
+
+ /**
+ * @dataProvider dataGetShareById
+ */
+ public function testGetShareById($currentUserIs) {
+ $otherUser1 = $this->getMock('\OCP\IUser');
+ $otherUser2 = $this->getMock('\OCP\IUser');
+ $otherUser3 = $this->getMock('\OCP\IUser');
+
+ $share = $this->getMock('\OC\Share20\IShare');
+ $share
+ ->method('getSharedWith')
+ ->with()
+ ->willReturn($currentUserIs === 'getSharedWith' ? $this->user : $otherUser1);
+ $share
+ ->method('getSharedBy')
+ ->with()
+ ->willReturn($currentUserIs === 'getSharedBy' ? $this->user : $otherUser2);
+ $share
+ ->method('getShareOwner')
+ ->with()
+ ->willReturn($currentUserIs === 'getShareOwner' ? $this->user : $otherUser3);
+
+ $this->defaultProvider
+ ->expects($this->once())
+ ->method('getShareById')
+ ->with(42)
+ ->willReturn($share);
+
+ $this->assertEquals($share, $this->manager->getShareById(42));
+ }
+}
diff --git a/tests/lib/subadmin.php b/tests/lib/subadmin.php
index 0855e514c7e..960943c22f5 100644
--- a/tests/lib/subadmin.php
+++ b/tests/lib/subadmin.php
@@ -55,6 +55,28 @@ class SubAdmin extends \Test\TestCase {
if (!$this->groupManager->groupExists('admin')) {
$this->groupManager->createGroup('admin');
}
+
+ // Create "orphaned" users and groups (scenario: temporarily disabled
+ // backend)
+ $qb = $this->dbConn->getQueryBuilder();
+ $qb->insert('group_admin')
+ ->values([
+ 'gid' => $qb->createNamedParameter($this->groups[0]->getGID()),
+ 'uid' => $qb->createNamedParameter('orphanedUser')
+ ])
+ ->execute();
+ $qb->insert('group_admin')
+ ->values([
+ 'gid' => $qb->createNamedParameter('orphanedGroup'),
+ 'uid' => $qb->createNamedParameter('orphanedUser')
+ ])
+ ->execute();
+ $qb->insert('group_admin')
+ ->values([
+ 'gid' => $qb->createNamedParameter('orphanedGroup'),
+ 'uid' => $qb->createNamedParameter($this->users[0]->getUID())
+ ])
+ ->execute();
}
public function tearDown() {
@@ -65,6 +87,12 @@ class SubAdmin extends \Test\TestCase {
foreach($this->groups as $group) {
$group->delete();
}
+
+ $qb = $this->dbConn->getQueryBuilder();
+ $qb->delete('group_admin')
+ ->where($qb->expr()->eq('uid', $qb->createNamedParameter('orphanedUser')))
+ ->orWhere($qb->expr()->eq('gid', $qb->createNamedParameter('orphanedGroup')))
+ ->execute();
}
public function testCreateSubAdmin() {
@@ -118,6 +146,7 @@ class SubAdmin extends \Test\TestCase {
$this->assertContains($this->groups[0], $result);
$this->assertContains($this->groups[1], $result);
$this->assertNotContains($this->groups[2], $result);
+ $this->assertNotContains(null, $result);
$this->assertTrue($subAdmin->deleteSubAdmin($this->users[0], $this->groups[0]));
$this->assertTrue($subAdmin->deleteSubAdmin($this->users[0], $this->groups[1]));
@@ -133,6 +162,7 @@ class SubAdmin extends \Test\TestCase {
$this->assertContains($this->users[0], $result);
$this->assertContains($this->users[1], $result);
$this->assertNotContains($this->users[2], $result);
+ $this->assertNotContains(null, $result);
$this->assertTrue($subAdmin->deleteSubAdmin($this->users[0], $this->groups[0]));
$this->assertTrue($subAdmin->deleteSubAdmin($this->users[1], $this->groups[0]));
@@ -150,6 +180,7 @@ class SubAdmin extends \Test\TestCase {
$this->assertContains(['user' => $this->users[0], 'group' => $this->groups[0]], $result);
$this->assertContains(['user' => $this->users[1], 'group' => $this->groups[1]], $result);
$this->assertContains(['user' => $this->users[2], 'group' => $this->groups[1]], $result);
+ $this->assertNotContains(['user' => null, 'group' => null], $result);
}
public function testIsSubAdminofGroup() {