ShareByMailProviderTest.php 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\ShareByMail\Tests;
  7. use OC\Mail\Message;
  8. use OCA\ShareByMail\Settings\SettingsManager;
  9. use OCA\ShareByMail\ShareByMailProvider;
  10. use OCP\Defaults;
  11. use OCP\EventDispatcher\IEventDispatcher;
  12. use OCP\Files\File;
  13. use OCP\Files\IRootFolder;
  14. use OCP\IConfig;
  15. use OCP\IDBConnection;
  16. use OCP\IL10N;
  17. use OCP\IURLGenerator;
  18. use OCP\IUser;
  19. use OCP\IUserManager;
  20. use OCP\Mail\IEMailTemplate;
  21. use OCP\Mail\IMailer;
  22. use OCP\Mail\IMessage;
  23. use OCP\Security\Events\GenerateSecurePasswordEvent;
  24. use OCP\Security\IHasher;
  25. use OCP\Security\ISecureRandom;
  26. use OCP\Share\IManager;
  27. use OCP\Share\IShare;
  28. use Psr\Log\LoggerInterface;
  29. use Test\TestCase;
  30. /**
  31. * Class ShareByMailProviderTest
  32. *
  33. * @package OCA\ShareByMail\Tests
  34. * @group DB
  35. */
  36. class ShareByMailProviderTest extends TestCase {
  37. /** @var IConfig */
  38. private $config;
  39. /** @var IDBConnection */
  40. private $connection;
  41. /** @var IManager | \PHPUnit\Framework\MockObject\MockObject */
  42. private $shareManager;
  43. /** @var IL10N | \PHPUnit\Framework\MockObject\MockObject */
  44. private $l;
  45. /** @var LoggerInterface | \PHPUnit\Framework\MockObject\MockObject */
  46. private $logger;
  47. /** @var IRootFolder | \PHPUnit\Framework\MockObject\MockObject */
  48. private $rootFolder;
  49. /** @var IUserManager | \PHPUnit\Framework\MockObject\MockObject */
  50. private $userManager;
  51. /** @var ISecureRandom | \PHPUnit\Framework\MockObject\MockObject */
  52. private $secureRandom;
  53. /** @var IMailer | \PHPUnit\Framework\MockObject\MockObject */
  54. private $mailer;
  55. /** @var IURLGenerator | \PHPUnit\Framework\MockObject\MockObject */
  56. private $urlGenerator;
  57. /** @var IShare | \PHPUnit\Framework\MockObject\MockObject */
  58. private $share;
  59. /** @var \OCP\Activity\IManager | \PHPUnit\Framework\MockObject\MockObject */
  60. private $activityManager;
  61. /** @var SettingsManager | \PHPUnit\Framework\MockObject\MockObject */
  62. private $settingsManager;
  63. /** @var Defaults|\PHPUnit\Framework\MockObject\MockObject */
  64. private $defaults;
  65. /** @var IHasher | \PHPUnit\Framework\MockObject\MockObject */
  66. private $hasher;
  67. /** @var IEventDispatcher */
  68. private $eventDispatcher;
  69. protected function setUp(): void {
  70. parent::setUp();
  71. $this->config = $this->getMockBuilder(IConfig::class)->getMock();
  72. $this->connection = \OC::$server->getDatabaseConnection();
  73. $this->l = $this->getMockBuilder(IL10N::class)->getMock();
  74. $this->l->method('t')
  75. ->willReturnCallback(function ($text, $parameters = []) {
  76. return vsprintf($text, $parameters);
  77. });
  78. $this->logger = $this->getMockBuilder(LoggerInterface::class)->getMock();
  79. $this->rootFolder = $this->getMockBuilder('OCP\Files\IRootFolder')->getMock();
  80. $this->userManager = $this->getMockBuilder(IUserManager::class)->getMock();
  81. $this->secureRandom = $this->getMockBuilder('\OCP\Security\ISecureRandom')->getMock();
  82. $this->mailer = $this->getMockBuilder('\OCP\Mail\IMailer')->getMock();
  83. $this->urlGenerator = $this->getMockBuilder(IURLGenerator::class)->getMock();
  84. $this->share = $this->getMockBuilder(IShare::class)->getMock();
  85. $this->activityManager = $this->getMockBuilder('OCP\Activity\IManager')->getMock();
  86. $this->settingsManager = $this->getMockBuilder(SettingsManager::class)->disableOriginalConstructor()->getMock();
  87. $this->defaults = $this->createMock(Defaults::class);
  88. $this->hasher = $this->getMockBuilder(IHasher::class)->getMock();
  89. $this->eventDispatcher = $this->getMockBuilder(IEventDispatcher::class)->getMock();
  90. $this->shareManager = $this->getMockBuilder(IManager::class)->getMock();
  91. $this->userManager->expects($this->any())->method('userExists')->willReturn(true);
  92. }
  93. /**
  94. * get instance of Mocked ShareByMailProvider
  95. *
  96. * @param array $mockedMethods internal methods which should be mocked
  97. * @return \PHPUnit\Framework\MockObject\MockObject | ShareByMailProvider
  98. */
  99. private function getInstance(array $mockedMethods = []) {
  100. $instance = $this->getMockBuilder('OCA\ShareByMail\ShareByMailProvider')
  101. ->setConstructorArgs(
  102. [
  103. $this->config,
  104. $this->connection,
  105. $this->secureRandom,
  106. $this->userManager,
  107. $this->rootFolder,
  108. $this->l,
  109. $this->logger,
  110. $this->mailer,
  111. $this->urlGenerator,
  112. $this->activityManager,
  113. $this->settingsManager,
  114. $this->defaults,
  115. $this->hasher,
  116. $this->eventDispatcher,
  117. $this->shareManager
  118. ]
  119. );
  120. if (!empty($mockedMethods)) {
  121. $instance->setMethods($mockedMethods);
  122. return $instance->getMock();
  123. }
  124. return new ShareByMailProvider(
  125. $this->config,
  126. $this->connection,
  127. $this->secureRandom,
  128. $this->userManager,
  129. $this->rootFolder,
  130. $this->l,
  131. $this->logger,
  132. $this->mailer,
  133. $this->urlGenerator,
  134. $this->activityManager,
  135. $this->settingsManager,
  136. $this->defaults,
  137. $this->hasher,
  138. $this->eventDispatcher,
  139. $this->shareManager
  140. );
  141. }
  142. protected function tearDown(): void {
  143. $this->connection->getQueryBuilder()->delete('share')->execute();
  144. parent::tearDown();
  145. }
  146. public function testCreate() {
  147. $expectedShare = $this->createMock(IShare::class);
  148. $share = $this->getMockBuilder(IShare::class)->getMock();
  149. $share->expects($this->any())->method('getSharedWith')->willReturn('user1');
  150. $node = $this->getMockBuilder(File::class)->getMock();
  151. $node->expects($this->any())->method('getName')->willReturn('filename');
  152. $instance = $this->getInstance(['getSharedWith', 'createMailShare', 'getRawShare', 'createShareObject', 'createShareActivity', 'sendPassword']);
  153. $instance->expects($this->once())->method('getSharedWith')->willReturn([]);
  154. $instance->expects($this->once())->method('createMailShare')->with($share)->willReturn(42);
  155. $instance->expects($this->once())->method('createShareActivity')->with($share);
  156. $instance->expects($this->once())->method('getRawShare')->with(42)->willReturn(['rawShare']);
  157. $instance->expects($this->once())->method('createShareObject')->with(['rawShare'])->willReturn($expectedShare);
  158. $instance->expects($this->any())->method('sendPassword')->willReturn(true);
  159. $share->expects($this->any())->method('getNode')->willReturn($node);
  160. $this->shareManager->expects($this->any())->method('shareApiLinkEnforcePassword')->willReturn(false);
  161. $this->settingsManager->expects($this->any())->method('sendPasswordByMail')->willReturn(true);
  162. $this->assertSame($expectedShare, $instance->create($share));
  163. }
  164. public function testCreateSendPasswordByMailWithoutEnforcedPasswordProtection() {
  165. $expectedShare = $this->createMock(IShare::class);
  166. $share = $this->getMockBuilder(IShare::class)->getMock();
  167. $share->expects($this->any())->method('getSharedWith')->willReturn('receiver@examplelölöl.com');
  168. $share->expects($this->any())->method('getSendPasswordByTalk')->willReturn(false);
  169. $share->expects($this->any())->method('getSharedBy')->willReturn('owner');
  170. $node = $this->getMockBuilder(File::class)->getMock();
  171. $node->expects($this->any())->method('getName')->willReturn('filename');
  172. $instance = $this->getInstance(['getSharedWith', 'createMailShare', 'getRawShare', 'createShareObject', 'createShareActivity', 'autoGeneratePassword', 'createPasswordSendActivity']);
  173. $instance->expects($this->once())->method('getSharedWith')->willReturn([]);
  174. $instance->expects($this->once())->method('createMailShare')->with($share)->willReturn(42);
  175. $instance->expects($this->once())->method('createShareActivity')->with($share);
  176. $instance->expects($this->once())->method('getRawShare')->with(42)->willReturn(['rawShare']);
  177. $instance->expects($this->once())->method('createShareObject')->with(['rawShare'])->willReturn($expectedShare);
  178. $share->expects($this->any())->method('getNode')->willReturn($node);
  179. // The autogenerated password should not be mailed.
  180. $this->shareManager->expects($this->any())->method('shareApiLinkEnforcePassword')->willReturn(false);
  181. $this->settingsManager->expects($this->any())->method('sendPasswordByMail')->willReturn(true);
  182. $instance->expects($this->never())->method('autoGeneratePassword');
  183. $this->mailer->expects($this->never())->method('send');
  184. $this->assertSame($expectedShare, $instance->create($share));
  185. }
  186. public function testCreateSendPasswordByMailWithPasswordAndWithoutEnforcedPasswordProtectionWithPermanentPassword() {
  187. $expectedShare = $this->createMock(IShare::class);
  188. $share = $this->getMockBuilder(IShare::class)->getMock();
  189. $share->expects($this->any())->method('getSharedWith')->willReturn('receiver@example.com');
  190. $share->expects($this->any())->method('getSendPasswordByTalk')->willReturn(false);
  191. $share->expects($this->any())->method('getSharedBy')->willReturn('owner');
  192. $node = $this->getMockBuilder(File::class)->getMock();
  193. $node->expects($this->any())->method('getName')->willReturn('filename');
  194. $instance = $this->getInstance(['getSharedWith', 'createMailShare', 'getRawShare', 'createShareObject', 'createShareActivity', 'autoGeneratePassword', 'createPasswordSendActivity']);
  195. $instance->expects($this->once())->method('getSharedWith')->willReturn([]);
  196. $instance->expects($this->once())->method('createMailShare')->with($share)->willReturn(42);
  197. $instance->expects($this->once())->method('createShareActivity')->with($share);
  198. $instance->expects($this->once())->method('getRawShare')->with(42)->willReturn(['rawShare']);
  199. $instance->expects($this->once())->method('createShareObject')->with(['rawShare'])->willReturn($expectedShare);
  200. $share->expects($this->any())->method('getNode')->willReturn($node);
  201. $share->expects($this->once())->method('getPassword')->willReturn('password');
  202. $this->hasher->expects($this->once())->method('hash')->with('password')->willReturn('passwordHashed');
  203. $share->expects($this->once())->method('setPassword')->with('passwordHashed');
  204. // The given password (but not the autogenerated password) should not be
  205. // mailed to the receiver of the share because permanent passwords are not enforced.
  206. $this->shareManager->expects($this->any())->method('shareApiLinkEnforcePassword')->willReturn(false);
  207. $this->config->expects($this->once())->method('getSystemValue')->with('sharing.enable_mail_link_password_expiration')->willReturn(false);
  208. $instance->expects($this->never())->method('autoGeneratePassword');
  209. $this->assertSame($expectedShare, $instance->create($share));
  210. }
  211. public function testCreateSendPasswordByMailWithPasswordAndWithoutEnforcedPasswordProtectionWithoutPermanentPassword() {
  212. $expectedShare = $this->createMock(IShare::class);
  213. $share = $this->getMockBuilder(IShare::class)->getMock();
  214. $share->expects($this->any())->method('getSharedWith')->willReturn('receiver@example.com');
  215. $share->expects($this->any())->method('getSendPasswordByTalk')->willReturn(false);
  216. $share->expects($this->any())->method('getSharedBy')->willReturn('owner');
  217. $node = $this->getMockBuilder(File::class)->getMock();
  218. $node->expects($this->any())->method('getName')->willReturn('filename');
  219. $instance = $this->getInstance(['getSharedWith', 'createMailShare', 'getRawShare', 'createShareObject', 'createShareActivity', 'autoGeneratePassword', 'createPasswordSendActivity']);
  220. $instance->expects($this->once())->method('getSharedWith')->willReturn([]);
  221. $instance->expects($this->once())->method('createMailShare')->with($share)->willReturn(42);
  222. $instance->expects($this->once())->method('createShareActivity')->with($share);
  223. $instance->expects($this->once())->method('getRawShare')->with(42)->willReturn(['rawShare']);
  224. $instance->expects($this->once())->method('createShareObject')->with(['rawShare'])->willReturn($expectedShare);
  225. $share->expects($this->any())->method('getNode')->willReturn($node);
  226. $share->expects($this->once())->method('getPassword')->willReturn('password');
  227. $this->hasher->expects($this->once())->method('hash')->with('password')->willReturn('passwordHashed');
  228. $share->expects($this->once())->method('setPassword')->with('passwordHashed');
  229. // The given password (but not the autogenerated password) should be
  230. // mailed to the receiver of the share because permanent passwords are enforced.
  231. $this->shareManager->expects($this->any())->method('shareApiLinkEnforcePassword')->willReturn(false);
  232. $instance->expects($this->never())->method('autoGeneratePassword');
  233. $this->config->expects($this->any())->method('getSystemValue')->withConsecutive(
  234. ['sharing.enable_mail_link_password_expiration'],
  235. ['sharing.enable_mail_link_password_expiration'],
  236. ['sharing.mail_link_password_expiration_interval']
  237. )->willReturnOnConsecutiveCalls(
  238. true,
  239. true,
  240. 3600
  241. );
  242. $this->assertSame($expectedShare, $instance->create($share));
  243. }
  244. public function testCreateSendPasswordByMailWithEnforcedPasswordProtectionWithPermanentPassword() {
  245. $expectedShare = $this->createMock(IShare::class);
  246. $share = $this->getMockBuilder(IShare::class)->getMock();
  247. $share->expects($this->any())->method('getSharedWith')->willReturn('receiver@example.com');
  248. $share->expects($this->any())->method('getSendPasswordByTalk')->willReturn(false);
  249. $share->expects($this->any())->method('getSharedBy')->willReturn('owner');
  250. $node = $this->getMockBuilder(File::class)->getMock();
  251. $node->expects($this->any())->method('getName')->willReturn('filename');
  252. $this->secureRandom->expects($this->once())
  253. ->method('generate')
  254. ->with(8, ISecureRandom::CHAR_HUMAN_READABLE)
  255. ->willReturn('autogeneratedPassword');
  256. $this->eventDispatcher->expects($this->once())
  257. ->method('dispatchTyped')
  258. ->with(new GenerateSecurePasswordEvent());
  259. $instance = $this->getInstance(['getSharedWith', 'createMailShare', 'getRawShare', 'createShareObject', 'createShareActivity', 'createPasswordSendActivity']);
  260. $instance->expects($this->once())->method('getSharedWith')->willReturn([]);
  261. $instance->expects($this->once())->method('createMailShare')->with($share)->willReturn(42);
  262. $instance->expects($this->once())->method('createShareActivity')->with($share);
  263. $instance->expects($this->once())->method('getRawShare')->with(42)->willReturn(['rawShare']);
  264. $instance->expects($this->once())->method('createShareObject')->with(['rawShare'])->willReturn($expectedShare);
  265. $share->expects($this->any())->method('getNode')->willReturn($node);
  266. $share->expects($this->once())->method('getPassword')->willReturn(null);
  267. $this->hasher->expects($this->once())->method('hash')->with('autogeneratedPassword')->willReturn('autogeneratedPasswordHashed');
  268. $share->expects($this->once())->method('setPassword')->with('autogeneratedPasswordHashed');
  269. // The autogenerated password should be mailed to the receiver of the share because permanent passwords are enforced.
  270. $this->shareManager->expects($this->any())->method('shareApiLinkEnforcePassword')->willReturn(true);
  271. $this->config->expects($this->any())->method('getSystemValue')->with('sharing.enable_mail_link_password_expiration')->willReturn(false);
  272. $this->settingsManager->expects($this->any())->method('sendPasswordByMail')->willReturn(true);
  273. $message = $this->createMock(IMessage::class);
  274. $message->expects($this->once())->method('setTo')->with(['receiver@example.com']);
  275. $this->mailer->expects($this->once())->method('createMessage')->willReturn($message);
  276. $this->mailer->expects($this->once())->method('createEMailTemplate')->with('sharebymail.RecipientPasswordNotification', [
  277. 'filename' => 'filename',
  278. 'password' => 'autogeneratedPassword',
  279. 'initiator' => 'owner',
  280. 'initiatorEmail' => null,
  281. 'shareWith' => 'receiver@example.com',
  282. ]);
  283. $this->mailer->expects($this->once())->method('send');
  284. $this->assertSame($expectedShare, $instance->create($share));
  285. }
  286. public function testCreateSendPasswordByMailWithPasswordAndWithEnforcedPasswordProtectionWithPermanentPassword() {
  287. $expectedShare = $this->createMock(IShare::class);
  288. $share = $this->getMockBuilder(IShare::class)->getMock();
  289. $share->expects($this->any())->method('getSharedWith')->willReturn('receiver@example.com');
  290. $share->expects($this->any())->method('getSendPasswordByTalk')->willReturn(false);
  291. $share->expects($this->any())->method('getSharedBy')->willReturn('owner');
  292. $node = $this->getMockBuilder(File::class)->getMock();
  293. $node->expects($this->any())->method('getName')->willReturn('filename');
  294. $instance = $this->getInstance(['getSharedWith', 'createMailShare', 'getRawShare', 'createShareObject', 'createShareActivity', 'autoGeneratePassword', 'createPasswordSendActivity']);
  295. $instance->expects($this->once())->method('getSharedWith')->willReturn([]);
  296. $instance->expects($this->once())->method('createMailShare')->with($share)->willReturn(42);
  297. $instance->expects($this->once())->method('createShareActivity')->with($share);
  298. $instance->expects($this->once())->method('getRawShare')->with(42)->willReturn(['rawShare']);
  299. $instance->expects($this->once())->method('createShareObject')->with(['rawShare'])->willReturn($expectedShare);
  300. $share->expects($this->any())->method('getNode')->willReturn($node);
  301. $share->expects($this->once())->method('getPassword')->willReturn('password');
  302. $this->hasher->expects($this->once())->method('hash')->with('password')->willReturn('passwordHashed');
  303. $share->expects($this->once())->method('setPassword')->with('passwordHashed');
  304. // The given password (but not the autogenerated password) should be
  305. // mailed to the receiver of the share.
  306. $this->shareManager->expects($this->any())->method('shareApiLinkEnforcePassword')->willReturn(true);
  307. $this->config->expects($this->any())->method('getSystemValue')->with('sharing.enable_mail_link_password_expiration')->willReturn(false);
  308. $this->settingsManager->expects($this->any())->method('sendPasswordByMail')->willReturn(true);
  309. $instance->expects($this->never())->method('autoGeneratePassword');
  310. $message = $this->createMock(IMessage::class);
  311. $message->expects($this->once())->method('setTo')->with(['receiver@example.com']);
  312. $this->mailer->expects($this->once())->method('createMessage')->willReturn($message);
  313. $this->mailer->expects($this->once())->method('createEMailTemplate')->with('sharebymail.RecipientPasswordNotification', [
  314. 'filename' => 'filename',
  315. 'password' => 'password',
  316. 'initiator' => 'owner',
  317. 'initiatorEmail' => null,
  318. 'shareWith' => 'receiver@example.com',
  319. ]);
  320. $this->mailer->expects($this->once())->method('send');
  321. $this->assertSame($expectedShare, $instance->create($share));
  322. }
  323. public function testCreateSendPasswordByTalkWithEnforcedPasswordProtectionWithPermanentPassword() {
  324. $expectedShare = $this->createMock(IShare::class);
  325. $share = $this->getMockBuilder(IShare::class)->getMock();
  326. $share->expects($this->any())->method('getSharedWith')->willReturn('receiver@example.com');
  327. $share->expects($this->any())->method('getSendPasswordByTalk')->willReturn(true);
  328. $share->expects($this->any())->method('getSharedBy')->willReturn('owner');
  329. $node = $this->getMockBuilder(File::class)->getMock();
  330. $node->expects($this->any())->method('getName')->willReturn('filename');
  331. $instance = $this->getInstance(['getSharedWith', 'createMailShare', 'getRawShare', 'createShareObject', 'createShareActivity', 'autoGeneratePassword', 'createPasswordSendActivity']);
  332. $instance->expects($this->once())->method('getSharedWith')->willReturn([]);
  333. $instance->expects($this->once())->method('createMailShare')->with($share)->willReturn(42);
  334. $instance->expects($this->once())->method('createShareActivity')->with($share);
  335. $instance->expects($this->once())->method('getRawShare')->with(42)->willReturn(['rawShare']);
  336. $instance->expects($this->once())->method('createShareObject')->with(['rawShare'])->willReturn($expectedShare);
  337. $share->expects($this->any())->method('getNode')->willReturn($node);
  338. $share->expects($this->once())->method('getPassword')->willReturn(null);
  339. $this->hasher->expects($this->once())->method('hash')->with('autogeneratedPassword')->willReturn('autogeneratedPasswordHashed');
  340. $share->expects($this->once())->method('setPassword')->with('autogeneratedPasswordHashed');
  341. // The autogenerated password should be mailed to the owner of the share.
  342. $this->shareManager->expects($this->any())->method('shareApiLinkEnforcePassword')->willReturn(true);
  343. $this->config->expects($this->any())->method('getSystemValue')->with('sharing.enable_mail_link_password_expiration')->willReturn(false);
  344. $this->settingsManager->expects($this->any())->method('sendPasswordByMail')->willReturn(true);
  345. $instance->expects($this->once())->method('autoGeneratePassword')->with($share)->willReturn('autogeneratedPassword');
  346. $message = $this->createMock(IMessage::class);
  347. $message->expects($this->once())->method('setTo')->with(['owner@example.com' => 'Owner display name']);
  348. $this->mailer->expects($this->once())->method('createMessage')->willReturn($message);
  349. $this->mailer->expects($this->once())->method('createEMailTemplate')->with('sharebymail.OwnerPasswordNotification', [
  350. 'filename' => 'filename',
  351. 'password' => 'autogeneratedPassword',
  352. 'initiator' => 'Owner display name',
  353. 'initiatorEmail' => 'owner@example.com',
  354. 'shareWith' => 'receiver@example.com',
  355. ]);
  356. $this->mailer->expects($this->once())->method('send');
  357. $user = $this->createMock(IUser::class);
  358. $this->userManager->expects($this->once())->method('get')->with('owner')->willReturn($user);
  359. $user->expects($this->once())->method('getDisplayName')->willReturn('Owner display name');
  360. $user->expects($this->once())->method('getEMailAddress')->willReturn('owner@example.com');
  361. $this->assertSame($expectedShare, $instance->create($share));
  362. }
  363. public function testCreateFailed() {
  364. $this->expectException(\Exception::class);
  365. $this->share->expects($this->once())->method('getSharedWith')->willReturn('user1');
  366. $node = $this->getMockBuilder('OCP\Files\Node')->getMock();
  367. $node->expects($this->any())->method('getName')->willReturn('fileName');
  368. $this->share->expects($this->any())->method('getNode')->willReturn($node);
  369. $instance = $this->getInstance(['getSharedWith', 'createMailShare', 'getRawShare', 'createShareObject']);
  370. $instance->expects($this->once())->method('getSharedWith')->willReturn(['found']);
  371. $instance->expects($this->never())->method('createMailShare');
  372. $instance->expects($this->never())->method('getRawShare');
  373. $instance->expects($this->never())->method('createShareObject');
  374. $this->assertSame('shareObject',
  375. $instance->create($this->share)
  376. );
  377. }
  378. public function testCreateMailShare() {
  379. $this->share->expects($this->any())->method('getToken')->willReturn('token');
  380. $this->share->expects($this->once())->method('setToken')->with('token');
  381. $this->share->expects($this->any())->method('getSharedBy')->willReturn('validby@valid.com');
  382. $this->share->expects($this->any())->method('getSharedWith')->willReturn('validwith@valid.com');
  383. $this->share->expects($this->any())->method('getNote')->willReturn('Check this!');
  384. $node = $this->getMockBuilder('OCP\Files\Node')->getMock();
  385. $node->expects($this->any())->method('getName')->willReturn('fileName');
  386. $this->share->expects($this->any())->method('getNode')->willReturn($node);
  387. $instance = $this->getInstance(['generateToken', 'addShareToDB', 'sendMailNotification']);
  388. $instance->expects($this->once())->method('generateToken')->willReturn('token');
  389. $instance->expects($this->once())->method('addShareToDB')->willReturn(42);
  390. $instance->expects($this->once())->method('sendMailNotification');
  391. $this->urlGenerator->expects($this->once())->method('linkToRouteAbsolute')
  392. ->with('files_sharing.sharecontroller.showShare', ['token' => 'token']);
  393. $instance->expects($this->once())->method('sendMailNotification');
  394. $this->assertSame(42,
  395. $this->invokePrivate($instance, 'createMailShare', [$this->share])
  396. );
  397. }
  398. public function testCreateMailShareFailed() {
  399. $this->expectException(\OCP\HintException::class);
  400. $this->share->expects($this->any())->method('getToken')->willReturn('token');
  401. $this->share->expects($this->once())->method('setToken')->with('token');
  402. $this->share->expects($this->any())->method('getSharedBy')->willReturn('validby@valid.com');
  403. $this->share->expects($this->any())->method('getSharedWith')->willReturn('validwith@valid.com');
  404. $this->share->expects($this->any())->method('getNote')->willReturn('Check this!');
  405. $node = $this->getMockBuilder('OCP\Files\Node')->getMock();
  406. $node->expects($this->any())->method('getName')->willReturn('fileName');
  407. $this->share->expects($this->any())->method('getNode')->willReturn($node);
  408. $instance = $this->getInstance(['generateToken', 'addShareToDB', 'sendMailNotification']);
  409. $instance->expects($this->once())->method('generateToken')->willReturn('token');
  410. $instance->expects($this->once())->method('addShareToDB')->willReturn(42);
  411. $instance->expects($this->once())->method('sendMailNotification');
  412. $this->urlGenerator->expects($this->once())->method('linkToRouteAbsolute')
  413. ->with('files_sharing.sharecontroller.showShare', ['token' => 'token']);
  414. $instance->expects($this->once())->method('sendMailNotification')
  415. ->willReturnCallback(
  416. function () {
  417. throw new \Exception('should be converted to a hint exception');
  418. }
  419. );
  420. $this->assertSame(42,
  421. $this->invokePrivate($instance, 'createMailShare', [$this->share])
  422. );
  423. }
  424. public function testGenerateToken() {
  425. $instance = $this->getInstance();
  426. $this->secureRandom->expects($this->once())->method('generate')->willReturn('token');
  427. $this->assertSame('token',
  428. $this->invokePrivate($instance, 'generateToken')
  429. );
  430. }
  431. public function testAddShareToDB() {
  432. $itemSource = 11;
  433. $itemType = 'file';
  434. $shareWith = 'user@server.com';
  435. $sharedBy = 'user1';
  436. $uidOwner = 'user2';
  437. $permissions = 1;
  438. $token = 'token';
  439. $password = 'password';
  440. $sendPasswordByTalk = true;
  441. $hideDownload = true;
  442. $label = 'label';
  443. $expiration = new \DateTime();
  444. $passwordExpirationTime = new \DateTime();
  445. $instance = $this->getInstance();
  446. $id = $this->invokePrivate(
  447. $instance,
  448. 'addShareToDB',
  449. [
  450. $itemSource,
  451. $itemType,
  452. $shareWith,
  453. $sharedBy,
  454. $uidOwner,
  455. $permissions,
  456. $token,
  457. $password,
  458. $passwordExpirationTime,
  459. $sendPasswordByTalk,
  460. $hideDownload,
  461. $label,
  462. $expiration
  463. ]
  464. );
  465. $qb = $this->connection->getQueryBuilder();
  466. $qb->select('*')
  467. ->from('share')
  468. ->where($qb->expr()->eq('id', $qb->createNamedParameter($id)));
  469. $qResult = $qb->execute();
  470. $result = $qResult->fetchAll();
  471. $qResult->closeCursor();
  472. $this->assertSame(1, count($result));
  473. $this->assertSame($itemSource, (int)$result[0]['item_source']);
  474. $this->assertSame($itemType, $result[0]['item_type']);
  475. $this->assertSame($shareWith, $result[0]['share_with']);
  476. $this->assertSame($sharedBy, $result[0]['uid_initiator']);
  477. $this->assertSame($uidOwner, $result[0]['uid_owner']);
  478. $this->assertSame($permissions, (int)$result[0]['permissions']);
  479. $this->assertSame($token, $result[0]['token']);
  480. $this->assertSame($password, $result[0]['password']);
  481. $this->assertSame($passwordExpirationTime->getTimestamp(), \DateTime::createFromFormat('Y-m-d H:i:s', $result[0]['password_expiration_time'])->getTimestamp());
  482. $this->assertSame($sendPasswordByTalk, (bool)$result[0]['password_by_talk']);
  483. $this->assertSame($hideDownload, (bool)$result[0]['hide_download']);
  484. $this->assertSame($label, $result[0]['label']);
  485. $this->assertSame($expiration->getTimestamp(), \DateTime::createFromFormat('Y-m-d H:i:s', $result[0]['expiration'])->getTimestamp());
  486. }
  487. public function testUpdate() {
  488. $itemSource = 11;
  489. $itemType = 'file';
  490. $shareWith = 'user@server.com';
  491. $sharedBy = 'user1';
  492. $uidOwner = 'user2';
  493. $permissions = 1;
  494. $token = 'token';
  495. $note = 'personal note';
  496. $instance = $this->getInstance();
  497. $id = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token, $note);
  498. $this->share->expects($this->once())->method('getPermissions')->willReturn($permissions + 1);
  499. $this->share->expects($this->once())->method('getShareOwner')->willReturn($uidOwner);
  500. $this->share->expects($this->once())->method('getSharedBy')->willReturn($sharedBy);
  501. $this->share->expects($this->any())->method('getNote')->willReturn($note);
  502. $this->share->expects($this->atLeastOnce())->method('getId')->willReturn($id);
  503. $this->assertSame($this->share,
  504. $instance->update($this->share)
  505. );
  506. $qb = $this->connection->getQueryBuilder();
  507. $qb->select('*')
  508. ->from('share')
  509. ->where($qb->expr()->eq('id', $qb->createNamedParameter($id)));
  510. $qResult = $qb->execute();
  511. $result = $qResult->fetchAll();
  512. $qResult->closeCursor();
  513. $this->assertSame(1, count($result));
  514. $this->assertSame($itemSource, (int)$result[0]['item_source']);
  515. $this->assertSame($itemType, $result[0]['item_type']);
  516. $this->assertSame($shareWith, $result[0]['share_with']);
  517. $this->assertSame($sharedBy, $result[0]['uid_initiator']);
  518. $this->assertSame($uidOwner, $result[0]['uid_owner']);
  519. $this->assertSame($permissions + 1, (int)$result[0]['permissions']);
  520. $this->assertSame($token, $result[0]['token']);
  521. $this->assertSame($note, $result[0]['note']);
  522. }
  523. public function dataUpdateSendPassword() {
  524. return [
  525. ['password', 'hashed', 'hashed new', false, false, true],
  526. ['', 'hashed', 'hashed new', false, false, false],
  527. [null, 'hashed', 'hashed new', false, false, false],
  528. ['password', 'hashed', 'hashed', false, false, false],
  529. ['password', 'hashed', 'hashed new', false, true, false],
  530. ['password', 'hashed', 'hashed new', true, false, true],
  531. ['password', 'hashed', 'hashed', true, false, true],
  532. ];
  533. }
  534. /**
  535. * @dataProvider dataUpdateSendPassword
  536. *
  537. * @param string|null plainTextPassword
  538. * @param string originalPassword
  539. * @param string newPassword
  540. * @param string originalSendPasswordByTalk
  541. * @param string newSendPasswordByTalk
  542. * @param bool sendMail
  543. */
  544. public function testUpdateSendPassword($plainTextPassword, string $originalPassword, string $newPassword, $originalSendPasswordByTalk, $newSendPasswordByTalk, bool $sendMail) {
  545. $node = $this->getMockBuilder(File::class)->getMock();
  546. $node->expects($this->any())->method('getName')->willReturn('filename');
  547. $this->settingsManager->method('sendPasswordByMail')->willReturn(true);
  548. $originalShare = $this->getMockBuilder(IShare::class)->getMock();
  549. $originalShare->expects($this->any())->method('getSharedWith')->willReturn('receiver@example.com');
  550. $originalShare->expects($this->any())->method('getNode')->willReturn($node);
  551. $originalShare->expects($this->any())->method('getId')->willReturn(42);
  552. $originalShare->expects($this->any())->method('getPassword')->willReturn($originalPassword);
  553. $originalShare->expects($this->any())->method('getSendPasswordByTalk')->willReturn($originalSendPasswordByTalk);
  554. $share = $this->getMockBuilder(IShare::class)->getMock();
  555. $share->expects($this->any())->method('getSharedWith')->willReturn('receiver@example.com');
  556. $share->expects($this->any())->method('getNode')->willReturn($node);
  557. $share->expects($this->any())->method('getId')->willReturn(42);
  558. $share->expects($this->any())->method('getPassword')->willReturn($newPassword);
  559. $share->expects($this->any())->method('getSendPasswordByTalk')->willReturn($newSendPasswordByTalk);
  560. if ($sendMail) {
  561. $this->mailer->expects($this->once())->method('createEMailTemplate')->with('sharebymail.RecipientPasswordNotification', [
  562. 'filename' => 'filename',
  563. 'password' => $plainTextPassword,
  564. 'initiator' => null,
  565. 'initiatorEmail' => null,
  566. 'shareWith' => 'receiver@example.com',
  567. ]);
  568. $this->mailer->expects($this->once())->method('send');
  569. } else {
  570. $this->mailer->expects($this->never())->method('send');
  571. }
  572. $instance = $this->getInstance(['getShareById', 'createPasswordSendActivity']);
  573. $instance->expects($this->once())->method('getShareById')->willReturn($originalShare);
  574. $this->assertSame($share,
  575. $instance->update($share, $plainTextPassword)
  576. );
  577. }
  578. public function testDelete() {
  579. $instance = $this->getInstance(['removeShareFromTable', 'createShareActivity']);
  580. $this->share->expects($this->once())->method('getId')->willReturn(42);
  581. $instance->expects($this->once())->method('removeShareFromTable')->with(42);
  582. $instance->expects($this->once())->method('createShareActivity')->with($this->share, 'unshare');
  583. $instance->delete($this->share);
  584. }
  585. public function testGetShareById() {
  586. $instance = $this->getInstance(['createShareObject']);
  587. $itemSource = 11;
  588. $itemType = 'file';
  589. $shareWith = 'user@server.com';
  590. $sharedBy = 'user1';
  591. $uidOwner = 'user2';
  592. $permissions = 1;
  593. $token = 'token';
  594. $this->createDummyShare($itemType, $itemSource, $shareWith, "user1wrong", "user2wrong", $permissions, $token);
  595. $id2 = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token);
  596. $instance->expects($this->once())->method('createShareObject')
  597. ->willReturnCallback(
  598. function ($data) use ($uidOwner, $sharedBy, $id2) {
  599. $this->assertSame($uidOwner, $data['uid_owner']);
  600. $this->assertSame($sharedBy, $data['uid_initiator']);
  601. $this->assertSame($id2, (int)$data['id']);
  602. return $this->share;
  603. }
  604. );
  605. $result = $instance->getShareById($id2);
  606. $this->assertInstanceOf('OCP\Share\IShare', $result);
  607. }
  608. public function testGetShareByIdFailed() {
  609. $this->expectException(\OCP\Share\Exceptions\ShareNotFound::class);
  610. $instance = $this->getInstance(['createShareObject']);
  611. $itemSource = 11;
  612. $itemType = 'file';
  613. $shareWith = 'user@server.com';
  614. $sharedBy = 'user1';
  615. $uidOwner = 'user2';
  616. $permissions = 1;
  617. $token = 'token';
  618. $id = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token);
  619. $instance->getShareById($id + 1);
  620. }
  621. public function testGetShareByPath() {
  622. $itemSource = 11;
  623. $itemType = 'file';
  624. $shareWith = 'user@server.com';
  625. $sharedBy = 'user1';
  626. $uidOwner = 'user2';
  627. $permissions = 1;
  628. $token = 'token';
  629. $node = $this->getMockBuilder('OCP\Files\Node')->getMock();
  630. $node->expects($this->once())->method('getId')->willReturn($itemSource);
  631. $instance = $this->getInstance(['createShareObject']);
  632. $this->createDummyShare($itemType, 111, $shareWith, $sharedBy, $uidOwner, $permissions, $token);
  633. $id = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token);
  634. $instance->expects($this->once())->method('createShareObject')
  635. ->willReturnCallback(
  636. function ($data) use ($uidOwner, $sharedBy, $id) {
  637. $this->assertSame($uidOwner, $data['uid_owner']);
  638. $this->assertSame($sharedBy, $data['uid_initiator']);
  639. $this->assertSame($id, (int)$data['id']);
  640. return $this->share;
  641. }
  642. );
  643. $result = $instance->getSharesByPath($node);
  644. $this->assertTrue(is_array($result));
  645. $this->assertSame(1, count($result));
  646. $this->assertInstanceOf('OCP\Share\IShare', $result[0]);
  647. }
  648. public function testGetShareByToken() {
  649. $itemSource = 11;
  650. $itemType = 'file';
  651. $shareWith = 'user@server.com';
  652. $sharedBy = 'user1';
  653. $uidOwner = 'user2';
  654. $permissions = 1;
  655. $token = 'token';
  656. $instance = $this->getInstance(['createShareObject']);
  657. $idMail = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token);
  658. $idPublic = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token, '', IShare::TYPE_LINK);
  659. $this->assertTrue($idMail !== $idPublic);
  660. $instance->expects($this->once())->method('createShareObject')
  661. ->willReturnCallback(
  662. function ($data) use ($idMail) {
  663. $this->assertSame($idMail, (int)$data['id']);
  664. return $this->share;
  665. }
  666. );
  667. $result = $instance->getShareByToken('token');
  668. $this->assertInstanceOf('OCP\Share\IShare', $result);
  669. }
  670. public function testGetShareByTokenFailed() {
  671. $this->expectException(\OCP\Share\Exceptions\ShareNotFound::class);
  672. $itemSource = 11;
  673. $itemType = 'file';
  674. $shareWith = 'user@server.com';
  675. $sharedBy = 'user1';
  676. $uidOwner = 'user2';
  677. $permissions = 1;
  678. $token = 'token';
  679. $instance = $this->getInstance(['createShareObject']);
  680. $idMail = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token);
  681. $idPublic = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, "token2", '', IShare::TYPE_LINK);
  682. $this->assertTrue($idMail !== $idPublic);
  683. $this->assertInstanceOf('OCP\Share\IShare',
  684. $instance->getShareByToken('token2')
  685. );
  686. }
  687. public function testRemoveShareFromTable() {
  688. $itemSource = 11;
  689. $itemType = 'file';
  690. $shareWith = 'user@server.com';
  691. $sharedBy = 'user1';
  692. $uidOwner = 'user2';
  693. $permissions = 1;
  694. $token = 'token';
  695. $instance = $this->getInstance();
  696. $id = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token);
  697. $query = $this->connection->getQueryBuilder();
  698. $query->select('*')->from('share')
  699. ->where($query->expr()->eq('id', $query->createNamedParameter($id)));
  700. $result = $query->execute();
  701. $before = $result->fetchAll();
  702. $result->closeCursor();
  703. $this->assertTrue(is_array($before));
  704. $this->assertSame(1, count($before));
  705. $this->invokePrivate($instance, 'removeShareFromTable', [$id]);
  706. $query = $this->connection->getQueryBuilder();
  707. $query->select('*')->from('share')
  708. ->where($query->expr()->eq('id', $query->createNamedParameter($id)));
  709. $result = $query->execute();
  710. $after = $result->fetchAll();
  711. $result->closeCursor();
  712. $this->assertTrue(is_array($after));
  713. $this->assertEmpty($after);
  714. }
  715. public function testUserDeleted() {
  716. $itemSource = 11;
  717. $itemType = 'file';
  718. $shareWith = 'user@server.com';
  719. $sharedBy = 'user1';
  720. $uidOwner = 'user2';
  721. $permissions = 1;
  722. $token = 'token';
  723. $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token);
  724. $id = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, 'user2Wrong', $permissions, $token);
  725. $query = $this->connection->getQueryBuilder();
  726. $query->select('*')->from('share');
  727. $result = $query->execute();
  728. $before = $result->fetchAll();
  729. $result->closeCursor();
  730. $this->assertTrue(is_array($before));
  731. $this->assertSame(2, count($before));
  732. $instance = $this->getInstance();
  733. $instance->userDeleted($uidOwner, IShare::TYPE_EMAIL);
  734. $query = $this->connection->getQueryBuilder();
  735. $query->select('*')->from('share');
  736. $result = $query->execute();
  737. $after = $result->fetchAll();
  738. $result->closeCursor();
  739. $this->assertTrue(is_array($after));
  740. $this->assertSame(1, count($after));
  741. $this->assertSame($id, (int)$after[0]['id']);
  742. }
  743. public function testGetRawShare() {
  744. $itemSource = 11;
  745. $itemType = 'file';
  746. $shareWith = 'user@server.com';
  747. $sharedBy = 'user1';
  748. $uidOwner = 'user2';
  749. $permissions = 1;
  750. $token = 'token';
  751. $instance = $this->getInstance();
  752. $id = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token);
  753. $result = $this->invokePrivate($instance, 'getRawShare', [$id]);
  754. $this->assertTrue(is_array($result));
  755. $this->assertSame($itemSource, (int)$result['item_source']);
  756. $this->assertSame($itemType, $result['item_type']);
  757. $this->assertSame($shareWith, $result['share_with']);
  758. $this->assertSame($sharedBy, $result['uid_initiator']);
  759. $this->assertSame($uidOwner, $result['uid_owner']);
  760. $this->assertSame($permissions, (int)$result['permissions']);
  761. $this->assertSame($token, $result['token']);
  762. }
  763. public function testGetRawShareFailed() {
  764. $this->expectException(\OCP\Share\Exceptions\ShareNotFound::class);
  765. $itemSource = 11;
  766. $itemType = 'file';
  767. $shareWith = 'user@server.com';
  768. $sharedBy = 'user1';
  769. $uidOwner = 'user2';
  770. $permissions = 1;
  771. $token = 'token';
  772. $instance = $this->getInstance();
  773. $id = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token);
  774. $this->invokePrivate($instance, 'getRawShare', [$id + 1]);
  775. }
  776. private function createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token, $note = '', $shareType = IShare::TYPE_EMAIL) {
  777. $qb = $this->connection->getQueryBuilder();
  778. $qb->insert('share')
  779. ->setValue('share_type', $qb->createNamedParameter($shareType))
  780. ->setValue('item_type', $qb->createNamedParameter($itemType))
  781. ->setValue('item_source', $qb->createNamedParameter($itemSource))
  782. ->setValue('file_source', $qb->createNamedParameter($itemSource))
  783. ->setValue('share_with', $qb->createNamedParameter($shareWith))
  784. ->setValue('uid_owner', $qb->createNamedParameter($uidOwner))
  785. ->setValue('uid_initiator', $qb->createNamedParameter($sharedBy))
  786. ->setValue('permissions', $qb->createNamedParameter($permissions))
  787. ->setValue('token', $qb->createNamedParameter($token))
  788. ->setValue('note', $qb->createNamedParameter($note))
  789. ->setValue('stime', $qb->createNamedParameter(time()));
  790. /*
  791. * Added to fix https://github.com/owncloud/core/issues/22215
  792. * Can be removed once we get rid of ajax/share.php
  793. */
  794. $qb->setValue('file_target', $qb->createNamedParameter(''));
  795. $qb->execute();
  796. $id = $qb->getLastInsertId();
  797. return (int)$id;
  798. }
  799. public function testGetSharesInFolder() {
  800. $userManager = \OC::$server->getUserManager();
  801. $rootFolder = \OC::$server->getRootFolder();
  802. $this->shareManager->expects($this->any())
  803. ->method('newShare')
  804. ->willReturn(new \OC\Share20\Share($rootFolder, $userManager));
  805. $provider = $this->getInstance(['sendMailNotification', 'createShareActivity']);
  806. $this->mailer->expects($this->any())->method('validateMailAddress')->willReturn(true);
  807. $u1 = $userManager->createUser('testFed', md5(time()));
  808. $u2 = $userManager->createUser('testFed2', md5(time()));
  809. $folder1 = $rootFolder->getUserFolder($u1->getUID())->newFolder('foo');
  810. $file1 = $folder1->newFile('bar1');
  811. $file2 = $folder1->newFile('bar2');
  812. $share1 = $this->shareManager->newShare();
  813. $share1->setSharedWith('user@server.com')
  814. ->setSharedBy($u1->getUID())
  815. ->setShareOwner($u1->getUID())
  816. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  817. ->setNode($file1);
  818. $provider->create($share1);
  819. $share2 = $this->shareManager->newShare();
  820. $share2->setSharedWith('user@server.com')
  821. ->setSharedBy($u2->getUID())
  822. ->setShareOwner($u1->getUID())
  823. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  824. ->setNode($file2);
  825. $provider->create($share2);
  826. $result = $provider->getSharesInFolder($u1->getUID(), $folder1, false);
  827. $this->assertCount(1, $result);
  828. $this->assertCount(1, $result[$file1->getId()]);
  829. $result = $provider->getSharesInFolder($u1->getUID(), $folder1, true);
  830. $this->assertCount(2, $result);
  831. $this->assertCount(1, $result[$file1->getId()]);
  832. $this->assertCount(1, $result[$file2->getId()]);
  833. $u1->delete();
  834. $u2->delete();
  835. }
  836. public function testGetAccessList() {
  837. $userManager = \OC::$server->getUserManager();
  838. $rootFolder = \OC::$server->getRootFolder();
  839. $this->shareManager->expects($this->any())
  840. ->method('newShare')
  841. ->willReturn(new \OC\Share20\Share($rootFolder, $userManager));
  842. $provider = $this->getInstance(['sendMailNotification', 'createShareActivity']);
  843. $this->mailer->expects($this->any())->method('validateMailAddress')->willReturn(true);
  844. $u1 = $userManager->createUser('testFed', md5(time()));
  845. $u2 = $userManager->createUser('testFed2', md5(time()));
  846. $folder = $rootFolder->getUserFolder($u1->getUID())->newFolder('foo');
  847. $accessList = $provider->getAccessList([$folder], true);
  848. $this->assertArrayHasKey('public', $accessList);
  849. $this->assertFalse($accessList['public']);
  850. $accessList = $provider->getAccessList([$folder], false);
  851. $this->assertArrayHasKey('public', $accessList);
  852. $this->assertFalse($accessList['public']);
  853. $share1 = $this->shareManager->newShare();
  854. $share1->setSharedWith('user@server.com')
  855. ->setSharedBy($u1->getUID())
  856. ->setShareOwner($u1->getUID())
  857. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  858. ->setNode($folder);
  859. $share1 = $provider->create($share1);
  860. $share2 = $this->shareManager->newShare();
  861. $share2->setSharedWith('user2@server.com')
  862. ->setSharedBy($u2->getUID())
  863. ->setShareOwner($u1->getUID())
  864. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  865. ->setNode($folder);
  866. $share2 = $provider->create($share2);
  867. $accessList = $provider->getAccessList([$folder], true);
  868. $this->assertArrayHasKey('public', $accessList);
  869. $this->assertTrue($accessList['public']);
  870. $accessList = $provider->getAccessList([$folder], false);
  871. $this->assertArrayHasKey('public', $accessList);
  872. $this->assertTrue($accessList['public']);
  873. $provider->delete($share2);
  874. $accessList = $provider->getAccessList([$folder], true);
  875. $this->assertArrayHasKey('public', $accessList);
  876. $this->assertTrue($accessList['public']);
  877. $accessList = $provider->getAccessList([$folder], false);
  878. $this->assertArrayHasKey('public', $accessList);
  879. $this->assertTrue($accessList['public']);
  880. $provider->delete($share1);
  881. $accessList = $provider->getAccessList([$folder], true);
  882. $this->assertArrayHasKey('public', $accessList);
  883. $this->assertFalse($accessList['public']);
  884. $accessList = $provider->getAccessList([$folder], false);
  885. $this->assertArrayHasKey('public', $accessList);
  886. $this->assertFalse($accessList['public']);
  887. $u1->delete();
  888. $u2->delete();
  889. }
  890. public function testSendMailNotificationWithSameUserAndUserEmail() {
  891. $provider = $this->getInstance();
  892. $user = $this->createMock(IUser::class);
  893. $this->settingsManager->expects($this->any())->method('replyToInitiator')->willReturn(true);
  894. $this->userManager
  895. ->expects($this->once())
  896. ->method('get')
  897. ->with('OwnerUser')
  898. ->willReturn($user);
  899. $user
  900. ->expects($this->once())
  901. ->method('getDisplayName')
  902. ->willReturn('Mrs. Owner User');
  903. $message = $this->createMock(Message::class);
  904. $this->mailer
  905. ->expects($this->once())
  906. ->method('createMessage')
  907. ->willReturn($message);
  908. $template = $this->createMock(IEMailTemplate::class);
  909. $this->mailer
  910. ->expects($this->once())
  911. ->method('createEMailTemplate')
  912. ->willReturn($template);
  913. $template
  914. ->expects($this->once())
  915. ->method('addHeader');
  916. $template
  917. ->expects($this->once())
  918. ->method('addHeading')
  919. ->with('Mrs. Owner User shared »file.txt« with you');
  920. $template
  921. ->expects($this->once())
  922. ->method('addBodyText')
  923. ->with(
  924. 'Mrs. Owner User shared »file.txt« with you. Click the button below to open it.',
  925. 'Mrs. Owner User shared »file.txt« with you.'
  926. );
  927. $template
  928. ->expects($this->once())
  929. ->method('addBodyButton')
  930. ->with(
  931. 'Open »file.txt«',
  932. 'https://example.com/file.txt'
  933. );
  934. $message
  935. ->expects($this->once())
  936. ->method('setTo')
  937. ->with(['john@doe.com']);
  938. $this->defaults
  939. ->expects($this->once())
  940. ->method('getName')
  941. ->willReturn('UnitTestCloud');
  942. $message
  943. ->expects($this->once())
  944. ->method('setFrom')
  945. ->with([
  946. \OCP\Util::getDefaultEmailAddress('UnitTestCloud') => 'Mrs. Owner User via UnitTestCloud'
  947. ]);
  948. $user
  949. ->expects($this->once())
  950. ->method('getEMailAddress')
  951. ->willReturn('owner@example.com');
  952. $message
  953. ->expects($this->once())
  954. ->method('setReplyTo')
  955. ->with(['owner@example.com' => 'Mrs. Owner User']);
  956. $this->defaults
  957. ->expects($this->exactly(2))
  958. ->method('getSlogan')
  959. ->willReturn('Testing like 1990');
  960. $template
  961. ->expects($this->once())
  962. ->method('addFooter')
  963. ->with('UnitTestCloud - Testing like 1990');
  964. $template
  965. ->expects($this->once())
  966. ->method('setSubject')
  967. ->with('Mrs. Owner User shared »file.txt« with you');
  968. $message
  969. ->expects($this->once())
  970. ->method('useTemplate')
  971. ->with($template);
  972. $this->mailer
  973. ->expects($this->once())
  974. ->method('send')
  975. ->with($message);
  976. self::invokePrivate(
  977. $provider,
  978. 'sendMailNotification',
  979. [
  980. 'file.txt',
  981. 'https://example.com/file.txt',
  982. 'OwnerUser',
  983. 'john@doe.com',
  984. null,
  985. ''
  986. ]);
  987. }
  988. public function testSendMailNotificationWithSameUserAndUserEmailAndNote() {
  989. $provider = $this->getInstance();
  990. $user = $this->createMock(IUser::class);
  991. $this->settingsManager->expects($this->any())->method('replyToInitiator')->willReturn(true);
  992. $this->userManager
  993. ->expects($this->once())
  994. ->method('get')
  995. ->with('OwnerUser')
  996. ->willReturn($user);
  997. $user
  998. ->expects($this->once())
  999. ->method('getDisplayName')
  1000. ->willReturn('Mrs. Owner User');
  1001. $message = $this->createMock(Message::class);
  1002. $this->mailer
  1003. ->expects($this->once())
  1004. ->method('createMessage')
  1005. ->willReturn($message);
  1006. $template = $this->createMock(IEMailTemplate::class);
  1007. $this->mailer
  1008. ->expects($this->once())
  1009. ->method('createEMailTemplate')
  1010. ->willReturn($template);
  1011. $template
  1012. ->expects($this->once())
  1013. ->method('addHeader');
  1014. $template
  1015. ->expects($this->once())
  1016. ->method('addHeading')
  1017. ->with('Mrs. Owner User shared »file.txt« with you');
  1018. $template
  1019. ->expects($this->exactly(2))
  1020. ->method('addBodyText')
  1021. ->withConsecutive(
  1022. ['This is a note to the recipient', 'This is a note to the recipient'],
  1023. ['Mrs. Owner User shared »file.txt« with you. Click the button below to open it.', 'Mrs. Owner User shared »file.txt« with you.'],
  1024. );
  1025. $template
  1026. ->expects($this->once())
  1027. ->method('addBodyButton')
  1028. ->with(
  1029. 'Open »file.txt«',
  1030. 'https://example.com/file.txt'
  1031. );
  1032. $message
  1033. ->expects($this->once())
  1034. ->method('setTo')
  1035. ->with(['john@doe.com']);
  1036. $this->defaults
  1037. ->expects($this->once())
  1038. ->method('getName')
  1039. ->willReturn('UnitTestCloud');
  1040. $message
  1041. ->expects($this->once())
  1042. ->method('setFrom')
  1043. ->with([
  1044. \OCP\Util::getDefaultEmailAddress('UnitTestCloud') => 'Mrs. Owner User via UnitTestCloud'
  1045. ]);
  1046. $user
  1047. ->expects($this->once())
  1048. ->method('getEMailAddress')
  1049. ->willReturn('owner@example.com');
  1050. $message
  1051. ->expects($this->once())
  1052. ->method('setReplyTo')
  1053. ->with(['owner@example.com' => 'Mrs. Owner User']);
  1054. $this->defaults
  1055. ->expects($this->exactly(2))
  1056. ->method('getSlogan')
  1057. ->willReturn('Testing like 1990');
  1058. $template
  1059. ->expects($this->once())
  1060. ->method('addFooter')
  1061. ->with('UnitTestCloud - Testing like 1990');
  1062. $template
  1063. ->expects($this->once())
  1064. ->method('setSubject')
  1065. ->with('Mrs. Owner User shared »file.txt« with you');
  1066. $message
  1067. ->expects($this->once())
  1068. ->method('useTemplate')
  1069. ->with($template);
  1070. $this->mailer
  1071. ->expects($this->once())
  1072. ->method('send')
  1073. ->with($message);
  1074. self::invokePrivate(
  1075. $provider,
  1076. 'sendMailNotification',
  1077. [
  1078. 'file.txt',
  1079. 'https://example.com/file.txt',
  1080. 'OwnerUser',
  1081. 'john@doe.com',
  1082. null,
  1083. 'This is a note to the recipient'
  1084. ]);
  1085. }
  1086. public function testSendMailNotificationWithDifferentUserAndNoUserEmail() {
  1087. $provider = $this->getInstance();
  1088. $initiatorUser = $this->createMock(IUser::class);
  1089. $this->settingsManager->expects($this->any())->method('replyToInitiator')->willReturn(true);
  1090. $this->userManager
  1091. ->expects($this->once())
  1092. ->method('get')
  1093. ->with('InitiatorUser')
  1094. ->willReturn($initiatorUser);
  1095. $initiatorUser
  1096. ->expects($this->once())
  1097. ->method('getDisplayName')
  1098. ->willReturn('Mr. Initiator User');
  1099. $message = $this->createMock(Message::class);
  1100. $this->mailer
  1101. ->expects($this->once())
  1102. ->method('createMessage')
  1103. ->willReturn($message);
  1104. $template = $this->createMock(IEMailTemplate::class);
  1105. $this->mailer
  1106. ->expects($this->once())
  1107. ->method('createEMailTemplate')
  1108. ->willReturn($template);
  1109. $template
  1110. ->expects($this->once())
  1111. ->method('addHeader');
  1112. $template
  1113. ->expects($this->once())
  1114. ->method('addHeading')
  1115. ->with('Mr. Initiator User shared »file.txt« with you');
  1116. $template
  1117. ->expects($this->once())
  1118. ->method('addBodyText')
  1119. ->with(
  1120. 'Mr. Initiator User shared »file.txt« with you. Click the button below to open it.',
  1121. 'Mr. Initiator User shared »file.txt« with you.'
  1122. );
  1123. $template
  1124. ->expects($this->once())
  1125. ->method('addBodyButton')
  1126. ->with(
  1127. 'Open »file.txt«',
  1128. 'https://example.com/file.txt'
  1129. );
  1130. $message
  1131. ->expects($this->once())
  1132. ->method('setTo')
  1133. ->with(['john@doe.com']);
  1134. $this->defaults
  1135. ->expects($this->once())
  1136. ->method('getName')
  1137. ->willReturn('UnitTestCloud');
  1138. $message
  1139. ->expects($this->once())
  1140. ->method('setFrom')
  1141. ->with([
  1142. \OCP\Util::getDefaultEmailAddress('UnitTestCloud') => 'Mr. Initiator User via UnitTestCloud'
  1143. ]);
  1144. $message
  1145. ->expects($this->never())
  1146. ->method('setReplyTo');
  1147. $template
  1148. ->expects($this->once())
  1149. ->method('addFooter')
  1150. ->with('');
  1151. $template
  1152. ->expects($this->once())
  1153. ->method('setSubject')
  1154. ->with('Mr. Initiator User shared »file.txt« with you');
  1155. $message
  1156. ->expects($this->once())
  1157. ->method('useTemplate')
  1158. ->with($template);
  1159. $this->mailer
  1160. ->expects($this->once())
  1161. ->method('send')
  1162. ->with($message);
  1163. self::invokePrivate(
  1164. $provider,
  1165. 'sendMailNotification',
  1166. [
  1167. 'file.txt',
  1168. 'https://example.com/file.txt',
  1169. 'InitiatorUser',
  1170. 'john@doe.com',
  1171. null,
  1172. ]);
  1173. }
  1174. public function testSendMailNotificationWithSameUserAndUserEmailAndReplyToDesactivate() {
  1175. $provider = $this->getInstance();
  1176. $user = $this->createMock(IUser::class);
  1177. $this->settingsManager->expects($this->any())->method('replyToInitiator')->willReturn(false);
  1178. $this->userManager
  1179. ->expects($this->once())
  1180. ->method('get')
  1181. ->with('OwnerUser')
  1182. ->willReturn($user);
  1183. $user
  1184. ->expects($this->once())
  1185. ->method('getDisplayName')
  1186. ->willReturn('Mrs. Owner User');
  1187. $message = $this->createMock(Message::class);
  1188. $this->mailer
  1189. ->expects($this->once())
  1190. ->method('createMessage')
  1191. ->willReturn($message);
  1192. $template = $this->createMock(IEMailTemplate::class);
  1193. $this->mailer
  1194. ->expects($this->once())
  1195. ->method('createEMailTemplate')
  1196. ->willReturn($template);
  1197. $template
  1198. ->expects($this->once())
  1199. ->method('addHeader');
  1200. $template
  1201. ->expects($this->once())
  1202. ->method('addHeading')
  1203. ->with('Mrs. Owner User shared »file.txt« with you');
  1204. $template
  1205. ->expects($this->once())
  1206. ->method('addBodyText')
  1207. ->with(
  1208. 'Mrs. Owner User shared »file.txt« with you. Click the button below to open it.',
  1209. 'Mrs. Owner User shared »file.txt« with you.'
  1210. );
  1211. $template
  1212. ->expects($this->once())
  1213. ->method('addBodyButton')
  1214. ->with(
  1215. 'Open »file.txt«',
  1216. 'https://example.com/file.txt'
  1217. );
  1218. $message
  1219. ->expects($this->once())
  1220. ->method('setTo')
  1221. ->with(['john@doe.com']);
  1222. $this->defaults
  1223. ->expects($this->once())
  1224. ->method('getName')
  1225. ->willReturn('UnitTestCloud');
  1226. $message
  1227. ->expects($this->once())
  1228. ->method('setFrom')
  1229. ->with([
  1230. \OCP\Util::getDefaultEmailAddress('UnitTestCloud') => 'UnitTestCloud'
  1231. ]);
  1232. $user
  1233. ->expects($this->once())
  1234. ->method('getEMailAddress')
  1235. ->willReturn('owner@example.com');
  1236. $message
  1237. ->expects($this->never())
  1238. ->method('setReplyTo');
  1239. $template
  1240. ->expects($this->once())
  1241. ->method('addFooter')
  1242. ->with('');
  1243. $template
  1244. ->expects($this->once())
  1245. ->method('setSubject')
  1246. ->with('Mrs. Owner User shared »file.txt« with you');
  1247. $message
  1248. ->expects($this->once())
  1249. ->method('useTemplate')
  1250. ->with($template);
  1251. $this->mailer
  1252. ->expects($this->once())
  1253. ->method('send')
  1254. ->with($message);
  1255. self::invokePrivate(
  1256. $provider,
  1257. 'sendMailNotification',
  1258. [
  1259. 'file.txt',
  1260. 'https://example.com/file.txt',
  1261. 'OwnerUser',
  1262. 'john@doe.com',
  1263. null,
  1264. ]);
  1265. }
  1266. public function testSendMailNotificationWithDifferentUserAndNoUserEmailAndReplyToDesactivate() {
  1267. $provider = $this->getInstance();
  1268. $initiatorUser = $this->createMock(IUser::class);
  1269. $this->settingsManager->expects($this->any())->method('replyToInitiator')->willReturn(false);
  1270. $this->userManager
  1271. ->expects($this->once())
  1272. ->method('get')
  1273. ->with('InitiatorUser')
  1274. ->willReturn($initiatorUser);
  1275. $initiatorUser
  1276. ->expects($this->once())
  1277. ->method('getDisplayName')
  1278. ->willReturn('Mr. Initiator User');
  1279. $message = $this->createMock(Message::class);
  1280. $this->mailer
  1281. ->expects($this->once())
  1282. ->method('createMessage')
  1283. ->willReturn($message);
  1284. $template = $this->createMock(IEMailTemplate::class);
  1285. $this->mailer
  1286. ->expects($this->once())
  1287. ->method('createEMailTemplate')
  1288. ->willReturn($template);
  1289. $template
  1290. ->expects($this->once())
  1291. ->method('addHeader');
  1292. $template
  1293. ->expects($this->once())
  1294. ->method('addHeading')
  1295. ->with('Mr. Initiator User shared »file.txt« with you');
  1296. $template
  1297. ->expects($this->once())
  1298. ->method('addBodyText')
  1299. ->with(
  1300. 'Mr. Initiator User shared »file.txt« with you. Click the button below to open it.',
  1301. 'Mr. Initiator User shared »file.txt« with you.'
  1302. );
  1303. $template
  1304. ->expects($this->once())
  1305. ->method('addBodyButton')
  1306. ->with(
  1307. 'Open »file.txt«',
  1308. 'https://example.com/file.txt'
  1309. );
  1310. $message
  1311. ->expects($this->once())
  1312. ->method('setTo')
  1313. ->with(['john@doe.com']);
  1314. $this->defaults
  1315. ->expects($this->once())
  1316. ->method('getName')
  1317. ->willReturn('UnitTestCloud');
  1318. $message
  1319. ->expects($this->once())
  1320. ->method('setFrom')
  1321. ->with([
  1322. \OCP\Util::getDefaultEmailAddress('UnitTestCloud') => 'UnitTestCloud'
  1323. ]);
  1324. $message
  1325. ->expects($this->never())
  1326. ->method('setReplyTo');
  1327. $template
  1328. ->expects($this->once())
  1329. ->method('addFooter')
  1330. ->with('');
  1331. $template
  1332. ->expects($this->once())
  1333. ->method('setSubject')
  1334. ->with('Mr. Initiator User shared »file.txt« with you');
  1335. $message
  1336. ->expects($this->once())
  1337. ->method('useTemplate')
  1338. ->with($template);
  1339. $this->mailer
  1340. ->expects($this->once())
  1341. ->method('send')
  1342. ->with($message);
  1343. self::invokePrivate(
  1344. $provider,
  1345. 'sendMailNotification',
  1346. [
  1347. 'file.txt',
  1348. 'https://example.com/file.txt',
  1349. 'InitiatorUser',
  1350. 'john@doe.com',
  1351. null,
  1352. ]);
  1353. }
  1354. }