DeleteOrphanedSharesJobTest.php 4.1 KB

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