aboutsummaryrefslogtreecommitdiffstats
path: root/apps/files_archive/tests/archive.php
blob: 2e26b5e03b5c430456394f7e28f9a6030beb25d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?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');
		$this->assertEqual(4,count($allFiles));
		foreach($expected as $file){
			$this->assertNotIdentical(false,array_search($file,$allFiles),'cant find '.$file.' in archive');
			$this->assertTrue($this->instance->fileExists($file));
		}
		$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'));
		
		$tmpFile=OC_Helper::tmpFile('.txt');
		$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'));
		
		$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');
		OC_Helper::streamCopy($source,$fh);
		fclose($source);
		fclose($fh);
		$this->assertTrue($this->instance->fileExists('lorem.txt'));
		$this->assertEqual(file_get_contents($dir.'/lorem.txt'),$this->instance->getFile('lorem.txt'));
	}
}