diff options
12 files changed, 356 insertions, 341 deletions
diff --git a/apps/updatenotification/appinfo/app.php b/apps/updatenotification/appinfo/app.php index fd1739bde55..5002fd7c837 100644 --- a/apps/updatenotification/appinfo/app.php +++ b/apps/updatenotification/appinfo/app.php @@ -1,56 +1,25 @@ <?php /** - * @copyright Copyright (c) 2016, ownCloud, Inc. + * @copyright Copyright (c) 2018, Joas Schilling <coding@schilljs.com> * * @author Joas Schilling <coding@schilljs.com> - * @author Lukas Reschke <lukas@statuscode.ch> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Thomas Müller <thomas.mueller@tmit.eu> * - * @license AGPL-3.0 + * @license GNU AGPL version 3 or any later version * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. * */ -if(\OC::$server->getConfig()->getSystemValue('updatechecker', true) === true) { - $updater = new \OC\Updater\VersionCheck( - \OC::$server->getHTTPClientService(), - \OC::$server->getConfig() - ); - $updateChecker = new \OCA\UpdateNotification\UpdateChecker( - $updater - ); - - $userObject = \OC::$server->getUserSession()->getUser(); - if($userObject !== null) { - if(\OC::$server->getGroupManager()->isAdmin($userObject->getUID()) && - !\OC::$server->getAppManager()->isEnabledForUser('notifications')) { - if($updateChecker->getUpdateState() !== []) { - \OCP\Util::addScript('updatenotification', 'notification'); - OC_Hook::connect('\OCP\Config', 'js', $updateChecker, 'getJavaScript'); - } - } - } - - $manager = \OC::$server->getNotificationManager(); - $manager->registerNotifier(function() use ($manager) { - return \OC::$server->query(\OCA\UpdateNotification\Notification\Notifier::class); - }, function() { - $l = \OC::$server->getL10N('updatenotification'); - return [ - 'id' => 'updatenotification', - 'name' => $l->t('Update notifications'), - ]; - }); -} +$app = new \OCA\UpdateNotification\AppInfo\Application(); +$app->register(); diff --git a/apps/updatenotification/appinfo/info.xml b/apps/updatenotification/appinfo/info.xml index 5dc0889bb9d..14ec98916ab 100644 --- a/apps/updatenotification/appinfo/info.xml +++ b/apps/updatenotification/appinfo/info.xml @@ -5,7 +5,7 @@ <description>Displays update notifications for Nextcloud and provides the SSO for the updater.</description> <licence>AGPL</licence> <author>Lukas Reschke</author> - <version>1.4.0</version> + <version>1.4.1</version> <namespace>UpdateNotification</namespace> <default_enable/> <dependencies> @@ -17,6 +17,6 @@ </background-jobs> <settings> - <admin>OCA\UpdateNotification\Controller\AdminController</admin> + <admin>OCA\UpdateNotification\Settings\Admin</admin> </settings> </info> diff --git a/apps/updatenotification/composer/composer/autoload_classmap.php b/apps/updatenotification/composer/composer/autoload_classmap.php index e1833548bcf..55ad497a858 100644 --- a/apps/updatenotification/composer/composer/autoload_classmap.php +++ b/apps/updatenotification/composer/composer/autoload_classmap.php @@ -11,5 +11,6 @@ return array( 'OCA\\UpdateNotification\\Notification\\BackgroundJob' => $baseDir . '/../lib/Notification/BackgroundJob.php', 'OCA\\UpdateNotification\\Notification\\Notifier' => $baseDir . '/../lib/Notification/Notifier.php', 'OCA\\UpdateNotification\\ResetTokenBackgroundJob' => $baseDir . '/../lib/ResetTokenBackgroundJob.php', + 'OCA\\UpdateNotification\\Settings\\Admin' => $baseDir . '/../lib/Settings/Admin.php', 'OCA\\UpdateNotification\\UpdateChecker' => $baseDir . '/../lib/UpdateChecker.php', ); diff --git a/apps/updatenotification/composer/composer/autoload_static.php b/apps/updatenotification/composer/composer/autoload_static.php index 1c63a2d234b..4a50e180a8f 100644 --- a/apps/updatenotification/composer/composer/autoload_static.php +++ b/apps/updatenotification/composer/composer/autoload_static.php @@ -26,6 +26,7 @@ class ComposerStaticInitUpdateNotification 'OCA\\UpdateNotification\\Notification\\BackgroundJob' => __DIR__ . '/..' . '/../lib/Notification/BackgroundJob.php', 'OCA\\UpdateNotification\\Notification\\Notifier' => __DIR__ . '/..' . '/../lib/Notification/Notifier.php', 'OCA\\UpdateNotification\\ResetTokenBackgroundJob' => __DIR__ . '/..' . '/../lib/ResetTokenBackgroundJob.php', + 'OCA\\UpdateNotification\\Settings\\Admin' => __DIR__ . '/..' . '/../lib/Settings/Admin.php', 'OCA\\UpdateNotification\\UpdateChecker' => __DIR__ . '/..' . '/../lib/UpdateChecker.php', ); diff --git a/apps/updatenotification/lib/AppInfo/Application.php b/apps/updatenotification/lib/AppInfo/Application.php index 5eba176f312..ef6f79e4eb5 100644 --- a/apps/updatenotification/lib/AppInfo/Application.php +++ b/apps/updatenotification/lib/AppInfo/Application.php @@ -1,56 +1,83 @@ <?php /** - * @copyright Copyright (c) 2016, ownCloud, Inc. + * @copyright Copyright (c) 2018, Joas Schilling <coding@schilljs.com> * - * @author Lukas Reschke <lukas@statuscode.ch> - * @author Thomas Müller <thomas.mueller@tmit.eu> + * @author Joas Schilling <coding@schilljs.com> * - * @license AGPL-3.0 + * @license GNU AGPL version 3 or any later version * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. * */ namespace OCA\UpdateNotification\AppInfo; -use OC\AppFramework\Utility\TimeFactory; -use OCA\UpdateNotification\Controller\AdminController; +use OCA\UpdateNotification\Notification\Notifier; use OCA\UpdateNotification\UpdateChecker; use OCP\AppFramework\App; -use OCP\AppFramework\IAppContainer; +use OCP\AppFramework\QueryException; +use OCP\IUser; +use OCP\Util; class Application extends App { - public function __construct (array $urlParams = array()) { - parent::__construct('updatenotification', $urlParams); - $container = $this->getContainer(); - - $container->registerService('AdminController', function(IAppContainer $c) { - $updater = new \OC\Updater\VersionCheck( - \OC::$server->getHTTPClientService(), - \OC::$server->getConfig() - ); - return new AdminController( - $c->query('AppName'), - $c->query('Request'), - $c->getServer()->getJobList(), - $c->getServer()->getSecureRandom(), - $c->getServer()->getConfig(), - new TimeFactory(), - $c->getServer()->getL10N($c->query('AppName')), - new UpdateChecker($updater), - $c->getServer()->getDateTimeFormatter() - ); - }); + public function __construct() { + parent::__construct('updatenotification', []); } + public function register() { + $server = $this->getContainer()->getServer(); + + if ($server->getConfig()->getSystemValue('updatechecker', true) !== true) { + // Updater check is disabled + return; + } + + $user = $server->getUserSession()->getUser(); + if (!$user instanceof IUser) { + // Nothing to do for guests + return; + } + + if ($server->getAppManager()->isEnabledForUser('notifications')) { + // Notifications app is available, so we register. + // Since notifications also work for non-admins we don't check this here. + $this->registerNotifier(); + } else if ($server->getGroupManager()->isAdmin($user->getUID())) { + try { + $updateChecker = $this->getContainer()->query(UpdateChecker::class); + } catch (QueryException $e) { + $server->getLogger()->logException($e); + return; + } + + if ($updateChecker->getUpdateState() !== []) { + Util::addScript('updatenotification', 'notification'); + \OC_Hook::connect('\OCP\Config', 'js', $updateChecker, 'populateJavaScriptVariables'); + } + } + } + + public function registerNotifier() { + $notificationsManager = $this->getContainer()->getServer()->getNotificationManager(); + $notificationsManager->registerNotifier(function() { + return $this->getContainer()->query(Notifier::class); + }, function() { + $l = $this->getContainer()->getServer()->getL10N('updatenotification'); + return [ + 'id' => 'updatenotification', + 'name' => $l->t('Update notifications'), + ]; + }); + } } diff --git a/apps/updatenotification/lib/Controller/AdminController.php b/apps/updatenotification/lib/Controller/AdminController.php index 9d2a5074a4a..ba16f114f61 100644 --- a/apps/updatenotification/lib/Controller/AdminController.php +++ b/apps/updatenotification/lib/Controller/AdminController.php @@ -25,20 +25,18 @@ namespace OCA\UpdateNotification\Controller; -use OCA\UpdateNotification\UpdateChecker; +use OCA\UpdateNotification\ResetTokenBackgroundJob; use OCP\AppFramework\Controller; use OCP\AppFramework\Http\DataResponse; -use OCP\AppFramework\Http\TemplateResponse; use OCP\AppFramework\Utility\ITimeFactory; use OCP\BackgroundJob\IJobList; use OCP\IConfig; -use OCP\IDateTimeFormatter; use OCP\IL10N; use OCP\IRequest; use OCP\Security\ISecureRandom; -use OCP\Settings\ISettings; +use OCP\Util; -class AdminController extends Controller implements ISettings { +class AdminController extends Controller { /** @var IJobList */ private $jobList; /** @var ISecureRandom */ @@ -47,12 +45,8 @@ class AdminController extends Controller implements ISettings { private $config; /** @var ITimeFactory */ private $timeFactory; - /** @var UpdateChecker */ - private $updateChecker; /** @var IL10N */ private $l10n; - /** @var IDateTimeFormatter */ - private $dateTimeFormatter; /** * @param string $appName @@ -62,8 +56,6 @@ class AdminController extends Controller implements ISettings { * @param IConfig $config * @param ITimeFactory $timeFactory * @param IL10N $l10n - * @param UpdateChecker $updateChecker - * @param IDateTimeFormatter $dateTimeFormatter */ public function __construct($appName, IRequest $request, @@ -71,70 +63,21 @@ class AdminController extends Controller implements ISettings { ISecureRandom $secureRandom, IConfig $config, ITimeFactory $timeFactory, - IL10N $l10n, - UpdateChecker $updateChecker, - IDateTimeFormatter $dateTimeFormatter) { + IL10N $l10n) { parent::__construct($appName, $request); $this->jobList = $jobList; $this->secureRandom = $secureRandom; $this->config = $config; $this->timeFactory = $timeFactory; $this->l10n = $l10n; - $this->updateChecker = $updateChecker; - $this->dateTimeFormatter = $dateTimeFormatter; } /** - * @return TemplateResponse - */ - public function displayPanel() { - $lastUpdateCheckTimestamp = $this->config->getAppValue('core', 'lastupdatedat'); - $lastUpdateCheck = $this->dateTimeFormatter->formatDateTime($lastUpdateCheckTimestamp); - - $channels = [ - 'daily', - 'beta', - 'stable', - 'production', - ]; - $currentChannel = \OCP\Util::getChannel(); - - // Remove the currently used channel from the channels list - if(($key = array_search($currentChannel, $channels)) !== false) { - unset($channels[$key]); - } - $updateState = $this->updateChecker->getUpdateState(); - - $notifyGroups = json_decode($this->config->getAppValue('updatenotification', 'notify_groups', '["admin"]'), true); - - $defaultUpdateServerURL = 'https://updates.nextcloud.com/server/'; - $updateServerURL = $this->config->getSystemValue('updater.server.url', $defaultUpdateServerURL); - - $params = [ - 'isNewVersionAvailable' => !empty($updateState['updateAvailable']), - 'isUpdateChecked' => $lastUpdateCheckTimestamp > 0, - 'lastChecked' => $lastUpdateCheck, - 'currentChannel' => $currentChannel, - 'channels' => $channels, - 'newVersionString' => (empty($updateState['updateVersion'])) ? '' : $updateState['updateVersion'], - 'downloadLink' => (empty($updateState['downloadLink'])) ? '' : $updateState['downloadLink'], - 'updaterEnabled' => (empty($updateState['updaterEnabled'])) ? false : $updateState['updaterEnabled'], - 'isDefaultUpdateServerURL' => $updateServerURL === $defaultUpdateServerURL, - 'updateServerURL' => $updateServerURL, - 'notify_groups' => implode('|', $notifyGroups), - ]; - - return new TemplateResponse($this->appName, 'admin', $params, ''); - } - - /** - * @UseSession - * * @param string $channel * @return DataResponse */ public function setChannel($channel) { - \OCP\Util::setChannel($channel); + Util::setChannel($channel); $this->config->setAppValue('core', 'lastupdatedat', 0); return new DataResponse(['status' => 'success', 'data' => ['message' => $this->l10n->t('Channel updated')]]); } @@ -144,7 +87,7 @@ class AdminController extends Controller implements ISettings { */ public function createCredentials() { // Create a new job and store the creation date - $this->jobList->add('OCA\UpdateNotification\ResetTokenBackgroundJob'); + $this->jobList->add(ResetTokenBackgroundJob::class); $this->config->setAppValue('core', 'updater.secret.created', $this->timeFactory->getTime()); // Create a new token @@ -153,29 +96,4 @@ class AdminController extends Controller implements ISettings { return new DataResponse($newToken); } - - /** - * @return TemplateResponse returns the instance with all parameters set, ready to be rendered - */ - public function getForm() { - return $this->displayPanel(); - } - - /** - * @return string the section ID, e.g. 'sharing' - */ - public function getSection() { - return 'server'; - } - - /** - * @return int whether the form should be rather on the top or bottom of - * the admin section. The forms are arranged in ascending order of the - * priority values. It is required to return a value between 0 and 100. - * - * E.g.: 70 - */ - public function getPriority() { - return 1; - } } diff --git a/apps/updatenotification/lib/ResetTokenBackgroundJob.php b/apps/updatenotification/lib/ResetTokenBackgroundJob.php index 93b5a750789..5dd7c4f35f1 100644 --- a/apps/updatenotification/lib/ResetTokenBackgroundJob.php +++ b/apps/updatenotification/lib/ResetTokenBackgroundJob.php @@ -22,7 +22,6 @@ namespace OCA\UpdateNotification; -use OC\AppFramework\Utility\TimeFactory; use OC\BackgroundJob\TimedJob; use OCP\AppFramework\Utility\ITimeFactory; use OCP\IConfig; @@ -40,28 +39,15 @@ class ResetTokenBackgroundJob extends TimedJob { private $timeFactory; /** - * @param IConfig|null $config - * @param ITimeFactory|null $timeFactory + * @param IConfig $config + * @param ITimeFactory $timeFactory */ - public function __construct(IConfig $config = null, - ITimeFactory $timeFactory = null) { + public function __construct(IConfig $config, + ITimeFactory $timeFactory) { // Run all 10 minutes $this->setInterval(60 * 10); - - if ($config instanceof IConfig && $timeFactory instanceof ITimeFactory) { - $this->config = $config; - $this->timeFactory = $timeFactory; - } else { - $this->fixDIForJobs(); - } - } - - /** - * DI for jobs - */ - private function fixDIForJobs() { - $this->config = \OC::$server->getConfig(); - $this->timeFactory = new TimeFactory(); + $this->config = $config; + $this->timeFactory = $timeFactory; } /** diff --git a/apps/updatenotification/lib/Settings/Admin.php b/apps/updatenotification/lib/Settings/Admin.php new file mode 100644 index 00000000000..1df4827d77f --- /dev/null +++ b/apps/updatenotification/lib/Settings/Admin.php @@ -0,0 +1,116 @@ +<?php +/** + * @copyright Copyright (c) 2016, ownCloud, Inc. + * + * @author Arthur Schiwon <blizzz@arthur-schiwon.de> + * @author Joas Schilling <coding@schilljs.com> + * @author Lukas Reschke <lukas@statuscode.ch> + * @author Morris Jobke <hey@morrisjobke.de> + * + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace OCA\UpdateNotification\Settings; + +use OCA\UpdateNotification\UpdateChecker; +use OCP\AppFramework\Http\TemplateResponse; +use OCP\IConfig; +use OCP\IDateTimeFormatter; +use OCP\Settings\ISettings; +use OCP\Util; + +class Admin implements ISettings { + /** @var IConfig */ + private $config; + /** @var UpdateChecker */ + private $updateChecker; + /** @var IDateTimeFormatter */ + private $dateTimeFormatter; + + /** + * @param IConfig $config + * @param UpdateChecker $updateChecker + * @param IDateTimeFormatter $dateTimeFormatter + */ + public function __construct(IConfig $config, + UpdateChecker $updateChecker, + IDateTimeFormatter $dateTimeFormatter) { + $this->config = $config; + $this->updateChecker = $updateChecker; + $this->dateTimeFormatter = $dateTimeFormatter; + } + + /** + * @return TemplateResponse + */ + public function getForm() { + $lastUpdateCheckTimestamp = $this->config->getAppValue('core', 'lastupdatedat'); + $lastUpdateCheck = $this->dateTimeFormatter->formatDateTime($lastUpdateCheckTimestamp); + + $channels = [ + 'daily', + 'beta', + 'stable', + 'production', + ]; + $currentChannel = Util::getChannel(); + + // Remove the currently used channel from the channels list + if(($key = array_search($currentChannel, $channels, true)) !== false) { + unset($channels[$key]); + } + $updateState = $this->updateChecker->getUpdateState(); + + $notifyGroups = json_decode($this->config->getAppValue('updatenotification', 'notify_groups', '["admin"]'), true); + + $defaultUpdateServerURL = 'https://updates.nextcloud.com/server/'; + $updateServerURL = $this->config->getSystemValue('updater.server.url', $defaultUpdateServerURL); + + $params = [ + 'isNewVersionAvailable' => !empty($updateState['updateAvailable']), + 'isUpdateChecked' => $lastUpdateCheckTimestamp > 0, + 'lastChecked' => $lastUpdateCheck, + 'currentChannel' => $currentChannel, + 'channels' => $channels, + 'newVersionString' => empty($updateState['updateVersion']) ? '' : $updateState['updateVersion'], + 'downloadLink' => empty($updateState['downloadLink']) ? '' : $updateState['downloadLink'], + 'updaterEnabled' => empty($updateState['updaterEnabled']) ? false : $updateState['updaterEnabled'], + 'isDefaultUpdateServerURL' => $updateServerURL === $defaultUpdateServerURL, + 'updateServerURL' => $updateServerURL, + 'notify_groups' => implode('|', $notifyGroups), + ]; + + return new TemplateResponse('updatenotification', 'admin', $params, ''); + } + + /** + * @return string the section ID, e.g. 'sharing' + */ + public function getSection() { + return 'server'; + } + + /** + * @return int whether the form should be rather on the top or bottom of + * the admin section. The forms are arranged in ascending order of the + * priority values. It is required to return a value between 0 and 100. + * + * E.g.: 70 + */ + public function getPriority() { + return 1; + } +} diff --git a/apps/updatenotification/lib/UpdateChecker.php b/apps/updatenotification/lib/UpdateChecker.php index ad76de56953..cabdfe8b9ff 100644 --- a/apps/updatenotification/lib/UpdateChecker.php +++ b/apps/updatenotification/lib/UpdateChecker.php @@ -64,7 +64,7 @@ class UpdateChecker { /** * @param array $data */ - public function getJavaScript(array $data) { + public function populateJavaScriptVariables(array $data) { $data['array']['oc_updateState'] = json_encode([ 'updateAvailable' => true, 'updateVersion' => $this->getUpdateState()['updateVersion'], diff --git a/apps/updatenotification/tests/Controller/AdminControllerTest.php b/apps/updatenotification/tests/Controller/AdminControllerTest.php index 75588a1aec5..00d3974ed02 100644 --- a/apps/updatenotification/tests/Controller/AdminControllerTest.php +++ b/apps/updatenotification/tests/Controller/AdminControllerTest.php @@ -25,6 +25,7 @@ namespace OCA\UpdateNotification\Tests\Controller; use OCA\UpdateNotification\Controller\AdminController; +use OCA\UpdateNotification\ResetTokenBackgroundJob; use OCA\UpdateNotification\UpdateChecker; use OCP\AppFramework\Http\DataResponse; use OCP\AppFramework\Http\TemplateResponse; @@ -35,40 +36,34 @@ use OCP\IDateTimeFormatter; use OCP\IL10N; use OCP\IRequest; use OCP\Security\ISecureRandom; +use OCP\Util; use Test\TestCase; class AdminControllerTest extends TestCase { - /** @var IRequest */ + /** @var IRequest|\PHPUnit_Framework_MockObject_MockObject */ private $request; - /** @var IJobList */ + /** @var IJobList|\PHPUnit_Framework_MockObject_MockObject */ private $jobList; - /** @var ISecureRandom */ + /** @var ISecureRandom|\PHPUnit_Framework_MockObject_MockObject */ private $secureRandom; - /** @var IConfig */ + /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */ private $config; /** @var AdminController */ private $adminController; - /** @var ITimeFactory */ + /** @var ITimeFactory|\PHPUnit_Framework_MockObject_MockObject */ private $timeFactory; - /** @var IL10N */ + /** @var IL10N|\PHPUnit_Framework_MockObject_MockObject */ private $l10n; - /** @var UpdateChecker */ - private $updateChecker; - /** @var IDateTimeFormatter */ - private $dateTimeFormatter; public function setUp() { parent::setUp(); - $this->request = $this->getMockBuilder('\\OCP\\IRequest')->getMock(); - $this->jobList = $this->getMockBuilder('\\OCP\\BackgroundJob\\IJobList')->getMock(); - $this->secureRandom = $this->getMockBuilder('\\OCP\\Security\\ISecureRandom')->getMock(); - $this->config = $this->getMockBuilder('\\OCP\\IConfig')->getMock(); - $this->timeFactory = $this->getMockBuilder('\\OCP\\AppFramework\\Utility\\ITimeFactory')->getMock(); - $this->l10n = $this->getMockBuilder('\\OCP\\IL10N')->getMock(); - $this->updateChecker = $this->getMockBuilder('\\OCA\\UpdateNotification\\UpdateChecker') - ->disableOriginalConstructor()->getMock(); - $this->dateTimeFormatter = $this->getMockBuilder('\\OCP\\IDateTimeFormatter')->getMock(); + $this->request = $this->createMock(IRequest::class); + $this->jobList = $this->createMock(IJobList::class); + $this->secureRandom = $this->createMock(ISecureRandom::class); + $this->config = $this->createMock(IConfig::class); + $this->timeFactory = $this->createMock(ITimeFactory::class); + $this->l10n = $this->createMock(IL10N::class); $this->adminController = new AdminController( 'updatenotification', @@ -77,131 +72,15 @@ class AdminControllerTest extends TestCase { $this->secureRandom, $this->config, $this->timeFactory, - $this->l10n, - $this->updateChecker, - $this->dateTimeFormatter + $this->l10n ); } - public function testDisplayPanelWithUpdate() { - $channels = [ - 'daily', - 'beta', - 'stable', - 'production', - ]; - $currentChannel = \OCP\Util::getChannel(); - - // Remove the currently used channel from the channels list - if(($key = array_search($currentChannel, $channels)) !== false) { - unset($channels[$key]); - } - - $this->config - ->expects($this->exactly(2)) - ->method('getAppValue') - ->willReturnMap([ - ['core', 'lastupdatedat', '', '12345'], - ['updatenotification', 'notify_groups', '["admin"]', '["admin"]'], - ]); - $this->config - ->expects($this->once()) - ->method('getSystemValue') - ->with('updater.server.url', 'https://updates.nextcloud.com/server/') - ->willReturn('https://updates.nextcloud.com/server/'); - $this->dateTimeFormatter - ->expects($this->once()) - ->method('formatDateTime') - ->with('12345') - ->willReturn('LastCheckedReturnValue'); - $this->updateChecker - ->expects($this->once()) - ->method('getUpdateState') - ->willReturn([ - 'updateAvailable' => true, - 'updateVersion' => '8.1.2', - 'downloadLink' => 'https://downloads.nextcloud.org/server', - 'updaterEnabled' => true, - ]); - - $params = [ - 'isNewVersionAvailable' => true, - 'isUpdateChecked' => true, - 'lastChecked' => 'LastCheckedReturnValue', - 'currentChannel' => \OCP\Util::getChannel(), - 'channels' => $channels, - 'newVersionString' => '8.1.2', - 'downloadLink' => 'https://downloads.nextcloud.org/server', - 'updaterEnabled' => true, - 'isDefaultUpdateServerURL' => true, - 'updateServerURL' => 'https://updates.nextcloud.com/server/', - 'notify_groups' => 'admin', - ]; - - $expected = new TemplateResponse('updatenotification', 'admin', $params, ''); - $this->assertEquals($expected, $this->adminController->displayPanel()); - } - - public function testDisplayPanelWithoutUpdate() { - $channels = [ - 'daily', - 'beta', - 'stable', - 'production', - ]; - $currentChannel = \OCP\Util::getChannel(); - - // Remove the currently used channel from the channels list - if(($key = array_search($currentChannel, $channels)) !== false) { - unset($channels[$key]); - } - - $this->config - ->expects($this->exactly(2)) - ->method('getAppValue') - ->willReturnMap([ - ['core', 'lastupdatedat', '', '12345'], - ['updatenotification', 'notify_groups', '["admin"]', '["admin"]'], - ]); - $this->config - ->expects($this->once()) - ->method('getSystemValue') - ->with('updater.server.url', 'https://updates.nextcloud.com/server/') - ->willReturn('https://updates.nextcloud.com/server/'); - $this->dateTimeFormatter - ->expects($this->once()) - ->method('formatDateTime') - ->with('12345') - ->willReturn('LastCheckedReturnValue'); - $this->updateChecker - ->expects($this->once()) - ->method('getUpdateState') - ->willReturn([]); - - $params = [ - 'isNewVersionAvailable' => false, - 'isUpdateChecked' => true, - 'lastChecked' => 'LastCheckedReturnValue', - 'currentChannel' => \OCP\Util::getChannel(), - 'channels' => $channels, - 'newVersionString' => '', - 'downloadLink' => '', - 'updaterEnabled' => 0, - 'isDefaultUpdateServerURL' => true, - 'updateServerURL' => 'https://updates.nextcloud.com/server/', - 'notify_groups' => 'admin', - ]; - - $expected = new TemplateResponse('updatenotification', 'admin', $params, ''); - $this->assertEquals($expected, $this->adminController->displayPanel()); - } - - public function testCreateCredentials() { $this->jobList ->expects($this->once()) ->method('add') - ->with('OCA\UpdateNotification\ResetTokenBackgroundJob'); + ->with(ResetTokenBackgroundJob::class); $this->secureRandom ->expects($this->once()) ->method('generate') @@ -224,11 +103,4 @@ class AdminControllerTest extends TestCase { $this->assertEquals($expected, $this->adminController->createCredentials()); } - public function testGetSection() { - $this->assertSame('server', $this->adminController->getSection()); - } - - public function testGetPriority() { - $this->assertSame(1, $this->adminController->getPriority()); - } } diff --git a/apps/updatenotification/tests/ResetTokenBackgroundJobTest.php b/apps/updatenotification/tests/ResetTokenBackgroundJobTest.php index 4309aed84bf..bd6223bab1d 100644 --- a/apps/updatenotification/tests/ResetTokenBackgroundJobTest.php +++ b/apps/updatenotification/tests/ResetTokenBackgroundJobTest.php @@ -29,23 +29,23 @@ use OCP\IConfig; use Test\TestCase; class ResetTokenBackgroundJobTest extends TestCase { - /** @var IConfig */ + /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */ private $config; + /** @var ITimeFactory|\PHPUnit_Framework_MockObject_MockObject */ + private $timeFactory; /** @var ResetTokenBackgroundJob */ private $resetTokenBackgroundJob; - /** @var ITimeFactory */ - private $timeFactory; public function setUp() { parent::setUp(); - $this->config = $this->getMockBuilder('\\OCP\\IConfig')->getMock(); - $this->timeFactory = $this->getMockBuilder('\\OCP\\AppFramework\\Utility\\ITimeFactory')->getMock(); + $this->config = $this->createMock(IConfig::class); + $this->timeFactory = $this->createMock(ITimeFactory::class); $this->resetTokenBackgroundJob = new ResetTokenBackgroundJob($this->config, $this->timeFactory); } public function testRunWithNotExpiredToken() { $this->timeFactory - ->expects($this->any()) + ->expects($this->atLeastOnce()) ->method('getTime') ->willReturn(123); $this->config @@ -54,10 +54,9 @@ class ResetTokenBackgroundJobTest extends TestCase { ->with('core', 'updater.secret.created', 123); $this->config ->expects($this->never()) - ->method('deleteSystemValue') - ->with('updater.secret'); + ->method('deleteSystemValue'); - $this->invokePrivate($this->resetTokenBackgroundJob, 'run', ['']); + static::invokePrivate($this->resetTokenBackgroundJob, 'run', [null]); } public function testRunWithExpiredToken() { @@ -78,6 +77,6 @@ class ResetTokenBackgroundJobTest extends TestCase { ->method('deleteSystemValue') ->with('updater.secret'); - $this->invokePrivate($this->resetTokenBackgroundJob, 'run', ['']); + static::invokePrivate($this->resetTokenBackgroundJob, 'run', [null]); } } diff --git a/apps/updatenotification/tests/Settings/AdminTest.php b/apps/updatenotification/tests/Settings/AdminTest.php new file mode 100644 index 00000000000..f49fdf378b8 --- /dev/null +++ b/apps/updatenotification/tests/Settings/AdminTest.php @@ -0,0 +1,126 @@ +<?php +/** + * @copyright Copyright (c) 2016, ownCloud, Inc. + * + * @author Joas Schilling <coding@schilljs.com> + * @author Lukas Reschke <lukas@statuscode.ch> + * @author Morris Jobke <hey@morrisjobke.de> + * + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace OCA\UpdateNotification\Tests\Settings; + +use OCA\UpdateNotification\Settings\Admin; +use OCA\UpdateNotification\UpdateChecker; +use OCP\AppFramework\Http\TemplateResponse; +use OCP\IConfig; +use OCP\IDateTimeFormatter; +use OCP\Util; +use Test\TestCase; + +class AdminTest extends TestCase { + /** @var Admin */ + private $admin; + /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */ + private $config; + /** @var UpdateChecker|\PHPUnit_Framework_MockObject_MockObject */ + private $updateChecker; + /** @var IDateTimeFormatter|\PHPUnit_Framework_MockObject_MockObject */ + private $dateTimeFormatter; + + public function setUp() { + parent::setUp(); + + $this->config = $this->createMock(IConfig::class); + $this->updateChecker = $this->createMock(UpdateChecker::class); + $this->dateTimeFormatter = $this->createMock(IDateTimeFormatter::class); + + $this->admin = new Admin( + $this->config, + $this->updateChecker, + $this->dateTimeFormatter + ); + } + + public function testGetFormWithUpdate() { + $channels = [ + 'daily', + 'beta', + 'stable', + 'production', + ]; + $currentChannel = Util::getChannel(); + + // Remove the currently used channel from the channels list + if(($key = array_search($currentChannel, $channels, true)) !== false) { + unset($channels[$key]); + } + + $this->config + ->expects($this->exactly(2)) + ->method('getAppValue') + ->willReturnMap([ + ['core', 'lastupdatedat', '', '12345'], + ['updatenotification', 'notify_groups', '["admin"]', '["admin"]'], + ]); + $this->config + ->expects($this->once()) + ->method('getSystemValue') + ->with('updater.server.url', 'https://updates.nextcloud.com/server/') + ->willReturn('https://updates.nextcloud.com/server/'); + $this->dateTimeFormatter + ->expects($this->once()) + ->method('formatDateTime') + ->with('12345') + ->willReturn('LastCheckedReturnValue'); + $this->updateChecker + ->expects($this->once()) + ->method('getUpdateState') + ->willReturn([ + 'updateAvailable' => true, + 'updateVersion' => '8.1.2', + 'downloadLink' => 'https://downloads.nextcloud.org/server', + 'updaterEnabled' => true, + ]); + + $params = [ + 'isNewVersionAvailable' => true, + 'isUpdateChecked' => true, + 'lastChecked' => 'LastCheckedReturnValue', + 'currentChannel' => Util::getChannel(), + 'channels' => $channels, + 'newVersionString' => '8.1.2', + 'downloadLink' => 'https://downloads.nextcloud.org/server', + 'updaterEnabled' => true, + 'isDefaultUpdateServerURL' => true, + 'updateServerURL' => 'https://updates.nextcloud.com/server/', + 'notify_groups' => 'admin', + ]; + + $expected = new TemplateResponse('updatenotification', 'admin', $params, ''); + $this->assertEquals($expected, $this->admin->getForm()); + } + + + public function testGetSection() { + $this->assertSame('server', $this->admin->getSection()); + } + + public function testGetPriority() { + $this->assertSame(1, $this->admin->getPriority()); + } +} |