ViewControllerTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  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 OCP\App\IAppManager;
  37. use OCP\AppFramework\Http;
  38. use OCP\AppFramework\Services\IInitialState;
  39. use OCP\EventDispatcher\IEventDispatcher;
  40. use OCP\Files\File;
  41. use OCP\Files\Folder;
  42. use OCP\Files\IRootFolder;
  43. use OCP\Files\Template\ITemplateManager;
  44. use OCP\IConfig;
  45. use OCP\IL10N;
  46. use OCP\IRequest;
  47. use OCP\IURLGenerator;
  48. use OCP\IUser;
  49. use OCP\IUserSession;
  50. use OCP\Share\IManager;
  51. use OCP\Template;
  52. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  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 EventDispatcherInterface */
  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. protected function setUp(): void {
  89. parent::setUp();
  90. $this->request = $this->getMockBuilder(IRequest::class)->getMock();
  91. $this->urlGenerator = $this->getMockBuilder(IURLGenerator::class)->getMock();
  92. $this->l10n = $this->getMockBuilder(IL10N::class)->getMock();
  93. $this->config = $this->getMockBuilder(IConfig::class)->getMock();
  94. $this->eventDispatcher = $this->createMock(IEventDispatcher::class);
  95. $this->userSession = $this->getMockBuilder(IUserSession::class)->getMock();
  96. $this->appManager = $this->getMockBuilder('\OCP\App\IAppManager')->getMock();
  97. $this->user = $this->getMockBuilder(IUser::class)->getMock();
  98. $this->user->expects($this->any())
  99. ->method('getUID')
  100. ->willReturn('testuser1');
  101. $this->userSession->expects($this->any())
  102. ->method('getUser')
  103. ->willReturn($this->user);
  104. $this->rootFolder = $this->getMockBuilder('\OCP\Files\IRootFolder')->getMock();
  105. $this->activityHelper = $this->createMock(Helper::class);
  106. $this->initialState = $this->createMock(IInitialState::class);
  107. $this->templateManager = $this->createMock(ITemplateManager::class);
  108. $this->shareManager = $this->createMock(IManager::class);
  109. $this->viewController = $this->getMockBuilder('\OCA\Files\Controller\ViewController')
  110. ->setConstructorArgs([
  111. 'files',
  112. $this->request,
  113. $this->urlGenerator,
  114. $this->l10n,
  115. $this->config,
  116. $this->eventDispatcher,
  117. $this->userSession,
  118. $this->appManager,
  119. $this->rootFolder,
  120. $this->activityHelper,
  121. $this->initialState,
  122. $this->templateManager,
  123. $this->shareManager,
  124. ])
  125. ->setMethods([
  126. 'getStorageInfo',
  127. 'renderScript'
  128. ])
  129. ->getMock();
  130. }
  131. public function testIndexWithRegularBrowser() {
  132. $this->viewController
  133. ->expects($this->once())
  134. ->method('getStorageInfo')
  135. ->willReturn([
  136. 'used' => 123,
  137. 'quota' => 100,
  138. 'total' => 100,
  139. 'relative' => 123,
  140. 'owner' => 'MyName',
  141. 'ownerDisplayName' => 'MyDisplayName',
  142. ]);
  143. $this->config
  144. ->method('getUserValue')
  145. ->willReturnMap([
  146. [$this->user->getUID(), 'files', 'file_sorting', 'name', 'name'],
  147. [$this->user->getUID(), 'files', 'file_sorting_direction', 'asc', 'asc'],
  148. [$this->user->getUID(), 'files', 'show_hidden', false, false],
  149. [$this->user->getUID(), 'files', 'crop_image_previews', true, true],
  150. [$this->user->getUID(), 'files', 'show_grid', true],
  151. ]);
  152. $this->config
  153. ->expects($this->any())
  154. ->method('getAppValue')
  155. ->willReturnArgument(2);
  156. $this->shareManager->method('shareApiAllowLinks')
  157. ->willReturn(true);
  158. $nav = new Template('files', 'appnavigation');
  159. $nav->assign('usage_relative', 123);
  160. $nav->assign('usage', '123 B');
  161. $nav->assign('quota', 100);
  162. $nav->assign('total_space', '100 B');
  163. $nav->assign('webdav_url', 'http://localhost/remote.php/dav/files/testuser1/');
  164. $nav->assign('navigationItems', [
  165. 'files' => [
  166. 'id' => 'files',
  167. 'appname' => 'files',
  168. 'script' => 'list.php',
  169. 'order' => 0,
  170. 'name' => \OC::$server->getL10N('files')->t('All files'),
  171. 'active' => false,
  172. 'icon' => '',
  173. 'type' => 'link',
  174. 'classes' => '',
  175. 'unread' => 0,
  176. ],
  177. 'recent' => [
  178. 'id' => 'recent',
  179. 'appname' => 'files',
  180. 'script' => 'recentlist.php',
  181. 'order' => 2,
  182. 'name' => \OC::$server->getL10N('files')->t('Recent'),
  183. 'active' => false,
  184. 'icon' => '',
  185. 'type' => 'link',
  186. 'classes' => '',
  187. 'unread' => 0,
  188. ],
  189. 'favorites' => [
  190. 'id' => 'favorites',
  191. 'appname' => 'files',
  192. 'script' => 'simplelist.php',
  193. 'order' => 5,
  194. 'name' => \OC::$server->getL10N('files')->t('Favorites'),
  195. 'active' => false,
  196. 'icon' => '',
  197. 'type' => 'link',
  198. 'classes' => 'collapsible',
  199. 'sublist' => [
  200. [
  201. 'id' => '-test1',
  202. 'view' => 'files',
  203. 'href' => '',
  204. 'dir' => '/test1',
  205. 'order' => 6,
  206. 'folderPosition' => 1,
  207. 'name' => 'test1',
  208. 'icon' => 'files',
  209. 'quickaccesselement' => 'true',
  210. ],
  211. [
  212. 'name' => 'test2',
  213. 'id' => '-test2-',
  214. 'view' => 'files',
  215. 'href' => '',
  216. 'dir' => '/test2/',
  217. 'order' => 7,
  218. 'folderPosition' => 2,
  219. 'icon' => 'files',
  220. 'quickaccesselement' => 'true',
  221. ],
  222. [
  223. 'name' => 'sub4',
  224. 'id' => '-test3-sub4',
  225. 'view' => 'files',
  226. 'href' => '',
  227. 'dir' => '/test3/sub4',
  228. 'order' => 8,
  229. 'folderPosition' => 3,
  230. 'icon' => 'files',
  231. 'quickaccesselement' => 'true',
  232. ],
  233. [
  234. 'name' => 'sub6',
  235. 'id' => '-test5-sub6-',
  236. 'view' => 'files',
  237. 'href' => '',
  238. 'dir' => '/test5/sub6/',
  239. 'order' => 9,
  240. 'folderPosition' => 4,
  241. 'icon' => 'files',
  242. 'quickaccesselement' => 'true',
  243. ],
  244. ],
  245. 'defaultExpandedState' => false,
  246. 'expandedState' => 'show_Quick_Access',
  247. 'unread' => 0,
  248. ],
  249. 'systemtagsfilter' => [
  250. 'id' => 'systemtagsfilter',
  251. 'appname' => 'systemtags',
  252. 'script' => 'list.php',
  253. 'order' => 25,
  254. 'name' => \OC::$server->getL10N('systemtags')->t('Tags'),
  255. 'active' => false,
  256. 'icon' => '',
  257. 'type' => 'link',
  258. 'classes' => '',
  259. 'unread' => 0,
  260. ],
  261. 'trashbin' => [
  262. 'id' => 'trashbin',
  263. 'appname' => 'files_trashbin',
  264. 'script' => 'list.php',
  265. 'order' => 50,
  266. 'name' => \OC::$server->getL10N('files_trashbin')->t('Deleted files'),
  267. 'active' => false,
  268. 'icon' => '',
  269. 'type' => 'link',
  270. 'classes' => 'pinned',
  271. 'unread' => 0,
  272. ],
  273. 'shareoverview' => [
  274. 'id' => 'shareoverview',
  275. 'appname' => 'files_sharing',
  276. 'script' => 'list.php',
  277. 'order' => 18,
  278. 'name' => \OC::$server->getL10N('files_sharing')->t('Shares'),
  279. 'classes' => 'collapsible',
  280. 'sublist' => [
  281. [
  282. 'id' => 'sharingout',
  283. 'appname' => 'files_sharing',
  284. 'script' => 'list.php',
  285. 'order' => 16,
  286. 'name' => \OC::$server->getL10N('files_sharing')->t('Shared with others'),
  287. ],
  288. [
  289. 'id' => 'sharingin',
  290. 'appname' => 'files_sharing',
  291. 'script' => 'list.php',
  292. 'order' => 15,
  293. 'name' => \OC::$server->getL10N('files_sharing')->t('Shared with you'),
  294. ],
  295. [
  296. 'id' => 'sharinglinks',
  297. 'appname' => 'files_sharing',
  298. 'script' => 'list.php',
  299. 'order' => 17,
  300. 'name' => \OC::$server->getL10N('files_sharing')->t('Shared by link', []),
  301. ],
  302. [
  303. 'id' => 'deletedshares',
  304. 'appname' => 'files_sharing',
  305. 'script' => 'list.php',
  306. 'order' => 19,
  307. 'name' => \OC::$server->getL10N('files_sharing')->t('Deleted shares'),
  308. ],
  309. [
  310. 'id' => 'pendingshares',
  311. 'appname' => 'files_sharing',
  312. 'script' => 'list.php',
  313. 'order' => 19,
  314. 'name' => \OC::$server->getL10N('files_sharing')->t('Pending shares'),
  315. ],
  316. ],
  317. 'active' => false,
  318. 'icon' => '',
  319. 'type' => 'link',
  320. 'expandedState' => 'show_sharing_menu',
  321. 'defaultExpandedState' => false,
  322. 'unread' => 0,
  323. ]
  324. ]);
  325. $expected = new Http\TemplateResponse(
  326. 'files',
  327. 'index',
  328. [
  329. 'usedSpacePercent' => 123,
  330. 'owner' => 'MyName',
  331. 'ownerDisplayName' => 'MyDisplayName',
  332. 'isPublic' => false,
  333. 'defaultFileSorting' => 'name',
  334. 'defaultFileSortingDirection' => 'asc',
  335. 'showHiddenFiles' => 0,
  336. 'cropImagePreviews' => 1,
  337. 'fileNotFound' => 0,
  338. 'allowShareWithLink' => 'yes',
  339. 'appNavigation' => $nav,
  340. 'appContents' => [
  341. 'files' => [
  342. 'id' => 'files',
  343. 'content' => null,
  344. ],
  345. 'recent' => [
  346. 'id' => 'recent',
  347. 'content' => null,
  348. ],
  349. 'favorites' => [
  350. 'id' => 'favorites',
  351. 'content' => null,
  352. ],
  353. 'systemtagsfilter' => [
  354. 'id' => 'systemtagsfilter',
  355. 'content' => null,
  356. ],
  357. 'trashbin' => [
  358. 'id' => 'trashbin',
  359. 'content' => null,
  360. ],
  361. 'sharingout' => [
  362. 'id' => 'sharingout',
  363. 'content' => null,
  364. ],
  365. 'sharingin' => [
  366. 'id' => 'sharingin',
  367. 'content' => null,
  368. ],
  369. 'sharinglinks' => [
  370. 'id' => 'sharinglinks',
  371. 'content' => null,
  372. ],
  373. 'deletedshares' => [
  374. 'id' => 'deletedshares',
  375. 'content' => null,
  376. ],
  377. 'pendingshares' => [
  378. 'id' => 'pendingshares',
  379. 'content' => null
  380. ],
  381. 'shareoverview' => [
  382. 'id' => 'shareoverview',
  383. 'content' => null,
  384. ],
  385. '-test1' => [
  386. 'id' => '-test1',
  387. 'content' => '',
  388. ],
  389. '-test2-' => [
  390. 'id' => '-test2-',
  391. 'content' => '',
  392. ],
  393. '-test3-sub4' => [
  394. 'id' => '-test3-sub4',
  395. 'content' => '',
  396. ],
  397. '-test5-sub6-' => [
  398. 'id' => '-test5-sub6-',
  399. 'content' => '',
  400. ],
  401. ],
  402. 'hiddenFields' => [],
  403. 'showgridview' => false
  404. ]
  405. );
  406. $policy = new Http\ContentSecurityPolicy();
  407. $policy->addAllowedFrameDomain('\'self\'');
  408. $expected->setContentSecurityPolicy($policy);
  409. $this->activityHelper->method('getFavoriteFilePaths')
  410. ->with($this->user->getUID())
  411. ->willReturn([
  412. 'item' => [],
  413. 'folders' => [
  414. '/test1',
  415. '/test2/',
  416. '/test3/sub4',
  417. '/test5/sub6/',
  418. ],
  419. ]);
  420. $this->assertEquals($expected, $this->viewController->index('MyDir', 'MyView'));
  421. }
  422. public function testShowFileRouteWithFolder() {
  423. $node = $this->getMockBuilder(Folder::class)->getMock();
  424. $node->expects($this->once())
  425. ->method('getPath')
  426. ->willReturn('/testuser1/files/test/sub');
  427. $baseFolder = $this->getMockBuilder(Folder::class)->getMock();
  428. $this->rootFolder->expects($this->once())
  429. ->method('getUserFolder')
  430. ->with('testuser1')
  431. ->willReturn($baseFolder);
  432. $baseFolder->expects($this->once())
  433. ->method('getById')
  434. ->with(123)
  435. ->willReturn([$node]);
  436. $baseFolder->expects($this->once())
  437. ->method('getRelativePath')
  438. ->with('/testuser1/files/test/sub')
  439. ->willReturn('/test/sub');
  440. $this->urlGenerator
  441. ->expects($this->once())
  442. ->method('linkToRoute')
  443. ->with('files.view.index', ['dir' => '/test/sub'])
  444. ->willReturn('/apps/files/?dir=/test/sub');
  445. $expected = new Http\RedirectResponse('/apps/files/?dir=/test/sub');
  446. $this->assertEquals($expected, $this->viewController->index('', '', '123'));
  447. }
  448. public function testShowFileRouteWithFile() {
  449. $parentNode = $this->getMockBuilder(Folder::class)->getMock();
  450. $parentNode->expects($this->once())
  451. ->method('getPath')
  452. ->willReturn('testuser1/files/test');
  453. $baseFolder = $this->getMockBuilder(Folder::class)->getMock();
  454. $this->rootFolder->expects($this->once())
  455. ->method('getUserFolder')
  456. ->with('testuser1')
  457. ->willReturn($baseFolder);
  458. $node = $this->getMockBuilder(File::class)->getMock();
  459. $node->expects($this->once())
  460. ->method('getParent')
  461. ->willReturn($parentNode);
  462. $node->expects($this->once())
  463. ->method('getName')
  464. ->willReturn('somefile.txt');
  465. $baseFolder->expects($this->once())
  466. ->method('getById')
  467. ->with(123)
  468. ->willReturn([$node]);
  469. $baseFolder->expects($this->once())
  470. ->method('getRelativePath')
  471. ->with('testuser1/files/test')
  472. ->willReturn('/test');
  473. $this->urlGenerator
  474. ->expects($this->once())
  475. ->method('linkToRoute')
  476. ->with('files.view.index', ['dir' => '/test', 'scrollto' => 'somefile.txt'])
  477. ->willReturn('/apps/files/?dir=/test/sub&scrollto=somefile.txt');
  478. $expected = new Http\RedirectResponse('/apps/files/?dir=/test/sub&scrollto=somefile.txt');
  479. $this->assertEquals($expected, $this->viewController->index('', '', '123'));
  480. }
  481. public function testShowFileRouteWithInvalidFileId() {
  482. $baseFolder = $this->getMockBuilder(Folder::class)->getMock();
  483. $this->rootFolder->expects($this->once())
  484. ->method('getUserFolder')
  485. ->with('testuser1')
  486. ->willReturn($baseFolder);
  487. $baseFolder->expects($this->once())
  488. ->method('getById')
  489. ->with(123)
  490. ->willReturn([]);
  491. $this->urlGenerator->expects($this->once())
  492. ->method('linkToRoute')
  493. ->with('files.view.index', ['fileNotFound' => true])
  494. ->willReturn('redirect.url');
  495. $response = $this->viewController->index('', 'MyView', '123');
  496. $this->assertInstanceOf('OCP\AppFramework\Http\RedirectResponse', $response);
  497. $this->assertEquals('redirect.url', $response->getRedirectURL());
  498. }
  499. public function testShowFileRouteWithTrashedFile() {
  500. $this->appManager->expects($this->once())
  501. ->method('isEnabledForUser')
  502. ->with('files_trashbin')
  503. ->willReturn(true);
  504. $parentNode = $this->getMockBuilder(Folder::class)->getMock();
  505. $parentNode->expects($this->once())
  506. ->method('getPath')
  507. ->willReturn('testuser1/files_trashbin/files/test.d1462861890/sub');
  508. $baseFolderFiles = $this->getMockBuilder(Folder::class)->getMock();
  509. $baseFolderTrash = $this->getMockBuilder(Folder::class)->getMock();
  510. $this->rootFolder->expects($this->once())
  511. ->method('getUserFolder')
  512. ->with('testuser1')
  513. ->willReturn($baseFolderFiles);
  514. $this->rootFolder->expects($this->once())
  515. ->method('get')
  516. ->with('testuser1/files_trashbin/files/')
  517. ->willReturn($baseFolderTrash);
  518. $baseFolderFiles->expects($this->once())
  519. ->method('getById')
  520. ->with(123)
  521. ->willReturn([]);
  522. $node = $this->getMockBuilder(File::class)->getMock();
  523. $node->expects($this->once())
  524. ->method('getParent')
  525. ->willReturn($parentNode);
  526. $node->expects($this->once())
  527. ->method('getName')
  528. ->willReturn('somefile.txt');
  529. $baseFolderTrash->expects($this->once())
  530. ->method('getById')
  531. ->with(123)
  532. ->willReturn([$node]);
  533. $baseFolderTrash->expects($this->once())
  534. ->method('getRelativePath')
  535. ->with('testuser1/files_trashbin/files/test.d1462861890/sub')
  536. ->willReturn('/test.d1462861890/sub');
  537. $this->urlGenerator
  538. ->expects($this->once())
  539. ->method('linkToRoute')
  540. ->with('files.view.index', ['view' => 'trashbin', 'dir' => '/test.d1462861890/sub', 'scrollto' => 'somefile.txt'])
  541. ->willReturn('/apps/files/?view=trashbin&dir=/test.d1462861890/sub&scrollto=somefile.txt');
  542. $expected = new Http\RedirectResponse('/apps/files/?view=trashbin&dir=/test.d1462861890/sub&scrollto=somefile.txt');
  543. $this->assertEquals($expected, $this->viewController->index('', '', '123'));
  544. }
  545. }