diff --git a/lib/private/Authentication/Login/SetUserTimezoneCommand.php b/lib/private/Authentication/Login/SetUserTimezoneCommand.php index 90bc444ae7d..ea80fbfc714 100644 --- a/lib/private/Authentication/Login/SetUserTimezoneCommand.php +++ b/lib/private/Authentication/Login/SetUserTimezoneCommand.php @@ -8,6 +8,8 @@ declare(strict_types=1); */ namespace OC\Authentication\Login; +use OC\Core\AppInfo\Application; +use OC\Core\AppInfo\ConfigLexicon; use OCP\IConfig; use OCP\ISession; @@ -26,12 +28,10 @@ class SetUserTimezoneCommand extends ALoginCommand { public function process(LoginData $loginData): LoginResult { if ($loginData->getTimeZoneOffset() !== '' && $this->isValidTimezone($loginData->getTimeZone())) { - $this->config->setUserValue( - $loginData->getUser()->getUID(), - 'core', - 'timezone', - $loginData->getTimeZone() - ); + $userId = $loginData->getUser()->getUID(); + if ($this->config->getUserValue($userId, Application::APP_ID, ConfigLexicon::USER_TIMEZONE, '') === '') { + $this->config->setUserValue($userId, Application::APP_ID, ConfigLexicon::USER_TIMEZONE, $loginData->getTimeZone()); + } $this->session->set( 'timezone', $loginData->getTimeZoneOffset() diff --git a/tests/lib/Authentication/Login/SetUserTimezoneCommandTest.php b/tests/lib/Authentication/Login/SetUserTimezoneCommandTest.php index fb8240c4b1e..473d14daddf 100644 --- a/tests/lib/Authentication/Login/SetUserTimezoneCommandTest.php +++ b/tests/lib/Authentication/Login/SetUserTimezoneCommandTest.php @@ -15,11 +15,10 @@ use OCP\ISession; use PHPUnit\Framework\MockObject\MockObject; class SetUserTimezoneCommandTest extends ALoginTestCommand { - /** @var IConfig|MockObject */ - private $config; - /** @var ISession|MockObject */ - private $session; + private IConfig&MockObject $config; + + private ISession&MockObject $session; protected function setUp(): void { parent::setUp(); @@ -50,6 +49,15 @@ class SetUserTimezoneCommandTest extends ALoginTestCommand { $this->user->expects($this->once()) ->method('getUID') ->willReturn($this->username); + $this->config->expects($this->once()) + ->method('getUserValue') + ->with( + $this->username, + 'core', + 'timezone', + '' + ) + ->willReturn(''); $this->config->expects($this->once()) ->method('setUserValue') ->with( @@ -69,4 +77,32 @@ class SetUserTimezoneCommandTest extends ALoginTestCommand { $this->assertTrue($result->isSuccess()); } + + public function testProcessAlreadySet(): void { + $data = $this->getLoggedInLoginDataWithTimezone(); + $this->user->expects($this->once()) + ->method('getUID') + ->willReturn($this->username); + $this->config->expects($this->once()) + ->method('getUserValue') + ->with( + $this->username, + 'core', + 'timezone', + '', + ) + ->willReturn('Europe/Berlin'); + $this->config->expects($this->never()) + ->method('setUserValue'); + $this->session->expects($this->once()) + ->method('set') + ->with( + 'timezone', + $this->timeZoneOffset + ); + + $result = $this->cmd->process($data); + + $this->assertTrue($result->isSuccess()); + } }