Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public function testViewApps(): void {
]);

$this->initialState
->expects($this->exactly(4))
->expects($this->exactly(5))
->method('provideInitialState');

$policy = new ContentSecurityPolicy();
Expand Down Expand Up @@ -222,7 +222,7 @@ public function testViewAppsAppstoreNotEnabled(): void {
]);

$this->initialState
->expects($this->exactly(4))
->expects($this->exactly(5))
->method('provideInitialState');

$policy = new ContentSecurityPolicy();
Expand Down
6 changes: 4 additions & 2 deletions lib/private/Updater/VersionCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
namespace OC\Updater;

use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Http\Client\IClientService;
use OCP\IAppConfig;
use OCP\IConfig;
Expand All @@ -25,6 +26,7 @@ public function __construct(
private IUserManager $userManager,
private IRegistry $registry,
private LoggerInterface $logger,
private ITimeFactory $timeFactory,
) {
}

Expand All @@ -41,13 +43,13 @@ public function check() {
}

// Look up the cache - it is invalidated all 30 minutes
if (($this->appConfig->getValueInt('core', 'lastupdatedat') + 1800) > time()) {
if (($this->appConfig->getValueInt('core', 'lastupdatedat') + 1800) > $this->timeFactory->getTime()) {
return json_decode($this->config->getAppValue('core', 'lastupdateResult'), true);
}

$updaterUrl = $this->config->getSystemValueString('updater.server.url', 'https://updates.nextcloud.com/updater_server/');

$this->appConfig->setValueInt('core', 'lastupdatedat', time());
$this->appConfig->setValueInt('core', 'lastupdatedat', $this->timeFactory->getTime());

if ($this->config->getAppValue('core', 'installedat', '') === '') {
$this->config->setAppValue('core', 'installedat', (string)microtime(true));
Expand Down
78 changes: 47 additions & 31 deletions tests/lib/Updater/VersionCheckTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,25 @@
namespace Test\Updater;

use OC\Updater\VersionCheck;
use OCP\AppFramework\Utility\ITimeFactory;
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 PHPUnit\Framework\MockObject\MockObject;
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 IAppConfig| \PHPUnit\Framework\MockObject\MockObject */
private $appConfig;
/** @var VersionCheck | \PHPUnit\Framework\MockObject\MockObject */
private $updater;
/** @var IRegistry | \PHPUnit\Framework\Mo2ckObject\MockObject */
private $registry;
/** @var LoggerInterface | \PHPUnit\Framework\Mo2ckObject\MockObject */
private $logger;
private ServerVersion&MockObject $serverVersion;
private IConfig&MockObject $config;
private IAppConfig&MockObject $appConfig;
private VersionCheck&MockObject $updater;
private IRegistry&MockObject $registry;
private LoggerInterface&MockObject $logger;
private ITimeFactory&MockObject $timeFactory;

protected function setUp(): void {
parent::setUp();
Expand All @@ -46,6 +43,7 @@ protected function setUp(): void {
->method('delegateHasValidSubscription')
->willReturn(false);
$this->logger = $this->createMock(LoggerInterface::class);
$this->timeFactory = $this->createMock(ITimeFactory::class);
$this->updater = $this->getMockBuilder(VersionCheck::class)
->onlyMethods(['getUrlContent'])
->setConstructorArgs([
Expand All @@ -56,17 +54,14 @@ protected function setUp(): void {
$this->createMock(IUserManager::class),
$this->registry,
$this->logger,
$this->timeFactory,
])
->getMock();
}

/**
* @param string $baseUrl
* @return string
*/
private function buildUpdateUrl($baseUrl) {
private function buildUpdateUrl(string $baseUrl, int $lastUpdateDate): string {
$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';
return $baseUrl . '?version=' . implode('x', $serverVersion->getVersion()) . 'xinstalledatx' . $lastUpdateDate . 'x' . $serverVersion->getChannel() . 'xxx' . PHP_MAJOR_VERSION . 'x' . PHP_MINOR_VERSION . 'x' . PHP_RELEASE_VERSION . 'x0x0';
}

public function testCheckInCache(): void {
Expand Down Expand Up @@ -98,6 +93,7 @@ public function testCheckInCache(): void {
}

public function testCheckWithoutUpdateUrl(): void {
$lastUpdateDate = time();
$expectedResult = [
'version' => '8.0.4.2',
'versionstring' => 'ownCloud 8.0.4',
Expand All @@ -119,7 +115,7 @@ public function testCheckWithoutUpdateUrl(): void {
->with('core', 'lastupdatedat')
->willReturnOnConsecutiveCalls(
0,
time(),
$lastUpdateDate,
);
$this->config
->expects($this->exactly(2))
Expand All @@ -134,11 +130,14 @@ public function testCheckWithoutUpdateUrl(): void {
$this->appConfig
->expects($this->once())
->method('setValueInt')
->with('core', 'lastupdatedat', time());
->with('core', 'lastupdatedat', $lastUpdateDate);
$this->config
->expects($this->once())
->method('setAppValue')
->with('core', 'lastupdateResult', json_encode($expectedResult));
$this->timeFactory
->method('getTime')
->willReturn($lastUpdateDate);

$updateXml = '<?xml version="1.0"?>
<owncloud>
Expand All @@ -152,13 +151,14 @@ public function testCheckWithoutUpdateUrl(): void {
$this->updater
->expects($this->once())
->method('getUrlContent')
->with($this->buildUpdateUrl('https://updates.nextcloud.com/updater_server/'))
->with($this->buildUpdateUrl('https://updates.nextcloud.com/updater_server/', $lastUpdateDate))
->willReturn($updateXml);

$this->assertSame($expectedResult, $this->updater->check());
}

public function testCheckWithInvalidXml(): void {
$lastUpdateDate = time();
$this->config
->expects($this->once())
->method('getSystemValueBool')
Expand All @@ -170,7 +170,7 @@ public function testCheckWithInvalidXml(): void {
->with('core', 'lastupdatedat')
->willReturnOnConsecutiveCalls(
0,
time(),
$lastUpdateDate,
);
$this->config
->expects($this->exactly(2))
Expand All @@ -185,23 +185,27 @@ public function testCheckWithInvalidXml(): void {
$this->appConfig
->expects($this->once())
->method('setValueInt')
->with('core', 'lastupdatedat', time());
->with('core', 'lastupdatedat', $lastUpdateDate);
$this->config
->expects($this->once())
->method('setAppValue')
->with('core', 'lastupdateResult', $this->isType('string'));
$this->timeFactory
->method('getTime')
->willReturn($lastUpdateDate);

$updateXml = 'Invalid XML Response!';
$this->updater
->expects($this->once())
->method('getUrlContent')
->with($this->buildUpdateUrl('https://updates.nextcloud.com/updater_server/'))
->with($this->buildUpdateUrl('https://updates.nextcloud.com/updater_server/', $lastUpdateDate))
->willReturn($updateXml);

$this->assertSame([], $this->updater->check());
}

public function testCheckWithEmptyValidXmlResponse(): void {
$lastUpdateDate = time();
$expectedResult = [
'version' => '',
'versionstring' => '',
Expand All @@ -223,7 +227,7 @@ public function testCheckWithEmptyValidXmlResponse(): void {
->with('core', 'lastupdatedat')
->willReturnOnConsecutiveCalls(
0,
time(),
$lastUpdateDate,
);
$this->config
->expects($this->exactly(2))
Expand All @@ -238,11 +242,14 @@ public function testCheckWithEmptyValidXmlResponse(): void {
$this->appConfig
->expects($this->once())
->method('setValueInt')
->with('core', 'lastupdatedat', time());
->with('core', 'lastupdatedat', $lastUpdateDate);
$this->config
->expects($this->once())
->method('setAppValue')
->with('core', 'lastupdateResult', $this->isType('string'));
$this->timeFactory
->method('getTime')
->willReturn($lastUpdateDate);

$updateXml = '<?xml version="1.0"?>
<owncloud>
Expand All @@ -255,13 +262,14 @@ public function testCheckWithEmptyValidXmlResponse(): void {
$this->updater
->expects($this->once())
->method('getUrlContent')
->with($this->buildUpdateUrl('https://updates.nextcloud.com/updater_server/'))
->with($this->buildUpdateUrl('https://updates.nextcloud.com/updater_server/', $lastUpdateDate))
->willReturn($updateXml);

$this->assertSame($expectedResult, $this->updater->check());
}

public function testCheckWithEmptyInvalidXmlResponse(): void {
$lastUpdateDate = time();
$expectedResult = [];

$this->config
Expand Down Expand Up @@ -295,18 +303,22 @@ public function testCheckWithEmptyInvalidXmlResponse(): void {
->expects($this->once())
->method('setAppValue')
->with('core', 'lastupdateResult', $this->isType('string'));
$this->timeFactory
->method('getTime')
->willReturn($lastUpdateDate);

$updateXml = '';
$this->updater
->expects($this->once())
->method('getUrlContent')
->with($this->buildUpdateUrl('https://updates.nextcloud.com/updater_server/'))
->with($this->buildUpdateUrl('https://updates.nextcloud.com/updater_server/', $lastUpdateDate))
->willReturn($updateXml);

$this->assertSame($expectedResult, $this->updater->check());
}

public function testCheckWithMissingAttributeXmlResponse(): void {
$lastUpdateDate = time();
$expectedResult = [
'version' => '',
'versionstring' => '',
Expand All @@ -328,7 +340,8 @@ public function testCheckWithMissingAttributeXmlResponse(): void {
->with('core', 'lastupdatedat')
->willReturnOnConsecutiveCalls(
0,
time(),
$lastUpdateDate,
$lastUpdateDate,
);
$this->config
->expects($this->exactly(2))
Expand All @@ -343,7 +356,10 @@ public function testCheckWithMissingAttributeXmlResponse(): void {
$this->appConfig
->expects($this->once())
->method('setValueInt')
->with('core', 'lastupdatedat', time());
->with('core', 'lastupdatedat', $lastUpdateDate);
$this->timeFactory
->method('getTime')
->willReturn($lastUpdateDate);
$this->config
->expects($this->once())
->method('setAppValue')
Expand All @@ -360,7 +376,7 @@ public function testCheckWithMissingAttributeXmlResponse(): void {
$this->updater
->expects($this->once())
->method('getUrlContent')
->with($this->buildUpdateUrl('https://updates.nextcloud.com/updater_server/'))
->with($this->buildUpdateUrl('https://updates.nextcloud.com/updater_server/', $lastUpdateDate))
->willReturn($updateXml);

$this->assertSame($expectedResult, $this->updater->check());
Expand Down
Loading