Storage.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  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 = array();
  73. while ($file = readdir($dh)) {
  74. if ($file != '.' and $file != '..') {
  75. $content[] = $file;
  76. }
  77. }
  78. $this->assertEquals(array($directory), $content);
  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 = array();
  86. while ($file = readdir($dh)) {
  87. if ($file != '.' and $file != '..') {
  88. $content[] = $file;
  89. }
  90. }
  91. $this->assertEquals(array(), $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. function loremFileProvider() {
  114. $root = \OC::$SERVERROOT . '/tests/data/';
  115. return array(
  116. // small file
  117. array($root . 'lorem.txt'),
  118. // bigger file (> 8 KB which is the standard PHP block size)
  119. array($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) {
  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() {
  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) {
  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) {
  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) {
  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) {
  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() {
  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() {
  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() {
  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() {
  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) {
  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() {
  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() {
  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() {
  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() {
  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 array(
  351. array('Foobar', 'md5'),
  352. array('Foobar', 'sha1'),
  353. array('Foobar', 'sha256'),
  354. );
  355. }
  356. /**
  357. * @dataProvider hashProvider
  358. */
  359. public function testHash($data, $type) {
  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() {
  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 = array();
  372. while ($file = readdir($dh)) {
  373. if ($file != '.' and $file != '..') {
  374. $content[] = $file;
  375. }
  376. }
  377. $this->assertEquals(array('test.txt'), $content);
  378. }
  379. public function testCopyOverWriteFile() {
  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() {
  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() {
  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. $this->assertEquals('foo', $this->instance->file_get_contents('target/test1.txt'));
  410. $this->assertEquals('qwerty', $this->instance->file_get_contents('target/test2.txt'));
  411. $this->assertEquals('bar', $this->instance->file_get_contents('target/subfolder/test.txt'));
  412. }
  413. public function testRenameOverWriteDirectory() {
  414. $this->instance->mkdir('source');
  415. $this->instance->file_put_contents('source/test1.txt', 'foo');
  416. $this->instance->mkdir('target');
  417. $this->instance->file_put_contents('target/test1.txt', 'bar');
  418. $this->instance->file_put_contents('target/test2.txt', 'bar');
  419. $this->assertTrue($this->instance->rename('source', 'target'), 'rename must return true on success');
  420. $this->assertFalse($this->instance->file_exists('source'), 'source has not been removed');
  421. $this->assertFalse($this->instance->file_exists('source/test1.txt'), 'source/test1.txt has not been removed');
  422. $this->assertFalse($this->instance->file_exists('target/test2.txt'), 'target/test2.txt has not been removed');
  423. $this->assertEquals('foo', $this->instance->file_get_contents('target/test1.txt'), 'target/test1.txt has not been overwritten');
  424. }
  425. public function testRenameOverWriteDirectoryOverFile() {
  426. $this->instance->mkdir('source');
  427. $this->instance->file_put_contents('source/test1.txt', 'foo');
  428. $this->instance->file_put_contents('target', 'bar');
  429. $this->assertTrue($this->instance->rename('source', 'target'), 'rename must return true on success');
  430. $this->assertFalse($this->instance->file_exists('source'));
  431. $this->assertFalse($this->instance->file_exists('source/test1.txt'));
  432. $this->assertEquals('foo', $this->instance->file_get_contents('target/test1.txt'));
  433. }
  434. public function testCopyDirectory() {
  435. $this->instance->mkdir('source');
  436. $this->instance->file_put_contents('source/test1.txt', 'foo');
  437. $this->instance->file_put_contents('source/test2.txt', 'qwerty');
  438. $this->instance->mkdir('source/subfolder');
  439. $this->instance->file_put_contents('source/subfolder/test.txt', 'bar');
  440. $this->instance->copy('source', 'target');
  441. $this->assertTrue($this->instance->file_exists('source'));
  442. $this->assertTrue($this->instance->file_exists('source/test1.txt'));
  443. $this->assertTrue($this->instance->file_exists('source/test2.txt'));
  444. $this->assertTrue($this->instance->file_exists('source/subfolder'));
  445. $this->assertTrue($this->instance->file_exists('source/subfolder/test.txt'));
  446. $this->assertTrue($this->instance->file_exists('target'));
  447. $this->assertTrue($this->instance->file_exists('target/test1.txt'));
  448. $this->assertTrue($this->instance->file_exists('target/test2.txt'));
  449. $this->assertTrue($this->instance->file_exists('target/subfolder'));
  450. $this->assertTrue($this->instance->file_exists('target/subfolder/test.txt'));
  451. $this->assertEquals('foo', $this->instance->file_get_contents('target/test1.txt'));
  452. $this->assertEquals('qwerty', $this->instance->file_get_contents('target/test2.txt'));
  453. $this->assertEquals('bar', $this->instance->file_get_contents('target/subfolder/test.txt'));
  454. }
  455. public function testCopyOverWriteDirectory() {
  456. $this->instance->mkdir('source');
  457. $this->instance->file_put_contents('source/test1.txt', 'foo');
  458. $this->instance->mkdir('target');
  459. $this->instance->file_put_contents('target/test1.txt', 'bar');
  460. $this->instance->file_put_contents('target/test2.txt', 'bar');
  461. $this->instance->copy('source', 'target');
  462. $this->assertFalse($this->instance->file_exists('target/test2.txt'));
  463. $this->assertEquals('foo', $this->instance->file_get_contents('target/test1.txt'));
  464. }
  465. public function testCopyOverWriteDirectoryOverFile() {
  466. $this->instance->mkdir('source');
  467. $this->instance->file_put_contents('source/test1.txt', 'foo');
  468. $this->instance->file_put_contents('target', 'bar');
  469. $this->instance->copy('source', 'target');
  470. $this->assertEquals('foo', $this->instance->file_get_contents('target/test1.txt'));
  471. }
  472. public function testInstanceOfStorage() {
  473. $this->assertTrue($this->instance->instanceOfStorage('\OCP\Files\Storage'));
  474. $this->assertTrue($this->instance->instanceOfStorage(get_class($this->instance)));
  475. $this->assertFalse($this->instance->instanceOfStorage('\OC'));
  476. }
  477. /**
  478. * @dataProvider copyAndMoveProvider
  479. */
  480. public function testCopyFromSameStorage($source, $target) {
  481. $this->initSourceAndTarget($source);
  482. $this->instance->copyFromStorage($this->instance, $source, $target);
  483. $this->assertTrue($this->instance->file_exists($target), $target . ' was not created');
  484. $this->assertSameAsLorem($target);
  485. $this->assertTrue($this->instance->file_exists($source), $source . ' was deleted');
  486. }
  487. public function testIsCreatable() {
  488. $this->instance->mkdir('source');
  489. $this->assertTrue($this->instance->isCreatable('source'));
  490. }
  491. public function testIsReadable() {
  492. $this->instance->mkdir('source');
  493. $this->assertTrue($this->instance->isReadable('source'));
  494. }
  495. public function testIsUpdatable() {
  496. $this->instance->mkdir('source');
  497. $this->assertTrue($this->instance->isUpdatable('source'));
  498. }
  499. public function testIsDeletable() {
  500. $this->instance->mkdir('source');
  501. $this->assertTrue($this->instance->isDeletable('source'));
  502. }
  503. public function testIsShareable() {
  504. $this->instance->mkdir('source');
  505. $this->assertTrue($this->instance->isSharable('source'));
  506. }
  507. public function testStatAfterWrite() {
  508. $this->instance->file_put_contents('foo.txt', 'bar');
  509. $stat = $this->instance->stat('foo.txt');
  510. $this->assertEquals(3, $stat['size']);
  511. $fh = $this->instance->fopen('foo.txt', 'w');
  512. fwrite($fh, 'qwerty');
  513. fclose($fh);
  514. $stat = $this->instance->stat('foo.txt');
  515. $this->assertEquals(6, $stat['size']);
  516. }
  517. public function testPartFile() {
  518. $this->instance->file_put_contents('bar.txt.part', 'bar');
  519. $this->instance->rename('bar.txt.part', 'bar.txt');
  520. $this->assertEquals('bar', $this->instance->file_get_contents('bar.txt'));
  521. }
  522. public function testWriteStream() {
  523. $textFile = \OC::$SERVERROOT . '/tests/data/lorem.txt';
  524. if (!$this->instance->instanceOfStorage(IWriteStreamStorage::class)) {
  525. $this->markTestSkipped('Not a WriteSteamStorage');
  526. }
  527. /** @var IWriteStreamStorage $storage */
  528. $storage = $this->instance;
  529. $source = fopen($textFile, 'r');
  530. $storage->writeStream('test.txt', $source);
  531. $this->assertTrue($storage->file_exists('test.txt'));
  532. $this->assertEquals(file_get_contents($textFile), $storage->file_get_contents('test.txt'));
  533. }
  534. }