diff options
Diffstat (limited to 'apps/files_versions')
-rw-r--r-- | apps/files_versions/l10n/lb.js | 11 | ||||
-rw-r--r-- | apps/files_versions/l10n/lb.json | 9 | ||||
-rw-r--r-- | apps/files_versions/lib/storage.php | 32 | ||||
-rw-r--r-- | apps/files_versions/tests/versions.php | 113 |
4 files changed, 159 insertions, 6 deletions
diff --git a/apps/files_versions/l10n/lb.js b/apps/files_versions/l10n/lb.js new file mode 100644 index 00000000000..1678cad569d --- /dev/null +++ b/apps/files_versions/l10n/lb.js @@ -0,0 +1,11 @@ +OC.L10N.register( + "files_versions", + { + "Could not revert: %s" : "Konnt net zrécksetzen: %s", + "Versions" : "Versiounen", + "Failed to revert {file} to revision {timestamp}." : "Konnt {file} net op d'Versioun {timestamp} zrécksetzen.", + "More versions..." : "Méi Versiounen...", + "No other versions available" : "Keng aner Versiounen disponibel", + "Restore" : "Zrécksetzen" +}, +"nplurals=2; plural=(n != 1);"); diff --git a/apps/files_versions/l10n/lb.json b/apps/files_versions/l10n/lb.json new file mode 100644 index 00000000000..e5fbb6c220c --- /dev/null +++ b/apps/files_versions/l10n/lb.json @@ -0,0 +1,9 @@ +{ "translations": { + "Could not revert: %s" : "Konnt net zrécksetzen: %s", + "Versions" : "Versiounen", + "Failed to revert {file} to revision {timestamp}." : "Konnt {file} net op d'Versioun {timestamp} zrécksetzen.", + "More versions..." : "Méi Versiounen...", + "No other versions available" : "Keng aner Versiounen disponibel", + "Restore" : "Zrécksetzen" +},"pluralForm" :"nplurals=2; plural=(n != 1);" +}
\ No newline at end of file diff --git a/apps/files_versions/lib/storage.php b/apps/files_versions/lib/storage.php index 5879cb01641..45f96ee776b 100644 --- a/apps/files_versions/lib/storage.php +++ b/apps/files_versions/lib/storage.php @@ -282,11 +282,16 @@ class Storage { } /** - * rollback to an old version of a file. + * Rollback to an old version of a file. + * + * @param string $file file name + * @param int $revision revision timestamp */ public static function rollback($file, $revision) { if(\OCP\Config::getSystemValue('files_versions', Storage::DEFAULTENABLED)=='true') { + // add expected leading slash + $file = '/' . ltrim($file, '/'); list($uid, $filename) = self::getUidAndFilename($file); $users_view = new \OC\Files\View('/'.$uid); $files_view = new \OC\Files\View('/'.\OCP\User::getUser().'/files'); @@ -302,12 +307,11 @@ class Storage { } // rollback - if( @$users_view->rename('files_versions'.$filename.'.v'.$revision, 'files'.$filename) ) { + if (self::copyFileContents($users_view, 'files_versions' . $filename . '.v' . $revision, 'files' . $filename)) { $files_view->touch($file, $revision); Storage::scheduleExpire($file); return true; - - }else if ( $versionCreated ) { + } else if ($versionCreated) { self::deleteVersion($users_view, $version); } } @@ -315,6 +319,23 @@ class Storage { } + /** + * Stream copy file contents from $path1 to $path2 + * + * @param \OC\Files\View $view view to use for copying + * @param string $path1 source file to copy + * @param string $path2 target file + * + * @return bool true for success, false otherwise + */ + private static function copyFileContents($view, $path1, $path2) { + list($storage1, $internalPath1) = $view->resolvePath($path1); + list($storage2, $internalPath2) = $view->resolvePath($path2); + + $result = $storage2->moveFromStorage($storage1, $internalPath1, $internalPath2); + + return ($result !== false); + } /** * get a list of all available versions of a file in descending chronological order @@ -325,6 +346,9 @@ class Storage { */ public static function getVersions($uid, $filename, $userFullPath = '') { $versions = array(); + if ($filename === '') { + return $versions; + } // fetch for old versions $view = new \OC\Files\View('/' . $uid . '/'); diff --git a/apps/files_versions/tests/versions.php b/apps/files_versions/tests/versions.php index a7d54d09f6a..2d3e2b66e06 100644 --- a/apps/files_versions/tests/versions.php +++ b/apps/files_versions/tests/versions.php @@ -25,6 +25,8 @@ require_once __DIR__ . '/../appinfo/app.php'; +use OC\Files\Storage\Temporary; + /** * Class Test_Files_versions * this class provide basic files versions test @@ -47,7 +49,9 @@ class Test_Files_Versioning extends \Test\TestCase { \OC_Hook::clear('OCP\\Share'); \OC::registerShareHooks(); \OCA\Files_Versions\Hooks::connectHooks(); - \OCP\Util::connectHook('OC_Filesystem', 'setup', '\OC\Files\Storage\Shared', 'setup'); + $application = new \OCA\Files_Sharing\AppInfo\Application(); + $application->registerMountProviders(); + $application->setupPropagation(); // create test user self::loginHelper(self::TEST_VERSIONS_USER2, true); @@ -527,7 +531,7 @@ class Test_Files_Versioning extends \Test\TestCase { // execute copy hook of versions app $versions = \OCA\Files_Versions\Storage::getVersions(self::TEST_VERSIONS_USER, '/subfolder/test.txt'); - $this->assertSame(2, count($versions)); + $this->assertCount(2, $versions); foreach ($versions as $version) { $this->assertSame('/subfolder/test.txt', $version['path']); @@ -539,6 +543,111 @@ class Test_Files_Versioning extends \Test\TestCase { } /** + * test if we find all versions and if the versions array contain + * the correct 'path' and 'name' + */ + public function testGetVersionsEmptyFile() { + // execute copy hook of versions app + $versions = \OCA\Files_Versions\Storage::getVersions(self::TEST_VERSIONS_USER, ''); + + $this->assertCount(0, $versions); + } + + public function testRestoreSameStorage() { + \OC\Files\Filesystem::mkdir('sub'); + $this->doTestRestore(); + } + + public function testRestoreCrossStorage() { + $storage2 = new Temporary(array()); + \OC\Files\Filesystem::mount($storage2, array(), self::TEST_VERSIONS_USER . '/files/sub'); + + $this->doTestRestore(); + } + + private function doTestRestore() { + $filePath = self::TEST_VERSIONS_USER . '/files/sub/test.txt'; + $this->rootView->file_put_contents($filePath, 'test file'); + + $t0 = $this->rootView->filemtime($filePath); + + // not exactly the same timestamp as the file + $t1 = time() - 60; + // second version is two weeks older + $t2 = $t1 - 60 * 60 * 24 * 14; + + // create some versions + $v1 = self::USERS_VERSIONS_ROOT . '/sub/test.txt.v' . $t1; + $v2 = self::USERS_VERSIONS_ROOT . '/sub/test.txt.v' . $t2; + + $this->rootView->mkdir(self::USERS_VERSIONS_ROOT . '/sub'); + $this->rootView->file_put_contents($v1, 'version1'); + $this->rootView->file_put_contents($v2, 'version2'); + + $oldVersions = \OCA\Files_Versions\Storage::getVersions( + self::TEST_VERSIONS_USER, '/sub/test.txt' + ); + + $this->assertCount(2, $oldVersions); + + $this->assertEquals('test file', $this->rootView->file_get_contents($filePath)); + $info1 = $this->rootView->getFileInfo($filePath); + + \OCA\Files_Versions\Storage::rollback('sub/test.txt', $t2); + + $this->assertEquals('version2', $this->rootView->file_get_contents($filePath)); + $info2 = $this->rootView->getFileInfo($filePath); + + $this->assertNotEquals( + $info2['etag'], + $info1['etag'], + 'Etag must change after rolling back version' + ); + $this->assertEquals( + $info2['fileid'], + $info1['fileid'], + 'File id must not change after rolling back version' + ); + $this->assertEquals( + $info2['mtime'], + $t2, + 'Restored file has mtime from version' + ); + + $newVersions = \OCA\Files_Versions\Storage::getVersions( + self::TEST_VERSIONS_USER, '/sub/test.txt' + ); + + $this->assertTrue( + $this->rootView->file_exists(self::USERS_VERSIONS_ROOT . '/sub/test.txt.v' . $t0), + 'A version file was created for the file before restoration' + ); + $this->assertTrue( + $this->rootView->file_exists($v1), + 'Untouched version file is still there' + ); + $this->assertFalse( + $this->rootView->file_exists($v2), + 'Restored version file gone from files_version folder' + ); + + $this->assertCount(2, $newVersions, 'Additional version created'); + + $this->assertTrue( + isset($newVersions[$t0 . '#' . 'test.txt']), + 'A version was created for the file before restoration' + ); + $this->assertTrue( + isset($newVersions[$t1 . '#' . 'test.txt']), + 'Untouched version is still there' + ); + $this->assertFalse( + isset($newVersions[$t2 . '#' . 'test.txt']), + 'Restored version is not in the list any more' + ); + } + + /** * @param string $user * @param bool $create * @param bool $password |