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.

view.php 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. <?php
  2. /**
  3. * Copyright (c) 2012 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file. */
  7. namespace Test\Files;
  8. use OC\Files\Cache\Watcher;
  9. class TemporaryNoTouch extends \OC\Files\Storage\Temporary {
  10. public function touch($path, $mtime = null) {
  11. return false;
  12. }
  13. }
  14. class View extends \PHPUnit_Framework_TestCase {
  15. /**
  16. * @var \OC\Files\Storage\Storage[] $storages
  17. */
  18. private $storages = array();
  19. private $user;
  20. public function setUp() {
  21. \OC_User::clearBackends();
  22. \OC_User::useBackend(new \OC_User_Dummy());
  23. //login
  24. \OC_User::createUser('test', 'test');
  25. $this->user = \OC_User::getUser();
  26. \OC_User::setUserId('test');
  27. \OC\Files\Filesystem::clearMounts();
  28. }
  29. public function tearDown() {
  30. \OC_User::setUserId($this->user);
  31. foreach ($this->storages as $storage) {
  32. $cache = $storage->getCache();
  33. $ids = $cache->getAll();
  34. $cache->clear();
  35. }
  36. }
  37. /**
  38. * @medium
  39. */
  40. public function testCacheAPI() {
  41. $storage1 = $this->getTestStorage();
  42. $storage2 = $this->getTestStorage();
  43. $storage3 = $this->getTestStorage();
  44. \OC\Files\Filesystem::mount($storage1, array(), '/');
  45. \OC\Files\Filesystem::mount($storage2, array(), '/substorage');
  46. \OC\Files\Filesystem::mount($storage3, array(), '/folder/anotherstorage');
  47. $textSize = strlen("dummy file data\n");
  48. $imageSize = filesize(\OC::$SERVERROOT . '/core/img/logo.png');
  49. $storageSize = $textSize * 2 + $imageSize;
  50. $rootView = new \OC\Files\View('');
  51. $cachedData = $rootView->getFileInfo('/foo.txt');
  52. $this->assertEquals($textSize, $cachedData['size']);
  53. $this->assertEquals('text/plain', $cachedData['mimetype']);
  54. $this->assertNotEquals(-1, $cachedData['permissions']);
  55. $cachedData = $rootView->getFileInfo('/');
  56. $this->assertEquals($storageSize * 3, $cachedData['size']);
  57. $this->assertEquals('httpd/unix-directory', $cachedData['mimetype']);
  58. // get cached data excluding mount points
  59. $cachedData = $rootView->getFileInfo('/', false);
  60. $this->assertEquals($storageSize, $cachedData['size']);
  61. $this->assertEquals('httpd/unix-directory', $cachedData['mimetype']);
  62. $cachedData = $rootView->getFileInfo('/folder');
  63. $this->assertEquals($storageSize + $textSize, $cachedData['size']);
  64. $this->assertEquals('httpd/unix-directory', $cachedData['mimetype']);
  65. $folderData = $rootView->getDirectoryContent('/');
  66. /**
  67. * expected entries:
  68. * folder
  69. * foo.png
  70. * foo.txt
  71. * substorage
  72. */
  73. $this->assertEquals(4, count($folderData));
  74. $this->assertEquals('folder', $folderData[0]['name']);
  75. $this->assertEquals('foo.png', $folderData[1]['name']);
  76. $this->assertEquals('foo.txt', $folderData[2]['name']);
  77. $this->assertEquals('substorage', $folderData[3]['name']);
  78. $this->assertEquals($storageSize + $textSize, $folderData[0]['size']);
  79. $this->assertEquals($imageSize, $folderData[1]['size']);
  80. $this->assertEquals($textSize, $folderData[2]['size']);
  81. $this->assertEquals($storageSize, $folderData[3]['size']);
  82. $folderData = $rootView->getDirectoryContent('/substorage');
  83. /**
  84. * expected entries:
  85. * folder
  86. * foo.png
  87. * foo.txt
  88. */
  89. $this->assertEquals(3, count($folderData));
  90. $this->assertEquals('folder', $folderData[0]['name']);
  91. $this->assertEquals('foo.png', $folderData[1]['name']);
  92. $this->assertEquals('foo.txt', $folderData[2]['name']);
  93. $folderView = new \OC\Files\View('/folder');
  94. $this->assertEquals($rootView->getFileInfo('/folder'), $folderView->getFileInfo('/'));
  95. $cachedData = $rootView->getFileInfo('/foo.txt');
  96. $this->assertFalse($cachedData['encrypted']);
  97. $id = $rootView->putFileInfo('/foo.txt', array('encrypted' => true));
  98. $cachedData = $rootView->getFileInfo('/foo.txt');
  99. $this->assertTrue($cachedData['encrypted']);
  100. $this->assertEquals($cachedData['fileid'], $id);
  101. $this->assertFalse($rootView->getFileInfo('/non/existing'));
  102. $this->assertEquals(array(), $rootView->getDirectoryContent('/non/existing'));
  103. }
  104. /**
  105. * @medium
  106. */
  107. function testGetPath() {
  108. $storage1 = $this->getTestStorage();
  109. $storage2 = $this->getTestStorage();
  110. $storage3 = $this->getTestStorage();
  111. \OC\Files\Filesystem::mount($storage1, array(), '/');
  112. \OC\Files\Filesystem::mount($storage2, array(), '/substorage');
  113. \OC\Files\Filesystem::mount($storage3, array(), '/folder/anotherstorage');
  114. $rootView = new \OC\Files\View('');
  115. $cachedData = $rootView->getFileInfo('/foo.txt');
  116. $id1 = $cachedData['fileid'];
  117. $this->assertEquals('/foo.txt', $rootView->getPath($id1));
  118. $cachedData = $rootView->getFileInfo('/substorage/foo.txt');
  119. $id2 = $cachedData['fileid'];
  120. $this->assertEquals('/substorage/foo.txt', $rootView->getPath($id2));
  121. $folderView = new \OC\Files\View('/substorage');
  122. $this->assertEquals('/foo.txt', $folderView->getPath($id2));
  123. $this->assertNull($folderView->getPath($id1));
  124. }
  125. /**
  126. * @medium
  127. */
  128. function testMountPointOverwrite() {
  129. $storage1 = $this->getTestStorage(false);
  130. $storage2 = $this->getTestStorage();
  131. $storage1->mkdir('substorage');
  132. \OC\Files\Filesystem::mount($storage1, array(), '/');
  133. \OC\Files\Filesystem::mount($storage2, array(), '/substorage');
  134. $rootView = new \OC\Files\View('');
  135. $folderContent = $rootView->getDirectoryContent('/');
  136. $this->assertEquals(4, count($folderContent));
  137. }
  138. function testCacheIncompleteFolder() {
  139. $storage1 = $this->getTestStorage(false);
  140. \OC\Files\Filesystem::mount($storage1, array(), '/');
  141. $rootView = new \OC\Files\View('');
  142. $entries = $rootView->getDirectoryContent('/');
  143. $this->assertEquals(3, count($entries));
  144. // /folder will already be in the cache but not scanned
  145. $entries = $rootView->getDirectoryContent('/folder');
  146. $this->assertEquals(1, count($entries));
  147. }
  148. public function testAutoScan() {
  149. $storage1 = $this->getTestStorage(false);
  150. $storage2 = $this->getTestStorage(false);
  151. \OC\Files\Filesystem::mount($storage1, array(), '/');
  152. \OC\Files\Filesystem::mount($storage2, array(), '/substorage');
  153. $textSize = strlen("dummy file data\n");
  154. $rootView = new \OC\Files\View('');
  155. $cachedData = $rootView->getFileInfo('/');
  156. $this->assertEquals('httpd/unix-directory', $cachedData['mimetype']);
  157. $this->assertEquals(-1, $cachedData['size']);
  158. $folderData = $rootView->getDirectoryContent('/substorage/folder');
  159. $this->assertEquals('text/plain', $folderData[0]['mimetype']);
  160. $this->assertEquals($textSize, $folderData[0]['size']);
  161. }
  162. /**
  163. * @medium
  164. */
  165. function testSearch() {
  166. $storage1 = $this->getTestStorage();
  167. $storage2 = $this->getTestStorage();
  168. $storage3 = $this->getTestStorage();
  169. \OC\Files\Filesystem::mount($storage1, array(), '/');
  170. \OC\Files\Filesystem::mount($storage2, array(), '/substorage');
  171. \OC\Files\Filesystem::mount($storage3, array(), '/folder/anotherstorage');
  172. $rootView = new \OC\Files\View('');
  173. $results = $rootView->search('foo');
  174. $this->assertEquals(6, count($results));
  175. $paths = array();
  176. foreach ($results as $result) {
  177. $this->assertEquals($result['path'], \OC\Files\Filesystem::normalizePath($result['path']));
  178. $paths[] = $result['path'];
  179. }
  180. $this->assertContains('/foo.txt', $paths);
  181. $this->assertContains('/foo.png', $paths);
  182. $this->assertContains('/substorage/foo.txt', $paths);
  183. $this->assertContains('/substorage/foo.png', $paths);
  184. $this->assertContains('/folder/anotherstorage/foo.txt', $paths);
  185. $this->assertContains('/folder/anotherstorage/foo.png', $paths);
  186. $folderView = new \OC\Files\View('/folder');
  187. $results = $folderView->search('bar');
  188. $this->assertEquals(2, count($results));
  189. $paths = array();
  190. foreach ($results as $result) {
  191. $paths[] = $result['path'];
  192. }
  193. $this->assertContains('/anotherstorage/folder/bar.txt', $paths);
  194. $this->assertContains('/bar.txt', $paths);
  195. $results = $folderView->search('foo');
  196. $this->assertEquals(2, count($results));
  197. $paths = array();
  198. foreach ($results as $result) {
  199. $paths[] = $result['path'];
  200. }
  201. $this->assertContains('/anotherstorage/foo.txt', $paths);
  202. $this->assertContains('/anotherstorage/foo.png', $paths);
  203. $this->assertEquals(6, count($rootView->searchByMime('text')));
  204. $this->assertEquals(3, count($folderView->searchByMime('text')));
  205. }
  206. /**
  207. * @medium
  208. */
  209. function testWatcher() {
  210. $storage1 = $this->getTestStorage();
  211. \OC\Files\Filesystem::mount($storage1, array(), '/');
  212. $storage1->getWatcher()->setPolicy(Watcher::CHECK_ALWAYS);
  213. $rootView = new \OC\Files\View('');
  214. $cachedData = $rootView->getFileInfo('foo.txt');
  215. $this->assertEquals(16, $cachedData['size']);
  216. $rootView->putFileInfo('foo.txt', array('storage_mtime' => 10));
  217. $storage1->file_put_contents('foo.txt', 'foo');
  218. clearstatcache();
  219. $cachedData = $rootView->getFileInfo('foo.txt');
  220. $this->assertEquals(3, $cachedData['size']);
  221. }
  222. /**
  223. * @medium
  224. */
  225. function testCopyBetweenStorages() {
  226. $storage1 = $this->getTestStorage();
  227. $storage2 = $this->getTestStorage();
  228. \OC\Files\Filesystem::mount($storage1, array(), '/');
  229. \OC\Files\Filesystem::mount($storage2, array(), '/substorage');
  230. $rootView = new \OC\Files\View('');
  231. $rootView->mkdir('substorage/emptyfolder');
  232. $rootView->copy('substorage', 'anotherfolder');
  233. $this->assertTrue($rootView->is_dir('/anotherfolder'));
  234. $this->assertTrue($rootView->is_dir('/substorage'));
  235. $this->assertTrue($rootView->is_dir('/anotherfolder/emptyfolder'));
  236. $this->assertTrue($rootView->is_dir('/substorage/emptyfolder'));
  237. $this->assertTrue($rootView->file_exists('/anotherfolder/foo.txt'));
  238. $this->assertTrue($rootView->file_exists('/anotherfolder/foo.png'));
  239. $this->assertTrue($rootView->file_exists('/anotherfolder/folder/bar.txt'));
  240. $this->assertTrue($rootView->file_exists('/substorage/foo.txt'));
  241. $this->assertTrue($rootView->file_exists('/substorage/foo.png'));
  242. $this->assertTrue($rootView->file_exists('/substorage/folder/bar.txt'));
  243. }
  244. /**
  245. * @medium
  246. */
  247. function testMoveBetweenStorages() {
  248. $storage1 = $this->getTestStorage();
  249. $storage2 = $this->getTestStorage();
  250. \OC\Files\Filesystem::mount($storage1, array(), '/');
  251. \OC\Files\Filesystem::mount($storage2, array(), '/substorage');
  252. $rootView = new \OC\Files\View('');
  253. $rootView->rename('foo.txt', 'substorage/folder/foo.txt');
  254. $this->assertFalse($rootView->file_exists('foo.txt'));
  255. $this->assertTrue($rootView->file_exists('substorage/folder/foo.txt'));
  256. $rootView->rename('substorage/folder', 'anotherfolder');
  257. $this->assertFalse($rootView->is_dir('substorage/folder'));
  258. $this->assertTrue($rootView->file_exists('anotherfolder/foo.txt'));
  259. $this->assertTrue($rootView->file_exists('anotherfolder/bar.txt'));
  260. }
  261. /**
  262. * @medium
  263. */
  264. function testUnlink() {
  265. $storage1 = $this->getTestStorage();
  266. $storage2 = $this->getTestStorage();
  267. \OC\Files\Filesystem::mount($storage1, array(), '/');
  268. \OC\Files\Filesystem::mount($storage2, array(), '/substorage');
  269. $rootView = new \OC\Files\View('');
  270. $rootView->file_put_contents('/foo.txt', 'asd');
  271. $rootView->file_put_contents('/substorage/bar.txt', 'asd');
  272. $this->assertTrue($rootView->file_exists('foo.txt'));
  273. $this->assertTrue($rootView->file_exists('substorage/bar.txt'));
  274. $this->assertTrue($rootView->unlink('foo.txt'));
  275. $this->assertTrue($rootView->unlink('substorage/bar.txt'));
  276. $this->assertFalse($rootView->file_exists('foo.txt'));
  277. $this->assertFalse($rootView->file_exists('substorage/bar.txt'));
  278. }
  279. /**
  280. * @medium
  281. */
  282. function testUnlinkRootMustFail() {
  283. $storage1 = $this->getTestStorage();
  284. $storage2 = $this->getTestStorage();
  285. \OC\Files\Filesystem::mount($storage1, array(), '/');
  286. \OC\Files\Filesystem::mount($storage2, array(), '/substorage');
  287. $rootView = new \OC\Files\View('');
  288. $rootView->file_put_contents('/foo.txt', 'asd');
  289. $rootView->file_put_contents('/substorage/bar.txt', 'asd');
  290. $this->assertFalse($rootView->unlink(''));
  291. $this->assertFalse($rootView->unlink('/'));
  292. $this->assertFalse($rootView->unlink('substorage'));
  293. $this->assertFalse($rootView->unlink('/substorage'));
  294. }
  295. /**
  296. * @medium
  297. */
  298. function testTouch() {
  299. $storage = $this->getTestStorage(true, '\Test\Files\TemporaryNoTouch');
  300. \OC\Files\Filesystem::mount($storage, array(), '/');
  301. $rootView = new \OC\Files\View('');
  302. $oldCachedData = $rootView->getFileInfo('foo.txt');
  303. $rootView->touch('foo.txt', 500);
  304. $cachedData = $rootView->getFileInfo('foo.txt');
  305. $this->assertEquals(500, $cachedData['mtime']);
  306. $this->assertEquals($oldCachedData['storage_mtime'], $cachedData['storage_mtime']);
  307. $rootView->putFileInfo('foo.txt', array('storage_mtime' => 1000)); //make sure the watcher detects the change
  308. $rootView->file_put_contents('foo.txt', 'asd');
  309. $cachedData = $rootView->getFileInfo('foo.txt');
  310. $this->assertGreaterThanOrEqual($cachedData['mtime'], $oldCachedData['mtime']);
  311. $this->assertEquals($cachedData['storage_mtime'], $cachedData['mtime']);
  312. }
  313. /**
  314. * @medium
  315. */
  316. function testViewHooks() {
  317. $storage1 = $this->getTestStorage();
  318. $storage2 = $this->getTestStorage();
  319. $defaultRoot = \OC\Files\Filesystem::getRoot();
  320. \OC\Files\Filesystem::mount($storage1, array(), '/');
  321. \OC\Files\Filesystem::mount($storage2, array(), $defaultRoot . '/substorage');
  322. \OC_Hook::connect('OC_Filesystem', 'post_write', $this, 'dummyHook');
  323. $rootView = new \OC\Files\View('');
  324. $subView = new \OC\Files\View($defaultRoot . '/substorage');
  325. $this->hookPath = null;
  326. $rootView->file_put_contents('/foo.txt', 'asd');
  327. $this->assertNull($this->hookPath);
  328. $subView->file_put_contents('/foo.txt', 'asd');
  329. $this->assertEquals('/substorage/foo.txt', $this->hookPath);
  330. }
  331. private $hookPath;
  332. public function dummyHook($params) {
  333. $this->hookPath = $params['path'];
  334. }
  335. public function testSearchNotOutsideView() {
  336. $storage1 = $this->getTestStorage();
  337. \OC\Files\Filesystem::mount($storage1, array(), '/');
  338. $storage1->rename('folder', 'foo');
  339. $scanner = $storage1->getScanner();
  340. $scanner->scan('');
  341. $view = new \OC\Files\View('/foo');
  342. $result = $view->search('.txt');
  343. $this->assertCount(1, $result);
  344. }
  345. /**
  346. * @param bool $scan
  347. * @param string $class
  348. * @return \OC\Files\Storage\Storage
  349. */
  350. private function getTestStorage($scan = true, $class = '\OC\Files\Storage\Temporary') {
  351. /**
  352. * @var \OC\Files\Storage\Storage $storage
  353. */
  354. $storage = new $class(array());
  355. $textData = "dummy file data\n";
  356. $imgData = file_get_contents(\OC::$SERVERROOT . '/core/img/logo.png');
  357. $storage->mkdir('folder');
  358. $storage->file_put_contents('foo.txt', $textData);
  359. $storage->file_put_contents('foo.png', $imgData);
  360. $storage->file_put_contents('folder/bar.txt', $textData);
  361. if ($scan) {
  362. $scanner = $storage->getScanner();
  363. $scanner->scan('');
  364. }
  365. $this->storages[] = $storage;
  366. return $storage;
  367. }
  368. /**
  369. * @medium
  370. */
  371. function testViewHooksIfRootStartsTheSame() {
  372. $storage1 = $this->getTestStorage();
  373. $storage2 = $this->getTestStorage();
  374. $defaultRoot = \OC\Files\Filesystem::getRoot();
  375. \OC\Files\Filesystem::mount($storage1, array(), '/');
  376. \OC\Files\Filesystem::mount($storage2, array(), $defaultRoot . '_substorage');
  377. \OC_Hook::connect('OC_Filesystem', 'post_write', $this, 'dummyHook');
  378. $subView = new \OC\Files\View($defaultRoot . '_substorage');
  379. $this->hookPath = null;
  380. $subView->file_put_contents('/foo.txt', 'asd');
  381. $this->assertNull($this->hookPath);
  382. }
  383. private $hookWritePath;
  384. private $hookCreatePath;
  385. private $hookUpdatePath;
  386. public function dummyHookWrite($params) {
  387. $this->hookWritePath = $params['path'];
  388. }
  389. public function dummyHookUpdate($params) {
  390. $this->hookUpdatePath = $params['path'];
  391. }
  392. public function dummyHookCreate($params) {
  393. $this->hookCreatePath = $params['path'];
  394. }
  395. public function testEditNoCreateHook() {
  396. $storage1 = $this->getTestStorage();
  397. $storage2 = $this->getTestStorage();
  398. $defaultRoot = \OC\Files\Filesystem::getRoot();
  399. \OC\Files\Filesystem::mount($storage1, array(), '/');
  400. \OC\Files\Filesystem::mount($storage2, array(), $defaultRoot);
  401. \OC_Hook::connect('OC_Filesystem', 'post_create', $this, 'dummyHookCreate');
  402. \OC_Hook::connect('OC_Filesystem', 'post_update', $this, 'dummyHookUpdate');
  403. \OC_Hook::connect('OC_Filesystem', 'post_write', $this, 'dummyHookWrite');
  404. $view = new \OC\Files\View($defaultRoot);
  405. $this->hookWritePath = $this->hookUpdatePath = $this->hookCreatePath = null;
  406. $view->file_put_contents('/asd.txt', 'foo');
  407. $this->assertEquals('/asd.txt', $this->hookCreatePath);
  408. $this->assertNull($this->hookUpdatePath);
  409. $this->assertEquals('/asd.txt', $this->hookWritePath);
  410. $this->hookWritePath = $this->hookUpdatePath = $this->hookCreatePath = null;
  411. $view->file_put_contents('/asd.txt', 'foo');
  412. $this->assertNull($this->hookCreatePath);
  413. $this->assertEquals('/asd.txt', $this->hookUpdatePath);
  414. $this->assertEquals('/asd.txt', $this->hookWritePath);
  415. \OC_Hook::clear('OC_Filesystem', 'post_create');
  416. \OC_Hook::clear('OC_Filesystem', 'post_update');
  417. \OC_Hook::clear('OC_Filesystem', 'post_write');
  418. }
  419. /**
  420. * @dataProvider resolvePathTestProvider
  421. */
  422. public function testResolvePath($expected, $pathToTest) {
  423. $storage1 = $this->getTestStorage();
  424. \OC\Files\Filesystem::mount($storage1, array(), '/');
  425. $view = new \OC\Files\View('');
  426. $result = $view->resolvePath($pathToTest);
  427. $this->assertEquals($expected, $result[1]);
  428. $exists = $view->file_exists($pathToTest);
  429. $this->assertTrue($exists);
  430. $exists = $view->file_exists($result[1]);
  431. $this->assertTrue($exists);
  432. }
  433. function resolvePathTestProvider() {
  434. return array(
  435. array('foo.txt', 'foo.txt'),
  436. array('foo.txt', '/foo.txt'),
  437. array('folder', 'folder'),
  438. array('folder', '/folder'),
  439. array('folder', 'folder/'),
  440. array('folder', '/folder/'),
  441. array('folder/bar.txt', 'folder/bar.txt'),
  442. array('folder/bar.txt', '/folder/bar.txt'),
  443. array('', ''),
  444. array('', '/'),
  445. );
  446. }
  447. public function testUTF8Names() {
  448. $names = array('虚', '和知しゃ和で', 'regular ascii', 'sɨˈrɪlɪk', 'ѨѬ', 'أنا أحب القراءة كثيرا');
  449. $storage = new \OC\Files\Storage\Temporary(array());
  450. \OC\Files\Filesystem::mount($storage, array(), '/');
  451. $rootView = new \OC\Files\View('');
  452. foreach ($names as $name) {
  453. $rootView->file_put_contents('/' . $name, 'dummy content');
  454. }
  455. $list = $rootView->getDirectoryContent('/');
  456. $this->assertCount(count($names), $list);
  457. foreach ($list as $item) {
  458. $this->assertContains($item['name'], $names);
  459. }
  460. $cache = $storage->getCache();
  461. $scanner = $storage->getScanner();
  462. $scanner->scan('');
  463. $list = $cache->getFolderContents('');
  464. $this->assertCount(count($names), $list);
  465. foreach ($list as $item) {
  466. $this->assertContains($item['name'], $names);
  467. }
  468. }
  469. public function testLongPath() {
  470. $storage = new \OC\Files\Storage\Temporary(array());
  471. \OC\Files\Filesystem::mount($storage, array(), '/');
  472. $rootView = new \OC\Files\View('');
  473. $longPath = '';
  474. // 4000 is the maximum path length in file_cache.path
  475. $folderName = 'abcdefghijklmnopqrstuvwxyz012345678901234567890123456789';
  476. $depth = (4000/57);
  477. foreach (range(0, $depth-1) as $i) {
  478. $longPath .= '/'.$folderName;
  479. $result = $rootView->mkdir($longPath);
  480. $this->assertTrue($result, "mkdir failed on $i - path length: " . strlen($longPath));
  481. $result = $rootView->file_put_contents($longPath . '/test.txt', 'lorem');
  482. $this->assertEquals(5, $result, "file_put_contents failed on $i");
  483. $this->assertTrue($rootView->file_exists($longPath));
  484. $this->assertTrue($rootView->file_exists($longPath . '/test.txt'));
  485. }
  486. $cache = $storage->getCache();
  487. $scanner = $storage->getScanner();
  488. $scanner->scan('');
  489. $longPath = $folderName;
  490. foreach (range(0, $depth-1) as $i) {
  491. $cachedFolder = $cache->get($longPath);
  492. $this->assertTrue(is_array($cachedFolder), "No cache entry for folder at $i");
  493. $this->assertEquals($folderName, $cachedFolder['name'], "Wrong cache entry for folder at $i");
  494. $cachedFile = $cache->get($longPath . '/test.txt');
  495. $this->assertTrue(is_array($cachedFile), "No cache entry for file at $i");
  496. $this->assertEquals('test.txt', $cachedFile['name'], "Wrong cache entry for file at $i");
  497. $longPath .= '/' . $folderName;
  498. }
  499. }
  500. public function testTouchNotSupported() {
  501. $storage = new TemporaryNoTouch(array());
  502. $scanner = $storage->getScanner();
  503. \OC\Files\Filesystem::mount($storage, array(), '/test/');
  504. $past = time() - 100;
  505. $storage->file_put_contents('test', 'foobar');
  506. $scanner->scan('');
  507. $view = new \OC\Files\View('');
  508. $info = $view->getFileInfo('/test/test');
  509. $view->touch('/test/test', $past);
  510. $scanner->scanFile('test', \OC\Files\Cache\Scanner::REUSE_ETAG);
  511. $info2 = $view->getFileInfo('/test/test');
  512. $this->assertSame($info['etag'], $info2['etag']);
  513. }
  514. /**
  515. * @dataProvider absolutePathProvider
  516. */
  517. public function testGetAbsolutePath($expectedPath, $relativePath) {
  518. $view = new \OC\Files\View('/files');
  519. $this->assertEquals($expectedPath, $view->getAbsolutePath($relativePath));
  520. }
  521. function absolutePathProvider() {
  522. return array(
  523. array('/files/', ''),
  524. array('/files/0', '0'),
  525. array('/files/false', 'false'),
  526. array('/files/true', 'true'),
  527. array('/files/', '/'),
  528. array('/files/test', 'test'),
  529. array('/files/test', '/test'),
  530. );
  531. }
  532. /**
  533. * @dataProvider tooLongPathDataProvider
  534. * @expectedException \OCP\Files\InvalidPathException
  535. */
  536. public function testTooLongPath($operation, $param0 = NULL) {
  537. $longPath = '';
  538. // 4000 is the maximum path length in file_cache.path
  539. $folderName = 'abcdefghijklmnopqrstuvwxyz012345678901234567890123456789';
  540. $depth = (4000/57);
  541. foreach (range(0, $depth+1) as $i) {
  542. $longPath .= '/'.$folderName;
  543. }
  544. $storage = new \OC\Files\Storage\Temporary(array());
  545. \OC\Files\Filesystem::mount($storage, array(), '/');
  546. $rootView = new \OC\Files\View('');
  547. if ($param0 === '@0') {
  548. $param0 = $longPath;
  549. }
  550. if ($operation === 'hash') {
  551. $param0 = $longPath;
  552. $longPath = 'md5';
  553. }
  554. call_user_func(array($rootView, $operation), $longPath, $param0);
  555. }
  556. public function tooLongPathDataProvider() {
  557. return array(
  558. array('getAbsolutePath'),
  559. array('getRelativePath'),
  560. array('getMountPoint'),
  561. array('resolvePath'),
  562. array('getLocalFile'),
  563. array('getLocalFolder'),
  564. array('mkdir'),
  565. array('rmdir'),
  566. array('opendir'),
  567. array('is_dir'),
  568. array('is_file'),
  569. array('stat'),
  570. array('filetype'),
  571. array('filesize'),
  572. array('readfile'),
  573. array('isCreatable'),
  574. array('isReadable'),
  575. array('isUpdatable'),
  576. array('isDeletable'),
  577. array('isSharable'),
  578. array('file_exists'),
  579. array('filemtime'),
  580. array('touch'),
  581. array('file_get_contents'),
  582. array('unlink'),
  583. array('deleteAll'),
  584. array('toTmpFile'),
  585. array('getMimeType'),
  586. array('free_space'),
  587. array('getFileInfo'),
  588. array('getDirectoryContent'),
  589. array('getOwner'),
  590. array('getETag'),
  591. array('file_put_contents', 'ipsum'),
  592. array('rename', '@0'),
  593. array('copy', '@0'),
  594. array('fopen', 'r'),
  595. array('fromTmpFile', '@0'),
  596. array('hash'),
  597. array('hasUpdated', 0),
  598. array('putFileInfo', array()),
  599. );
  600. }
  601. }