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
58 changes: 18 additions & 40 deletions apps/updatenotification/lib/Notification/BackgroundJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
use OC\Installer;
use OC\Updater\VersionCheck;
use OCP\App\IAppManager;
use OCP\Http\Client\IClientService;
use OCP\IConfig;
use OCP\IGroup;
use OCP\IGroupManager;
Expand All @@ -39,42 +38,33 @@
class BackgroundJob extends TimedJob {
protected $connectionNotifications = [3, 7, 14, 30];

/** @var IConfig */
protected $config;

/** @var IManager */
protected $notificationManager;

/** @var IGroupManager */
protected $groupManager;

/** @var IAppManager */
protected $appManager;

/** @var IClientService */
protected $client;

/** @var Installer */
protected $installer;
protected IConfig $config;
protected IManager $notificationManager;
protected IGroupManager $groupManager;
protected IAppManager $appManager;
protected Installer $installer;
protected VersionCheck $versionCheck;

/** @var string[] */
protected $users;

public function __construct(IConfig $config,
IManager $notificationManager,
IGroupManager $groupManager,
IAppManager $appManager,
IClientService $client,
Installer $installer) {
public function __construct(
IConfig $config,
IManager $notificationManager,
IGroupManager $groupManager,
IAppManager $appManager,
Installer $installer,
VersionCheck $versionCheck
) {
// Run once a day
$this->setInterval(60 * 60 * 24);

$this->config = $config;
$this->notificationManager = $notificationManager;
$this->groupManager = $groupManager;
$this->appManager = $appManager;
$this->client = $client;
$this->installer = $installer;
$this->versionCheck = $versionCheck;
}

protected function run($argument) {
Expand All @@ -101,12 +91,10 @@ protected function checkCoreUpdate() {
return;
}

$updater = $this->createVersionCheck();

$status = $updater->check();
$status = $this->versionCheck->check();
if ($status === false) {
$errors = 1 + (int) $this->config->getAppValue('updatenotification', 'update_check_errors', 0);
$this->config->setAppValue('updatenotification', 'update_check_errors', $errors);
$errors = 1 + (int) $this->config->getAppValue('updatenotification', 'update_check_errors', '0');
$this->config->setAppValue('updatenotification', 'update_check_errors', (string) $errors);

if (\in_array($errors, $this->connectionNotifications, true)) {
$this->sendErrorNotifications($errors);
Expand Down Expand Up @@ -255,16 +243,6 @@ protected function deleteOutdatedNotifications($app, $version) {
$this->notificationManager->markProcessed($notification);
}

/**
* @return VersionCheck
*/
protected function createVersionCheck(): VersionCheck {
return new VersionCheck(
$this->client,
$this->config
);
}

/**
* @return string
*/
Expand Down
27 changes: 10 additions & 17 deletions apps/updatenotification/tests/Notification/BackgroundJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
use OC\Updater\VersionCheck;
use OCA\UpdateNotification\Notification\BackgroundJob;
use OCP\App\IAppManager;
use OCP\Http\Client\IClientService;
use OCP\IConfig;
use OCP\IGroup;
use OCP\IGroupManager;
Expand All @@ -50,10 +49,10 @@ class BackgroundJobTest extends TestCase {
protected $groupManager;
/** @var IAppManager|\PHPUnit\Framework\MockObject\MockObject */
protected $appManager;
/** @var IClientService|\PHPUnit\Framework\MockObject\MockObject */
protected $client;
/** @var Installer|\PHPUnit\Framework\MockObject\MockObject */
/** @var Installer|MockObject */
protected $installer;
/** @var VersionCheck|MockObject */
protected $versionCheck;

protected function setUp(): void {
parent::setUp();
Expand All @@ -62,8 +61,8 @@ protected function setUp(): void {
$this->notificationManager = $this->createMock(IManager::class);
$this->groupManager = $this->createMock(IGroupManager::class);
$this->appManager = $this->createMock(IAppManager::class);
$this->client = $this->createMock(IClientService::class);
$this->installer = $this->createMock(Installer::class);
$this->versionCheck = $this->createMock(VersionCheck::class);
}

/**
Expand All @@ -77,8 +76,8 @@ protected function getJob(array $methods = []) {
$this->notificationManager,
$this->groupManager,
$this->appManager,
$this->client,
$this->installer
$this->installer,
$this->versionCheck,
);
}
{
Expand All @@ -88,8 +87,8 @@ protected function getJob(array $methods = []) {
$this->notificationManager,
$this->groupManager,
$this->appManager,
$this->client,
$this->installer,
$this->versionCheck,
])
->setMethods($methods)
->getMock();
Expand Down Expand Up @@ -154,7 +153,6 @@ public function dataCheckCoreUpdate(): array {
public function testCheckCoreUpdate(string $channel, $versionCheck, $version, $readableVersion, $errorDays) {
$job = $this->getJob([
'getChannel',
'createVersionCheck',
'createNotifications',
'clearErrorNotifications',
'sendErrorNotifications',
Expand All @@ -165,17 +163,12 @@ public function testCheckCoreUpdate(string $channel, $versionCheck, $version, $r
->willReturn($channel);

if ($versionCheck === null) {
$job->expects($this->never())
->method('createVersionCheck');
$this->versionCheck->expects($this->never())
->method('check');
} else {
$check = $this->createMock(VersionCheck::class);
$check->expects($this->once())
$this->versionCheck->expects($this->once())
->method('check')
->willReturn($versionCheck);

$job->expects($this->once())
->method('createVersionCheck')
->willReturn($check);
}

if ($version === null) {
Expand Down
52 changes: 38 additions & 14 deletions lib/private/Updater/VersionCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,28 @@

use OCP\Http\Client\IClientService;
use OCP\IConfig;
use OCP\IUserManager;
use OCP\Support\Subscription\IRegistry;
use OCP\Util;

class VersionCheck {

/** @var IClientService */
private $clientService;

/** @var IConfig */
private $config;

/**
* @param IClientService $clientService
* @param IConfig $config
*/
public function __construct(IClientService $clientService,
IConfig $config) {
private IClientService $clientService;
private IConfig $config;
private IUserManager $userManager;
private IRegistry $registry;

public function __construct(
IClientService $clientService,
IConfig $config,
IUserManager $userManager,
IRegistry $registry
) {
$this->clientService = $clientService;
$this->config = $config;
$this->userManager = $userManager;
$this->registry = $registry;
}


/**
* Check if a new version is available
*
Expand Down Expand Up @@ -82,6 +83,8 @@ public function check() {
$version['php_major'] = PHP_MAJOR_VERSION;
$version['php_minor'] = PHP_MINOR_VERSION;
$version['php_release'] = PHP_RELEASE_VERSION;
$version['category'] = $this->computeCategory();
$version['isSubscriber'] = (int) $this->registry->delegateHasValidSubscription();
$versionString = implode('x', $version);

//fetch xml data from updater
Expand Down Expand Up @@ -131,4 +134,25 @@ protected function getUrlContent($url) {
$response = $client->get($url);
return $response->getBody();
}

private function computeCategory(): int {
$categoryBoundaries = [
100,
500,
1000,
5000,
10000,
100000,
1000000,
];

$nbUsers = $this->userManager->countSeenUsers();
foreach ($categoryBoundaries as $categoryId => $boundary) {
if ($nbUsers <= $boundary) {
return $categoryId;
}
}

return count($categoryBoundaries);
}
}
27 changes: 20 additions & 7 deletions tests/lib/Updater/VersionCheckTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,17 @@
use OC\Updater\VersionCheck;
use OCP\Http\Client\IClientService;
use OCP\IConfig;
use OCP\IUserManager;
use OCP\Support\Subscription\IRegistry;
use OCP\Util;

class VersionCheckTest extends \Test\TestCase {
/** @var IConfig| \PHPUnit\Framework\MockObject\MockObject */
private $config;
/** @var VersionCheck | \PHPUnit\Framework\MockObject\MockObject*/
private $updater;
/** @var IRegistry | \PHPUnit\Framework\Mo2ckObject\MockObject*/
private $registry;

protected function setUp(): void {
parent::setUp();
Expand All @@ -42,9 +46,18 @@ protected function setUp(): void {
->disableOriginalConstructor()
->getMock();

$this->registry = $this->createMock(IRegistry::class);
$this->registry
->method('delegateHasValidSubscription')
->willReturn(false);
$this->updater = $this->getMockBuilder(VersionCheck::class)
->setMethods(['getUrlContent'])
->setConstructorArgs([$clientService, $this->config])
->setConstructorArgs([
$clientService,
$this->config,
$this->createMock(IUserManager::class),
$this->registry,
])
->getMock();
}

Expand All @@ -53,7 +66,7 @@ protected function setUp(): void {
* @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;
return $baseUrl . '?version='.implode('x', Util::getVersion()).'xinstalledatxlastupdatedatx'.\OC_Util::getChannel().'xxx'.PHP_MAJOR_VERSION.'x'.PHP_MINOR_VERSION.'x'.PHP_RELEASE_VERSION.'x0x0';
}

public function testCheckInCache() {
Expand Down Expand Up @@ -114,7 +127,7 @@ public function testCheckWithoutUpdateUrl() {
0,
'installedat',
'installedat',
'lastupdatedat'
'lastupdatedat',
);
$this->config
->expects($this->once())
Expand Down Expand Up @@ -166,7 +179,7 @@ public function testCheckWithInvalidXml() {
0,
'installedat',
'installedat',
'lastupdatedat'
'lastupdatedat',
);
$this->config
->expects($this->once())
Expand Down Expand Up @@ -220,7 +233,7 @@ public function testCheckWithEmptyValidXmlResponse() {
0,
'installedat',
'installedat',
'lastupdatedat'
'lastupdatedat',
);
$this->config
->expects($this->once())
Expand Down Expand Up @@ -273,7 +286,7 @@ public function testCheckWithEmptyInvalidXmlResponse() {
0,
'installedat',
'installedat',
'lastupdatedat'
'lastupdatedat',
);
$this->config
->expects($this->once())
Expand Down Expand Up @@ -327,7 +340,7 @@ public function testCheckWithMissingAttributeXmlResponse() {
0,
'installedat',
'installedat',
'lastupdatedat'
'lastupdatedat',
);
$this->config
->expects($this->once())
Expand Down