ViewControllerTest.php 17 KB

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