diff options
author | Morris Jobke <morris.jobke@gmail.com> | 2014-02-20 15:21:53 +0100 |
---|---|---|
committer | Morris Jobke <morris.jobke@gmail.com> | 2014-03-13 13:09:32 +0100 |
commit | ec54bc77093265b2b9f03eecc1507cb266683624 (patch) | |
tree | d8492d1bf4edba95aa48bd859e788c4313cb4ee3 | |
parent | d58fda7ff3e8f67e7699123eb4cca8248484c9ea (diff) | |
download | nextcloud-server-ec54bc77093265b2b9f03eecc1507cb266683624.tar.gz nextcloud-server-ec54bc77093265b2b9f03eecc1507cb266683624.zip |
Refactor update script to class and add unit test
-rw-r--r-- | apps/files_sharing/appinfo/update.php | 14 | ||||
-rw-r--r-- | apps/files_sharing/lib/updater.php | 22 | ||||
-rw-r--r-- | apps/files_sharing/tests/updater.php | 85 |
3 files changed, 108 insertions, 13 deletions
diff --git a/apps/files_sharing/appinfo/update.php b/apps/files_sharing/appinfo/update.php index 446c94ea540..ab32108ea25 100644 --- a/apps/files_sharing/appinfo/update.php +++ b/apps/files_sharing/appinfo/update.php @@ -72,17 +72,5 @@ if (version_compare($installedVersion, '0.3', '<')) { // clean up oc_share table from files which are no longer exists if (version_compare($installedVersion, '0.3.5.6', '<')) { - // delete all shares where the original file no longer exists - $findShares = \OC_DB::prepare('SELECT `*PREFIX*share`.`id` ' . - 'FROM `*PREFIX*share` LEFT JOIN `*PREFIX*filecache` ON `file_source` = `*PREFIX*filecache`.`fileid` ' . - 'WHERE `*PREFIX*filecache`.`fileid` IS NULL AND `*PREFIX*share`.`item_type` IN (\'file\', \'folder\')'); - $sharesFound = $findShares->execute(array())->fetchAll(); - - // delete those shares from the oc_share table - if (is_array($sharesFound) && !empty($sharesFound)) { - $removeShares = \OC_DB::prepare('DELETE FROM `*PREFIX*share` WHERE `id` = ? '); - foreach ($sharesFound as $share) { - $result = $removeShares->execute(array($share['id'])); - } - } + \OC\Files\Cache\Shared_Updater::fixBrokenSharesOnAppUpdate(); } diff --git a/apps/files_sharing/lib/updater.php b/apps/files_sharing/lib/updater.php index 23ebc9fb811..950575ec26b 100644 --- a/apps/files_sharing/lib/updater.php +++ b/apps/files_sharing/lib/updater.php @@ -135,4 +135,26 @@ class Shared_Updater { } } + /** + * clean up oc_share table from files which are no longer exists + * + * This fixes issues from updates from files_sharing < 0.3.5.6 (ownCloud 4.5) + * It will just be called during the update of the app + */ + static public function fixBrokenSharesOnAppUpdate() { + // delete all shares where the original file no longer exists + $findShares = \OC_DB::prepare('SELECT `*PREFIX*share`.`id` ' . + 'FROM `*PREFIX*share` LEFT JOIN `*PREFIX*filecache` ON `file_source` = `*PREFIX*filecache`.`fileid` ' . + 'WHERE `*PREFIX*filecache`.`fileid` IS NULL AND `*PREFIX*share`.`item_type` IN (\'file\', \'folder\')'); + $sharesFound = $findShares->execute(array())->fetchAll(); + + // delete those shares from the oc_share table + if (is_array($sharesFound) && !empty($sharesFound)) { + $removeShares = \OC_DB::prepare('DELETE FROM `*PREFIX*share` WHERE `id` = ? '); + foreach ($sharesFound as $share) { + $result = $removeShares->execute(array($share['id'])); + } + } + } + } diff --git a/apps/files_sharing/tests/updater.php b/apps/files_sharing/tests/updater.php new file mode 100644 index 00000000000..f369062c229 --- /dev/null +++ b/apps/files_sharing/tests/updater.php @@ -0,0 +1,85 @@ +<?php +/** + * ownCloud + * + * @author Morris Jobke + * @copyright 2014 Morris Jobke <morris.jobke@gmail.com> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE + * License as published by the Free Software Foundation; either + * version 3 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU AFFERO GENERAL PUBLIC LICENSE for more details. + * + * You should have received a copy of the GNU Affero General Public + * License along with this library. If not, see <http://www.gnu.org/licenses/>. + * + */ + +/** + * Class Test_Files_Sharing_Updater + */ +class Test_Files_Sharing_Updater extends \PHPUnit_Framework_TestCase { + + function setUp() { + $addShares = \OC_DB::prepare('INSERT INTO `*PREFIX*share` (file_source, id, item_type, uid_owner) VALUES (?, ?, \'file\', 1)'); + $shares = array(1, 2, 3); + foreach($shares as $share) { + // the number is used as item_source and id + $addShares->execute(array($share, $share)); + } + // add items except one - because this is the test case for the broken share table + $addItems = \OC_DB::prepare('INSERT INTO `*PREFIX*filecache` (fileid, storage, path_hash, ' . + 'parent, mimetype, mimepart, size, mtime, storage_mtime) ' . + 'VALUES (?, 1, ?, 1, 1, 1, 1, 1, 1)'); + $items = array(1, 3); + foreach($items as $item) { + // the number is used as file_id and path_hash + $addItems->execute(array($item, $item)); + } + } + + function tearDown() { + $removeShares = \OC_DB::prepare('DELETE FROM `*PREFIX*share`'); + $removeShares->execute(); + $removeItems = \OC_DB::prepare('DELETE FROM `*PREFIX*filecache`'); + $removeItems->execute(); + } + + /** + * @medium + */ + function testRemoveBrokenShares() { + // check if there are just 3 shares (see setUp - precondition: empty table) + $countShares = \OC_DB::prepare('SELECT COUNT(id) FROM `*PREFIX*share`'); + $result = $countShares->execute()->fetchOne(); + $this->assertEquals(3, $result); + + // check if there are just 2 items (see setUp - precondition: empty table) + $countItems = \OC_DB::prepare('SELECT COUNT(fileid) FROM `*PREFIX*filecache`'); + $result = $countItems->execute()->fetchOne(); + $this->assertEquals(2, $result); + + // execute actual code which should be tested + \OC\Files\Cache\Shared_Updater::fixBrokenSharesOnAppUpdate(); + + // check if there are just 2 shares (one gets killed by the code as there is no filecache entry for this) + $countShares = \OC_DB::prepare('SELECT COUNT(id) FROM `*PREFIX*share`'); + $result = $countShares->execute()->fetchOne(); + $this->assertEquals(2, $result); + + // check if the share of file '2' is removed as there is no entry for this in filecache table + $countShares = \OC_DB::prepare('SELECT COUNT(id) FROM `*PREFIX*share` WHERE file_source = 2'); + $result = $countShares->execute()->fetchOne(); + $this->assertEquals(0, $result); + + // check if there are just 2 items + $countItems = \OC_DB::prepare('SELECT COUNT(fileid) FROM `*PREFIX*filecache`'); + $result = $countItems->execute()->fetchOne(); + $this->assertEquals(2, $result); + } +} |