|
8 | 8 | namespace OCA\Files\Tests\Controller; |
9 | 9 |
|
10 | 10 | use OC\Files\FilenameValidator; |
| 11 | +use OC\Route\Router; |
| 12 | +use OC\URLGenerator; |
11 | 13 | use OCA\Files\Controller\ViewController; |
12 | 14 | use OCA\Files\Service\UserConfig; |
13 | 15 | use OCA\Files\Service\ViewConfig; |
|
16 | 18 | use OCP\AppFramework\Http\RedirectResponse; |
17 | 19 | use OCP\AppFramework\Http\TemplateResponse; |
18 | 20 | use OCP\AppFramework\Services\IInitialState; |
| 21 | +use OCP\Diagnostics\IEventLogger; |
19 | 22 | use OCP\EventDispatcher\IEventDispatcher; |
20 | 23 | use OCP\Files\File; |
21 | 24 | use OCP\Files\Folder; |
22 | 25 | use OCP\Files\IRootFolder; |
23 | 26 | use OCP\Files\Template\ITemplateManager; |
| 27 | +use OCP\ICacheFactory; |
24 | 28 | use OCP\IConfig; |
25 | 29 | use OCP\IL10N; |
26 | 30 | use OCP\IRequest; |
27 | 31 | use OCP\IURLGenerator; |
28 | 32 | use OCP\IUser; |
29 | 33 | use OCP\IUserSession; |
30 | 34 | use PHPUnit\Framework\MockObject\MockObject; |
| 35 | +use Psr\Container\ContainerInterface; |
| 36 | +use Psr\Log\LoggerInterface; |
31 | 37 | use Test\TestCase; |
32 | 38 |
|
33 | 39 | /** |
34 | 40 | * Class ViewControllerTest |
35 | 41 | * |
| 42 | + * @group RoutingWeirdness |
| 43 | + * |
36 | 44 | * @package OCA\Files\Tests\Controller |
37 | 45 | */ |
38 | 46 | class ViewControllerTest extends TestCase { |
39 | | - private IRequest&MockObject $request; |
40 | | - private IURLGenerator&MockObject $urlGenerator; |
41 | | - private IL10N&MockObject $l10n; |
| 47 | + private ContainerInterface&MockObject $container; |
| 48 | + private IAppManager&MockObject $appManager; |
| 49 | + private ICacheFactory&MockObject $cacheFactory; |
42 | 50 | private IConfig&MockObject $config; |
43 | 51 | private IEventDispatcher $eventDispatcher; |
44 | | - private IUser&MockObject $user; |
45 | | - private IUserSession&MockObject $userSession; |
46 | | - private IAppManager&MockObject $appManager; |
47 | | - private IRootFolder&MockObject $rootFolder; |
| 52 | + private IEventLogger&MockObject $eventLogger; |
48 | 53 | private IInitialState&MockObject $initialState; |
| 54 | + private IL10N&MockObject $l10n; |
| 55 | + private IRequest&MockObject $request; |
| 56 | + private IRootFolder&MockObject $rootFolder; |
49 | 57 | private ITemplateManager&MockObject $templateManager; |
| 58 | + private IURLGenerator $urlGenerator; |
| 59 | + private IUser&MockObject $user; |
| 60 | + private IUserSession&MockObject $userSession; |
| 61 | + private LoggerInterface&MockObject $logger; |
50 | 62 | private UserConfig&MockObject $userConfig; |
51 | 63 | private ViewConfig&MockObject $viewConfig; |
| 64 | + private Router $router; |
52 | 65 |
|
53 | 66 | private ViewController&MockObject $viewController; |
54 | 67 |
|
55 | 68 | protected function setUp(): void { |
56 | 69 | parent::setUp(); |
57 | | - $this->request = $this->getMockBuilder(IRequest::class)->getMock(); |
58 | | - $this->urlGenerator = $this->getMockBuilder(IURLGenerator::class)->getMock(); |
59 | | - $this->l10n = $this->getMockBuilder(IL10N::class)->getMock(); |
60 | | - $this->config = $this->getMockBuilder(IConfig::class)->getMock(); |
| 70 | + $this->appManager = $this->createMock(IAppManager::class); |
| 71 | + $this->config = $this->createMock(IConfig::class); |
61 | 72 | $this->eventDispatcher = $this->createMock(IEventDispatcher::class); |
62 | | - $this->userSession = $this->getMockBuilder(IUserSession::class)->getMock(); |
63 | | - $this->appManager = $this->getMockBuilder('\OCP\App\IAppManager')->getMock(); |
| 73 | + $this->initialState = $this->createMock(IInitialState::class); |
| 74 | + $this->l10n = $this->createMock(IL10N::class); |
| 75 | + $this->request = $this->createMock(IRequest::class); |
| 76 | + $this->rootFolder = $this->createMock(IRootFolder::class); |
| 77 | + $this->templateManager = $this->createMock(ITemplateManager::class); |
| 78 | + $this->userConfig = $this->createMock(UserConfig::class); |
| 79 | + $this->userSession = $this->createMock(IUserSession::class); |
| 80 | + $this->viewConfig = $this->createMock(ViewConfig::class); |
| 81 | + |
64 | 82 | $this->user = $this->getMockBuilder(IUser::class)->getMock(); |
65 | 83 | $this->user->expects($this->any()) |
66 | 84 | ->method('getUID') |
67 | 85 | ->willReturn('testuser1'); |
68 | 86 | $this->userSession->expects($this->any()) |
69 | 87 | ->method('getUser') |
70 | 88 | ->willReturn($this->user); |
71 | | - $this->rootFolder = $this->getMockBuilder('\OCP\Files\IRootFolder')->getMock(); |
72 | | - $this->initialState = $this->createMock(IInitialState::class); |
73 | | - $this->templateManager = $this->createMock(ITemplateManager::class); |
74 | | - $this->userConfig = $this->createMock(UserConfig::class); |
75 | | - $this->viewConfig = $this->createMock(ViewConfig::class); |
76 | 89 |
|
77 | | - $filenameValidator = $this->createMock(FilenameValidator::class); |
| 90 | + // Make sure we know the app is enabled |
| 91 | + $this->appManager->expects($this->any()) |
| 92 | + ->method('cleanAppId') |
| 93 | + ->willReturnArgument(0); |
| 94 | + $this->appManager->expects($this->any()) |
| 95 | + ->method('getAppPath') |
| 96 | + ->willReturnCallback(fn (string $appid): string => \OC::$SERVERROOT . '/apps/' . $appid); |
| 97 | + |
| 98 | + $this->cacheFactory = $this->createMock(ICacheFactory::class); |
| 99 | + $this->logger = $this->createMock(LoggerInterface::class); |
| 100 | + $this->eventLogger = $this->createMock(IEventLogger::class); |
| 101 | + $this->container = $this->createMock(ContainerInterface::class); |
| 102 | + $this->router = new Router( |
| 103 | + $this->logger, |
| 104 | + $this->request, |
| 105 | + $this->config, |
| 106 | + $this->eventLogger, |
| 107 | + $this->container, |
| 108 | + $this->appManager, |
| 109 | + ); |
78 | 110 |
|
| 111 | + // Create a real URLGenerator instance to generate URLs |
| 112 | + $this->urlGenerator = new URLGenerator( |
| 113 | + $this->config, |
| 114 | + $this->userSession, |
| 115 | + $this->cacheFactory, |
| 116 | + $this->request, |
| 117 | + $this->router |
| 118 | + ); |
| 119 | + |
| 120 | + $filenameValidator = $this->createMock(FilenameValidator::class); |
79 | 121 | $this->viewController = $this->getMockBuilder(ViewController::class) |
80 | 122 | ->setConstructorArgs([ |
81 | 123 | 'files', |
@@ -147,10 +189,60 @@ public function testIndexWithRegularBrowser(): void { |
147 | 189 | $this->assertEquals($expected, $this->viewController->index('MyDir', 'MyView')); |
148 | 190 | } |
149 | 191 |
|
| 192 | + public function dataTestShortRedirect(): array { |
| 193 | + // openfile is true by default |
| 194 | + // opendetails is undefined by default |
| 195 | + // both will be evaluated as truthy |
| 196 | + return [ |
| 197 | + [null, null, '/index.php/apps/files/files/123456?openfile=true'], |
| 198 | + ['', null, '/index.php/apps/files/files/123456?openfile=true'], |
| 199 | + [null, '', '/index.php/apps/files/files/123456?openfile=true&opendetails=true'], |
| 200 | + ['', '', '/index.php/apps/files/files/123456?openfile=true&opendetails=true'], |
| 201 | + ['false', '', '/index.php/apps/files/files/123456?openfile=false'], |
| 202 | + [null, 'false', '/index.php/apps/files/files/123456?openfile=true&opendetails=false'], |
| 203 | + ['true', 'false', '/index.php/apps/files/files/123456?openfile=true&opendetails=false'], |
| 204 | + ['false', 'true', '/index.php/apps/files/files/123456?openfile=false&opendetails=true'], |
| 205 | + ['false', 'false', '/index.php/apps/files/files/123456?openfile=false&opendetails=false'], |
| 206 | + ]; |
| 207 | + } |
| 208 | + |
| 209 | + /** |
| 210 | + * @dataProvider dataTestShortRedirect |
| 211 | + */ |
| 212 | + public function testShortRedirect($openfile, $opendetails, $result) { |
| 213 | + $this->appManager->expects($this->any()) |
| 214 | + ->method('isEnabledForUser') |
| 215 | + ->with('files') |
| 216 | + ->willReturn(true); |
| 217 | + |
| 218 | + $baseFolderFiles = $this->getMockBuilder(Folder::class)->getMock(); |
| 219 | + $this->rootFolder->expects($this->any()) |
| 220 | + ->method('getUserFolder') |
| 221 | + ->with('testuser1') |
| 222 | + ->willReturn($baseFolderFiles); |
| 223 | + |
| 224 | + $parentNode = $this->getMockBuilder(Folder::class)->getMock(); |
| 225 | + $parentNode->expects($this->once()) |
| 226 | + ->method('getPath') |
| 227 | + ->willReturn('testuser1/files/Folder'); |
| 228 | + |
| 229 | + $node = $this->getMockBuilder(File::class)->getMock(); |
| 230 | + $node->expects($this->once()) |
| 231 | + ->method('getParent') |
| 232 | + ->willReturn($parentNode); |
| 233 | + |
| 234 | + $baseFolderFiles->expects($this->any()) |
| 235 | + ->method('getFirstNodeById') |
| 236 | + ->with(123456) |
| 237 | + ->willReturn($node); |
| 238 | + |
| 239 | + $response = $this->viewController->showFile(123456, $opendetails, $openfile); |
| 240 | + $this->assertStringContainsString($result, $response->getHeaders()['Location']); |
| 241 | + } |
| 242 | + |
150 | 243 | public function testShowFileRouteWithTrashedFile(): void { |
151 | | - $this->appManager->expects($this->once()) |
| 244 | + $this->appManager->expects($this->exactly(2)) |
152 | 245 | ->method('isEnabledForUser') |
153 | | - ->with('files_trashbin') |
154 | 246 | ->willReturn(true); |
155 | 247 |
|
156 | 248 | $parentNode = $this->getMockBuilder(Folder::class)->getMock(); |
@@ -189,13 +281,7 @@ public function testShowFileRouteWithTrashedFile(): void { |
189 | 281 | ->with('testuser1/files_trashbin/files/test.d1462861890/sub') |
190 | 282 | ->willReturn('/test.d1462861890/sub'); |
191 | 283 |
|
192 | | - $this->urlGenerator |
193 | | - ->expects($this->once()) |
194 | | - ->method('linkToRoute') |
195 | | - ->with('files.view.indexViewFileid', ['view' => 'trashbin', 'dir' => '/test.d1462861890/sub', 'fileid' => '123']) |
196 | | - ->willReturn('/apps/files/trashbin/123?dir=/test.d1462861890/sub'); |
197 | | - |
198 | | - $expected = new RedirectResponse('/apps/files/trashbin/123?dir=/test.d1462861890/sub'); |
| 284 | + $expected = new RedirectResponse('/index.php/apps/files/trashbin/123?dir=/test.d1462861890/sub'); |
199 | 285 | $this->assertEquals($expected, $this->viewController->index('', '', '123')); |
200 | 286 | } |
201 | 287 | } |
0 commit comments