Skip to content

Commit

Permalink
fixup! enh(userstatus): add OOO automation and remove calendar automa…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
miaulalala committed Nov 27, 2023
1 parent 07952c9 commit d90b982
Showing 1 changed file with 87 additions and 47 deletions.
134 changes: 87 additions & 47 deletions apps/dav/tests/unit/BackgroundJob/UserStatusAutomationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@

namespace OCA\DAV\Tests\unit\BackgroundJob;

use OC\User\OutOfOfficeData;
use OCA\DAV\BackgroundJob\UserStatusAutomation;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJobList;
use OCP\IConfig;
use OCP\IUser;
use OCP\IUserManager;
use OCP\User\IAvailabilityCoordinator;
use OCP\UserStatus\IManager;
use OCP\UserStatus\IUserStatus;
use PHPUnit\Framework\MockObject\MockObject;
Expand All @@ -46,6 +50,8 @@ class UserStatusAutomationTest extends TestCase {
protected MockObject|LoggerInterface $logger;
protected MockObject|IManager $statusManager;
protected MockObject|IConfig $config;
private IAvailabilityCoordinator|MockObject $coordinator;
private IUserManager|MockObject $userManager;

protected function setUp(): void {
parent::setUp();
Expand All @@ -55,6 +61,8 @@ protected function setUp(): void {
$this->logger = $this->createMock(LoggerInterface::class);
$this->statusManager = $this->createMock(IManager::class);
$this->config = $this->createMock(IConfig::class);
$this->coordinator = $this->createMock(IAvailabilityCoordinator::class);
$this->userManager = $this->createMock(IUserManager::class);

}

Expand All @@ -67,6 +75,8 @@ protected function getAutomationMock(array $methods): MockObject|UserStatusAutom
$this->logger,
$this->statusManager,
$this->config,
$this->coordinator,
$this->userManager,
);
}

Expand All @@ -78,6 +88,8 @@ protected function getAutomationMock(array $methods): MockObject|UserStatusAutom
$this->logger,
$this->statusManager,
$this->config,
$this->coordinator,
$this->userManager,
])
->setMethods($methods)
->getMock();
Expand All @@ -95,14 +107,31 @@ public function dataRun(): array {
/**
* @dataProvider dataRun
*/
public function testRun(string $ruleDay, string $currentTime, bool $isAvailable): void {
public function testRunNoOOO(string $ruleDay, string $currentTime, bool $isAvailable): void {
$user = $this->createConfiguredMock(IUser::class, [
'getUID' => 'user'
]);

$this->userManager->expects(self::once())
->method('get')
->willReturn($user);
$this->coordinator->expects(self::once())
->method('getCurrentOutOfOfficeData')
->willReturn(null);
$this->config->method('getUserValue')
->with('user', 'dav', 'user_status_automation', 'no')
->willReturn('yes');

$this->time->method('getDateTime')
->willReturn(new \DateTime($currentTime, new \DateTimeZone('UTC')));

$this->logger->expects(self::exactly(4))
->method('debug');
$this->statusManager->expects(self::exactly(2))
->method('revertUserStatus');
if (!$isAvailable) {
$this->statusManager->expects(self::once())
->method('setUserStatus')
->with('user', IUserStatus::MESSAGE_AVAILABILITY, IUserStatus::DND, true);
}
$automation = $this->getAutomationMock(['getAvailabilityFromPropertiesTable']);
$automation->method('getAvailabilityFromPropertiesTable')
->with('user')
Expand Down Expand Up @@ -141,63 +170,74 @@ public function testRun(string $ruleDay, string $currentTime, bool $isAvailable)
END:VAVAILABILITY
END:VCALENDAR');

if ($isAvailable) {
$this->statusManager->expects($this->once())
->method('revertUserStatus')
->with('user', IUserStatus::MESSAGE_AVAILABILITY, IUserStatus::DND);
} else {
$this->statusManager->expects($this->once())
->method('revertUserStatus')
->with('user', IUserStatus::MESSAGE_CALL, IUserStatus::AWAY);
$this->statusManager->expects($this->once())
->method('setUserStatus')
->with('user', IUserStatus::MESSAGE_AVAILABILITY, IUserStatus::DND, true);
}

self::invokePrivate($automation, 'run', [['userId' => 'user']]);
}

public function testRunNoMoreAvailabilityDefined(): void {
public function testRunNoAvailabilityNoOOO(): void {
$user = $this->createConfiguredMock(IUser::class, [
'getUID' => 'user'
]);

$this->userManager->expects(self::once())
->method('get')
->willReturn($user);
$this->coordinator->expects(self::once())
->method('getCurrentOutOfOfficeData')
->willReturn(null);
$this->config->method('getUserValue')
->with('user', 'dav', 'user_status_automation', 'no')
->willReturn('yes');

$this->time->method('getDateTime')
->willReturn(new \DateTime('2023-02-24 13:58:24.479357', new \DateTimeZone('UTC')));

$this->statusManager->expects($this->exactly(3))
->method('revertUserStatus');
$this->jobList->expects($this->once())
->method('remove')
->with(UserStatusAutomation::class, ['userId' => 'user']);
$this->logger->expects(self::once())
->method('debug');
$this->logger->expects(self::once())
->method('info');
$automation = $this->getAutomationMock(['getAvailabilityFromPropertiesTable']);
$automation->method('getAvailabilityFromPropertiesTable')
->with('user')
->willReturn('BEGIN:VCALENDAR
PRODID:Nextcloud DAV app
BEGIN:VTIMEZONE
TZID:Europe/Berlin
BEGIN:STANDARD
TZNAME:CET
TZOFFSETFROM:+0200
TZOFFSETTO:+0100
DTSTART:19701025T030000
RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU
END:STANDARD
BEGIN:DAYLIGHT
TZNAME:CEST
TZOFFSETFROM:+0100
TZOFFSETTO:+0200
DTSTART:19700329T020000
RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU
END:DAYLIGHT
END:VTIMEZONE
BEGIN:VAVAILABILITY
END:VAVAILABILITY
END:VCALENDAR');
->willReturn(false);

$this->statusManager->expects($this->once())
->method('revertUserStatus')
->with('user', IUserStatus::MESSAGE_AVAILABILITY, IUserStatus::DND);
self::invokePrivate($automation, 'run', [['userId' => 'user']]);
}

$this->jobList->expects($this->once())
->method('remove')
->with(UserStatusAutomation::class, ['userId' => 'user']);
public function testRunNoAvailabilityWithOOO(): void {
$user = $this->createConfiguredMock(IUser::class, [
'getUID' => 'user'
]);
$ooo = $this->createConfiguredMock(OutOfOfficeData::class, [
'getShortMessage' => 'On Vacation',
'getEndDate' => 123456,
]);

$this->userManager->expects(self::once())
->method('get')
->willReturn($user);
$this->coordinator->expects(self::once())
->method('getCurrentOutOfOfficeData')
->willReturn($ooo);
$this->coordinator->expects(self::once())
->method('isInEffect')
->willReturn(true);
$this->statusManager->expects($this->exactly(2))
->method('revertUserStatus');
$this->statusManager->expects(self::once())
->method('setUserStatus')
->with('user', IUserStatus::MESSAGE_VACATION, IUserStatus::DND, true, $ooo->getShortMessage());
$this->config->expects(self::never())
->method('getUserValue');
$this->time->method('getDateTime')
->willReturn(new \DateTime('2023-02-24 13:58:24.479357', new \DateTimeZone('UTC')));
$this->jobList->expects($this->never())
->method('remove');
$this->logger->expects(self::exactly(2))
->method('debug');
$automation = $this->getAutomationMock([]);

self::invokePrivate($automation, 'run', [['userId' => 'user']]);
}
Expand Down

0 comments on commit d90b982

Please sign in to comment.