Files
nextcloud-server-mirror/apps/comments/tests/Unit/Controller/NotificationsTest.php
Côme Chilliet 611180d728 chore: Replace $this->mocks[… by $this->getAutoMock(… in tests
This is clearer, safer and better typed.

Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
2026-10-01 15:45:42 +02:00

201 lines
5.7 KiB
PHP

<?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\Comments\Tests\Unit\Controller;
use OCA\Comments\Controller\NotificationsController;
use OCP\AppFramework\Http\NotFoundResponse;
use OCP\AppFramework\Http\RedirectResponse;
use OCP\Comments\IComment;
use OCP\Comments\ICommentsManager;
use OCP\Comments\NotFoundException;
use OCP\Files\IRootFolder;
use OCP\Files\IUserFolder;
use OCP\Files\Node;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\IUserSession;
use OCP\Notification\IManager;
use OCP\Notification\INotification;
use Test\TestCase;
class NotificationsTest extends TestCase {
protected NotificationsController $notificationsController;
#[\Override]
protected function setUp(): void {
parent::setUp();
$this->notificationsController = $this->createInstanceWithMocks(
NotificationsController::class,
[
'appName' => 'comments',
]
);
}
public function testViewGuestRedirect(): void {
$this->getAutoMock(ICommentsManager::class)->expects($this->never())
->method('get');
$this->getAutoMock(IRootFolder::class)->expects($this->never())
->method('getUserFolder');
$this->getAutoMock(IUserSession::class)->expects($this->once())
->method('getUser')
->willReturn(null);
$this->getAutoMock(IManager::class)->expects($this->never())
->method('createNotification');
$this->getAutoMock(IManager::class)->expects($this->never())
->method('markProcessed');
$this->getAutoMock(IURLGenerator::class)->expects($this->exactly(2))
->method('linkToRoute')
->willReturnMap([
['comments.Notifications.view', ['id' => '42'], 'link-to-comment'],
['core.login.showLoginForm', ['redirect_url' => 'link-to-comment'], 'link-to-login'],
]);
/** @var RedirectResponse $response */
$response = $this->notificationsController->view('42');
$this->assertInstanceOf(RedirectResponse::class, $response);
$this->assertSame('link-to-login', $response->getRedirectURL());
}
public function testViewSuccess(): void {
$comment = $this->createMock(IComment::class);
$comment->expects($this->any())
->method('getObjectType')
->willReturn('files');
$comment->expects($this->any())
->method('getId')
->willReturn('1234');
$this->getAutoMock(ICommentsManager::class)->expects($this->any())
->method('get')
->with('42')
->willReturn($comment);
$file = $this->createMock(Node::class);
$folder = $this->createMock(IUserFolder::class);
$user = $this->createMock(IUser::class);
$this->getAutoMock(IRootFolder::class)->expects($this->once())
->method('getUserFolder')
->willReturn($folder);
$folder->expects($this->once())
->method('getFirstNodeById')
->willReturn($file);
$this->getAutoMock(IUserSession::class)->expects($this->once())
->method('getUser')
->willReturn($user);
$user->expects($this->any())
->method('getUID')
->willReturn('user');
$notification = $this->createMock(INotification::class);
$notification->expects($this->any())
->method($this->anything())
->willReturn($notification);
$this->getAutoMock(IManager::class)->expects($this->once())
->method('createNotification')
->willReturn($notification);
$this->getAutoMock(IManager::class)->expects($this->once())
->method('markProcessed')
->with($notification);
$response = $this->notificationsController->view('42');
$this->assertInstanceOf(RedirectResponse::class, $response);
}
public function testViewInvalidComment(): void {
$this->getAutoMock(ICommentsManager::class)->expects($this->any())
->method('get')
->with('42')
->willThrowException(new NotFoundException());
$this->getAutoMock(IRootFolder::class)->expects($this->never())
->method('getUserFolder');
$user = $this->createMock(IUser::class);
$this->getAutoMock(IUserSession::class)->expects($this->once())
->method('getUser')
->willReturn($user);
$user->expects($this->any())
->method('getUID')
->willReturn('user');
$this->getAutoMock(IManager::class)->expects($this->never())
->method('createNotification');
$this->getAutoMock(IManager::class)->expects($this->never())
->method('markProcessed');
$response = $this->notificationsController->view('42');
$this->assertInstanceOf(NotFoundResponse::class, $response);
}
public function testViewNoFile(): void {
$comment = $this->createMock(IComment::class);
$comment->expects($this->any())
->method('getObjectType')
->willReturn('files');
$comment->expects($this->any())
->method('getId')
->willReturn('1234');
$this->getAutoMock(ICommentsManager::class)->expects($this->any())
->method('get')
->with('42')
->willReturn($comment);
$folder = $this->createMock(IUserFolder::class);
$this->getAutoMock(IRootFolder::class)->expects($this->once())
->method('getUserFolder')
->willReturn($folder);
$folder->expects($this->once())
->method('getFirstNodeById')
->willReturn(null);
$user = $this->createMock(IUser::class);
$this->getAutoMock(IUserSession::class)->expects($this->once())
->method('getUser')
->willReturn($user);
$user->expects($this->any())
->method('getUID')
->willReturn('user');
$notification = $this->createMock(INotification::class);
$notification->expects($this->any())
->method($this->anything())
->willReturn($notification);
$this->getAutoMock(IManager::class)->expects($this->once())
->method('createNotification')
->willReturn($notification);
$this->getAutoMock(IManager::class)->expects($this->once())
->method('markProcessed')
->with($notification);
$response = $this->notificationsController->view('42');
$this->assertInstanceOf(NotFoundResponse::class, $response);
}
}