Storage.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Robin Appelman
  6. * @copyright 2012 Robin Appelman icewind@owncloud.com
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace Test\Files\Storage;
  23. use OC\Files\Cache\Watcher;
  24. use OCP\Files\Storage\IWriteStreamStorage;
  25. abstract class Storage extends \Test\TestCase {
  26. /**
  27. * @var \OC\Files\Storage\Storage instance
  28. */
  29. protected $instance;
  30. protected $waitDelay = 0;
  31. /**
  32. * Sleep for the number of seconds specified in the
  33. * $waitDelay attribute
  34. */
  35. protected function wait() {
  36. if ($this->waitDelay > 0) {
  37. sleep($this->waitDelay);
  38. }
  39. }
  40. /**
  41. * the root folder of the storage should always exist, be readable and be recognized as a directory
  42. */
  43. public function testRoot() {
  44. $this->assertTrue($this->instance->file_exists('/'), 'Root folder does not exist');
  45. $this->assertTrue($this->instance->isReadable('/'), 'Root folder is not readable');
  46. $this->assertTrue($this->instance->is_dir('/'), 'Root folder is not a directory');
  47. $this->assertFalse($this->instance->is_file('/'), 'Root folder is a file');
  48. $this->assertEquals('dir', $this->instance->filetype('/'));
  49. //without this, any further testing would be useless, not an actual requirement for filestorage though
  50. $this->assertTrue($this->instance->isUpdatable('/'), 'Root folder is not writable');
  51. }
  52. /**
  53. * Check that the test() function works
  54. */
  55. public function testTestFunction() {
  56. $this->assertTrue($this->instance->test());
  57. }
  58. /**
  59. * @dataProvider directoryProvider
  60. */
  61. public function testDirectories($directory) {
  62. $this->assertFalse($this->instance->file_exists('/' . $directory));
  63. $this->assertTrue($this->instance->mkdir('/' . $directory));
  64. $this->assertTrue($this->instance->file_exists('/' . $directory));
  65. $this->assertTrue($this->instance->is_dir('/' . $directory));
  66. $this->assertFalse($this->instance->is_file('/' . $directory));
  67. $this->assertEquals('dir', $this->instance->filetype('/' . $directory));
  68. $this->assertEquals(0, $this->instance->filesize('/' . $directory));
  69. $this->assertTrue($this->instance->isReadable('/' . $directory));
  70. $this->assertTrue($this->instance->isUpdatable('/' . $directory));
  71. $dh = $this->instance->opendir('/');
  72. $content = [];
  73. while (($file = readdir($dh)) !== false) {
  74. if ($file != '.' and $file != '..') {
  75. $content[] = $file;
  76. }
  77. }
  78. $this->assertEquals([$directory], $content);
  79. $content = iterator_to_array($this->instance->getDirectoryContent('/'));
  80. $this->assertCount(1, $content);
  81. $dirEntry = $content[0];
  82. unset($dirEntry['scan_permissions']);
  83. unset($dirEntry['etag']);
  84. $this->assertEquals([
  85. 'name' => $directory,
  86. 'mimetype' => $this->instance->getMimeType($directory),
  87. 'mtime' => $this->instance->filemtime($directory),
  88. 'size' => -1,
  89. 'storage_mtime' => $this->instance->filemtime($directory),
  90. 'permissions' => $this->instance->getPermissions($directory),
  91. ], $dirEntry);
  92. $this->assertFalse($this->instance->mkdir('/' . $directory)); //can't create existing folders
  93. $this->assertTrue($this->instance->rmdir('/' . $directory));
  94. $this->wait();
  95. $this->assertFalse($this->instance->file_exists('/' . $directory));
  96. $this->assertFalse($this->instance->rmdir('/' . $directory)); //can't remove non existing folders
  97. $dh = $this->instance->opendir('/');
  98. $content = [];
  99. while (($file = readdir($dh)) !== false) {
  100. if ($file != '.' and $file != '..') {
  101. $content[] = $file;
  102. }
  103. }
  104. $this->assertEquals([], $content);
  105. }
  106. public function fileNameProvider() {
  107. return [
  108. ['file.txt'],
  109. [' file.txt'],
  110. ['folder .txt'],
  111. ['file with space.txt'],
  112. ['spéciäl fäile'],
  113. ['test single\'quote.txt'],
  114. ];
  115. }
  116. public function directoryProvider() {
  117. return [
  118. ['folder'],
  119. [' folder'],
  120. ['folder '],
  121. ['folder with space'],
  122. ['spéciäl földer'],
  123. ['test single\'quote'],
  124. ];
  125. }
  126. public function loremFileProvider() {
  127. $root = \OC::$SERVERROOT . '/tests/data/';
  128. return [
  129. // small file
  130. [$root . 'lorem.txt'],
  131. // bigger file (> 8 KB which is the standard PHP block size)
  132. [$root . 'lorem-big.txt']
  133. ];
  134. }
  135. /**
  136. * test the various uses of file_get_contents and file_put_contents
  137. *
  138. * @dataProvider loremFileProvider
  139. */
  140. public function testGetPutContents($sourceFile) {
  141. $sourceText = file_get_contents($sourceFile);
  142. //fill a file with string data
  143. $this->instance->file_put_contents('/lorem.txt', $sourceText);
  144. $this->assertFalse($this->instance->is_dir('/lorem.txt'));
  145. $this->assertEquals($sourceText, $this->instance->file_get_contents('/lorem.txt'), 'data returned from file_get_contents is not equal to the source data');
  146. //empty the file
  147. $this->instance->file_put_contents('/lorem.txt', '');
  148. $this->assertEquals('', $this->instance->file_get_contents('/lorem.txt'), 'file not emptied');
  149. }
  150. /**
  151. * test various known mimetypes
  152. */
  153. public function testMimeType() {
  154. $this->assertEquals('httpd/unix-directory', $this->instance->getMimeType('/'));
  155. $this->assertEquals(false, $this->instance->getMimeType('/non/existing/file'));
  156. $textFile = \OC::$SERVERROOT . '/tests/data/lorem.txt';
  157. $this->instance->file_put_contents('/lorem.txt', file_get_contents($textFile, 'r'));
  158. $this->assertEquals('text/plain', $this->instance->getMimeType('/lorem.txt'));
  159. $pngFile = \OC::$SERVERROOT . '/tests/data/desktopapp.png';
  160. $this->instance->file_put_contents('/desktopapp.png', file_get_contents($pngFile, 'r'));
  161. $this->assertEquals('image/png', $this->instance->getMimeType('/desktopapp.png'));
  162. $svgFile = \OC::$SERVERROOT . '/tests/data/desktopapp.svg';
  163. $this->instance->file_put_contents('/desktopapp.svg', file_get_contents($svgFile, 'r'));
  164. $this->assertEquals('image/svg+xml', $this->instance->getMimeType('/desktopapp.svg'));
  165. }
  166. public function copyAndMoveProvider() {
  167. return [
  168. ['/source.txt', '/target.txt'],
  169. ['/source.txt', '/target with space.txt'],
  170. ['/source with space.txt', '/target.txt'],
  171. ['/source with space.txt', '/target with space.txt'],
  172. ['/source.txt', '/tärgét.txt'],
  173. ['/sòurcē.txt', '/target.txt'],
  174. ['/sòurcē.txt', '/tärgét.txt'],
  175. ['/single \' quote.txt', '/tar\'get.txt'],
  176. ];
  177. }
  178. public function initSourceAndTarget($source, $target = null) {
  179. $textFile = \OC::$SERVERROOT . '/tests/data/lorem.txt';
  180. $this->instance->file_put_contents($source, file_get_contents($textFile));
  181. if ($target) {
  182. $testContents = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  183. $this->instance->file_put_contents($target, $testContents);
  184. }
  185. }
  186. public function assertSameAsLorem($file) {
  187. $textFile = \OC::$SERVERROOT . '/tests/data/lorem.txt';
  188. $this->assertEquals(
  189. file_get_contents($textFile),
  190. $this->instance->file_get_contents($file),
  191. 'Expected ' . $file . ' to be a copy of ' . $textFile
  192. );
  193. }
  194. /**
  195. * @dataProvider copyAndMoveProvider
  196. */
  197. public function testCopy($source, $target) {
  198. $this->initSourceAndTarget($source);
  199. $this->instance->copy($source, $target);
  200. $this->assertTrue($this->instance->file_exists($target), $target . ' was not created');
  201. $this->assertSameAsLorem($target);
  202. $this->assertTrue($this->instance->file_exists($source), $source . ' was deleted');
  203. }
  204. /**
  205. * @dataProvider copyAndMoveProvider
  206. */
  207. public function testMove($source, $target) {
  208. $this->initSourceAndTarget($source);
  209. $this->instance->rename($source, $target);
  210. $this->wait();
  211. $this->assertTrue($this->instance->file_exists($target), $target . ' was not created');
  212. $this->assertFalse($this->instance->file_exists($source), $source . ' still exists');
  213. $this->assertSameAsLorem($target);
  214. }
  215. /**
  216. * @dataProvider copyAndMoveProvider
  217. */
  218. public function testCopyOverwrite($source, $target) {
  219. $this->initSourceAndTarget($source, $target);
  220. $this->instance->copy($source, $target);
  221. $this->assertTrue($this->instance->file_exists($target), $target . ' was not created');
  222. $this->assertTrue($this->instance->file_exists($source), $source . ' was deleted');
  223. $this->assertSameAsLorem($target);
  224. $this->assertSameAsLorem($source);
  225. }
  226. /**
  227. * @dataProvider copyAndMoveProvider
  228. */
  229. public function testMoveOverwrite($source, $target) {
  230. $this->initSourceAndTarget($source, $target);
  231. $this->instance->rename($source, $target);
  232. $this->assertTrue($this->instance->file_exists($target), $target . ' was not created');
  233. $this->assertFalse($this->instance->file_exists($source), $source . ' still exists');
  234. $this->assertSameAsLorem($target);
  235. }
  236. public function testLocal() {
  237. $textFile = \OC::$SERVERROOT . '/tests/data/lorem.txt';
  238. $this->instance->file_put_contents('/lorem.txt', file_get_contents($textFile));
  239. $localFile = $this->instance->getLocalFile('/lorem.txt');
  240. $this->assertTrue(file_exists($localFile));
  241. $this->assertEquals(file_get_contents($textFile), file_get_contents($localFile));
  242. $this->instance->mkdir('/folder');
  243. $this->instance->file_put_contents('/folder/lorem.txt', file_get_contents($textFile));
  244. $this->instance->file_put_contents('/folder/bar.txt', 'asd');
  245. $this->instance->mkdir('/folder/recursive');
  246. $this->instance->file_put_contents('/folder/recursive/file.txt', 'foo');
  247. // test below require to use instance->getLocalFile because the physical storage might be different
  248. $localFile = $this->instance->getLocalFile('/folder/lorem.txt');
  249. $this->assertTrue(file_exists($localFile));
  250. $this->assertEquals(file_get_contents($localFile), file_get_contents($textFile));
  251. $localFile = $this->instance->getLocalFile('/folder/bar.txt');
  252. $this->assertTrue(file_exists($localFile));
  253. $this->assertEquals(file_get_contents($localFile), 'asd');
  254. $localFile = $this->instance->getLocalFile('/folder/recursive/file.txt');
  255. $this->assertTrue(file_exists($localFile));
  256. $this->assertEquals(file_get_contents($localFile), 'foo');
  257. }
  258. public function testStat() {
  259. $textFile = \OC::$SERVERROOT . '/tests/data/lorem.txt';
  260. $ctimeStart = time();
  261. $this->instance->file_put_contents('/lorem.txt', file_get_contents($textFile));
  262. $this->assertTrue($this->instance->isReadable('/lorem.txt'));
  263. $ctimeEnd = time();
  264. $mTime = $this->instance->filemtime('/lorem.txt');
  265. $this->assertTrue($this->instance->hasUpdated('/lorem.txt', $ctimeStart - 5));
  266. $this->assertTrue($this->instance->hasUpdated('/', $ctimeStart - 5));
  267. // check that ($ctimeStart - 5) <= $mTime <= ($ctimeEnd + 1)
  268. $this->assertGreaterThanOrEqual(($ctimeStart - 5), $mTime);
  269. $this->assertLessThanOrEqual(($ctimeEnd + 1), $mTime);
  270. $this->assertEquals(filesize($textFile), $this->instance->filesize('/lorem.txt'));
  271. $stat = $this->instance->stat('/lorem.txt');
  272. //only size and mtime are required in the result
  273. $this->assertEquals($stat['size'], $this->instance->filesize('/lorem.txt'));
  274. $this->assertEquals($stat['mtime'], $mTime);
  275. if ($this->instance->touch('/lorem.txt', 100) !== false) {
  276. $mTime = $this->instance->filemtime('/lorem.txt');
  277. $this->assertEquals($mTime, 100);
  278. }
  279. $mtimeStart = time();
  280. $this->instance->unlink('/lorem.txt');
  281. $this->assertTrue($this->instance->hasUpdated('/', $mtimeStart - 5));
  282. }
  283. /**
  284. * Test whether checkUpdate properly returns false when there was
  285. * no change.
  286. */
  287. public function testCheckUpdate() {
  288. if ($this->instance instanceof \OC\Files\Storage\Wrapper\Wrapper) {
  289. $this->markTestSkipped('Cannot test update check on wrappers');
  290. }
  291. $textFile = \OC::$SERVERROOT . '/tests/data/lorem.txt';
  292. $watcher = $this->instance->getWatcher();
  293. $watcher->setPolicy(Watcher::CHECK_ALWAYS);
  294. $this->instance->file_put_contents('/lorem.txt', file_get_contents($textFile));
  295. $this->assertTrue($watcher->checkUpdate('/lorem.txt'), 'Update detected');
  296. $this->assertFalse($watcher->checkUpdate('/lorem.txt'), 'No update');
  297. }
  298. public function testUnlink() {
  299. $textFile = \OC::$SERVERROOT . '/tests/data/lorem.txt';
  300. $this->instance->file_put_contents('/lorem.txt', file_get_contents($textFile));
  301. $this->assertTrue($this->instance->file_exists('/lorem.txt'));
  302. $this->assertTrue($this->instance->unlink('/lorem.txt'));
  303. $this->wait();
  304. $this->assertFalse($this->instance->file_exists('/lorem.txt'));
  305. }
  306. /**
  307. * @dataProvider fileNameProvider
  308. */
  309. public function testFOpen($fileName) {
  310. $textFile = \OC::$SERVERROOT . '/tests/data/lorem.txt';
  311. $fh = @$this->instance->fopen($fileName, 'r');
  312. if ($fh) {
  313. fclose($fh);
  314. }
  315. $this->assertFalse($fh);
  316. $this->assertFalse($this->instance->file_exists($fileName));
  317. $fh = $this->instance->fopen($fileName, 'w');
  318. fwrite($fh, file_get_contents($textFile));
  319. fclose($fh);
  320. $this->assertTrue($this->instance->file_exists($fileName));
  321. $fh = $this->instance->fopen($fileName, 'r');
  322. $content = stream_get_contents($fh);
  323. $this->assertEquals(file_get_contents($textFile), $content);
  324. }
  325. public function testTouchCreateFile() {
  326. $this->assertFalse($this->instance->file_exists('touch'));
  327. // returns true on success
  328. $this->assertTrue($this->instance->touch('touch'));
  329. $this->assertTrue($this->instance->file_exists('touch'));
  330. }
  331. public function testRecursiveRmdir() {
  332. $this->instance->mkdir('folder');
  333. $this->instance->mkdir('folder/bar');
  334. $this->wait();
  335. $this->instance->file_put_contents('folder/asd.txt', 'foobar');
  336. $this->instance->file_put_contents('folder/bar/foo.txt', 'asd');
  337. $this->assertTrue($this->instance->rmdir('folder'));
  338. $this->wait();
  339. $this->assertFalse($this->instance->file_exists('folder/asd.txt'));
  340. $this->assertFalse($this->instance->file_exists('folder/bar/foo.txt'));
  341. $this->assertFalse($this->instance->file_exists('folder/bar'));
  342. $this->assertFalse($this->instance->file_exists('folder'));
  343. }
  344. public function testRmdirEmptyFolder() {
  345. $this->assertTrue($this->instance->mkdir('empty'));
  346. $this->wait();
  347. $this->assertTrue($this->instance->rmdir('empty'));
  348. $this->assertFalse($this->instance->file_exists('empty'));
  349. }
  350. public function testRecursiveUnlink() {
  351. $this->instance->mkdir('folder');
  352. $this->instance->mkdir('folder/bar');
  353. $this->instance->file_put_contents('folder/asd.txt', 'foobar');
  354. $this->instance->file_put_contents('folder/bar/foo.txt', 'asd');
  355. $this->assertTrue($this->instance->unlink('folder'));
  356. $this->wait();
  357. $this->assertFalse($this->instance->file_exists('folder/asd.txt'));
  358. $this->assertFalse($this->instance->file_exists('folder/bar/foo.txt'));
  359. $this->assertFalse($this->instance->file_exists('folder/bar'));
  360. $this->assertFalse($this->instance->file_exists('folder'));
  361. }
  362. public function hashProvider() {
  363. return [
  364. ['Foobar', 'md5'],
  365. ['Foobar', 'sha1'],
  366. ['Foobar', 'sha256'],
  367. ];
  368. }
  369. /**
  370. * @dataProvider hashProvider
  371. */
  372. public function testHash($data, $type) {
  373. $this->instance->file_put_contents('hash.txt', $data);
  374. $this->assertEquals(hash($type, $data), $this->instance->hash($type, 'hash.txt'));
  375. $this->assertEquals(hash($type, $data, true), $this->instance->hash($type, 'hash.txt', true));
  376. }
  377. public function testHashInFileName() {
  378. $this->instance->file_put_contents('#test.txt', 'data');
  379. $this->assertEquals('data', $this->instance->file_get_contents('#test.txt'));
  380. $this->instance->mkdir('#foo');
  381. $this->instance->file_put_contents('#foo/test.txt', 'data');
  382. $this->assertEquals('data', $this->instance->file_get_contents('#foo/test.txt'));
  383. $dh = $this->instance->opendir('#foo');
  384. $content = [];
  385. while ($file = readdir($dh)) {
  386. if ($file != '.' and $file != '..') {
  387. $content[] = $file;
  388. }
  389. }
  390. $this->assertEquals(['test.txt'], $content);
  391. }
  392. public function testCopyOverWriteFile() {
  393. $this->instance->file_put_contents('target.txt', 'foo');
  394. $this->instance->file_put_contents('source.txt', 'bar');
  395. $this->instance->copy('source.txt', 'target.txt');
  396. $this->assertEquals('bar', $this->instance->file_get_contents('target.txt'));
  397. }
  398. public function testRenameOverWriteFile() {
  399. $this->instance->file_put_contents('target.txt', 'foo');
  400. $this->instance->file_put_contents('source.txt', 'bar');
  401. $this->instance->rename('source.txt', 'target.txt');
  402. $this->assertEquals('bar', $this->instance->file_get_contents('target.txt'));
  403. $this->assertFalse($this->instance->file_exists('source.txt'));
  404. }
  405. public function testRenameDirectory() {
  406. $this->instance->mkdir('source');
  407. $this->instance->file_put_contents('source/test1.txt', 'foo');
  408. $this->instance->file_put_contents('source/test2.txt', 'qwerty');
  409. $this->instance->mkdir('source/subfolder');
  410. $this->instance->file_put_contents('source/subfolder/test.txt', 'bar');
  411. $this->instance->rename('source', 'target');
  412. $this->assertFalse($this->instance->file_exists('source'));
  413. $this->assertFalse($this->instance->file_exists('source/test1.txt'));
  414. $this->assertFalse($this->instance->file_exists('source/test2.txt'));
  415. $this->assertFalse($this->instance->file_exists('source/subfolder'));
  416. $this->assertFalse($this->instance->file_exists('source/subfolder/test.txt'));
  417. $this->assertTrue($this->instance->file_exists('target'));
  418. $this->assertTrue($this->instance->file_exists('target/test1.txt'));
  419. $this->assertTrue($this->instance->file_exists('target/test2.txt'));
  420. $this->assertTrue($this->instance->file_exists('target/subfolder'));
  421. $this->assertTrue($this->instance->file_exists('target/subfolder/test.txt'));
  422. $contents = iterator_to_array($this->instance->getDirectoryContent(''));
  423. $this->assertCount(1, $contents);
  424. $this->assertEquals('foo', $this->instance->file_get_contents('target/test1.txt'));
  425. $this->assertEquals('qwerty', $this->instance->file_get_contents('target/test2.txt'));
  426. $this->assertEquals('bar', $this->instance->file_get_contents('target/subfolder/test.txt'));
  427. }
  428. public function testRenameOverWriteDirectory() {
  429. $this->instance->mkdir('source');
  430. $this->instance->file_put_contents('source/test1.txt', 'foo');
  431. $this->instance->mkdir('target');
  432. $this->instance->file_put_contents('target/test1.txt', 'bar');
  433. $this->instance->file_put_contents('target/test2.txt', 'bar');
  434. $this->assertTrue($this->instance->rename('source', 'target'), 'rename must return true on success');
  435. $this->assertFalse($this->instance->file_exists('source'), 'source has not been removed');
  436. $this->assertFalse($this->instance->file_exists('source/test1.txt'), 'source/test1.txt has not been removed');
  437. $this->assertFalse($this->instance->file_exists('target/test2.txt'), 'target/test2.txt has not been removed');
  438. $this->assertEquals('foo', $this->instance->file_get_contents('target/test1.txt'), 'target/test1.txt has not been overwritten');
  439. }
  440. public function testRenameOverWriteDirectoryOverFile() {
  441. $this->instance->mkdir('source');
  442. $this->instance->file_put_contents('source/test1.txt', 'foo');
  443. $this->instance->file_put_contents('target', 'bar');
  444. $this->assertTrue($this->instance->rename('source', 'target'), 'rename must return true on success');
  445. $this->assertFalse($this->instance->file_exists('source'));
  446. $this->assertFalse($this->instance->file_exists('source/test1.txt'));
  447. $this->assertEquals('foo', $this->instance->file_get_contents('target/test1.txt'));
  448. }
  449. public function testCopyDirectory() {
  450. $this->instance->mkdir('source');
  451. $this->instance->file_put_contents('source/test1.txt', 'foo');
  452. $this->instance->file_put_contents('source/test2.txt', 'qwerty');
  453. $this->instance->mkdir('source/subfolder');
  454. $this->instance->file_put_contents('source/subfolder/test.txt', 'bar');
  455. $this->instance->copy('source', 'target');
  456. $this->assertTrue($this->instance->file_exists('source'));
  457. $this->assertTrue($this->instance->file_exists('source/test1.txt'));
  458. $this->assertTrue($this->instance->file_exists('source/test2.txt'));
  459. $this->assertTrue($this->instance->file_exists('source/subfolder'));
  460. $this->assertTrue($this->instance->file_exists('source/subfolder/test.txt'));
  461. $this->assertTrue($this->instance->file_exists('target'));
  462. $this->assertTrue($this->instance->file_exists('target/test1.txt'));
  463. $this->assertTrue($this->instance->file_exists('target/test2.txt'));
  464. $this->assertTrue($this->instance->file_exists('target/subfolder'));
  465. $this->assertTrue($this->instance->file_exists('target/subfolder/test.txt'));
  466. $this->assertEquals('foo', $this->instance->file_get_contents('target/test1.txt'));
  467. $this->assertEquals('qwerty', $this->instance->file_get_contents('target/test2.txt'));
  468. $this->assertEquals('bar', $this->instance->file_get_contents('target/subfolder/test.txt'));
  469. }
  470. public function testCopyOverWriteDirectory() {
  471. $this->instance->mkdir('source');
  472. $this->instance->file_put_contents('source/test1.txt', 'foo');
  473. $this->instance->mkdir('target');
  474. $this->instance->file_put_contents('target/test1.txt', 'bar');
  475. $this->instance->file_put_contents('target/test2.txt', 'bar');
  476. $this->instance->copy('source', 'target');
  477. $this->assertFalse($this->instance->file_exists('target/test2.txt'));
  478. $this->assertEquals('foo', $this->instance->file_get_contents('target/test1.txt'));
  479. }
  480. public function testCopyOverWriteDirectoryOverFile() {
  481. $this->instance->mkdir('source');
  482. $this->instance->file_put_contents('source/test1.txt', 'foo');
  483. $this->instance->file_put_contents('target', 'bar');
  484. $this->instance->copy('source', 'target');
  485. $this->assertEquals('foo', $this->instance->file_get_contents('target/test1.txt'));
  486. }
  487. public function testInstanceOfStorage() {
  488. $this->assertTrue($this->instance->instanceOfStorage('\OCP\Files\Storage'));
  489. $this->assertTrue($this->instance->instanceOfStorage(get_class($this->instance)));
  490. $this->assertFalse($this->instance->instanceOfStorage('\OC'));
  491. }
  492. /**
  493. * @dataProvider copyAndMoveProvider
  494. */
  495. public function testCopyFromSameStorage($source, $target) {
  496. $this->initSourceAndTarget($source);
  497. $this->instance->copyFromStorage($this->instance, $source, $target);
  498. $this->assertTrue($this->instance->file_exists($target), $target . ' was not created');
  499. $this->assertSameAsLorem($target);
  500. $this->assertTrue($this->instance->file_exists($source), $source . ' was deleted');
  501. }
  502. public function testIsCreatable() {
  503. $this->instance->mkdir('source');
  504. $this->assertTrue($this->instance->isCreatable('source'));
  505. }
  506. public function testIsReadable() {
  507. $this->instance->mkdir('source');
  508. $this->assertTrue($this->instance->isReadable('source'));
  509. }
  510. public function testIsUpdatable() {
  511. $this->instance->mkdir('source');
  512. $this->assertTrue($this->instance->isUpdatable('source'));
  513. }
  514. public function testIsDeletable() {
  515. $this->instance->mkdir('source');
  516. $this->assertTrue($this->instance->isDeletable('source'));
  517. }
  518. public function testIsShareable() {
  519. $this->instance->mkdir('source');
  520. $this->assertTrue($this->instance->isSharable('source'));
  521. }
  522. public function testStatAfterWrite() {
  523. $this->instance->file_put_contents('foo.txt', 'bar');
  524. $stat = $this->instance->stat('foo.txt');
  525. $this->assertEquals(3, $stat['size']);
  526. $fh = $this->instance->fopen('foo.txt', 'w');
  527. fwrite($fh, 'qwerty');
  528. fclose($fh);
  529. $stat = $this->instance->stat('foo.txt');
  530. $this->assertEquals(6, $stat['size']);
  531. }
  532. public function testPartFile() {
  533. $this->instance->file_put_contents('bar.txt.part', 'bar');
  534. $this->instance->rename('bar.txt.part', 'bar.txt');
  535. $this->assertEquals('bar', $this->instance->file_get_contents('bar.txt'));
  536. }
  537. public function testWriteStream() {
  538. $textFile = \OC::$SERVERROOT . '/tests/data/lorem.txt';
  539. if (!$this->instance->instanceOfStorage(IWriteStreamStorage::class)) {
  540. $this->markTestSkipped('Not a WriteSteamStorage');
  541. }
  542. /** @var IWriteStreamStorage $storage */
  543. $storage = $this->instance;
  544. $source = fopen($textFile, 'r');
  545. $storage->writeStream('test.txt', $source);
  546. $this->assertTrue($storage->file_exists('test.txt'));
  547. $this->assertStringEqualsFile($textFile, $storage->file_get_contents('test.txt'));
  548. $this->assertEquals('resource (closed)', gettype($source));
  549. }
  550. public function testFseekSize() {
  551. $textFile = \OC::$SERVERROOT . '/tests/data/lorem.txt';
  552. $this->instance->file_put_contents('bar.txt', file_get_contents($textFile));
  553. $size = $this->instance->filesize('bar.txt');
  554. $this->assertEquals(filesize($textFile), $size);
  555. $fh = $this->instance->fopen('bar.txt', 'r');
  556. fseek($fh, 0, SEEK_END);
  557. $pos = ftell($fh);
  558. $this->assertEquals($size, $pos);
  559. }
  560. }