diff --git a/SPECIFICATION.md b/SPECIFICATION.md index fe51f1d..cf79e19 100644 --- a/SPECIFICATION.md +++ b/SPECIFICATION.md @@ -1326,12 +1326,14 @@ All four channels are required. - **Carry-over about to expire** (→ the employee, §6.2): how many carried-over days expire and on which date, as a notification and an email that link to My leave. It names no leave type and carries no buttons. -- **Manager changed** (→ the employee and HR, never the person who made the change, - §2.1): only for a change a signed-in user made. A notification without email and - without buttons: "Your manager was changed" for the employee, "The manager of - {name} was changed" for HR, and "{actor} changed it from {before} to {after}." It - stores uids only, and the names are read when it is shown. It links to the - employee's profile, where the change is in the history. +- **Manager changed** (→ the employee, their new managers and their former ones, + never the person who made the change, §2.1; not HR, who find it in the person's + history): only for a change a signed-in user made. A notification without email + and without buttons: "Your manager was changed" for the employee, "{name} now + reports to you" for a new manager, "{name} no longer reports to you" for a former + one, and "{actor} changed it from {before} to {after}." It stores uids only, and + the names are read when it is shown. It links to the employee's profile, where the + change is in the history, and a former manager to their team. - **Actionable where the recipient owes an answer.** The four notifications that ask for a decision — new request, escalation, reminder, withdrawal — carry three buttons: **Approve**, **Decline** and **Review**. diff --git a/lib/Notification/Notifier.php b/lib/Notification/Notifier.php index ae42159..6bc9725 100644 --- a/lib/Notification/Notifier.php +++ b/lib/Notification/Notifier.php @@ -219,8 +219,11 @@ private function addDecisionActions(INotification $notification, IL10N $l, strin } /** - * The employee reads that their manager changed, HR whose manager changed; - * both read who changed it, from whom to whom. + * The employee reads that their manager changed, the new manager that the + * employee now reports to them, the former one that they no longer do (and + * lands on their team, as the profile may be closed to them by now). Older + * notifications HR got read whose manager changed. All read who changed it, + * from whom to whom. */ private function prepareManagerChange(INotification $notification, IL10N $l): INotification { $params = $notification->getSubjectParameters(); @@ -228,17 +231,26 @@ private function prepareManagerChange(INotification $notification, IL10N $l): IN if ($employeeUid === '') { throw new UnknownNotificationException('Incomplete manager change'); } - $own = $notification->getUser() === $employeeUid; - return $notification->setParsedSubject($own - ? $l->t('Your manager was changed') - : $l->t('The manager of %s was changed', [$this->displayName($l, $employeeUid)])) + $user = $notification->getUser(); + $own = $user === $employeeUid; + $name = $this->displayName($l, $employeeUid); + $isNew = in_array($user, (array)($params['after'] ?? []), true); + $isFormer = !$isNew && in_array($user, (array)($params['before'] ?? []), true); + $subject = match (true) { + $own => $l->t('Your manager was changed'), + $isNew => $l->t('%s now reports to you', [$name]), + $isFormer => $l->t('%s no longer reports to you', [$name]), + default => $l->t('The manager of %s was changed', [$name]), + }; + $link = $isFormer ? '#/team' : '#/profile' . ($own ? '' : '/' . rawurlencode($employeeUid)); + return $notification->setParsedSubject($subject) ->setParsedMessage($l->t('%1$s changed it from %2$s to %3$s.', [ $this->displayName($l, (string)($params['actor'] ?? '')), $this->names($l, $params['before'] ?? []), $this->names($l, $params['after'] ?? []), ])) ->setIcon($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath(ConfigService::APP_ID, 'app-dark.svg'))) - ->setLink($this->urlGenerator->linkToRouteAbsolute('absence.page.index') . '#/profile' . ($own ? '' : '/' . rawurlencode($employeeUid))); + ->setLink($this->urlGenerator->linkToRouteAbsolute('absence.page.index') . $link); } private function names(IL10N $l, mixed $uids): string { diff --git a/lib/Service/EmployeeService.php b/lib/Service/EmployeeService.php index f38c007..fda7622 100644 --- a/lib/Service/EmployeeService.php +++ b/lib/Service/EmployeeService.php @@ -49,7 +49,6 @@ public function __construct( private IEventDispatcher $dispatcher, private NotificationService $notifications, private HolidayService $holidays, - private PermissionService $permission, private IL10N $l, ) { } @@ -225,8 +224,9 @@ public function recordRegionChange(string $uid, string $before, string $after, ? /** * Keeps a change of someone's managers (§2.1) in their history and the admin * audit log. A change a signed-in person made is also told to the employee - * and HR; one without, from occ, a background job or an account sync, is - * only kept, so that a first sync does not notify HR of every employee. + * and their former and new managers; one without, from occ, a background job + * or an account sync, is only kept, so that a first sync does not notify + * every manager of every employee. * * @param array $before the configured manager uids * @param array $after @@ -242,7 +242,7 @@ public function recordManagerChange(string $uid, array $before, array $after, ?s 'Manager of %s changed from "%s" to "%s" by %s', [$uid, implode(',', $before), implode(',', $after), $actor ?? 'system'], )); if ($actor !== null) { - $this->notifications->notifyManagerChanged($uid, $before, $after, $actor, $this->permission->getHrUids()); + $this->notifications->notifyManagerChanged($uid, $before, $after, $actor); } } diff --git a/lib/Service/NotificationService.php b/lib/Service/NotificationService.php index 4c0b9cd..e0d74be 100644 --- a/lib/Service/NotificationService.php +++ b/lib/Service/NotificationService.php @@ -483,16 +483,17 @@ public function notifyCarryOverExpiring(string $uid, float $days, string $expiry } /** - * Tells an employee and HR that the employee's manager changed (§2.1), and - * who changed it. The notification carries uids only; the names are read - * when it is shown. Nobody is told about their own change. + * Tells the employee, their new managers and their former ones that the + * employee's manager changed (§2.1), and who changed it. Not HR: the change + * is in the person's history, where HR looks. The notification carries uids + * only; the names are read when it is shown. Nobody is told about their own + * change. * * @param list $before * @param list $after - * @param string[] $hrUids */ - public function notifyManagerChanged(string $employeeUid, array $before, array $after, string $actorUid, array $hrUids): void { - foreach (array_unique([$employeeUid, ...$hrUids]) as $uid) { + public function notifyManagerChanged(string $employeeUid, array $before, array $after, string $actorUid): void { + foreach (array_unique([$employeeUid, ...$after, ...$before]) as $uid) { if ($uid === $actorUid || $this->userManager->get($uid)?->isEnabled() !== true) { continue; } diff --git a/tests/Integration/ManagerChangeHistoryTest.php b/tests/Integration/ManagerChangeHistoryTest.php index 5939326..f905e37 100644 --- a/tests/Integration/ManagerChangeHistoryTest.php +++ b/tests/Integration/ManagerChangeHistoryTest.php @@ -59,7 +59,7 @@ protected function tearDown(): void { parent::tearDown(); } - public function testAChangeBySomeoneIsInTheHistoryAndToldToTheEmployeeAndHr(): void { + public function testAChangeBySomeoneIsInTheHistoryAndToldToTheEmployeeAndTheFormerManager(): void { $users = Server::get(IUserManager::class); Server::get(IUserSession::class)->setUser($users->get(self::SUBADMIN)); @@ -68,7 +68,8 @@ public function testAChangeBySomeoneIsInTheHistoryAndToldToTheEmployeeAndHr(): v $latest = Server::get(EmployeeService::class)->history(self::EMPLOYEE)[0]; self::assertSame(['manager', self::SUBADMIN, [self::FORMER], [self::SUBADMIN]], [$latest['field'], $latest['actor'], $latest['before'], $latest['after']]); $told = array_filter(RecordingNotifications::$sent, static fn (INotification $n): bool => $n->getSubject() === NotificationService::SUBJECT_MANAGER_CHANGED); - self::assertEqualsCanonicalizing([self::EMPLOYEE, self::HR], array_map(static fn (INotification $n): string => $n->getUser(), $told)); + // The new manager made the change, so the employee and the former manager are told; HR is not + self::assertEqualsCanonicalizing([self::EMPLOYEE, self::FORMER], array_map(static fn (INotification $n): string => $n->getUser(), $told)); } public function testAChangeWithoutASessionIsInTheHistoryOnly(): void { diff --git a/tests/Unit/Notification/NotifierTest.php b/tests/Unit/Notification/NotifierTest.php index f095943..4dd56e9 100644 --- a/tests/Unit/Notification/NotifierTest.php +++ b/tests/Unit/Notification/NotifierTest.php @@ -355,4 +355,28 @@ public function testHrReadsWhoseManagerChangedAndOpensTheirProfile(): void { self::assertSame('Admin changed it from nobody to Carol, Boss.', $this->parsedMessage); self::assertSame('https://cloud.example/apps/absence/#/profile/emp', $this->link); } + + public function testTheNewManagerReadsThatTheEmployeeNowReportsToThem(): void { + $this->prepare(NotificationService::SUBJECT_MANAGER_CHANGED, [ + 'employee' => 'emp', + 'actor' => 'admin', + 'before' => ['boss'], + 'after' => ['carol'], + ], 'carol'); + + self::assertSame('Emp now reports to you', $this->parsedSubject); + self::assertSame('https://cloud.example/apps/absence/#/profile/emp', $this->link); + } + + public function testTheFormerManagerReadsThatTheEmployeeNoLongerReportsToThemAndOpensTheirTeam(): void { + $this->prepare(NotificationService::SUBJECT_MANAGER_CHANGED, [ + 'employee' => 'emp', + 'actor' => 'admin', + 'before' => ['boss'], + 'after' => ['carol'], + ], 'boss'); + + self::assertSame('Emp no longer reports to you', $this->parsedSubject); + self::assertSame('https://cloud.example/apps/absence/#/team', $this->link); + } } diff --git a/tests/Unit/Service/EmployeeServiceTest.php b/tests/Unit/Service/EmployeeServiceTest.php index de94f91..5693064 100644 --- a/tests/Unit/Service/EmployeeServiceTest.php +++ b/tests/Unit/Service/EmployeeServiceTest.php @@ -17,7 +17,6 @@ use OCA\Absence\Service\EmployeeService; use OCA\Absence\Service\HolidayService; use OCA\Absence\Service\NotificationService; -use OCA\Absence\Service\PermissionService; use OCA\Absence\Tests\Unit\ClockMockTrait; use OCP\EventDispatcher\IEventDispatcher; use OCP\IDBConnection; @@ -58,8 +57,6 @@ protected function setUp(): void { $this->directory = $this->createMock(EmployeeDirectory::class); $this->directory->method('isEmployee')->willReturnCallback(static fn (string $uid): bool => $uid !== 'guest'); $this->notifications = $this->createMock(NotificationService::class); - $permission = $this->createMock(PermissionService::class); - $permission->method('getHrUids')->willReturn(['hr1', 'hr2']); $this->service = new EmployeeService( $this->createMock(EmploymentMapper::class), @@ -72,14 +69,13 @@ protected function setUp(): void { $dispatcher, $this->notifications, $this->createMock(HolidayService::class), - $permission, $this->createMock(IL10N::class), ); } - public function testAChangeBySomeoneIsKeptAuditedAndToldToTheEmployeeAndHr(): void { + public function testAChangeBySomeoneIsKeptAuditedAndTold(): void { $this->notifications->expects(self::once())->method('notifyManagerChanged') - ->with('emp', ['boss'], ['carol'], 'carol', ['hr1', 'hr2']); + ->with('emp', ['boss'], ['carol'], 'carol'); $this->service->recordManagerChange('emp', ['boss'], ['carol'], 'carol'); diff --git a/tests/Unit/Service/NotificationServiceTest.php b/tests/Unit/Service/NotificationServiceTest.php index 56d7c02..95e6a1c 100644 --- a/tests/Unit/Service/NotificationServiceTest.php +++ b/tests/Unit/Service/NotificationServiceTest.php @@ -667,13 +667,13 @@ public function testCarryOverReminderSendsNoEmailForAnAccountThatIsGone(): void $this->service->notifyCarryOverExpiring('robin', 3.0, '2032-03-31'); } - public function testAManagerChangeIsToldToTheEmployeeAndHrButNotToWhoeverMadeIt(): void { - $this->withUsers('emp', 'hr1', 'hr2', 'hr3-disabled', 'carol'); + public function testAManagerChangeIsToldToTheEmployeeAndTheirManagersButNotToWhoeverMadeIt(): void { + $this->withUsers('emp', 'boss', 'carol', 'dave-disabled', 'hr2'); $this->mailer->expects(self::never())->method('send'); - $this->service->notifyManagerChanged('emp', ['boss'], ['carol'], 'hr2', ['hr1', 'hr2', 'hr3-disabled', 'emp']); + $this->service->notifyManagerChanged('emp', ['boss', 'hr2'], ['carol', 'dave-disabled'], 'hr2'); - self::assertSame(['emp', 'hr1'], $this->recipients); - self::assertSame(['employee' => 'emp', 'actor' => 'hr2', 'before' => ['boss'], 'after' => ['carol']], $this->pushed[0]); + self::assertSame(['emp', 'carol', 'boss'], $this->recipients); + self::assertSame(['employee' => 'emp', 'actor' => 'hr2', 'before' => ['boss', 'hr2'], 'after' => ['carol', 'dave-disabled']], $this->pushed[0]); } }