From 81d571241991926f8820c43020a0250eb305a0b0 Mon Sep 17 00:00:00 2001 From: Victor Dubiniuk Date: Mon, 8 Dec 2014 17:38:56 +0300 Subject: [PATCH] Fix tests. Add two more test cases --- lib/private/updater.php | 13 +++--- tests/lib/updater.php | 91 +++++++++++++++++++++++------------------ 2 files changed, 59 insertions(+), 45 deletions(-) diff --git a/lib/private/updater.php b/lib/private/updater.php index 5846a6a655..266cf9cc89 100644 --- a/lib/private/updater.php +++ b/lib/private/updater.php @@ -104,15 +104,16 @@ class Updater extends BasicEmitter { $tmp = array(); $xml = $this->httpHelper->getUrlContent($url); - if ($xml !== false) { + if ($xml) { $loadEntities = libxml_disable_entity_loader(true); $data = @simplexml_load_string($xml); libxml_disable_entity_loader($loadEntities); - - $tmp['version'] = $data->version; - $tmp['versionstring'] = $data->versionstring; - $tmp['url'] = $data->url; - $tmp['web'] = $data->web; + if ($data !== false) { + $tmp['version'] = $data->version; + $tmp['versionstring'] = $data->versionstring; + $tmp['url'] = $data->url; + $tmp['web'] = $data->web; + } } else { $data = array(); } diff --git a/tests/lib/updater.php b/tests/lib/updater.php index 5a05bc09bb..832f60fac0 100644 --- a/tests/lib/updater.php +++ b/tests/lib/updater.php @@ -33,58 +33,71 @@ class UpdaterTest extends \Test\TestCase { public function testCheck(){ - $httpHelper = $this->getMockBuilder('\OC\HTTPHelper') - ->getMock(); + // Valid XML. Empty values + $updater = $this->getUpdaterMock( + '' + ); + $result = array_map('strval', $updater->check()); - $httpHelper->method('getUrlContent') - ->willReturn( - '' - ) - ; - - $updater = new Updater($httpHelper); - // Invalidate cache - \OC_Appconfig::setValue('core', 'lastupdatedat', 0); - $result = $updater->check(); - $this->assertContains('version', $result); - $this->assertContains('versionstring', $result); - $this->assertContains('url', $result); - $this->assertContains('web', $result); + $this->assertArrayHasKey('version', $result); + $this->assertArrayHasKey('versionstring', $result); + $this->assertArrayHasKey('url', $result); + $this->assertArrayHasKey('web', $result); $this->assertEmpty($result['version']); $this->assertEmpty($result['versionstring']); $this->assertEmpty($result['url']); $this->assertEmpty($result['web']); - // Invalidate cache - \OC_Appconfig::setValue('core', 'lastupdatedat', 0); - $httpHelper->method('getUrlContent') - ->willReturn('') - ; - - $emptyResult = $updater->check(); + // Empty feed + $emptyUpdater = $this->getUpdaterMock(''); + $emptyResult = $emptyUpdater->check(); $this->assertEmpty($emptyResult); - - // Invalidate cache - \OC_Appconfig::setValue('core', 'lastupdatedat', 0); - $httpHelper->method('getUrlContent') - ->willReturn(' + + // 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( + ' 7.0.3.4 ownCloud 7.0.3 http://download.owncloud.org/community/owncloud-7.0.3.zip http://owncloud.org/ -') +' + ); + $newResult = array_map('strval', $newUpdater->check()); + + $this->assertArrayHasKey('version', $newResult); + $this->assertArrayHasKey('versionstring', $newResult); + $this->assertArrayHasKey('url', $newResult); + $this->assertArrayHasKey('web', $newResult); + $this->assertEquals('7.0.3.4', $newResult['version']); + $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://owncloud.org/', $newResult['web']); + + // Invalid XML + $invalidUpdater = $this->getUpdaterMock('OMG!'); + $invalidResult = $invalidUpdater->check(); + $this->assertEmpty($invalidResult); + } + + protected function getUpdaterMock($content){ + // Invalidate cache + \OC_Appconfig::setValue('core', 'lastupdatedat', 0); + + $mockedHTTPHelper = $this->getMockBuilder('\OC\HTTPHelper') + ->setConstructorArgs(array(\OC::$server->getConfig())) + ->getMock() ; - $newResult = $updater->check(); - $this->assertContains('version', $newResult); - $this->assertContains('versionstring', $newResult); - $this->assertContains('url', $newResult); - $this->assertContains('web', $newResult); - $this->assertEqual('7.0.3.4', $newResult['version']); - $this->assertEqual('ownCloud 7.0.3', $newResult['versionstring']); - $this->assertEqual('http://download.owncloud.org/community/owncloud-7.0.3.zip', $newResult['url']); - $this->assertEqual('http://owncloud.org/', $newResult['web']); + $mockedHTTPHelper->method('getUrlContent') + ->willReturn($content) + ; + return new Updater($mockedHTTPHelper); } -} \ No newline at end of file +}