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.

ExpireSharesJobTest.php 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\Files_Sharing\Tests;
  26. use OCA\Files_Sharing\ExpireSharesJob;
  27. use OCP\AppFramework\Utility\ITimeFactory;
  28. use OCP\Share\IManager;
  29. use OCP\Share\IShare;
  30. /**
  31. * Class ExpireSharesJobTest
  32. *
  33. * @group DB
  34. *
  35. * @package OCA\Files_Sharing\Tests
  36. */
  37. class ExpireSharesJobTest extends \Test\TestCase {
  38. /** @var ExpireSharesJob */
  39. private $job;
  40. /** @var \OCP\IDBConnection */
  41. private $connection;
  42. /** @var string */
  43. private $user1;
  44. /** @var string */
  45. private $user2;
  46. protected function setUp(): void {
  47. parent::setUp();
  48. $this->connection = \OC::$server->getDatabaseConnection();
  49. // clear occasional leftover shares from other tests
  50. $this->connection->executeUpdate('DELETE FROM `*PREFIX*share`');
  51. $this->user1 = $this->getUniqueID('user1_');
  52. $this->user2 = $this->getUniqueID('user2_');
  53. $userManager = \OC::$server->getUserManager();
  54. $userManager->createUser($this->user1, 'longrandompassword');
  55. $userManager->createUser($this->user2, 'longrandompassword');
  56. \OC::registerShareHooks(\OC::$server->getSystemConfig());
  57. $this->job = new ExpireSharesJob(\OC::$server->get(ITimeFactory::class), \OC::$server->get(IManager::class), $this->connection);
  58. }
  59. protected function tearDown(): void {
  60. $this->connection->executeUpdate('DELETE FROM `*PREFIX*share`');
  61. $userManager = \OC::$server->getUserManager();
  62. $user1 = $userManager->get($this->user1);
  63. if ($user1) {
  64. $user1->delete();
  65. }
  66. $user2 = $userManager->get($this->user2);
  67. if ($user2) {
  68. $user2->delete();
  69. }
  70. $this->logout();
  71. parent::tearDown();
  72. }
  73. private function getShares() {
  74. $shares = [];
  75. $qb = $this->connection->getQueryBuilder();
  76. $result = $qb->select('*')
  77. ->from('share')
  78. ->execute();
  79. while ($row = $result->fetch()) {
  80. $shares[] = $row;
  81. }
  82. $result->closeCursor();
  83. return $shares;
  84. }
  85. public function dataExpireLinkShare() {
  86. return [
  87. [false, '', false, false],
  88. [false, '', true, false],
  89. [true, 'P1D', false, true],
  90. [true, 'P1D', true, false],
  91. [true, 'P1W', false, true],
  92. [true, 'P1W', true, false],
  93. [true, 'P1M', false, true],
  94. [true, 'P1M', true, false],
  95. [true, 'P1Y', false, true],
  96. [true, 'P1Y', true, false],
  97. ];
  98. }
  99. /**
  100. * @dataProvider dataExpireLinkShare
  101. *
  102. * @param bool addExpiration Should we add an expire date
  103. * @param string $interval The dateInterval
  104. * @param bool $addInterval If true add to the current time if false subtract
  105. * @param bool $shouldExpire Should this share be expired
  106. */
  107. public function testExpireLinkShare($addExpiration, $interval, $addInterval, $shouldExpire) {
  108. $this->loginAsUser($this->user1);
  109. $user1Folder = \OC::$server->getUserFolder($this->user1);
  110. $testFolder = $user1Folder->newFolder('test');
  111. $shareManager = \OC::$server->getShareManager();
  112. $share = $shareManager->newShare();
  113. $share->setNode($testFolder)
  114. ->setShareType(IShare::TYPE_LINK)
  115. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  116. ->setSharedBy($this->user1);
  117. $shareManager->createShare($share);
  118. $shares = $this->getShares();
  119. $this->assertCount(1, $shares);
  120. reset($shares);
  121. $share = current($shares);
  122. if ($addExpiration) {
  123. $expire = new \DateTime();
  124. $expire->setTime(0, 0, 0);
  125. if ($addInterval) {
  126. $expire->add(new \DateInterval($interval));
  127. } else {
  128. $expire->sub(new \DateInterval($interval));
  129. }
  130. $expire = $expire->format('Y-m-d 00:00:00');
  131. // Set expiration date to yesterday
  132. $qb = $this->connection->getQueryBuilder();
  133. $qb->update('share')
  134. ->set('expiration', $qb->createParameter('expiration'))
  135. ->where($qb->expr()->eq('id', $qb->createParameter('id')))
  136. ->setParameter('id', $share['id'])
  137. ->setParameter('expiration', $expire)
  138. ->execute();
  139. $shares = $this->getShares();
  140. $this->assertCount(1, $shares);
  141. }
  142. $this->logout();
  143. $this->job->run([]);
  144. $shares = $this->getShares();
  145. if ($shouldExpire) {
  146. $this->assertCount(0, $shares);
  147. } else {
  148. $this->assertCount(1, $shares);
  149. }
  150. }
  151. public function testDoNotExpireOtherShares() {
  152. $this->loginAsUser($this->user1);
  153. $user1Folder = \OC::$server->getUserFolder($this->user1);
  154. $testFolder = $user1Folder->newFolder('test');
  155. $shareManager = \OC::$server->getShareManager();
  156. $share = $shareManager->newShare();
  157. $share->setNode($testFolder)
  158. ->setShareType(IShare::TYPE_USER)
  159. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  160. ->setSharedBy($this->user1)
  161. ->setSharedWith($this->user2);
  162. $shareManager->createShare($share);
  163. $shares = $this->getShares();
  164. $this->assertCount(1, $shares);
  165. $this->logout();
  166. $this->job->run([]);
  167. $shares = $this->getShares();
  168. $this->assertCount(1, $shares);
  169. }
  170. }