Skip to content

Commit ae4d38e

Browse files
committed
fix: Allow hyphen in appid
It’s rare but exists for some apps not in the appstore. Also added unit tests for cleanAppId and fixed small issues with it. Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
1 parent 0baaebd commit ae4d38e

2 files changed

Lines changed: 27 additions & 5 deletions

File tree

lib/private/App/AppManager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1021,7 +1021,7 @@ public function isBackendRequired(string $backend): bool {
10211021
*/
10221022
public function cleanAppId(string $app): string {
10231023
/* Only lowercase alphanumeric is allowed */
1024-
return preg_replace('/(^[0-9_]|[^a-z0-9_]+|_$)/', '', $app);
1024+
return preg_replace('/(^[0-9_-]+|[^a-z0-9_-]+|[_-]+$)/', '', $app);
10251025
}
10261026

10271027
/**

tests/lib/App/AppManagerTest.php

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use OCP\IUser;
2929
use OCP\IUserSession;
3030
use OCP\ServerVersion;
31+
use PHPUnit\Framework\Attributes\DataProvider;
3132
use PHPUnit\Framework\MockObject\MockObject;
3233
use Psr\Log\LoggerInterface;
3334
use Test\TestCase;
@@ -143,7 +144,7 @@ protected function setUp(): void {
143144
);
144145
}
145146

146-
#[\PHPUnit\Framework\Attributes\DataProvider('dataGetAppIcon')]
147+
#[DataProvider('dataGetAppIcon')]
147148
public function testGetAppIcon($callback, ?bool $dark, ?string $expected): void {
148149
$this->urlGenerator->expects($this->atLeastOnce())
149150
->method('imagePath')
@@ -314,7 +315,7 @@ public static function dataEnableAppForGroupsAllowedTypes(): array {
314315
/**
315316
* @param array $appInfo
316317
*/
317-
#[\PHPUnit\Framework\Attributes\DataProvider('dataEnableAppForGroupsAllowedTypes')]
318+
#[DataProvider('dataEnableAppForGroupsAllowedTypes')]
318319
public function testEnableAppForGroupsAllowedTypes(array $appInfo): void {
319320
$group1 = $this->createMock(IGroup::class);
320321
$group1->method('getGID')
@@ -375,7 +376,7 @@ public static function dataEnableAppForGroupsForbiddenTypes(): array {
375376
* @param string $type
376377
*
377378
*/
378-
#[\PHPUnit\Framework\Attributes\DataProvider('dataEnableAppForGroupsForbiddenTypes')]
379+
#[DataProvider('dataEnableAppForGroupsForbiddenTypes')]
379380
public function testEnableAppForGroupsForbiddenTypes($type): void {
380381
$this->expectException(\Exception::class);
381382
$this->expectExceptionMessage('test can\'t be enabled for groups.');
@@ -781,7 +782,7 @@ public static function isBackendRequiredDataProvider(): array {
781782
];
782783
}
783784

784-
#[\PHPUnit\Framework\Attributes\DataProvider('isBackendRequiredDataProvider')]
785+
#[DataProvider('isBackendRequiredDataProvider')]
785786
public function testIsBackendRequired(
786787
string $backend,
787788
array $appBackends,
@@ -896,4 +897,25 @@ public function testGetAppVersionUnknown() {
896897
$manager->getAppVersion('unknown'),
897898
);
898899
}
900+
901+
public static function dataCleanAppId(): array {
902+
return [
903+
['simple', 'simple'],
904+
['UPPERCASEa', 'a'],
905+
['MixEdCaSe', 'ixdae'],
906+
['007startwithdigit', 'startwithdigit'],
907+
['0-numb3rs-4ll0w3d-1n-m1ddle-0', 'numb3rs-4ll0w3d-1n-m1ddle-0'],
908+
['hyphen-and_underscore_allowed', 'hyphen-and_underscore_allowed'],
909+
['_but-not-at-the-end_', 'but-not-at-the-end'],
910+
['-but-not-at-the-end-', 'but-not-at-the-end'],
911+
['--_but-not-at-the-end___', 'but-not-at-the-end'],
912+
[' also remove all spaces', 'alsoremoveallspaces'],
913+
['a«"«»()@+-/*=%\{}…~|&œ—<>[]^±_−÷×≠‰A', 'a-_'],
914+
];
915+
}
916+
917+
#[DataProvider('dataCleanAppId')]
918+
public function testCleanAppId(string $inputString, string $appid): void {
919+
$this->assertEquals($appid, $this->manager->cleanAppId($inputString));
920+
}
899921
}

0 commit comments

Comments
 (0)