ExpireSharesJobTest.php 4.8 KB

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