DeleteOrphanedSharesJobTest.php 4.0 KB

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