PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/lib/files/storage/storage.php

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