1
0

ExpireSharesJobTest.php 5.2 KB

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