diff --git a/apps/updatenotification/lib/Notification/BackgroundJob.php b/apps/updatenotification/lib/Notification/BackgroundJob.php index f8f1f41e589ef..a56a6c5d7dc70 100644 --- a/apps/updatenotification/lib/Notification/BackgroundJob.php +++ b/apps/updatenotification/lib/Notification/BackgroundJob.php @@ -35,49 +35,30 @@ use OCP\IConfig; use OCP\IGroup; use OCP\IGroupManager; +use OCP\IUserManager; use OCP\Notification\IManager; +use OCP\Support\Subscription\IRegistry; 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; - /** @var string[] */ protected $users; - public function __construct(ITimeFactory $timeFactory, - IConfig $config, - IManager $notificationManager, - IGroupManager $groupManager, - IAppManager $appManager, - IClientService $client, - Installer $installer) { + public function __construct( + ITimeFactory $timeFactory, + protected IConfig $config, + protected IManager $notificationManager, + protected IUserManager $userManager, + protected IGroupManager $groupManager, + protected IAppManager $appManager, + protected IClientService $client, + protected Installer $installer, + protected IRegistry $subscriptionRegistry, + ) { parent::__construct($timeFactory); // 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; } protected function run($argument) { @@ -264,7 +245,9 @@ protected function deleteOutdatedNotifications($app, $version) { protected function createVersionCheck(): VersionCheck { return new VersionCheck( $this->client, - $this->config + $this->config, + $this->userManager, + $this->subscriptionRegistry, ); } diff --git a/apps/updatenotification/tests/Notification/BackgroundJobTest.php b/apps/updatenotification/tests/Notification/BackgroundJobTest.php index 70d1a918a01b7..c6f950bb768fe 100644 --- a/apps/updatenotification/tests/Notification/BackgroundJobTest.php +++ b/apps/updatenotification/tests/Notification/BackgroundJobTest.php @@ -37,8 +37,10 @@ use OCP\IGroup; use OCP\IGroupManager; use OCP\IUser; +use OCP\IUserManager; use OCP\Notification\IManager; use OCP\Notification\INotification; +use OCP\Support\Subscription\IRegistry; use PHPUnit\Framework\MockObject\MockObject; use Test\TestCase; @@ -57,17 +59,21 @@ class BackgroundJobTest extends TestCase { protected $installer; /** @var ITimeFactory|MockObject */ protected $timeFactory; + /** @var IRegistry|MockObject */ + protected $registry; protected function setUp(): void { parent::setUp(); $this->config = $this->createMock(IConfig::class); $this->notificationManager = $this->createMock(IManager::class); + $this->userManager = $this->createMock(IUserManager::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->timeFactory = $this->createMock(ITimeFactory::class); + $this->registry = $this->createMock(IRegistry::class); } /** @@ -80,10 +86,12 @@ protected function getJob(array $methods = []) { $this->timeFactory, $this->config, $this->notificationManager, + $this->userManager, $this->groupManager, $this->appManager, $this->client, - $this->installer + $this->installer, + $this->registry, ); } { @@ -92,10 +100,12 @@ protected function getJob(array $methods = []) { $this->timeFactory, $this->config, $this->notificationManager, + $this->userManager, $this->groupManager, $this->appManager, $this->client, $this->installer, + $this->registry, ]) ->setMethods($methods) ->getMock(); diff --git a/lib/private/Updater/VersionCheck.php b/lib/private/Updater/VersionCheck.php index a634ae4cc710d..a4e7153a16e83 100644 --- a/lib/private/Updater/VersionCheck.php +++ b/lib/private/Updater/VersionCheck.php @@ -28,23 +28,17 @@ 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) { - $this->clientService = $clientService; - $this->config = $config; + public function __construct( + private IClientService $clientService, + private IConfig $config, + private IUserManager $userManager, + private IRegistry $registry, + ) { } @@ -81,6 +75,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 @@ -130,4 +126,25 @@ protected function getUrlContent($url) { $response = $client->get($url); return $response->getBody(); } + + private function computeCategory() { + $categoryBoundaries = [ + 100, + 500, + 1000, + 5000, + 10000, + 100000, + 1000000, + ]; + + $nbUsers = $this->userManager->countSeenUsers(); + foreach ($categoryBoundaries as $categoryId => $boundary) { + if ($nbUsers <= $boundary) { + return $categoryId; + } + } + + return $categoryId + 1; + } } diff --git a/tests/lib/Updater/VersionCheckTest.php b/tests/lib/Updater/VersionCheckTest.php index 1cd632875c393..be847253035f4 100644 --- a/tests/lib/Updater/VersionCheckTest.php +++ b/tests/lib/Updater/VersionCheckTest.php @@ -25,6 +25,8 @@ 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 { @@ -32,6 +34,8 @@ class VersionCheckTest extends \Test\TestCase { private $config; /** @var VersionCheck | \PHPUnit\Framework\MockObject\MockObject*/ private $updater; + /** @var IRegistry | \PHPUnit\Framework\Mo2ckObject\MockObject*/ + private $registry; protected function setUp(): void { parent::setUp(); @@ -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(); } @@ -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() { @@ -114,7 +127,7 @@ public function testCheckWithoutUpdateUrl() { '0', 'installedat', 'installedat', - 'lastupdatedat' + 'lastupdatedat', ); $this->config ->expects($this->once()) @@ -166,7 +179,7 @@ public function testCheckWithInvalidXml() { '0', 'installedat', 'installedat', - 'lastupdatedat' + 'lastupdatedat', ); $this->config ->expects($this->once()) @@ -220,7 +233,7 @@ public function testCheckWithEmptyValidXmlResponse() { '0', 'installedat', 'installedat', - 'lastupdatedat' + 'lastupdatedat', ); $this->config ->expects($this->once()) @@ -273,7 +286,7 @@ public function testCheckWithEmptyInvalidXmlResponse() { '0', 'installedat', 'installedat', - 'lastupdatedat' + 'lastupdatedat', ); $this->config ->expects($this->once()) @@ -327,7 +340,7 @@ public function testCheckWithMissingAttributeXmlResponse() { '0', 'installedat', 'installedat', - 'lastupdatedat' + 'lastupdatedat', ); $this->config ->expects($this->once())