streamwrappers.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. class Test_StreamWrappers extends PHPUnit_Framework_TestCase {
  23. public function testFakeDir() {
  24. $items = array('foo', 'bar');
  25. \OC\Files\Stream\Dir::register('test', $items);
  26. $dh = opendir('fakedir://test');
  27. $result = array();
  28. while ($file = readdir($dh)) {
  29. $result[] = $file;
  30. $this->assertContains($file, $items);
  31. }
  32. $this->assertEquals(count($items), count($result));
  33. }
  34. public function testCloseStream() {
  35. //ensure all basic stream stuff works
  36. $sourceFile = OC::$SERVERROOT . '/tests/data/lorem.txt';
  37. $tmpFile = OC_Helper::TmpFile('.txt');
  38. $file = 'close://' . $tmpFile;
  39. $this->assertTrue(file_exists($file));
  40. file_put_contents($file, file_get_contents($sourceFile));
  41. $this->assertEquals(file_get_contents($sourceFile), file_get_contents($file));
  42. unlink($file);
  43. clearstatcache();
  44. $this->assertFalse(file_exists($file));
  45. //test callback
  46. $tmpFile = OC_Helper::TmpFile('.txt');
  47. $file = 'close://' . $tmpFile;
  48. \OC\Files\Stream\Close::registerCallback($tmpFile, array('Test_StreamWrappers', 'closeCallBack'));
  49. $fh = fopen($file, 'w');
  50. fwrite($fh, 'asd');
  51. try {
  52. fclose($fh);
  53. $this->fail('Expected exception');
  54. } catch (Exception $e) {
  55. $path = $e->getMessage();
  56. $this->assertEquals($path, $tmpFile);
  57. }
  58. }
  59. public static function closeCallBack($path) {
  60. throw new Exception($path);
  61. }
  62. public function testOC() {
  63. \OC\Files\Filesystem::clearMounts();
  64. $storage = new \OC\Files\Storage\Temporary(array());
  65. $storage->file_put_contents('foo.txt', 'asd');
  66. \OC\Files\Filesystem::mount($storage, array(), '/');
  67. $this->assertTrue(file_exists('oc:///foo.txt'));
  68. $this->assertEquals('asd', file_get_contents('oc:///foo.txt'));
  69. $this->assertEquals(array('.', '..', 'foo.txt'), scandir('oc:///'));
  70. file_put_contents('oc:///bar.txt', 'qwerty');
  71. $this->assertEquals('qwerty', $storage->file_get_contents('bar.txt'));
  72. $this->assertEquals(array('.', '..', 'bar.txt', 'foo.txt'), scandir('oc:///'));
  73. $this->assertEquals('qwerty', file_get_contents('oc:///bar.txt'));
  74. unlink('oc:///foo.txt');
  75. $this->assertEquals(array('.', '..', 'bar.txt'), scandir('oc:///'));
  76. }
  77. }