You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

streamwrappers.php 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. /**
  23. * Class Test_StreamWrappers
  24. *
  25. * @group DB
  26. */
  27. class Test_StreamWrappers extends \Test\TestCase {
  28. private static $trashBinStatus;
  29. public static function setUpBeforeClass() {
  30. self::$trashBinStatus = \OC_App::isEnabled('files_trashbin');
  31. \OC_App::disable('files_trashbin');
  32. }
  33. public static function tearDownAfterClass() {
  34. if (self::$trashBinStatus) {
  35. \OC_App::enable('files_trashbin');
  36. }
  37. }
  38. public function testFakeDir() {
  39. $items = array('foo', 'bar');
  40. \OC\Files\Stream\Dir::register('test', $items);
  41. $dh = opendir('fakedir://test');
  42. $result = array();
  43. while ($file = readdir($dh)) {
  44. $result[] = $file;
  45. $this->assertContains($file, $items);
  46. }
  47. $this->assertEquals(count($items), count($result));
  48. }
  49. public function testCloseStream() {
  50. //ensure all basic stream stuff works
  51. $sourceFile = OC::$SERVERROOT . '/tests/data/lorem.txt';
  52. $tmpFile = \OC::$server->getTempManager()->getTemporaryFile('.txt');
  53. $file = 'close://' . $tmpFile;
  54. $this->assertTrue(file_exists($file));
  55. file_put_contents($file, file_get_contents($sourceFile));
  56. $this->assertEquals(file_get_contents($sourceFile), file_get_contents($file));
  57. unlink($file);
  58. clearstatcache();
  59. $this->assertFalse(file_exists($file));
  60. //test callback
  61. $tmpFile = \OC::$server->getTempManager()->getTemporaryFile('.txt');
  62. $file = 'close://' . $tmpFile;
  63. $actual = false;
  64. $callback = function($path) use (&$actual) { $actual = $path; };
  65. \OC\Files\Stream\Close::registerCallback($tmpFile, $callback);
  66. $fh = fopen($file, 'w');
  67. fwrite($fh, 'asd');
  68. fclose($fh);
  69. $this->assertSame($tmpFile, $actual);
  70. }
  71. public function testOC() {
  72. // FIXME: use proper tearDown with $this->loginAsUser() and $this->logout()
  73. // (would currently break the tests for some reason)
  74. $originalStorage = \OC\Files\Filesystem::getStorage('/');
  75. \OC\Files\Filesystem::clearMounts();
  76. $storage = new \OC\Files\Storage\Temporary(array());
  77. $storage->file_put_contents('foo.txt', 'asd');
  78. \OC\Files\Filesystem::mount($storage, array(), '/');
  79. $this->assertTrue(file_exists('oc:///foo.txt'));
  80. $this->assertEquals('asd', file_get_contents('oc:///foo.txt'));
  81. $this->assertEquals(array('.', '..', 'foo.txt'), scandir('oc:///'));
  82. file_put_contents('oc:///bar.txt', 'qwerty');
  83. $this->assertEquals('qwerty', $storage->file_get_contents('bar.txt'));
  84. $this->assertEquals(array('.', '..', 'bar.txt', 'foo.txt'), scandir('oc:///'));
  85. $this->assertEquals('qwerty', file_get_contents('oc:///bar.txt'));
  86. $fh = fopen('oc:///bar.txt', 'rb');
  87. $this->assertSame(0, ftell($fh));
  88. $content = fread($fh, 4);
  89. $this->assertSame(4, ftell($fh));
  90. $this->assertSame('qwer', $content);
  91. $content = fread($fh, 1);
  92. $this->assertSame(5, ftell($fh));
  93. $this->assertSame(0, fseek($fh, 0));
  94. $this->assertSame(0, ftell($fh));
  95. unlink('oc:///foo.txt');
  96. $this->assertEquals(array('.', '..', 'bar.txt'), scandir('oc:///'));
  97. \OC\Files\Filesystem::clearMounts();
  98. \OC\Files\Filesystem::mount($originalStorage, array(), '/');
  99. }
  100. }