Skip to content
Merged
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
3 changes: 3 additions & 0 deletions lib/Dashboard/CalendarWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ public function getItems(string $userId, ?string $since = null, int $limit = 7):
];
$widgetItems = [];
foreach ($calendars as $calendar) {
if (method_exists($calendar, 'isEnabled') && $calendar->isEnabled() === false) {
continue;
}
if ($calendar->isDeleted()) {
continue;
}
Expand Down
68 changes: 65 additions & 3 deletions tests/php/unit/Dashbaord/CalendarWidgetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@

use DateTimeImmutable;
use OCA\Calendar\Service\JSDataService;
use OCA\DAV\CalDAV\CalendarImpl;
use OCP\AppFramework\Services\IInitialState;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Calendar\ICalendar;
use OCP\Calendar\ICalendarIsEnabled;
use OCP\Calendar\IManager;
use OCP\Dashboard\IButtonWidget;
use OCP\Dashboard\Model\WidgetItem;
Expand All @@ -21,6 +22,11 @@
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;

interface ITestCalendar extends ICalendar, ICalendarIsEnabled {
// workaround for creating mock class with multiple interfaces
// TODO: remove after phpUnit 10 is supported.
}

class CalendarWidgetTest extends TestCase {
/** @var IL10N|MockObject */
private $l10n;
Expand Down Expand Up @@ -97,7 +103,7 @@ public function testGetUrl(): void {

public function testGetItems() : void {
$userId = 'admin';
$calendar = $this->createMock(CalendarImpl::class);
$calendar = $this->createMock(ITestCalendar::class);
self::invokePrivate($calendar, 'calendarInfo', [['{http://apple.com/ns/ical/}calendar-color' => '#ffffff']]);
$calendars = [$calendar];
$time = 1665550936;
Expand Down Expand Up @@ -133,6 +139,12 @@ public function testGetItems() : void {
$this->timeFactory->expects(self::once())
->method('getTime')
->willReturn($time);
$calendar->expects(self::once())
->method('isEnabled')
->willReturn(true);
$calendar->expects(self::once())
->method('isDeleted')
->willReturn(false);
$calendar->expects(self::once())
->method('search')
->with('', [], $options, $limit)
Expand Down Expand Up @@ -160,9 +172,56 @@ public function testGetItems() : void {
$this->assertEquals($widgets[0], $widget);
}

public function testGetItemsWithDisabledCalendar() {
$userId = 'admin';
$calendar = $this->createMock(ITestCalendar::class);
$calendars = [$calendar];
$time = 1665550936;
$start = (new DateTimeImmutable())->setTimestamp($time);
$twoWeeks = $start->add(new \DateInterval('P14D'));
$options = [
'timerange' => [
'start' => $start,
'end' => $twoWeeks,
]
];
$limit = 7;
$result = [
'id' => '3599',
'uid' => '59d30b6c-5a31-4d28-b1d6-c8f928180e96',
'uri' => '60EE4FCB-2144-4811-BBD3-FFEA44739F40.ics',
'objects' => [
[
'DTSTART' => [
$start
],
'SUMMARY' => [
'Test',
]
]
]
];

$this->calendarManager->expects(self::once())
->method('getCalendarsForPrincipal')
->with('principals/users/' . $userId)
->willReturn($calendars);
$this->timeFactory->expects(self::once())
->method('getTime')
->willReturn($time);
$calendar->expects(self::once())
->method('isEnabled')
->willReturn(false);
$calendar->expects(self::never())
->method('isDeleted');

$widgets = $this->widget->getItems($userId);
$this->assertCount(0, $widgets);
}

public function testGetItemsWithDeletedCalendar() {
$userId = 'admin';
$calendar = $this->createMock(CalendarImpl::class);
$calendar = $this->createMock(ITestCalendar::class);
$calendars = [$calendar];
$time = 1665550936;
$start = (new DateTimeImmutable())->setTimestamp($time);
Expand Down Expand Up @@ -197,6 +256,9 @@ public function testGetItemsWithDeletedCalendar() {
$this->timeFactory->expects(self::once())
->method('getTime')
->willReturn($time);
$calendar->expects(self::once())
->method('isEnabled')
->willReturn(true);
$calendar->expects(self::once())
->method('isDeleted')
->willReturn(true);
Expand Down
Loading