DeleteOrphanedSharesJobTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Thomas Müller <thomas.mueller@tmit.eu>
  8. * @author Vincent Petry <pvince81@owncloud.com>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\Files_Sharing\Tests;
  26. use OCA\Files_Sharing\DeleteOrphanedSharesJob;
  27. /**
  28. * Class DeleteOrphanedSharesJobTest
  29. *
  30. * @group DB
  31. *
  32. * @package OCA\Files_Sharing\Tests
  33. */
  34. class DeleteOrphanedSharesJobTest extends \Test\TestCase {
  35. /**
  36. * @var bool
  37. */
  38. private static $trashBinStatus;
  39. /**
  40. * @var DeleteOrphanedSharesJob
  41. */
  42. private $job;
  43. /**
  44. * @var \OCP\IDBConnection
  45. */
  46. private $connection;
  47. /**
  48. * @var string
  49. */
  50. private $user1;
  51. /**
  52. * @var string
  53. */
  54. private $user2;
  55. public static function setUpBeforeClass() {
  56. $appManager = \OC::$server->getAppManager();
  57. self::$trashBinStatus = $appManager->isEnabledForUser('files_trashbin');
  58. $appManager->disableApp('files_trashbin');
  59. // just in case...
  60. \OC\Files\Filesystem::getLoader()->removeStorageWrapper('oc_trashbin');
  61. }
  62. public static function tearDownAfterClass() {
  63. if (self::$trashBinStatus) {
  64. \OC::$server->getAppManager()->enableApp('files_trashbin');
  65. }
  66. }
  67. protected function setup() {
  68. parent::setUp();
  69. $this->connection = \OC::$server->getDatabaseConnection();
  70. // clear occasional leftover shares from other tests
  71. $this->connection->executeUpdate('DELETE FROM `*PREFIX*share`');
  72. $this->user1 = $this->getUniqueID('user1_');
  73. $this->user2 = $this->getUniqueID('user2_');
  74. $userManager = \OC::$server->getUserManager();
  75. $userManager->createUser($this->user1, 'pass');
  76. $userManager->createUser($this->user2, 'pass');
  77. \OC::registerShareHooks();
  78. $this->job = new DeleteOrphanedSharesJob();
  79. }
  80. protected function tearDown() {
  81. $this->connection->executeUpdate('DELETE FROM `*PREFIX*share`');
  82. $userManager = \OC::$server->getUserManager();
  83. $user1 = $userManager->get($this->user1);
  84. if($user1) {
  85. $user1->delete();
  86. }
  87. $user2 = $userManager->get($this->user2);
  88. if($user2) {
  89. $user2->delete();
  90. }
  91. $this->logout();
  92. parent::tearDown();
  93. }
  94. private function getShares() {
  95. $shares = [];
  96. $result = $this->connection->executeQuery('SELECT * FROM `*PREFIX*share`');
  97. while ($row = $result->fetch()) {
  98. $shares[] = $row;
  99. }
  100. $result->closeCursor();
  101. return $shares;
  102. }
  103. /**
  104. * Test clearing orphaned shares
  105. */
  106. public function testClearShares() {
  107. $this->loginAsUser($this->user1);
  108. $user1Folder = \OC::$server->getUserFolder($this->user1);
  109. $testFolder = $user1Folder->newFolder('test');
  110. $testSubFolder = $testFolder->newFolder('sub');
  111. $shareManager = \OC::$server->getShareManager();
  112. $share = $shareManager->newShare();
  113. $share->setNode($testSubFolder)
  114. ->setShareType(\OCP\Share::SHARE_TYPE_USER)
  115. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  116. ->setSharedWith($this->user2)
  117. ->setSharedBy($this->user1);
  118. $shareManager->createShare($share);
  119. $this->assertCount(1, $this->getShares());
  120. $this->job->run([]);
  121. $this->assertCount(1, $this->getShares(), 'Linked shares not deleted');
  122. $testFolder->delete();
  123. $this->job->run([]);
  124. $this->assertCount(0, $this->getShares(), 'Orphaned shares deleted');
  125. }
  126. }