Skip to content

Commit

Permalink
fix(updatenotification): spread the use of new appconfig
Browse files Browse the repository at this point in the history
Signed-off-by: Maxence Lange <[email protected]>
  • Loading branch information
ArtificialOwl committed Mar 13, 2024
1 parent 5723c13 commit 519e434
Show file tree
Hide file tree
Showing 11 changed files with 196 additions and 201 deletions.
35 changes: 12 additions & 23 deletions apps/settings/lib/Settings/Admin/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Services\IInitialState;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\IAppConfig;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\IL10N;
Expand All @@ -39,28 +40,16 @@
class Server implements IDelegatedSettings {
use TProfileHelper;

private IDBConnection $connection;
private IInitialState $initialStateService;
private ProfileManager $profileManager;
private ITimeFactory $timeFactory;
private IConfig $config;
private IL10N $l;
private IURLGenerator $urlGenerator;

public function __construct(IDBConnection $connection,
IInitialState $initialStateService,
ProfileManager $profileManager,
ITimeFactory $timeFactory,
IURLGenerator $urlGenerator,
IConfig $config,
IL10N $l) {
$this->connection = $connection;
$this->initialStateService = $initialStateService;
$this->profileManager = $profileManager;
$this->timeFactory = $timeFactory;
$this->config = $config;
$this->l = $l;
$this->urlGenerator = $urlGenerator;
public function __construct(
private IDBConnection $connection,
private IInitialState $initialStateService,
private ProfileManager $profileManager,
private ITimeFactory $timeFactory,
private IURLGenerator $urlGenerator,
private IConfig $config,
private IAppConfig $appConfig,
private IL10N $l,
) {
}

/**
Expand All @@ -69,7 +58,7 @@ public function __construct(IDBConnection $connection,
public function getForm() {
// Background jobs
$this->initialStateService->provideInitialState('backgroundJobsMode', $this->config->getAppValue('core', 'backgroundjobs_mode', 'ajax'));
$this->initialStateService->provideInitialState('lastCron', (int)$this->config->getAppValue('core', 'lastcron', '0'));
$this->initialStateService->provideInitialState('lastCron', $this->appConfig->getValueInt('core', 'lastcron', 0));
$this->initialStateService->provideInitialState('cronMaxAge', $this->cronMaxAge());
$this->initialStateService->provideInitialState('cronErrors', $this->config->getAppValue('core', 'cronErrors'));
$this->initialStateService->provideInitialState('cliBasedCronPossible', function_exists('posix_getpwuid'));
Expand Down
4 changes: 3 additions & 1 deletion apps/settings/lib/SetupChecks/CronInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

namespace OCA\Settings\SetupChecks;

use OCP\IAppConfig;
use OCP\IConfig;
use OCP\IDateTimeFormatter;
use OCP\IL10N;
Expand All @@ -37,6 +38,7 @@ class CronInfo implements ISetupCheck {
public function __construct(
private IL10N $l10n,
private IConfig $config,
private IAppConfig $appConfig,
private IURLGenerator $urlGenerator,
private IDateTimeFormatter $dateTimeFormatter,
) {
Expand All @@ -51,7 +53,7 @@ public function getName(): string {
}

public function run(): SetupResult {
$lastCronRun = (int)$this->config->getAppValue('core', 'lastcron', '0');
$lastCronRun = $this->appConfig->getValueInt('core', 'lastcron', 0);
$relativeTime = $this->dateTimeFormatter->formatTimeSpan($lastCronRun);

if ((time() - $lastCronRun) > 3600) {
Expand Down
5 changes: 5 additions & 0 deletions apps/settings/tests/Settings/Admin/ServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Services\IInitialState;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\IAppConfig;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\IL10N;
Expand All @@ -59,6 +60,8 @@ class ServerTest extends TestCase {
private $timeFactory;
/** @var IConfig|MockObject */
private $config;
/** @var IAppConfig|MockObject */
private $appConfig;
/** @var IL10N|MockObject */
private $l10n;
/** @var IUrlGenerator|MockObject */
Expand All @@ -71,6 +74,7 @@ protected function setUp(): void {
$this->profileManager = $this->createMock(ProfileManager::class);
$this->timeFactory = $this->createMock(ITimeFactory::class);
$this->config = $this->createMock(IConfig::class);
$this->appConfig = $this->createMock(IAppConfig::class);
$this->l10n = $this->createMock(IL10N::class);
$this->urlGenerator = $this->createMock(IUrlGenerator::class);

Expand All @@ -83,6 +87,7 @@ protected function setUp(): void {
$this->timeFactory,
$this->urlGenerator,
$this->config,
$this->appConfig,
$this->l10n,
])
->getMock();
Expand Down
41 changes: 12 additions & 29 deletions apps/updatenotification/lib/Settings/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use OCA\UpdateNotification\UpdateChecker;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Services\IInitialState;
use OCP\IAppConfig;
use OCP\IConfig;
use OCP\IDateTimeFormatter;
use OCP\IGroupManager;
Expand All @@ -45,40 +46,22 @@
use Psr\Log\LoggerInterface;

class Admin implements ISettings {
private IConfig $config;
private UpdateChecker $updateChecker;
private IGroupManager $groupManager;
private IDateTimeFormatter $dateTimeFormatter;
private IFactory $l10nFactory;
private IRegistry $subscriptionRegistry;
private IUserManager $userManager;
private LoggerInterface $logger;
private IInitialState $initialState;

public function __construct(
IConfig $config,
UpdateChecker $updateChecker,
IGroupManager $groupManager,
IDateTimeFormatter $dateTimeFormatter,
IFactory $l10nFactory,
IRegistry $subscriptionRegistry,
IUserManager $userManager,
LoggerInterface $logger,
IInitialState $initialState
private IConfig $config,
private IAppConfig $appConfig,
private UpdateChecker $updateChecker,
private IGroupManager $groupManager,
private IDateTimeFormatter $dateTimeFormatter,
private IFactory $l10nFactory,
private IRegistry $subscriptionRegistry,
private IUserManager $userManager,
private LoggerInterface $logger,
private IInitialState $initialState
) {
$this->config = $config;
$this->updateChecker = $updateChecker;
$this->groupManager = $groupManager;
$this->dateTimeFormatter = $dateTimeFormatter;
$this->l10nFactory = $l10nFactory;
$this->subscriptionRegistry = $subscriptionRegistry;
$this->userManager = $userManager;
$this->logger = $logger;
$this->initialState = $initialState;
}

public function getForm(): TemplateResponse {
$lastUpdateCheckTimestamp = (int)$this->config->getAppValue('core', 'lastupdatedat');
$lastUpdateCheckTimestamp = $this->appConfig->getValueInt('core', 'lastupdatedat');
$lastUpdateCheck = $this->dateTimeFormatter->formatDateTime($lastUpdateCheckTimestamp);

$channels = [
Expand Down
47 changes: 30 additions & 17 deletions apps/updatenotification/tests/Settings/AdminTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use OCA\UpdateNotification\UpdateChecker;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Services\IInitialState;
use OCP\IAppConfig;
use OCP\IConfig;
use OCP\IDateTimeFormatter;
use OCP\IGroup;
Expand All @@ -55,6 +56,8 @@ class AdminTest extends TestCase {
private $admin;
/** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
private $config;
/** @var IAppConfig|\PHPUnit\Framework\MockObject\MockObject */
private $appConfig;
/** @var UpdateChecker|\PHPUnit\Framework\MockObject\MockObject */
private $updateChecker;
/** @var IGroupManager|\PHPUnit\Framework\MockObject\MockObject */
Expand All @@ -74,6 +77,7 @@ protected function setUp(): void {
parent::setUp();

$this->config = $this->createMock(IConfig::class);
$this->appConfig = $this->createMock(IAppConfig::class);
$this->updateChecker = $this->createMock(UpdateChecker::class);
$this->groupManager = $this->createMock(IGroupManager::class);
$this->dateTimeFormatter = $this->createMock(IDateTimeFormatter::class);
Expand All @@ -85,6 +89,7 @@ protected function setUp(): void {

$this->admin = new Admin(
$this->config,
$this->appConfig,
$this->updateChecker,
$this->groupManager,
$this->dateTimeFormatter,
Expand Down Expand Up @@ -143,14 +148,16 @@ public function testGetFormWithUpdate() {
if ($currentChannel === 'git') {
$channels[] = 'git';
}

$this->appConfig
->expects($this->once())
->method('getValueInt')
->with('core', 'lastupdatedat', 0)
->willReturn(12345);
$this->config
->expects($this->exactly(2))
->expects($this->once())
->method('getAppValue')
->willReturnMap([
['core', 'lastupdatedat', '', '12345'],
['updatenotification', 'notify_groups', '["admin"]', '["admin"]'],
]);
->with('updatenotification', 'notify_groups', '["admin"]')
->willReturn('["admin"]');
$this->config
->method('getSystemValue')
->willReturnMap([
Expand All @@ -160,7 +167,7 @@ public function testGetFormWithUpdate() {
$this->dateTimeFormatter
->expects($this->once())
->method('formatDateTime')
->with('12345')
->with(12345)
->willReturn('LastCheckedReturnValue');
$this->updateChecker
->expects($this->once())
Expand Down Expand Up @@ -268,13 +275,16 @@ public function testGetFormWithUpdateAndChangedUpdateServer() {
$channels[] = 'git';
}

$this->appConfig
->expects($this->once())
->method('getValueInt')
->with('core', 'lastupdatedat', 0)
->willReturn(12345);
$this->config
->expects($this->exactly(2))
->expects($this->once())
->method('getAppValue')
->willReturnMap([
['core', 'lastupdatedat', '', '12345'],
['updatenotification', 'notify_groups', '["admin"]', '["admin"]'],
]);
->with('updatenotification', 'notify_groups', '["admin"]')
->willReturn('["admin"]');
$this->config
->method('getSystemValue')
->willReturnMap([
Expand Down Expand Up @@ -392,13 +402,16 @@ public function testGetFormWithUpdateAndCustomersUpdateServer() {
$channels[] = 'git';
}

$this->appConfig
->expects($this->once())
->method('getValueInt')
->with('core', 'lastupdatedat', 0)
->willReturn(12345);
$this->config
->expects($this->exactly(2))
->expects($this->once())
->method('getAppValue')
->willReturnMap([
['core', 'lastupdatedat', '', '12345'],
['updatenotification', 'notify_groups', '["admin"]', '["admin"]'],
]);
->with('updatenotification', 'notify_groups', '["admin"]')
->willReturn('["admin"]');
$this->config
->method('getSystemValue')
->willReturnMap([
Expand Down
13 changes: 8 additions & 5 deletions core/ajax/update.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,13 @@
use OC\Repair\Events\RepairWarningEvent;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IAppConfig;
use OCP\IConfig;
use OCP\IEventSource;
use OCP\IEventSourceFactory;
use OCP\IL10N;
use OCP\L10N\IFactory;
use OCP\Server;
use Psr\Log\LoggerInterface;

if (!str_contains(@ini_get('disable_functions'), 'set_time_limit')) {
Expand Down Expand Up @@ -111,13 +114,13 @@ public function handleRepairFeedback(Event $event): void {
// avoid side effects
\OC_User::setIncognitoMode(true);

$logger = \OC::$server->get(\Psr\Log\LoggerInterface::class);
$config = \OC::$server->getConfig();
$config = Server::get(IConfig::class);
$updater = new \OC\Updater(
$config,
Server::get(IAppConfig::class),
\OC::$server->getIntegrityCodeChecker(),
$logger,
\OC::$server->query(\OC\Installer::class)
Server::get(LoggerInterface::class),
Server::get(\OC\Installer::class)
);
$incompatibleApps = [];
$incompatibleOverwrites = $config->getSystemValue('app_install_overwrite', []);
Expand Down Expand Up @@ -189,7 +192,7 @@ function (MigratorExecuteSqlEvent $event) use ($eventSource, $l): void {
try {
$updater->upgrade();
} catch (\Exception $e) {
\OCP\Server::get(LoggerInterface::class)->error(
Server::get(LoggerInterface::class)->error(
$e->getMessage(),
[
'exception' => $e,
Expand Down
4 changes: 3 additions & 1 deletion lib/private/Setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJobList;
use OCP\Defaults;
use OCP\IAppConfig;
use OCP\IConfig;
use OCP\IGroup;
use OCP\IGroupManager;
Expand Down Expand Up @@ -382,7 +383,8 @@ public function install(array $options, ?IOutput $output = null): array {

$config = Server::get(IConfig::class);
$config->setAppValue('core', 'installedat', (string)microtime(true));
$config->setAppValue('core', 'lastupdatedat', (string)microtime(true));
$appConfig = Server::get(IAppConfig::class);
$appConfig->setValueInt('core', 'lastupdatedat', time());

$vendorData = $this->getVendorData();
$config->setAppValue('core', 'vendor', $vendorData['vendor']);
Expand Down
Loading

0 comments on commit 519e434

Please sign in to comment.