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 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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. 'expanded' => false,
  246. 'unread' => 0,
  247. ],
  248. 'systemtagsfilter' => [
  249. 'id' => 'systemtagsfilter',
  250. 'appname' => 'systemtags',
  251. 'script' => 'list.php',
  252. 'order' => 25,
  253. 'name' => \OC::$server->getL10N('systemtags')->t('Tags'),
  254. 'active' => false,
  255. 'icon' => '',
  256. 'type' => 'link',
  257. 'classes' => '',
  258. 'unread' => 0,
  259. ],
  260. 'trashbin' => [
  261. 'id' => 'trashbin',
  262. 'appname' => 'files_trashbin',
  263. 'script' => 'list.php',
  264. 'order' => 50,
  265. 'name' => \OC::$server->getL10N('files_trashbin')->t('Deleted files'),
  266. 'active' => false,
  267. 'icon' => '',
  268. 'type' => 'link',
  269. 'classes' => 'pinned',
  270. 'unread' => 0,
  271. ],
  272. 'shareoverview' => [
  273. 'id' => 'shareoverview',
  274. 'appname' => 'files_sharing',
  275. 'script' => 'list.php',
  276. 'order' => 18,
  277. 'name' => \OC::$server->getL10N('files_sharing')->t('Shares'),
  278. 'classes' => 'collapsible',
  279. 'sublist' => [
  280. [
  281. 'id' => 'sharingout',
  282. 'appname' => 'files_sharing',
  283. 'script' => 'list.php',
  284. 'order' => 16,
  285. 'name' => \OC::$server->getL10N('files_sharing')->t('Shared with others'),
  286. ],
  287. [
  288. 'id' => 'sharingin',
  289. 'appname' => 'files_sharing',
  290. 'script' => 'list.php',
  291. 'order' => 15,
  292. 'name' => \OC::$server->getL10N('files_sharing')->t('Shared with you'),
  293. ],
  294. [
  295. 'id' => 'sharinglinks',
  296. 'appname' => 'files_sharing',
  297. 'script' => 'list.php',
  298. 'order' => 17,
  299. 'name' => \OC::$server->getL10N('files_sharing')->t('Shared by link', []),
  300. ],
  301. [
  302. 'id' => 'deletedshares',
  303. 'appname' => 'files_sharing',
  304. 'script' => 'list.php',
  305. 'order' => 19,
  306. 'name' => \OC::$server->getL10N('files_sharing')->t('Deleted shares'),
  307. ],
  308. [
  309. 'id' => 'pendingshares',
  310. 'appname' => 'files_sharing',
  311. 'script' => 'list.php',
  312. 'order' => 19,
  313. 'name' => \OC::$server->getL10N('files_sharing')->t('Pending shares'),
  314. ],
  315. ],
  316. 'active' => false,
  317. 'icon' => '',
  318. 'type' => 'link',
  319. 'expanded' => false,
  320. 'unread' => 0,
  321. ]
  322. ]);
  323. $expected = new Http\TemplateResponse(
  324. 'files',
  325. 'index',
  326. [
  327. 'usedSpacePercent' => 123,
  328. 'owner' => 'MyName',
  329. 'ownerDisplayName' => 'MyDisplayName',
  330. 'isPublic' => false,
  331. 'defaultFileSorting' => 'name',
  332. 'defaultFileSortingDirection' => 'asc',
  333. 'showHiddenFiles' => 0,
  334. 'cropImagePreviews' => 1,
  335. 'fileNotFound' => 0,
  336. 'allowShareWithLink' => 'yes',
  337. 'appNavigation' => $nav,
  338. 'appContents' => [
  339. 'files' => [
  340. 'id' => 'files',
  341. 'content' => null,
  342. ],
  343. 'recent' => [
  344. 'id' => 'recent',
  345. 'content' => null,
  346. ],
  347. 'favorites' => [
  348. 'id' => 'favorites',
  349. 'content' => null,
  350. ],
  351. 'systemtagsfilter' => [
  352. 'id' => 'systemtagsfilter',
  353. 'content' => null,
  354. ],
  355. 'trashbin' => [
  356. 'id' => 'trashbin',
  357. 'content' => null,
  358. ],
  359. 'sharingout' => [
  360. 'id' => 'sharingout',
  361. 'content' => null,
  362. ],
  363. 'sharingin' => [
  364. 'id' => 'sharingin',
  365. 'content' => null,
  366. ],
  367. 'sharinglinks' => [
  368. 'id' => 'sharinglinks',
  369. 'content' => null,
  370. ],
  371. 'deletedshares' => [
  372. 'id' => 'deletedshares',
  373. 'content' => null,
  374. ],
  375. 'pendingshares' => [
  376. 'id' => 'pendingshares',
  377. 'content' => null
  378. ],
  379. 'shareoverview' => [
  380. 'id' => 'shareoverview',
  381. 'content' => null,
  382. ],
  383. '-test1' => [
  384. 'id' => '-test1',
  385. 'content' => '',
  386. ],
  387. '-test2-' => [
  388. 'id' => '-test2-',
  389. 'content' => '',
  390. ],
  391. '-test3-sub4' => [
  392. 'id' => '-test3-sub4',
  393. 'content' => '',
  394. ],
  395. '-test5-sub6-' => [
  396. 'id' => '-test5-sub6-',
  397. 'content' => '',
  398. ],
  399. ],
  400. 'hiddenFields' => [],
  401. 'showgridview' => false
  402. ]
  403. );
  404. $policy = new Http\ContentSecurityPolicy();
  405. $policy->addAllowedFrameDomain('\'self\'');
  406. $expected->setContentSecurityPolicy($policy);
  407. $this->activityHelper->method('getFavoriteFilePaths')
  408. ->with($this->user->getUID())
  409. ->willReturn([
  410. 'item' => [],
  411. 'folders' => [
  412. '/test1',
  413. '/test2/',
  414. '/test3/sub4',
  415. '/test5/sub6/',
  416. ],
  417. ]);
  418. $this->assertEquals($expected, $this->viewController->index('MyDir', 'MyView'));
  419. }
  420. public function testShowFileRouteWithFolder() {
  421. $node = $this->getMockBuilder(Folder::class)->getMock();
  422. $node->expects($this->once())
  423. ->method('getPath')
  424. ->willReturn('/testuser1/files/test/sub');
  425. $baseFolder = $this->getMockBuilder(Folder::class)->getMock();
  426. $this->rootFolder->expects($this->once())
  427. ->method('getUserFolder')
  428. ->with('testuser1')
  429. ->willReturn($baseFolder);
  430. $baseFolder->expects($this->once())
  431. ->method('getById')
  432. ->with(123)
  433. ->willReturn([$node]);
  434. $baseFolder->expects($this->once())
  435. ->method('getRelativePath')
  436. ->with('/testuser1/files/test/sub')
  437. ->willReturn('/test/sub');
  438. $this->urlGenerator
  439. ->expects($this->once())
  440. ->method('linkToRoute')
  441. ->with('files.view.index', ['dir' => '/test/sub'])
  442. ->willReturn('/apps/files/?dir=/test/sub');
  443. $expected = new Http\RedirectResponse('/apps/files/?dir=/test/sub');
  444. $this->assertEquals($expected, $this->viewController->index('', '', '123'));
  445. }
  446. public function testShowFileRouteWithFile() {
  447. $parentNode = $this->getMockBuilder(Folder::class)->getMock();
  448. $parentNode->expects($this->once())
  449. ->method('getPath')
  450. ->willReturn('testuser1/files/test');
  451. $baseFolder = $this->getMockBuilder(Folder::class)->getMock();
  452. $this->rootFolder->expects($this->once())
  453. ->method('getUserFolder')
  454. ->with('testuser1')
  455. ->willReturn($baseFolder);
  456. $node = $this->getMockBuilder(File::class)->getMock();
  457. $node->expects($this->once())
  458. ->method('getParent')
  459. ->willReturn($parentNode);
  460. $node->expects($this->once())
  461. ->method('getName')
  462. ->willReturn('somefile.txt');
  463. $baseFolder->expects($this->once())
  464. ->method('getById')
  465. ->with(123)
  466. ->willReturn([$node]);
  467. $baseFolder->expects($this->once())
  468. ->method('getRelativePath')
  469. ->with('testuser1/files/test')
  470. ->willReturn('/test');
  471. $this->urlGenerator
  472. ->expects($this->once())
  473. ->method('linkToRoute')
  474. ->with('files.view.index', ['dir' => '/test', 'scrollto' => 'somefile.txt'])
  475. ->willReturn('/apps/files/?dir=/test/sub&scrollto=somefile.txt');
  476. $expected = new Http\RedirectResponse('/apps/files/?dir=/test/sub&scrollto=somefile.txt');
  477. $this->assertEquals($expected, $this->viewController->index('', '', '123'));
  478. }
  479. public function testShowFileRouteWithInvalidFileId() {
  480. $baseFolder = $this->getMockBuilder(Folder::class)->getMock();
  481. $this->rootFolder->expects($this->once())
  482. ->method('getUserFolder')
  483. ->with('testuser1')
  484. ->willReturn($baseFolder);
  485. $baseFolder->expects($this->once())
  486. ->method('getById')
  487. ->with(123)
  488. ->willReturn([]);
  489. $this->urlGenerator->expects($this->once())
  490. ->method('linkToRoute')
  491. ->with('files.view.index', ['fileNotFound' => true])
  492. ->willReturn('redirect.url');
  493. $response = $this->viewController->index('', 'MyView', '123');
  494. $this->assertInstanceOf('OCP\AppFramework\Http\RedirectResponse', $response);
  495. $this->assertEquals('redirect.url', $response->getRedirectURL());
  496. }
  497. public function testShowFileRouteWithTrashedFile() {
  498. $this->appManager->expects($this->once())
  499. ->method('isEnabledForUser')
  500. ->with('files_trashbin')
  501. ->willReturn(true);
  502. $parentNode = $this->getMockBuilder(Folder::class)->getMock();
  503. $parentNode->expects($this->once())
  504. ->method('getPath')
  505. ->willReturn('testuser1/files_trashbin/files/test.d1462861890/sub');
  506. $baseFolderFiles = $this->getMockBuilder(Folder::class)->getMock();
  507. $baseFolderTrash = $this->getMockBuilder(Folder::class)->getMock();
  508. $this->rootFolder->expects($this->once())
  509. ->method('getUserFolder')
  510. ->with('testuser1')
  511. ->willReturn($baseFolderFiles);
  512. $this->rootFolder->expects($this->once())
  513. ->method('get')
  514. ->with('testuser1/files_trashbin/files/')
  515. ->willReturn($baseFolderTrash);
  516. $baseFolderFiles->expects($this->once())
  517. ->method('getById')
  518. ->with(123)
  519. ->willReturn([]);
  520. $node = $this->getMockBuilder(File::class)->getMock();
  521. $node->expects($this->once())
  522. ->method('getParent')
  523. ->willReturn($parentNode);
  524. $node->expects($this->once())
  525. ->method('getName')
  526. ->willReturn('somefile.txt');
  527. $baseFolderTrash->expects($this->once())
  528. ->method('getById')
  529. ->with(123)
  530. ->willReturn([$node]);
  531. $baseFolderTrash->expects($this->once())
  532. ->method('getRelativePath')
  533. ->with('testuser1/files_trashbin/files/test.d1462861890/sub')
  534. ->willReturn('/test.d1462861890/sub');
  535. $this->urlGenerator
  536. ->expects($this->once())
  537. ->method('linkToRoute')
  538. ->with('files.view.index', ['view' => 'trashbin', 'dir' => '/test.d1462861890/sub', 'scrollto' => 'somefile.txt'])
  539. ->willReturn('/apps/files/?view=trashbin&dir=/test.d1462861890/sub&scrollto=somefile.txt');
  540. $expected = new Http\RedirectResponse('/apps/files/?view=trashbin&dir=/test.d1462861890/sub&scrollto=somefile.txt');
  541. $this->assertEquals($expected, $this->viewController->index('', '', '123'));
  542. }
  543. }