2011-06-12 02:58:47 +04:00
< ? php
class OC_FILEYSYSTEM_Test extends OC_TestCase
{
public function setup (){
2011-07-29 23:36:03 +04:00
OC_Util :: setupFS ( 'testuser' , 'testcase' );
2011-06-12 02:58:47 +04:00
}
2011-06-12 04:55:13 +04:00
public function tearDown (){
2011-07-29 23:36:03 +04:00
OC_Filesystem :: chroot ( '' );
OC_Filesystem :: delTree ( '/testuser' );
OC_Util :: tearDownFS ();
2011-06-12 04:55:13 +04:00
}
2011-06-12 02:58:47 +04:00
public function isDir (){
2011-07-29 23:36:03 +04:00
$this -> assertEquals ( true , OC_Filesystem :: is_dir ( '/' ), 'Root is not a directory' );
2011-06-12 02:58:47 +04:00
}
public function fileExists (){
2011-07-29 23:36:03 +04:00
$this -> assertEquals ( false , OC_Filesystem :: file_exists ( '/dummy' ), 'Unexpected result with non-existing file' );
OC_Filesystem :: file_put_contents ( '/dummy' , 'foo' );
$this -> assertEquals ( true , OC_Filesystem :: file_exists ( '/dummy' ), 'Unexpected result with existing file' );
2011-06-12 02:58:47 +04:00
}
public function mkdir (){
2011-07-29 23:36:03 +04:00
OC_Filesystem :: mkdir ( '/dummy' );
$this -> assertEquals ( true , OC_Filesystem :: file_exists ( '/dummy' ), 'No such file or directory after creating folder' );
$this -> assertEquals ( true , OC_Filesystem :: is_dir ( '/dummy' ), 'File created instead of filder' );
2011-06-12 02:58:47 +04:00
}
2011-06-12 04:55:13 +04:00
public function rmdir (){
2011-07-29 23:36:03 +04:00
OC_Filesystem :: mkdir ( '/dummy' );
OC_Filesystem :: rmdir ( '/dummy' );
$this -> assertEquals ( false , OC_Filesystem :: file_exists ( '/dummy' ), 'Folder still exists after removing' );
2011-06-12 04:55:13 +04:00
}
public function isFile (){
2011-07-29 23:36:03 +04:00
$this -> assertEquals ( false , OC_Filesystem :: is_file ( '/' ), 'Root is a file' );
OC_Filesystem :: file_put_contents ( '/dummy' , 'foo' );
$this -> assertEquals ( true , OC_Filesystem :: is_file ( '/dummy' ), 'Created file is not said to be a file' );
2011-06-12 04:55:13 +04:00
}
public function opendir (){
2011-07-29 23:36:03 +04:00
OC_Filesystem :: file_put_contents ( '/dummy1' , 'foo' );
OC_Filesystem :: file_put_contents ( '/dummy2' , 'foo' );
$dh = OC_Filesystem :: opendir ( '/' );
2011-06-12 04:55:13 +04:00
if ( ! $dh ){
$this -> fail ( 'Failed to open root' );
}
$dummy1Found = false ;
$dummy2Found = false ;
while ( $file = readdir ( $dh )){
if ( $file == 'dummy1' ){
$dummy1Found = true ;
} elseif ( $file == 'dummy2' ){
$dummy2Found = true ;
} elseif ( $file != '.' and $file != '..' ){
$this -> fail ( 'Unexpected filename when reading opened dir' );
}
}
$this -> assertEquals ( true , $dummy1Found , 'Not all files found when reading opened dir' );
$this -> assertEquals ( true , $dummy2Found , 'Not all files found when reading opened dir' );
}
public function filesize (){
2011-07-29 23:36:03 +04:00
OC_Filesystem :: file_put_contents ( '/dummy' , '1234567890' );
$this -> assertEquals ( 10 , OC_Filesystem :: filesize ( '/dummy' ), 'Unexpected filesize' );
2011-06-12 04:55:13 +04:00
}
public function stat (){
2011-07-29 23:36:03 +04:00
OC_Filesystem :: file_put_contents ( '/dummy' , 'foo' );
2011-06-12 04:55:13 +04:00
$time = time ();
2011-07-29 23:36:03 +04:00
$stat = OC_Filesystem :: stat ( '/dummy' );
2011-06-12 04:55:13 +04:00
$this -> assertEquals ( true , abs ( $time - $stat [ 'atime' ]) < 1 , 'Unexpected access time' ); //there can be small difference between those values due to running time
$this -> assertEquals ( true , abs ( $time - $stat [ 'ctime' ]) < 1 , 'Unexpected creation time' );
$this -> assertEquals ( true , abs ( $time - $stat [ 'mtime' ]) < 1 , 'Unexpected modified time' );
$this -> assertEquals ( 3 , $stat [ 'size' ], 'Unexpected filesize' );
}
public function filetype (){
2011-07-29 23:36:03 +04:00
OC_Filesystem :: file_put_contents ( '/dummyFile' , 'foo' );
OC_Filesystem :: mkdir ( '/dummyFolder' );
$this -> assertEquals ( 'file' , OC_Filesystem :: filetype ( '/dummyFile' ), 'Unexpected filetype of file' );
$this -> assertEquals ( 'dir' , OC_Filesystem :: filetype ( '/dummyFolder' ), 'Unexpected filetype of folder' );
2011-06-12 04:55:13 +04:00
}
public function readfile (){
2011-07-29 23:36:03 +04:00
OC_Filesystem :: file_put_contents ( '/dummy' , 'foo' );
2011-06-12 04:55:13 +04:00
ob_start ();
2011-07-29 23:36:03 +04:00
OC_Filesystem :: readfile ( '/dummy' );
2011-06-12 04:55:13 +04:00
$this -> assertEquals ( 'foo' , ob_get_contents (), 'Unexpected output of readfile' );
2011-10-17 03:18:02 +04:00
@ ob_end_clean ();
2011-06-12 04:55:13 +04:00
}
public function isReadable (){
2011-07-29 23:36:03 +04:00
OC_Filesystem :: file_put_contents ( '/dummy' , 'foo' );
$this -> assertEquals ( true , OC_Filesystem :: is_readable ( '/dummy' ), 'Can\'t read created file' );
2011-06-12 04:55:13 +04:00
}
public function isWritable (){
2011-07-29 23:36:03 +04:00
OC_Filesystem :: file_put_contents ( '/dummy' , 'foo' );
$this -> assertEquals ( true , OC_Filesystem :: is_writeable ( '/dummy' ), 'Can\'t write created file' );
2011-06-12 04:55:13 +04:00
}
public function fileatime (){
2011-07-29 23:36:03 +04:00
OC_Filesystem :: file_put_contents ( '/dummy' , 'foo' );
2011-06-12 04:55:13 +04:00
$time = time ();
2011-07-29 23:36:03 +04:00
$this -> assertEquals ( true , abs ( $time - OC_Filesystem :: fileatime ( '/dummy' )) < 1 , 'Unexpected access time' ); //there can be small difference between those values due to running time
2011-06-12 04:55:13 +04:00
}
public function filectime (){
2011-07-29 23:36:03 +04:00
OC_Filesystem :: file_put_contents ( '/dummy' , 'foo' );
2011-06-12 04:55:13 +04:00
$time = time ();
2011-07-29 23:36:03 +04:00
$this -> assertEquals ( true , abs ( $time - OC_Filesystem :: filectime ( '/dummy' )) < 1 , 'Unexpected creation time' ); //there can be small difference between those values due to running time
2011-06-12 04:55:13 +04:00
}
public function filemtime (){
2011-07-29 23:36:03 +04:00
OC_Filesystem :: file_put_contents ( '/dummy' , 'foo' );
2011-06-12 04:55:13 +04:00
$time = time ();
2011-07-29 23:36:03 +04:00
$this -> assertEquals ( true , abs ( $time - OC_Filesystem :: filemtime ( '/dummy' )) < 1 , 'Unexpected modified time' ); //there can be small difference between those values due to running time
2011-06-12 04:55:13 +04:00
}
public function fileGetContents (){
2011-07-29 23:36:03 +04:00
OC_Filesystem :: file_put_contents ( '/dummy' , 'foo' );
$this -> assertEquals ( 'foo' , OC_Filesystem :: file_get_contents ( '/dummy' ), 'Unexpected content of file' );
2011-06-12 04:55:13 +04:00
}
public function unlink (){
2011-07-29 23:36:03 +04:00
OC_Filesystem :: file_put_contents ( '/dummy' , 'foo' );
OC_Filesystem :: unlink ( '/dummy' );
$this -> assertEquals ( false , OC_Filesystem :: file_exists ( '/dummy' ), 'File still exists after deletion' );
2011-06-12 04:55:13 +04:00
}
public function rename (){
2011-07-29 23:36:03 +04:00
OC_Filesystem :: file_put_contents ( '/dummy' , 'foo' );
OC_Filesystem :: rename ( '/dummy' , '/bar' );
$this -> assertEquals ( true , OC_Filesystem :: file_exists ( '/bar' ), 'New file doesnt exists after moving' );
$this -> assertEquals ( false , OC_Filesystem :: file_exists ( '/dummy' ), 'Old file still exists after moving' );
$this -> assertEquals ( 'foo' , OC_Filesystem :: file_get_contents ( '/bar' ), 'Unexpected content of file after moving' );
2011-06-12 04:55:13 +04:00
}
public function copy (){
2011-07-29 23:36:03 +04:00
OC_Filesystem :: file_put_contents ( '/dummy' , 'foo' );
OC_Filesystem :: copy ( '/dummy' , '/bar' );
$this -> assertEquals ( true , OC_Filesystem :: file_exists ( '/bar' ), 'New file doesnt exists after copying' );
$this -> assertEquals ( true , OC_Filesystem :: file_exists ( '/dummy' ), 'Old file doesnt exists after copying' );
$this -> assertEquals ( 'foo' , OC_Filesystem :: file_get_contents ( '/bar' ), 'Unexpected content of file after copying' );
2011-06-12 04:55:13 +04:00
}
public function fopen (){
2011-07-29 23:36:03 +04:00
OC_Filesystem :: file_put_contents ( '/dummy' , 'foo' );
$fh = OC_Filesystem :: fopen ( '/dummy' , 'r' );
2011-06-12 04:55:13 +04:00
if ( ! $fh ){
$this -> fail ( 'Cant open file for reading' );
}
$content = fread ( $fh , 3 );
$this -> assertEquals ( 'foo' , $content , 'Unexpected content of file' );
fclose ( $fh );
2011-07-29 23:36:03 +04:00
$fh = OC_Filesystem :: fopen ( '/dummy' , 'a' );
2011-06-12 04:55:13 +04:00
fwrite ( $fh , 'bar' , 3 );
fclose ( $fh );
2011-07-29 23:36:03 +04:00
$this -> assertEquals ( 'foobar' , OC_Filesystem :: file_get_contents ( '/dummy' ), 'Unexpected content of file after appending' );
$fh = OC_Filesystem :: fopen ( '/dummy' , 'w' );
2011-06-12 04:55:13 +04:00
fwrite ( $fh , 'bar' , 3 );
fclose ( $fh );
2011-07-29 23:36:03 +04:00
$this -> assertEquals ( 'bar' , OC_Filesystem :: file_get_contents ( '/dummy' ), 'Unexpected content of file after writing' );
2011-06-12 04:55:13 +04:00
}
public function toTmpFile (){
2011-07-29 23:36:03 +04:00
OC_Filesystem :: file_put_contents ( '/dummy' , 'foo' );
$tmp = OC_Filesystem :: toTmpFile ( '/dummy' );
2011-06-12 04:55:13 +04:00
$this -> assertEquals ( 'foo' , file_get_contents ( $tmp ), 'Unexpected content of temporary file' );
unlink ( $tmp );
}
public function fromTmpFile (){
2011-07-29 23:36:03 +04:00
OC_Filesystem :: file_put_contents ( '/dummy' , 'foo' );
$tmp = OC_Filesystem :: toTmpFile ( '/dummy' );
OC_Filesystem :: fromTmpFile ( $tmp , '/bar' );
$this -> assertEquals ( 'foo' , OC_Filesystem :: file_get_contents ( '/bar' ), 'Unexpected content of new file' );
2011-06-12 04:55:13 +04:00
$this -> assertEquals ( false , file_exists ( $tmp ), 'Temporary file still exists' );
}
public function getMimeType (){
2011-07-29 23:36:03 +04:00
OC_Filesystem :: file_put_contents ( '/dummy' , 'some plain text' );
$this -> assertEquals ( 'text/plain' , OC_Filesystem :: getMimeType ( '/dummy' ), 'Unexpected mimetype of pain text file' );
OC_Filesystem :: file_put_contents ( '/dummy' , " <?xml version='1.0'?> \n </dummy> " );
$this -> assertEquals ( 'application/xml' , OC_Filesystem :: getMimeType ( '/dummy' ), 'Unexpected mimetype of xml file' );
2011-06-12 04:55:13 +04:00
}
public function delTree (){
2011-07-29 23:36:03 +04:00
OC_Filesystem :: mkdir ( '/dummy' );
OC_Filesystem :: file_put_contents ( '/dummy/bar' , 'foo' );
OC_Filesystem :: delTree ( '/dummy' );
$this -> assertEquals ( false , OC_Filesystem :: file_exists ( '/dummy/bar' ), 'File in deleted folder still exists' );
$this -> assertEquals ( false , OC_Filesystem :: file_exists ( '/dummy' ), 'Deleted folder still exists' );
2011-06-12 04:55:13 +04:00
}
public function getTree (){
2011-07-29 23:36:03 +04:00
OC_Filesystem :: mkdir ( '/dummy' );
OC_Filesystem :: file_put_contents ( '/dummy/bar' , 'foo' );
2011-06-12 04:55:13 +04:00
$expected = array ( '/dummy' , '/dummy/bar' );
2011-07-29 23:36:03 +04:00
$this -> assertEquals ( $expected , OC_Filesystem :: getTree ( '/dummy' ), 'Unexpected filelist returned' );
2011-06-12 04:55:13 +04:00
}
public function hash (){
2011-07-29 23:36:03 +04:00
OC_Filesystem :: file_put_contents ( '/dummy' , 'foo' );
$this -> assertEquals ( md5 ( 'foo' ), OC_Filesystem :: hash ( 'md5' , '/dummy' ), 'Unexpected md5 hash of file' );
2011-06-12 04:55:13 +04:00
}
public function freeSpace (){
2011-07-29 23:36:03 +04:00
$oldSpace = OC_Filesystem :: free_space ( '/' );
OC_Filesystem :: file_put_contents ( '/dummy' , 'foo' );
$newSpace = OC_Filesystem :: free_space ( '/' );
2011-06-12 04:55:13 +04:00
$this -> assertEquals ( true , $newSpace < $oldSpace , 'free space not smaller after creating a non empty file' );
}
public function search (){
2011-07-29 23:36:03 +04:00
OC_Filesystem :: file_put_contents ( '/dummy' , 'foo' );
$this -> assertEquals ( array ( '/dummy' ), OC_Filesystem :: search ( 'my' ), 'unexpected file list after search' );
2011-06-12 02:58:47 +04:00
}
}
return 'OC_FILEYSYSTEM_Test' ;
?>