diff options
Diffstat (limited to 'tests/lib')
-rw-r--r-- | tests/lib/UrlGeneratorTest.php | 111 | ||||
-rw-r--r-- | tests/lib/UtilTest.php | 105 |
2 files changed, 111 insertions, 105 deletions
diff --git a/tests/lib/UrlGeneratorTest.php b/tests/lib/UrlGeneratorTest.php index 761f4b42eea..9e5795fc41e 100644 --- a/tests/lib/UrlGeneratorTest.php +++ b/tests/lib/UrlGeneratorTest.php @@ -13,6 +13,8 @@ use OCP\ICacheFactory; use OCP\IConfig; use OCP\IRequest; use OCP\IURLGenerator; +use OCP\IUser; +use OCP\IUserSession; /** * Class UrlGeneratorTest @@ -23,6 +25,8 @@ 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|ICacheFactory */ private $cacheFactory; /** @var \PHPUnit\Framework\MockObject\MockObject|IRequest */ @@ -37,11 +41,13 @@ class UrlGeneratorTest extends \Test\TestCase { protected function setUp(): void { parent::setUp(); $this->config = $this->createMock(IConfig::class); + $this->userSession = $this->createMock(IUserSession::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->cacheFactory, $this->request, $this->router @@ -210,4 +216,109 @@ class UrlGeneratorTest extends \Test\TestCase { ['core.WhatsNew.dismiss', 'http://localhost/nextcloud/ocs/v2.php/core/whatsnew'], ]; } + + private function mockLinkToDefaultPageUrl(string $defaultAppConfig = '', bool $ignoreFrontControllerConfig = false) { + $this->config->expects($this->exactly(2)) + ->method('getSystemValue') + ->withConsecutive( + ['defaultapp', $this->anything()], + ['htaccess.IgnoreFrontController', $this->anything()], + ) + ->will($this->onConsecutiveCalls( + $defaultAppConfig, + $ignoreFrontControllerConfig + )); + $this->config->expects($this->once()) + ->method('getAppValue') + ->with('core', 'defaultpage') + ->willReturn(''); + } + + public function testLinkToDefaultPageUrlWithRedirectUrlWithoutFrontController() { + $this->mockBaseUrl(); + + $_REQUEST['redirect_url'] = 'myRedirectUrl.com'; + $this->assertSame('http://localhost' . \OC::$WEBROOT . '/myRedirectUrl.com', $this->urlGenerator->linkToDefaultPageUrl()); + } + + public function testLinkToDefaultPageUrlWithRedirectUrlRedirectBypassWithoutFrontController() { + $this->mockBaseUrl(); + $this->mockLinkToDefaultPageUrl(); + putenv('front_controller_active=false'); + + $_REQUEST['redirect_url'] = 'myRedirectUrl.com@foo.com:a'; + $this->assertSame('http://localhost' . \OC::$WEBROOT . '/index.php/apps/files/', $this->urlGenerator->linkToDefaultPageUrl()); + } + + public function testLinkToDefaultPageUrlWithRedirectUrlRedirectBypassWithFrontController() { + $this->mockBaseUrl(); + $this->mockLinkToDefaultPageUrl(); + putenv('front_controller_active=true'); + + $_REQUEST['redirect_url'] = 'myRedirectUrl.com@foo.com:a'; + $this->assertSame('http://localhost' . \OC::$WEBROOT . '/apps/files/', $this->urlGenerator->linkToDefaultPageUrl()); + } + + public function testLinkToDefaultPageUrlWithRedirectUrlWithIgnoreFrontController() { + $this->mockBaseUrl(); + $this->mockLinkToDefaultPageUrl('', true); + putenv('front_controller_active=false'); + + $_REQUEST['redirect_url'] = 'myRedirectUrl.com@foo.com:a'; + $this->assertSame('http://localhost' . \OC::$WEBROOT . '/apps/files/', $this->urlGenerator->linkToDefaultPageUrl()); + } + + /** + * @dataProvider provideDefaultApps + */ + public function testLinkToDefaultPageUrlWithDefaultApps($defaultAppConfig, $expectedPath) { + $userId = $this->getUniqueID(); + + /** @var \PHPUnit\Framework\MockObject\MockObject|IUser $userMock */ + $userMock = $this->createMock(IUser::class); + $userMock->expects($this->once()) + ->method('getUID') + ->willReturn($userId); + + $this->mockBaseUrl(); + $this->mockLinkToDefaultPageUrl($defaultAppConfig); + + $this->config->expects($this->once()) + ->method('getUserValue') + ->with($userId, 'core', 'defaultapp') + ->willReturn(''); + $this->userSession->expects($this->once()) + ->method('isLoggedIn') + ->willReturn(true); + $this->userSession->expects($this->once()) + ->method('getUser') + ->willReturn($userMock); + + $this->assertEquals('http://localhost' . \OC::$WEBROOT . $expectedPath, $this->urlGenerator->linkToDefaultPageUrl()); + } + + public function provideDefaultApps() { + return [ + // none specified, default to files + [ + '', + '/index.php/apps/files/', + ], + // unexisting or inaccessible app specified, default to files + [ + 'unexist', + '/index.php/apps/files/', + ], + // non-standard app + [ + 'settings', + '/index.php/apps/settings/', + ], + // non-standard app with fallback + [ + 'unexist,settings', + '/index.php/apps/settings/', + ], + ]; + } } diff --git a/tests/lib/UtilTest.php b/tests/lib/UtilTest.php index e21a5323b1b..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,96 +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); - }); - Dummy_OC_Util::$appManager = $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, Dummy_OC_Util::getDefaultPageUrl()); - - // restore old state - \OC::$WEBROOT = $oldWebRoot; - \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'], - ], - ]; - } - - public function testGetDefaultPageUrlWithRedirectUrlWithoutFrontController() { - putenv('front_controller_active=false'); - \OC::$server->getConfig()->deleteSystemValue('htaccess.IgnoreFrontController'); - - $_REQUEST['redirect_url'] = 'myRedirectUrl.com'; - $this->assertSame('http://localhost'.\OC::$WEBROOT.'/myRedirectUrl.com', OC_Util::getDefaultPageUrl()); - } - - public function testGetDefaultPageUrlWithRedirectUrlRedirectBypassWithoutFrontController() { - putenv('front_controller_active=false'); - \OC::$server->getConfig()->deleteSystemValue('htaccess.IgnoreFrontController'); - - $_REQUEST['redirect_url'] = 'myRedirectUrl.com@foo.com:a'; - $this->assertSame('http://localhost'.\OC::$WEBROOT.'/index.php/apps/files/', OC_Util::getDefaultPageUrl()); - } - - public function testGetDefaultPageUrlWithRedirectUrlRedirectBypassWithFrontController() { - putenv('front_controller_active=true'); - $_REQUEST['redirect_url'] = 'myRedirectUrl.com@foo.com:a'; - $this->assertSame('http://localhost'.\OC::$WEBROOT.'/apps/files/', OC_Util::getDefaultPageUrl()); - } - - public function testGetDefaultPageUrlWithRedirectUrlWithIgnoreFrontController() { - putenv('front_controller_active=false'); - \OC::$server->getConfig()->setSystemValue('htaccess.IgnoreFrontController', true); - - $_REQUEST['redirect_url'] = 'myRedirectUrl.com@foo.com:a'; - $this->assertSame('http://localhost'.\OC::$WEBROOT.'/apps/files/', OC_Util::getDefaultPageUrl()); - } - - /** * Test needUpgrade() when the core version is increased */ public function testNeedUpgradeCore() { @@ -390,17 +299,3 @@ class UtilTest extends \Test\TestCase { ], \OC_Util::$styles); } } - -/** - * Dummy OC Util class to make it possible to override the app manager - */ -class Dummy_OC_Util extends OC_Util { - /** - * @var \OCP\App\IAppManager - */ - public static $appManager; - - protected static function getAppManager() { - return self::$appManager; - } -} |