ViewControllerTest.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 */
  73. private $user;
  74. /** @var IUserSession */
  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. 'renderScript'
  136. ])
  137. ->getMock();
  138. }
  139. public function testIndexWithRegularBrowser() {
  140. $this->viewController
  141. ->expects($this->any())
  142. ->method('getStorageInfo')
  143. ->willReturn([
  144. 'used' => 123,
  145. 'quota' => 100,
  146. 'total' => 100,
  147. 'relative' => 123,
  148. 'owner' => 'MyName',
  149. 'ownerDisplayName' => 'MyDisplayName',
  150. ]);
  151. $this->config
  152. ->expects($this->any())
  153. ->method('getSystemValue')
  154. ->with('forbidden_chars', [])
  155. ->willReturn([]);
  156. $this->config
  157. ->method('getUserValue')
  158. ->willReturnMap([
  159. [$this->user->getUID(), 'files', 'file_sorting', 'name', 'name'],
  160. [$this->user->getUID(), 'files', 'file_sorting_direction', 'asc', 'asc'],
  161. [$this->user->getUID(), 'files', 'files_sorting_configs', '{}', '{}'],
  162. [$this->user->getUID(), 'files', 'show_hidden', false, false],
  163. [$this->user->getUID(), 'files', 'crop_image_previews', true, true],
  164. [$this->user->getUID(), 'files', 'show_grid', true],
  165. ]);
  166. $baseFolderFiles = $this->getMockBuilder(Folder::class)->getMock();
  167. $this->rootFolder->expects($this->any())
  168. ->method('getUserFolder')
  169. ->with('testuser1')
  170. ->willReturn($baseFolderFiles);
  171. $this->config
  172. ->expects($this->any())
  173. ->method('getAppValue')
  174. ->willReturnArgument(2);
  175. $expected = new Http\TemplateResponse(
  176. 'files',
  177. 'index',
  178. [
  179. 'fileNotFound' => 0,
  180. ]
  181. );
  182. $policy = new Http\ContentSecurityPolicy();
  183. $policy->addAllowedWorkerSrcDomain('\'self\'');
  184. $policy->addAllowedFrameDomain('\'self\'');
  185. $expected->setContentSecurityPolicy($policy);
  186. $this->activityHelper->method('getFavoriteFilePaths')
  187. ->with($this->user->getUID())
  188. ->willReturn([
  189. 'item' => [],
  190. 'folders' => [
  191. '/test1',
  192. '/test2/',
  193. '/test3/sub4',
  194. '/test5/sub6/',
  195. ],
  196. ]);
  197. $this->assertEquals($expected, $this->viewController->index('MyDir', 'MyView'));
  198. }
  199. public function testShowFileRouteWithTrashedFile() {
  200. $this->appManager->expects($this->once())
  201. ->method('isEnabledForUser')
  202. ->with('files_trashbin')
  203. ->willReturn(true);
  204. $parentNode = $this->getMockBuilder(Folder::class)->getMock();
  205. $parentNode->expects($this->once())
  206. ->method('getPath')
  207. ->willReturn('testuser1/files_trashbin/files/test.d1462861890/sub');
  208. $baseFolderFiles = $this->getMockBuilder(Folder::class)->getMock();
  209. $baseFolderTrash = $this->getMockBuilder(Folder::class)->getMock();
  210. $this->rootFolder->expects($this->any())
  211. ->method('getUserFolder')
  212. ->with('testuser1')
  213. ->willReturn($baseFolderFiles);
  214. $this->rootFolder->expects($this->once())
  215. ->method('get')
  216. ->with('testuser1/files_trashbin/files/')
  217. ->willReturn($baseFolderTrash);
  218. $baseFolderFiles->expects($this->any())
  219. ->method('getById')
  220. ->with(123)
  221. ->willReturn([]);
  222. $node = $this->getMockBuilder(File::class)->getMock();
  223. $node->expects($this->once())
  224. ->method('getParent')
  225. ->willReturn($parentNode);
  226. $baseFolderTrash->expects($this->once())
  227. ->method('getById')
  228. ->with(123)
  229. ->willReturn([$node]);
  230. $baseFolderTrash->expects($this->once())
  231. ->method('getRelativePath')
  232. ->with('testuser1/files_trashbin/files/test.d1462861890/sub')
  233. ->willReturn('/test.d1462861890/sub');
  234. $this->urlGenerator
  235. ->expects($this->once())
  236. ->method('linkToRoute')
  237. ->with('files.view.indexViewFileid', ['view' => 'trashbin', 'dir' => '/test.d1462861890/sub', 'fileid' => '123'])
  238. ->willReturn('/apps/files/trashbin/123?dir=/test.d1462861890/sub');
  239. $expected = new Http\RedirectResponse('/apps/files/trashbin/123?dir=/test.d1462861890/sub');
  240. $this->assertEquals($expected, $this->viewController->index('', '', '123'));
  241. }
  242. }