ViewControllerTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files\Tests\Controller;
  8. use OC\Files\FilenameValidator;
  9. use OCA\Files\Controller\ViewController;
  10. use OCA\Files\Service\UserConfig;
  11. use OCA\Files\Service\ViewConfig;
  12. use OCP\App\IAppManager;
  13. use OCP\AppFramework\Http\ContentSecurityPolicy;
  14. use OCP\AppFramework\Http\RedirectResponse;
  15. use OCP\AppFramework\Http\TemplateResponse;
  16. use OCP\AppFramework\Services\IInitialState;
  17. use OCP\EventDispatcher\IEventDispatcher;
  18. use OCP\Files\File;
  19. use OCP\Files\Folder;
  20. use OCP\Files\IRootFolder;
  21. use OCP\Files\Template\ITemplateManager;
  22. use OCP\IConfig;
  23. use OCP\IL10N;
  24. use OCP\IRequest;
  25. use OCP\IURLGenerator;
  26. use OCP\IUser;
  27. use OCP\IUserSession;
  28. use PHPUnit\Framework\MockObject\MockObject;
  29. use Test\TestCase;
  30. /**
  31. * Class ViewControllerTest
  32. *
  33. * @package OCA\Files\Tests\Controller
  34. */
  35. class ViewControllerTest extends TestCase {
  36. private IRequest&MockObject $request;
  37. private IURLGenerator&MockObject $urlGenerator;
  38. private IL10N&MockObject $l10n;
  39. private IConfig&MockObject $config;
  40. private IEventDispatcher $eventDispatcher;
  41. private IUser&MockObject $user;
  42. private IUserSession&MockObject $userSession;
  43. private IAppManager&MockObject $appManager;
  44. private IRootFolder&MockObject $rootFolder;
  45. private IInitialState&MockObject $initialState;
  46. private ITemplateManager&MockObject $templateManager;
  47. private UserConfig&MockObject $userConfig;
  48. private ViewConfig&MockObject $viewConfig;
  49. private ViewController&MockObject $viewController;
  50. protected function setUp(): void {
  51. parent::setUp();
  52. $this->request = $this->getMockBuilder(IRequest::class)->getMock();
  53. $this->urlGenerator = $this->getMockBuilder(IURLGenerator::class)->getMock();
  54. $this->l10n = $this->getMockBuilder(IL10N::class)->getMock();
  55. $this->config = $this->getMockBuilder(IConfig::class)->getMock();
  56. $this->eventDispatcher = $this->createMock(IEventDispatcher::class);
  57. $this->userSession = $this->getMockBuilder(IUserSession::class)->getMock();
  58. $this->appManager = $this->getMockBuilder('\OCP\App\IAppManager')->getMock();
  59. $this->user = $this->getMockBuilder(IUser::class)->getMock();
  60. $this->user->expects($this->any())
  61. ->method('getUID')
  62. ->willReturn('testuser1');
  63. $this->userSession->expects($this->any())
  64. ->method('getUser')
  65. ->willReturn($this->user);
  66. $this->rootFolder = $this->getMockBuilder('\OCP\Files\IRootFolder')->getMock();
  67. $this->initialState = $this->createMock(IInitialState::class);
  68. $this->templateManager = $this->createMock(ITemplateManager::class);
  69. $this->userConfig = $this->createMock(UserConfig::class);
  70. $this->viewConfig = $this->createMock(ViewConfig::class);
  71. $filenameValidator = $this->createMock(FilenameValidator::class);
  72. $this->viewController = $this->getMockBuilder(ViewController::class)
  73. ->setConstructorArgs([
  74. 'files',
  75. $this->request,
  76. $this->urlGenerator,
  77. $this->l10n,
  78. $this->config,
  79. $this->eventDispatcher,
  80. $this->userSession,
  81. $this->appManager,
  82. $this->rootFolder,
  83. $this->initialState,
  84. $this->templateManager,
  85. $this->userConfig,
  86. $this->viewConfig,
  87. $filenameValidator,
  88. ])
  89. ->onlyMethods([
  90. 'getStorageInfo',
  91. ])
  92. ->getMock();
  93. }
  94. public function testIndexWithRegularBrowser(): void {
  95. $this->viewController
  96. ->expects($this->any())
  97. ->method('getStorageInfo')
  98. ->willReturn([
  99. 'used' => 123,
  100. 'quota' => 100,
  101. 'total' => 100,
  102. 'relative' => 123,
  103. 'owner' => 'MyName',
  104. 'ownerDisplayName' => 'MyDisplayName',
  105. ]);
  106. $this->config
  107. ->method('getUserValue')
  108. ->willReturnMap([
  109. [$this->user->getUID(), 'files', 'file_sorting', 'name', 'name'],
  110. [$this->user->getUID(), 'files', 'file_sorting_direction', 'asc', 'asc'],
  111. [$this->user->getUID(), 'files', 'files_sorting_configs', '{}', '{}'],
  112. [$this->user->getUID(), 'files', 'show_hidden', false, false],
  113. [$this->user->getUID(), 'files', 'crop_image_previews', true, true],
  114. [$this->user->getUID(), 'files', 'show_grid', true],
  115. ]);
  116. $baseFolderFiles = $this->getMockBuilder(Folder::class)->getMock();
  117. $this->rootFolder->expects($this->any())
  118. ->method('getUserFolder')
  119. ->with('testuser1')
  120. ->willReturn($baseFolderFiles);
  121. $this->config
  122. ->expects($this->any())
  123. ->method('getAppValue')
  124. ->willReturnArgument(2);
  125. $expected = new TemplateResponse(
  126. 'files',
  127. 'index',
  128. );
  129. $policy = new ContentSecurityPolicy();
  130. $policy->addAllowedWorkerSrcDomain('\'self\'');
  131. $policy->addAllowedFrameDomain('\'self\'');
  132. $expected->setContentSecurityPolicy($policy);
  133. $this->assertEquals($expected, $this->viewController->index('MyDir', 'MyView'));
  134. }
  135. public function testShowFileRouteWithTrashedFile(): void {
  136. $this->appManager->expects($this->once())
  137. ->method('isEnabledForUser')
  138. ->with('files_trashbin')
  139. ->willReturn(true);
  140. $parentNode = $this->getMockBuilder(Folder::class)->getMock();
  141. $parentNode->expects($this->once())
  142. ->method('getPath')
  143. ->willReturn('testuser1/files_trashbin/files/test.d1462861890/sub');
  144. $baseFolderFiles = $this->getMockBuilder(Folder::class)->getMock();
  145. $baseFolderTrash = $this->getMockBuilder(Folder::class)->getMock();
  146. $this->rootFolder->expects($this->any())
  147. ->method('getUserFolder')
  148. ->with('testuser1')
  149. ->willReturn($baseFolderFiles);
  150. $this->rootFolder->expects($this->once())
  151. ->method('get')
  152. ->with('testuser1/files_trashbin/files/')
  153. ->willReturn($baseFolderTrash);
  154. $baseFolderFiles->expects($this->any())
  155. ->method('getFirstNodeById')
  156. ->with(123)
  157. ->willReturn(null);
  158. $node = $this->getMockBuilder(File::class)->getMock();
  159. $node->expects($this->once())
  160. ->method('getParent')
  161. ->willReturn($parentNode);
  162. $baseFolderTrash->expects($this->once())
  163. ->method('getFirstNodeById')
  164. ->with(123)
  165. ->willReturn($node);
  166. $baseFolderTrash->expects($this->once())
  167. ->method('getRelativePath')
  168. ->with('testuser1/files_trashbin/files/test.d1462861890/sub')
  169. ->willReturn('/test.d1462861890/sub');
  170. $this->urlGenerator
  171. ->expects($this->once())
  172. ->method('linkToRoute')
  173. ->with('files.view.indexViewFileid', ['view' => 'trashbin', 'dir' => '/test.d1462861890/sub', 'fileid' => '123'])
  174. ->willReturn('/apps/files/trashbin/123?dir=/test.d1462861890/sub');
  175. $expected = new RedirectResponse('/apps/files/trashbin/123?dir=/test.d1462861890/sub');
  176. $this->assertEquals($expected, $this->viewController->index('', '', '123'));
  177. }
  178. }