ViewControllerTest.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 OCA\Files\Activity\Helper;
  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;
  14. use OCP\AppFramework\Services\IInitialState;
  15. use OCP\EventDispatcher\IEventDispatcher;
  16. use OCP\Files\File;
  17. use OCP\Files\Folder;
  18. use OCP\Files\IRootFolder;
  19. use OCP\Files\Template\ITemplateManager;
  20. use OCP\IConfig;
  21. use OCP\IL10N;
  22. use OCP\IRequest;
  23. use OCP\IURLGenerator;
  24. use OCP\IUser;
  25. use OCP\IUserSession;
  26. use OCP\Share\IManager;
  27. use Test\TestCase;
  28. /**
  29. * Class ViewControllerTest
  30. *
  31. * @package OCA\Files\Tests\Controller
  32. */
  33. class ViewControllerTest extends TestCase {
  34. /** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */
  35. private $request;
  36. /** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */
  37. private $urlGenerator;
  38. /** @var IL10N */
  39. private $l10n;
  40. /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
  41. private $config;
  42. /** @var IEventDispatcher */
  43. private $eventDispatcher;
  44. /** @var ViewController|\PHPUnit\Framework\MockObject\MockObject */
  45. private $viewController;
  46. /** @var IUser|\PHPUnit\Framework\MockObject\MockObject */
  47. private $user;
  48. /** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */
  49. private $userSession;
  50. /** @var IAppManager|\PHPUnit\Framework\MockObject\MockObject */
  51. private $appManager;
  52. /** @var IRootFolder|\PHPUnit\Framework\MockObject\MockObject */
  53. private $rootFolder;
  54. /** @var Helper|\PHPUnit\Framework\MockObject\MockObject */
  55. private $activityHelper;
  56. /** @var IInitialState|\PHPUnit\Framework\MockObject\MockObject */
  57. private $initialState;
  58. /** @var ITemplateManager|\PHPUnit\Framework\MockObject\MockObject */
  59. private $templateManager;
  60. /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
  61. private $shareManager;
  62. /** @var UserConfig|\PHPUnit\Framework\MockObject\MockObject */
  63. private $userConfig;
  64. /** @var ViewConfig|\PHPUnit\Framework\MockObject\MockObject */
  65. private $viewConfig;
  66. protected function setUp(): void {
  67. parent::setUp();
  68. $this->request = $this->getMockBuilder(IRequest::class)->getMock();
  69. $this->urlGenerator = $this->getMockBuilder(IURLGenerator::class)->getMock();
  70. $this->l10n = $this->getMockBuilder(IL10N::class)->getMock();
  71. $this->config = $this->getMockBuilder(IConfig::class)->getMock();
  72. $this->eventDispatcher = $this->createMock(IEventDispatcher::class);
  73. $this->userSession = $this->getMockBuilder(IUserSession::class)->getMock();
  74. $this->appManager = $this->getMockBuilder('\OCP\App\IAppManager')->getMock();
  75. $this->user = $this->getMockBuilder(IUser::class)->getMock();
  76. $this->user->expects($this->any())
  77. ->method('getUID')
  78. ->willReturn('testuser1');
  79. $this->userSession->expects($this->any())
  80. ->method('getUser')
  81. ->willReturn($this->user);
  82. $this->rootFolder = $this->getMockBuilder('\OCP\Files\IRootFolder')->getMock();
  83. $this->activityHelper = $this->createMock(Helper::class);
  84. $this->initialState = $this->createMock(IInitialState::class);
  85. $this->templateManager = $this->createMock(ITemplateManager::class);
  86. $this->shareManager = $this->createMock(IManager::class);
  87. $this->userConfig = $this->createMock(UserConfig::class);
  88. $this->viewConfig = $this->createMock(ViewConfig::class);
  89. $this->viewController = $this->getMockBuilder('\OCA\Files\Controller\ViewController')
  90. ->setConstructorArgs([
  91. 'files',
  92. $this->request,
  93. $this->urlGenerator,
  94. $this->l10n,
  95. $this->config,
  96. $this->eventDispatcher,
  97. $this->userSession,
  98. $this->appManager,
  99. $this->rootFolder,
  100. $this->activityHelper,
  101. $this->initialState,
  102. $this->templateManager,
  103. $this->shareManager,
  104. $this->userConfig,
  105. $this->viewConfig,
  106. ])
  107. ->setMethods([
  108. 'getStorageInfo',
  109. ])
  110. ->getMock();
  111. }
  112. public function testIndexWithRegularBrowser() {
  113. $this->viewController
  114. ->expects($this->any())
  115. ->method('getStorageInfo')
  116. ->willReturn([
  117. 'used' => 123,
  118. 'quota' => 100,
  119. 'total' => 100,
  120. 'relative' => 123,
  121. 'owner' => 'MyName',
  122. 'ownerDisplayName' => 'MyDisplayName',
  123. ]);
  124. $this->config
  125. ->method('getUserValue')
  126. ->willReturnMap([
  127. [$this->user->getUID(), 'files', 'file_sorting', 'name', 'name'],
  128. [$this->user->getUID(), 'files', 'file_sorting_direction', 'asc', 'asc'],
  129. [$this->user->getUID(), 'files', 'files_sorting_configs', '{}', '{}'],
  130. [$this->user->getUID(), 'files', 'show_hidden', false, false],
  131. [$this->user->getUID(), 'files', 'crop_image_previews', true, true],
  132. [$this->user->getUID(), 'files', 'show_grid', true],
  133. ]);
  134. $baseFolderFiles = $this->getMockBuilder(Folder::class)->getMock();
  135. $this->rootFolder->expects($this->any())
  136. ->method('getUserFolder')
  137. ->with('testuser1')
  138. ->willReturn($baseFolderFiles);
  139. $this->config
  140. ->expects($this->any())
  141. ->method('getAppValue')
  142. ->willReturnArgument(2);
  143. $expected = new Http\TemplateResponse(
  144. 'files',
  145. 'index',
  146. );
  147. $policy = new Http\ContentSecurityPolicy();
  148. $policy->addAllowedWorkerSrcDomain('\'self\'');
  149. $policy->addAllowedFrameDomain('\'self\'');
  150. $expected->setContentSecurityPolicy($policy);
  151. $this->activityHelper->method('getFavoriteFilePaths')
  152. ->with($this->user->getUID())
  153. ->willReturn([
  154. 'item' => [],
  155. 'folders' => [
  156. '/test1',
  157. '/test2/',
  158. '/test3/sub4',
  159. '/test5/sub6/',
  160. ],
  161. ]);
  162. $this->assertEquals($expected, $this->viewController->index('MyDir', 'MyView'));
  163. }
  164. public function testShowFileRouteWithTrashedFile() {
  165. $this->appManager->expects($this->once())
  166. ->method('isEnabledForUser')
  167. ->with('files_trashbin')
  168. ->willReturn(true);
  169. $parentNode = $this->getMockBuilder(Folder::class)->getMock();
  170. $parentNode->expects($this->once())
  171. ->method('getPath')
  172. ->willReturn('testuser1/files_trashbin/files/test.d1462861890/sub');
  173. $baseFolderFiles = $this->getMockBuilder(Folder::class)->getMock();
  174. $baseFolderTrash = $this->getMockBuilder(Folder::class)->getMock();
  175. $this->rootFolder->expects($this->any())
  176. ->method('getUserFolder')
  177. ->with('testuser1')
  178. ->willReturn($baseFolderFiles);
  179. $this->rootFolder->expects($this->once())
  180. ->method('get')
  181. ->with('testuser1/files_trashbin/files/')
  182. ->willReturn($baseFolderTrash);
  183. $baseFolderFiles->expects($this->any())
  184. ->method('getFirstNodeById')
  185. ->with(123)
  186. ->willReturn(null);
  187. $node = $this->getMockBuilder(File::class)->getMock();
  188. $node->expects($this->once())
  189. ->method('getParent')
  190. ->willReturn($parentNode);
  191. $baseFolderTrash->expects($this->once())
  192. ->method('getFirstNodeById')
  193. ->with(123)
  194. ->willReturn($node);
  195. $baseFolderTrash->expects($this->once())
  196. ->method('getRelativePath')
  197. ->with('testuser1/files_trashbin/files/test.d1462861890/sub')
  198. ->willReturn('/test.d1462861890/sub');
  199. $this->urlGenerator
  200. ->expects($this->once())
  201. ->method('linkToRoute')
  202. ->with('files.view.indexViewFileid', ['view' => 'trashbin', 'dir' => '/test.d1462861890/sub', 'fileid' => '123'])
  203. ->willReturn('/apps/files/trashbin/123?dir=/test.d1462861890/sub');
  204. $expected = new Http\RedirectResponse('/apps/files/trashbin/123?dir=/test.d1462861890/sub');
  205. $this->assertEquals($expected, $this->viewController->index('', '', '123'));
  206. }
  207. }