StreamWrappersTest.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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;
  23. /**
  24. * Class StreamWrappersTest
  25. *
  26. * @group DB
  27. */
  28. class StreamWrappersTest extends \Test\TestCase {
  29. private static $trashBinStatus;
  30. public static function setUpBeforeClass() {
  31. self::$trashBinStatus = \OC_App::isEnabled('files_trashbin');
  32. \OC_App::disable('files_trashbin');
  33. }
  34. public static function tearDownAfterClass() {
  35. if (self::$trashBinStatus) {
  36. (new \OC_App())->enable('files_trashbin');
  37. }
  38. }
  39. public function testFakeDir() {
  40. $items = array('foo', 'bar');
  41. \OC\Files\Stream\Dir::register('test', $items);
  42. $dh = opendir('fakedir://test');
  43. $result = array();
  44. while ($file = readdir($dh)) {
  45. $result[] = $file;
  46. $this->assertContains($file, $items);
  47. }
  48. $this->assertEquals(count($items), count($result));
  49. }
  50. public function testCloseStream() {
  51. //ensure all basic stream stuff works
  52. $sourceFile = \OC::$SERVERROOT . '/tests/data/lorem.txt';
  53. $tmpFile = \OC::$server->getTempManager()->getTemporaryFile('.txt');
  54. $file = 'close://' . $tmpFile;
  55. $this->assertTrue(file_exists($file));
  56. file_put_contents($file, file_get_contents($sourceFile));
  57. $this->assertEquals(file_get_contents($sourceFile), file_get_contents($file));
  58. unlink($file);
  59. clearstatcache();
  60. $this->assertFalse(file_exists($file));
  61. //test callback
  62. $tmpFile = \OC::$server->getTempManager()->getTemporaryFile('.txt');
  63. $file = 'close://' . $tmpFile;
  64. $actual = false;
  65. $callback = function($path) use (&$actual) { $actual = $path; };
  66. \OC\Files\Stream\Close::registerCallback($tmpFile, $callback);
  67. $fh = fopen($file, 'w');
  68. fwrite($fh, 'asd');
  69. fclose($fh);
  70. $this->assertSame($tmpFile, $actual);
  71. }
  72. }