SharesReminderJobTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\Files_Sharing\Tests;
  8. use OC\SystemConfig;
  9. use OCA\Files_Sharing\SharesReminderJob;
  10. use OCP\AppFramework\Utility\ITimeFactory;
  11. use OCP\Constants;
  12. use OCP\Defaults;
  13. use OCP\Files\IRootFolder;
  14. use OCP\IDBConnection;
  15. use OCP\IURLGenerator;
  16. use OCP\IUserManager;
  17. use OCP\L10N\IFactory;
  18. use OCP\Mail\IMailer;
  19. use OCP\Mail\IMessage;
  20. use OCP\Share\IManager;
  21. use OCP\Share\IShare;
  22. use PHPUnit\Framework\MockObject\MockObject;
  23. use Psr\Log\LoggerInterface;
  24. /**
  25. * Class SharesReminderJobTest
  26. *
  27. * @group DB
  28. *
  29. * @package OCA\Files_Sharing\Tests
  30. */
  31. class SharesReminderJobTest extends \Test\TestCase {
  32. private SharesReminderJob $job;
  33. private IDBConnection $db;
  34. private IManager $shareManager;
  35. private IUserManager $userManager;
  36. private IMailer|MockObject $mailer;
  37. private string $user1;
  38. private string $user2;
  39. protected function setUp(): void {
  40. parent::setUp();
  41. $this->db = \OC::$server->get(IDBConnection::class);
  42. $this->shareManager = \OC::$server->get(IManager::class);
  43. $this->userManager = \OC::$server->get(IUserManager::class);
  44. $this->mailer = $this->createMock(IMailer::class);
  45. // Clear occasional leftover shares from other tests
  46. $this->db->executeUpdate('DELETE FROM `*PREFIX*share`');
  47. $this->user1 = $this->getUniqueID('user1_');
  48. $this->user2 = $this->getUniqueID('user2_');
  49. $user1 = $this->userManager->createUser($this->user1, 'longrandompassword');
  50. $user2 = $this->userManager->createUser($this->user2, 'longrandompassword');
  51. $user1->setSystemEMailAddress('user1@test.com');
  52. $user2->setSystemEMailAddress('user2@test.com');
  53. \OC::registerShareHooks(\OC::$server->get(SystemConfig::class));
  54. $this->job = new SharesReminderJob(
  55. \OC::$server->get(ITimeFactory::class),
  56. $this->db,
  57. \OC::$server->get(IManager::class),
  58. $this->userManager,
  59. \OC::$server->get(LoggerInterface::class),
  60. \OC::$server->get(IURLGenerator::class),
  61. \OC::$server->get(IFactory::class),
  62. $this->mailer,
  63. \OC::$server->get(Defaults::class),
  64. );
  65. }
  66. protected function tearDown(): void {
  67. $this->db->executeUpdate('DELETE FROM `*PREFIX*share`');
  68. $userManager = \OC::$server->get(IUserManager::class);
  69. $user1 = $userManager->get($this->user1);
  70. if ($user1) {
  71. $user1->delete();
  72. }
  73. $user2 = $userManager->get($this->user2);
  74. if ($user2) {
  75. $user2->delete();
  76. }
  77. $this->logout();
  78. parent::tearDown();
  79. }
  80. public function dataSharesReminder() {
  81. $someMail = 'test@test.com';
  82. $noExpirationDate = null;
  83. $today = new \DateTime();
  84. // For expiration dates, the time is always automatically set to zero by ShareAPIController
  85. $today->setTime(0, 0);
  86. $nearFuture = new \DateTime();
  87. $nearFuture->setTimestamp($today->getTimestamp() + 86400 * 1);
  88. $farFuture = new \DateTime();
  89. $farFuture->setTimestamp($today->getTimestamp() + 86400 * 2);
  90. $permissionRead = Constants::PERMISSION_READ;
  91. $permissionCreate = $permissionRead | Constants::PERMISSION_CREATE;
  92. $permissionUpdate = $permissionRead | Constants::PERMISSION_UPDATE;
  93. $permissionDelete = $permissionRead | Constants::PERMISSION_DELETE;
  94. $permissionAll = Constants::PERMISSION_ALL;
  95. return [
  96. // No reminders for folders without expiration date
  97. [$noExpirationDate, '', false, $permissionRead, false],
  98. [$noExpirationDate, '', false, $permissionCreate, false],
  99. [$noExpirationDate, '', true, $permissionDelete, false],
  100. [$noExpirationDate, '', true, $permissionCreate, false],
  101. [$noExpirationDate, $someMail, false, $permissionUpdate, false],
  102. [$noExpirationDate, $someMail, false, $permissionCreate, false],
  103. [$noExpirationDate, $someMail, true, $permissionRead, false],
  104. [$noExpirationDate, $someMail, true, $permissionAll, false],
  105. // No reminders for folders with expiration date in the far future
  106. [$farFuture, '', false, $permissionRead, false],
  107. [$farFuture, '', false, $permissionCreate, false],
  108. [$farFuture, '', true, $permissionDelete, false],
  109. [$farFuture, '', true, $permissionCreate, false],
  110. [$farFuture, $someMail, false, $permissionUpdate, false],
  111. [$farFuture, $someMail, false, $permissionCreate, false],
  112. [$farFuture, $someMail, true, $permissionRead, false],
  113. [$farFuture, $someMail, true, $permissionAll, false],
  114. /* Should send reminders for folders with expiration date in the near future
  115. if the folder is empty and the user has write permission */
  116. [$nearFuture, '', false, $permissionRead, false],
  117. [$nearFuture, '', false, $permissionCreate, false],
  118. [$nearFuture, '', true, $permissionDelete, false],
  119. [$nearFuture, '', true, $permissionCreate, true],
  120. [$nearFuture, $someMail, false, $permissionUpdate, false],
  121. [$nearFuture, $someMail, false, $permissionCreate, false],
  122. [$nearFuture, $someMail, true, $permissionRead, false],
  123. [$nearFuture, $someMail, true, $permissionAll, true],
  124. ];
  125. }
  126. /**
  127. * @dataProvider dataSharesReminder
  128. *
  129. * @param \DateTime|null $expirationDate Share expiration date
  130. * @param string $email Share with this email. If empty, the share is of type TYPE_USER and the sharee is user2
  131. * @param bool $isEmpty Is share folder empty?
  132. * @param int $permissions
  133. * @param bool $shouldBeReminded
  134. */
  135. public function testSharesReminder(
  136. \DateTime|null $expirationDate, string $email, bool $isEmpty, int $permissions, bool $shouldBeReminded
  137. ): void {
  138. $this->loginAsUser($this->user1);
  139. $user1Folder = \OC::$server->get(IRootFolder::class)->getUserFolder($this->user1);
  140. $testFolder = $user1Folder->newFolder('test');
  141. if (!$isEmpty) {
  142. $testFolder->newFile('some_file.txt');
  143. }
  144. $share = $this->shareManager->newShare();
  145. $share->setNode($testFolder)
  146. ->setShareType(($email ? IShare::TYPE_EMAIL : IShare::TYPE_USER))
  147. ->setPermissions($permissions)
  148. ->setSharedBy($this->user1)
  149. ->setSharedWith(($email ?: $this->user2))
  150. ->setExpirationDate($expirationDate);
  151. $share = $this->shareManager->createShare($share);
  152. $this->logout();
  153. $messageMock = $this->createMock(IMessage::class);
  154. $this->mailer->method('createMessage')->willReturn($messageMock);
  155. $this->mailer
  156. ->expects(($shouldBeReminded ? $this->once() : $this->never()))
  157. ->method('send')
  158. ->with($messageMock);
  159. $messageMock
  160. ->expects(($shouldBeReminded ? $this->once() : $this->never()))
  161. ->method('setTo')
  162. ->with([$email ?: $this->userManager->get($this->user2)->getSystemEMailAddress()]);
  163. $this->assertSame(false, $share->getReminderSent());
  164. $this->job->run([]);
  165. $share = $this->shareManager->getShareById($share->getFullId());
  166. $this->assertEquals($shouldBeReminded, $share->getReminderSent());
  167. }
  168. }