Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix: Correctly return app id and app version for core styles and im…
…ages

Signed-off-by: Ferdinand Thiessen <[email protected]>
  • Loading branch information
susnux authored and backportbot[bot] committed Jan 24, 2025
commit ae3e26024e505cfae3196471525a701a2e87e7dd
10 changes: 8 additions & 2 deletions lib/private/App/AppManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\IUserSession;
use OCP\ServerVersion;
use OCP\Settings\IManager as ISettingsManager;
use Psr\Log\LoggerInterface;

Expand Down Expand Up @@ -80,6 +81,7 @@ public function __construct(
private ICacheFactory $memCacheFactory,
private IEventDispatcher $dispatcher,
private LoggerInterface $logger,
private ServerVersion $serverVersion,
) {
}

Expand Down Expand Up @@ -786,8 +788,12 @@ public function getAppInfoByPath(string $path, ?string $lang = null): ?array {

public function getAppVersion(string $appId, bool $useCache = true): string {
if (!$useCache || !isset($this->appVersions[$appId])) {
$appInfo = $this->getAppInfo($appId);
$this->appVersions[$appId] = ($appInfo !== null && isset($appInfo['version'])) ? $appInfo['version'] : '0';
if ($appId === 'core') {
$this->appVersions[$appId] = $this->serverVersion->getVersionString();
} else {
$appInfo = $this->getAppInfo($appId);
$this->appVersions[$appId] = ($appInfo !== null && isset($appInfo['version'])) ? $appInfo['version'] : '0';
}
}
return $this->appVersions[$appId];
}
Expand Down
1 change: 1 addition & 0 deletions lib/private/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,7 @@ public function __construct($webRoot, \OC\Config $config) {
$c->get(ICacheFactory::class),
$c->get(IEventDispatcher::class),
$c->get(LoggerInterface::class),
$c->get(ServerVersion::class),
);
});
$this->registerAlias(IAppManager::class, AppManager::class);
Expand Down
15 changes: 6 additions & 9 deletions lib/private/TemplateLayout.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,12 +304,7 @@ public function __construct($renderAs, $appId = '') {
$this->assign('id-app-navigation', $renderAs === TemplateResponse::RENDER_AS_USER ? '#app-navigation' : null);
}

/**
* @param string $path
* @param string $file
* @return string
*/
protected function getVersionHashSuffix($path = false, $file = false) {
protected function getVersionHashSuffix(string $path = '', string $file = ''): string {
if ($this->config->getSystemValueBool('debug', false)) {
// allows chrome workspace mapping in debug mode
return '';
Expand All @@ -322,11 +317,11 @@ protected function getVersionHashSuffix($path = false, $file = false) {

$hash = false;
// Try the web-root first
if (is_string($path) && $path !== '') {
if ($path !== '') {
$hash = $this->getVersionHashByPath($path);
}
// If not found try the file
if ($hash === false && is_string($file) && $file !== '') {
if ($hash === false && $file !== '') {
$hash = $this->getVersionHashByPath($file);
}
// As a last resort we use the server version hash
Expand Down Expand Up @@ -376,14 +371,16 @@ public static function findStylesheetFiles($styles, $compileScss = true) {

/**
* @param string $path
* @return string|boolean
* @return string|false
*/
public function getAppNamefromPath($path) {
if ($path !== '' && is_string($path)) {
$pathParts = explode('/', $path);
if ($pathParts[0] === 'css') {
// This is a scss request
return $pathParts[1];
} elseif ($pathParts[0] === 'core') {
return 'core';
}
return end($pathParts);
}
Expand Down
103 changes: 103 additions & 0 deletions tests/lib/App/AppManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\IUserSession;
use OCP\ServerVersion;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
use Test\TestCase;
Expand Down Expand Up @@ -100,6 +101,8 @@ protected function getAppConfig() {

protected IURLGenerator&MockObject $urlGenerator;

protected ServerVersion&MockObject $serverVersion;

/** @var IAppManager */
protected $manager;

Expand All @@ -115,6 +118,7 @@ protected function setUp(): void {
$this->eventDispatcher = $this->createMock(IEventDispatcher::class);
$this->logger = $this->createMock(LoggerInterface::class);
$this->urlGenerator = $this->createMock(IURLGenerator::class);
$this->serverVersion = $this->createMock(ServerVersion::class);

$this->overwriteService(AppConfig::class, $this->appConfig);
$this->overwriteService(IURLGenerator::class, $this->urlGenerator);
Expand All @@ -136,6 +140,7 @@ protected function setUp(): void {
$this->cacheFactory,
$this->eventDispatcher,
$this->logger,
$this->serverVersion,
);
}

Expand Down Expand Up @@ -278,6 +283,7 @@ public function testEnableAppForGroups(): void {
$this->cacheFactory,
$this->eventDispatcher,
$this->logger,
$this->serverVersion,
])
->onlyMethods([
'getAppPath',
Expand Down Expand Up @@ -331,6 +337,7 @@ public function testEnableAppForGroupsAllowedTypes(array $appInfo): void {
$this->cacheFactory,
$this->eventDispatcher,
$this->logger,
$this->serverVersion,
])
->onlyMethods([
'getAppPath',
Expand Down Expand Up @@ -392,6 +399,7 @@ public function testEnableAppForGroupsForbiddenTypes($type): void {
$this->cacheFactory,
$this->eventDispatcher,
$this->logger,
$this->serverVersion,
])
->onlyMethods([
'getAppPath',
Expand Down Expand Up @@ -596,6 +604,7 @@ public function testGetAppsNeedingUpgrade(): void {
$this->cacheFactory,
$this->eventDispatcher,
$this->logger,
$this->serverVersion,
])
->onlyMethods(['getAppInfo'])
->getMock();
Expand Down Expand Up @@ -655,6 +664,7 @@ public function testGetIncompatibleApps(): void {
$this->cacheFactory,
$this->eventDispatcher,
$this->logger,
$this->serverVersion,
])
->onlyMethods(['getAppInfo'])
->getMock();
Expand Down Expand Up @@ -785,4 +795,97 @@ public function testIsBackendRequired(

$this->assertEquals($expected, $this->manager->isBackendRequired($backend));
}

public function testGetAppVersion() {
$manager = $this->getMockBuilder(AppManager::class)
->setConstructorArgs([
$this->userSession,
$this->config,
$this->groupManager,
$this->cacheFactory,
$this->eventDispatcher,
$this->logger,
$this->serverVersion,
])
->onlyMethods([
'getAppInfo',
])
->getMock();

$manager->expects(self::once())
->method('getAppInfo')
->with('myapp')
->willReturn(['version' => '99.99.99-rc.99']);

$this->serverVersion
->expects(self::never())
->method('getVersionString');

$this->assertEquals(
'99.99.99-rc.99',
$manager->getAppVersion('myapp'),
);
}

public function testGetAppVersionCore() {
$manager = $this->getMockBuilder(AppManager::class)
->setConstructorArgs([
$this->userSession,
$this->config,
$this->groupManager,
$this->cacheFactory,
$this->eventDispatcher,
$this->logger,
$this->serverVersion,
])
->onlyMethods([
'getAppInfo',
])
->getMock();

$manager->expects(self::never())
->method('getAppInfo');

$this->serverVersion
->expects(self::once())
->method('getVersionString')
->willReturn('1.2.3-beta.4');

$this->assertEquals(
'1.2.3-beta.4',
$manager->getAppVersion('core'),
);
}

public function testGetAppVersionUnknown() {
$manager = $this->getMockBuilder(AppManager::class)
->setConstructorArgs([
$this->userSession,
$this->config,
$this->groupManager,
$this->cacheFactory,
$this->eventDispatcher,
$this->logger,
$this->serverVersion,
])
->onlyMethods([
'getAppInfo',
])
->getMock();

$manager->expects(self::once())
->method('getAppInfo')
->with('unknown')
->willReturn(null);

$this->serverVersion
->expects(self::never())
->method('getVersionString');

$this->assertEquals(
'0',
$manager->getAppVersion('unknown'),
);
}

}