Skip to content

Commit

Permalink
fixup! feat(user status): automate user status for events
Browse files Browse the repository at this point in the history
  • Loading branch information
miaulalala committed Nov 8, 2023
1 parent 18ac6b6 commit 23fbe23
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 51 deletions.
16 changes: 8 additions & 8 deletions apps/dav/tests/unit/CalDAV/Status/StatusServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ public function testEmptyAvailabilityAndSearchCalendarsNoResults(): void {
->willReturn([$calendar]);
$this->timeFactory->expects(self::once())
->method('getDateTime')
->with('+5 minutes')
->with('+10 minutes')
->willReturn($inAnHour);
$query->expects(self::once())
->method('setTimerangeStart')
Expand Down Expand Up @@ -685,7 +685,7 @@ public function testAvailabilityAndSearchCalendarsNoResults(): void {
->willReturn([$calendar]);
$this->timeFactory->expects(self::once())
->method('getDateTime')
->with('+5 minutes')
->with('+10 minutes')
->willReturn($inAnHour);
$query->expects(self::once())
->method('setTimerangeStart')
Expand Down Expand Up @@ -808,7 +808,7 @@ public function testAvailabilityAndSearchCalendarsStatusOnline(): void {
->willReturn([$calendar]);
$this->timeFactory->expects(self::once())
->method('getDateTime')
->with('+5 minutes')
->with('+10 minutes')
->willReturn($inAnHour);
$query->expects(self::once())
->method('setTimerangeStart')
Expand Down Expand Up @@ -932,7 +932,7 @@ public function testAvailabilityAndSearchCalendarsStatusBusyNoFBType(): void {
->willReturn([$calendar]);
$this->timeFactory->expects(self::once())
->method('getDateTime')
->with('+5 minutes')
->with('+10 minutes')
->willReturn($inAnHour);
$query->expects(self::once())
->method('setTimerangeStart')
Expand Down Expand Up @@ -1056,7 +1056,7 @@ public function testAvailabilityAndSearchCalendarsStatusBusy(): void {
->willReturn([$calendar]);
$this->timeFactory->expects(self::once())
->method('getDateTime')
->with('+5 minutes')
->with('+10 minutes')
->willReturn($inAnHour);
$query->expects(self::once())
->method('setTimerangeStart')
Expand Down Expand Up @@ -1184,7 +1184,7 @@ public function testAvailabilityAndSearchCalendarsStatusBusyUnavailable(): void
->willReturn([$calendar]);
$this->timeFactory->expects(self::once())
->method('getDateTime')
->with('+5 minutes')
->with('+10 minutes')
->willReturn($inAnHour);
$query->expects(self::once())
->method('setTimerangeStart')
Expand Down Expand Up @@ -1309,7 +1309,7 @@ public function testAvailabilityAndSearchCalendarsStatusBusyTentative(): void {
->willReturn([$calendar]);
$this->timeFactory->expects(self::once())
->method('getDateTime')
->with('+5 minutes')
->with('+10 minutes')
->willReturn($inAnHour);
$query->expects(self::once())
->method('setTimerangeStart')
Expand Down Expand Up @@ -1434,7 +1434,7 @@ public function testAvailabilityAndSearchCalendarsStatusBusyUnknownProperty(): v
->willReturn([$calendar]);
$this->timeFactory->expects(self::once())
->method('getDateTime')
->with('+5 minutes')
->with('+10 minutes')
->willReturn($inAnHour);
$query->expects(self::once())
->method('setTimerangeStart')
Expand Down
2 changes: 1 addition & 1 deletion apps/user_status/lib/Service/StatusService.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public function findAllRecentStatusChanges(?int $limit = null, ?int $offset = nu
*/
public function findByUserId(string $userId): UserStatus {
$userStatus = $this->mapper->findByUserId($userId);
// If the status is user-defined and one of the persistent statuses, we
// If the status is user-defined and one of the persistent status, we
// will not override it.
if ($userStatus->getIsUserDefined() && \in_array($userStatus->getStatus(), StatusService::PERSISTENT_STATUSES, true)) {
return $this->processStatus($userStatus);
Expand Down
188 changes: 146 additions & 42 deletions apps/user_status/tests/Unit/Service/StatusServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,16 +185,6 @@ public function testFindAllRecentStatusChangesNoEnumeration(): void {
$this->assertEquals([], $this->service->findAllRecentStatusChanges(20, 50));
}

public function testFindByUserId(): void {
$status = $this->createMock(UserStatus::class);
$this->mapper->expects($this->once())
->method('findByUserId')
->with('john.doe')
->willReturn($status);

$this->assertEquals($status, $this->service->findByUserId('john.doe'));
}

public function testFindByUserIdDoesNotExist(): void {
$this->mapper->expects($this->once())
->method('findByUserId')
Expand Down Expand Up @@ -934,16 +924,6 @@ public function testCalendarAvailabilityVavailablilityAvailable(): void {
->method('processCalendarAvailability')
->with($user, $vavailability)
->willReturn($status);
$this->timeFactory->expects(self::once())
->method('getTime')
->willReturn(0);
$this->mapper->expects(self::once())
->method('findByUserId')
->with($user->getUID())
->willThrowException(new DoesNotExistException('Does not exist'));
$this->mapper->expects(self::once())
->method('insert')
->willReturn(new UserStatus());

$this->service->getCalendarStatus($user->getUID());
}
Expand All @@ -954,21 +934,6 @@ public function testCalendarAvailabilityVavailablilityUpdate(): void {
'getEMailAddress' => 'test@test.com',
]);
$calDavStatus = new Status(IUserStatus::BUSY, 'meeting', 'In a meeting');

$oldStatus = new UserStatus();
$oldStatus->setId(42);
$oldStatus->setUserId($user->getUID());
$oldStatus->setStatus(IUserStatus::ONLINE);
$oldStatus->setStatusTimestamp(0);
$oldStatus->setIsUserDefined(true);

$newStatus = new UserStatus();
$newStatus->setUserId($user->getUID());
$newStatus->setStatus(IUserStatus::BUSY);
$newStatus->setStatusTimestamp(0);
$newStatus->setIsUserDefined(false);
$newStatus->setIsBackup(false);

$vavailability = <<<EOF
BEGIN:VCALENDAR
PRODID:Nextcloud DAV app
Expand Down Expand Up @@ -1010,20 +975,159 @@ public function testCalendarAvailabilityVavailablilityUpdate(): void {
->method('processCalendarAvailability')
->with($user, $vavailability)
->willReturn($calDavStatus);

$this->service->getCalendarStatus($user->getUID());
}

public function testFindByUserIdUserDefinedAndPersistent(): void {
$status = new UserStatus();
$status->setIsUserDefined(true);
$status->setStatus(IUserStatus::DND);

$this->mapper->expects($this->once())
->method('findByUserId')
->with('admin')
->willReturn($status);
$this->mapper->expects(self::never())
->method('getAvailabilityFromPropertiesTable');
$this->calendarStatusService->expects(self::never())
->method('processCalendarAvailability');

$this->assertEquals($status, $this->service->findByUserId('admin'));
}

public function testFindByUserIdUserDefinedNoCalStatus(): void {
$user = $this->createConfiguredMock(IUser::class, [
'getUID' => 'admin',
'getEMailAddress' => 'test@test.com',
]);
$status = new UserStatus();
$status->setIsUserDefined(true);
$status->setStatus(IUserStatus::ONLINE);

$this->mapper->expects($this->once())
->method('findByUserId')
->with($user->getUID())
->willReturn($status);
$this->userManager->expects(self::once())
->method('get')
->willReturn($user);
$this->mapper->expects(self::once())
->method('getAvailabilityFromPropertiesTable')
->willReturn('');
$this->calendarStatusService->expects(self::once())
->method('processCalendarAvailability')
->with($user, '')
->willReturn(null);

$this->assertEquals($status, $this->service->findByUserId('admin'));
}

public function testFindByUserIdUserDefinedCalStatusIdentical(): void {
$user = $this->createConfiguredMock(IUser::class, [
'getUID' => 'admin',
'getEMailAddress' => 'test@test.com',
]);
$calDavStatus = new Status(IUserStatus::ONLINE);
$userStatus = new UserStatus();
$userStatus->setStatus(IUserStatus::ONLINE);
$userStatus->setIsUserDefined(true);
$userStatus->setCustomMessage('Test');

$this->mapper->expects(self::once())
->method('findByUserId')
->with($user->getUID())
->willReturn($userStatus);
$this->userManager->expects(self::once())
->method('get')
->willReturn($user);
$this->mapper->expects(self::once())
->method('getAvailabilityFromPropertiesTable')
->willReturn('');
$this->calendarStatusService->expects(self::once())
->method('processCalendarAvailability')
->with($user, '')
->willReturn($calDavStatus);

$this->assertEquals($userStatus, $this->service->findByUserId('admin'));
}

public function testFindByUserIdUserDefinedCalStatusUpdate(): void {
$user = $this->createConfiguredMock(IUser::class, [
'getUID' => 'admin',
'getEMailAddress' => 'test@test.com',
]);
$calDavStatus = new Status(IUserStatus::BUSY, 'meeting', 'In a meeting');

$oldStatus = new UserStatus();
$oldStatus->setId(42);
$oldStatus->setUserId($user->getUID());
$oldStatus->setStatus(IUserStatus::ONLINE);
$oldStatus->setStatusTimestamp(0);
$oldStatus->setIsUserDefined(true);

$expected = new UserStatus();
$expected->setUserId($user->getUID());
$expected->setStatus(IUserStatus::BUSY);
$expected->setStatusTimestamp(0);
$expected->setIsUserDefined(true);
$expected->setIsBackup(false);

$this->mapper->expects(self::once())
->method('findByUserId')
->with($user->getUID())
->willReturn($oldStatus);
$this->userManager->expects(self::once())
->method('get')
->willReturn($user);
$this->mapper->expects(self::once())
->method('getAvailabilityFromPropertiesTable')
->willReturn('');
$this->mapper->expects(self::once())
->method('createBackupStatus')
->with($user->getUID())
->willReturn(true);
$this->calendarStatusService->expects(self::once())
->method('processCalendarAvailability')
->with($user, '')
->willReturn($calDavStatus);
$this->predefinedStatusService->expects(self::once())
->method('isValidId')
->with($calDavStatus->getMessage())
->willReturn(true);
$this->timeFactory->expects(self::once())
->method('getTime')
->willReturn(0);
$this->mapper->expects(self::once())
->method('insert')
->willReturn($expected);

$actual = $this->service->findByUserId('admin');
$this->assertEquals($expected->getStatus(), $actual->getStatus());
$this->assertEquals($expected->getCustomMessage(), $actual->getCustomMessage());
}

public function testFindByUserIdSystemDefined(): void {
$user = $this->createConfiguredMock(IUser::class, [
'getUID' => 'admin',
'getEMailAddress' => 'test@test.com',
]);
$status = new UserStatus();
$status->setIsUserDefined(false);
$status->setStatus(IUserStatus::ONLINE);

$this->mapper->expects($this->once())
->method('findByUserId')
->with($user->getUID())
->willReturn($oldStatus);
->willReturn($status);
$this->userManager->expects(self::once())
->method('get')
->willReturn($user);
$this->mapper->expects(self::once())
->method('update')
->willReturn($newStatus);
->method('getAvailabilityFromPropertiesTable')
->willReturn('');
$this->calendarStatusService->expects(self::once())
->method('processCalendarAvailability')
->with($user, '')
->willReturn(null);

$this->service->getCalendarStatus($user->getUID());
$this->assertEquals($status, $this->service->findByUserId('admin'));
}
}

0 comments on commit 23fbe23

Please sign in to comment.