|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * @copyright 2022 Thomas Citharel <[email protected]> |
| 7 | + * |
| 8 | + * @author Thomas Citharel <[email protected]> |
| 9 | + * |
| 10 | + * @license GNU AGPL version 3 or any later version |
| 11 | + * |
| 12 | + * This program is free software: you can redistribute it and/or modify |
| 13 | + * it under the terms of the GNU Affero General Public License as |
| 14 | + * published by the Free Software Foundation, either version 3 of the |
| 15 | + * License, or (at your option) any later version. |
| 16 | + * |
| 17 | + * This program is distributed in the hope that it will be useful, |
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | + * GNU Affero General Public License for more details. |
| 21 | + * |
| 22 | + * You should have received a copy of the GNU Affero General Public License |
| 23 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 24 | + * |
| 25 | + */ |
| 26 | +namespace OCA\DAV\Tests\Unit\Listener; |
| 27 | + |
| 28 | +use OCA\DAV\CalDAV\Activity\Backend as ActivityBackend; |
| 29 | +use OCA\DAV\CalDAV\Activity\Provider\Event; |
| 30 | +use OCA\DAV\DAV\Sharing\Plugin as SharingPlugin; |
| 31 | +use OCA\DAV\Events\CalendarDeletedEvent; |
| 32 | +use OCA\DAV\Events\CalendarObjectDeletedEvent; |
| 33 | +use OCA\DAV\Listener\ActivityUpdaterListener; |
| 34 | +use PHPUnit\Framework\MockObject\MockObject; |
| 35 | +use Psr\Log\LoggerInterface; |
| 36 | +use Test\TestCase; |
| 37 | + |
| 38 | +class ActivityUpdaterListenerTest extends TestCase { |
| 39 | + |
| 40 | + /** @var ActivityBackend|MockObject */ |
| 41 | + private $activityBackend; |
| 42 | + /** @var LoggerInterface|MockObject */ |
| 43 | + private $logger; |
| 44 | + /** @var ActivityUpdaterListener */ |
| 45 | + private ActivityUpdaterListener $listener; |
| 46 | + |
| 47 | + protected function setUp(): void { |
| 48 | + parent::setUp(); |
| 49 | + |
| 50 | + $this->activityBackend = $this->createMock(ActivityBackend::class); |
| 51 | + $this->logger = $this->createMock(LoggerInterface::class); |
| 52 | + |
| 53 | + $this->listener = new ActivityUpdaterListener( |
| 54 | + $this->activityBackend, |
| 55 | + $this->logger |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * @dataProvider dataForTestHandleCalendarObjectDeletedEvent |
| 61 | + */ |
| 62 | + public function testHandleCalendarObjectDeletedEvent(int $calendarId, array $calendarData, array $shares, array $objectData, bool $createsActivity): void { |
| 63 | + $event = new CalendarObjectDeletedEvent($calendarId, $calendarData, $shares, $objectData); |
| 64 | + $this->logger->expects($this->once())->method('debug')->with( |
| 65 | + $createsActivity ? "Activity generated for deleted calendar object in calendar $calendarId" : "Calendar object in calendar $calendarId was already in trashbin, skipping deletion activity" |
| 66 | + ); |
| 67 | + $this->activityBackend->expects($createsActivity ? $this->once() : $this->never())->method('onTouchCalendarObject')->with( |
| 68 | + Event::SUBJECT_OBJECT_DELETE, |
| 69 | + $calendarData, |
| 70 | + $shares, |
| 71 | + $objectData |
| 72 | + ); |
| 73 | + $this->listener->handle($event); |
| 74 | + } |
| 75 | + |
| 76 | + public function dataForTestHandleCalendarObjectDeletedEvent(): array { |
| 77 | + return [ |
| 78 | + [1, [], [], [], true], |
| 79 | + [1, [], [], ['{' . SharingPlugin::NS_NEXTCLOUD . '}deleted-at' => 120], false], |
| 80 | + ]; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * @dataProvider dataForTestHandleCalendarDeletedEvent |
| 85 | + */ |
| 86 | + public function testHandleCalendarDeletedEvent(int $calendarId, array $calendarData, array $shares, bool $createsActivity): void { |
| 87 | + $event = new CalendarDeletedEvent($calendarId, $calendarData, $shares); |
| 88 | + $this->logger->expects($this->once())->method('debug')->with( |
| 89 | + $createsActivity ? "Activity generated for deleted calendar $calendarId" : "Calendar $calendarId was already in trashbin, skipping deletion activity" |
| 90 | + ); |
| 91 | + $this->activityBackend->expects($createsActivity ? $this->once() : $this->never())->method('onCalendarDelete')->with( |
| 92 | + $calendarData, |
| 93 | + $shares |
| 94 | + ); |
| 95 | + $this->listener->handle($event); |
| 96 | + } |
| 97 | + |
| 98 | + public function dataForTestHandleCalendarDeletedEvent(): array { |
| 99 | + return [ |
| 100 | + [1, [], [], true], |
| 101 | + [1, ['{' . SharingPlugin::NS_NEXTCLOUD . '}deleted-at' => 120], [], false], |
| 102 | + ]; |
| 103 | + } |
| 104 | +} |
0 commit comments