From 42d8b3ed6c7642d4115aa88816ad69bd9926273e Mon Sep 17 00:00:00 2001 From: Victor Dubiniuk Date: Wed, 13 Jun 2018 16:39:57 +0300 Subject: [PATCH] Fix get imprint and privacy policy URLs when not installed --- lib/private/legacy/defaults.php | 26 +++++++++++++++--- tests/lib/legacy/DefaultsTest.php | 44 +++++++++++++++++++++++-------- 2 files changed, 56 insertions(+), 14 deletions(-) diff --git a/lib/private/legacy/defaults.php b/lib/private/legacy/defaults.php index dc5103e90f89..3639fed5441d 100644 --- a/lib/private/legacy/defaults.php +++ b/lib/private/legacy/defaults.php @@ -1,4 +1,7 @@ * @author Jan-Christoph Borchardt @@ -49,7 +52,7 @@ class OC_Defaults { private $defaultLogoClaim; private $defaultMailHeaderColor; /** - * @var \OCP\IConfig + * @var IConfig */ private $config; @@ -304,7 +307,11 @@ public function getMailHeaderColor() { * @return string */ public function getImprintUrl() { - return $this->config->getAppValue('core', 'legal.imprint_url', ''); + try { + return $this->config->getAppValue('core', 'legal.imprint_url', ''); + } catch (\Exception $e) { + return ''; + } } /** @@ -312,6 +319,19 @@ public function getImprintUrl() { * @return string */ public function getPrivacyPolicyUrl() { - return $this->config->getAppValue('core', 'legal.privacy_policy_url', ''); + try { + return $this->config->getAppValue('core', 'legal.privacy_policy_url', ''); + } catch (\Exception $e) { + return ''; + } + } + + /** + * @internal Used for unit tests + * + * @param IConfig $config + */ + public function setConfig(IConfig $config) { + $this->config = $config; } } diff --git a/tests/lib/legacy/DefaultsTest.php b/tests/lib/legacy/DefaultsTest.php index b86e4864284a..ebcd1118554b 100644 --- a/tests/lib/legacy/DefaultsTest.php +++ b/tests/lib/legacy/DefaultsTest.php @@ -28,27 +28,49 @@ class DefaultsTest extends TestCase { */ protected $defaults; - protected function setUp() { - $this->defaults = $this->getMockBuilder(OC_Defaults::class) - ->setMethods(['themeExist', 'getImprintUrl', 'getPrivacyPolicyUrl']) - ->getMock(); - return parent::setUp(); - } - public function testGetShortFooterFromCore() { $imprintUrl = 'http://example.org/imprint'; $privacyPolicyUrl = 'http://example.org/privacy'; - $this->defaults->expects($this->any()) + $defaults = $this->getDefaultsMock( + ['themeExist', 'getImprintUrl', 'getPrivacyPolicyUrl'] + ); + $defaults->expects($this->any()) ->method('themeExist') ->willReturn(false); - $this->defaults->expects($this->exactly(2)) + $defaults->expects($this->exactly(2)) ->method('getImprintUrl') ->willReturn($imprintUrl); - $this->defaults->expects($this->exactly(2)) + $defaults->expects($this->exactly(2)) ->method('getPrivacyPolicyUrl') ->willReturn($privacyPolicyUrl); - $footer = $this->defaults->getShortFooter(); + $footer = $defaults->getShortFooter(); $this->assertContains($privacyPolicyUrl, $footer); $this->assertContains($imprintUrl, $footer); } + + public function testGetImprintWhenNotInstalled() { + $config = $this->getMockBuilder(\OCP\IConfig::class) + ->disableOriginalConstructor() + ->getMock(); + $config->expects($this->any()) + ->method('getAppValue') + ->willThrowException(new \Exception()); + $defaults = $this->getDefaultsMock( + ['themeExist'] + ); + $defaults->expects($this->any()) + ->method('themeExist') + ->willReturn(false); + $defaults->setConfig($config); + + $footer = $defaults->getShortFooter(); + $this->assertNotContains('Privacy Policy', $footer); + $this->assertNotContains('Imprint', $footer); + } + + protected function getDefaultsMock($mockedMethods) { + return $this->getMockBuilder(OC_Defaults::class) + ->setMethods($mockedMethods) + ->getMock(); + } }