diff --git a/lib/Chat/ChatManager.php b/lib/Chat/ChatManager.php index a0339b1cf4..91df8d54ad 100644 --- a/lib/Chat/ChatManager.php +++ b/lib/Chat/ChatManager.php @@ -814,10 +814,10 @@ class ChatManager { return array_filter($comments, function (IComment $comment) { if ($this->isSharedFile($comment->getMessage())) { if (!$this->fileOfMessageExists($comment->getMessage())) { - return true; + return false; } } - return false; + return true; }); } } diff --git a/tests/php/Chat/ChatManagerTest.php b/tests/php/Chat/ChatManagerTest.php index 82b775dc69..a7039e660f 100644 --- a/tests/php/Chat/ChatManagerTest.php +++ b/tests/php/Chat/ChatManagerTest.php @@ -764,4 +764,36 @@ class ChatManagerTest extends TestCase { [json_encode(['parameters' => ['share' => 1]]), true], ]; } + + /** + * @dataProvider dataRemoveFileNotExists + */ + public function testRemoveFileNotExists(array $list, int $expectedCount): void { + // Transform text messages in instance of comment and mock with the message + foreach ($list as $key => $message) { + $list[$key] = $this->createMock(IComment::class); + $list[$key]->method('getMessage') + ->willReturn($message); + $messageDecoded = json_decode($message, true); + if (isset($messageDecoded['parameters']['share']) && $messageDecoded['parameters']['share'] === 'notExists') { + $this->shareProvider->expects($this->once()) + ->method('getShareById') + ->with('notExists') + ->willThrowException(new ShareNotFound()); + } + } + if (count($list) !== $expectedCount) { + } + $result = $this->chatManager->removeFileNotExists($list); + $this->assertCount($expectedCount, $result); + } + + public function dataRemoveFileNotExists(): array { + return [ + [[], 0], + [[json_encode(['parameters' => ['not a shared file']])], 1], + [[json_encode(['parameters' => ['share' => 'notExists']])], 0], + [[json_encode(['parameters' => ['share' => 1]])], 1], + ]; + } }