From f9ecb28d8b40b5bab79954bdc675cd1d6d111010 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Molakvo=C3=A6?= <14975046+skjnldsv@users.noreply.github.com> Date: Mon, 5 Oct 2026 09:03:03 +0200 Subject: [PATCH] fix(files): keep the view when reloading a file opened outside the files view MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Assisted-by: ClaudeCode:claude-opus-5-5 Signed-off-by: John Molakvoæ <14975046+skjnldsv@users.noreply.github.com> --- apps/files/lib/Controller/ViewController.php | 5 +-- .../tests/Controller/ViewControllerTest.php | 32 +++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/apps/files/lib/Controller/ViewController.php b/apps/files/lib/Controller/ViewController.php index d826f6e8745aa..c7506c002d0e2 100644 --- a/apps/files/lib/Controller/ViewController.php +++ b/apps/files/lib/Controller/ViewController.php @@ -151,8 +151,9 @@ public function index($dir = '', $view = '', $fileid = null) { // If the file doesn't exists in the folder and // exists in only one occurrence, redirect to that file - // in the correct folder - if ($fileid && $dir !== '') { + // in the correct folder. Only where dir is a path in the + // user's files: other views use it for their own ids (tags). + if ($fileid && $dir !== '' && in_array($view, ['', 'files', 'personal'], true)) { $baseFolder = $this->rootFolder->getUserFolder($userId); $nodes = $baseFolder->getById((int)$fileid); if (!empty($nodes)) { diff --git a/apps/files/tests/Controller/ViewControllerTest.php b/apps/files/tests/Controller/ViewControllerTest.php index 1738cfa847c1e..a18bb8f4110eb 100644 --- a/apps/files/tests/Controller/ViewControllerTest.php +++ b/apps/files/tests/Controller/ViewControllerTest.php @@ -207,6 +207,38 @@ public function testIndexWithRegularBrowser(): void { $this->assertEquals($expected, $this->viewController->index('MyDir', 'MyView')); } + public static function dataTestRedirectToFileFolder(): array { + return [ + 'files view' => ['files', true], + 'no view' => ['', true], + 'personal files' => ['personal', true], + // The tags view puts the tag id in dir + 'tags view' => ['tags', false], + ]; + } + + #[\PHPUnit\Framework\Attributes\DataProvider(methodName: 'dataTestRedirectToFileFolder')] + public function testRedirectToFileFolder(string $view, bool $redirects): void { + $this->viewController->method('getStorageInfo')->willReturn([]); + $this->config->method('getUserValue')->willReturnArgument(3); + + $node = $this->createMock(File::class); + $node->method('getPath')->willReturn('/testuser1/files/Folder/photo.jpg'); + $node->method('getParent')->willReturn($this->createMock(Folder::class)); + + $baseFolderFiles = $this->createMock(IUserFolder::class); + $baseFolderFiles->method('getById')->with(123)->willReturn([$node]); + $baseFolderFiles->method('getFirstNodeById')->with(123)->willReturn($node); + $baseFolderFiles->method('getRelativePath')->willReturnMap([ + ['/testuser1/files/Folder/photo.jpg', '/Folder/photo.jpg'], + ['', '/Folder'], + ]); + $this->rootFolder->method('getUserFolder')->with('testuser1')->willReturn($baseFolderFiles); + + $response = $this->viewController->index('/1', $view, '123'); + $this->assertSame($redirects, $response instanceof RedirectResponse); + } + public static function dataTestShortRedirect(): array { // openfile is true by default // opendetails is undefined by default