aboutsummaryrefslogtreecommitdiffstats
path: root/apps/dav/tests/unit/CalDAV/AppCalendar/CalendarObjectTest.php
blob: 92288f5dfb020bf5fc4194a9d2da38b031ecaf32 (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php

/**
 * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */
namespace OCA\DAV\Tests\unit\CalDAV\AppCalendar;

use OCA\DAV\CalDAV\AppCalendar\AppCalendar;
use OCA\DAV\CalDAV\AppCalendar\CalendarObject;
use OCP\Calendar\ICalendar;
use OCP\Calendar\ICreateFromString;
use OCP\Constants;
use PHPUnit\Framework\MockObject\MockObject;
use Sabre\VObject\Component\VCalendar;
use Test\TestCase;

class CalendarObjectTest extends TestCase {
	private CalendarObject $calendarObject;
	private AppCalendar|MockObject $calendar;
	private ICalendar|MockObject $backend;
	private VCalendar|MockObject $vobject;

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

		$this->calendar = $this->createMock(AppCalendar::class);
		$this->calendar->method('getOwner')->willReturn('owner');
		$this->calendar->method('getGroup')->willReturn('group');

		$this->backend = $this->createMock(ICalendar::class);
		$this->vobject = $this->createMock(VCalendar::class);
		$this->calendarObject = new CalendarObject($this->calendar, $this->backend, $this->vobject);
	}

	public function testGetOwner() {
		$this->assertEquals($this->calendarObject->getOwner(), 'owner');
	}

	public function testGetGroup() {
		$this->assertEquals($this->calendarObject->getGroup(), 'group');
	}

	public function testGetACL() {
		$this->calendar->expects($this->exactly(2))
			->method('getPermissions')
			->willReturnOnConsecutiveCalls(Constants::PERMISSION_READ, Constants::PERMISSION_ALL);

		// read only
		$this->assertEquals($this->calendarObject->getACL(), [
			[
				'privilege' => '{DAV:}read',
				'principal' => 'owner',
				'protected' => true,
			]
		]);

		// write permissions
		$this->assertEquals($this->calendarObject->getACL(), [
			[
				'privilege' => '{DAV:}read',
				'principal' => 'owner',
				'protected' => true,
			],
			[
				'privilege' => '{DAV:}write-content',
				'principal' => 'owner',
				'protected' => true,
			]
		]);
	}

	public function testSetACL() {
		$this->expectException(\Sabre\DAV\Exception\Forbidden::class);
		$this->calendarObject->setACL([]);
	}

	public function testPut_readOnlyBackend() {
		$this->expectException(\Sabre\DAV\Exception\Forbidden::class);
		$this->calendarObject->put('foo');
	}

	public function testPut_noPermissions() {
		$this->expectException(\Sabre\DAV\Exception\Forbidden::class);

		$backend = $this->createMock(ICreateFromString::class);
		$calendarObject = new CalendarObject($this->calendar, $backend, $this->vobject);

		$this->calendar->expects($this->once())
			->method('getPermissions')
			->willReturn(Constants::PERMISSION_READ);

		$calendarObject->put('foo');
	}

	public function testPut() {
		$backend = $this->createMock(ICreateFromString::class);
		$calendarObject = new CalendarObject($this->calendar, $backend, $this->vobject);

		$this->vobject->expects($this->once())
			->method('getBaseComponent')
			->willReturn((object)['UID' => 'someid']);
		$this->calendar->expects($this->once())
			->method('getPermissions')
			->willReturn(Constants::PERMISSION_ALL);

		$backend->expects($this->once())
			->method('createFromString')
			->with('someid.ics', 'foo');
		$calendarObject->put('foo');
	}

	public function testGet() {
		$this->vobject->expects($this->once())
			->method('serialize')
			->willReturn('foo');
		$this->assertEquals($this->calendarObject->get(), 'foo');
	}

	public function testDelete_notWriteable() {
		$this->expectException(\Sabre\DAV\Exception\Forbidden::class);
		$this->calendarObject->delete();
	}

	public function testDelete_noPermission() {
		$backend = $this->createMock(ICreateFromString::class);
		$calendarObject = new CalendarObject($this->calendar, $backend, $this->vobject);

		$this->expectException(\Sabre\DAV\Exception\Forbidden::class);
		$calendarObject->delete();
	}

	public function testDelete() {
		$backend = $this->createMock(ICreateFromString::class);
		$calendarObject = new CalendarObject($this->calendar, $backend, $this->vobject);

		$components = [(new VCalendar(['VEVENT' => ['UID' => 'someid']]))->getBaseComponent()];

		$this->calendar->expects($this->once())
			->method('getPermissions')
			->willReturn(Constants::PERMISSION_DELETE);
		$this->vobject->expects($this->once())
			->method('getBaseComponents')
			->willReturn($components);
		$this->vobject->expects($this->once())
			->method('getBaseComponent')
			->willReturn($components[0]);

		$backend->expects($this->once())
			->method('createFromString')
			->with('someid.ics', self::callback(fn ($data): bool => preg_match('/BEGIN:VEVENT(.|\r\n)+STATUS:CANCELLED/', $data) === 1));

		$calendarObject->delete();
	}

	public function testGetName() {
		$this->vobject->expects($this->exactly(2))
			->method('getBaseComponent')
			->willReturnOnConsecutiveCalls((object)['UID' => 'someid'], (object)['UID' => 'someid', 'X-FILENAME' => 'real-filename.ics']);

		$this->assertEquals($this->calendarObject->getName(), 'someid.ics');
		$this->assertEquals($this->calendarObject->getName(), 'real-filename.ics');
	}

	public function testSetName() {
		$this->expectException(\Sabre\DAV\Exception\Forbidden::class);
		$this->calendarObject->setName('Some name');
	}
}