diff options
author | Florin Peter <github@florin-peter.de> | 2013-05-26 03:22:16 +0200 |
---|---|---|
committer | Florin Peter <github@florin-peter.de> | 2013-05-26 03:22:16 +0200 |
commit | c7981abbc902ece98ceaad72e475ec5515cd26a7 (patch) | |
tree | 4d68b3e09d059740f4b0a116f943fe3c2773ec6e /apps/files_encryption/tests/trashbin.php | |
parent | 0c621ff6a93fe1b34c77257d83fd344489f59bab (diff) | |
download | nextcloud-server-c7981abbc902ece98ceaad72e475ec5515cd26a7.tar.gz nextcloud-server-c7981abbc902ece98ceaad72e475ec5515cd26a7.zip |
improved test
- fixed testPermanentDeleteFile sometimes failed
- speed optimization
- reformat code
Diffstat (limited to 'apps/files_encryption/tests/trashbin.php')
-rwxr-xr-x | apps/files_encryption/tests/trashbin.php | 193 |
1 files changed, 95 insertions, 98 deletions
diff --git a/apps/files_encryption/tests/trashbin.php b/apps/files_encryption/tests/trashbin.php index cc8709b6f24..1d7cdc60b21 100755 --- a/apps/files_encryption/tests/trashbin.php +++ b/apps/files_encryption/tests/trashbin.php @@ -20,14 +20,14 @@ * */ -require_once realpath(dirname(__FILE__) . '/../../../lib/base.php'); -require_once realpath(dirname(__FILE__) . '/../lib/crypt.php'); -require_once realpath(dirname(__FILE__) . '/../lib/keymanager.php'); -require_once realpath(dirname(__FILE__) . '/../lib/proxy.php'); -require_once realpath(dirname(__FILE__) . '/../lib/stream.php'); -require_once realpath(dirname(__FILE__) . '/../lib/util.php'); -require_once realpath(dirname(__FILE__) . '/../appinfo/app.php'); -require_once realpath(dirname(__FILE__) . '/../../files_trashbin/appinfo/app.php'); +require_once realpath( dirname( __FILE__ ) . '/../../../lib/base.php' ); +require_once realpath( dirname( __FILE__ ) . '/../lib/crypt.php' ); +require_once realpath( dirname( __FILE__ ) . '/../lib/keymanager.php' ); +require_once realpath( dirname( __FILE__ ) . '/../lib/proxy.php' ); +require_once realpath( dirname( __FILE__ ) . '/../lib/stream.php' ); +require_once realpath( dirname( __FILE__ ) . '/../lib/util.php' ); +require_once realpath( dirname( __FILE__ ) . '/../appinfo/app.php' ); +require_once realpath( dirname( __FILE__ ) . '/../../files_trashbin/appinfo/app.php' ); use OCA\Encryption; @@ -50,18 +50,45 @@ class Test_Encryption_Trashbin extends \PHPUnit_Framework_TestCase public $subfolder; public $subsubfolder; - function setUp() - { + public static function setUpBeforeClass() { // reset backend - \OC_User::useBackend('database'); + \OC_User::clearBackends(); + \OC_User::useBackend( 'database' ); + \OC_Hook::clear( 'OC_Filesystem' ); + \OC_Hook::clear( 'OC_User' ); + + // trashbin hooks + \OCA\Files_Trashbin\Trashbin::registerHooks(); + + // Filesystem related hooks + \OCA\Encryption\Helper::registerFilesystemHooks(); + + // clear and register hooks + \OC_FileProxy::clearProxies(); + \OC_FileProxy::register( new OCA\Encryption\Proxy() ); + + // setup filesystem + \OC_Util::tearDownFS(); + \OC_User::setUserId( '' ); + \OC\Files\Filesystem::tearDown(); + \OC_Util::setupFS( 'admin' ); + \OC_User::setUserId( 'admin' ); + + // login admin + $params['uid'] = 'admin'; + $params['password'] = 'admin'; + OCA\Encryption\Hooks::login( $params ); + } + + function setUp() { // set user id - \OC_User::setUserId('admin'); + \OC_User::setUserId( 'admin' ); $this->userId = 'admin'; $this->pass = 'admin'; // init filesystem view - $this->view = new \OC_FilesystemView('/'); + $this->view = new \OC_FilesystemView( '/' ); // init short data $this->dataShort = 'hats'; @@ -70,48 +97,24 @@ class Test_Encryption_Trashbin extends \PHPUnit_Framework_TestCase $this->subfolder = '/subfolder1'; $this->subsubfolder = '/subsubfolder1'; - \OC_Hook::clear('OC_Filesystem'); - \OC_Hook::clear('OC_User'); - - // init filesystem related hooks - \OCA\Encryption\Helper::registerFilesystemHooks(); - - // register encryption file proxy - \OC_FileProxy::register(new OCA\Encryption\Proxy()); - - // trashbin hooks - \OCA\Files_Trashbin\Trashbin::registerHooks(); - // remember files_trashbin state - $this->stateFilesTrashbin = OC_App::isEnabled('files_trashbin'); - - // we don't want to tests with app files_trashbin enabled - \OC_App::enable('files_trashbin'); + $this->stateFilesTrashbin = OC_App::isEnabled( 'files_trashbin' ); - // init filesystem for user - \OC_Util::tearDownFS(); - \OC_User::setUserId(''); - \OC\Files\Filesystem::tearDown(); - \OC_Util::setupFS($this->userId); - \OC_User::setUserId($this->userId); - - // login user - $params['uid'] = $this->userId; - $params['password'] = $this->pass; - OCA\Encryption\Hooks::login($params); + // we want to tests with app files_trashbin enabled + \OC_App::enable( 'files_trashbin' ); } - function tearDown() - { + function tearDown() { // reset app files_trashbin - if ($this->stateFilesTrashbin) { - OC_App::enable('files_trashbin'); + if ( $this->stateFilesTrashbin ) { + OC_App::enable( 'files_trashbin' ); } else { - OC_App::disable('files_trashbin'); + OC_App::disable( 'files_trashbin' ); } + } + + public static function tearDownAfterClass() { - // clear all proxies - \OC_FileProxy::clearProxies(); } /** @@ -123,49 +126,49 @@ class Test_Encryption_Trashbin extends \PHPUnit_Framework_TestCase $filename = 'tmp-' . time() . '.txt'; // save file with content - $cryptedFile = file_put_contents('crypt:///' . $filename, $this->dataShort); + $cryptedFile = file_put_contents( 'crypt:///' . $filename, $this->dataShort ); // test that data was successfully written - $this->assertTrue(is_int($cryptedFile)); + $this->assertTrue( is_int( $cryptedFile ) ); // check if key for admin exists - $this->assertTrue($this->view->file_exists('/admin/files_encryption/keyfiles/' . $filename . '.key')); + $this->assertTrue( $this->view->file_exists( '/admin/files_encryption/keyfiles/' . $filename . '.key' ) ); // check if share key for admin exists - $this->assertTrue($this->view->file_exists('/admin/files_encryption/share-keys/' . $filename . '.admin.shareKey')); + $this->assertTrue( $this->view->file_exists( '/admin/files_encryption/share-keys/' . $filename . '.admin.shareKey' ) ); // delete file - \OC\FIles\Filesystem::unlink($filename); + \OC\FIles\Filesystem::unlink( $filename ); // check if file not exists - $this->assertFalse($this->view->file_exists('/admin/files/' . $filename)); + $this->assertFalse( $this->view->file_exists( '/admin/files/' . $filename ) ); // check if key for admin not exists - $this->assertFalse($this->view->file_exists('/admin/files_encryption/keyfiles/' . $filename . '.key')); + $this->assertFalse( $this->view->file_exists( '/admin/files_encryption/keyfiles/' . $filename . '.key' ) ); // check if share key for admin not exists - $this->assertFalse($this->view->file_exists('/admin/files_encryption/share-keys/' . $filename . '.admin.shareKey')); + $this->assertFalse( $this->view->file_exists( '/admin/files_encryption/share-keys/' . $filename . '.admin.shareKey' ) ); // get files - $trashFiles = $this->view->getDirectoryContent('/admin/files_trashbin/files/'); + $trashFiles = $this->view->getDirectoryContent( '/admin/files_trashbin/files/' ); $trashFileSuffix = null; // find created file with timestamp - foreach($trashFiles as $file) { - if(strncmp($file['path'], $filename, strlen($filename))) { - $path_parts = pathinfo($file['name']); + foreach ( $trashFiles as $file ) { + if ( strncmp( $file['path'], $filename, strlen( $filename ) ) ) { + $path_parts = pathinfo( $file['name'] ); $trashFileSuffix = $path_parts['extension']; } } // check if we found the file we created - $this->assertNotNull($trashFileSuffix); + $this->assertNotNull( $trashFileSuffix ); // check if key for admin not exists - $this->assertTrue($this->view->file_exists('/admin/files_trashbin/keyfiles/' . $filename . '.key.' . $trashFileSuffix)); + $this->assertTrue( $this->view->file_exists( '/admin/files_trashbin/keyfiles/' . $filename . '.key.' . $trashFileSuffix ) ); // check if share key for admin not exists - $this->assertTrue($this->view->file_exists('/admin/files_trashbin/share-keys/' . $filename . '.admin.shareKey.' . $trashFileSuffix)); + $this->assertTrue( $this->view->file_exists( '/admin/files_trashbin/share-keys/' . $filename . '.admin.shareKey.' . $trashFileSuffix ) ); // return filename for next test return $filename . '.' . $trashFileSuffix; @@ -176,25 +179,25 @@ class Test_Encryption_Trashbin extends \PHPUnit_Framework_TestCase * * @depends testDeleteFile */ - function testRestoreFile($filename) { + function testRestoreFile( $filename ) { // prepare file information - $path_parts = pathinfo($filename); + $path_parts = pathinfo( $filename ); $trashFileSuffix = $path_parts['extension']; - $timestamp = str_replace('d', '', $trashFileSuffix); - $fileNameWithoutSuffix = str_replace('.'.$trashFileSuffix, '', $filename); + $timestamp = str_replace( 'd', '', $trashFileSuffix ); + $fileNameWithoutSuffix = str_replace( '.' . $trashFileSuffix, '', $filename ); // restore file - $this->assertTrue(\OCA\Files_Trashbin\Trashbin::restore($filename, $fileNameWithoutSuffix, $timestamp)); + $this->assertTrue( \OCA\Files_Trashbin\Trashbin::restore( $filename, $fileNameWithoutSuffix, $timestamp ) ); // check if file exists - $this->assertTrue($this->view->file_exists('/admin/files/' . $fileNameWithoutSuffix)); + $this->assertTrue( $this->view->file_exists( '/admin/files/' . $fileNameWithoutSuffix ) ); // check if key for admin exists - $this->assertTrue($this->view->file_exists('/admin/files_encryption/keyfiles/' . $fileNameWithoutSuffix . '.key')); + $this->assertTrue( $this->view->file_exists( '/admin/files_encryption/keyfiles/' . $fileNameWithoutSuffix . '.key' ) ); // check if share key for admin exists - $this->assertTrue($this->view->file_exists('/admin/files_encryption/share-keys/' . $fileNameWithoutSuffix . '.admin.shareKey')); + $this->assertTrue( $this->view->file_exists( '/admin/files_encryption/share-keys/' . $fileNameWithoutSuffix . '.admin.shareKey' ) ); } /** @@ -206,65 +209,59 @@ class Test_Encryption_Trashbin extends \PHPUnit_Framework_TestCase $filename = 'tmp-' . time() . '.txt'; // save file with content - $cryptedFile = file_put_contents('crypt:///' . $filename, $this->dataShort); + $cryptedFile = file_put_contents( 'crypt:///' . $filename, $this->dataShort ); // test that data was successfully written - $this->assertTrue(is_int($cryptedFile)); + $this->assertTrue( is_int( $cryptedFile ) ); // check if key for admin exists - $this->assertTrue($this->view->file_exists('/admin/files_encryption/keyfiles/' . $filename . '.key')); + $this->assertTrue( $this->view->file_exists( '/admin/files_encryption/keyfiles/' . $filename . '.key' ) ); // check if share key for admin exists - $this->assertTrue($this->view->file_exists('/admin/files_encryption/share-keys/' . $filename . '.admin.shareKey')); + $this->assertTrue( $this->view->file_exists( '/admin/files_encryption/share-keys/' . $filename . '.admin.shareKey' ) ); // delete file - \OC\FIles\Filesystem::unlink($filename); + \OC\FIles\Filesystem::unlink( $filename ); // check if file not exists - $this->assertFalse($this->view->file_exists('/admin/files/' . $filename)); + $this->assertFalse( $this->view->file_exists( '/admin/files/' . $filename ) ); // check if key for admin not exists - $this->assertFalse($this->view->file_exists('/admin/files_encryption/keyfiles/' . $filename . '.key')); + $this->assertFalse( $this->view->file_exists( '/admin/files_encryption/keyfiles/' . $filename . '.key' ) ); // check if share key for admin not exists - $this->assertFalse($this->view->file_exists('/admin/files_encryption/share-keys/' . $filename . '.admin.shareKey')); + $this->assertFalse( $this->view->file_exists( '/admin/files_encryption/share-keys/' . $filename . '.admin.shareKey' ) ); - // get files - $trashFiles = $this->view->getDirectoryContent('/admin/files_trashbin/files/'); - - $trashFileSuffix = null; // find created file with timestamp - foreach($trashFiles as $file) { - if(strncmp($file['name'], $filename, strlen($filename)) == 0) { - $path_parts = pathinfo($file['name']); - $trashFileSuffix = $path_parts['extension']; - break; - } - } + $query = \OC_DB::prepare( 'SELECT `timestamp`,`type` FROM `*PREFIX*files_trash`' + . ' WHERE `id`=?' ); + $result = $query->execute( array( $filename ) )->fetchRow(); - // check if we found the file we created - $this->assertNotNull($trashFileSuffix); + $this->assertTrue( is_array( $result ) ); + + // build suffix + $trashFileSuffix = 'd' . $result['timestamp']; // check if key for admin exists - $this->assertTrue($this->view->file_exists('/admin/files_trashbin/keyfiles/' . $filename . '.key.' . $trashFileSuffix)); + $this->assertTrue( $this->view->file_exists( '/admin/files_trashbin/keyfiles/' . $filename . '.key.' . $trashFileSuffix ) ); // check if share key for admin exists - $this->assertTrue($this->view->file_exists('/admin/files_trashbin/share-keys/' . $filename . '.admin.shareKey.' . $trashFileSuffix)); + $this->assertTrue( $this->view->file_exists( '/admin/files_trashbin/share-keys/' . $filename . '.admin.shareKey.' . $trashFileSuffix ) ); // get timestamp from file - $timestamp = str_replace('d', '', $trashFileSuffix); + $timestamp = str_replace( 'd', '', $trashFileSuffix ); // delete file forever - $this->assertGreaterThan(0, \OCA\Files_Trashbin\Trashbin::delete($filename, $timestamp)); + $this->assertGreaterThan( 0, \OCA\Files_Trashbin\Trashbin::delete( $filename, $timestamp ) ); // check if key for admin not exists - $this->assertFalse($this->view->file_exists('/admin/files_trashbin/files/' . $filename . '.' . $trashFileSuffix)); + $this->assertFalse( $this->view->file_exists( '/admin/files_trashbin/files/' . $filename . '.' . $trashFileSuffix ) ); // check if key for admin not exists - $this->assertFalse($this->view->file_exists('/admin/files_trashbin/keyfiles/' . $filename . '.key.' . $trashFileSuffix)); + $this->assertFalse( $this->view->file_exists( '/admin/files_trashbin/keyfiles/' . $filename . '.key.' . $trashFileSuffix ) ); // check if share key for admin not exists - $this->assertFalse($this->view->file_exists('/admin/files_trashbin/share-keys/' . $filename . '.admin.shareKey.' . $trashFileSuffix)); + $this->assertFalse( $this->view->file_exists( '/admin/files_trashbin/share-keys/' . $filename . '.admin.shareKey.' . $trashFileSuffix ) ); } }
\ No newline at end of file |