SharesReminderJobTest.php 6.7 KB

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