Skip to content

Commit

Permalink
feat(info.xml): add backends options to indicate used backends
Browse files Browse the repository at this point in the history
At the moment, this is only used for the caldav backend to hide its
admin settings section if no app actually uses the caldav backend.

Signed-off-by: Richard Steinmetz <richard@steinmetz.cloud>
  • Loading branch information
st3iny committed Jul 13, 2024
1 parent 4b296c7 commit 3c01364
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 10 deletions.
14 changes: 9 additions & 5 deletions apps/dav/lib/Settings/CalDAVSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace OCA\DAV\Settings;

use OCA\DAV\AppInfo\Application;
use OCP\App\IAppManager;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Services\IInitialState;
use OCP\IConfig;
Expand All @@ -21,6 +22,7 @@ class CalDAVSettings implements IDelegatedSettings {
private $initialState;

private IURLGenerator $urlGenerator;
private IAppManager $appManager;

private const defaults = [
'sendInvitations' => 'yes',
Expand All @@ -36,10 +38,11 @@ class CalDAVSettings implements IDelegatedSettings {
* @param IConfig $config
* @param IInitialState $initialState
*/
public function __construct(IConfig $config, IInitialState $initialState, IURLGenerator $urlGenerator) {
public function __construct(IConfig $config, IInitialState $initialState, IURLGenerator $urlGenerator, IAppManager $appManager) {
$this->config = $config;
$this->initialState = $initialState;
$this->urlGenerator = $urlGenerator;
$this->appManager = $appManager;
}

public function getForm(): TemplateResponse {
Expand All @@ -51,10 +54,11 @@ public function getForm(): TemplateResponse {
return new TemplateResponse(Application::APP_ID, 'settings-admin-caldav');
}

/**
* @return string
*/
public function getSection() {
public function getSection(): ?string {
if (!$this->appManager->isBackendRequired(IAppManager::BACKEND_CALDAV)) {
return null;
}

return 'groupware';
}

Expand Down
20 changes: 19 additions & 1 deletion apps/dav/tests/unit/Settings/CalDAVSettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace OCA\DAV\Tests\Unit\DAV\Settings;

use OCA\DAV\Settings\CalDAVSettings;
use OCP\App\IAppManager;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Services\IInitialState;
use OCP\IConfig;
Expand All @@ -24,6 +25,9 @@ class CalDAVSettingsTest extends TestCase {
/** @var IURLGenerator|MockObject */
private $urlGenerator;

/** @var IAppManager|MockObject */
private $appManager;

private CalDAVSettings $settings;

protected function setUp(): void {
Expand All @@ -32,7 +36,8 @@ protected function setUp(): void {
$this->config = $this->createMock(IConfig::class);
$this->initialState = $this->createMock(IInitialState::class);
$this->urlGenerator = $this->createMock(IURLGenerator::class);
$this->settings = new CalDAVSettings($this->config, $this->initialState, $this->urlGenerator);
$this->appManager = $this->createMock(IAppManager::class);
$this->settings = new CalDAVSettings($this->config, $this->initialState, $this->urlGenerator, $this->appManager);
}

public function testGetForm(): void {
Expand Down Expand Up @@ -65,10 +70,23 @@ public function testGetForm(): void {
}

public function testGetSection(): void {
$this->appManager->expects(self::once())
->method('isBackendRequired')
->with(IAppManager::BACKEND_CALDAV)
->willReturn(true);
$this->assertEquals('groupware', $this->settings->getSection());
}

public function testGetSectionWithoutCaldavBackend(): void {
$this->appManager->expects(self::once())
->method('isBackendRequired')
->with(IAppManager::BACKEND_CALDAV)
->willReturn(false);
$this->assertEquals(null, $this->settings->getSection());
}

public function testGetPriority(): void {
$this->assertEquals(10, $this->settings->getPriority());
}

}
10 changes: 10 additions & 0 deletions lib/private/App/AppManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -875,4 +875,14 @@ public function setDefaultApps(array $defaultApps): void {

$this->config->setSystemValue('defaultapp', join(',', $defaultApps));
}

public function isBackendRequired(string $backend): bool {
foreach ($this->appInfos as $appInfo) {
if (isset($appInfo['backends'][$backend])) {
return true;
}
}

return false;
}
}
3 changes: 3 additions & 0 deletions lib/private/App/InfoParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ public function parse($file) {
if (!array_key_exists('personal-section', $array['settings'])) {
$array['settings']['personal-section'] = [];
}
if (!array_key_exists('backends', $array)) {
$array['backends'] = [];
}

if (array_key_exists('types', $array)) {
if (is_array($array['types'])) {
Expand Down
15 changes: 15 additions & 0 deletions lib/public/App/IAppManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
* @since 8.0.0
*/
interface IAppManager {
/**
* @since 30.0.0
*/
const BACKEND_CALDAV = 'caldav';

/**
* Returns the app information from "appinfo/info.xml".
*
Expand Down Expand Up @@ -261,4 +266,14 @@ public function getDefaultApps(): array;
* @since 28.0.0
*/
public function setDefaultApps(array $defaultApps): void;

/**
* Check whether the given backend is required by at least one app.
*
* @param self::BACKEND_* $backend Name of the backend, one of `self::BACKEND_*`
* @return bool True if at least one app requires the backend
*
* @since 30.0.0
*/
public function isBackendRequired(string $backend): bool;
}
3 changes: 3 additions & 0 deletions tests/data/app/expected-info.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,8 @@
"admin-section": [],
"personal": [],
"personal-section": []
},
"backends": {
"caldav": ""
}
}
5 changes: 3 additions & 2 deletions tests/data/app/navigation-one-item.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,6 @@
"live-migration": [],
"uninstall": []
},
"two-factor-providers": []
}
"two-factor-providers": [],
"backends": {}
}
5 changes: 3 additions & 2 deletions tests/data/app/navigation-two-items.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,6 @@
"live-migration": [],
"uninstall": []
},
"two-factor-providers": []
}
"two-factor-providers": [],
"backends": {}
}
3 changes: 3 additions & 0 deletions tests/data/app/valid-info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,7 @@
<os>Linux</os>
<owncloud min-version="7.0.1" max-version="8" />
</dependencies>
<backends>
<caldav />
</backends>
</info>

0 comments on commit 3c01364

Please sign in to comment.