diff options
-rw-r--r-- | lib/private/URLGenerator.php | 15 | ||||
-rw-r--r-- | tests/lib/UrlGeneratorTest.php | 73 | ||||
-rw-r--r-- | tests/lib/UtilTest.php | 62 |
3 files changed, 84 insertions, 66 deletions
diff --git a/lib/private/URLGenerator.php b/lib/private/URLGenerator.php index 1d8cb041611..7c97a76e257 100644 --- a/lib/private/URLGenerator.php +++ b/lib/private/URLGenerator.php @@ -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; } diff --git a/tests/lib/UrlGeneratorTest.php b/tests/lib/UrlGeneratorTest.php index 732aa2d2a27..5ba2e166829 100644 --- a/tests/lib/UrlGeneratorTest.php +++ b/tests/lib/UrlGeneratorTest.php @@ -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'], + ], + ]; + } } diff --git a/tests/lib/UtilTest.php b/tests/lib/UtilTest.php index 9a82f3c5ac6..04d4858fb26 100644 --- a/tests/lib/UtilTest.php +++ b/tests/lib/UtilTest.php @@ -9,7 +9,6 @@ namespace Test; use OC_Util; -use OCP\App\IAppManager; /** * Class UtilTest @@ -170,67 +169,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 */ public function testNeedUpgradeCore() { |