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.

413 lines
12 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@owncloud.com>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Vincent Petry <pvince81@owncloud.com>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\Files\Tests\Controller;
  26. use OCA\Files\Controller\ViewController;
  27. use OCP\AppFramework\Http;
  28. use OCP\Files\IRootFolder;
  29. use OCP\IUser;
  30. use OCP\Template;
  31. use Test\TestCase;
  32. use OCP\IRequest;
  33. use OCP\IURLGenerator;
  34. use OCP\AppFramework\Http\RedirectResponse;
  35. use OCP\INavigationManager;
  36. use OCP\IL10N;
  37. use OCP\IConfig;
  38. use OCP\IUserSession;
  39. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  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|\PHPUnit_Framework_MockObject_MockObject */
  48. private $request;
  49. /** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */
  50. private $urlGenerator;
  51. /** @var INavigationManager */
  52. private $navigationManager;
  53. /** @var IL10N */
  54. private $l10n;
  55. /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
  56. private $config;
  57. /** @var EventDispatcherInterface */
  58. private $eventDispatcher;
  59. /** @var ViewController|\PHPUnit_Framework_MockObject_MockObject */
  60. private $viewController;
  61. /** @var IUser */
  62. private $user;
  63. /** @var IUserSession */
  64. private $userSession;
  65. /** @var IAppManager|\PHPUnit_Framework_MockObject_MockObject */
  66. private $appManager;
  67. /** @var IRootFolder|\PHPUnit_Framework_MockObject_MockObject */
  68. private $rootFolder;
  69. public function setUp() {
  70. parent::setUp();
  71. $this->request = $this->getMockBuilder('\OCP\IRequest')->getMock();
  72. $this->urlGenerator = $this->getMockBuilder('\OCP\IURLGenerator')->getMock();
  73. $this->navigationManager = $this->getMockBuilder('\OCP\INavigationManager')->getMock();
  74. $this->l10n = $this->getMockBuilder('\OCP\IL10N')->getMock();
  75. $this->config = $this->getMockBuilder('\OCP\IConfig')->getMock();
  76. $this->eventDispatcher = $this->getMockBuilder('\Symfony\Component\EventDispatcher\EventDispatcherInterface')->getMock();
  77. $this->userSession = $this->getMockBuilder('\OCP\IUserSession')->getMock();
  78. $this->appManager = $this->getMockBuilder('\OCP\App\IAppManager')->getMock();
  79. $this->user = $this->getMockBuilder('\OCP\IUser')->getMock();
  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->getMockBuilder('\OCP\Files\IRootFolder')->getMock();
  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 testIndexWithRegularBrowser() {
  107. $this->viewController
  108. ->expects($this->once())
  109. ->method('getStorageInfo')
  110. ->will($this->returnValue([
  111. 'relative' => 123,
  112. 'owner' => 'MyName',
  113. 'ownerDisplayName' => 'MyDisplayName',
  114. ]));
  115. $this->config->expects($this->exactly(3))
  116. ->method('getUserValue')
  117. ->will($this->returnValueMap([
  118. [$this->user->getUID(), 'files', 'file_sorting', 'name', 'name'],
  119. [$this->user->getUID(), 'files', 'file_sorting_direction', 'asc', 'asc'],
  120. [$this->user->getUID(), 'files', 'show_hidden', false, false],
  121. ]));
  122. $this->config
  123. ->expects($this->any())
  124. ->method('getAppValue')
  125. ->will($this->returnArgument(2));
  126. $nav = new Template('files', 'appnavigation');
  127. $nav->assign('navigationItems', [
  128. [
  129. 'id' => 'files',
  130. 'appname' => 'files',
  131. 'script' => 'list.php',
  132. 'order' => 0,
  133. 'name' => \OC::$server->getL10N('files')->t('All files'),
  134. 'active' => false,
  135. 'icon' => '',
  136. ],
  137. [
  138. 'id' => 'recent',
  139. 'appname' => 'files',
  140. 'script' => 'recentlist.php',
  141. 'order' => 2,
  142. 'name' => \OC::$server->getL10N('files')->t('Recent'),
  143. 'active' => false,
  144. 'icon' => '',
  145. ],
  146. [
  147. 'id' => 'favorites',
  148. 'appname' => 'files',
  149. 'script' => 'simplelist.php',
  150. 'order' => 5,
  151. 'name' => null,
  152. 'active' => false,
  153. 'icon' => '',
  154. ],
  155. [
  156. 'id' => 'sharingin',
  157. 'appname' => 'files_sharing',
  158. 'script' => 'list.php',
  159. 'order' => 10,
  160. 'name' => \OC::$server->getL10N('files_sharing')->t('Shared with you'),
  161. 'active' => false,
  162. 'icon' => '',
  163. ],
  164. [
  165. 'id' => 'sharingout',
  166. 'appname' => 'files_sharing',
  167. 'script' => 'list.php',
  168. 'order' => 15,
  169. 'name' => \OC::$server->getL10N('files_sharing')->t('Shared with others'),
  170. 'active' => false,
  171. 'icon' => '',
  172. ],
  173. [
  174. 'id' => 'sharinglinks',
  175. 'appname' => 'files_sharing',
  176. 'script' => 'list.php',
  177. 'order' => 20,
  178. 'name' => \OC::$server->getL10N('files_sharing')->t('Shared by link', []),
  179. 'active' => false,
  180. 'icon' => '',
  181. ],
  182. [
  183. 'id' => 'systemtagsfilter',
  184. 'appname' => 'systemtags',
  185. 'script' => 'list.php',
  186. 'order' => 25,
  187. 'name' => \OC::$server->getL10N('systemtags')->t('Tags'),
  188. 'active' => false,
  189. 'icon' => '',
  190. ],
  191. [
  192. 'id' => 'trashbin',
  193. 'appname' => 'files_trashbin',
  194. 'script' => 'list.php',
  195. 'order' => 50,
  196. 'name' => \OC::$server->getL10N('files_trashbin')->t('Deleted files'),
  197. 'active' => false,
  198. 'icon' => '',
  199. ],
  200. ]);
  201. $expected = new Http\TemplateResponse(
  202. 'files',
  203. 'index',
  204. [
  205. 'usedSpacePercent' => 123,
  206. 'owner' => 'MyName',
  207. 'ownerDisplayName' => 'MyDisplayName',
  208. 'isPublic' => false,
  209. 'defaultFileSorting' => 'name',
  210. 'defaultFileSortingDirection' => 'asc',
  211. 'showHiddenFiles' => 0,
  212. 'fileNotFound' => 0,
  213. 'allowShareWithLink' => 'yes',
  214. 'appNavigation' => $nav,
  215. 'appContents' => [
  216. [
  217. 'id' => 'files',
  218. 'content' => null,
  219. ],
  220. [
  221. 'id' => 'recent',
  222. 'content' => null,
  223. ],
  224. [
  225. 'id' => 'favorites',
  226. 'content' => null,
  227. ],
  228. [
  229. 'id' => 'sharingin',
  230. 'content' => null,
  231. ],
  232. [
  233. 'id' => 'sharingout',
  234. 'content' => null,
  235. ],
  236. [
  237. 'id' => 'sharinglinks',
  238. 'content' => null,
  239. ],
  240. [
  241. 'id' => 'systemtagsfilter',
  242. 'content' => null,
  243. ],
  244. [
  245. 'id' => 'trashbin',
  246. 'content' => null,
  247. ],
  248. ],
  249. ]
  250. );
  251. $policy = new Http\ContentSecurityPolicy();
  252. $policy->addAllowedFrameDomain('\'self\'');
  253. $expected->setContentSecurityPolicy($policy);
  254. $this->assertEquals($expected, $this->viewController->index('MyDir', 'MyView'));
  255. }
  256. public function testShowFileRouteWithFolder() {
  257. $node = $this->getMockBuilder('\OCP\Files\Folder')->getMock();
  258. $node->expects($this->once())
  259. ->method('getPath')
  260. ->will($this->returnValue('/testuser1/files/test/sub'));
  261. $baseFolder = $this->getMockBuilder('\OCP\Files\Folder')->getMock();
  262. $this->rootFolder->expects($this->once())
  263. ->method('getUserFolder')
  264. ->with('testuser1')
  265. ->will($this->returnValue($baseFolder));
  266. $baseFolder->expects($this->at(0))
  267. ->method('getById')
  268. ->with(123)
  269. ->will($this->returnValue([$node]));
  270. $baseFolder->expects($this->at(1))
  271. ->method('getRelativePath')
  272. ->with('/testuser1/files/test/sub')
  273. ->will($this->returnValue('/test/sub'));
  274. $this->urlGenerator
  275. ->expects($this->once())
  276. ->method('linkToRoute')
  277. ->with('files.view.index', ['dir' => '/test/sub'])
  278. ->will($this->returnValue('/apps/files/?dir=/test/sub'));
  279. $expected = new Http\RedirectResponse('/apps/files/?dir=/test/sub');
  280. $this->assertEquals($expected, $this->viewController->index('/whatever', '', '123'));
  281. }
  282. public function testShowFileRouteWithFile() {
  283. $parentNode = $this->getMockBuilder('\OCP\Files\Folder')->getMock();
  284. $parentNode->expects($this->once())
  285. ->method('getPath')
  286. ->will($this->returnValue('testuser1/files/test'));
  287. $baseFolder = $this->getMockBuilder('\OCP\Files\Folder')->getMock();
  288. $this->rootFolder->expects($this->once())
  289. ->method('getUserFolder')
  290. ->with('testuser1')
  291. ->will($this->returnValue($baseFolder));
  292. $node = $this->getMockBuilder('\OCP\Files\File')->getMock();
  293. $node->expects($this->once())
  294. ->method('getParent')
  295. ->will($this->returnValue($parentNode));
  296. $node->expects($this->once())
  297. ->method('getName')
  298. ->will($this->returnValue('somefile.txt'));
  299. $baseFolder->expects($this->at(0))
  300. ->method('getById')
  301. ->with(123)
  302. ->will($this->returnValue([$node]));
  303. $baseFolder->expects($this->at(1))
  304. ->method('getRelativePath')
  305. ->with('testuser1/files/test')
  306. ->will($this->returnValue('/test'));
  307. $this->urlGenerator
  308. ->expects($this->once())
  309. ->method('linkToRoute')
  310. ->with('files.view.index', ['dir' => '/test', 'scrollto' => 'somefile.txt'])
  311. ->will($this->returnValue('/apps/files/?dir=/test/sub&scrollto=somefile.txt'));
  312. $expected = new Http\RedirectResponse('/apps/files/?dir=/test/sub&scrollto=somefile.txt');
  313. $this->assertEquals($expected, $this->viewController->index('/whatever', '', '123'));
  314. }
  315. public function testShowFileRouteWithInvalidFileId() {
  316. $baseFolder = $this->getMockBuilder('\OCP\Files\Folder')->getMock();
  317. $this->rootFolder->expects($this->once())
  318. ->method('getUserFolder')
  319. ->with('testuser1')
  320. ->will($this->returnValue($baseFolder));
  321. $baseFolder->expects($this->at(0))
  322. ->method('getById')
  323. ->with(123)
  324. ->will($this->returnValue([]));
  325. $this->urlGenerator->expects($this->once())
  326. ->method('linkToRoute')
  327. ->with('files.view.index', ['fileNotFound' => true])
  328. ->willReturn('redirect.url');
  329. $response = $this->viewController->index('MyDir', 'MyView', '123');
  330. $this->assertInstanceOf('OCP\AppFramework\Http\RedirectResponse', $response);
  331. $this->assertEquals('redirect.url', $response->getRedirectURL());
  332. }
  333. public function testShowFileRouteWithTrashedFile() {
  334. $this->appManager->expects($this->once())
  335. ->method('isEnabledForUser')
  336. ->with('files_trashbin')
  337. ->will($this->returnValue(true));
  338. $parentNode = $this->getMockBuilder('\OCP\Files\Folder')->getMock();
  339. $parentNode->expects($this->once())
  340. ->method('getPath')
  341. ->will($this->returnValue('testuser1/files_trashbin/files/test.d1462861890/sub'));
  342. $baseFolderFiles = $this->getMockBuilder('\OCP\Files\Folder')->getMock();
  343. $baseFolderTrash = $this->getMockBuilder('\OCP\Files\Folder')->getMock();
  344. $this->rootFolder->expects($this->at(0))
  345. ->method('getUserFolder')
  346. ->with('testuser1')
  347. ->will($this->returnValue($baseFolderFiles));
  348. $this->rootFolder->expects($this->at(1))
  349. ->method('get')
  350. ->with('testuser1/files_trashbin/files/')
  351. ->will($this->returnValue($baseFolderTrash));
  352. $baseFolderFiles->expects($this->once())
  353. ->method('getById')
  354. ->with(123)
  355. ->will($this->returnValue([]));
  356. $node = $this->getMockBuilder('\OCP\Files\File')->getMock();
  357. $node->expects($this->once())
  358. ->method('getParent')
  359. ->will($this->returnValue($parentNode));
  360. $node->expects($this->once())
  361. ->method('getName')
  362. ->will($this->returnValue('somefile.txt'));
  363. $baseFolderTrash->expects($this->at(0))
  364. ->method('getById')
  365. ->with(123)
  366. ->will($this->returnValue([$node]));
  367. $baseFolderTrash->expects($this->at(1))
  368. ->method('getRelativePath')
  369. ->with('testuser1/files_trashbin/files/test.d1462861890/sub')
  370. ->will($this->returnValue('/test.d1462861890/sub'));
  371. $this->urlGenerator
  372. ->expects($this->once())
  373. ->method('linkToRoute')
  374. ->with('files.view.index', ['view' => 'trashbin', 'dir' => '/test.d1462861890/sub', 'scrollto' => 'somefile.txt'])
  375. ->will($this->returnValue('/apps/files/?view=trashbin&dir=/test.d1462861890/sub&scrollto=somefile.txt'));
  376. $expected = new Http\RedirectResponse('/apps/files/?view=trashbin&dir=/test.d1462861890/sub&scrollto=somefile.txt');
  377. $this->assertEquals($expected, $this->viewController->index('/whatever', '', '123'));
  378. }
  379. }