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.

487 lines
14 KiB

10 years ago
  1. <?php
  2. /**
  3. * @author Christoph Wurst <christoph@owncloud.com>
  4. * @author Joas Schilling <nickvergessen@owncloud.com>
  5. * @author Lukas Reschke <lukas@statuscode.ch>
  6. * @author Vincent Petry <pvince81@owncloud.com>
  7. *
  8. * @copyright Copyright (c) 2016, ownCloud, Inc.
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OCA\Files\Tests\Controller;
  25. use OCA\Files\Controller\ViewController;
  26. use OCP\AppFramework\Http;
  27. use OCP\Files\NotFoundException;
  28. use OCP\IUser;
  29. use OCP\Template;
  30. use Test\TestCase;
  31. use OCP\IRequest;
  32. use OCP\IURLGenerator;
  33. use OCP\AppFramework\Http\RedirectResponse;
  34. use OCP\INavigationManager;
  35. use OCP\IL10N;
  36. use OCP\IConfig;
  37. use OCP\IUserSession;
  38. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  39. use OCP\Files\Folder;
  40. use OCP\App\IAppManager;
  41. /**
  42. * Class ViewControllerTest
  43. *
  44. * @package OCA\Files\Tests\Controller
  45. */
  46. class ViewControllerTest extends TestCase {
  47. /** @var IRequest */
  48. private $request;
  49. /** @var IURLGenerator */
  50. private $urlGenerator;
  51. /** @var INavigationManager */
  52. private $navigationManager;
  53. /** @var IL10N */
  54. private $l10n;
  55. /** @var IConfig */
  56. private $config;
  57. /** @var EventDispatcherInterface */
  58. private $eventDispatcher;
  59. /** @var ViewController */
  60. private $viewController;
  61. /** @var IUser */
  62. private $user;
  63. /** @var IUserSession */
  64. private $userSession;
  65. /** @var IAppManager */
  66. private $appManager;
  67. /** @var Folder */
  68. private $rootFolder;
  69. public function setUp() {
  70. parent::setUp();
  71. $this->request = $this->getMock('\OCP\IRequest');
  72. $this->urlGenerator = $this->getMock('\OCP\IURLGenerator');
  73. $this->navigationManager = $this->getMock('\OCP\INavigationManager');
  74. $this->l10n = $this->getMock('\OCP\IL10N');
  75. $this->config = $this->getMock('\OCP\IConfig');
  76. $this->eventDispatcher = $this->getMock('\Symfony\Component\EventDispatcher\EventDispatcherInterface');
  77. $this->userSession = $this->getMock('\OCP\IUserSession');
  78. $this->appManager = $this->getMock('\OCP\App\IAppManager');
  79. $this->user = $this->getMock('\OCP\IUser');
  80. $this->user->expects($this->any())
  81. ->method('getUID')
  82. ->will($this->returnValue('testuser1'));
  83. $this->userSession->expects($this->any())
  84. ->method('getUser')
  85. ->will($this->returnValue($this->user));
  86. $this->rootFolder = $this->getMock('\OCP\Files\Folder');
  87. $this->viewController = $this->getMockBuilder('\OCA\Files\Controller\ViewController')
  88. ->setConstructorArgs([
  89. 'files',
  90. $this->request,
  91. $this->urlGenerator,
  92. $this->navigationManager,
  93. $this->l10n,
  94. $this->config,
  95. $this->eventDispatcher,
  96. $this->userSession,
  97. $this->appManager,
  98. $this->rootFolder
  99. ])
  100. ->setMethods([
  101. 'getStorageInfo',
  102. 'renderScript'
  103. ])
  104. ->getMock();
  105. }
  106. public function testIndexWithIE8RedirectAndDirDefined() {
  107. $this->request
  108. ->expects($this->once())
  109. ->method('isUserAgent')
  110. ->with(['/MSIE 8.0/'])
  111. ->will($this->returnValue(true));
  112. $this->urlGenerator
  113. ->expects($this->once())
  114. ->method('linkToRoute')
  115. ->with('files.view.index')
  116. ->will($this->returnValue('/apps/files/'));
  117. $expected = new Http\RedirectResponse('/apps/files/#?dir=MyDir');
  118. $this->assertEquals($expected, $this->viewController->index('MyDir'));
  119. }
  120. public function testIndexWithIE8RedirectAndViewDefined() {
  121. $this->request
  122. ->expects($this->once())
  123. ->method('isUserAgent')
  124. ->with(['/MSIE 8.0/'])
  125. ->will($this->returnValue(true));
  126. $this->urlGenerator
  127. ->expects($this->once())
  128. ->method('linkToRoute')
  129. ->with('files.view.index')
  130. ->will($this->returnValue('/apps/files/'));
  131. $expected = new Http\RedirectResponse('/apps/files/#?dir=/&view=MyView');
  132. $this->assertEquals($expected, $this->viewController->index('', 'MyView'));
  133. }
  134. public function testIndexWithIE8RedirectAndViewAndDirDefined() {
  135. $this->request
  136. ->expects($this->once())
  137. ->method('isUserAgent')
  138. ->with(['/MSIE 8.0/'])
  139. ->will($this->returnValue(true));
  140. $this->urlGenerator
  141. ->expects($this->once())
  142. ->method('linkToRoute')
  143. ->with('files.view.index')
  144. ->will($this->returnValue('/apps/files/'));
  145. $expected = new RedirectResponse('/apps/files/#?dir=MyDir&view=MyView');
  146. $this->assertEquals($expected, $this->viewController->index('MyDir', 'MyView'));
  147. }
  148. public function testIndexWithRegularBrowser() {
  149. $this->request
  150. ->expects($this->once())
  151. ->method('isUserAgent')
  152. ->with(['/MSIE 8.0/'])
  153. ->will($this->returnValue(false));
  154. $this->viewController
  155. ->expects($this->once())
  156. ->method('getStorageInfo')
  157. ->will($this->returnValue([
  158. 'relative' => 123,
  159. 'owner' => 'MyName',
  160. 'ownerDisplayName' => 'MyDisplayName',
  161. ]));
  162. $this->config->expects($this->exactly(3))
  163. ->method('getUserValue')
  164. ->will($this->returnValueMap([
  165. [$this->user->getUID(), 'files', 'file_sorting', 'name', 'name'],
  166. [$this->user->getUID(), 'files', 'file_sorting_direction', 'asc', 'asc'],
  167. [$this->user->getUID(), 'files', 'show_hidden', false, false],
  168. ]));
  169. $this->config
  170. ->expects($this->any())
  171. ->method('getAppValue')
  172. ->will($this->returnArgument(2));
  173. $nav = new Template('files', 'appnavigation');
  174. $nav->assign('navigationItems', [
  175. [
  176. 'id' => 'files',
  177. 'appname' => 'files',
  178. 'script' => 'list.php',
  179. 'order' => 0,
  180. 'name' => new \OC_L10N_String(new \OC_L10N('files'), 'All files', []),
  181. 'active' => false,
  182. 'icon' => '',
  183. ],
  184. [
  185. 'id' => 'favorites',
  186. 'appname' => 'files',
  187. 'script' => 'simplelist.php',
  188. 'order' => 5,
  189. 'name' => null,
  190. 'active' => false,
  191. 'icon' => '',
  192. ],
  193. [
  194. 'id' => 'sharingin',
  195. 'appname' => 'files_sharing',
  196. 'script' => 'list.php',
  197. 'order' => 10,
  198. 'name' => new \OC_L10N_String(new \OC_L10N('files_sharing'), 'Shared with you', []),
  199. 'active' => false,
  200. 'icon' => '',
  201. ],
  202. [
  203. 'id' => 'sharingout',
  204. 'appname' => 'files_sharing',
  205. 'script' => 'list.php',
  206. 'order' => 15,
  207. 'name' => new \OC_L10N_String(new \OC_L10N('files_sharing'), 'Shared with others', []),
  208. 'active' => false,
  209. 'icon' => '',
  210. ],
  211. [
  212. 'id' => 'sharinglinks',
  213. 'appname' => 'files_sharing',
  214. 'script' => 'list.php',
  215. 'order' => 20,
  216. 'name' => new \OC_L10N_String(new \OC_L10N('files_sharing'), 'Shared by link', []),
  217. 'active' => false,
  218. 'icon' => '',
  219. ],
  220. [
  221. 'id' => 'systemtagsfilter',
  222. 'appname' => 'systemtags',
  223. 'script' => 'list.php',
  224. 'order' => 25,
  225. 'name' => new \OC_L10N_String(new \OC_L10N('systemtags'), 'Tags', []),
  226. 'active' => false,
  227. 'icon' => '',
  228. ],
  229. [
  230. 'id' => 'trashbin',
  231. 'appname' => 'files_trashbin',
  232. 'script' => 'list.php',
  233. 'order' => 50,
  234. 'name' => new \OC_L10N_String(new \OC_L10N('files_trashbin'), 'Deleted files', []),
  235. 'active' => false,
  236. 'icon' => '',
  237. ],
  238. ]);
  239. $expected = new Http\TemplateResponse(
  240. 'files',
  241. 'index',
  242. [
  243. 'usedSpacePercent' => 123,
  244. 'owner' => 'MyName',
  245. 'ownerDisplayName' => 'MyDisplayName',
  246. 'isPublic' => false,
  247. 'defaultFileSorting' => 'name',
  248. 'defaultFileSortingDirection' => 'asc',
  249. 'showHiddenFiles' => 0,
  250. 'fileNotFound' => 0,
  251. 'mailNotificationEnabled' => 'no',
  252. 'mailPublicNotificationEnabled' => 'no',
  253. 'allowShareWithLink' => 'yes',
  254. 'appNavigation' => $nav,
  255. 'appContents' => [
  256. [
  257. 'id' => 'files',
  258. 'content' => null,
  259. ],
  260. [
  261. 'id' => 'favorites',
  262. 'content' => null,
  263. ],
  264. [
  265. 'id' => 'sharingin',
  266. 'content' => null,
  267. ],
  268. [
  269. 'id' => 'sharingout',
  270. 'content' => null,
  271. ],
  272. [
  273. 'id' => 'sharinglinks',
  274. 'content' => null,
  275. ],
  276. [
  277. 'id' => 'systemtagsfilter',
  278. 'content' => null,
  279. ],
  280. [
  281. 'id' => 'trashbin',
  282. 'content' => null,
  283. ],
  284. ],
  285. ]
  286. );
  287. $policy = new Http\ContentSecurityPolicy();
  288. $policy->addAllowedFrameDomain('\'self\'');
  289. $expected->setContentSecurityPolicy($policy);
  290. $this->assertEquals($expected, $this->viewController->index('MyDir', 'MyView'));
  291. }
  292. public function showFileMethodProvider() {
  293. return [
  294. [true],
  295. [false],
  296. ];
  297. }
  298. /**
  299. * @dataProvider showFileMethodProvider
  300. */
  301. public function testShowFileRouteWithFolder($useShowFile) {
  302. $node = $this->getMock('\OCP\Files\Folder');
  303. $node->expects($this->once())
  304. ->method('getPath')
  305. ->will($this->returnValue('/testuser1/files/test/sub'));
  306. $baseFolder = $this->getMock('\OCP\Files\Folder');
  307. $this->rootFolder->expects($this->once())
  308. ->method('get')
  309. ->with('testuser1/files/')
  310. ->will($this->returnValue($baseFolder));
  311. $baseFolder->expects($this->at(0))
  312. ->method('getById')
  313. ->with(123)
  314. ->will($this->returnValue([$node]));
  315. $baseFolder->expects($this->at(1))
  316. ->method('getRelativePath')
  317. ->with('/testuser1/files/test/sub')
  318. ->will($this->returnValue('/test/sub'));
  319. $this->urlGenerator
  320. ->expects($this->once())
  321. ->method('linkToRoute')
  322. ->with('files.view.index', ['dir' => '/test/sub'])
  323. ->will($this->returnValue('/apps/files/?dir=/test/sub'));
  324. $expected = new Http\RedirectResponse('/apps/files/?dir=/test/sub');
  325. if ($useShowFile) {
  326. $this->assertEquals($expected, $this->viewController->showFile(123));
  327. } else {
  328. $this->assertEquals($expected, $this->viewController->index('/whatever', '', '123'));
  329. }
  330. }
  331. /**
  332. * @dataProvider showFileMethodProvider
  333. */
  334. public function testShowFileRouteWithFile($useShowFile) {
  335. $parentNode = $this->getMock('\OCP\Files\Folder');
  336. $parentNode->expects($this->once())
  337. ->method('getPath')
  338. ->will($this->returnValue('testuser1/files/test'));
  339. $baseFolder = $this->getMock('\OCP\Files\Folder');
  340. $this->rootFolder->expects($this->once())
  341. ->method('get')
  342. ->with('testuser1/files/')
  343. ->will($this->returnValue($baseFolder));
  344. $node = $this->getMock('\OCP\Files\File');
  345. $node->expects($this->once())
  346. ->method('getParent')
  347. ->will($this->returnValue($parentNode));
  348. $node->expects($this->once())
  349. ->method('getName')
  350. ->will($this->returnValue('somefile.txt'));
  351. $baseFolder->expects($this->at(0))
  352. ->method('getById')
  353. ->with(123)
  354. ->will($this->returnValue([$node]));
  355. $baseFolder->expects($this->at(1))
  356. ->method('getRelativePath')
  357. ->with('testuser1/files/test')
  358. ->will($this->returnValue('/test'));
  359. $this->urlGenerator
  360. ->expects($this->once())
  361. ->method('linkToRoute')
  362. ->with('files.view.index', ['dir' => '/test', 'scrollto' => 'somefile.txt'])
  363. ->will($this->returnValue('/apps/files/?dir=/test/sub&scrollto=somefile.txt'));
  364. $expected = new Http\RedirectResponse('/apps/files/?dir=/test/sub&scrollto=somefile.txt');
  365. if ($useShowFile) {
  366. $this->assertEquals($expected, $this->viewController->showFile(123));
  367. } else {
  368. $this->assertEquals($expected, $this->viewController->index('/whatever', '', '123'));
  369. }
  370. }
  371. /**
  372. * @dataProvider showFileMethodProvider
  373. */
  374. public function testShowFileRouteWithInvalidFileId($useShowFile) {
  375. $baseFolder = $this->getMock('\OCP\Files\Folder');
  376. $this->rootFolder->expects($this->once())
  377. ->method('get')
  378. ->with('testuser1/files/')
  379. ->will($this->returnValue($baseFolder));
  380. $baseFolder->expects($this->at(0))
  381. ->method('getById')
  382. ->with(123)
  383. ->will($this->returnValue([]));
  384. if ($useShowFile) {
  385. $this->expectException('OCP\Files\NotFoundException');
  386. $this->viewController->showFile(123);
  387. } else {
  388. $response = $this->viewController->index('MyDir', 'MyView', '123');
  389. $this->assertInstanceOf('OCP\AppFramework\Http\TemplateResponse', $response);
  390. $params = $response->getParams();
  391. $this->assertEquals(1, $params['fileNotFound']);
  392. }
  393. }
  394. /**
  395. * @dataProvider showFileMethodProvider
  396. */
  397. public function testShowFileRouteWithTrashedFile($useShowFile) {
  398. $this->appManager->expects($this->once())
  399. ->method('isEnabledForUser')
  400. ->with('files_trashbin')
  401. ->will($this->returnValue(true));
  402. $parentNode = $this->getMock('\OCP\Files\Folder');
  403. $parentNode->expects($this->once())
  404. ->method('getPath')
  405. ->will($this->returnValue('testuser1/files_trashbin/files/test.d1462861890/sub'));
  406. $baseFolderFiles = $this->getMock('\OCP\Files\Folder');
  407. $baseFolderTrash = $this->getMock('\OCP\Files\Folder');
  408. $this->rootFolder->expects($this->at(0))
  409. ->method('get')
  410. ->with('testuser1/files/')
  411. ->will($this->returnValue($baseFolderFiles));
  412. $this->rootFolder->expects($this->at(1))
  413. ->method('get')
  414. ->with('testuser1/files_trashbin/files/')
  415. ->will($this->returnValue($baseFolderTrash));
  416. $baseFolderFiles->expects($this->once())
  417. ->method('getById')
  418. ->with(123)
  419. ->will($this->returnValue([]));
  420. $node = $this->getMock('\OCP\Files\File');
  421. $node->expects($this->once())
  422. ->method('getParent')
  423. ->will($this->returnValue($parentNode));
  424. $node->expects($this->once())
  425. ->method('getName')
  426. ->will($this->returnValue('somefile.txt'));
  427. $baseFolderTrash->expects($this->at(0))
  428. ->method('getById')
  429. ->with(123)
  430. ->will($this->returnValue([$node]));
  431. $baseFolderTrash->expects($this->at(1))
  432. ->method('getRelativePath')
  433. ->with('testuser1/files_trashbin/files/test.d1462861890/sub')
  434. ->will($this->returnValue('/test.d1462861890/sub'));
  435. $this->urlGenerator
  436. ->expects($this->once())
  437. ->method('linkToRoute')
  438. ->with('files.view.index', ['view' => 'trashbin', 'dir' => '/test.d1462861890/sub', 'scrollto' => 'somefile.txt'])
  439. ->will($this->returnValue('/apps/files/?view=trashbin&dir=/test.d1462861890/sub&scrollto=somefile.txt'));
  440. $expected = new Http\RedirectResponse('/apps/files/?view=trashbin&dir=/test.d1462861890/sub&scrollto=somefile.txt');
  441. if ($useShowFile) {
  442. $this->assertEquals($expected, $this->viewController->showFile(123));
  443. } else {
  444. $this->assertEquals($expected, $this->viewController->index('/whatever', '', '123'));
  445. }
  446. }
  447. }