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.

218 lines
5.3 KiB

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