Skip to content

Commit

Permalink
test: add GetUsersSharingFileTest
Browse files Browse the repository at this point in the history
  • Loading branch information
DeepDiver1975 committed Nov 23, 2023
1 parent dcb0f9d commit 43fd4e3
Showing 1 changed file with 122 additions and 6 deletions.
128 changes: 122 additions & 6 deletions tests/lib/Share/GetUsersSharingFileTest.php
@@ -1,8 +1,9 @@
<?php

namespace lib\Share;
namespace Test\Share;

use OC;
use OCP\Files\Node;
use OC\Share\Constants;
use OC\Share\Share;
use OC\User\NoUserException;
Expand All @@ -14,6 +15,7 @@
use OCP\Share\Exceptions\GenericShareException;
use OCP\Share\IShare;
use Test\TestCase;
use Test\Traits\AssertQueryCountTrait;
use Test\Traits\GroupTrait;
use Test\Traits\UserTrait;
use function PHPUnit\Framework\assertEquals;
Expand All @@ -22,7 +24,7 @@
* @group DB
*/
class GetUsersSharingFileTest extends TestCase {
use UserTrait, GroupTrait;
use UserTrait, GroupTrait, AssertQueryCountTrait;

/**
* @throws GenericShareException
Expand All @@ -42,7 +44,7 @@ private static function shareWithGroup(File $file, IGroup $group, IUser $owner):
/**
* @throws GenericShareException
*/
private static function shareWithUser(File $file, IUser $user, IUser $owner): IShare {
private static function shareWithUser(Node $file, IUser $user, IUser $owner): IShare {
$sm = OC::$server->getShareManager();
$share = $sm->newShare();
$share->setSharedBy($owner->getUID());
Expand All @@ -54,6 +56,20 @@ private static function shareWithUser(File $file, IUser $user, IUser $owner): IS
return $sm->createShare($share);
}

/**
* @throws GenericShareException
*/
private static function shareByLink(File $file, IUser $owner): IShare {
$sm = OC::$server->getShareManager();
$share = $sm->newShare();
$share->setSharedBy($owner->getUID());
$share->setNode($file);
$share->setShareType(Constants::SHARE_TYPE_LINK);
$share->setPermissions(OCConstants::PERMISSION_READ);

return $sm->createShare($share);
}

/**
* @see https://github.com/owncloud/activity/issues/1143
* @throws NotPermittedException
Expand All @@ -69,15 +85,17 @@ public function testGroupShare() : void {
self::loginAsUser($alice->getUID());
$aliceFolder = OC::$server->getUserFolder($alice->getUID());
$file = self::createFileWithContent($aliceFolder, 'welcome.txt', 'loram ipsum');
$share = self::shareWithGroup($file, $group, $alice);
self::shareWithGroup($file, $group, $alice);

# test
self::trackQueries();
$result = Share::getUsersSharingFile('welcome.txt', $alice->getUID());
assertEquals([
'users' => ['alice', 'bob'],
'public' => false,
'remote' => false
], $result);
$this->assertQueryCountLessThan(9);
}

/**
Expand All @@ -96,16 +114,18 @@ public function testRejected() : void {
$share = self::shareWithUser($file, $bob, $alice);

# test accepted
self::trackQueries();
$result = Share::getUsersSharingFile('welcome.txt', $alice->getUID());
assertEquals([
'users' => ['bob'],
'public' => false,
'remote' => false
], $result);
$this->assertQueryCountMatches(7);

# tst rejected
# test rejected
$share->setState(Constants::STATE_REJECTED);
\OC::$server->getShareManager()->updateShare($share);
OC::$server->getShareManager()->updateShare($share);

$result = Share::getUsersSharingFile('welcome.txt', $alice->getUID());
assertEquals([
Expand All @@ -114,4 +134,100 @@ public function testRejected() : void {
'remote' => false
], $result);
}

/**
* @throws NotPermittedException
* @throws NoUserException
* @throws GenericShareException
*/
public function testPublicLink(): void {
# setup
$alice = $this->createUser('alice');

self::loginAsUser($alice->getUID());
$aliceFolder = OC::$server->getUserFolder($alice->getUID());
$file = self::createFileWithContent($aliceFolder, 'welcome.txt', 'loram ipsum');
self::shareByLink($file, $alice);

# test
self::trackQueries();
$result = Share::getUsersSharingFile('welcome.txt', $alice->getUID());
assertEquals([
'users' => [],
'public' => true,
'remote' => false
], $result);
$this->assertQueryCountMatches(7);
}

/**
* @throws NotPermittedException
* @throws NoUserException
* @throws GenericShareException
*/
public function testRecursive() : void {
# setup
$alice = $this->createUser('alice');
$bob = $this->createUser('bob');

self::loginAsUser($alice->getUID());
$aliceFolder = OC::$server->getUserFolder($alice->getUID());
$fooFolder = $aliceFolder->newFolder('foo');
self::createFileWithContent($fooFolder, 'welcome.txt', 'loram ipsum');
self::shareWithUser($fooFolder, $bob, $alice);

# test recursive
$result = Share::getUsersSharingFile('foo/welcome.txt', $alice->getUID());
assertEquals([
'users' => ['bob'],
'public' => false,
'remote' => false
], $result);

# test recursive
self::trackQueries();
$result = Share::getUsersSharingFile('foo/welcome.txt', $alice->getUID(), false, false, false);
assertEquals([
'users' => [],
'public' => false,
'remote' => false
], $result);
$this->assertQueryCountMatches(4);
}

/**
* @throws NotPermittedException
* @throws NoUserException
* @throws GenericShareException
*/
public function testIncludeOwner() : void {
# setup
$alice = $this->createUser('alice');
$bob = $this->createUser('bob');

self::loginAsUser($alice->getUID());
$aliceFolder = OC::$server->getUserFolder($alice->getUID());
$fooFolder = $aliceFolder->newFolder('foo');
self::createFileWithContent($fooFolder, 'welcome.txt', 'loram ipsum');
self::shareWithUser($fooFolder, $bob, $alice);

self::trackQueries();
$result = Share::getUsersSharingFile('foo/welcome.txt', $alice->getUID(), true, true);
assertEquals([
'bob' => '/foo/welcome.txt',
'alice' => '/foo/welcome.txt',
], $result);
# in some cases there is an additional query required because Cache::$path_cache is not holding the path in question
#$this->assertQueryCountLessThan(11);

# let's see how bob is doing ....
self::loginAsUser($bob->getUID());
self::trackQueries();
$result = Share::getUsersSharingFile('foo/welcome.txt', $bob->getUID(), true, true);
assertEquals([
'bob' => '/foo/welcome.txt',
], $result);
# in some cases there is an additional query required because Cache::$path_cache is not holding the path in question
#$this->assertQueryCountLessThan(11);
}
}

0 comments on commit 43fd4e3

Please sign in to comment.