ExpireSharesJobTest.php 5.5 KB

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