You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

updater.php 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Morris Jobke
  6. * @copyright 2014 Morris Jobke <morris.jobke@gmail.com>
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. require_once __DIR__ . '/../appinfo/update.php';
  23. /**
  24. * Class Test_Files_Sharing_Updater
  25. */
  26. class Test_Files_Sharing_Updater extends \PHPUnit_Framework_TestCase {
  27. function setUp() {
  28. // some previous tests didn't clean up and therefore this has to be done here
  29. // FIXME: DIRTY HACK - TODO: find tests, that don't clean up and fix it there
  30. $this->tearDown();
  31. // add items except one - because this is the test case for the broken share table
  32. $addItems = \OC_DB::prepare('INSERT INTO `*PREFIX*filecache` (`storage`, `path_hash`, ' .
  33. '`parent`, `mimetype`, `mimepart`, `size`, `mtime`, `storage_mtime`) ' .
  34. 'VALUES (1, ?, 1, 1, 1, 1, 1, 1)');
  35. $items = array(1, 3);
  36. $fileIds = array();
  37. foreach($items as $item) {
  38. // the number is used as path_hash
  39. $addItems->execute(array($item));
  40. $fileIds[] = \OC_DB::insertId('*PREFIX*filecache');
  41. }
  42. $addShares = \OC_DB::prepare('INSERT INTO `*PREFIX*share` (`file_source`, `item_type`, `uid_owner`) VALUES (?, \'file\', 1)');
  43. // the number is used as item_source
  44. $addShares->execute(array($fileIds[0]));
  45. $addShares->execute(array(200)); // id of "deleted" file
  46. $addShares->execute(array($fileIds[1]));
  47. }
  48. function tearDown() {
  49. $removeShares = \OC_DB::prepare('DELETE FROM `*PREFIX*share`');
  50. $removeShares->execute();
  51. $removeItems = \OC_DB::prepare('DELETE FROM `*PREFIX*filecache`');
  52. $removeItems->execute();
  53. }
  54. /**
  55. * @medium
  56. */
  57. function testRemoveBrokenShares() {
  58. // check if there are just 3 shares (see setUp - precondition: empty table)
  59. $countShares = \OC_DB::prepare('SELECT COUNT(`id`) FROM `*PREFIX*share`');
  60. $result = $countShares->execute()->fetchOne();
  61. $this->assertEquals(3, $result);
  62. // check if there are just 2 items (see setUp - precondition: empty table)
  63. $countItems = \OC_DB::prepare('SELECT COUNT(`fileid`) FROM `*PREFIX*filecache`');
  64. $result = $countItems->execute()->fetchOne();
  65. $this->assertEquals(2, $result);
  66. // execute actual code which should be tested
  67. \OC\Files\Cache\Shared_Updater::fixBrokenSharesOnAppUpdate();
  68. // check if there are just 2 shares (one gets killed by the code as there is no filecache entry for this)
  69. $countShares = \OC_DB::prepare('SELECT COUNT(`id`) FROM `*PREFIX*share`');
  70. $result = $countShares->execute()->fetchOne();
  71. $this->assertEquals(2, $result);
  72. // check if the share of file '200' is removed as there is no entry for this in filecache table
  73. $countShares = \OC_DB::prepare('SELECT COUNT(`id`) FROM `*PREFIX*share` WHERE `file_source` = 200');
  74. $result = $countShares->execute()->fetchOne();
  75. $this->assertEquals(0, $result);
  76. // check if there are just 2 items
  77. $countItems = \OC_DB::prepare('SELECT COUNT(`fileid`) FROM `*PREFIX*filecache`');
  78. $result = $countItems->execute()->fetchOne();
  79. $this->assertEquals(2, $result);
  80. }
  81. /**
  82. * test update for the removal of the logical "Shared" folder. It should update
  83. * the file_target for every share and create a physical "Shared" folder for each user
  84. */
  85. function testRemoveSharedFolder() {
  86. self::prepareDB();
  87. // run the update routine to remove the shared folder and replace it with a real folder
  88. removeSharedFolder(false, 2);
  89. // verify results
  90. $query = \OC_DB::prepare('SELECT * FROM `*PREFIX*share`');
  91. $result = $query->execute(array());
  92. $newDBContent = $result->fetchAll();
  93. foreach ($newDBContent as $row) {
  94. if ((int)$row['share_type'] === \OCP\Share::SHARE_TYPE_USER) {
  95. $this->assertSame('/Shared', substr($row['file_target'], 0, strlen('/Shared')));
  96. } else {
  97. $this->assertSame('/ShouldNotChange', $row['file_target']);
  98. }
  99. }
  100. $this->cleanupSharedTable();
  101. }
  102. private function cleanupSharedTable() {
  103. $query = \OC_DB::prepare('DELETE FROM `*PREFIX*share`');
  104. $query->execute();
  105. }
  106. private function prepareDB() {
  107. $this->cleanupSharedTable();
  108. // add items except one - because this is the test case for the broken share table
  109. $addItems = \OC_DB::prepare('INSERT INTO `*PREFIX*share` (`share_type`, `item_type`, ' .
  110. '`share_with`, `uid_owner` , `file_target`) ' .
  111. 'VALUES (?, ?, ?, ?, ?)');
  112. $items = array(
  113. array(\OCP\Share::SHARE_TYPE_USER, 'file', 'user1', 'admin' , '/foo'),
  114. array(\OCP\Share::SHARE_TYPE_USER, 'folder', 'user2', 'admin', '/foo2'),
  115. array(\OCP\Share::SHARE_TYPE_USER, 'file', 'user3', 'admin', '/foo3'),
  116. array(\OCP\Share::SHARE_TYPE_USER, 'folder', 'user4', 'admin', '/foo4'),
  117. array(\OCP\Share::SHARE_TYPE_LINK, 'file', 'user1', 'admin', '/ShouldNotChange'),
  118. array(\OCP\Share::SHARE_TYPE_CONTACT, 'contact', 'admin', 'user1', '/ShouldNotChange'),
  119. );
  120. foreach($items as $item) {
  121. // the number is used as path_hash
  122. $addItems->execute($item);
  123. }
  124. }
  125. }