2016-01-27 12:30:09 -05:00
|
|
|
<?php
|
2024-05-28 06:34:11 -04:00
|
|
|
|
2025-05-27 17:36:08 -04:00
|
|
|
declare(strict_types=1);
|
2016-01-27 12:30:09 -05:00
|
|
|
/**
|
2024-05-28 06:34:11 -04:00
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2016-01-27 12:30:09 -05:00
|
|
|
*/
|
2016-05-25 10:04:15 -04:00
|
|
|
namespace OCA\DAV\Tests\unit\Connector\Sabre;
|
2016-01-27 12:30:09 -05:00
|
|
|
|
2019-11-22 14:52:10 -05:00
|
|
|
use OCA\DAV\Connector\Sabre\CommentPropertiesPlugin as CommentPropertiesPluginImplementation;
|
2025-05-27 17:36:08 -04:00
|
|
|
use OCA\DAV\Connector\Sabre\Directory;
|
2017-10-24 09:26:53 -04:00
|
|
|
use OCA\DAV\Connector\Sabre\File;
|
|
|
|
|
use OCP\Comments\ICommentsManager;
|
|
|
|
|
use OCP\IUser;
|
|
|
|
|
use OCP\IUserSession;
|
2025-05-27 17:36:08 -04:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2017-10-24 18:03:28 -04:00
|
|
|
use Sabre\DAV\PropFind;
|
2025-05-27 17:36:08 -04:00
|
|
|
use Sabre\DAV\Server;
|
2016-01-27 12:30:09 -05:00
|
|
|
|
2016-05-25 10:04:15 -04:00
|
|
|
class CommentsPropertiesPluginTest extends \Test\TestCase {
|
2025-05-27 17:36:08 -04:00
|
|
|
protected CommentPropertiesPluginImplementation $plugin;
|
|
|
|
|
protected ICommentsManager&MockObject $commentsManager;
|
|
|
|
|
protected IUserSession&MockObject $userSession;
|
|
|
|
|
protected Server&MockObject $server;
|
2016-01-27 12:30:09 -05:00
|
|
|
|
2019-11-27 09:27:18 -05:00
|
|
|
protected function setUp(): void {
|
2016-01-27 12:30:09 -05:00
|
|
|
parent::setUp();
|
|
|
|
|
|
2025-05-27 17:36:08 -04:00
|
|
|
$this->commentsManager = $this->createMock(ICommentsManager::class);
|
|
|
|
|
$this->userSession = $this->createMock(IUserSession::class);
|
|
|
|
|
$this->server = $this->createMock(Server::class);
|
2016-01-27 12:30:09 -05:00
|
|
|
|
|
|
|
|
$this->plugin = new CommentPropertiesPluginImplementation($this->commentsManager, $this->userSession);
|
|
|
|
|
$this->plugin->initialize($this->server);
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-27 17:36:08 -04:00
|
|
|
public static function nodeProvider(): array {
|
2016-01-27 12:30:09 -05:00
|
|
|
return [
|
2025-05-27 17:36:08 -04:00
|
|
|
[File::class, true],
|
|
|
|
|
[Directory::class, true],
|
|
|
|
|
[\Sabre\DAV\INode::class, false]
|
2016-01-27 12:30:09 -05:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-07 08:21:48 -05:00
|
|
|
#[\PHPUnit\Framework\Attributes\DataProvider(methodName: 'nodeProvider')]
|
2025-05-27 17:36:08 -04:00
|
|
|
public function testHandleGetProperties(string $class, bool $expectedSuccessful): void {
|
|
|
|
|
$propFind = $this->createMock(PropFind::class);
|
2016-01-27 12:30:09 -05:00
|
|
|
|
|
|
|
|
if ($expectedSuccessful) {
|
|
|
|
|
$propFind->expects($this->exactly(3))
|
|
|
|
|
->method('handle');
|
|
|
|
|
} else {
|
|
|
|
|
$propFind->expects($this->never())
|
|
|
|
|
->method('handle');
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-27 17:36:08 -04:00
|
|
|
$node = $this->createMock($class);
|
2016-01-27 12:30:09 -05:00
|
|
|
$this->plugin->handleGetProperties($propFind, $node);
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-27 17:36:08 -04:00
|
|
|
public static function baseUriProvider(): array {
|
2016-01-27 12:30:09 -05:00
|
|
|
return [
|
2026-02-12 06:05:11 -05:00
|
|
|
['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]
|
2016-01-27 12:30:09 -05:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-07 08:21:48 -05:00
|
|
|
#[\PHPUnit\Framework\Attributes\DataProvider(methodName: 'baseUriProvider')]
|
2026-02-12 06:05:11 -05:00
|
|
|
public function testGetCommentsLink(string $baseUri, int $fid, ?string $expectedHref): void {
|
2025-05-27 17:36:08 -04:00
|
|
|
$node = $this->createMock(File::class);
|
2016-01-27 12:30:09 -05:00
|
|
|
$node->expects($this->any())
|
|
|
|
|
->method('getId')
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturn($fid);
|
2016-01-27 12:30:09 -05:00
|
|
|
|
|
|
|
|
$this->server->expects($this->once())
|
|
|
|
|
->method('getBaseUri')
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturn($baseUri);
|
2016-01-27 12:30:09 -05:00
|
|
|
|
|
|
|
|
$href = $this->plugin->getCommentsLink($node);
|
|
|
|
|
$this->assertSame($expectedHref, $href);
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-27 17:36:08 -04:00
|
|
|
public static function userProvider(): array {
|
2016-01-27 12:30:09 -05:00
|
|
|
return [
|
2025-05-27 17:36:08 -04:00
|
|
|
[IUser::class],
|
2016-01-27 12:30:09 -05:00
|
|
|
[null]
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-07 08:21:48 -05:00
|
|
|
#[\PHPUnit\Framework\Attributes\DataProvider(methodName: 'userProvider')]
|
2025-05-27 17:36:08 -04:00
|
|
|
public function testGetUnreadCount(?string $user): void {
|
|
|
|
|
$node = $this->createMock(File::class);
|
2016-01-27 12:30:09 -05:00
|
|
|
$node->expects($this->any())
|
|
|
|
|
->method('getId')
|
2026-02-12 06:05:11 -05:00
|
|
|
->willReturn(4567);
|
2016-01-27 12:30:09 -05:00
|
|
|
|
2025-05-27 17:36:08 -04:00
|
|
|
if ($user !== null) {
|
|
|
|
|
$user = $this->createMock($user);
|
|
|
|
|
}
|
2016-01-27 12:30:09 -05:00
|
|
|
$this->userSession->expects($this->once())
|
|
|
|
|
->method('getUser')
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturn($user);
|
2016-01-27 12:30:09 -05:00
|
|
|
|
|
|
|
|
$this->commentsManager->expects($this->any())
|
2025-08-13 10:15:31 -04:00
|
|
|
->method('getNumberOfUnreadCommentsForObjects')
|
|
|
|
|
->willReturn(['4567' => 42]);
|
2016-01-27 12:30:09 -05:00
|
|
|
|
|
|
|
|
$unread = $this->plugin->getUnreadCount($node);
|
|
|
|
|
if (is_null($user)) {
|
|
|
|
|
$this->assertNull($unread);
|
|
|
|
|
} else {
|
|
|
|
|
$this->assertSame($unread, 42);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|