PageRenderTime 45ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/lib/filestorage.php

https://github.com/jlgg/simple_trash
PHP | 234 lines | 162 code | 36 blank | 36 comment | 11 complexity | 91940098cef81724f5d181ea31cc467b MD5 | raw file
Possible License(s): AGPL-3.0, AGPL-1.0, MPL-2.0-no-copyleft-exception
  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. abstract class Test_FileStorage extends UnitTestCase {
  23. /**
  24. * @var OC_Filestorage instance
  25. */
  26. protected $instance;
  27. /**
  28. * the root folder of the storage should always exist, be readable and be recognized as a directory
  29. */
  30. public function testRoot() {
  31. $this->assertTrue($this->instance->file_exists('/'), 'Root folder does not exist');
  32. $this->assertTrue($this->instance->isReadable('/'), 'Root folder is not readable');
  33. $this->assertTrue($this->instance->is_dir('/'), 'Root folder is not a directory');
  34. $this->assertFalse($this->instance->is_file('/'), 'Root folder is a file');
  35. $this->assertEqual('dir', $this->instance->filetype('/'));
  36. //without this, any further testing would be useless, not an acutal requirement for filestorage though
  37. $this->assertTrue($this->instance->isUpdatable('/'), 'Root folder is not writable');
  38. }
  39. public function testDirectories() {
  40. $this->assertFalse($this->instance->file_exists('/folder'));
  41. $this->assertTrue($this->instance->mkdir('/folder'));
  42. $this->assertTrue($this->instance->file_exists('/folder'));
  43. $this->assertTrue($this->instance->is_dir('/folder'));
  44. $this->assertFalse($this->instance->is_file('/folder'));
  45. $this->assertEqual('dir', $this->instance->filetype('/folder'));
  46. $this->assertEqual(0, $this->instance->filesize('/folder'));
  47. $this->assertTrue($this->instance->isReadable('/folder'));
  48. $this->assertTrue($this->instance->isUpdatable('/folder'));
  49. $dh = $this->instance->opendir('/');
  50. $content = array();
  51. while ($file = readdir($dh)) {
  52. if ($file != '.' and $file != '..') {
  53. $content[] = $file;
  54. }
  55. }
  56. $this->assertEqual(array('folder'), $content);
  57. $this->assertFalse($this->instance->mkdir('/folder')); //cant create existing folders
  58. $this->assertTrue($this->instance->rmdir('/folder'));
  59. $this->assertFalse($this->instance->file_exists('/folder'));
  60. $this->assertFalse($this->instance->rmdir('/folder')); //cant remove non existing folders
  61. $dh = $this->instance->opendir('/');
  62. $content = array();
  63. while ($file = readdir($dh)) {
  64. if ($file != '.' and $file != '..') {
  65. $content[] = $file;
  66. }
  67. }
  68. $this->assertEqual(array(), $content);
  69. }
  70. /**
  71. * test the various uses of file_get_contents and file_put_contents
  72. */
  73. public function testGetPutContents() {
  74. $sourceFile = OC::$SERVERROOT . '/tests/data/lorem.txt';
  75. $sourceText = file_get_contents($sourceFile);
  76. //fill a file with string data
  77. $this->instance->file_put_contents('/lorem.txt', $sourceText);
  78. $this->assertFalse($this->instance->is_dir('/lorem.txt'));
  79. $this->assertEqual($sourceText, $this->instance->file_get_contents('/lorem.txt'), 'data returned from file_get_contents is not equal to the source data');
  80. //empty the file
  81. $this->instance->file_put_contents('/lorem.txt', '');
  82. $this->assertEqual('', $this->instance->file_get_contents('/lorem.txt'), 'file not emptied');
  83. }
  84. /**
  85. * test various known mimetypes
  86. */
  87. public function testMimeType() {
  88. $this->assertEqual('httpd/unix-directory', $this->instance->getMimeType('/'));
  89. $this->assertEqual(false, $this->instance->getMimeType('/non/existing/file'));
  90. $textFile = OC::$SERVERROOT . '/tests/data/lorem.txt';
  91. $this->instance->file_put_contents('/lorem.txt', file_get_contents($textFile, 'r'));
  92. $this->assertEqual('text/plain', $this->instance->getMimeType('/lorem.txt'));
  93. $pngFile = OC::$SERVERROOT . '/tests/data/logo-wide.png';
  94. $this->instance->file_put_contents('/logo-wide.png', file_get_contents($pngFile, 'r'));
  95. $this->assertEqual('image/png', $this->instance->getMimeType('/logo-wide.png'));
  96. $svgFile = OC::$SERVERROOT . '/tests/data/logo-wide.svg';
  97. $this->instance->file_put_contents('/logo-wide.svg', file_get_contents($svgFile, 'r'));
  98. $this->assertEqual('image/svg+xml', $this->instance->getMimeType('/logo-wide.svg'));
  99. }
  100. public function testCopyAndMove() {
  101. $textFile = OC::$SERVERROOT . '/tests/data/lorem.txt';
  102. $this->instance->file_put_contents('/source.txt', file_get_contents($textFile));
  103. $this->instance->copy('/source.txt', '/target.txt');
  104. $this->assertTrue($this->instance->file_exists('/target.txt'));
  105. $this->assertEqual($this->instance->file_get_contents('/source.txt'), $this->instance->file_get_contents('/target.txt'));
  106. $this->instance->rename('/source.txt', '/target2.txt');
  107. $this->assertTrue($this->instance->file_exists('/target2.txt'));
  108. $this->assertFalse($this->instance->file_exists('/source.txt'));
  109. $this->assertEqual(file_get_contents($textFile), $this->instance->file_get_contents('/target.txt'));
  110. }
  111. public function testLocal() {
  112. $textFile = OC::$SERVERROOT . '/tests/data/lorem.txt';
  113. $this->instance->file_put_contents('/lorem.txt', file_get_contents($textFile));
  114. $localFile = $this->instance->getLocalFile('/lorem.txt');
  115. $this->assertTrue(file_exists($localFile));
  116. $this->assertEqual(file_get_contents($localFile), file_get_contents($textFile));
  117. $this->instance->mkdir('/folder');
  118. $this->instance->file_put_contents('/folder/lorem.txt', file_get_contents($textFile));
  119. $this->instance->file_put_contents('/folder/bar.txt', 'asd');
  120. $this->instance->mkdir('/folder/recursive');
  121. $this->instance->file_put_contents('/folder/recursive/file.txt', 'foo');
  122. $localFolder = $this->instance->getLocalFolder('/folder');
  123. $this->assertTrue(is_dir($localFolder));
  124. $this->assertTrue(file_exists($localFolder . '/lorem.txt'));
  125. $this->assertEqual(file_get_contents($localFolder . '/lorem.txt'), file_get_contents($textFile));
  126. $this->assertEqual(file_get_contents($localFolder . '/bar.txt'), 'asd');
  127. $this->assertEqual(file_get_contents($localFolder . '/recursive/file.txt'), 'foo');
  128. }
  129. public function testStat() {
  130. $textFile = OC::$SERVERROOT . '/tests/data/lorem.txt';
  131. $ctimeStart = time();
  132. $this->instance->file_put_contents('/lorem.txt', file_get_contents($textFile));
  133. $this->assertTrue($this->instance->isReadable('/lorem.txt'));
  134. $ctimeEnd = time();
  135. $mTime = $this->instance->filemtime('/lorem.txt');
  136. $this->assertTrue($this->instance->hasUpdated('/lorem.txt', $ctimeStart - 1));
  137. $this->assertTrue($this->instance->hasUpdated('/', $ctimeStart - 1));
  138. $this->assertTrue(($ctimeStart - 1) <= $mTime);
  139. $this->assertTrue($mTime <= ($ctimeEnd + 1));
  140. $this->assertEqual(filesize($textFile), $this->instance->filesize('/lorem.txt'));
  141. $stat = $this->instance->stat('/lorem.txt');
  142. //only size and mtime are requered in the result
  143. $this->assertEqual($stat['size'], $this->instance->filesize('/lorem.txt'));
  144. $this->assertEqual($stat['mtime'], $mTime);
  145. $mtimeStart = time();
  146. $supportsTouch = $this->instance->touch('/lorem.txt');
  147. $mtimeEnd = time();
  148. if ($supportsTouch !== false) {
  149. $mTime = $this->instance->filemtime('/lorem.txt');
  150. $this->assertTrue(($mtimeStart - 1) <= $mTime);
  151. $this->assertTrue($mTime <= ($mtimeEnd + 1));
  152. $this->assertTrue($this->instance->hasUpdated('/lorem.txt', $mtimeStart - 1));
  153. if ($this->instance->touch('/lorem.txt', 100) !== false) {
  154. $mTime = $this->instance->filemtime('/lorem.txt');
  155. $this->assertEqual($mTime, 100);
  156. }
  157. }
  158. $mtimeStart = time();
  159. $fh = $this->instance->fopen('/lorem.txt', 'a');
  160. fwrite($fh, ' ');
  161. fclose($fh);
  162. clearstatcache();
  163. $mtimeEnd = time();
  164. $mTime = $this->instance->filemtime('/lorem.txt');
  165. $this->assertTrue(($mtimeStart - 1) <= $mTime);
  166. $this->assertTrue($mTime <= ($mtimeEnd + 1));
  167. $this->instance->unlink('/lorem.txt');
  168. $this->assertTrue($this->instance->hasUpdated('/', $mtimeStart - 1));
  169. }
  170. public function testSearch() {
  171. $textFile = OC::$SERVERROOT . '/tests/data/lorem.txt';
  172. $this->instance->file_put_contents('/lorem.txt', file_get_contents($textFile, 'r'));
  173. $pngFile = OC::$SERVERROOT . '/tests/data/logo-wide.png';
  174. $this->instance->file_put_contents('/logo-wide.png', file_get_contents($pngFile, 'r'));
  175. $svgFile = OC::$SERVERROOT . '/tests/data/logo-wide.svg';
  176. $this->instance->file_put_contents('/logo-wide.svg', file_get_contents($svgFile, 'r'));
  177. $result = $this->instance->search('logo');
  178. $this->assertEqual(2, count($result));
  179. $this->assertContains('/logo-wide.svg', $result);
  180. $this->assertContains('/logo-wide.png', $result);
  181. }
  182. public function testFOpen() {
  183. $textFile = OC::$SERVERROOT . '/tests/data/lorem.txt';
  184. $fh = @$this->instance->fopen('foo', 'r');
  185. if ($fh) {
  186. fclose($fh);
  187. }
  188. $this->assertFalse($fh);
  189. $this->assertFalse($this->instance->file_exists('foo'));
  190. $fh = $this->instance->fopen('foo', 'w');
  191. fwrite($fh, file_get_contents($textFile));
  192. fclose($fh);
  193. $this->assertTrue($this->instance->file_exists('foo'));
  194. $fh = $this->instance->fopen('foo', 'r');
  195. $content = stream_get_contents($fh);
  196. $this->assertEqual(file_get_contents($textFile), $content);
  197. }
  198. }