Skip to content

Commit

Permalink
fix(dav): add a new config key to check to retrigger regenerating bir…
Browse files Browse the repository at this point in the history
…thday calendars

So that birthday calendars are immediately updated and we don't need to wait for user to change a
card or disable/enable the calendar. We reuse the existing RegenerateBirthdayCalendars repair step
instead of adding a new one.

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
  • Loading branch information
tcitworld committed Feb 1, 2024
1 parent 8939a2e commit 4451809
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 51 deletions.
26 changes: 7 additions & 19 deletions apps/dav/lib/Migration/RegenerateBirthdayCalendars.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
* @author Georg Ehrke <oc.list@georgehrke.com>
* @author Thomas Citharel <nextcloud@tcit.fr>
*
* @license GNU AGPL version 3 or any later version
*
Expand All @@ -30,36 +31,22 @@
use OCP\Migration\IRepairStep;

class RegenerateBirthdayCalendars implements IRepairStep {
private IJobList $jobList;
private IConfig $config;

/** @var IJobList */
private $jobList;

/** @var IConfig */
private $config;

/**
* @param IJobList $jobList
* @param IConfig $config
*/
public function __construct(IJobList $jobList,
IConfig $config) {
$this->jobList = $jobList;
$this->config = $config;
}

/**
* @return string
*/
public function getName() {
public function getName(): string {
return 'Regenerating birthday calendars to use new icons and fix old birthday events without year';
}

/**
* @param IOutput $output
*/
public function run(IOutput $output) {
public function run(IOutput $output): void {
// only run once
if ($this->config->getAppValue('dav', 'regeneratedBirthdayCalendarsForYearFix') === 'yes') {
if ($this->config->getAppValue('dav', 'regeneratedBirthdayCalendarsForYearFix') === 'yes' && $this->config->getAppValue('dav', 'regeneratedBirthdayCalendarsForAlarmFix') === 'yes') {

Check notice

Code scanning / Psalm

DeprecatedMethod Note

The method OCP\IConfig::getAppValue has been marked as deprecated

Check notice

Code scanning / Psalm

DeprecatedMethod Note

The method OCP\IConfig::getAppValue has been marked as deprecated
$output->info('Repair step already executed');
return;
}
Expand All @@ -69,5 +56,6 @@ public function run(IOutput $output) {

// if all were done, no need to redo the repair during next upgrade
$this->config->setAppValue('dav', 'regeneratedBirthdayCalendarsForYearFix', 'yes');
$this->config->setAppValue('dav', 'regeneratedBirthdayCalendarsForAlarmFix', 'yes');

Check notice

Code scanning / Psalm

DeprecatedMethod Note

The method OCP\IConfig::setAppValue has been marked as deprecated
}
}
69 changes: 37 additions & 32 deletions apps/dav/tests/unit/Migration/RegenerateBirthdayCalendarsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,17 @@
use OCP\BackgroundJob\IJobList;
use OCP\IConfig;
use OCP\Migration\IOutput;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;

class RegenerateBirthdayCalendarsTest extends TestCase {

/** @var IJobList | \PHPUnit\Framework\MockObject\MockObject */
/** @var IJobList | MockObject */
private $jobList;

/** @var IConfig | \PHPUnit\Framework\MockObject\MockObject */
/** @var IConfig | MockObject */
private $config;

/** @var RegenerateBirthdayCalendars */
private $migration;
private RegenerateBirthdayCalendars $migration;

protected function setUp(): void {
parent::setUp();
Expand All @@ -61,41 +60,47 @@ public function testGetName(): void {
);
}

public function testRun(): void {
$this->config->expects($this->once())
public function dataForTestRun(): array {
return [
['', '', true],
['yes', '', true],
['yes', 'yes', false]
];
}

/**
* @dataProvider dataForTestRun
*/
public function testRun(string $yearFix, string $alarmFix, bool $run): void {
$this->config->expects($this->exactly($yearFix === '' ? 1 : 2))
->method('getAppValue')
->with('dav', 'regeneratedBirthdayCalendarsForYearFix')
->willReturn(null);
->withConsecutive(['dav', 'regeneratedBirthdayCalendarsForYearFix'], ['dav', 'regeneratedBirthdayCalendarsForAlarmFix'])
->willReturnOnConsecutiveCalls($yearFix, $alarmFix);

$output = $this->createMock(IOutput::class);
$output->expects($this->once())
->method('info')
->with('Adding background jobs to regenerate birthday calendar');

$this->jobList->expects($this->once())
->method('add')
->with(RegisterRegenerateBirthdayCalendars::class);

$this->config->expects($this->once())
->method('setAppValue')
->with('dav', 'regeneratedBirthdayCalendarsForYearFix', 'yes');
if ($run) {
$output->expects($this->once())
->method('info')
->with('Adding background jobs to regenerate birthday calendar');

$this->migration->run($output);
}
$this->jobList->expects($this->once())
->method('add')
->with(RegisterRegenerateBirthdayCalendars::class);

public function testRunSecondTime(): void {
$this->config->expects($this->once())
->method('getAppValue')
->with('dav', 'regeneratedBirthdayCalendarsForYearFix')
->willReturn('yes');
$this->config->expects($this->exactly(2))
->method('setAppValue')
->withConsecutive(['dav', 'regeneratedBirthdayCalendarsForYearFix', 'yes'], ['dav', 'regeneratedBirthdayCalendarsForAlarmFix', 'yes']);
} else {
$output->expects($this->once())
->method('info')
->with('Repair step already executed');

$output = $this->createMock(IOutput::class);
$output->expects($this->once())
->method('info')
->with('Repair step already executed');
$this->jobList->expects($this->never())
->method('add');

$this->jobList->expects($this->never())
->method('add');
$this->config->expects($this->never())->method('setAppValue');
}

$this->migration->run($output);
}
Expand Down

0 comments on commit 4451809

Please sign in to comment.