aboutsummaryrefslogtreecommitdiffstats
path: root/apps/dav/tests/unit/CalDAV/Export/ExportServiceTest.php
blob: 838dfc18f2fdf80c5a3972b4338de0d809cf2434 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php

declare(strict_types=1);
/**
 * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */
namespace OCA\DAV\Tests\unit\CalDAV\Export;

use Generator;
use OCA\DAV\CalDAV\Export\ExportService;
use OCP\Calendar\CalendarExportOptions;
use OCP\Calendar\ICalendarExport;
use OCP\ServerVersion;
use PHPUnit\Framework\MockObject\MockObject;
use Sabre\VObject\Component\VCalendar;

class ExportServiceTest extends \Test\TestCase {
	private ServerVersion&MockObject $serverVersion;
	private ExportService $service;
	private ICalendarExport&MockObject $calendar;
	private array $mockExportCollection;

	protected function setUp(): void {
		parent::setUp();

		$this->serverVersion = $this->createMock(ServerVersion::class);
		$this->serverVersion->method('getVersionString')
			->willReturn('32.0.0.0');
		$this->service = new ExportService($this->serverVersion);
		$this->calendar = $this->createMock(ICalendarExport::class);

	}

	protected function mockGenerator(): Generator {
		foreach ($this->mockExportCollection as $entry) {
			yield $entry;
		}
	}

	public function testExport(): void {
		// Arrange
		// construct calendar with a 1 hour event and same start/end time zones
		$vCalendar = new VCalendar();
		/** @var \Sabre\VObject\Component\VEvent $vEvent */
		$vEvent = $vCalendar->add('VEVENT', []);
		$vEvent->UID->setValue('96a0e6b1-d886-4a55-a60d-152b31401dcc');
		$vEvent->add('DTSTART', '20240701T080000', ['TZID' => 'America/Toronto']);
		$vEvent->add('DTEND', '20240701T090000', ['TZID' => 'America/Toronto']);
		$vEvent->add('SUMMARY', 'Test Recurrence Event');
		$vEvent->add('ORGANIZER', 'mailto:organizer@testing.com', ['CN' => 'Organizer']);
		$vEvent->add('ATTENDEE', 'mailto:attendee1@testing.com', [
			'CN' => 'Attendee One',
			'CUTYPE' => 'INDIVIDUAL',
			'PARTSTAT' => 'NEEDS-ACTION',
			'ROLE' => 'REQ-PARTICIPANT',
			'RSVP' => 'TRUE'
		]);
		// construct calendar return
		$options = new CalendarExportOptions();
		$this->mockExportCollection[] = $vCalendar;
		$this->calendar->expects($this->once())
			->method('export')
			->with($options)
			->willReturn($this->mockGenerator());

		// Act
		$document = '';
		foreach ($this->service->export($this->calendar, $options) as $chunk) {
			$document .= $chunk;
		}

		// Assert
		$this->assertStringContainsString('BEGIN:VCALENDAR', $document, 'Exported document calendar start missing');
		$this->assertStringContainsString('BEGIN:VEVENT', $document, 'Exported document event start missing');
		$this->assertStringContainsString('END:VEVENT', $document, 'Exported document event end missing');
		$this->assertStringContainsString('END:VCALENDAR', $document, 'Exported document calendar end missing');

	}

}