Inject config

This commit is contained in:
Victor Dubiniuk 2014-12-10 01:13:38 +03:00
parent 81d5712419
commit c9fd3c9d29
5 changed files with 46 additions and 34 deletions

View File

@ -10,7 +10,8 @@ if (OC::checkUpgrade(false)) {
$l = new \OC_L10N('core'); $l = new \OC_L10N('core');
$eventSource = \OC::$server->createEventSource(); $eventSource = \OC::$server->createEventSource();
$updater = new \OC\Updater( $updater = new \OC\Updater(
\OC::$server->getHTTPHelper(), \OC::$server->getHTTPHelper(),
\OC::$server->getAppConfig(),
\OC_Log::$object \OC_Log::$object
); );
$updater->listen('\OC\Updater', 'maintenanceStart', function () use ($eventSource, $l) { $updater->listen('\OC\Updater', 'maintenanceStart', function () use ($eventSource, $l) {

View File

@ -84,7 +84,7 @@ class Upgrade extends Command {
if(\OC::checkUpgrade(false)) { if(\OC::checkUpgrade(false)) {
$self = $this; $self = $this;
$updater = new Updater(\OC::$server->getHTTPHelper()); $updater = new Updater(\OC::$server->getHTTPHelper(), \OC::$server->getAppConfig());
$updater->setSimulateStepEnabled($simulateStepEnabled); $updater->setSimulateStepEnabled($simulateStepEnabled);
$updater->setUpdateStepEnabled($updateStepEnabled); $updater->setUpdateStepEnabled($updateStepEnabled);

View File

@ -44,7 +44,7 @@ class OC_TemplateLayout extends OC_Template {
// Update notification // Update notification
if($this->config->getSystemValue('updatechecker', true) === true && if($this->config->getSystemValue('updatechecker', true) === true &&
OC_User::isAdminUser(OC_User::getUser())) { OC_User::isAdminUser(OC_User::getUser())) {
$updater = new \OC\Updater(\OC::$server->getHTTPHelper()); $updater = new \OC\Updater(\OC::$server->getHTTPHelper(), \OC::$server->getAppConfig());
$data = $updater->check(); $data = $updater->check();
if(isset($data['version']) && $data['version'] != '' and $data['version'] !== Array()) { if(isset($data['version']) && $data['version'] != '' and $data['version'] !== Array()) {

View File

@ -30,6 +30,11 @@ class Updater extends BasicEmitter {
* @var \OC\HTTPHelper $helper; * @var \OC\HTTPHelper $helper;
*/ */
private $httpHelper; private $httpHelper;
/**
* @var \OCP\IAppConfig;
*/
private $config;
private $simulateStepEnabled; private $simulateStepEnabled;
@ -38,9 +43,10 @@ class Updater extends BasicEmitter {
/** /**
* @param \OC\Log $log * @param \OC\Log $log
*/ */
public function __construct($httpHelper, $log = null) { public function __construct($httpHelper, $config, $log = null) {
$this->httpHelper = $httpHelper; $this->httpHelper = $httpHelper;
$this->log = $log; $this->log = $log;
$this->config = $config;
$this->simulateStepEnabled = true; $this->simulateStepEnabled = true;
$this->updateStepEnabled = true; $this->updateStepEnabled = true;
} }
@ -75,23 +81,23 @@ class Updater extends BasicEmitter {
public function check($updaterUrl = null) { public function check($updaterUrl = null) {
// Look up the cache - it is invalidated all 30 minutes // Look up the cache - it is invalidated all 30 minutes
if ((\OC_Appconfig::getValue('core', 'lastupdatedat') + 1800) > time()) { if (($this->config->getValue('core', 'lastupdatedat') + 1800) > time()) {
return json_decode(\OC_Appconfig::getValue('core', 'lastupdateResult'), true); return json_decode($this->config->getValue('core', 'lastupdateResult'), true);
} }
if (is_null($updaterUrl)) { if (is_null($updaterUrl)) {
$updaterUrl = 'https://apps.owncloud.com/updater.php'; $updaterUrl = 'https://apps.owncloud.com/updater.php';
} }
\OC_Appconfig::setValue('core', 'lastupdatedat', time()); $this->config->setValue('core', 'lastupdatedat', time());
if (\OC_Appconfig::getValue('core', 'installedat', '') == '') { if ($this->config->getValue('core', 'installedat', '') == '') {
\OC_Appconfig::setValue('core', 'installedat', microtime(true)); $this->config->setValue('core', 'installedat', microtime(true));
} }
$version = \OC_Util::getVersion(); $version = \OC_Util::getVersion();
$version['installed'] = \OC_Appconfig::getValue('core', 'installedat'); $version['installed'] = $this->config->getValue('core', 'installedat');
$version['updated'] = \OC_Appconfig::getValue('core', 'lastupdatedat'); $version['updated'] = $this->config->getValue('core', 'lastupdatedat');
$version['updatechannel'] = \OC_Util::getChannel(); $version['updatechannel'] = \OC_Util::getChannel();
$version['edition'] = \OC_Util::getEditionString(); $version['edition'] = \OC_Util::getEditionString();
$version['build'] = \OC_Util::getBuild(); $version['build'] = \OC_Util::getBuild();
@ -119,7 +125,7 @@ class Updater extends BasicEmitter {
} }
// Cache the result // Cache the result
\OC_Appconfig::setValue('core', 'lastupdateResult', json_encode($data)); $this->config->setValue('core', 'lastupdateResult', json_encode($data));
return $tmp; return $tmp;
} }
@ -219,7 +225,7 @@ class Updater extends BasicEmitter {
$repair->run(); $repair->run();
//Invalidate update feed //Invalidate update feed
\OC_Appconfig::setValue('core', 'lastupdatedat', 0); $this->config->setValue('core', 'lastupdatedat', 0);
// only set the final version if everything went well // only set the final version if everything went well
\OC_Config::setValue('version', implode('.', \OC_Util::getVersion())); \OC_Config::setValue('version', implode('.', \OC_Util::getVersion()));

View File

@ -27,13 +27,28 @@ class UpdaterTest extends \Test\TestCase {
* @dataProvider versionCompatibilityTestData * @dataProvider versionCompatibilityTestData
*/ */
public function testIsUpgradePossible($oldVersion, $newVersion, $result) { public function testIsUpgradePossible($oldVersion, $newVersion, $result) {
$updater = new Updater(\OC::$server->getHTTPHelper()); $updater = new Updater(\OC::$server->getHTTPHelper(), \OC::$server->getConfig());
$this->assertSame($result, $updater->isUpgradePossible($oldVersion, $newVersion)); $this->assertSame($result, $updater->isUpgradePossible($oldVersion, $newVersion));
} }
public function testBrokenXmlResponse(){
$invalidUpdater = $this->getUpdaterMock('OMG!');
$invalidResult = $invalidUpdater->check();
$this->assertEmpty($invalidResult);
}
public function testCheck(){ public function testEmptyResponse(){
// Valid XML. Empty values $emptyUpdater = $this->getUpdaterMock('');
$emptyResult = $emptyUpdater->check();
$this->assertEmpty($emptyResult);
// Error while fetching new contents e.g. too many redirects
$falseUpdater = $this->getUpdaterMock(false);
$falseResult = $falseUpdater->check();
$this->assertEmpty($falseResult);
}
public function testValidEmptyXmlResponse(){
$updater = $this->getUpdaterMock( $updater = $this->getUpdaterMock(
'<?xml version="1.0"?><owncloud><version></version><versionstring></versionstring><url></url><web></web></owncloud>' '<?xml version="1.0"?><owncloud><version></version><versionstring></versionstring><url></url><web></web></owncloud>'
); );
@ -47,18 +62,9 @@ class UpdaterTest extends \Test\TestCase {
$this->assertEmpty($result['versionstring']); $this->assertEmpty($result['versionstring']);
$this->assertEmpty($result['url']); $this->assertEmpty($result['url']);
$this->assertEmpty($result['web']); $this->assertEmpty($result['web']);
}
// Empty feed
$emptyUpdater = $this->getUpdaterMock(''); public function testValidUpdateResponse(){
$emptyResult = $emptyUpdater->check();
$this->assertEmpty($emptyResult);
// Error while fetching new contents e.g. too many redirects
$falseUpdater = $this->getUpdaterMock(false);
$falseResult = $falseUpdater->check();
$this->assertEmpty($falseResult);
// Valid XML. New version available
$newUpdater = $this->getUpdaterMock( $newUpdater = $this->getUpdaterMock(
'<?xml version="1.0"?> '<?xml version="1.0"?>
<owncloud> <owncloud>
@ -78,15 +84,14 @@ class UpdaterTest extends \Test\TestCase {
$this->assertEquals('ownCloud 7.0.3', $newResult['versionstring']); $this->assertEquals('ownCloud 7.0.3', $newResult['versionstring']);
$this->assertEquals('http://download.owncloud.org/community/owncloud-7.0.3.zip', $newResult['url']); $this->assertEquals('http://download.owncloud.org/community/owncloud-7.0.3.zip', $newResult['url']);
$this->assertEquals('http://owncloud.org/', $newResult['web']); $this->assertEquals('http://owncloud.org/', $newResult['web']);
// Invalid XML
$invalidUpdater = $this->getUpdaterMock('OMG!');
$invalidResult = $invalidUpdater->check();
$this->assertEmpty($invalidResult);
} }
protected function getUpdaterMock($content){ protected function getUpdaterMock($content){
// Invalidate cache // Invalidate cache
$mockedAppConfig = $this->getMockBuilder('\OC\AppConfig')
->disableOriginalConstructor()
->getMock()
;
\OC_Appconfig::setValue('core', 'lastupdatedat', 0); \OC_Appconfig::setValue('core', 'lastupdatedat', 0);
$mockedHTTPHelper = $this->getMockBuilder('\OC\HTTPHelper') $mockedHTTPHelper = $this->getMockBuilder('\OC\HTTPHelper')
@ -97,7 +102,7 @@ class UpdaterTest extends \Test\TestCase {
$mockedHTTPHelper->method('getUrlContent') $mockedHTTPHelper->method('getUrlContent')
->willReturn($content) ->willReturn($content)
; ;
return new Updater($mockedHTTPHelper); return new Updater($mockedHTTPHelper, $mockedAppConfig);
} }
} }