nextcloud/apps/dav/tests/unit/Connector/Sabre/CommentsPropertiesPluginTest.php

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

118 lines
3.4 KiB
PHP
Raw Permalink Normal View History

2016-01-27 12:30:09 -05:00
<?php
declare(strict_types=1);
2016-01-27 12:30:09 -05: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
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;
2016-01-27 12:30:09 -05:00
2016-05-25 10:04:15 -04:00
class CommentsPropertiesPluginTest extends \Test\TestCase {
protected CommentPropertiesPluginImplementation $plugin;
protected ICommentsManager&MockObject $commentsManager;
protected IUserSession&MockObject $userSession;
protected Server&MockObject $server;
2016-01-27 12:30:09 -05:00
protected function setUp(): void {
2016-01-27 12:30:09 -05:00
parent::setUp();
$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);
}
public static function nodeProvider(): array {
2016-01-27 12:30:09 -05:00
return [
[File::class, true],
[Directory::class, true],
[\Sabre\DAV\INode::class, false]
2016-01-27 12:30:09 -05:00
];
}
#[\PHPUnit\Framework\Attributes\DataProvider(methodName: 'nodeProvider')]
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');
}
$node = $this->createMock($class);
2016-01-27 12:30:09 -05:00
$this->plugin->handleGetProperties($propFind, $node);
}
public static function baseUriProvider(): array {
2016-01-27 12:30:09 -05:00
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]
2016-01-27 12:30:09 -05:00
];
}
#[\PHPUnit\Framework\Attributes\DataProvider(methodName: 'baseUriProvider')]
public function testGetCommentsLink(string $baseUri, int $fid, ?string $expectedHref): void {
$node = $this->createMock(File::class);
2016-01-27 12:30:09 -05:00
$node->expects($this->any())
->method('getId')
->willReturn($fid);
2016-01-27 12:30:09 -05:00
$this->server->expects($this->once())
->method('getBaseUri')
->willReturn($baseUri);
2016-01-27 12:30:09 -05:00
$href = $this->plugin->getCommentsLink($node);
$this->assertSame($expectedHref, $href);
}
public static function userProvider(): array {
2016-01-27 12:30:09 -05:00
return [
[IUser::class],
2016-01-27 12:30:09 -05:00
[null]
];
}
#[\PHPUnit\Framework\Attributes\DataProvider(methodName: 'userProvider')]
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')
->willReturn(4567);
2016-01-27 12:30:09 -05:00
if ($user !== null) {
$user = $this->createMock($user);
}
2016-01-27 12:30:09 -05:00
$this->userSession->expects($this->once())
->method('getUser')
->willReturn($user);
2016-01-27 12:30:09 -05:00
$this->commentsManager->expects($this->any())
->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);
}
}
}