From 7ed583cb8ee64f77696b0e23f79d8d1b4038bcbc Mon Sep 17 00:00:00 2001 From: Côme Chilliet Date: Thu, 12 Sep 2024 16:17:19 +0200 Subject: chore: Migrate cleanAppId and getAppPath calls to IAppManager from OC_App MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- tests/lib/App/AppManagerTest.php | 4 ++-- tests/lib/InfoXmlTest.php | 12 ++++++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) (limited to 'tests/lib') diff --git a/tests/lib/App/AppManagerTest.php b/tests/lib/App/AppManagerTest.php index 82a3f0d2045..ac470c00335 100644 --- a/tests/lib/App/AppManagerTest.php +++ b/tests/lib/App/AppManagerTest.php @@ -341,7 +341,7 @@ class AppManagerTest extends TestCase { $manager->expects($this->once()) ->method('getAppPath') ->with('test') - ->willReturn(null); + ->willReturn(''); $manager->expects($this->once()) ->method('getAppInfo') @@ -402,7 +402,7 @@ class AppManagerTest extends TestCase { $manager->expects($this->once()) ->method('getAppPath') ->with('test') - ->willReturn(null); + ->willReturn(''); $manager->expects($this->once()) ->method('getAppInfo') diff --git a/tests/lib/InfoXmlTest.php b/tests/lib/InfoXmlTest.php index 702eca4c0ce..1527e363bd7 100644 --- a/tests/lib/InfoXmlTest.php +++ b/tests/lib/InfoXmlTest.php @@ -7,6 +7,7 @@ namespace Test; use OCP\App\IAppManager; +use OCP\Server; /** * Class InfoXmlTest @@ -15,6 +16,13 @@ use OCP\App\IAppManager; * @package Test */ class InfoXmlTest extends TestCase { + private IAppManager $appManager; + + protected function setUp(): void { + parent::setUp(); + $this->appManager = Server::get(IAppManager::class); + } + public function dataApps() { return [ ['admin_audit'], @@ -45,8 +53,8 @@ class InfoXmlTest extends TestCase { * @param string $app */ public function testClasses($app) { - $appInfo = \OCP\Server::get(IAppManager::class)->getAppInfo($app); - $appPath = \OC_App::getAppPath($app); + $appInfo = $this->appManager->getAppInfo($app); + $appPath = $this->appManager->getAppPath($app); \OC_App::registerAutoloading($app, $appPath); //Add the appcontainer -- cgit v1.2.3 From 76f2bc0bfc86a9d1ed34d37c574c7e7a327c0fab Mon Sep 17 00:00:00 2001 From: Côme Chilliet Date: Thu, 12 Sep 2024 16:38:35 +0200 Subject: fix: Replace OC_App::getAllApps with a method in AppManager MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- core/Command/App/Enable.php | 2 +- core/Command/App/GetPath.php | 2 +- core/Command/App/Update.php | 2 +- core/Command/Db/Migrations/GenerateCommand.php | 13 ++++----- .../Db/Migrations/GenerateMetadataCommand.php | 2 +- core/Command/L10n/CreateJs.php | 2 +- lib/private/App/AppManager.php | 31 ++++++++++++++++++++++ lib/private/IntegrityCheck/Checker.php | 4 +-- lib/private/IntegrityCheck/Helpers/AppLocator.php | 9 ------- lib/private/Server.php | 2 +- lib/private/legacy/OC_App.php | 28 +++---------------- lib/public/App/IAppManager.php | 8 ++++++ tests/lib/IntegrityCheck/CheckerTest.php | 4 +-- .../lib/IntegrityCheck/Helpers/AppLocatorTest.php | 4 --- 14 files changed, 58 insertions(+), 55 deletions(-) (limited to 'tests/lib') diff --git a/core/Command/App/Enable.php b/core/Command/App/Enable.php index b351a14c39e..5366230b841 100644 --- a/core/Command/App/Enable.php +++ b/core/Command/App/Enable.php @@ -145,7 +145,7 @@ class Enable extends Command implements CompletionAwareInterface { */ public function completeArgumentValues($argumentName, CompletionContext $context): array { if ($argumentName === 'app-id') { - $allApps = \OC_App::getAllApps(); + $allApps = $this->appManager->getAllAppsInAppsFolders(); return array_diff($allApps, \OC_App::getEnabledApps(true, true)); } return []; diff --git a/core/Command/App/GetPath.php b/core/Command/App/GetPath.php index 442af2f3570..3ba4ed7781b 100644 --- a/core/Command/App/GetPath.php +++ b/core/Command/App/GetPath.php @@ -63,7 +63,7 @@ class GetPath extends Base { */ public function completeArgumentValues($argumentName, CompletionContext $context): array { if ($argumentName === 'app') { - return \OC_App::getAllApps(); + return $this->appManager->getAllAppsInAppsFolders(); } return []; } diff --git a/core/Command/App/Update.php b/core/Command/App/Update.php index b4018c94b79..b2d02e222de 100644 --- a/core/Command/App/Update.php +++ b/core/Command/App/Update.php @@ -69,7 +69,7 @@ class Update extends Command { return 1; } } elseif ($input->getOption('all') || $input->getOption('showonly')) { - $apps = \OC_App::getAllApps(); + $apps = $this->manager->getAllAppsInAppsFolders(); } else { $output->writeln('Please specify an app to update or "--all" to update all updatable apps"'); return 1; diff --git a/core/Command/Db/Migrations/GenerateCommand.php b/core/Command/Db/Migrations/GenerateCommand.php index 62d5ad201ed..cd92dc5acd6 100644 --- a/core/Command/Db/Migrations/GenerateCommand.php +++ b/core/Command/Db/Migrations/GenerateCommand.php @@ -71,13 +71,10 @@ class {{classname}} extends SimpleMigrationStep { } '; - protected Connection $connection; - protected IAppManager $appManager; - - public function __construct(Connection $connection, IAppManager $appManager) { - $this->connection = $connection; - $this->appManager = $appManager; - + public function __construct( + protected Connection $connection, + protected IAppManager $appManager, + ) { parent::__construct(); } @@ -155,7 +152,7 @@ class {{classname}} extends SimpleMigrationStep { */ public function completeArgumentValues($argumentName, CompletionContext $context) { if ($argumentName === 'app') { - $allApps = \OC_App::getAllApps(); + $allApps = $this->appManager->getAllAppsInAppsFolders(); return array_diff($allApps, \OC_App::getEnabledApps(true, true)); } diff --git a/core/Command/Db/Migrations/GenerateMetadataCommand.php b/core/Command/Db/Migrations/GenerateMetadataCommand.php index 55a2a6aedab..ae83f92b29e 100644 --- a/core/Command/Db/Migrations/GenerateMetadataCommand.php +++ b/core/Command/Db/Migrations/GenerateMetadataCommand.php @@ -64,7 +64,7 @@ class GenerateMetadataCommand extends Command { * @throws \Exception */ private function extractMigrationMetadataFromApps(): array { - $allApps = \OC_App::getAllApps(); + $allApps = $this->appManager->getAllAppsInAppsFolders(); $metadata = []; foreach ($allApps as $appId) { // We need to load app before being able to extract Migrations diff --git a/core/Command/L10n/CreateJs.php b/core/Command/L10n/CreateJs.php index 27d537b093e..7d45fbe91f8 100644 --- a/core/Command/L10n/CreateJs.php +++ b/core/Command/L10n/CreateJs.php @@ -148,7 +148,7 @@ class CreateJs extends Command implements CompletionAwareInterface { */ public function completeArgumentValues($argumentName, CompletionContext $context) { if ($argumentName === 'app') { - return \OC_App::getAllApps(); + return $this->appManager->getAllAppsInAppsFolders(); } elseif ($argumentName === 'lang') { $appName = $context->getWordAtIndex($context->getWordIndex() - 1); try { diff --git a/lib/private/App/AppManager.php b/lib/private/App/AppManager.php index 974545cfe92..4ffddef98c3 100644 --- a/lib/private/App/AppManager.php +++ b/lib/private/App/AppManager.php @@ -155,6 +155,37 @@ class AppManager implements IAppManager { return array_keys($this->getInstalledAppsValues()); } + /** + * Get a list of all apps in the apps folder + * + * @return list an array of app names (string IDs) + */ + public function getAllAppsInAppsFolders(): array { + $apps = []; + + foreach (\OC::$APPSROOTS as $apps_dir) { + if (!is_readable($apps_dir['path'])) { + $this->logger->warning('unable to read app folder : ' . $apps_dir['path'], ['app' => 'core']); + continue; + } + $dh = opendir($apps_dir['path']); + + if (is_resource($dh)) { + while (($file = readdir($dh)) !== false) { + if ( + $file[0] != '.' && + is_dir($apps_dir['path'] . '/' . $file) && + is_file($apps_dir['path'] . '/' . $file . '/appinfo/info.xml') + ) { + $apps[] = $file; + } + } + } + } + + return array_values(array_unique($apps)); + } + /** * List all apps enabled for a user * diff --git a/lib/private/IntegrityCheck/Checker.php b/lib/private/IntegrityCheck/Checker.php index d38ccf497f4..6443c43d210 100644 --- a/lib/private/IntegrityCheck/Checker.php +++ b/lib/private/IntegrityCheck/Checker.php @@ -46,7 +46,7 @@ class Checker { private ?IConfig $config, private ?IAppConfig $appConfig, ICacheFactory $cacheFactory, - private ?IAppManager $appManager, + private IAppManager $appManager, private IMimeTypeDetector $mimeTypeDetector, ) { $this->cache = $cacheFactory->createDistributed(self::CACHE_KEY); @@ -536,7 +536,7 @@ class Checker { public function runInstanceVerification() { $this->cleanResults(); $this->verifyCoreSignature(); - $appIds = $this->appLocator->getAllApps(); + $appIds = $this->appManager->getAllAppsInAppsFolders(); foreach ($appIds as $appId) { // If an application is shipped a valid signature is required $isShipped = $this->appManager->isShipped($appId); diff --git a/lib/private/IntegrityCheck/Helpers/AppLocator.php b/lib/private/IntegrityCheck/Helpers/AppLocator.php index 3da5cc13227..148a3aeda76 100644 --- a/lib/private/IntegrityCheck/Helpers/AppLocator.php +++ b/lib/private/IntegrityCheck/Helpers/AppLocator.php @@ -30,13 +30,4 @@ class AppLocator { } return $path; } - - /** - * Providers \OC_App::getAllApps() - * - * @return array - */ - public function getAllApps(): array { - return \OC_App::getAllApps(); - } } diff --git a/lib/private/Server.php b/lib/private/Server.php index b0ccb0f4b4d..c514a4b93ff 100644 --- a/lib/private/Server.php +++ b/lib/private/Server.php @@ -954,10 +954,10 @@ class Server extends ServerContainer implements IServerContainer { if (\OC::$server->get(SystemConfig::class)->getValue('installed', false)) { $config = $c->get(\OCP\IConfig::class); $appConfig = $c->get(\OCP\IAppConfig::class); - $appManager = $c->get(IAppManager::class); } else { $config = $appConfig = $appManager = null; } + $appManager = $c->get(IAppManager::class); return new Checker( new EnvironmentHelper(), diff --git a/lib/private/legacy/OC_App.php b/lib/private/legacy/OC_App.php index d72d5fe8522..ef84d35d7bc 100644 --- a/lib/private/legacy/OC_App.php +++ b/lib/private/legacy/OC_App.php @@ -468,30 +468,10 @@ class OC_App { * get a list of all apps in the apps folder * * @return string[] an array of app names (string IDs) - * @todo: change the name of this method to getInstalledApps, which is more accurate + * @deprecated 31.0.0 Use IAppManager::getAllAppsInAppsFolders instead */ public static function getAllApps(): array { - $apps = []; - - foreach (OC::$APPSROOTS as $apps_dir) { - if (!is_readable($apps_dir['path'])) { - \OCP\Server::get(LoggerInterface::class)->warning('unable to read app folder : ' . $apps_dir['path'], ['app' => 'core']); - continue; - } - $dh = opendir($apps_dir['path']); - - if (is_resource($dh)) { - while (($file = readdir($dh)) !== false) { - if ($file[0] != '.' and is_dir($apps_dir['path'] . '/' . $file) and is_file($apps_dir['path'] . '/' . $file . '/appinfo/info.xml')) { - $apps[] = $file; - } - } - } - } - - $apps = array_unique($apps); - - return $apps; + return \OCP\Server::get(IAppManager::class)->getAllAppsInAppsFolders(); } /** @@ -512,9 +492,9 @@ class OC_App { * @return array */ public function listAllApps(): array { - $installedApps = OC_App::getAllApps(); - $appManager = \OC::$server->getAppManager(); + + $installedApps = $appManager->getAllAppsInAppsFolders(); //we don't want to show configuration for these $blacklist = $appManager->getAlwaysEnabledApps(); $appList = []; diff --git a/lib/public/App/IAppManager.php b/lib/public/App/IAppManager.php index 68c3cc771f4..580288fbff7 100644 --- a/lib/public/App/IAppManager.php +++ b/lib/public/App/IAppManager.php @@ -292,4 +292,12 @@ interface IAppManager { * @since 31.0.0 */ public function cleanAppId(string $app): string; + + /** + * Get a list of all apps in the apps folder + * + * @return list an array of app names (string IDs) + * @since 31.0.0 + */ + public function getAllAppsInAppsFolders(): array; } diff --git a/tests/lib/IntegrityCheck/CheckerTest.php b/tests/lib/IntegrityCheck/CheckerTest.php index bf4ea16f564..05607f8113c 100644 --- a/tests/lib/IntegrityCheck/CheckerTest.php +++ b/tests/lib/IntegrityCheck/CheckerTest.php @@ -1030,9 +1030,9 @@ class CheckerTest extends TestCase { $this->checker ->expects($this->once()) ->method('verifyCoreSignature'); - $this->appLocator + $this->appManager ->expects($this->once()) - ->method('getAllApps') + ->method('getAllAppsInAppsFolders') ->willReturn([ 'files', 'calendar', diff --git a/tests/lib/IntegrityCheck/Helpers/AppLocatorTest.php b/tests/lib/IntegrityCheck/Helpers/AppLocatorTest.php index a86507f6bb2..d99a42e0d63 100644 --- a/tests/lib/IntegrityCheck/Helpers/AppLocatorTest.php +++ b/tests/lib/IntegrityCheck/Helpers/AppLocatorTest.php @@ -30,8 +30,4 @@ class AppLocatorTest extends TestCase { $this->locator->getAppPath('aTotallyNotExistingApp'); } - - public function testGetAllApps() { - $this->assertSame(\OC_App::getAllApps(), $this->locator->getAllApps()); - } } -- cgit v1.2.3 From 7a16d01ea79bf8effedd5e0e9ac7c2e4b4ea41f8 Mon Sep 17 00:00:00 2001 From: Côme Chilliet Date: Fri, 13 Sep 2024 10:26:36 +0200 Subject: chore(tests): Fix Router test by mocking AppManager methods correctly MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- tests/lib/Route/RouterTest.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'tests/lib') diff --git a/tests/lib/Route/RouterTest.php b/tests/lib/Route/RouterTest.php index 713d90d3c20..8647bb0f2f6 100644 --- a/tests/lib/Route/RouterTest.php +++ b/tests/lib/Route/RouterTest.php @@ -13,6 +13,7 @@ use OCP\App\IAppManager; use OCP\Diagnostics\IEventLogger; use OCP\IConfig; use OCP\IRequest; +use PHPUnit\Framework\MockObject\MockObject; use Psr\Container\ContainerInterface; use Psr\Log\LoggerInterface; use Test\TestCase; @@ -26,6 +27,8 @@ use Test\TestCase; */ class RouterTest extends TestCase { private Router $router; + private IAppManager&MockObject $appManager; + protected function setUp(): void { parent::setUp(); /** @var LoggerInterface $logger */ @@ -36,13 +39,16 @@ class RouterTest extends TestCase { $this->fail('Unexpected info log: '.(string)($data['exception'] ?? $message)); } ); + + $this->appManager = $this->createMock(IAppManager::class); + $this->router = new Router( $logger, $this->createMock(IRequest::class), $this->createMock(IConfig::class), $this->createMock(IEventLogger::class), $this->createMock(ContainerInterface::class), - $this->createMock(IAppManager::class), + $this->appManager, ); } @@ -51,6 +57,12 @@ class RouterTest extends TestCase { } public function testGenerateConsecutively(): void { + $this->appManager->expects(self::atLeastOnce()) + ->method('cleanAppId') + ->willReturnArgument(0); + $this->appManager->expects(self::atLeastOnce()) + ->method('getAppPath') + ->willReturnCallback(fn (string $appid): string => \OC::$SERVERROOT . '/apps/' . $appid); $this->assertEquals('/index.php/apps/files/', $this->router->generate('files.view.index')); -- cgit v1.2.3