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.
|
|
|
|
*/
|
|
|
|
|
2016-05-18 19:46:03 +03:00
|
|
|
namespace Test\Archive;
|
|
|
|
|
|
|
|
abstract class TestBase extends \Test\TestCase {
|
2012-03-03 03:57:03 +04:00
|
|
|
/**
|
2016-05-18 19:46:03 +03:00
|
|
|
* @var \OC\Archive\Archive
|
2012-03-03 03:57:03 +04:00
|
|
|
*/
|
|
|
|
protected $instance;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get the existing test archive
|
2016-05-18 19:46:03 +03:00
|
|
|
* @return \OC\Archive\Archive
|
2012-03-03 03:57:03 +04:00
|
|
|
*/
|
|
|
|
abstract protected function getExisting();
|
|
|
|
/**
|
|
|
|
* get a new archive for write testing
|
2016-05-18 19:46:03 +03:00
|
|
|
* @return \OC\Archive\Archive
|
2012-03-03 03:57:03 +04:00
|
|
|
*/
|
|
|
|
abstract protected function getNew();
|
2012-10-06 00:24:36 +04:00
|
|
|
|
2012-09-07 17:22:01 +04:00
|
|
|
public function testGetFiles() {
|
2012-03-03 03:57:03 +04:00
|
|
|
$this->instance=$this->getExisting();
|
|
|
|
$allFiles=$this->instance->getFiles();
|
2020-03-26 11:30:18 +03:00
|
|
|
$expected=['lorem.txt','logo-wide.png','dir/', 'dir/lorem.txt'];
|
2013-01-24 19:47:17 +04:00
|
|
|
$this->assertEquals(4, count($allFiles), 'only found '.count($allFiles).' out of 4 expected files');
|
2012-09-07 17:22:01 +04:00
|
|
|
foreach($expected as $file) {
|
2012-10-06 00:24:36 +04:00
|
|
|
$this->assertContains($file, $allFiles, 'cant find '. $file . ' in archive');
|
2012-11-04 14:10:46 +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'));
|
2012-10-06 00:24:36 +04:00
|
|
|
|
2012-03-03 03:57:03 +04:00
|
|
|
$rootContent=$this->instance->getFolder('');
|
2020-03-26 11:30:18 +03:00
|
|
|
$expected=['lorem.txt','logo-wide.png', 'dir/'];
|
2013-01-24 19:47:17 +04:00
|
|
|
$this->assertEquals(3, count($rootContent));
|
2012-09-07 17:22:01 +04:00
|
|
|
foreach($expected as $file) {
|
2012-10-06 00:24:36 +04:00
|
|
|
$this->assertContains($file, $rootContent, 'cant find '. $file . ' in archive');
|
2012-03-03 03:57:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
$dirContent=$this->instance->getFolder('dir/');
|
2020-03-26 11:30:18 +03:00
|
|
|
$expected=['lorem.txt'];
|
2013-01-24 19:47:17 +04:00
|
|
|
$this->assertEquals(1, count($dirContent));
|
2012-09-07 17:22:01 +04:00
|
|
|
foreach($expected as $file) {
|
2012-10-06 00:24:36 +04:00
|
|
|
$this->assertContains($file, $dirContent, 'cant find '. $file . ' in archive');
|
2012-03-03 03:57:03 +04:00
|
|
|
}
|
|
|
|
}
|
2012-10-06 00:24:36 +04:00
|
|
|
|
2012-09-07 17:22:01 +04:00
|
|
|
public function testContent() {
|
2012-03-03 03:57:03 +04:00
|
|
|
$this->instance=$this->getExisting();
|
2016-05-18 19:46:03 +03:00
|
|
|
$dir=\OC::$SERVERROOT.'/tests/data';
|
2012-03-03 03:57:03 +04:00
|
|
|
$textFile=$dir.'/lorem.txt';
|
2013-01-24 19:47:17 +04:00
|
|
|
$this->assertEquals(file_get_contents($textFile), $this->instance->getFile('lorem.txt'));
|
2012-10-06 00:24:36 +04:00
|
|
|
|
2018-03-19 12:52:35 +03:00
|
|
|
$tmpFile = \OC::$server->getTempManager()->getTemporaryFile('.txt');
|
2012-11-02 22:53:02 +04:00
|
|
|
$this->instance->extractFile('lorem.txt', $tmpFile);
|
2013-01-24 19:47:17 +04:00
|
|
|
$this->assertEquals(file_get_contents($textFile), file_get_contents($tmpFile));
|
2012-03-03 03:57:03 +04:00
|
|
|
}
|
|
|
|
|
2012-09-07 17:22:01 +04:00
|
|
|
public function testWrite() {
|
2016-05-18 19:46:03 +03:00
|
|
|
$dir=\OC::$SERVERROOT.'/tests/data';
|
2012-03-03 03:57:03 +04:00
|
|
|
$textFile=$dir.'/lorem.txt';
|
|
|
|
$this->instance=$this->getNew();
|
2013-01-24 19:47:17 +04:00
|
|
|
$this->assertEquals(0, count($this->instance->getFiles()));
|
2012-11-02 22:53:02 +04:00
|
|
|
$this->instance->addFile('lorem.txt', $textFile);
|
2013-01-24 19:47:17 +04:00
|
|
|
$this->assertEquals(1, count($this->instance->getFiles()));
|
2012-03-03 03:57:03 +04:00
|
|
|
$this->assertTrue($this->instance->fileExists('lorem.txt'));
|
2012-03-29 01:46:44 +04:00
|
|
|
$this->assertFalse($this->instance->fileExists('lorem.txt/'));
|
2012-10-06 00:24:36 +04:00
|
|
|
|
2013-01-24 19:47:17 +04:00
|
|
|
$this->assertEquals(file_get_contents($textFile), $this->instance->getFile('lorem.txt'));
|
2012-11-04 14:10:46 +04:00
|
|
|
$this->instance->addFile('lorem.txt', 'foobar');
|
2013-01-24 19:47:17 +04:00
|
|
|
$this->assertEquals('foobar', $this->instance->getFile('lorem.txt'));
|
2012-03-03 03:57:03 +04:00
|
|
|
}
|
|
|
|
|
2012-09-07 17:22:01 +04:00
|
|
|
public function testReadStream() {
|
2016-05-18 19:46:03 +03:00
|
|
|
$dir=\OC::$SERVERROOT.'/tests/data';
|
2012-03-03 03:57:03 +04:00
|
|
|
$this->instance=$this->getExisting();
|
2012-11-04 14:10:46 +04:00
|
|
|
$fh=$this->instance->getStream('lorem.txt', 'r');
|
2013-01-24 19:47:17 +04:00
|
|
|
$this->assertTrue((bool)$fh);
|
2012-11-02 22:53:02 +04:00
|
|
|
$content=fread($fh, $this->instance->filesize('lorem.txt'));
|
2012-03-03 03:57:03 +04:00
|
|
|
fclose($fh);
|
2013-01-24 19:47:17 +04:00
|
|
|
$this->assertEquals(file_get_contents($dir.'/lorem.txt'), $content);
|
2012-03-03 03:57:03 +04:00
|
|
|
}
|
2012-09-07 17:22:01 +04:00
|
|
|
public function testWriteStream() {
|
2016-05-18 19:46:03 +03:00
|
|
|
$dir=\OC::$SERVERROOT.'/tests/data';
|
2012-03-03 03:57:03 +04:00
|
|
|
$this->instance=$this->getNew();
|
2012-11-04 14:10:46 +04:00
|
|
|
$fh=$this->instance->getStream('lorem.txt', 'w');
|
|
|
|
$source=fopen($dir.'/lorem.txt', 'r');
|
2016-05-18 19:46:03 +03: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'));
|
2013-01-24 19:47:17 +04:00
|
|
|
$this->assertEquals(file_get_contents($dir.'/lorem.txt'), $this->instance->getFile('lorem.txt'));
|
2012-03-03 03:57:03 +04:00
|
|
|
}
|
2012-09-07 17:22:01 +04:00
|
|
|
public function testFolder() {
|
2012-03-29 01:46:44 +04:00
|
|
|
$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-09-07 17:22:01 +04:00
|
|
|
public function testExtract() {
|
2016-05-18 19:46:03 +03:00
|
|
|
$dir=\OC::$SERVERROOT.'/tests/data';
|
2012-03-29 00:31:45 +04:00
|
|
|
$this->instance=$this->getExisting();
|
2018-03-19 12:54:39 +03:00
|
|
|
$tmpDir = \OC::$server->getTempManager()->getTemporaryFolder();
|
2012-03-29 00:31:45 +04:00
|
|
|
$this->instance->extract($tmpDir);
|
2013-01-24 19:47:17 +04:00
|
|
|
$this->assertEquals(true, file_exists($tmpDir.'lorem.txt'));
|
|
|
|
$this->assertEquals(true, file_exists($tmpDir.'dir/lorem.txt'));
|
|
|
|
$this->assertEquals(true, file_exists($tmpDir.'logo-wide.png'));
|
|
|
|
$this->assertEquals(file_get_contents($dir.'/lorem.txt'), file_get_contents($tmpDir.'lorem.txt'));
|
2016-05-18 19:46:03 +03:00
|
|
|
\OCP\Files::rmdirr($tmpDir);
|
2012-03-29 00:31:45 +04:00
|
|
|
}
|
2012-09-07 17:22:01 +04:00
|
|
|
public function testMoveRemove() {
|
2016-05-18 19:46:03 +03:00
|
|
|
$dir=\OC::$SERVERROOT.'/tests/data';
|
2012-03-29 01:46:44 +04:00
|
|
|
$textFile=$dir.'/lorem.txt';
|
|
|
|
$this->instance=$this->getNew();
|
2012-11-02 22:53:02 +04:00
|
|
|
$this->instance->addFile('lorem.txt', $textFile);
|
2012-03-29 01:46:44 +04:00
|
|
|
$this->assertFalse($this->instance->fileExists('target.txt'));
|
2012-11-04 14:10:46 +04:00
|
|
|
$this->instance->rename('lorem.txt', 'target.txt');
|
2012-03-29 01:46:44 +04:00
|
|
|
$this->assertTrue($this->instance->fileExists('target.txt'));
|
|
|
|
$this->assertFalse($this->instance->fileExists('lorem.txt'));
|
2013-01-24 19:47:17 +04:00
|
|
|
$this->assertEquals(file_get_contents($textFile), $this->instance->getFile('target.txt'));
|
2012-03-29 01:46:44 +04:00
|
|
|
$this->instance->remove('target.txt');
|
|
|
|
$this->assertFalse($this->instance->fileExists('target.txt'));
|
|
|
|
}
|
2012-09-07 17:22:01 +04:00
|
|
|
public function testRecursive() {
|
2016-05-18 19:46:03 +03:00
|
|
|
$dir=\OC::$SERVERROOT.'/tests/data';
|
2012-08-18 03:17:28 +04:00
|
|
|
$this->instance=$this->getNew();
|
2012-11-02 22:53:02 +04:00
|
|
|
$this->instance->addRecursive('/dir', $dir);
|
2012-08-18 03:17:28 +04:00
|
|
|
$this->assertTrue($this->instance->fileExists('/dir/lorem.txt'));
|
|
|
|
$this->assertTrue($this->instance->fileExists('/dir/data.zip'));
|
|
|
|
$this->assertTrue($this->instance->fileExists('/dir/data.tar.gz'));
|
|
|
|
}
|
2012-03-03 03:57:03 +04:00
|
|
|
}
|