1
0

ShareByMailProviderTest.php 74 KB

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