Storage.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  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. abstract class Storage extends \Test\TestCase {
  25. /**
  26. * @var \OC\Files\Storage\Storage instance
  27. */
  28. protected $instance;
  29. protected $waitDelay = 0;
  30. /**
  31. * Sleep for the number of seconds specified in the
  32. * $waitDelay attribute
  33. */
  34. protected function wait() {
  35. if ($this->waitDelay > 0) {
  36. sleep($this->waitDelay);
  37. }
  38. }
  39. /**
  40. * the root folder of the storage should always exist, be readable and be recognized as a directory
  41. */
  42. public function testRoot() {
  43. $this->assertTrue($this->instance->file_exists('/'), 'Root folder does not exist');
  44. $this->assertTrue($this->instance->isReadable('/'), 'Root folder is not readable');
  45. $this->assertTrue($this->instance->is_dir('/'), 'Root folder is not a directory');
  46. $this->assertFalse($this->instance->is_file('/'), 'Root folder is a file');
  47. $this->assertEquals('dir', $this->instance->filetype('/'));
  48. //without this, any further testing would be useless, not an actual requirement for filestorage though
  49. $this->assertTrue($this->instance->isUpdatable('/'), 'Root folder is not writable');
  50. }
  51. /**
  52. * Check that the test() function works
  53. */
  54. public function testTestFunction() {
  55. $this->assertTrue($this->instance->test());
  56. }
  57. /**
  58. * @dataProvider directoryProvider
  59. */
  60. public function testDirectories($directory) {
  61. $this->assertFalse($this->instance->file_exists('/' . $directory));
  62. $this->assertTrue($this->instance->mkdir('/' . $directory));
  63. $this->assertTrue($this->instance->file_exists('/' . $directory));
  64. $this->assertTrue($this->instance->is_dir('/' . $directory));
  65. $this->assertFalse($this->instance->is_file('/' . $directory));
  66. $this->assertEquals('dir', $this->instance->filetype('/' . $directory));
  67. $this->assertEquals(0, $this->instance->filesize('/' . $directory));
  68. $this->assertTrue($this->instance->isReadable('/' . $directory));
  69. $this->assertTrue($this->instance->isUpdatable('/' . $directory));
  70. $dh = $this->instance->opendir('/');
  71. $content = array();
  72. while ($file = readdir($dh)) {
  73. if ($file != '.' and $file != '..') {
  74. $content[] = $file;
  75. }
  76. }
  77. $this->assertEquals(array($directory), $content);
  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 = array();
  85. while ($file = readdir($dh)) {
  86. if ($file != '.' and $file != '..') {
  87. $content[] = $file;
  88. }
  89. }
  90. $this->assertEquals(array(), $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. function loremFileProvider() {
  113. $root = \OC::$SERVERROOT . '/tests/data/';
  114. return array(
  115. // small file
  116. array($root . 'lorem.txt'),
  117. // bigger file (> 8 KB which is the standard PHP block size)
  118. array($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) {
  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() {
  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) {
  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) {
  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) {
  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) {
  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() {
  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() {
  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() {
  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() {
  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) {
  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() {
  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() {
  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() {
  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() {
  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 array(
  350. array('Foobar', 'md5'),
  351. array('Foobar', 'sha1'),
  352. array('Foobar', 'sha256'),
  353. );
  354. }
  355. /**
  356. * @dataProvider hashProvider
  357. */
  358. public function testHash($data, $type) {
  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() {
  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 = array();
  371. while ($file = readdir($dh)) {
  372. if ($file != '.' and $file != '..') {
  373. $content[] = $file;
  374. }
  375. }
  376. $this->assertEquals(array('test.txt'), $content);
  377. }
  378. public function testCopyOverWriteFile() {
  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() {
  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() {
  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. $this->assertEquals('foo', $this->instance->file_get_contents('target/test1.txt'));
  409. $this->assertEquals('qwerty', $this->instance->file_get_contents('target/test2.txt'));
  410. $this->assertEquals('bar', $this->instance->file_get_contents('target/subfolder/test.txt'));
  411. }
  412. public function testRenameOverWriteDirectory() {
  413. $this->instance->mkdir('source');
  414. $this->instance->file_put_contents('source/test1.txt', 'foo');
  415. $this->instance->mkdir('target');
  416. $this->instance->file_put_contents('target/test1.txt', 'bar');
  417. $this->instance->file_put_contents('target/test2.txt', 'bar');
  418. $this->assertTrue($this->instance->rename('source', 'target'), 'rename must return true on success');
  419. $this->assertFalse($this->instance->file_exists('source'), 'source has not been removed');
  420. $this->assertFalse($this->instance->file_exists('source/test1.txt'), 'source/test1.txt has not been removed');
  421. $this->assertFalse($this->instance->file_exists('target/test2.txt'), 'target/test2.txt has not been removed');
  422. $this->assertEquals('foo', $this->instance->file_get_contents('target/test1.txt'), 'target/test1.txt has not been overwritten');
  423. }
  424. public function testRenameOverWriteDirectoryOverFile() {
  425. $this->instance->mkdir('source');
  426. $this->instance->file_put_contents('source/test1.txt', 'foo');
  427. $this->instance->file_put_contents('target', 'bar');
  428. $this->assertTrue($this->instance->rename('source', 'target'), 'rename must return true on success');
  429. $this->assertFalse($this->instance->file_exists('source'));
  430. $this->assertFalse($this->instance->file_exists('source/test1.txt'));
  431. $this->assertEquals('foo', $this->instance->file_get_contents('target/test1.txt'));
  432. }
  433. public function testCopyDirectory() {
  434. $this->instance->mkdir('source');
  435. $this->instance->file_put_contents('source/test1.txt', 'foo');
  436. $this->instance->file_put_contents('source/test2.txt', 'qwerty');
  437. $this->instance->mkdir('source/subfolder');
  438. $this->instance->file_put_contents('source/subfolder/test.txt', 'bar');
  439. $this->instance->copy('source', 'target');
  440. $this->assertTrue($this->instance->file_exists('source'));
  441. $this->assertTrue($this->instance->file_exists('source/test1.txt'));
  442. $this->assertTrue($this->instance->file_exists('source/test2.txt'));
  443. $this->assertTrue($this->instance->file_exists('source/subfolder'));
  444. $this->assertTrue($this->instance->file_exists('source/subfolder/test.txt'));
  445. $this->assertTrue($this->instance->file_exists('target'));
  446. $this->assertTrue($this->instance->file_exists('target/test1.txt'));
  447. $this->assertTrue($this->instance->file_exists('target/test2.txt'));
  448. $this->assertTrue($this->instance->file_exists('target/subfolder'));
  449. $this->assertTrue($this->instance->file_exists('target/subfolder/test.txt'));
  450. $this->assertEquals('foo', $this->instance->file_get_contents('target/test1.txt'));
  451. $this->assertEquals('qwerty', $this->instance->file_get_contents('target/test2.txt'));
  452. $this->assertEquals('bar', $this->instance->file_get_contents('target/subfolder/test.txt'));
  453. }
  454. public function testCopyOverWriteDirectory() {
  455. $this->instance->mkdir('source');
  456. $this->instance->file_put_contents('source/test1.txt', 'foo');
  457. $this->instance->mkdir('target');
  458. $this->instance->file_put_contents('target/test1.txt', 'bar');
  459. $this->instance->file_put_contents('target/test2.txt', 'bar');
  460. $this->instance->copy('source', 'target');
  461. $this->assertFalse($this->instance->file_exists('target/test2.txt'));
  462. $this->assertEquals('foo', $this->instance->file_get_contents('target/test1.txt'));
  463. }
  464. public function testCopyOverWriteDirectoryOverFile() {
  465. $this->instance->mkdir('source');
  466. $this->instance->file_put_contents('source/test1.txt', 'foo');
  467. $this->instance->file_put_contents('target', 'bar');
  468. $this->instance->copy('source', 'target');
  469. $this->assertEquals('foo', $this->instance->file_get_contents('target/test1.txt'));
  470. }
  471. public function testInstanceOfStorage() {
  472. $this->assertTrue($this->instance->instanceOfStorage('\OCP\Files\Storage'));
  473. $this->assertTrue($this->instance->instanceOfStorage(get_class($this->instance)));
  474. $this->assertFalse($this->instance->instanceOfStorage('\OC'));
  475. }
  476. /**
  477. * @dataProvider copyAndMoveProvider
  478. */
  479. public function testCopyFromSameStorage($source, $target) {
  480. $this->initSourceAndTarget($source);
  481. $this->instance->copyFromStorage($this->instance, $source, $target);
  482. $this->assertTrue($this->instance->file_exists($target), $target . ' was not created');
  483. $this->assertSameAsLorem($target);
  484. $this->assertTrue($this->instance->file_exists($source), $source . ' was deleted');
  485. }
  486. public function testIsCreatable() {
  487. $this->instance->mkdir('source');
  488. $this->assertTrue($this->instance->isCreatable('source'));
  489. }
  490. public function testIsReadable() {
  491. $this->instance->mkdir('source');
  492. $this->assertTrue($this->instance->isReadable('source'));
  493. }
  494. public function testIsUpdatable() {
  495. $this->instance->mkdir('source');
  496. $this->assertTrue($this->instance->isUpdatable('source'));
  497. }
  498. public function testIsDeletable() {
  499. $this->instance->mkdir('source');
  500. $this->assertTrue($this->instance->isDeletable('source'));
  501. }
  502. public function testIsShareable() {
  503. $this->instance->mkdir('source');
  504. $this->assertTrue($this->instance->isSharable('source'));
  505. }
  506. public function testStatAfterWrite() {
  507. $this->instance->file_put_contents('foo.txt', 'bar');
  508. $stat = $this->instance->stat('foo.txt');
  509. $this->assertEquals(3, $stat['size']);
  510. $fh = $this->instance->fopen('foo.txt', 'w');
  511. fwrite($fh, 'qwerty');
  512. fclose($fh);
  513. $stat = $this->instance->stat('foo.txt');
  514. $this->assertEquals(6, $stat['size']);
  515. }
  516. public function testPartFile() {
  517. $this->instance->file_put_contents('bar.txt.part', 'bar');
  518. $this->instance->rename('bar.txt.part', 'bar.txt');
  519. $this->assertEquals('bar', $this->instance->file_get_contents('bar.txt'));
  520. }
  521. }