You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ViewControllerTest.php 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author John Molakvoæ <skjnldsv@protonmail.com>
  9. * @author Julius Härtl <jus@bitgrid.net>
  10. * @author Lukas Reschke <lukas@statuscode.ch>
  11. * @author Michael Weimann <mail@michael-weimann.eu>
  12. * @author Morris Jobke <hey@morrisjobke.de>
  13. * @author Nina Pypchenko <22447785+nina-py@users.noreply.github.com>
  14. * @author Robin Appelman <robin@icewind.nl>
  15. * @author Roeland Jago Douma <roeland@famdouma.nl>
  16. * @author Vincent Petry <vincent@nextcloud.com>
  17. *
  18. * @license AGPL-3.0
  19. *
  20. * This code is free software: you can redistribute it and/or modify
  21. * it under the terms of the GNU Affero General Public License, version 3,
  22. * as published by the Free Software Foundation.
  23. *
  24. * This program is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU Affero General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU Affero General Public License, version 3,
  30. * along with this program. If not, see <http://www.gnu.org/licenses/>
  31. *
  32. */
  33. namespace OCA\Files\Tests\Controller;
  34. use OCA\Files\Activity\Helper;
  35. use OCA\Files\Controller\ViewController;
  36. use OCA\Files\Service\UserConfig;
  37. use OCA\Files\Service\ViewConfig;
  38. use OCP\App\IAppManager;
  39. use OCP\AppFramework\Http;
  40. use OCP\AppFramework\Services\IInitialState;
  41. use OCP\EventDispatcher\IEventDispatcher;
  42. use OCP\Files\File;
  43. use OCP\Files\Folder;
  44. use OCP\Files\IRootFolder;
  45. use OCP\Files\Template\ITemplateManager;
  46. use OCP\IConfig;
  47. use OCP\IL10N;
  48. use OCP\IRequest;
  49. use OCP\IURLGenerator;
  50. use OCP\IUser;
  51. use OCP\IUserSession;
  52. use OCP\Share\IManager;
  53. use Test\TestCase;
  54. /**
  55. * Class ViewControllerTest
  56. *
  57. * @package OCA\Files\Tests\Controller
  58. */
  59. class ViewControllerTest extends TestCase {
  60. /** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */
  61. private $request;
  62. /** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */
  63. private $urlGenerator;
  64. /** @var IL10N */
  65. private $l10n;
  66. /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
  67. private $config;
  68. /** @var IEventDispatcher */
  69. private $eventDispatcher;
  70. /** @var ViewController|\PHPUnit\Framework\MockObject\MockObject */
  71. private $viewController;
  72. /** @var IUser|\PHPUnit\Framework\MockObject\MockObject */
  73. private $user;
  74. /** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */
  75. private $userSession;
  76. /** @var IAppManager|\PHPUnit\Framework\MockObject\MockObject */
  77. private $appManager;
  78. /** @var IRootFolder|\PHPUnit\Framework\MockObject\MockObject */
  79. private $rootFolder;
  80. /** @var Helper|\PHPUnit\Framework\MockObject\MockObject */
  81. private $activityHelper;
  82. /** @var IInitialState|\PHPUnit\Framework\MockObject\MockObject */
  83. private $initialState;
  84. /** @var ITemplateManager|\PHPUnit\Framework\MockObject\MockObject */
  85. private $templateManager;
  86. /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
  87. private $shareManager;
  88. /** @var UserConfig|\PHPUnit\Framework\MockObject\MockObject */
  89. private $userConfig;
  90. /** @var ViewConfig|\PHPUnit\Framework\MockObject\MockObject */
  91. private $viewConfig;
  92. protected function setUp(): void {
  93. parent::setUp();
  94. $this->request = $this->getMockBuilder(IRequest::class)->getMock();
  95. $this->urlGenerator = $this->getMockBuilder(IURLGenerator::class)->getMock();
  96. $this->l10n = $this->getMockBuilder(IL10N::class)->getMock();
  97. $this->config = $this->getMockBuilder(IConfig::class)->getMock();
  98. $this->eventDispatcher = $this->createMock(IEventDispatcher::class);
  99. $this->userSession = $this->getMockBuilder(IUserSession::class)->getMock();
  100. $this->appManager = $this->getMockBuilder('\OCP\App\IAppManager')->getMock();
  101. $this->user = $this->getMockBuilder(IUser::class)->getMock();
  102. $this->user->expects($this->any())
  103. ->method('getUID')
  104. ->willReturn('testuser1');
  105. $this->userSession->expects($this->any())
  106. ->method('getUser')
  107. ->willReturn($this->user);
  108. $this->rootFolder = $this->getMockBuilder('\OCP\Files\IRootFolder')->getMock();
  109. $this->activityHelper = $this->createMock(Helper::class);
  110. $this->initialState = $this->createMock(IInitialState::class);
  111. $this->templateManager = $this->createMock(ITemplateManager::class);
  112. $this->shareManager = $this->createMock(IManager::class);
  113. $this->userConfig = $this->createMock(UserConfig::class);
  114. $this->viewConfig = $this->createMock(ViewConfig::class);
  115. $this->viewController = $this->getMockBuilder('\OCA\Files\Controller\ViewController')
  116. ->setConstructorArgs([
  117. 'files',
  118. $this->request,
  119. $this->urlGenerator,
  120. $this->l10n,
  121. $this->config,
  122. $this->eventDispatcher,
  123. $this->userSession,
  124. $this->appManager,
  125. $this->rootFolder,
  126. $this->activityHelper,
  127. $this->initialState,
  128. $this->templateManager,
  129. $this->shareManager,
  130. $this->userConfig,
  131. $this->viewConfig,
  132. ])
  133. ->setMethods([
  134. 'getStorageInfo',
  135. ])
  136. ->getMock();
  137. }
  138. public function testIndexWithRegularBrowser() {
  139. $this->viewController
  140. ->expects($this->any())
  141. ->method('getStorageInfo')
  142. ->willReturn([
  143. 'used' => 123,
  144. 'quota' => 100,
  145. 'total' => 100,
  146. 'relative' => 123,
  147. 'owner' => 'MyName',
  148. 'ownerDisplayName' => 'MyDisplayName',
  149. ]);
  150. $this->config
  151. ->expects($this->any())
  152. ->method('getSystemValue')
  153. ->with('forbidden_chars', [])
  154. ->willReturn([]);
  155. $this->config
  156. ->method('getUserValue')
  157. ->willReturnMap([
  158. [$this->user->getUID(), 'files', 'file_sorting', 'name', 'name'],
  159. [$this->user->getUID(), 'files', 'file_sorting_direction', 'asc', 'asc'],
  160. [$this->user->getUID(), 'files', 'files_sorting_configs', '{}', '{}'],
  161. [$this->user->getUID(), 'files', 'show_hidden', false, false],
  162. [$this->user->getUID(), 'files', 'crop_image_previews', true, true],
  163. [$this->user->getUID(), 'files', 'show_grid', true],
  164. ]);
  165. $baseFolderFiles = $this->getMockBuilder(Folder::class)->getMock();
  166. $this->rootFolder->expects($this->any())
  167. ->method('getUserFolder')
  168. ->with('testuser1')
  169. ->willReturn($baseFolderFiles);
  170. $this->config
  171. ->expects($this->any())
  172. ->method('getAppValue')
  173. ->willReturnArgument(2);
  174. $expected = new Http\TemplateResponse(
  175. 'files',
  176. 'index',
  177. );
  178. $policy = new Http\ContentSecurityPolicy();
  179. $policy->addAllowedWorkerSrcDomain('\'self\'');
  180. $policy->addAllowedFrameDomain('\'self\'');
  181. $expected->setContentSecurityPolicy($policy);
  182. $this->activityHelper->method('getFavoriteFilePaths')
  183. ->with($this->user->getUID())
  184. ->willReturn([
  185. 'item' => [],
  186. 'folders' => [
  187. '/test1',
  188. '/test2/',
  189. '/test3/sub4',
  190. '/test5/sub6/',
  191. ],
  192. ]);
  193. $this->assertEquals($expected, $this->viewController->index('MyDir', 'MyView'));
  194. }
  195. public function testShowFileRouteWithTrashedFile() {
  196. $this->appManager->expects($this->once())
  197. ->method('isEnabledForUser')
  198. ->with('files_trashbin')
  199. ->willReturn(true);
  200. $parentNode = $this->getMockBuilder(Folder::class)->getMock();
  201. $parentNode->expects($this->once())
  202. ->method('getPath')
  203. ->willReturn('testuser1/files_trashbin/files/test.d1462861890/sub');
  204. $baseFolderFiles = $this->getMockBuilder(Folder::class)->getMock();
  205. $baseFolderTrash = $this->getMockBuilder(Folder::class)->getMock();
  206. $this->rootFolder->expects($this->any())
  207. ->method('getUserFolder')
  208. ->with('testuser1')
  209. ->willReturn($baseFolderFiles);
  210. $this->rootFolder->expects($this->once())
  211. ->method('get')
  212. ->with('testuser1/files_trashbin/files/')
  213. ->willReturn($baseFolderTrash);
  214. $baseFolderFiles->expects($this->any())
  215. ->method('getFirstNodeById')
  216. ->with(123)
  217. ->willReturn(null);
  218. $node = $this->getMockBuilder(File::class)->getMock();
  219. $node->expects($this->once())
  220. ->method('getParent')
  221. ->willReturn($parentNode);
  222. $baseFolderTrash->expects($this->once())
  223. ->method('getFirstNodeById')
  224. ->with(123)
  225. ->willReturn($node);
  226. $baseFolderTrash->expects($this->once())
  227. ->method('getRelativePath')
  228. ->with('testuser1/files_trashbin/files/test.d1462861890/sub')
  229. ->willReturn('/test.d1462861890/sub');
  230. $this->urlGenerator
  231. ->expects($this->once())
  232. ->method('linkToRoute')
  233. ->with('files.view.indexViewFileid', ['view' => 'trashbin', 'dir' => '/test.d1462861890/sub', 'fileid' => '123'])
  234. ->willReturn('/apps/files/trashbin/123?dir=/test.d1462861890/sub');
  235. $expected = new Http\RedirectResponse('/apps/files/trashbin/123?dir=/test.d1462861890/sub');
  236. $this->assertEquals($expected, $this->viewController->index('', '', '123'));
  237. }
  238. }