You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DeleteOrphanedSharesJobTest.php 3.9KB

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