Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add GetUsersSharingFileTest #41122

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
129 changes: 123 additions & 6 deletions tests/lib/Share/GetUsersSharingFileTest.php
Original file line number Diff line number Diff line change
@@ -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,101 @@ 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 {
self::assertTrue(Share::isEnabled());
# 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);
}
}