aboutsummaryrefslogtreecommitdiffstats
path: root/apps/dav/tests/unit/Comments/CommentsNodeTest.php
diff options
context:
space:
mode:
Diffstat (limited to 'apps/dav/tests/unit/Comments/CommentsNodeTest.php')
-rw-r--r--apps/dav/tests/unit/Comments/CommentsNodeTest.php174
1 files changed, 65 insertions, 109 deletions
diff --git a/apps/dav/tests/unit/Comments/CommentsNodeTest.php b/apps/dav/tests/unit/Comments/CommentsNodeTest.php
index f085ace9d89..9e108b4cf63 100644
--- a/apps/dav/tests/unit/Comments/CommentsNodeTest.php
+++ b/apps/dav/tests/unit/Comments/CommentsNodeTest.php
@@ -1,28 +1,10 @@
<?php
+
+declare(strict_types=1);
/**
- * @copyright Copyright (c) 2016, ownCloud, Inc.
- *
- * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
- * @author Christoph Wurst <christoph@winzerhof-wurst.at>
- * @author Joas Schilling <coding@schilljs.com>
- * @author Morris Jobke <hey@morrisjobke.de>
- * @author Roeland Jago Douma <roeland@famdouma.nl>
- * @author Vincent Petry <vincent@nextcloud.com>
- *
- * @license AGPL-3.0
- *
- * This code is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License, version 3,
- * as published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License, version 3,
- * along with this program. If not, see <http://www.gnu.org/licenses/>
- *
+ * 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\Comments;
@@ -30,41 +12,29 @@ use OCA\DAV\Comments\CommentNode;
use OCP\Comments\IComment;
use OCP\Comments\ICommentsManager;
use OCP\Comments\MessageTooLongException;
-use OCP\ILogger;
use OCP\IUser;
use OCP\IUserManager;
use OCP\IUserSession;
+use PHPUnit\Framework\MockObject\MockObject;
+use Psr\Log\LoggerInterface;
use Sabre\DAV\PropPatch;
class CommentsNodeTest extends \Test\TestCase {
-
- /** @var ICommentsManager|\PHPUnit\Framework\MockObject\MockObject */
- protected $commentsManager;
-
- protected $comment;
- protected $node;
- protected $userManager;
- protected $logger;
- protected $userSession;
+ protected ICommentsManager&MockObject $commentsManager;
+ protected IComment&MockObject $comment;
+ protected IUserManager&MockObject $userManager;
+ protected LoggerInterface&MockObject $logger;
+ protected IUserSession&MockObject $userSession;
+ protected CommentNode $node;
protected function setUp(): void {
parent::setUp();
- $this->commentsManager = $this->getMockBuilder(ICommentsManager::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->comment = $this->getMockBuilder(IComment::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->userManager = $this->getMockBuilder(IUserManager::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->userSession = $this->getMockBuilder(IUserSession::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->logger = $this->getMockBuilder(ILogger::class)
- ->disableOriginalConstructor()
- ->getMock();
+ $this->commentsManager = $this->createMock(ICommentsManager::class);
+ $this->comment = $this->createMock(IComment::class);
+ $this->userManager = $this->createMock(IUserManager::class);
+ $this->userSession = $this->createMock(IUserSession::class);
+ $this->logger = $this->createMock(LoggerInterface::class);
$this->node = new CommentNode(
$this->commentsManager,
@@ -75,11 +45,8 @@ class CommentsNodeTest extends \Test\TestCase {
);
}
- public function testDelete() {
- $user = $this->getMockBuilder(IUser::class)
- ->disableOriginalConstructor()
- ->getMock();
-
+ public function testDelete(): void {
+ $user = $this->createMock(IUser::class);
$user->expects($this->once())
->method('getUID')
->willReturn('alice');
@@ -108,13 +75,10 @@ class CommentsNodeTest extends \Test\TestCase {
}
- public function testDeleteForbidden() {
+ public function testDeleteForbidden(): void {
$this->expectException(\Sabre\DAV\Exception\Forbidden::class);
- $user = $this->getMockBuilder(IUser::class)
- ->disableOriginalConstructor()
- ->getMock();
-
+ $user = $this->createMock(IUser::class);
$user->expects($this->once())
->method('getUID')
->willReturn('mallory');
@@ -140,7 +104,7 @@ class CommentsNodeTest extends \Test\TestCase {
$this->node->delete();
}
- public function testGetName() {
+ public function testGetName(): void {
$id = '19';
$this->comment->expects($this->once())
->method('getId')
@@ -150,23 +114,20 @@ class CommentsNodeTest extends \Test\TestCase {
}
- public function testSetName() {
+ public function testSetName(): void {
$this->expectException(\Sabre\DAV\Exception\MethodNotAllowed::class);
$this->node->setName('666');
}
- public function testGetLastModified() {
+ public function testGetLastModified(): void {
$this->assertSame($this->node->getLastModified(), null);
}
- public function testUpdateComment() {
+ public function testUpdateComment(): void {
$msg = 'Hello Earth';
- $user = $this->getMockBuilder(IUser::class)
- ->disableOriginalConstructor()
- ->getMock();
-
+ $user = $this->createMock(IUser::class);
$user->expects($this->once())
->method('getUID')
->willReturn('alice');
@@ -195,16 +156,13 @@ class CommentsNodeTest extends \Test\TestCase {
}
- public function testUpdateCommentLogException() {
+ public function testUpdateCommentLogException(): void {
$this->expectException(\Exception::class);
$this->expectExceptionMessage('buh!');
$msg = null;
- $user = $this->getMockBuilder(IUser::class)
- ->disableOriginalConstructor()
- ->getMock();
-
+ $user = $this->createMock(IUser::class);
$user->expects($this->once())
->method('getUID')
->willReturn('alice');
@@ -216,7 +174,7 @@ class CommentsNodeTest extends \Test\TestCase {
$this->comment->expects($this->once())
->method('setMessage')
->with($msg)
- ->will($this->throwException(new \Exception('buh!')));
+ ->willThrowException(new \Exception('buh!'));
$this->comment->expects($this->any())
->method('getActorType')
@@ -230,20 +188,17 @@ class CommentsNodeTest extends \Test\TestCase {
->method('save');
$this->logger->expects($this->once())
- ->method('logException');
+ ->method('error');
$this->node->updateComment($msg);
}
- public function testUpdateCommentMessageTooLongException() {
+ public function testUpdateCommentMessageTooLongException(): void {
$this->expectException(\Sabre\DAV\Exception\BadRequest::class);
$this->expectExceptionMessage('Message exceeds allowed character limit of');
- $user = $this->getMockBuilder(IUser::class)
- ->disableOriginalConstructor()
- ->getMock();
-
+ $user = $this->createMock(IUser::class);
$user->expects($this->once())
->method('getUID')
->willReturn('alice');
@@ -254,7 +209,7 @@ class CommentsNodeTest extends \Test\TestCase {
$this->comment->expects($this->once())
->method('setMessage')
- ->will($this->throwException(new MessageTooLongException()));
+ ->willThrowException(new MessageTooLongException());
$this->comment->expects($this->any())
->method('getActorType')
@@ -268,22 +223,19 @@ class CommentsNodeTest extends \Test\TestCase {
->method('save');
$this->logger->expects($this->once())
- ->method('logException');
+ ->method('error');
// imagine 'foo' has >1k characters. comment is mocked anyway.
$this->node->updateComment('foo');
}
- public function testUpdateForbiddenByUser() {
+ public function testUpdateForbiddenByUser(): void {
$this->expectException(\Sabre\DAV\Exception\Forbidden::class);
$msg = 'HaXX0r';
- $user = $this->getMockBuilder(IUser::class)
- ->disableOriginalConstructor()
- ->getMock();
-
+ $user = $this->createMock(IUser::class);
$user->expects($this->once())
->method('getUID')
->willReturn('mallory');
@@ -310,15 +262,12 @@ class CommentsNodeTest extends \Test\TestCase {
}
- public function testUpdateForbiddenByType() {
+ public function testUpdateForbiddenByType(): void {
$this->expectException(\Sabre\DAV\Exception\Forbidden::class);
$msg = 'HaXX0r';
- $user = $this->getMockBuilder(IUser::class)
- ->disableOriginalConstructor()
- ->getMock();
-
+ $user = $this->createMock(IUser::class);
$user->expects($this->never())
->method('getUID');
@@ -340,7 +289,7 @@ class CommentsNodeTest extends \Test\TestCase {
}
- public function testUpdateForbiddenByNotLoggedIn() {
+ public function testUpdateForbiddenByNotLoggedIn(): void {
$this->expectException(\Sabre\DAV\Exception\Forbidden::class);
$msg = 'HaXX0r';
@@ -362,11 +311,8 @@ class CommentsNodeTest extends \Test\TestCase {
$this->node->updateComment($msg);
}
- public function testPropPatch() {
- $propPatch = $this->getMockBuilder(PropPatch::class)
- ->disableOriginalConstructor()
- ->getMock();
-
+ public function testPropPatch(): void {
+ $propPatch = $this->createMock(PropPatch::class);
$propPatch->expects($this->once())
->method('handle')
->with('{http://owncloud.org/ns}message');
@@ -374,7 +320,7 @@ class CommentsNodeTest extends \Test\TestCase {
$this->node->propPatch($propPatch);
}
- public function testGetProperties() {
+ public function testGetProperties(): void {
$ns = '{http://owncloud.org/ns}';
$expected = [
$ns . 'id' => '123',
@@ -405,15 +351,20 @@ class CommentsNodeTest extends \Test\TestCase {
$ns . 'referenceId' => 'ref',
$ns . 'isUnread' => null,
$ns . 'reactions' => [],
+ $ns . 'metaData' => [
+ 'last_edited_at' => 1702553770,
+ 'last_edited_by_id' => 'charly',
+ 'last_edited_by_type' => 'user',
+ ],
+ $ns . 'expireDate' => new \DateTime('2016-01-12 19:00:00'),
];
$this->commentsManager->expects($this->exactly(2))
->method('resolveDisplayName')
- ->withConsecutive(
- [$this->equalTo('user'), $this->equalTo('alice')],
- [$this->equalTo('user'), $this->equalTo('bob')]
- )
- ->willReturnOnConsecutiveCalls('Alice Al-Isson', 'Unknown user');
+ ->willReturnMap([
+ ['user', 'alice', 'Alice Al-Isson'],
+ ['user', 'bob', 'Unknown user']
+ ]);
$this->comment->expects($this->once())
->method('getId')
@@ -474,6 +425,14 @@ class CommentsNodeTest extends \Test\TestCase {
->method('getReferenceId')
->willReturn($expected[$ns . 'referenceId']);
+ $this->comment->expects($this->once())
+ ->method('getMetaData')
+ ->willReturn($expected[$ns . 'metaData']);
+
+ $this->comment->expects($this->once())
+ ->method('getExpireDate')
+ ->willReturn($expected[$ns . 'expireDate']);
+
$user = $this->getMockBuilder(IUser::class)
->disableOriginalConstructor()
->getMock();
@@ -489,14 +448,14 @@ class CommentsNodeTest extends \Test\TestCase {
$properties = $this->node->getProperties(null);
foreach ($properties as $name => $value) {
- $this->assertArrayHasKey($name, $expected);
+ $this->assertArrayHasKey($name, $expected, 'Key not found in the list of $expected');
$this->assertSame($expected[$name], $value);
unset($expected[$name]);
}
$this->assertTrue(empty($expected));
}
- public function readCommentProvider() {
+ public static function readCommentProvider(): array {
$creationDT = new \DateTime('2016-01-19 18:48:00');
$diff = new \DateInterval('PT2H');
$readDT1 = clone $creationDT;
@@ -510,11 +469,8 @@ class CommentsNodeTest extends \Test\TestCase {
];
}
- /**
- * @dataProvider readCommentProvider
- * @param $expected
- */
- public function testGetPropertiesUnreadProperty($creationDT, $readDT, $expected) {
+ #[\PHPUnit\Framework\Attributes\DataProvider('readCommentProvider')]
+ public function testGetPropertiesUnreadProperty(\DateTime $creationDT, ?\DateTime $readDT, string $expected): void {
$this->comment->expects($this->any())
->method('getCreationDateTime')
->willReturn($creationDT);