1
0

DeleteOrphanedSharesJobTest.php 3.3 KB

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