aboutsummaryrefslogtreecommitdiffstats
path: root/apps/dav/tests/unit/DAV/SystemPrincipalBackendTest.php
blob: 3df861accf2d25af16a2aff63eeb52ea049a5ea8 (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
<?php

declare(strict_types=1);
/**
 * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
 * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
 * SPDX-License-Identifier: AGPL-3.0-only
 */
namespace OCA\DAV\Tests\unit\DAV;

use OCA\DAV\DAV\SystemPrincipalBackend;
use Sabre\DAV\Exception;
use Test\TestCase;

class SystemPrincipalBackendTest extends TestCase {

	#[\PHPUnit\Framework\Attributes\DataProvider('providesPrefix')]
	public function testGetPrincipalsByPrefix(array $expected, string $prefix): void {
		$backend = new SystemPrincipalBackend();
		$result = $backend->getPrincipalsByPrefix($prefix);
		$this->assertEquals($expected, $result);
	}

	public static function providesPrefix(): array {
		return [
			[[], ''],
			[[[
				'uri' => 'principals/system/system',
				'{DAV:}displayname' => 'system',
			],
				[
					'uri' => 'principals/system/public',
					'{DAV:}displayname' => 'public',
				]
			], 'principals/system'],
		];
	}

	#[\PHPUnit\Framework\Attributes\DataProvider('providesPath')]
	public function testGetPrincipalByPath(?array $expected, string $path): void {
		$backend = new SystemPrincipalBackend();
		$result = $backend->getPrincipalByPath($path);
		$this->assertEquals($expected, $result);
	}

	public static function providesPath(): array {
		return [
			[null, ''],
			[null, 'principals'],
			[null, 'principals/system'],
			[[
				'uri' => 'principals/system/system',
				'{DAV:}displayname' => 'system',
			], 'principals/system/system'],
		];
	}

	#[\PHPUnit\Framework\Attributes\DataProvider('providesPrincipalForGetGroupMemberSet')]
	public function testGetGroupMemberSetExceptional(?string $principal): void {
		$this->expectException(Exception::class);
		$this->expectExceptionMessage('Principal not found');

		$backend = new SystemPrincipalBackend();
		$backend->getGroupMemberSet($principal);
	}

	public static function providesPrincipalForGetGroupMemberSet(): array {
		return [
			[null],
			['principals/system'],
		];
	}

	public function testGetGroupMemberSet(): void {
		$backend = new SystemPrincipalBackend();
		$result = $backend->getGroupMemberSet('principals/system/system');
		$this->assertEquals(['principals/system/system'], $result);
	}

	#[\PHPUnit\Framework\Attributes\DataProvider('providesPrincipalForGetGroupMembership')]
	public function testGetGroupMembershipExceptional(string $principal): void {
		$this->expectException(Exception::class);
		$this->expectExceptionMessage('Principal not found');

		$backend = new SystemPrincipalBackend();
		$backend->getGroupMembership($principal);
	}

	public static function providesPrincipalForGetGroupMembership(): array {
		return [
			['principals/system/a'],
		];
	}

	public function testGetGroupMembership(): void {
		$backend = new SystemPrincipalBackend();
		$result = $backend->getGroupMembership('principals/system/system');
		$this->assertEquals([], $result);
	}
}