aboutsummaryrefslogtreecommitdiffstats
path: root/apps/dav/tests/unit/Connector/Sabre/CommentsPropertiesPluginTest.php
blob: a934d6401c2dc288c67a0cf6c8f7eb0f83b04df4 (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
<?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\Connector\Sabre;

use OCA\DAV\Connector\Sabre\CommentPropertiesPlugin as CommentPropertiesPluginImplementation;
use OCA\DAV\Connector\Sabre\Directory;
use OCA\DAV\Connector\Sabre\File;
use OCP\Comments\ICommentsManager;
use OCP\IUser;
use OCP\IUserSession;
use PHPUnit\Framework\MockObject\MockObject;
use Sabre\DAV\PropFind;
use Sabre\DAV\Server;

class CommentsPropertiesPluginTest extends \Test\TestCase {
	protected CommentPropertiesPluginImplementation $plugin;
	protected ICommentsManager&MockObject $commentsManager;
	protected IUserSession&MockObject $userSession;
	protected Server&MockObject $server;

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

		$this->commentsManager = $this->createMock(ICommentsManager::class);
		$this->userSession = $this->createMock(IUserSession::class);
		$this->server = $this->createMock(Server::class);

		$this->plugin = new CommentPropertiesPluginImplementation($this->commentsManager, $this->userSession);
		$this->plugin->initialize($this->server);
	}

	public static function nodeProvider(): array {
		return [
			[File::class, true],
			[Directory::class, true],
			[\Sabre\DAV\INode::class, false]
		];
	}

	#[\PHPUnit\Framework\Attributes\DataProvider('nodeProvider')]
	public function testHandleGetProperties(string $class, bool $expectedSuccessful): void {
		$propFind = $this->createMock(PropFind::class);

		if ($expectedSuccessful) {
			$propFind->expects($this->exactly(3))
				->method('handle');
		} else {
			$propFind->expects($this->never())
				->method('handle');
		}

		$node = $this->createMock($class);
		$this->plugin->handleGetProperties($propFind, $node);
	}

	public static function baseUriProvider(): array {
		return [
			['owncloud/remote.php/webdav/', '4567', 'owncloud/remote.php/dav/comments/files/4567'],
			['owncloud/remote.php/files/', '4567', 'owncloud/remote.php/dav/comments/files/4567'],
			['owncloud/wicked.php/files/', '4567', null]
		];
	}

	#[\PHPUnit\Framework\Attributes\DataProvider('baseUriProvider')]
	public function testGetCommentsLink(string $baseUri, string $fid, ?string $expectedHref): void {
		$node = $this->createMock(File::class);
		$node->expects($this->any())
			->method('getId')
			->willReturn($fid);

		$this->server->expects($this->once())
			->method('getBaseUri')
			->willReturn($baseUri);

		$href = $this->plugin->getCommentsLink($node);
		$this->assertSame($expectedHref, $href);
	}

	public static function userProvider(): array {
		return [
			[IUser::class],
			[null]
		];
	}

	#[\PHPUnit\Framework\Attributes\DataProvider('userProvider')]
	public function testGetUnreadCount(?string $user): void {
		$node = $this->createMock(File::class);
		$node->expects($this->any())
			->method('getId')
			->willReturn('4567');

		if ($user !== null) {
			$user = $this->createMock($user);
		}
		$this->userSession->expects($this->once())
			->method('getUser')
			->willReturn($user);

		$this->commentsManager->expects($this->any())
			->method('getNumberOfCommentsForObject')
			->willReturn(42);

		$unread = $this->plugin->getUnreadCount($node);
		if (is_null($user)) {
			$this->assertNull($unread);
		} else {
			$this->assertSame($unread, 42);
		}
	}
}