Storage.php 24 KB

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