Automatic DI for Controllers also works

Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
Joas Schilling 2018-01-11 11:38:26 +01:00
parent ffb3a3e33a
commit 50e1cee5c7
No known key found for this signature in database
GPG Key ID: 7076EA9751AACDDA
4 changed files with 48 additions and 78 deletions

View File

@ -44,7 +44,7 @@ if(\OC::$server->getConfig()->getSystemValue('updatechecker', true) === true) {
}
$manager = \OC::$server->getNotificationManager();
$manager->registerNotifier(function() use ($manager) {
$manager->registerNotifier(function() {
return \OC::$server->query(\OCA\UpdateNotification\Notification\Notifier::class);
}, function() {
$l = \OC::$server->getL10N('updatenotification');

View File

@ -1,56 +1,32 @@
<?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
* 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\UpdateChecker;
use OCP\AppFramework\App;
use OCP\AppFramework\IAppContainer;
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', []);
}
}

View File

@ -25,6 +25,7 @@
namespace OCA\UpdateNotification\Controller;
use OCA\UpdateNotification\ResetTokenBackgroundJob;
use OCA\UpdateNotification\UpdateChecker;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\DataResponse;
@ -37,6 +38,7 @@ use OCP\IL10N;
use OCP\IRequest;
use OCP\Security\ISecureRandom;
use OCP\Settings\ISettings;
use OCP\Util;
class AdminController extends Controller implements ISettings {
/** @var IJobList */
@ -87,7 +89,7 @@ class AdminController extends Controller implements ISettings {
/**
* @return TemplateResponse
*/
public function displayPanel() {
public function getForm() {
$lastUpdateCheckTimestamp = $this->config->getAppValue('core', 'lastupdatedat');
$lastUpdateCheck = $this->dateTimeFormatter->formatDateTime($lastUpdateCheckTimestamp);
@ -97,7 +99,7 @@ class AdminController extends Controller implements ISettings {
'stable',
'production',
];
$currentChannel = \OCP\Util::getChannel();
$currentChannel = Util::getChannel();
// Remove the currently used channel from the channels list
if(($key = array_search($currentChannel, $channels)) !== false) {
@ -128,13 +130,11 @@ class AdminController extends Controller implements ISettings {
}
/**
* @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 +144,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
@ -154,13 +154,6 @@ 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'
*/

View File

@ -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,40 @@ 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 */
/** @var UpdateChecker|\PHPUnit_Framework_MockObject_MockObject */
private $updateChecker;
/** @var IDateTimeFormatter */
/** @var IDateTimeFormatter|\PHPUnit_Framework_MockObject_MockObject */
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->updateChecker = $this->createMock(UpdateChecker::class);
$this->dateTimeFormatter = $this->createMock(IDateTimeFormatter::class);
$this->adminController = new AdminController(
'updatenotification',
@ -83,17 +84,17 @@ class AdminControllerTest extends TestCase {
);
}
public function testDisplayPanelWithUpdate() {
public function testGetFormWithUpdate() {
$channels = [
'daily',
'beta',
'stable',
'production',
];
$currentChannel = \OCP\Util::getChannel();
$currentChannel = Util::getChannel();
// Remove the currently used channel from the channels list
if(($key = array_search($currentChannel, $channels)) !== false) {
if(($key = array_search($currentChannel, $channels, true)) !== false) {
unset($channels[$key]);
}
@ -128,7 +129,7 @@ class AdminControllerTest extends TestCase {
'isNewVersionAvailable' => true,
'isUpdateChecked' => true,
'lastChecked' => 'LastCheckedReturnValue',
'currentChannel' => \OCP\Util::getChannel(),
'currentChannel' => Util::getChannel(),
'channels' => $channels,
'newVersionString' => '8.1.2',
'downloadLink' => 'https://downloads.nextcloud.org/server',
@ -139,20 +140,20 @@ class AdminControllerTest extends TestCase {
];
$expected = new TemplateResponse('updatenotification', 'admin', $params, '');
$this->assertEquals($expected, $this->adminController->displayPanel());
$this->assertEquals($expected, $this->adminController->getForm());
}
public function testDisplayPanelWithoutUpdate() {
public function testGetFormWithoutUpdate() {
$channels = [
'daily',
'beta',
'stable',
'production',
];
$currentChannel = \OCP\Util::getChannel();
$currentChannel = Util::getChannel();
// Remove the currently used channel from the channels list
if(($key = array_search($currentChannel, $channels)) !== false) {
if(($key = array_search($currentChannel, $channels, true)) !== false) {
unset($channels[$key]);
}
@ -182,7 +183,7 @@ class AdminControllerTest extends TestCase {
'isNewVersionAvailable' => false,
'isUpdateChecked' => true,
'lastChecked' => 'LastCheckedReturnValue',
'currentChannel' => \OCP\Util::getChannel(),
'currentChannel' => Util::getChannel(),
'channels' => $channels,
'newVersionString' => '',
'downloadLink' => '',
@ -193,7 +194,7 @@ class AdminControllerTest extends TestCase {
];
$expected = new TemplateResponse('updatenotification', 'admin', $params, '');
$this->assertEquals($expected, $this->adminController->displayPanel());
$this->assertEquals($expected, $this->adminController->getForm());
}
@ -201,7 +202,7 @@ class AdminControllerTest extends TestCase {
$this->jobList
->expects($this->once())
->method('add')
->with('OCA\UpdateNotification\ResetTokenBackgroundJob');
->with(ResetTokenBackgroundJob::class);
$this->secureRandom
->expects($this->once())
->method('generate')