2012-03-03 03:57:03 +04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Copyright (c) 2012 Robin Appelman <icewind@owncloud.com>
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
abstract class Test_Archive extends UnitTestCase {
|
|
|
|
/**
|
|
|
|
* @var OC_Archive
|
|
|
|
*/
|
|
|
|
protected $instance;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get the existing test archive
|
|
|
|
* @return OC_Archive
|
|
|
|
*/
|
|
|
|
abstract protected function getExisting();
|
|
|
|
/**
|
|
|
|
* get a new archive for write testing
|
|
|
|
* @return OC_Archive
|
|
|
|
*/
|
|
|
|
abstract protected function getNew();
|
|
|
|
|
|
|
|
public function testGetFiles(){
|
|
|
|
$this->instance=$this->getExisting();
|
|
|
|
$allFiles=$this->instance->getFiles();
|
|
|
|
$expected=array('lorem.txt','logo-wide.png','dir/','dir/lorem.txt');
|
2012-03-29 00:31:45 +04:00
|
|
|
$this->assertEqual(4,count($allFiles),'only found '.count($allFiles).' out of 4 expected files');
|
2012-03-03 03:57:03 +04:00
|
|
|
foreach($expected as $file){
|
|
|
|
$this->assertNotIdentical(false,array_search($file,$allFiles),'cant find '.$file.' in archive');
|
2012-03-29 00:31:45 +04:00
|
|
|
$this->assertTrue($this->instance->fileExists($file),'file '.$file.' does not exist in archive');
|
2012-03-03 03:57:03 +04:00
|
|
|
}
|
|
|
|
$this->assertFalse($this->instance->fileExists('non/existing/file'));
|
|
|
|
|
|
|
|
$rootContent=$this->instance->getFolder('');
|
|
|
|
$expected=array('lorem.txt','logo-wide.png','dir/');
|
|
|
|
$this->assertEqual(3,count($rootContent));
|
|
|
|
foreach($expected as $file){
|
|
|
|
$this->assertNotIdentical(false,array_search($file,$rootContent),'cant find '.$file.' in archive');
|
|
|
|
}
|
|
|
|
|
|
|
|
$dirContent=$this->instance->getFolder('dir/');
|
|
|
|
$expected=array('lorem.txt');
|
|
|
|
$this->assertEqual(1,count($dirContent));
|
|
|
|
foreach($expected as $file){
|
|
|
|
$this->assertNotIdentical(false,array_search($file,$dirContent),'cant find '.$file.' in archive');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testContent(){
|
|
|
|
$this->instance=$this->getExisting();
|
|
|
|
$dir=OC::$SERVERROOT.'/apps/files_archive/tests/data';
|
|
|
|
$textFile=$dir.'/lorem.txt';
|
|
|
|
$this->assertEqual(file_get_contents($textFile),$this->instance->getFile('lorem.txt'));
|
|
|
|
|
2012-05-02 14:54:31 +04:00
|
|
|
$tmpFile=OCP\Files::tmpFile('.txt');
|
2012-03-03 03:57:03 +04:00
|
|
|
$this->instance->extractFile('lorem.txt',$tmpFile);
|
|
|
|
$this->assertEqual(file_get_contents($textFile),file_get_contents($tmpFile));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testWrite(){
|
|
|
|
$dir=OC::$SERVERROOT.'/apps/files_archive/tests/data';
|
|
|
|
$textFile=$dir.'/lorem.txt';
|
|
|
|
$this->instance=$this->getNew();
|
|
|
|
$this->assertEqual(0,count($this->instance->getFiles()));
|
|
|
|
$this->instance->addFile('lorem.txt',$textFile);
|
|
|
|
$this->assertEqual(1,count($this->instance->getFiles()));
|
|
|
|
$this->assertTrue($this->instance->fileExists('lorem.txt'));
|
2012-03-29 01:46:44 +04:00
|
|
|
$this->assertFalse($this->instance->fileExists('lorem.txt/'));
|
2012-03-03 03:57:03 +04:00
|
|
|
|
|
|
|
$this->assertEqual(file_get_contents($textFile),$this->instance->getFile('lorem.txt'));
|
|
|
|
$this->instance->addFile('lorem.txt','foobar');
|
|
|
|
$this->assertEqual('foobar',$this->instance->getFile('lorem.txt'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testReadStream(){
|
|
|
|
$dir=OC::$SERVERROOT.'/apps/files_archive/tests/data';
|
|
|
|
$this->instance=$this->getExisting();
|
|
|
|
$fh=$this->instance->getStream('lorem.txt','r');
|
|
|
|
$this->assertTrue($fh);
|
|
|
|
$content=fread($fh,$this->instance->filesize('lorem.txt'));
|
|
|
|
fclose($fh);
|
|
|
|
$this->assertEqual(file_get_contents($dir.'/lorem.txt'),$content);
|
|
|
|
}
|
|
|
|
public function testWriteStream(){
|
|
|
|
$dir=OC::$SERVERROOT.'/apps/files_archive/tests/data';
|
|
|
|
$this->instance=$this->getNew();
|
|
|
|
$fh=$this->instance->getStream('lorem.txt','w');
|
|
|
|
$source=fopen($dir.'/lorem.txt','r');
|
2012-05-02 14:54:31 +04:00
|
|
|
OCP\Files::streamCopy($source,$fh);
|
2012-03-03 03:57:03 +04:00
|
|
|
fclose($source);
|
|
|
|
fclose($fh);
|
|
|
|
$this->assertTrue($this->instance->fileExists('lorem.txt'));
|
|
|
|
$this->assertEqual(file_get_contents($dir.'/lorem.txt'),$this->instance->getFile('lorem.txt'));
|
|
|
|
}
|
2012-03-29 01:46:44 +04:00
|
|
|
public function testFolder(){
|
|
|
|
$this->instance=$this->getNew();
|
|
|
|
$this->assertFalse($this->instance->fileExists('/test'));
|
|
|
|
$this->assertFalse($this->instance->fileExists('/test/'));
|
|
|
|
$this->instance->addFolder('/test');
|
|
|
|
$this->assertTrue($this->instance->fileExists('/test'));
|
|
|
|
$this->assertTrue($this->instance->fileExists('/test/'));
|
|
|
|
$this->instance->remove('/test');
|
|
|
|
$this->assertFalse($this->instance->fileExists('/test'));
|
|
|
|
$this->assertFalse($this->instance->fileExists('/test/'));
|
|
|
|
}
|
2012-03-29 00:31:45 +04:00
|
|
|
public function testExtract(){
|
|
|
|
$dir=OC::$SERVERROOT.'/apps/files_archive/tests/data';
|
|
|
|
$this->instance=$this->getExisting();
|
2012-05-02 14:54:31 +04:00
|
|
|
$tmpDir=OCP\Files::tmpFolder();
|
2012-03-29 00:31:45 +04:00
|
|
|
$this->instance->extract($tmpDir);
|
|
|
|
$this->assertEqual(true,file_exists($tmpDir.'lorem.txt'));
|
|
|
|
$this->assertEqual(true,file_exists($tmpDir.'dir/lorem.txt'));
|
|
|
|
$this->assertEqual(true,file_exists($tmpDir.'logo-wide.png'));
|
|
|
|
$this->assertEqual(file_get_contents($dir.'/lorem.txt'),file_get_contents($tmpDir.'lorem.txt'));
|
2012-05-02 14:54:31 +04:00
|
|
|
OCP\Files::rmdirr($tmpDir);
|
2012-03-29 00:31:45 +04:00
|
|
|
}
|
2012-03-29 01:46:44 +04:00
|
|
|
public function testMoveRemove(){
|
|
|
|
$dir=OC::$SERVERROOT.'/apps/files_archive/tests/data';
|
|
|
|
$textFile=$dir.'/lorem.txt';
|
|
|
|
$this->instance=$this->getNew();
|
|
|
|
$this->instance->addFile('lorem.txt',$textFile);
|
|
|
|
$this->assertFalse($this->instance->fileExists('target.txt'));
|
|
|
|
$this->instance->rename('lorem.txt','target.txt');
|
|
|
|
$this->assertTrue($this->instance->fileExists('target.txt'));
|
|
|
|
$this->assertFalse($this->instance->fileExists('lorem.txt'));
|
|
|
|
$this->assertEqual(file_get_contents($textFile),$this->instance->getFile('target.txt'));
|
|
|
|
$this->instance->remove('target.txt');
|
|
|
|
$this->assertFalse($this->instance->fileExists('target.txt'));
|
|
|
|
}
|
2012-03-03 03:57:03 +04:00
|
|
|
}
|