Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 10 additions & 1 deletion lib/Settings/Personal.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use OCA\Absence\Service\ConfigService;
use OCA\Absence\Service\PersonalDefaultsService;
use OCP\App\IAppManager;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Services\IInitialState;
use OCP\IUserSession;
Expand All @@ -21,6 +22,7 @@ public function __construct(
private IInitialState $initialState,
private PersonalDefaultsService $personalDefaults,
private IUserSession $userSession,
private IAppManager $appManager,
) {
}

Expand All @@ -33,7 +35,14 @@ public function getForm(): TemplateResponse {
}

#[\Override]
public function getSection(): string {
public function getSection(): ?string {
// The app may be restricted to some groups, but its settings are still
// registered for everyone. Hide the form from users without access, as
// the API would reject them anyway.
$user = $this->userSession->getUser();
if ($user === null || !$this->appManager->isEnabledForUser(ConfigService::APP_ID, $user)) {
return null;
}
// Append to the built-in Availability page (/settings/user/availability)
// rather than a separate Absence section.
return 'availability';
Expand Down
66 changes: 66 additions & 0 deletions tests/Unit/Settings/PersonalTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Absence\Tests\Unit\Settings;

use OCA\Absence\Service\PersonalDefaultsService;
use OCA\Absence\Settings\Personal;
use OCP\App\IAppManager;
use OCP\AppFramework\Services\IInitialState;
use OCP\IUser;
use OCP\IUserSession;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class PersonalTest extends TestCase {
private IUserSession&MockObject $userSession;
private IAppManager&MockObject $appManager;
private Personal $form;

protected function setUp(): void {
parent::setUp();
$this->userSession = $this->createMock(IUserSession::class);
$this->appManager = $this->createMock(IAppManager::class);
$this->form = new Personal(
$this->createMock(IInitialState::class),
$this->createMock(PersonalDefaultsService::class),
$this->userSession,
$this->appManager,
);
}

private function loginUser(): IUser&MockObject {
$user = $this->createMock(IUser::class);
$this->userSession->method('getUser')->willReturn($user);
return $user;
}

public function testSectionIsAvailabilityWhenAppIsEnabledForTheUser(): void {
$user = $this->loginUser();
$this->appManager->expects($this->once())
->method('isEnabledForUser')
->with('absence', $user)
->willReturn(true);

$this->assertSame('availability', $this->form->getSection());
}

public function testNoSectionWhenAppIsNotEnabledForTheUser(): void {
$this->loginUser();
$this->appManager->method('isEnabledForUser')->willReturn(false);

$this->assertNull($this->form->getSection());
}

public function testNoSectionWithoutAUser(): void {
$this->userSession->method('getUser')->willReturn(null);
$this->appManager->expects($this->never())->method('isEnabledForUser');

$this->assertNull($this->form->getSection());
}
}
Loading