ExpireSharesJobTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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\ExpireSharesJob;
  9. use OCP\AppFramework\Utility\ITimeFactory;
  10. use OCP\Constants;
  11. use OCP\IDBConnection;
  12. use OCP\Share\IManager;
  13. use OCP\Share\IShare;
  14. /**
  15. * Class ExpireSharesJobTest
  16. *
  17. * @group DB
  18. *
  19. * @package OCA\Files_Sharing\Tests
  20. */
  21. class ExpireSharesJobTest extends \Test\TestCase {
  22. /** @var ExpireSharesJob */
  23. private $job;
  24. /** @var IDBConnection */
  25. private $connection;
  26. /** @var string */
  27. private $user1;
  28. /** @var string */
  29. private $user2;
  30. protected function setUp(): void {
  31. parent::setUp();
  32. $this->connection = \OC::$server->getDatabaseConnection();
  33. // clear occasional leftover shares from other tests
  34. $this->connection->executeUpdate('DELETE FROM `*PREFIX*share`');
  35. $this->user1 = $this->getUniqueID('user1_');
  36. $this->user2 = $this->getUniqueID('user2_');
  37. $userManager = \OC::$server->getUserManager();
  38. $userManager->createUser($this->user1, 'longrandompassword');
  39. $userManager->createUser($this->user2, 'longrandompassword');
  40. \OC::registerShareHooks(\OC::$server->getSystemConfig());
  41. $this->job = new ExpireSharesJob(\OC::$server->get(ITimeFactory::class), \OC::$server->get(IManager::class), $this->connection);
  42. }
  43. protected function tearDown(): void {
  44. $this->connection->executeUpdate('DELETE FROM `*PREFIX*share`');
  45. $userManager = \OC::$server->getUserManager();
  46. $user1 = $userManager->get($this->user1);
  47. if ($user1) {
  48. $user1->delete();
  49. }
  50. $user2 = $userManager->get($this->user2);
  51. if ($user2) {
  52. $user2->delete();
  53. }
  54. $this->logout();
  55. parent::tearDown();
  56. }
  57. private function getShares() {
  58. $shares = [];
  59. $qb = $this->connection->getQueryBuilder();
  60. $result = $qb->select('*')
  61. ->from('share')
  62. ->execute();
  63. while ($row = $result->fetch()) {
  64. $shares[] = $row;
  65. }
  66. $result->closeCursor();
  67. return $shares;
  68. }
  69. public function dataExpireLinkShare() {
  70. return [
  71. [false, '', false, false],
  72. [false, '', true, false],
  73. [true, 'P1D', false, true],
  74. [true, 'P1D', true, false],
  75. [true, 'P1W', false, true],
  76. [true, 'P1W', true, false],
  77. [true, 'P1M', false, true],
  78. [true, 'P1M', true, false],
  79. [true, 'P1Y', false, true],
  80. [true, 'P1Y', true, false],
  81. ];
  82. }
  83. /**
  84. * @dataProvider dataExpireLinkShare
  85. *
  86. * @param bool addExpiration Should we add an expire date
  87. * @param string $interval The dateInterval
  88. * @param bool $addInterval If true add to the current time if false subtract
  89. * @param bool $shouldExpire Should this share be expired
  90. */
  91. public function testExpireLinkShare($addExpiration, $interval, $addInterval, $shouldExpire): void {
  92. $this->loginAsUser($this->user1);
  93. $user1Folder = \OC::$server->getUserFolder($this->user1);
  94. $testFolder = $user1Folder->newFolder('test');
  95. $shareManager = \OC::$server->getShareManager();
  96. $share = $shareManager->newShare();
  97. $share->setNode($testFolder)
  98. ->setShareType(IShare::TYPE_LINK)
  99. ->setPermissions(Constants::PERMISSION_READ)
  100. ->setSharedBy($this->user1);
  101. $shareManager->createShare($share);
  102. $shares = $this->getShares();
  103. $this->assertCount(1, $shares);
  104. reset($shares);
  105. $share = current($shares);
  106. if ($addExpiration) {
  107. $expire = new \DateTime();
  108. $expire->setTime(0, 0, 0);
  109. if ($addInterval) {
  110. $expire->add(new \DateInterval($interval));
  111. } else {
  112. $expire->sub(new \DateInterval($interval));
  113. }
  114. $expire = $expire->format('Y-m-d 00:00:00');
  115. // Set expiration date to yesterday
  116. $qb = $this->connection->getQueryBuilder();
  117. $qb->update('share')
  118. ->set('expiration', $qb->createParameter('expiration'))
  119. ->where($qb->expr()->eq('id', $qb->createParameter('id')))
  120. ->setParameter('id', $share['id'])
  121. ->setParameter('expiration', $expire)
  122. ->execute();
  123. $shares = $this->getShares();
  124. $this->assertCount(1, $shares);
  125. }
  126. $this->logout();
  127. $this->job->run([]);
  128. $shares = $this->getShares();
  129. if ($shouldExpire) {
  130. $this->assertCount(0, $shares);
  131. } else {
  132. $this->assertCount(1, $shares);
  133. }
  134. }
  135. public function testDoNotExpireOtherShares(): void {
  136. $this->loginAsUser($this->user1);
  137. $user1Folder = \OC::$server->getUserFolder($this->user1);
  138. $testFolder = $user1Folder->newFolder('test');
  139. $shareManager = \OC::$server->getShareManager();
  140. $share = $shareManager->newShare();
  141. $share->setNode($testFolder)
  142. ->setShareType(IShare::TYPE_USER)
  143. ->setPermissions(Constants::PERMISSION_READ)
  144. ->setSharedBy($this->user1)
  145. ->setSharedWith($this->user2);
  146. $shareManager->createShare($share);
  147. $shares = $this->getShares();
  148. $this->assertCount(1, $shares);
  149. $this->logout();
  150. $this->job->run([]);
  151. $shares = $this->getShares();
  152. $this->assertCount(1, $shares);
  153. }
  154. }