1
0

ViewControllerTest.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. ->method('getUserValue')
  152. ->willReturnMap([
  153. [$this->user->getUID(), 'files', 'file_sorting', 'name', 'name'],
  154. [$this->user->getUID(), 'files', 'file_sorting_direction', 'asc', 'asc'],
  155. [$this->user->getUID(), 'files', 'files_sorting_configs', '{}', '{}'],
  156. [$this->user->getUID(), 'files', 'show_hidden', false, false],
  157. [$this->user->getUID(), 'files', 'crop_image_previews', true, true],
  158. [$this->user->getUID(), 'files', 'show_grid', true],
  159. ]);
  160. $baseFolderFiles = $this->getMockBuilder(Folder::class)->getMock();
  161. $this->rootFolder->expects($this->any())
  162. ->method('getUserFolder')
  163. ->with('testuser1')
  164. ->willReturn($baseFolderFiles);
  165. $this->config
  166. ->expects($this->any())
  167. ->method('getAppValue')
  168. ->willReturnArgument(2);
  169. $expected = new Http\TemplateResponse(
  170. 'files',
  171. 'index',
  172. );
  173. $policy = new Http\ContentSecurityPolicy();
  174. $policy->addAllowedWorkerSrcDomain('\'self\'');
  175. $policy->addAllowedFrameDomain('\'self\'');
  176. $expected->setContentSecurityPolicy($policy);
  177. $this->activityHelper->method('getFavoriteFilePaths')
  178. ->with($this->user->getUID())
  179. ->willReturn([
  180. 'item' => [],
  181. 'folders' => [
  182. '/test1',
  183. '/test2/',
  184. '/test3/sub4',
  185. '/test5/sub6/',
  186. ],
  187. ]);
  188. $this->assertEquals($expected, $this->viewController->index('MyDir', 'MyView'));
  189. }
  190. public function testShowFileRouteWithTrashedFile() {
  191. $this->appManager->expects($this->once())
  192. ->method('isEnabledForUser')
  193. ->with('files_trashbin')
  194. ->willReturn(true);
  195. $parentNode = $this->getMockBuilder(Folder::class)->getMock();
  196. $parentNode->expects($this->once())
  197. ->method('getPath')
  198. ->willReturn('testuser1/files_trashbin/files/test.d1462861890/sub');
  199. $baseFolderFiles = $this->getMockBuilder(Folder::class)->getMock();
  200. $baseFolderTrash = $this->getMockBuilder(Folder::class)->getMock();
  201. $this->rootFolder->expects($this->any())
  202. ->method('getUserFolder')
  203. ->with('testuser1')
  204. ->willReturn($baseFolderFiles);
  205. $this->rootFolder->expects($this->once())
  206. ->method('get')
  207. ->with('testuser1/files_trashbin/files/')
  208. ->willReturn($baseFolderTrash);
  209. $baseFolderFiles->expects($this->any())
  210. ->method('getFirstNodeById')
  211. ->with(123)
  212. ->willReturn(null);
  213. $node = $this->getMockBuilder(File::class)->getMock();
  214. $node->expects($this->once())
  215. ->method('getParent')
  216. ->willReturn($parentNode);
  217. $baseFolderTrash->expects($this->once())
  218. ->method('getFirstNodeById')
  219. ->with(123)
  220. ->willReturn($node);
  221. $baseFolderTrash->expects($this->once())
  222. ->method('getRelativePath')
  223. ->with('testuser1/files_trashbin/files/test.d1462861890/sub')
  224. ->willReturn('/test.d1462861890/sub');
  225. $this->urlGenerator
  226. ->expects($this->once())
  227. ->method('linkToRoute')
  228. ->with('files.view.indexViewFileid', ['view' => 'trashbin', 'dir' => '/test.d1462861890/sub', 'fileid' => '123'])
  229. ->willReturn('/apps/files/trashbin/123?dir=/test.d1462861890/sub');
  230. $expected = new Http\RedirectResponse('/apps/files/trashbin/123?dir=/test.d1462861890/sub');
  231. $this->assertEquals($expected, $this->viewController->index('', '', '123'));
  232. }
  233. }