1
0

ShareByMailProviderTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test\Share20;
  8. use OC\Files\Node\Node;
  9. use OCA\ShareByMail\Settings\SettingsManager;
  10. use OCA\ShareByMail\ShareByMailProvider;
  11. use OCP\DB\QueryBuilder\IQueryBuilder;
  12. use OCP\Defaults;
  13. use OCP\EventDispatcher\IEventDispatcher;
  14. use OCP\Files\IRootFolder;
  15. use OCP\IConfig;
  16. use OCP\IDBConnection;
  17. use OCP\IL10N;
  18. use OCP\IURLGenerator;
  19. use OCP\IUserManager;
  20. use OCP\Mail\IMailer;
  21. use OCP\Security\IHasher;
  22. use OCP\Security\ISecureRandom;
  23. use OCP\Share\IShare;
  24. use PHPUnit\Framework\MockObject\MockObject;
  25. use Psr\Log\LoggerInterface;
  26. use Test\TestCase;
  27. /**
  28. * Class ShareByMailProviderTest
  29. *
  30. * @package Test\Share20
  31. * @group DB
  32. */
  33. class ShareByMailProviderTest extends TestCase {
  34. /** @var IDBConnection */
  35. protected $dbConn;
  36. /** @var IUserManager | \PHPUnit\Framework\MockObject\MockObject */
  37. protected $userManager;
  38. /** @var IRootFolder | \PHPUnit\Framework\MockObject\MockObject */
  39. protected $rootFolder;
  40. /** @var ShareByMailProvider */
  41. protected $provider;
  42. /** @var \PHPUnit\Framework\MockObject\MockObject|IMailer */
  43. protected $mailer;
  44. /** @var \PHPUnit\Framework\MockObject\MockObject|IL10N */
  45. protected $l10n;
  46. /** @var \PHPUnit\Framework\MockObject\MockObject|Defaults */
  47. protected $defaults;
  48. /** @var \PHPUnit\Framework\MockObject\MockObject|IURLGenerator */
  49. protected $urlGenerator;
  50. /** @var IConfig|MockObject */
  51. protected $config;
  52. /** @var LoggerInterface|MockObject */
  53. private $logger;
  54. /** @var IHasher|MockObject */
  55. private $hasher;
  56. /** @var \OCP\Activity\IManager|MockObject */
  57. private $activityManager;
  58. /** @var IEventDispatcher|MockObject */
  59. private $eventDispatcher;
  60. /** @var \OCP\Share\IManager|MockObject */
  61. private $shareManager;
  62. /** @var ISecureRandom|MockObject */
  63. private $secureRandom;
  64. /** @var SettingsManager|MockObject */
  65. private $settingsManager;
  66. protected function setUp(): void {
  67. $this->dbConn = \OC::$server->getDatabaseConnection();
  68. $this->userManager = $this->createMock(IUserManager::class);
  69. $this->rootFolder = $this->createMock(IRootFolder::class);
  70. $this->mailer = $this->createMock(IMailer::class);
  71. $this->l10n = $this->createMock(IL10N::class);
  72. $this->defaults = $this->getMockBuilder(Defaults::class)->disableOriginalConstructor()->getMock();
  73. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  74. $this->logger = $this->createMock(LoggerInterface::class);
  75. $this->activityManager = $this->createMock(\OCP\Activity\IManager::class);
  76. $this->settingsManager = $this->createMock(SettingsManager::class);
  77. $this->hasher = $this->createMock(IHasher::class);
  78. $this->eventDispatcher = $this->createMock(IEventDispatcher::class);
  79. $this->shareManager = $this->createMock(\OCP\Share\IManager::class);
  80. $this->secureRandom = $this->createMock(ISecureRandom::class);
  81. $this->config = $this->createMock(IConfig::class);
  82. // Empty share table
  83. $this->dbConn->getQueryBuilder()->delete('share')->execute();
  84. $this->provider = new ShareByMailProvider(
  85. $this->config,
  86. $this->dbConn,
  87. $this->secureRandom,
  88. $this->userManager,
  89. $this->rootFolder,
  90. $this->l10n,
  91. $this->logger,
  92. $this->mailer,
  93. $this->urlGenerator,
  94. $this->activityManager,
  95. $this->settingsManager,
  96. $this->defaults,
  97. $this->hasher,
  98. $this->eventDispatcher,
  99. $this->shareManager,
  100. );
  101. }
  102. protected function tearDown(): void {
  103. $this->dbConn->getQueryBuilder()->delete('share')->execute();
  104. $this->dbConn->getQueryBuilder()->delete('filecache')->execute();
  105. $this->dbConn->getQueryBuilder()->delete('storages')->execute();
  106. }
  107. /**
  108. * @param int $shareType
  109. * @param string $sharedWith
  110. * @param string $sharedBy
  111. * @param string $shareOwner
  112. * @param string $itemType
  113. * @param int $fileSource
  114. * @param string $fileTarget
  115. * @param int $permissions
  116. * @param $token
  117. * @param $expiration
  118. * @param $parent
  119. * @return int
  120. *
  121. * @throws \OCP\DB\Exception
  122. */
  123. private function addShareToDB($shareType, $sharedWith, $sharedBy, $shareOwner,
  124. $itemType, $fileSource, $fileTarget, $permissions, $token, $expiration,
  125. $parent) {
  126. $qb = $this->dbConn->getQueryBuilder();
  127. $qb->insert('share');
  128. if ($shareType) {
  129. $qb->setValue('share_type', $qb->expr()->literal($shareType));
  130. }
  131. if ($sharedWith) {
  132. $qb->setValue('share_with', $qb->expr()->literal($sharedWith));
  133. }
  134. if ($sharedBy) {
  135. $qb->setValue('uid_initiator', $qb->expr()->literal($sharedBy));
  136. }
  137. if ($shareOwner) {
  138. $qb->setValue('uid_owner', $qb->expr()->literal($shareOwner));
  139. }
  140. if ($itemType) {
  141. $qb->setValue('item_type', $qb->expr()->literal($itemType));
  142. }
  143. if ($fileSource) {
  144. $qb->setValue('file_source', $qb->expr()->literal($fileSource));
  145. }
  146. if ($fileTarget) {
  147. $qb->setValue('file_target', $qb->expr()->literal($fileTarget));
  148. }
  149. if ($permissions) {
  150. $qb->setValue('permissions', $qb->expr()->literal($permissions));
  151. }
  152. if ($token) {
  153. $qb->setValue('token', $qb->expr()->literal($token));
  154. }
  155. if ($expiration) {
  156. $qb->setValue('expiration', $qb->createNamedParameter($expiration, IQueryBuilder::PARAM_DATE));
  157. }
  158. if ($parent) {
  159. $qb->setValue('parent', $qb->expr()->literal($parent));
  160. }
  161. $this->assertEquals(1, $qb->execute());
  162. return $qb->getLastInsertId();
  163. }
  164. public function testGetSharesByWithResharesAndNoNode() {
  165. $this->addShareToDB(
  166. IShare::TYPE_EMAIL,
  167. 'external.one@domain.tld',
  168. 'user1',
  169. 'user1',
  170. 'folder',
  171. 42,
  172. null,
  173. 17,
  174. 'foobar',
  175. null,
  176. null,
  177. );
  178. $this->addShareToDB(
  179. IShare::TYPE_EMAIL,
  180. 'external.two@domain.tld',
  181. 'user2',
  182. 'user2',
  183. 'folder',
  184. 42,
  185. null,
  186. 17,
  187. 'barfoo',
  188. null,
  189. null,
  190. );
  191. // Return own shares only if not asked for a specific node
  192. /** @var IShare[] $actual */
  193. $actual = $this->provider->getSharesBy(
  194. 'user1',
  195. IShare::TYPE_EMAIL,
  196. null,
  197. true,
  198. -1,
  199. 0,
  200. );
  201. $this->assertCount(1, $actual);
  202. $this->assertEquals(IShare::TYPE_EMAIL, $actual[0]->getShareType());
  203. $this->assertEquals('user1', $actual[0]->getSharedBy());
  204. $this->assertEquals('user1', $actual[0]->getShareOwner());
  205. $this->assertEquals('external.one@domain.tld', $actual[0]->getSharedWith());
  206. }
  207. public function testGetSharesByWithResharesAndNode() {
  208. $this->addShareToDB(
  209. IShare::TYPE_EMAIL,
  210. 'external.one@domain.tld',
  211. 'user1',
  212. 'user1',
  213. 'folder',
  214. 42,
  215. null,
  216. 17,
  217. 'foobar',
  218. null,
  219. null,
  220. );
  221. $this->addShareToDB(
  222. IShare::TYPE_EMAIL,
  223. 'external.two@domain.tld',
  224. 'user2',
  225. 'user2',
  226. 'folder',
  227. 42,
  228. null,
  229. 17,
  230. 'barfoo',
  231. null,
  232. null,
  233. );
  234. $node = $this->createMock(Node::class);
  235. $node->expects($this->once())
  236. ->method('getId')
  237. ->willReturn(42);
  238. // Return all shares if asked for specific node
  239. /** @var IShare[] $actual */
  240. $actual = $this->provider->getSharesBy(
  241. 'user1',
  242. IShare::TYPE_EMAIL,
  243. $node,
  244. true,
  245. -1,
  246. 0,
  247. );
  248. $this->assertCount(2, $actual);
  249. $this->assertEquals(IShare::TYPE_EMAIL, $actual[0]->getShareType());
  250. $this->assertEquals('user1', $actual[0]->getSharedBy());
  251. $this->assertEquals('user1', $actual[0]->getShareOwner());
  252. $this->assertEquals('external.one@domain.tld', $actual[0]->getSharedWith());
  253. $this->assertEquals(IShare::TYPE_EMAIL, $actual[1]->getShareType());
  254. $this->assertEquals('user2', $actual[1]->getSharedBy());
  255. $this->assertEquals('user2', $actual[1]->getShareOwner());
  256. $this->assertEquals('external.two@domain.tld', $actual[1]->getSharedWith());
  257. }
  258. }