mirror of
https://github.com/nextcloud/server.git
synced 2026-06-08 16:26:59 -04:00
Move UtilTest::testDefaultApps() to UrlGeneratorTest
Signed-off-by: Daniel Rudolf <github.com@daniel-rudolf.de>
This commit is contained in:
parent
e16bf707aa
commit
15a445f743
3 changed files with 84 additions and 66 deletions
|
|
@ -42,6 +42,7 @@ namespace OC;
|
|||
|
||||
use OC\Route\Router;
|
||||
use OCA\Theming\ThemingDefaults;
|
||||
use OCP\App\IAppManager;
|
||||
use OCP\ICacheFactory;
|
||||
use OCP\IConfig;
|
||||
use OCP\IRequest;
|
||||
|
|
@ -55,6 +56,10 @@ use RuntimeException;
|
|||
class URLGenerator implements IURLGenerator {
|
||||
/** @var IConfig */
|
||||
private $config;
|
||||
/** @var IUserSession */
|
||||
public $userSession;
|
||||
/** @var IAppManager */
|
||||
public $appManager;
|
||||
/** @var ICacheFactory */
|
||||
private $cacheFactory;
|
||||
/** @var IRequest */
|
||||
|
|
@ -65,10 +70,14 @@ class URLGenerator implements IURLGenerator {
|
|||
private $baseUrl = null;
|
||||
|
||||
public function __construct(IConfig $config,
|
||||
IUserSession $userSession,
|
||||
IAppManager $appManager,
|
||||
ICacheFactory $cacheFactory,
|
||||
IRequest $request,
|
||||
Router $router) {
|
||||
$this->config = $config;
|
||||
$this->userSession = $userSession;
|
||||
$this->appManager = $appManager;
|
||||
$this->cacheFactory = $cacheFactory;
|
||||
$this->request = $request;
|
||||
$this->router = $router;
|
||||
|
|
@ -289,9 +298,7 @@ class URLGenerator implements IURLGenerator {
|
|||
$appId = 'files';
|
||||
$defaultApps = explode(',', $this->config->getSystemValue('defaultapp', 'dashboard,files'));
|
||||
|
||||
/** @var IUserSession $userSession */
|
||||
$userSession = \OC::$server->get(IUserSession::class);
|
||||
$userId = $userSession->isLoggedIn() ? $userSession->getUser()->getUID() : null;
|
||||
$userId = $this->userSession->isLoggedIn() ? $this->userSession->getUser()->getUID() : null;
|
||||
if ($userId !== null) {
|
||||
$userDefaultApps = explode(',', $this->config->getUserValue($userId, 'core', 'defaultapp'));
|
||||
$defaultApps = array_filter(array_merge($userDefaultApps, $defaultApps));
|
||||
|
|
@ -300,7 +307,7 @@ class URLGenerator implements IURLGenerator {
|
|||
// find the first app that is enabled for the current user
|
||||
foreach ($defaultApps as $defaultApp) {
|
||||
$defaultApp = \OC_App::cleanAppId(strip_tags($defaultApp));
|
||||
if (\OC::$server->getAppManager()->isEnabledForUser($defaultApp)) {
|
||||
if ($this->appManager->isEnabledForUser($defaultApp)) {
|
||||
$appId = $defaultApp;
|
||||
break;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,10 +9,13 @@
|
|||
namespace Test;
|
||||
|
||||
use OC\Route\Router;
|
||||
use OCP\App\IAppManager;
|
||||
use OCP\ICacheFactory;
|
||||
use OCP\IConfig;
|
||||
use OCP\IRequest;
|
||||
use OCP\IURLGenerator;
|
||||
use OCP\IUser;
|
||||
use OCP\IUserSession;
|
||||
|
||||
/**
|
||||
* Class UrlGeneratorTest
|
||||
|
|
@ -23,6 +26,10 @@ class UrlGeneratorTest extends \Test\TestCase {
|
|||
|
||||
/** @var \PHPUnit\Framework\MockObject\MockObject|IConfig */
|
||||
private $config;
|
||||
/** @var \PHPUnit\Framework\MockObject\MockObject|IUserSession */
|
||||
private $userSession;
|
||||
/** @var \PHPUnit\Framework\MockObject\MockObject|IAppManager */
|
||||
private $appManager;
|
||||
/** @var \PHPUnit\Framework\MockObject\MockObject|ICacheFactory */
|
||||
private $cacheFactory;
|
||||
/** @var \PHPUnit\Framework\MockObject\MockObject|IRequest */
|
||||
|
|
@ -37,11 +44,15 @@ class UrlGeneratorTest extends \Test\TestCase {
|
|||
protected function setUp(): void {
|
||||
parent::setUp();
|
||||
$this->config = $this->createMock(IConfig::class);
|
||||
$this->userSession = $this->createMock(IUserSession::class);
|
||||
$this->appManager = $this->createMock(IAppManager::class);
|
||||
$this->cacheFactory = $this->createMock(ICacheFactory::class);
|
||||
$this->request = $this->createMock(IRequest::class);
|
||||
$this->router = $this->createMock(Router::class);
|
||||
$this->urlGenerator = new \OC\URLGenerator(
|
||||
$this->config,
|
||||
$this->userSession,
|
||||
$this->appManager,
|
||||
$this->cacheFactory,
|
||||
$this->request,
|
||||
$this->router
|
||||
|
|
@ -234,4 +245,66 @@ class UrlGeneratorTest extends \Test\TestCase {
|
|||
$_REQUEST['redirect_url'] = 'myRedirectUrl.com@foo.com:a';
|
||||
$this->assertSame('http://localhost'.\OC::$WEBROOT.'/apps/files/', $this->urlGenerator->linkToDefaultPageUrl());
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider provideDefaultApps
|
||||
*/
|
||||
public function testGetDefaultPageUrlWithDefaultApps($defaultAppConfig, $expectedPath, $enabledApps) {
|
||||
$oldDefaultApps = \OC::$server->getConfig()->getSystemValue('defaultapp', '');
|
||||
|
||||
/** @var \PHPUnit\Framework\MockObject\MockObject|IUser $userMock */
|
||||
$userMock = $this->createMock(IUser::class);
|
||||
$userMock->expects($this->once())
|
||||
->method('getUID')
|
||||
->willReturn($this->getUniqueID());
|
||||
|
||||
$this->userSession->expects($this->once())
|
||||
->method('isLoggedIn')
|
||||
->willReturn(true);
|
||||
$this->userSession->expects($this->once())
|
||||
->method('getUser')
|
||||
->willReturn($userMock);
|
||||
$this->appManager->expects($this->any())
|
||||
->method('isEnabledForUser')
|
||||
->willReturnCallback(function ($appId) use ($enabledApps) {
|
||||
return in_array($appId, $enabledApps);
|
||||
});
|
||||
|
||||
try {
|
||||
\OC::$server->getConfig()->setSystemValue('defaultapp', $defaultAppConfig);
|
||||
$this->assertEquals('http://localhost' . \OC::$WEBROOT . $expectedPath, $this->urlGenerator->linkToDefaultPageUrl());
|
||||
} finally {
|
||||
// restore old state
|
||||
\OC::$server->getConfig()->setSystemValue('defaultapp', $oldDefaultApps);
|
||||
}
|
||||
}
|
||||
|
||||
public function provideDefaultApps() {
|
||||
return [
|
||||
// none specified, default to files
|
||||
[
|
||||
'',
|
||||
'apps/files/',
|
||||
['files'],
|
||||
],
|
||||
// unexisting or inaccessible app specified, default to files
|
||||
[
|
||||
'unexist',
|
||||
'apps/files/',
|
||||
['files'],
|
||||
],
|
||||
// non-standard app
|
||||
[
|
||||
'calendar',
|
||||
'apps/calendar/',
|
||||
['files', 'calendar'],
|
||||
],
|
||||
// non-standard app with fallback
|
||||
[
|
||||
'contacts,calendar',
|
||||
'apps/calendar/',
|
||||
['files', 'calendar'],
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,7 +9,6 @@
|
|||
namespace Test;
|
||||
|
||||
use OC_Util;
|
||||
use OCP\App\IAppManager;
|
||||
|
||||
/**
|
||||
* Class UtilTest
|
||||
|
|
@ -169,67 +168,6 @@ class UtilTest extends \Test\TestCase {
|
|||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Test default apps
|
||||
*
|
||||
* @dataProvider defaultAppsProvider
|
||||
* @group DB
|
||||
*/
|
||||
public function testDefaultApps($defaultAppConfig, $expectedPath, $enabledApps) {
|
||||
$oldDefaultApps = \OC::$server->getConfig()->getSystemValue('defaultapp', '');
|
||||
// CLI is doing messy stuff with the webroot, so need to work it around
|
||||
$oldWebRoot = \OC::$WEBROOT;
|
||||
\OC::$WEBROOT = '';
|
||||
|
||||
$appManager = $this->createMock(IAppManager::class);
|
||||
$appManager->expects($this->any())
|
||||
->method('isEnabledForUser')
|
||||
->willReturnCallback(function ($appId) use ($enabledApps) {
|
||||
return in_array($appId, $enabledApps);
|
||||
});
|
||||
$this->overwriteService(IAppManager::class, $appManager);
|
||||
|
||||
// need to set a user id to make sure enabled apps are read from cache
|
||||
\OC_User::setUserId($this->getUniqueID());
|
||||
\OC::$server->getConfig()->setSystemValue('defaultapp', $defaultAppConfig);
|
||||
$this->assertEquals('http://localhost/' . $expectedPath, OC_Util::getDefaultPageUrl());
|
||||
|
||||
// restore old state
|
||||
\OC::$WEBROOT = $oldWebRoot;
|
||||
$this->restoreService(IAppManager::class);
|
||||
\OC::$server->getConfig()->setSystemValue('defaultapp', $oldDefaultApps);
|
||||
\OC_User::setUserId(null);
|
||||
}
|
||||
|
||||
public function defaultAppsProvider() {
|
||||
return [
|
||||
// none specified, default to files
|
||||
[
|
||||
'',
|
||||
'index.php/apps/files/',
|
||||
['files'],
|
||||
],
|
||||
// unexisting or inaccessible app specified, default to files
|
||||
[
|
||||
'unexist',
|
||||
'index.php/apps/files/',
|
||||
['files'],
|
||||
],
|
||||
// non-standard app
|
||||
[
|
||||
'calendar',
|
||||
'index.php/apps/calendar/',
|
||||
['files', 'calendar'],
|
||||
],
|
||||
// non-standard app with fallback
|
||||
[
|
||||
'contacts,calendar',
|
||||
'index.php/apps/calendar/',
|
||||
['files', 'calendar'],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Test needUpgrade() when the core version is increased
|
||||
*/
|
||||
|
|
|
|||
Loading…
Reference in a new issue