filestorage.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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->is_readable('/'),'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->is_writable('/'),'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->is_readable('/folder'));
  48. $this->assertTrue($this->instance->is_writable('/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 testLocalFile(){
  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. }
  118. public function testStat(){
  119. $textFile=OC::$SERVERROOT.'/tests/data/lorem.txt';
  120. $ctimeStart=time();
  121. $this->instance->file_put_contents('/lorem.txt',file_get_contents($textFile));
  122. $this->assertTrue($this->instance->is_readable('/lorem.txt'));
  123. $ctimeEnd=time();
  124. $cTime=$this->instance->filectime('/lorem.txt');
  125. $mTime=$this->instance->filemtime('/lorem.txt');
  126. if($cTime!=-1){//not everything can support ctime
  127. $this->assertTrue(($ctimeStart-1)<=$cTime);
  128. $this->assertTrue($cTime<=($ctimeEnd+1));
  129. }
  130. $this->assertTrue($this->instance->hasUpdated('/lorem.txt',$ctimeStart-1));
  131. $this->assertTrue($this->instance->hasUpdated('/',$ctimeStart-1));
  132. $this->assertTrue(($ctimeStart-1)<=$mTime);
  133. $this->assertTrue($mTime<=($ctimeEnd+1));
  134. $this->assertEqual(filesize($textFile),$this->instance->filesize('/lorem.txt'));
  135. $stat=$this->instance->stat('/lorem.txt');
  136. //only size, mtime and ctime are requered in the result
  137. $this->assertEqual($stat['size'],$this->instance->filesize('/lorem.txt'));
  138. $this->assertEqual($stat['mtime'],$mTime);
  139. $this->assertEqual($stat['ctime'],$cTime);
  140. $mtimeStart=time();
  141. $this->instance->touch('/lorem.txt');
  142. $mtimeEnd=time();
  143. $originalCTime=$cTime;
  144. $cTime=$this->instance->filectime('/lorem.txt');
  145. $mTime=$this->instance->filemtime('/lorem.txt');
  146. $this->assertTrue(($mtimeStart-1)<=$mTime);
  147. $this->assertTrue($mTime<=($mtimeEnd+1));
  148. $this->assertEqual($cTime,$originalCTime);
  149. $this->assertTrue($this->instance->hasUpdated('/lorem.txt',$mtimeStart-1));
  150. if($this->instance->touch('/lorem.txt',100)!==false){
  151. $mTime=$this->instance->filemtime('/lorem.txt');
  152. $this->assertEqual($mTime,100);
  153. }
  154. $mtimeStart=time();
  155. $fh=$this->instance->fopen('/lorem.txt','a');
  156. fwrite($fh,' ');
  157. fclose($fh);
  158. clearstatcache();
  159. $mtimeEnd=time();
  160. $originalCTime=$cTime;
  161. $mTime=$this->instance->filemtime('/lorem.txt');
  162. $this->assertTrue(($mtimeStart-1)<=$mTime);
  163. $this->assertTrue($mTime<=($mtimeEnd+1));
  164. $this->instance->unlink('/lorem.txt');
  165. $this->assertTrue($this->instance->hasUpdated('/',$mtimeStart-1));
  166. }
  167. public function testSearch(){
  168. $textFile=OC::$SERVERROOT.'/tests/data/lorem.txt';
  169. $this->instance->file_put_contents('/lorem.txt',file_get_contents($textFile,'r'));
  170. $pngFile=OC::$SERVERROOT.'/tests/data/logo-wide.png';
  171. $this->instance->file_put_contents('/logo-wide.png',file_get_contents($pngFile,'r'));
  172. $svgFile=OC::$SERVERROOT.'/tests/data/logo-wide.svg';
  173. $this->instance->file_put_contents('/logo-wide.svg',file_get_contents($svgFile,'r'));
  174. $result=$this->instance->search('logo');
  175. $this->assertEqual(2,count($result));
  176. $this->assertNotIdentical(false,array_search('/logo-wide.svg',$result));
  177. $this->assertNotIdentical(false,array_search('/logo-wide.png',$result));
  178. }
  179. }