Files
nextcloud-server-mirror/apps/files/tests/Controller/OpenedControllerTest.php
John Molakvoæ 53edf7085f feat(files): add an API to report that a user opened a file
Reading a file is not opening it: sync clients, previews and downloads
all fire BeforeNodeReadEvent. Clients can now call
POST /ocs/v2.php/apps/files/api/v1/opened when the user actually opens
a file or folder, which dispatches a NodeOpenedEvent apps can listen to.

Assisted-by: ClaudeCode:claude-opus-5-5
Signed-off-by: John Molakvoæ <14975046+skjnldsv@users.noreply.github.com>
2026-10-29 03:42:22 +01:00

83 lines
2.5 KiB
PHP

<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Files\Tests\Controller;
use OCA\Files\Controller\OpenedController;
use OCP\AppFramework\OCS\OCSNotFoundException;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\Events\Node\NodeOpenedEvent;
use OCP\Files\File;
use OCP\Files\IRootFolder;
use OCP\Files\IUserFolder;
use OCP\IL10N;
use OCP\IRequest;
use OCP\IUser;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;
class OpenedControllerTest extends TestCase {
private IUserFolder&MockObject $userFolder;
private IEventDispatcher&MockObject $eventDispatcher;
private IUser&MockObject $user;
private OpenedController $controller;
protected function setUp(): void {
parent::setUp();
$this->user = $this->createMock(IUser::class);
$this->user->method('getUID')->willReturn('alice');
$this->userFolder = $this->createMock(IUserFolder::class);
$rootFolder = $this->createMock(IRootFolder::class);
$rootFolder->method('getUserFolder')->with('alice')->willReturn($this->userFolder);
$this->eventDispatcher = $this->createMock(IEventDispatcher::class);
$this->controller = new OpenedController(
'files',
$this->createMock(IRequest::class),
$rootFolder,
$this->eventDispatcher,
$this->createMock(IL10N::class),
);
}
public function testDispatchesEvent(): void {
$file = $this->createMock(File::class);
$file->method('isReadable')->willReturn(true);
$this->userFolder->method('getFirstNodeById')->with(42)->willReturn($file);
$this->eventDispatcher->expects($this->once())
->method('dispatchTyped')
->with($this->callback(fn (NodeOpenedEvent $event): bool
=> $event->getNode() === $file && $event->getUser() === $this->user));
$this->controller->opened($this->user, 42);
}
public function testNotFound(): void {
$this->userFolder->method('getFirstNodeById')->with(42)->willReturn(null);
$this->eventDispatcher->expects($this->never())->method('dispatchTyped');
$this->expectException(OCSNotFoundException::class);
$this->controller->opened($this->user, 42);
}
public function testNotReadable(): void {
$file = $this->createMock(File::class);
$file->method('isReadable')->willReturn(false);
$this->userFolder->method('getFirstNodeById')->with(42)->willReturn($file);
$this->eventDispatcher->expects($this->never())->method('dispatchTyped');
$this->expectException(OCSNotFoundException::class);
$this->controller->opened($this->user, 42);
}
}