Storage.php 24 KB

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