aboutsummaryrefslogtreecommitdiffstats
path: root/apps/dav/tests/unit/CalDAV/AppCalendar/AppCalendarTest.php
blob: 84879e872385630f11c5a24721297b2cfa357c11 (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
<?php

declare(strict_types=1);
/**
 * 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 OCP\Calendar\ICalendar;
use OCP\Calendar\ICreateFromString;
use OCP\Constants;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;

use function rewind;

class AppCalendarTest extends TestCase {
	private string $principal = 'principals/users/foo';

	private AppCalendar $appCalendar;
	private AppCalendar $writeableAppCalendar;

	private ICalendar&MockObject $calendar;
	private ICalendar&MockObject $writeableCalendar;

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

		$this->calendar = $this->getMockBuilder(ICalendar::class)->getMock();
		$this->calendar->method('getPermissions')
			->willReturn(Constants::PERMISSION_READ);

		$this->writeableCalendar = $this->getMockBuilder(ICreateFromString::class)->getMock();
		$this->writeableCalendar->method('getPermissions')
			->willReturn(Constants::PERMISSION_READ | Constants::PERMISSION_CREATE);

		$this->appCalendar = new AppCalendar('dav-wrapper', $this->calendar, $this->principal);
		$this->writeableAppCalendar = new AppCalendar('dav-wrapper', $this->writeableCalendar, $this->principal);
	}

	public function testGetPrincipal():void {
		// Check that the correct name is returned
		$this->assertEquals($this->principal, $this->appCalendar->getOwner());
		$this->assertEquals($this->principal, $this->writeableAppCalendar->getOwner());
	}

	public function testDelete(): void {
		$this->expectException(\Sabre\DAV\Exception\Forbidden::class);
		$this->expectExceptionMessage('Deleting an entry is not implemented');

		$this->appCalendar->delete();
	}

	public function testCreateFile(): void {
		$calls = [
			['some-name', 'data'],
			['other-name', ''],
			['name', 'some data'],
		];
		$this->writeableCalendar->expects($this->exactly(3))
			->method('createFromString')
			->willReturnCallback(function () use (&$calls): void {
				$expected = array_shift($calls);
				$this->assertEquals($expected, func_get_args());
			});

		// pass data
		$this->assertNull($this->writeableAppCalendar->createFile('some-name', 'data'));
		// null is empty string
		$this->assertNull($this->writeableAppCalendar->createFile('other-name', null));
		// resource to data
		$fp = fopen('php://memory', 'r+');
		fwrite($fp, 'some data');
		rewind($fp);
		$this->assertNull($this->writeableAppCalendar->createFile('name', $fp));
		fclose($fp);
	}

	public function testCreateFile_readOnly(): void {
		// If writing is not supported
		$this->expectException(\Sabre\DAV\Exception\Forbidden::class);
		$this->expectExceptionMessage('Creating a new entry is not allowed');

		$this->appCalendar->createFile('some-name', 'data');
	}

	public function testSetACL(): void {
		$this->expectException(\Sabre\DAV\Exception\Forbidden::class);
		$this->expectExceptionMessage('Setting ACL is not supported on this node');

		$this->appCalendar->setACL([]);
	}

	public function testGetACL():void {
		$expectedRO = [
			[
				'privilege' => '{DAV:}read',
				'principal' => $this->principal,
				'protected' => true,
			],
			[
				'privilege' => '{DAV:}write-properties',
				'principal' => $this->principal,
				'protected' => true,
			]
		];
		$expectedRW = $expectedRO;
		$expectedRW[] = [
			'privilege' => '{DAV:}write',
			'principal' => $this->principal,
			'protected' => true,
		];

		// Check that the correct ACL is returned (default be only readable)
		$this->assertEquals($expectedRO, $this->appCalendar->getACL());
		$this->assertEquals($expectedRW, $this->writeableAppCalendar->getACL());
	}
}