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.

204 lines
5.0 KiB

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