DeleteOrphanedSharesJobTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_Sharing\Tests;
  8. use OCA\Files_Sharing\DeleteOrphanedSharesJob;
  9. use OCP\Share\IShare;
  10. /**
  11. * Class DeleteOrphanedSharesJobTest
  12. *
  13. * @group DB
  14. *
  15. * @package OCA\Files_Sharing\Tests
  16. */
  17. class DeleteOrphanedSharesJobTest extends \Test\TestCase {
  18. /**
  19. * @var bool
  20. */
  21. private static $trashBinStatus;
  22. /**
  23. * @var DeleteOrphanedSharesJob
  24. */
  25. private $job;
  26. /**
  27. * @var \OCP\IDBConnection
  28. */
  29. private $connection;
  30. /**
  31. * @var string
  32. */
  33. private $user1;
  34. /**
  35. * @var string
  36. */
  37. private $user2;
  38. public static function setUpBeforeClass(): void {
  39. $appManager = \OC::$server->getAppManager();
  40. self::$trashBinStatus = $appManager->isEnabledForUser('files_trashbin');
  41. $appManager->disableApp('files_trashbin');
  42. // just in case...
  43. \OC\Files\Filesystem::getLoader()->removeStorageWrapper('oc_trashbin');
  44. }
  45. public static function tearDownAfterClass(): void {
  46. if (self::$trashBinStatus) {
  47. \OC::$server->getAppManager()->enableApp('files_trashbin');
  48. }
  49. }
  50. protected function setUp(): void {
  51. parent::setUp();
  52. $this->connection = \OC::$server->getDatabaseConnection();
  53. // clear occasional leftover shares from other tests
  54. $this->connection->executeUpdate('DELETE FROM `*PREFIX*share`');
  55. $this->user1 = $this->getUniqueID('user1_');
  56. $this->user2 = $this->getUniqueID('user2_');
  57. $userManager = \OC::$server->getUserManager();
  58. $userManager->createUser($this->user1, 'pass');
  59. $userManager->createUser($this->user2, 'pass');
  60. \OC::registerShareHooks(\OC::$server->getSystemConfig());
  61. $this->job = \OCP\Server::get(DeleteOrphanedSharesJob::class);
  62. }
  63. protected function tearDown(): void {
  64. $this->connection->executeUpdate('DELETE FROM `*PREFIX*share`');
  65. $userManager = \OC::$server->getUserManager();
  66. $user1 = $userManager->get($this->user1);
  67. if ($user1) {
  68. $user1->delete();
  69. }
  70. $user2 = $userManager->get($this->user2);
  71. if ($user2) {
  72. $user2->delete();
  73. }
  74. $this->logout();
  75. parent::tearDown();
  76. }
  77. private function getShares() {
  78. $shares = [];
  79. $result = $this->connection->executeQuery('SELECT * FROM `*PREFIX*share`');
  80. while ($row = $result->fetch()) {
  81. $shares[] = $row;
  82. }
  83. $result->closeCursor();
  84. return $shares;
  85. }
  86. /**
  87. * Test clearing orphaned shares
  88. */
  89. public function testClearShares() {
  90. $this->loginAsUser($this->user1);
  91. $user1Folder = \OC::$server->getUserFolder($this->user1);
  92. $testFolder = $user1Folder->newFolder('test');
  93. $testSubFolder = $testFolder->newFolder('sub');
  94. $shareManager = \OC::$server->getShareManager();
  95. $share = $shareManager->newShare();
  96. $share->setNode($testSubFolder)
  97. ->setShareType(IShare::TYPE_USER)
  98. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  99. ->setSharedWith($this->user2)
  100. ->setSharedBy($this->user1);
  101. $shareManager->createShare($share);
  102. $this->assertCount(1, $this->getShares());
  103. $this->job->run([]);
  104. $this->assertCount(1, $this->getShares(), 'Linked shares not deleted');
  105. $testFolder->delete();
  106. $this->job->run([]);
  107. $this->assertCount(0, $this->getShares(), 'Orphaned shares deleted');
  108. }
  109. }