From 91648c874e0e556ee343e6860afffcfa82df23a4 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 8 Jul 2020 14:22:11 +0200 Subject: Use the L10N factory to get the users language Signed-off-by: Joas Schilling --- lib/Controller/EndpointController.php | 27 +++------ lib/Push.php | 10 ++-- tests/Unit/Controller/EndpointControllerTest.php | 35 ++++++------ tests/Unit/PushTest.php | 73 ++++++++++-------------- 4 files changed, 62 insertions(+), 83 deletions(-) diff --git a/lib/Controller/EndpointController.php b/lib/Controller/EndpointController.php index f184fd9..f568fde 100644 --- a/lib/Controller/EndpointController.php +++ b/lib/Controller/EndpointController.php @@ -27,10 +27,10 @@ use OCA\Notifications\Push; use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; use OCP\AppFramework\OCSController; -use OCP\IConfig; use OCP\IRequest; use OCP\IUser; use OCP\IUserSession; +use OCP\L10N\IFactory; use OCP\Notification\IAction; use OCP\Notification\IManager; use OCP\Notification\INotification; @@ -40,35 +40,25 @@ class EndpointController extends OCSController { private $handler; /** @var IManager */ private $manager; - /** @var IConfig */ - private $config; + /** @var IFactory */ + private $l10nFactory; /** @var IUserSession */ private $session; /** @var Push */ private $push; - - /** - * @param string $appName - * @param IRequest $request - * @param Handler $handler - * @param IManager $manager - * @param IConfig $config - * @param IUserSession $session - * @param Push $push - */ public function __construct(string $appName, IRequest $request, Handler $handler, IManager $manager, - IConfig $config, + IFactory $l10nFactory, IUserSession $session, Push $push) { parent::__construct($appName, $request); $this->handler = $handler; $this->manager = $manager; - $this->config = $config; + $this->l10nFactory = $l10nFactory; $this->session = $session; $this->push = $push; } @@ -89,9 +79,7 @@ class EndpointController extends OCSController { $filter = $this->manager->createNotification(); $filter->setUser($this->getCurrentUser()); - $language = $this->config->getUserValue($this->getCurrentUser(), 'core', 'lang', null); - $language = $language ?? $this->config->getSystemValue('default_language', 'en'); - + $language = $this->l10nFactory->getUserLanguage($this->session->getUser()); $notifications = $this->handler->get($filter); $data = []; @@ -140,8 +128,7 @@ class EndpointController extends OCSController { return new DataResponse(null, Http::STATUS_NOT_FOUND); } - $language = $this->config->getUserValue($this->getCurrentUser(), 'core', 'lang', null); - $language = $language ?? $this->config->getSystemValue('default_language', 'en'); + $language = $this->l10nFactory->getUserLanguage($this->session->getUser()); try { $notification = $this->manager->prepare($notification, $language); diff --git a/lib/Push.php b/lib/Push.php index 8e7c00e..717cf24 100644 --- a/lib/Push.php +++ b/lib/Push.php @@ -36,6 +36,7 @@ use OCP\IDBConnection; use OCP\ILogger; use OCP\IUser; use OCP\IUserManager; +use OCP\L10N\IFactory; use OCP\Notification\IManager as INotificationManager; use OCP\Notification\INotification; use Symfony\Component\Console\Output\OutputInterface; @@ -57,6 +58,8 @@ class Push { protected $clientService; /** @var ICache */ protected $cache; + /** @var IFactory */ + protected $l10nFactory; /** @var ILogger */ protected $log; /** @var OutputInterface */ @@ -70,6 +73,7 @@ class Push { IUserManager $userManager, IClientService $clientService, ICacheFactory $cacheFactory, + IFactory $l10nFactory, ILogger $log) { $this->db = $connection; $this->notificationManager = $notificationManager; @@ -79,6 +83,7 @@ class Push { $this->userManager = $userManager; $this->clientService = $clientService; $this->cache = $cacheFactory->createDistributed('pushtokens'); + $this->l10nFactory = $l10nFactory; $this->log = $log; } @@ -107,10 +112,7 @@ class Push { $this->printInfo('Trying to push to ' . count($devices) . ' devices'); $this->printInfo(''); - $language = $this->config->getSystemValue('force_language', false); - $language = \is_string($language) ? $language : $this->config->getUserValue($notification->getUser(), 'core', 'lang', null); - $language = $language ?? $this->config->getSystemValue('default_language', 'en'); - + $language = $this->l10nFactory->getUserLanguage($user); $this->printInfo('Language is set to ' . $language); try { diff --git a/tests/Unit/Controller/EndpointControllerTest.php b/tests/Unit/Controller/EndpointControllerTest.php index 6b29114..b1ae0d1 100644 --- a/tests/Unit/Controller/EndpointControllerTest.php +++ b/tests/Unit/Controller/EndpointControllerTest.php @@ -33,6 +33,7 @@ use OCP\IConfig; use OCP\IRequest; use OCP\IUser; use OCP\IUserSession; +use OCP\L10N\IFactory; use OCP\Notification\IAction; use OCP\Notification\IManager; use OCP\Notification\INotification; @@ -49,8 +50,8 @@ class EndpointControllerTest extends TestCase { /** @var IManager|MockObject */ protected $manager; - /** @var IConfig|MockObject */ - protected $config; + /** @var IFactory|MockObject */ + protected $l10nFactory; /** @var IUserSession|MockObject */ protected $session; @@ -69,7 +70,7 @@ class EndpointControllerTest extends TestCase { $this->request = $this->createMock(IRequest::class); $this->handler = $this->createMock(Handler::class); $this->manager = $this->createMock(IManager::class); - $this->config = $this->createMock(IConfig::class); + $this->l10nFactory = $this->createMock(IFactory::class); $this->session = $this->createMock(IUserSession::class); $this->user = $this->createMock(IUser::class); $this->push = $this->createMock(Push::class); @@ -90,7 +91,7 @@ class EndpointControllerTest extends TestCase { $this->request, $this->handler, $this->manager, - $this->config, + $this->l10nFactory, $this->session, $this->push ); @@ -102,7 +103,7 @@ class EndpointControllerTest extends TestCase { $this->request, $this->handler, $this->manager, - $this->config, + $this->l10nFactory, $this->session, $this->push, ]) @@ -170,9 +171,9 @@ class EndpointControllerTest extends TestCase { ->method('prepare') ->willReturnArgument(0); - $this->config->expects($this->once()) - ->method('getUserValue') - ->with('username', 'core', 'lang', null) + $this->l10nFactory + ->method('getUserLanguage') + ->with($this->user) ->willReturn('en'); $this->handler->expects($this->once()) @@ -240,9 +241,9 @@ class EndpointControllerTest extends TestCase { ->method('prepare') ->willReturnArgument(0); - $this->config->expects($this->once()) - ->method('getUserValue') - ->with('username', 'core', 'lang', null) + $this->l10nFactory + ->method('getUserLanguage') + ->with($this->user) ->willReturn('en'); $this->handler->expects($this->once()) @@ -321,9 +322,9 @@ class EndpointControllerTest extends TestCase { ->with($id, $notification) ->willReturn(['$notification']); - $this->config->expects($this->once()) - ->method('getUserValue') - ->with($username, 'core', 'lang', null) + $this->l10nFactory + ->method('getUserLanguage') + ->with($this->user) ->willReturn('en'); $response = $controller->getNotification($apiVersion, $id); @@ -371,9 +372,9 @@ class EndpointControllerTest extends TestCase { ->method('getById') ->willReturn($notification); - $this->config->expects($this->once()) - ->method('getUserValue') - ->with('username', 'core', 'lang', null) + $this->l10nFactory + ->method('getUserLanguage') + ->with($this->user) ->willReturn('en'); $this->manager->expects($this->once()) diff --git a/tests/Unit/PushTest.php b/tests/Unit/PushTest.php index 57efa14..93100ca 100644 --- a/tests/Unit/PushTest.php +++ b/tests/Unit/PushTest.php @@ -39,6 +39,7 @@ use OCP\IDBConnection; use OCP\ILogger; use OCP\IUser; use OCP\IUserManager; +use OCP\L10N\IFactory; use OCP\Notification\IManager as INotificationManager; use OCP\Notification\INotification; use PHPUnit\Framework\MockObject\MockObject; @@ -68,6 +69,8 @@ class PushTest extends TestCase { protected $cacheFactory; /** @var ICache|MockObject */ protected $cache; + /** @var IFactory|MockObject */ + protected $l10nFactory; /** @var ILogger|MockObject */ protected $logger; @@ -83,6 +86,7 @@ class PushTest extends TestCase { $this->clientService = $this->createMock(IClientService::class); $this->cacheFactory = $this->createMock(ICacheFactory::class); $this->cache = $this->createMock(ICache::class); + $this->l10nFactory = $this->createMock(IFactory::class); $this->logger = $this->createMock(ILogger::class); $this->cacheFactory->method('createDistributed') @@ -106,6 +110,7 @@ class PushTest extends TestCase { $this->userManager, $this->clientService, $this->cacheFactory, + $this->l10nFactory, $this->logger, ]) ->setMethods($methods) @@ -121,6 +126,7 @@ class PushTest extends TestCase { $this->userManager, $this->clientService, $this->cacheFactory, + $this->l10nFactory, $this->logger ); } @@ -134,7 +140,7 @@ class PushTest extends TestCase { /** @var INotification|MockObject$notification */ $notification = $this->createMock(INotification::class); - $notification->expects($this->once()) + $notification ->method('getUser') ->willReturn('invalid'); @@ -155,7 +161,7 @@ class PushTest extends TestCase { /** @var INotification|MockObject $notification */ $notification = $this->createMock(INotification::class); - $notification->expects($this->exactly(2)) + $notification ->method('getUser') ->willReturn('valid'); @@ -183,7 +189,7 @@ class PushTest extends TestCase { /** @var INotification|MockObject $notification */ $notification = $this->createMock(INotification::class); - $notification->expects($this->exactly(3)) + $notification ->method('getUser') ->willReturn('valid'); @@ -202,13 +208,9 @@ class PushTest extends TestCase { 'token' => 'token1', ]]); - $this->config->expects($this->once()) - ->method('getSystemValue') - ->with('force_language', false) - ->willReturn(false); - $this->config->expects($this->once()) - ->method('getUserValue') - ->with('valid', 'core', 'lang', null) + $this->l10nFactory + ->method('getUserLanguage') + ->with($user) ->willReturn('de'); $this->notificationManager->expects($this->once()) @@ -226,7 +228,7 @@ class PushTest extends TestCase { /** @var INotification|MockObject $notification */ $notification = $this->createMock(INotification::class); - $notification->expects($this->exactly(3)) + $notification ->method('getUser') ->willReturn('valid'); @@ -246,13 +248,9 @@ class PushTest extends TestCase { 'apptype' => 'other', ]]); - $this->config->expects($this->once()) - ->method('getSystemValue') - ->with('force_language', false) - ->willReturn(false); - $this->config->expects($this->once()) - ->method('getUserValue') - ->with('valid', 'core', 'lang', null) + $this->l10nFactory + ->method('getUserLanguage') + ->with($user) ->willReturn('ru'); $this->notificationManager->expects($this->once()) @@ -290,7 +288,7 @@ class PushTest extends TestCase { /** @var INotification|MockObject $notification */ $notification = $this->createMock(INotification::class); - $notification->expects($this->exactly(2)) + $notification ->method('getUser') ->willReturn('valid'); @@ -310,19 +308,16 @@ class PushTest extends TestCase { 'apptype' => 'other', ]]); - $this->config->expects($this->once()) - ->method('getSystemValue') - ->with('force_language', false) + $this->l10nFactory + ->method('getUserLanguage') + ->with($user) ->willReturn('ru'); - $this->config->expects($this->never()) - ->method('getUserValue'); $this->notificationManager->expects($this->once()) ->method('prepare') ->with($notification, 'ru') ->willReturnArgument(0); - /** @var Key|MockObject $key */ $key = $this->createMock(Key::class); @@ -362,7 +357,7 @@ class PushTest extends TestCase { /** @var INotification|MockObject $notification */ $notification = $this->createMock(INotification::class); - $notification->expects($this->exactly(3)) + $notification ->method('getUser') ->willReturn('valid'); @@ -404,16 +399,14 @@ class PushTest extends TestCase { ], ]); - $this->config->expects($this->exactly(2)) + $this->config ->method('getSystemValue') - ->willReturnMap([ - ['debug', false, $isDebug], - ['force_language', false, false], - ]); + ->with('debug', false) + ->willReturn($isDebug); - $this->config->expects($this->once()) - ->method('getUserValue') - ->with('valid', 'core', 'lang', null) + $this->l10nFactory + ->method('getUserLanguage') + ->with($user) ->willReturn('ru'); $this->notificationManager->expects($this->once()) @@ -555,7 +548,7 @@ class PushTest extends TestCase { /** @var INotification|MockObject $notification */ $notification = $this->createMock(INotification::class); - $notification->expects($this->exactly(3)) + $notification ->method('getUser') ->willReturn('valid'); @@ -589,13 +582,9 @@ class PushTest extends TestCase { ->method('getDevicesForUser') ->willReturn($devices); - $this->config->expects($this->once()) - ->method('getSystemValue') - ->with('force_language', false) - ->willReturn(false); - $this->config->expects($this->once()) - ->method('getUserValue') - ->with('valid', 'core', 'lang', null) + $this->l10nFactory + ->method('getUserLanguage') + ->with($user) ->willReturn('ru'); $this->notificationManager->expects($this->once()) -- cgit v1.2.3