diff options
author | Robin Appelman <icewind@owncloud.com> | 2012-02-12 18:06:32 +0100 |
---|---|---|
committer | Robin Appelman <icewind@owncloud.com> | 2012-02-12 18:07:58 +0100 |
commit | 357944693017572319334aa8943e888cde0e99c0 (patch) | |
tree | d5c7e79c674c3db637865e8f14356ef2235a81aa /tests | |
parent | 0917bdecddd74a48ee2b21f18e184c579d156b62 (diff) | |
download | nextcloud-server-357944693017572319334aa8943e888cde0e99c0.tar.gz nextcloud-server-357944693017572319334aa8943e888cde0e99c0.zip |
use SimpleTest for unit testing
includes some tests for storage providers, more to come
Diffstat (limited to 'tests')
-rw-r--r-- | tests/index.php | 77 | ||||
-rw-r--r-- | tests/lib/filestorage.php | 63 | ||||
-rw-r--r-- | tests/lib/filestorage/local.php | 41 | ||||
-rw-r--r-- | tests/lib/filesystem.php | 222 | ||||
-rw-r--r-- | tests/templates/index.php | 11 |
5 files changed, 131 insertions, 283 deletions
diff --git a/tests/index.php b/tests/index.php index 34e1d4166ce..d29579a4bba 100644 --- a/tests/index.php +++ b/tests/index.php @@ -3,7 +3,7 @@ * ownCloud * * @author Robin Appelman -* @copyright 2010 Robin Appelman icewind1991@gmailc.om +* @copyright 2012 Robin Appelman icewind@owncloud.com * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE @@ -20,59 +20,36 @@ * */ +require_once '../lib/base.php'; +require_once 'simpletest/unit_tester.php'; +require_once 'simpletest/mock_objects.php'; +require_once 'simpletest/collector.php'; +require_once 'simpletest/default_reporter.php'; -/** - * run all test cases - */ - $RUNTIME_NOSETUPFS=true; -require_once('../lib/base.php'); -OC_Util::checkAdminUser(); +//load all test cases +loadTests(); -$testCases=loadFiles(__DIR__,array('index.php','templates')); -@ob_end_clean(); -$testResults=array(); -foreach($testCases as $testCaseClass){ - $testCase=new $testCaseClass(); - $results=array(); - foreach($testCase->getTests() as $test){ - $testCase->setup(); - try{ - $testCase->$test(); - $results[$test]='Ok'; - }catch(Exception $e){ - $results[$test]=$e->getMessage(); +function loadTests($dir=''){ + $basedir=dirname(__FILE__).'/'; + if($dh=opendir($basedir.$dir)){ + while($name=readdir($dh)){ + if(substr($name,0,1)!='.'){//no hidden files, '.' or '..' + $file=$dir.'/'.$name; + if(is_dir($basedir.$file)){ + loadTests($file); + }elseif(substr($file,-4)=='.php' and $file!=__FILE__){ + $testCase=new TestSuite(getTestName($file)); + $testCase->addFile($basedir.$file); + if($testCase->getSize()>0){ + $testCase->run(new DefaultReporter()); + } + } + } } - $testCase->tearDown(); } - $testResults[$testCaseClass]=$results; } -$tmpl = new OC_Template( 'tests', 'index'); -$tmpl->assign('tests',$testResults); -$tmpl->printPage(); - -/** - * recursively load all files in a folder - * @param string $path - * @param array $exclude list of files to exclude - */ -function loadFiles($path,$exclude=false){ - $results=array(); - if(!$exclude){ - $exclude=array(); - } - $dh=opendir($path); - while($file=readdir($dh)){ - if($file!='.' && $file!='..' && array_search($file,$exclude)===false){ - if(is_file($path.'/'.$file) and substr($file,-3)=='php'){ - $result=require_once($path.'/'.$file); - $results[]=$result; - }elseif(is_dir($path.'/'.$file)){ - $subResults=loadFiles($path.'/'.$file); - $results=array_merge($results,$subResults); - } - } - } - return $results; +function getTestName($file){ + //TODO: get better test names + return substr($file,5,-4);//strip /lib/ and .php } -?> diff --git a/tests/lib/filestorage.php b/tests/lib/filestorage.php new file mode 100644 index 00000000000..edb029d916f --- /dev/null +++ b/tests/lib/filestorage.php @@ -0,0 +1,63 @@ +<?php +/** +* ownCloud +* +* @author Robin Appelman +* @copyright 2012 Robin Appelman icewind@owncloud.com +* +* This library is free software; you can redistribute it and/or +* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE +* License as published by the Free Software Foundation; either +* version 3 of the License, or any later version. +* +* This library is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU AFFERO GENERAL PUBLIC LICENSE for more details. +* +* You should have received a copy of the GNU Affero General Public +* License along with this library. If not, see <http://www.gnu.org/licenses/>. +* +*/ + +abstract class Test_FileStorage extends UnitTestCase { + /** + * @var OC_Filestorage instance + */ + protected $instance; + + /** + * the root folder of the storage should always exist, be readable and be recognized as a directory + */ + public function testRoot(){ + $this->assertTrue($this->instance->file_exists('/'),'Root folder does not exist'); + $this->assertTrue($this->instance->is_readable('/'),'Root folder is not readable'); + $this->assertTrue($this->instance->is_dir('/'),'Root folder is not a directory'); + $this->assertFalse($this->instance->is_file('/'),'Root folder is a file'); + + //without this, any further testing would be useless, not an acutal requirement for filestorage though + $this->assertTrue($this->instance->is_writable('/'),'Root folder is not writable'); + } + + /** + * test the various uses of file_get_contents and file_put_contents + */ + public function testGetPutContents(){ + $sourceFile=OC::$SERVERROOT.'/tests/data/lorem.txt'; + $sourceText=file_get_contents($sourceFile); + + //fill a file with string data + $this->instance->file_put_contents('/lorem.txt',$sourceText); + $this->assertEqual($sourceText,$this->instance->file_get_contents('/lorem.txt'),'data returned from file_get_contents is not equal to the source data'); + + //fill a file with a stream + $this->instance->file_put_contents('/lorem.txt',fopen($sourceFile,'r')); + $this->assertEqual($sourceText,$this->instance->file_get_contents('/lorem.txt'),'data returned from file_get_contents is not equal to the source data'); + + //empty the file + $this->instance->file_put_contents('/lorem.txt',''); + $this->assertEqual('',$this->instance->file_get_contents('/lorem.txt'),'file not emptied'); + } +} + + diff --git a/tests/lib/filestorage/local.php b/tests/lib/filestorage/local.php new file mode 100644 index 00000000000..0a2d46c5ebc --- /dev/null +++ b/tests/lib/filestorage/local.php @@ -0,0 +1,41 @@ +<?php +/** +* ownCloud +* +* @author Robin Appelman +* @copyright 2012 Robin Appelman icewind@owncloud.com +* +* This library is free software; you can redistribute it and/or +* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE +* License as published by the Free Software Foundation; either +* version 3 of the License, or any later version. +* +* This library is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU AFFERO GENERAL PUBLIC LICENSE for more details. +* +* You should have received a copy of the GNU Affero General Public +* License along with this library. If not, see <http://www.gnu.org/licenses/>. +* +*/ + +class Test_Filestorage_Local extends Test_FileStorage { + /** + * @var string tmpDir + */ + private $tmpDir; + public function setUp(){ + $this->tmpDir=get_temp_dir().'/filestoragelocal'; + if(!file_exists($this->tmpDir)){ + mkdir($this->tmpDir); + } + $this->instance=new OC_Filestorage_Local(array('datadir'=>$this->tmpDir)); + } + + public function tearDown(){ + OC_Helper::rmdirr($this->tmpDir); + } +} + +?>
\ No newline at end of file diff --git a/tests/lib/filesystem.php b/tests/lib/filesystem.php deleted file mode 100644 index 43cf2e53f3f..00000000000 --- a/tests/lib/filesystem.php +++ /dev/null @@ -1,222 +0,0 @@ -<?php -class OC_FILEYSYSTEM_Test extends OC_TestCase -{ - public function setup(){ - OC_Util::setupFS('testuser','testcase'); - } - public function tearDown(){ - OC_Filesystem::chroot(''); - OC_Filesystem::delTree('/testuser'); - OC_Util::tearDownFS(); - } - - public function isDir(){ - $this->assertEquals(true, OC_Filesystem::is_dir('/'),'Root is not a directory'); - } - - public function fileExists(){ - $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'); - } - - public function mkdir(){ - 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'); - } - - public function rmdir(){ - OC_Filesystem::mkdir('/dummy'); - OC_Filesystem::rmdir('/dummy'); - $this->assertEquals(false, OC_Filesystem::file_exists('/dummy'),'Folder still exists after removing'); - } - - public function isFile(){ - $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'); - } - - public function opendir(){ - OC_Filesystem::file_put_contents('/dummy1','foo'); - OC_Filesystem::file_put_contents('/dummy2','foo'); - $dh=OC_Filesystem::opendir('/'); - 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(){ - OC_Filesystem::file_put_contents('/dummy','1234567890'); - $this->assertEquals(10, OC_Filesystem::filesize('/dummy'),'Unexpected filesize'); - } - - public function stat(){ - OC_Filesystem::file_put_contents('/dummy','foo'); - $time=time(); - $stat=OC_Filesystem::stat('/dummy'); - $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(){ - 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'); - } - - public function readfile(){ - OC_Filesystem::file_put_contents('/dummy','foo'); - ob_start(); - OC_Filesystem::readfile('/dummy'); - $this->assertEquals('foo', ob_get_contents(),'Unexpected output of readfile'); - @ob_end_clean(); - } - - public function isReadable(){ - OC_Filesystem::file_put_contents('/dummy','foo'); - $this->assertEquals(true, OC_Filesystem::is_readable('/dummy'),'Can\'t read created file'); - } - - public function isWritable(){ - OC_Filesystem::file_put_contents('/dummy','foo'); - $this->assertEquals(true, OC_Filesystem::is_writeable('/dummy'),'Can\'t write created file'); - } - - public function fileatime(){ - OC_Filesystem::file_put_contents('/dummy','foo'); - $time=time(); - $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 - } - - public function filectime(){ - OC_Filesystem::file_put_contents('/dummy','foo'); - $time=time(); - $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 - } - - public function filemtime(){ - OC_Filesystem::file_put_contents('/dummy','foo'); - $time=time(); - $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 - } - - public function fileGetContents(){ - OC_Filesystem::file_put_contents('/dummy','foo'); - $this->assertEquals('foo', OC_Filesystem::file_get_contents('/dummy'),'Unexpected content of file'); - } - - public function unlink(){ - OC_Filesystem::file_put_contents('/dummy','foo'); - OC_Filesystem::unlink('/dummy'); - $this->assertEquals(false, OC_Filesystem::file_exists('/dummy'),'File still exists after deletion'); - } - - public function rename(){ - 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'); - } - - public function copy(){ - 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'); - } - - public function fopen(){ - OC_Filesystem::file_put_contents('/dummy','foo'); - $fh=OC_Filesystem::fopen('/dummy','r'); - if(!$fh){ - $this->fail('Cant open file for reading'); - } - $content=fread($fh,3); - $this->assertEquals('foo', $content,'Unexpected content of file'); - fclose($fh); - $fh=OC_Filesystem::fopen('/dummy','a'); - fwrite($fh,'bar',3); - fclose($fh); - $this->assertEquals('foobar', OC_Filesystem::file_get_contents('/dummy'),'Unexpected content of file after appending'); - $fh=OC_Filesystem::fopen('/dummy','w'); - fwrite($fh,'bar',3); - fclose($fh); - $this->assertEquals('bar', OC_Filesystem::file_get_contents('/dummy'),'Unexpected content of file after writing'); - } - - public function toTmpFile(){ - OC_Filesystem::file_put_contents('/dummy','foo'); - $tmp=OC_Filesystem::toTmpFile('/dummy'); - $this->assertEquals('foo', file_get_contents($tmp),'Unexpected content of temporary file'); - unlink($tmp); - } - - public function fromTmpFile(){ - 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'); - $this->assertEquals(false, file_exists($tmp),'Temporary file still exists'); - } - - public function getMimeType(){ - 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'); - } - - public function delTree(){ - 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'); - } - - public function getTree(){ - OC_Filesystem::mkdir('/dummy'); - OC_Filesystem::file_put_contents('/dummy/bar','foo'); - $expected=array('/dummy','/dummy/bar'); - $this->assertEquals($expected, OC_Filesystem::getTree('/dummy'),'Unexpected filelist returned'); - } - - public function hash(){ - OC_Filesystem::file_put_contents('/dummy','foo'); - $this->assertEquals(md5('foo'), OC_Filesystem::hash('md5','/dummy'),'Unexpected md5 hash of file'); - } - - public function freeSpace(){ - $oldSpace=OC_Filesystem::free_space('/'); - OC_Filesystem::file_put_contents('/dummy','foo'); - $newSpace=OC_Filesystem::free_space('/'); - $this->assertEquals(true, $newSpace<$oldSpace,'free space not smaller after creating a non empty file'); - } - - public function search(){ - OC_Filesystem::file_put_contents('/dummy','foo'); - $this->assertEquals(array('/dummy'),OC_Filesystem::search('my'),'unexpected file list after search'); - } -} -return 'OC_FILEYSYSTEM_Test'; -?>
\ No newline at end of file diff --git a/tests/templates/index.php b/tests/templates/index.php deleted file mode 100644 index b8d3dac0666..00000000000 --- a/tests/templates/index.php +++ /dev/null @@ -1,11 +0,0 @@ -<?php foreach($_['tests'] as $name=>$results):?> - <h2><?php echo $name;?></h2> - <ul> - <?php foreach($results as $test=>$result):?> - <li> - <b><?php echo $test;?></b> - <?php echo $result;?> - </il> - <?php endforeach ?> - </ul> -<?php endforeach ?>
\ No newline at end of file |