ExpireSharesJobTest.php 5.3 KB

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