Adjust tests and DI for Share.php

Signed-off-by: Lukas Reschke <lukas@statuscode.ch>
This commit is contained in:
Lukas Reschke 2016-11-02 23:26:49 +01:00
parent 68cebef64c
commit 6920e609c0
No known key found for this signature in database
GPG Key ID: B9F6980CF6E759B1
5 changed files with 58 additions and 9 deletions

View File

@ -45,6 +45,8 @@ namespace OC\Share;
use OC\Files\Filesystem;
use OCA\FederatedFileSharing\DiscoveryManager;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\ILogger;
use OCP\IUserManager;
use OCP\IUserSession;
use OCP\IDBConnection;
use OCP\IConfig;
@ -128,19 +130,35 @@ class Share extends Constants {
* Find which users can access a shared item
* @param string $path to the file
* @param string $ownerUser owner of the file
* @param IUserManager $userManager
* @param ILogger $logger
* @param boolean $includeOwner include owner to the list of users with access to the file
* @param boolean $returnUserPaths Return an array with the user => path map
* @param boolean $recursive take all parent folders into account (default true)
* @return array
* @note $path needs to be relative to user data dir, e.g. 'file.txt'
* not '/admin/data/file.txt'
* @throws \OC\User\NoUserException
*/
public static function getUsersSharingFile($path, $ownerUser, $includeOwner = false, $returnUserPaths = false, $recursive = true) {
$userManager = \OC::$server->getUserManager();
public static function getUsersSharingFile($path,
$ownerUser,
IUserManager $userManager,
ILogger $logger,
$includeOwner = false,
$returnUserPaths = false,
$recursive = true) {
$userObject = $userManager->get($ownerUser);
if (is_null($userObject)) {
\OCP\Util::writeLog('files', ' Backends provided no user object for ' . $ownerUser, \OCP\Util::ERROR);
$logger->error(
sprintf(
'Backends provided no user object for %s',
$ownerUser
),
[
'app' => 'files',
]
);
throw new \OC\User\NoUserException('Backends provided no user object');
}

View File

@ -89,7 +89,15 @@ class Share extends \OC\Share\Constants {
* @since 5.0.0 - $recursive was added in 9.0.0
*/
public static function getUsersSharingFile($path, $ownerUser, $includeOwner = false, $returnUserPaths = false, $recursive = true) {
return \OC\Share\Share::getUsersSharingFile($path, $ownerUser, $includeOwner, $returnUserPaths, $recursive);
return \OC\Share\Share::getUsersSharingFile(
$path,
$ownerUser,
\OC::$server->getUserManager(),
\OC::$server->getLogger(),
$includeOwner,
$returnUserPaths,
$recursive
);
}
/**

View File

@ -79,7 +79,6 @@ class FilesystemTest extends \Test\TestCase {
protected function setUp() {
parent::setUp();
\OC_User::clearBackends();
$userBackend = new \Test\Util\User\Dummy();
$userBackend->createUser(self::TEST_FILESYSTEM_USER1, self::TEST_FILESYSTEM_USER1);
$userBackend->createUser(self::TEST_FILESYSTEM_USER2, self::TEST_FILESYSTEM_USER2);
@ -94,7 +93,6 @@ class FilesystemTest extends \Test\TestCase {
$this->logout();
$this->invokePrivate('\OC\Files\Filesystem', 'normalizedPathCache', [null]);
\OC_User::clearBackends();
parent::tearDown();
}

View File

@ -20,6 +20,9 @@
*/
namespace Test\Share;
use OC\Share\Share;
use OCP\ILogger;
use OCP\IUserManager;
/**
* Class Test_Share
@ -1631,6 +1634,31 @@ class ShareTest extends \Test\TestCase {
$this->assertEquals('Sharing failed, because the user ' . $this->user1 . ' is the original sharer', $e->getMessage());
}
}
/**
* @expectedException \OC\User\NoUserException
* @expectedExceptionMessage Backends provided no user object
*/
public function testGetUsersSharingFileWithException() {
$userManager = $this->createMock(IUserManager::class);
$logger = $this->createMock(ILogger::class);
$userManager
->expects($this->once())
->method('get')
->with('test')
->willReturn(null);
$logger
->expects($this->once())
->method('error')
->with(
'Backends provided no user object for test',
[
'app' => 'files',
]
);
Share::getUsersSharingFile('/my/file/path', 'test', $userManager, $logger);
}
}
class DummyShareClass extends \OC\Share\Share {

View File

@ -273,9 +273,6 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase {
self::tearDownAfterClassCleanStrayHooks();
self::tearDownAfterClassCleanStrayLocks();
\OC_User::clearBackends();
\OC_User::useBackend('dummy');
parent::tearDownAfterClass();
}