diff options
Diffstat (limited to 'tests/lib/Updater/VersionCheckTest.php')
-rw-r--r-- | tests/lib/Updater/VersionCheckTest.php | 261 |
1 files changed, 125 insertions, 136 deletions
diff --git a/tests/lib/Updater/VersionCheckTest.php b/tests/lib/Updater/VersionCheckTest.php index 0f073abc3ce..1936062a5d0 100644 --- a/tests/lib/Updater/VersionCheckTest.php +++ b/tests/lib/Updater/VersionCheckTest.php @@ -1,53 +1,45 @@ <?php + /** - * @author Lukas Reschke <lukas@owncloud.com> - * @author Victor Dubiniuk <dubiniuk@owncloud.com> - * - * @copyright Copyright (c) 2015, ownCloud, Inc. - * @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/> - * + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ namespace Test\Updater; use OC\Updater\VersionCheck; use OCP\Http\Client\IClientService; +use OCP\IAppConfig; use OCP\IConfig; use OCP\IUserManager; +use OCP\Server; +use OCP\ServerVersion; use OCP\Support\Subscription\IRegistry; -use OCP\Util; use Psr\Log\LoggerInterface; class VersionCheckTest extends \Test\TestCase { + /** @var ServerVersion|\PHPUnit\Framework\MockObject\MockObject */ + private $serverVersion; /** @var IConfig| \PHPUnit\Framework\MockObject\MockObject */ private $config; - /** @var VersionCheck | \PHPUnit\Framework\MockObject\MockObject*/ + /** @var IAppConfig| \PHPUnit\Framework\MockObject\MockObject */ + private $appConfig; + /** @var VersionCheck | \PHPUnit\Framework\MockObject\MockObject */ private $updater; - /** @var IRegistry | \PHPUnit\Framework\Mo2ckObject\MockObject*/ + /** @var IRegistry | \PHPUnit\Framework\Mo2ckObject\MockObject */ private $registry; - /** @var LoggerInterface | \PHPUnit\Framework\Mo2ckObject\MockObject*/ + /** @var LoggerInterface | \PHPUnit\Framework\Mo2ckObject\MockObject */ private $logger; protected function setUp(): void { parent::setUp(); - $this->config = $this->getMockBuilder(IConfig::class) - ->disableOriginalConstructor() - ->getMock(); - $clientService = $this->getMockBuilder(IClientService::class) - ->disableOriginalConstructor() - ->getMock(); + $this->serverVersion = $this->createMock(ServerVersion::class); + $this->config = $this->createMock(IConfig::class); + $this->appConfig = $this->createMock(IAppConfig::class); + $clientService = $this->createMock(IClientService::class); + + $this->serverVersion->method('getChannel')->willReturn('git'); $this->registry = $this->createMock(IRegistry::class); $this->registry @@ -55,10 +47,12 @@ class VersionCheckTest extends \Test\TestCase { ->willReturn(false); $this->logger = $this->createMock(LoggerInterface::class); $this->updater = $this->getMockBuilder(VersionCheck::class) - ->setMethods(['getUrlContent']) + ->onlyMethods(['getUrlContent']) ->setConstructorArgs([ + $this->serverVersion, $clientService, $this->config, + $this->appConfig, $this->createMock(IUserManager::class), $this->registry, $this->logger, @@ -71,10 +65,11 @@ class VersionCheckTest extends \Test\TestCase { * @return string */ private function buildUpdateUrl($baseUrl) { - return $baseUrl . '?version='.implode('x', Util::getVersion()).'xinstalledatxlastupdatedatx'.\OC_Util::getChannel().'xxx'.PHP_MAJOR_VERSION.'x'.PHP_MINOR_VERSION.'x'.PHP_RELEASE_VERSION.'x0x0'; + $serverVersion = Server::get(ServerVersion::class); + return $baseUrl . '?version=' . implode('x', $serverVersion->getVersion()) . 'xinstalledatx' . time() . 'x' . $serverVersion->getChannel() . 'xxx' . PHP_MAJOR_VERSION . 'x' . PHP_MINOR_VERSION . 'x' . PHP_RELEASE_VERSION . 'x0x0'; } - public function testCheckInCache() { + public function testCheckInCache(): void { $expectedResult = [ 'version' => '8.0.4.2', 'versionstring' => 'ownCloud 8.0.4', @@ -88,22 +83,21 @@ class VersionCheckTest extends \Test\TestCase { ->method('getSystemValueBool') ->with('has_internet_connection', true) ->willReturn(true); + $this->appConfig + ->expects($this->once()) + ->method('getValueInt') + ->with('core', 'lastupdatedat') + ->willReturn(time()); $this->config - ->expects($this->exactly(2)) + ->expects($this->once()) ->method('getAppValue') - ->withConsecutive( - ['core', 'lastupdatedat'], - ['core', 'lastupdateResult'] - ) - ->willReturnOnConsecutiveCalls( - time(), - json_encode($expectedResult) - ); + ->with('core', 'lastupdateResult') + ->willReturn(json_encode($expectedResult)); $this->assertSame($expectedResult, $this->updater->check()); } - public function testCheckWithoutUpdateUrl() { + public function testCheckWithoutUpdateUrl(): void { $expectedResult = [ 'version' => '8.0.4.2', 'versionstring' => 'ownCloud 8.0.4', @@ -119,33 +113,32 @@ class VersionCheckTest extends \Test\TestCase { ->method('getSystemValueBool') ->with('has_internet_connection', true) ->willReturn(true); - $this->config - ->expects($this->exactly(4)) - ->method('getAppValue') - ->withConsecutive( - ['core', 'lastupdatedat'], - ['core', 'installedat'], - ['core', 'installedat'], - ['core', 'lastupdatedat'], - ) + $this->appConfig + ->expects($this->exactly(2)) + ->method('getValueInt') + ->with('core', 'lastupdatedat') ->willReturnOnConsecutiveCalls( - '0', - 'installedat', - 'installedat', - 'lastupdatedat', + 0, + time(), ); $this->config + ->expects($this->exactly(2)) + ->method('getAppValue') + ->with('core', 'installedat') + ->willReturn('installedat'); + $this->config ->expects($this->once()) ->method('getSystemValueString') ->with('updater.server.url', 'https://updates.nextcloud.com/updater_server/') ->willReturnArgument(1); + $this->appConfig + ->expects($this->once()) + ->method('setValueInt') + ->with('core', 'lastupdatedat', time()); $this->config - ->expects($this->exactly(2)) + ->expects($this->once()) ->method('setAppValue') - ->withConsecutive( - ['core', 'lastupdatedat', $this->isType('string')], - ['core', 'lastupdateResult', json_encode($expectedResult)] - ); + ->with('core', 'lastupdateResult', json_encode($expectedResult)); $updateXml = '<?xml version="1.0"?> <owncloud> @@ -165,39 +158,38 @@ class VersionCheckTest extends \Test\TestCase { $this->assertSame($expectedResult, $this->updater->check()); } - public function testCheckWithInvalidXml() { + public function testCheckWithInvalidXml(): void { $this->config ->expects($this->once()) ->method('getSystemValueBool') ->with('has_internet_connection', true) ->willReturn(true); - $this->config - ->expects($this->exactly(4)) - ->method('getAppValue') - ->withConsecutive( - ['core', 'lastupdatedat'], - ['core', 'installedat'], - ['core', 'installedat'], - ['core', 'lastupdatedat'], - ) + $this->appConfig + ->expects($this->exactly(2)) + ->method('getValueInt') + ->with('core', 'lastupdatedat') ->willReturnOnConsecutiveCalls( - '0', - 'installedat', - 'installedat', - 'lastupdatedat', + 0, + time(), ); $this->config + ->expects($this->exactly(2)) + ->method('getAppValue') + ->with('core', 'installedat') + ->willReturn('installedat'); + $this->config ->expects($this->once()) ->method('getSystemValueString') ->with('updater.server.url', 'https://updates.nextcloud.com/updater_server/') ->willReturnArgument(1); + $this->appConfig + ->expects($this->once()) + ->method('setValueInt') + ->with('core', 'lastupdatedat', time()); $this->config - ->expects($this->exactly(2)) + ->expects($this->once()) ->method('setAppValue') - ->withConsecutive( - ['core', 'lastupdatedat', $this->isType('string')], - ['core', 'lastupdateResult', '[]'] - ); + ->with('core', 'lastupdateResult', $this->isType('string')); $updateXml = 'Invalid XML Response!'; $this->updater @@ -209,7 +201,7 @@ class VersionCheckTest extends \Test\TestCase { $this->assertSame([], $this->updater->check()); } - public function testCheckWithEmptyValidXmlResponse() { + public function testCheckWithEmptyValidXmlResponse(): void { $expectedResult = [ 'version' => '', 'versionstring' => '', @@ -225,33 +217,32 @@ class VersionCheckTest extends \Test\TestCase { ->method('getSystemValueBool') ->with('has_internet_connection', true) ->willReturn(true); - $this->config - ->expects($this->exactly(4)) - ->method('getAppValue') - ->withConsecutive( - ['core', 'lastupdatedat'], - ['core', 'installedat'], - ['core', 'installedat'], - ['core', 'lastupdatedat'], - ) + $this->appConfig + ->expects($this->exactly(2)) + ->method('getValueInt') + ->with('core', 'lastupdatedat') ->willReturnOnConsecutiveCalls( - '0', - 'installedat', - 'installedat', - 'lastupdatedat', + 0, + time(), ); $this->config + ->expects($this->exactly(2)) + ->method('getAppValue') + ->with('core', 'installedat') + ->willReturn('installedat'); + $this->config ->expects($this->once()) ->method('getSystemValueString') ->with('updater.server.url', 'https://updates.nextcloud.com/updater_server/') ->willReturnArgument(1); + $this->appConfig + ->expects($this->once()) + ->method('setValueInt') + ->with('core', 'lastupdatedat', time()); $this->config - ->expects($this->exactly(2)) + ->expects($this->once()) ->method('setAppValue') - ->withConsecutive( - ['core', 'lastupdatedat', $this->isType('string')], - ['core', 'lastupdateResult', $this->isType('string')] - ); + ->with('core', 'lastupdateResult', $this->isType('string')); $updateXml = '<?xml version="1.0"?> <owncloud> @@ -270,7 +261,7 @@ class VersionCheckTest extends \Test\TestCase { $this->assertSame($expectedResult, $this->updater->check()); } - public function testCheckWithEmptyInvalidXmlResponse() { + public function testCheckWithEmptyInvalidXmlResponse(): void { $expectedResult = []; $this->config @@ -278,33 +269,32 @@ class VersionCheckTest extends \Test\TestCase { ->method('getSystemValueBool') ->with('has_internet_connection', true) ->willReturn(true); - $this->config - ->expects($this->exactly(4)) - ->method('getAppValue') - ->withConsecutive( - ['core', 'lastupdatedat'], - ['core', 'installedat'], - ['core', 'installedat'], - ['core', 'lastupdatedat'], - ) + $this->appConfig + ->expects($this->exactly(2)) + ->method('getValueInt') + ->with('core', 'lastupdatedat') ->willReturnOnConsecutiveCalls( - '0', - 'installedat', - 'installedat', - 'lastupdatedat', + 0, + time(), ); $this->config + ->expects($this->exactly(2)) + ->method('getAppValue') + ->with('core', 'installedat') + ->willReturn('installedat'); + $this->config ->expects($this->once()) ->method('getSystemValueString') ->with('updater.server.url', 'https://updates.nextcloud.com/updater_server/') ->willReturnArgument(1); + $this->appConfig + ->expects($this->once()) + ->method('setValueInt') + ->with('core', 'lastupdatedat', time()); $this->config - ->expects($this->exactly(2)) + ->expects($this->once()) ->method('setAppValue') - ->withConsecutive( - ['core', 'lastupdatedat', $this->isType('string')], - ['core', 'lastupdateResult', json_encode($expectedResult)] - ); + ->with('core', 'lastupdateResult', $this->isType('string')); $updateXml = ''; $this->updater @@ -316,7 +306,7 @@ class VersionCheckTest extends \Test\TestCase { $this->assertSame($expectedResult, $this->updater->check()); } - public function testCheckWithMissingAttributeXmlResponse() { + public function testCheckWithMissingAttributeXmlResponse(): void { $expectedResult = [ 'version' => '', 'versionstring' => '', @@ -332,33 +322,32 @@ class VersionCheckTest extends \Test\TestCase { ->method('getSystemValueBool') ->with('has_internet_connection', true) ->willReturn(true); - $this->config - ->expects($this->exactly(4)) - ->method('getAppValue') - ->withConsecutive( - ['core', 'lastupdatedat'], - ['core', 'installedat'], - ['core', 'installedat'], - ['core', 'lastupdatedat'], - ) + $this->appConfig + ->expects($this->exactly(2)) + ->method('getValueInt') + ->with('core', 'lastupdatedat') ->willReturnOnConsecutiveCalls( - '0', - 'installedat', - 'installedat', - 'lastupdatedat', + 0, + time(), ); $this->config + ->expects($this->exactly(2)) + ->method('getAppValue') + ->with('core', 'installedat') + ->willReturn('installedat'); + $this->config ->expects($this->once()) ->method('getSystemValueString') ->with('updater.server.url', 'https://updates.nextcloud.com/updater_server/') ->willReturnArgument(1); + $this->appConfig + ->expects($this->once()) + ->method('setValueInt') + ->with('core', 'lastupdatedat', time()); $this->config - ->expects($this->exactly(2)) + ->expects($this->once()) ->method('setAppValue') - ->withConsecutive( - ['core', 'lastupdatedat', $this->isType('string')], - ['core', 'lastupdateResult', $this->isType('string')] - ); + ->with('core', 'lastupdateResult', $this->isType('string')); // missing autoupdater element should still not fail $updateXml = '<?xml version="1.0"?> @@ -377,7 +366,7 @@ class VersionCheckTest extends \Test\TestCase { $this->assertSame($expectedResult, $this->updater->check()); } - public function testNoInternet() { + public function testNoInternet(): void { $this->config ->expects($this->once()) ->method('getSystemValueBool') |