diff --git a/lib/private/App/AppManager.php b/lib/private/App/AppManager.php index 2ff8fed6b17..2ecb846f40f 100644 --- a/lib/private/App/AppManager.php +++ b/lib/private/App/AppManager.php @@ -1021,7 +1021,7 @@ class AppManager implements IAppManager { */ public function cleanAppId(string $app): string { /* Only lowercase alphanumeric is allowed */ - return preg_replace('/(^[0-9_]|[^a-z0-9_]+|_$)/', '', $app); + return preg_replace('/(^[0-9_-]+|[^a-z0-9_-]+|[_-]+$)/', '', $app); } /** diff --git a/tests/lib/App/AppManagerTest.php b/tests/lib/App/AppManagerTest.php index dcaeef90ff5..2a5871cb405 100644 --- a/tests/lib/App/AppManagerTest.php +++ b/tests/lib/App/AppManagerTest.php @@ -28,6 +28,7 @@ use OCP\IURLGenerator; use OCP\IUser; use OCP\IUserSession; use OCP\ServerVersion; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\MockObject\MockObject; use Psr\Log\LoggerInterface; use Test\TestCase; @@ -143,7 +144,7 @@ class AppManagerTest extends TestCase { ); } - #[\PHPUnit\Framework\Attributes\DataProvider('dataGetAppIcon')] + #[DataProvider('dataGetAppIcon')] public function testGetAppIcon($callback, ?bool $dark, ?string $expected): void { $this->urlGenerator->expects($this->atLeastOnce()) ->method('imagePath') @@ -314,7 +315,7 @@ class AppManagerTest extends TestCase { /** * @param array $appInfo */ - #[\PHPUnit\Framework\Attributes\DataProvider('dataEnableAppForGroupsAllowedTypes')] + #[DataProvider('dataEnableAppForGroupsAllowedTypes')] public function testEnableAppForGroupsAllowedTypes(array $appInfo): void { $group1 = $this->createMock(IGroup::class); $group1->method('getGID') @@ -375,7 +376,7 @@ class AppManagerTest extends TestCase { * @param string $type * */ - #[\PHPUnit\Framework\Attributes\DataProvider('dataEnableAppForGroupsForbiddenTypes')] + #[DataProvider('dataEnableAppForGroupsForbiddenTypes')] public function testEnableAppForGroupsForbiddenTypes($type): void { $this->expectException(\Exception::class); $this->expectExceptionMessage('test can\'t be enabled for groups.'); @@ -781,7 +782,7 @@ class AppManagerTest extends TestCase { ]; } - #[\PHPUnit\Framework\Attributes\DataProvider('isBackendRequiredDataProvider')] + #[DataProvider('isBackendRequiredDataProvider')] public function testIsBackendRequired( string $backend, array $appBackends, @@ -896,4 +897,25 @@ class AppManagerTest extends TestCase { $manager->getAppVersion('unknown'), ); } + + public static function dataCleanAppId(): array { + return [ + ['simple', 'simple'], + ['UPPERCASEa', 'a'], + ['MixEdCaSe', 'ixdae'], + ['007startwithdigit', 'startwithdigit'], + ['0-numb3rs-4ll0w3d-1n-m1ddle-0', 'numb3rs-4ll0w3d-1n-m1ddle-0'], + ['hyphen-and_underscore_allowed', 'hyphen-and_underscore_allowed'], + ['_but-not-at-the-end_', 'but-not-at-the-end'], + ['-but-not-at-the-end-', 'but-not-at-the-end'], + ['--_but-not-at-the-end___', 'but-not-at-the-end'], + [' also remove all spaces', 'alsoremoveallspaces'], + ['a«"«»()@+-/*=%\{}…~|&œ—<>[]^±_−÷×≠‰A', 'a-_'], + ]; + } + + #[DataProvider('dataCleanAppId')] + public function testCleanAppId(string $inputString, string $appid): void { + $this->assertEquals($appid, $this->manager->cleanAppId($inputString)); + } }