From 6aa675c6bbf1cfb1f5427a80d47103fc2d28d3be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Thu, 10 Jul 2025 12:05:45 +0200 Subject: [PATCH] fix: Cleanup OC_App uses in Updater class MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- core/ajax/update.php | 13 +------------ lib/private/Updater.php | 28 +++++++++++++--------------- tests/lib/UpdaterTest.php | 6 +++++- 3 files changed, 19 insertions(+), 28 deletions(-) diff --git a/core/ajax/update.php b/core/ajax/update.php index 69665cf62df..22bbdcff3e0 100644 --- a/core/ajax/update.php +++ b/core/ajax/update.php @@ -7,8 +7,6 @@ */ use OC\Core\Listener\FeedBackHandler; use OC\DB\MigratorExecuteSqlEvent; -use OC\Installer; -use OC\IntegrityCheck\Checker; use OC\Repair\Events\RepairAdvanceEvent; use OC\Repair\Events\RepairErrorEvent; use OC\Repair\Events\RepairFinishEvent; @@ -20,13 +18,11 @@ use OC\SystemConfig; use OC\Updater; use OCP\EventDispatcher\Event; use OCP\EventDispatcher\IEventDispatcher; -use OCP\IAppConfig; use OCP\IConfig; use OCP\IEventSourceFactory; use OCP\IL10N; use OCP\L10N\IFactory; use OCP\Server; -use OCP\ServerVersion; use OCP\Util; use Psr\Log\LoggerInterface; @@ -58,14 +54,7 @@ if (Util::needUpgrade()) { \OC_User::setIncognitoMode(true); $config = Server::get(IConfig::class); - $updater = new Updater( - Server::get(ServerVersion::class), - $config, - Server::get(IAppConfig::class), - Server::get(Checker::class), - Server::get(LoggerInterface::class), - Server::get(Installer::class) - ); + $updater = Server::get(Updater::class); $incompatibleApps = []; $incompatibleOverwrites = $config->getSystemValue('app_install_overwrite', []); diff --git a/lib/private/Updater.php b/lib/private/Updater.php index 9cd33863612..17b740a0b65 100644 --- a/lib/private/Updater.php +++ b/lib/private/Updater.php @@ -60,6 +60,7 @@ class Updater extends BasicEmitter { private Checker $checker, private ?LoggerInterface $log, private Installer $installer, + private IAppManager $appManager, ) { } @@ -238,12 +239,10 @@ class Updater extends BasicEmitter { // Update the appfetchers version so it downloads the correct list from the appstore \OC::$server->get(AppFetcher::class)->setVersion($currentVersion); - /** @var AppManager $appManager */ - $appManager = \OC::$server->getAppManager(); - // upgrade appstore apps - $this->upgradeAppStoreApps($appManager->getEnabledApps()); - $autoDisabledApps = $appManager->getAutoDisabledApps(); + $this->upgradeAppStoreApps($this->appManager->getEnabledApps()); + /** @var AppManager $this->appManager */ + $autoDisabledApps = $this->appManager->getAutoDisabledApps(); if (!empty($autoDisabledApps)) { $this->upgradeAppStoreApps(array_keys($autoDisabledApps), $autoDisabledApps); } @@ -296,7 +295,7 @@ class Updater extends BasicEmitter { * @throws NeedsUpdateException */ protected function doAppUpgrade(): void { - $apps = \OC_App::getEnabledApps(); + $apps = $this->appManager->getEnabledApps(); $priorityTypes = ['authentication', 'extended_authentication', 'filesystem', 'logging']; $pseudoOtherType = 'other'; $stacks = [$pseudoOtherType => []]; @@ -307,7 +306,7 @@ class Updater extends BasicEmitter { if (!isset($stacks[$type])) { $stacks[$type] = []; } - if (\OC_App::isType($appId, [$type])) { + if ($this->appManager->isType($appId, [$type])) { $stacks[$type][] = $appId; $priorityType = true; break; @@ -321,15 +320,15 @@ class Updater extends BasicEmitter { $stack = $stacks[$type]; foreach ($stack as $appId) { if (\OC_App::shouldUpgrade($appId)) { - $this->emit('\OC\Updater', 'appUpgradeStarted', [$appId, \OCP\Server::get(IAppManager::class)->getAppVersion($appId)]); + $this->emit('\OC\Updater', 'appUpgradeStarted', [$appId, $this->appManager->getAppVersion($appId)]); \OC_App::updateApp($appId); - $this->emit('\OC\Updater', 'appUpgrade', [$appId, \OCP\Server::get(IAppManager::class)->getAppVersion($appId)]); + $this->emit('\OC\Updater', 'appUpgrade', [$appId, $this->appManager->getAppVersion($appId)]); } if ($type !== $pseudoOtherType) { // load authentication, filesystem and logging apps after // upgrading them. Other apps my need to rely on modifying // user and/or filesystem aspects. - \OC_App::loadApp($appId); + $this->appManager->loadApp($appId); } } } @@ -345,17 +344,16 @@ class Updater extends BasicEmitter { */ private function checkAppsRequirements(): void { $isCoreUpgrade = $this->isCodeUpgrade(); - $apps = OC_App::getEnabledApps(); + $apps = $this->appManager->getEnabledApps(); $version = implode('.', Util::getVersion()); - $appManager = \OC::$server->getAppManager(); foreach ($apps as $app) { // check if the app is compatible with this version of Nextcloud - $info = $appManager->getAppInfo($app); + $info = $this->appManager->getAppInfo($app); if ($info === null || !OC_App::isAppCompatible($version, $info)) { - if ($appManager->isShipped($app)) { + if ($this->appManager->isShipped($app)) { throw new \UnexpectedValueException('The files of the app "' . $app . '" were not correctly replaced before running the update'); } - $appManager->disableApp($app, true); + $this->appManager->disableApp($app, true); $this->emit('\OC\Updater', 'incompatibleAppDisabled', [$app]); } } diff --git a/tests/lib/UpdaterTest.php b/tests/lib/UpdaterTest.php index 37a4a105628..23a4d5329c8 100644 --- a/tests/lib/UpdaterTest.php +++ b/tests/lib/UpdaterTest.php @@ -11,6 +11,7 @@ namespace Test; use OC\Installer; use OC\IntegrityCheck\Checker; use OC\Updater; +use OCP\App\IAppManager; use OCP\IAppConfig; use OCP\IConfig; use OCP\ServerVersion; @@ -32,6 +33,7 @@ class UpdaterTest extends TestCase { private $checker; /** @var Installer|MockObject */ private $installer; + private IAppManager&MockObject $appManager; protected function setUp(): void { parent::setUp(); @@ -41,6 +43,7 @@ class UpdaterTest extends TestCase { $this->logger = $this->createMock(LoggerInterface::class); $this->checker = $this->createMock(Checker::class); $this->installer = $this->createMock(Installer::class); + $this->appManager = $this->createMock(IAppManager::class); $this->updater = new Updater( $this->serverVersion, @@ -48,7 +51,8 @@ class UpdaterTest extends TestCase { $this->appConfig, $this->checker, $this->logger, - $this->installer + $this->installer, + $this->appManager, ); }