You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

filestorage.php 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. }
  62. /**
  63. * test the various uses of file_get_contents and file_put_contents
  64. */
  65. public function testGetPutContents(){
  66. $sourceFile=OC::$SERVERROOT.'/tests/data/lorem.txt';
  67. $sourceText=file_get_contents($sourceFile);
  68. //fill a file with string data
  69. $this->instance->file_put_contents('/lorem.txt',$sourceText);
  70. $this->assertEqual($sourceText,$this->instance->file_get_contents('/lorem.txt'),'data returned from file_get_contents is not equal to the source data');
  71. //empty the file
  72. $this->instance->file_put_contents('/lorem.txt','');
  73. $this->assertEqual('',$this->instance->file_get_contents('/lorem.txt'),'file not emptied');
  74. }
  75. /**
  76. * test various known mimetypes
  77. */
  78. public function testMimeType(){
  79. $this->assertEqual('httpd/unix-directory',$this->instance->getMimeType('/'));
  80. $this->assertEqual(false,$this->instance->getMimeType('/non/existing/file'));
  81. $textFile=OC::$SERVERROOT.'/tests/data/lorem.txt';
  82. $this->instance->file_put_contents('/lorem.txt',file_get_contents($textFile,'r'));
  83. $this->assertEqual('text/plain',$this->instance->getMimeType('/lorem.txt'));
  84. $pngFile=OC::$SERVERROOT.'/tests/data/logo-wide.png';
  85. $this->instance->file_put_contents('/logo-wide.png',file_get_contents($pngFile,'r'));
  86. $this->assertEqual('image/png',$this->instance->getMimeType('/logo-wide.png'));
  87. $svgFile=OC::$SERVERROOT.'/tests/data/logo-wide.svg';
  88. $this->instance->file_put_contents('/logo-wide.svg',file_get_contents($svgFile,'r'));
  89. $this->assertEqual('image/svg+xml',$this->instance->getMimeType('/logo-wide.svg'));
  90. }
  91. public function testCopyAndMove(){
  92. $textFile=OC::$SERVERROOT.'/tests/data/lorem.txt';
  93. $this->instance->file_put_contents('/source.txt',file_get_contents($textFile));
  94. $this->instance->copy('/source.txt','/target.txt');
  95. $this->assertTrue($this->instance->file_exists('/target.txt'));
  96. $this->assertEqual($this->instance->file_get_contents('/source.txt'),$this->instance->file_get_contents('/target.txt'));
  97. $this->instance->rename('/source.txt','/target2.txt');
  98. $this->assertTrue($this->instance->file_exists('/target2.txt'));
  99. $this->assertFalse($this->instance->file_exists('/source.txt'));
  100. $this->assertEqual(file_get_contents($textFile),$this->instance->file_get_contents('/target.txt'));
  101. }
  102. public function testLocalFile(){
  103. $textFile=OC::$SERVERROOT.'/tests/data/lorem.txt';
  104. $this->instance->file_put_contents('/lorem.txt',file_get_contents($textFile));
  105. $localFile=$this->instance->getLocalFile('/lorem.txt');
  106. $this->assertTrue(file_exists($localFile));
  107. $this->assertEqual(file_get_contents($localFile),file_get_contents($textFile));
  108. }
  109. public function testStat(){
  110. $textFile=OC::$SERVERROOT.'/tests/data/lorem.txt';
  111. $ctimeStart=time();
  112. $this->instance->file_put_contents('/lorem.txt',file_get_contents($textFile));
  113. $this->assertTrue($this->instance->is_readable('/lorem.txt'));
  114. $ctimeEnd=time();
  115. $cTime=$this->instance->filectime('/lorem.txt');
  116. $mTime=$this->instance->filemtime('/lorem.txt');
  117. $this->assertTrue($ctimeStart<=$cTime);
  118. $this->assertTrue($cTime<=$ctimeEnd);
  119. $this->assertEqual($cTime,$mTime);
  120. $this->assertEqual(filesize($textFile),$this->instance->filesize('/lorem.txt'));
  121. $stat=$this->instance->stat('/lorem.txt');
  122. //only size, mtime and ctime are requered in the result
  123. $this->assertEqual($stat['size'],$this->instance->filesize('/lorem.txt'));
  124. $this->assertEqual($stat['mtime'],$mTime);
  125. $this->assertEqual($stat['ctime'],$cTime);
  126. $mtimeStart=time();
  127. $this->instance->touch('/lorem.txt');
  128. $mtimeEnd=time();
  129. $originalCTime=$cTime;
  130. $cTime=$this->instance->filectime('/lorem.txt');
  131. $mTime=$this->instance->filemtime('/lorem.txt');
  132. $this->assertTrue($mtimeStart<=$mTime);
  133. $this->assertTrue($mTime<=$mtimeEnd);
  134. $this->assertEqual($cTime,$originalCTime);
  135. if($this->instance->touch('/lorem.txt',100)!==false){
  136. $mTime=$this->instance->filemtime('/lorem.txt');
  137. $this->assertEqual($mTime,100);
  138. }
  139. $mtimeStart=time();
  140. $fh=$this->instance->fopen('/lorem.txt','a');
  141. fwrite($fh,' ');
  142. fclose($fh);
  143. clearstatcache();
  144. $mtimeEnd=time();
  145. $originalCTime=$cTime;
  146. $mTime=$this->instance->filemtime('/lorem.txt');
  147. $this->assertTrue($mtimeStart<=$mTime);
  148. $this->assertTrue($mTime<=$mtimeEnd);
  149. }
  150. public function testSearch(){
  151. $textFile=OC::$SERVERROOT.'/tests/data/lorem.txt';
  152. $this->instance->file_put_contents('/lorem.txt',file_get_contents($textFile,'r'));
  153. $pngFile=OC::$SERVERROOT.'/tests/data/logo-wide.png';
  154. $this->instance->file_put_contents('/logo-wide.png',file_get_contents($pngFile,'r'));
  155. $svgFile=OC::$SERVERROOT.'/tests/data/logo-wide.svg';
  156. $this->instance->file_put_contents('/logo-wide.svg',file_get_contents($svgFile,'r'));
  157. $result=$this->instance->search('logo');
  158. $this->assertEqual(2,count($result));
  159. $this->assertNotIdentical(false,array_search('/logo-wide.svg',$result));
  160. $this->assertNotIdentical(false,array_search('/logo-wide.png',$result));
  161. }
  162. }