ShareByMailProviderTest.php 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948
  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 OC\Share20\Share;
  10. use OCA\ShareByMail\Settings\SettingsManager;
  11. use OCA\ShareByMail\ShareByMailProvider;
  12. use OCP\Activity\IManager as IActivityManager;
  13. use OCP\Constants;
  14. use OCP\Defaults;
  15. use OCP\EventDispatcher\IEventDispatcher;
  16. use OCP\Files\File;
  17. use OCP\Files\IRootFolder;
  18. use OCP\Files\Node;
  19. use OCP\IConfig;
  20. use OCP\IDBConnection;
  21. use OCP\IL10N;
  22. use OCP\IURLGenerator;
  23. use OCP\IUser;
  24. use OCP\IUserManager;
  25. use OCP\Mail\IEMailTemplate;
  26. use OCP\Mail\IMailer;
  27. use OCP\Mail\IMessage;
  28. use OCP\Security\Events\GenerateSecurePasswordEvent;
  29. use OCP\Security\IHasher;
  30. use OCP\Security\ISecureRandom;
  31. use OCP\Security\PasswordContext;
  32. use OCP\Server;
  33. use OCP\Share\Exceptions\ShareNotFound;
  34. use OCP\Share\IAttributes;
  35. use OCP\Share\IManager;
  36. use OCP\Share\IShare;
  37. use OCP\Util;
  38. use PHPUnit\Framework\MockObject\MockObject;
  39. use Psr\Log\LoggerInterface;
  40. use Test\TestCase;
  41. /**
  42. * Class ShareByMailProviderTest
  43. *
  44. * @package OCA\ShareByMail\Tests
  45. * @group DB
  46. */
  47. class ShareByMailProviderTest extends TestCase {
  48. private IDBConnection $connection;
  49. private IL10N&MockObject $l;
  50. private IShare&MockObject $share;
  51. private IConfig&MockObject $config;
  52. private IMailer&MockObject $mailer;
  53. private IHasher&MockObject $hasher;
  54. private Defaults&MockObject $defaults;
  55. private IManager&MockObject $shareManager;
  56. private LoggerInterface&MockObject $logger;
  57. private IRootFolder&MockObject $rootFolder;
  58. private IUserManager&MockObject $userManager;
  59. private ISecureRandom&MockObject $secureRandom;
  60. private IURLGenerator&MockObject $urlGenerator;
  61. private SettingsManager&MockObject $settingsManager;
  62. private IActivityManager&MockObject $activityManager;
  63. private IEventDispatcher&MockObject $eventDispatcher;
  64. protected function setUp(): void {
  65. parent::setUp();
  66. $this->connection = Server::get(IDBConnection::class);
  67. $this->l = $this->getMockBuilder(IL10N::class)->getMock();
  68. $this->l->method('t')
  69. ->willReturnCallback(function ($text, $parameters = []) {
  70. return vsprintf($text, $parameters);
  71. });
  72. $this->config = $this->getMockBuilder(IConfig::class)->getMock();
  73. $this->logger = $this->getMockBuilder(LoggerInterface::class)->getMock();
  74. $this->rootFolder = $this->getMockBuilder('OCP\Files\IRootFolder')->getMock();
  75. $this->userManager = $this->getMockBuilder(IUserManager::class)->getMock();
  76. $this->secureRandom = $this->getMockBuilder('\OCP\Security\ISecureRandom')->getMock();
  77. $this->mailer = $this->getMockBuilder('\OCP\Mail\IMailer')->getMock();
  78. $this->urlGenerator = $this->getMockBuilder(IURLGenerator::class)->getMock();
  79. $this->share = $this->getMockBuilder(IShare::class)->getMock();
  80. $this->activityManager = $this->getMockBuilder('OCP\Activity\IManager')->getMock();
  81. $this->settingsManager = $this->getMockBuilder(SettingsManager::class)->disableOriginalConstructor()->getMock();
  82. $this->defaults = $this->createMock(Defaults::class);
  83. $this->hasher = $this->getMockBuilder(IHasher::class)->getMock();
  84. $this->eventDispatcher = $this->getMockBuilder(IEventDispatcher::class)->getMock();
  85. $this->shareManager = $this->getMockBuilder(IManager::class)->getMock();
  86. $this->userManager->expects($this->any())->method('userExists')->willReturn(true);
  87. $this->config->expects($this->any())->method('getAppValue')->with('core', 'enforce_strict_email_check')->willReturn('yes');
  88. }
  89. /**
  90. * get instance of Mocked ShareByMailProvider
  91. *
  92. * @param array $mockedMethods internal methods which should be mocked
  93. * @return \PHPUnit\Framework\MockObject\MockObject | ShareByMailProvider
  94. */
  95. private function getInstance(array $mockedMethods = []) {
  96. if (!empty($mockedMethods)) {
  97. return $this->getMockBuilder('OCA\ShareByMail\ShareByMailProvider')
  98. ->setConstructorArgs([
  99. $this->config,
  100. $this->connection,
  101. $this->secureRandom,
  102. $this->userManager,
  103. $this->rootFolder,
  104. $this->l,
  105. $this->logger,
  106. $this->mailer,
  107. $this->urlGenerator,
  108. $this->activityManager,
  109. $this->settingsManager,
  110. $this->defaults,
  111. $this->hasher,
  112. $this->eventDispatcher,
  113. $this->shareManager
  114. ])
  115. ->setMethods($mockedMethods)
  116. ->getMock();
  117. }
  118. return new ShareByMailProvider(
  119. $this->config,
  120. $this->connection,
  121. $this->secureRandom,
  122. $this->userManager,
  123. $this->rootFolder,
  124. $this->l,
  125. $this->logger,
  126. $this->mailer,
  127. $this->urlGenerator,
  128. $this->activityManager,
  129. $this->settingsManager,
  130. $this->defaults,
  131. $this->hasher,
  132. $this->eventDispatcher,
  133. $this->shareManager
  134. );
  135. }
  136. protected function tearDown(): void {
  137. $this->connection
  138. ->getQueryBuilder()
  139. ->delete('share')
  140. ->executeStatement();
  141. parent::tearDown();
  142. }
  143. public function testCreate(): void {
  144. $expectedShare = $this->createMock(IShare::class);
  145. $share = $this->getMockBuilder(IShare::class)->getMock();
  146. $share->expects($this->any())->method('getSharedWith')->willReturn('user1');
  147. $node = $this->getMockBuilder(File::class)->getMock();
  148. $node->expects($this->any())->method('getName')->willReturn('filename');
  149. $instance = $this->getInstance(['getSharedWith', 'createMailShare', 'getRawShare', 'createShareObject', 'createShareActivity', 'sendEmail', 'sendPassword']);
  150. $instance->expects($this->once())->method('getSharedWith')->willReturn([]);
  151. $instance->expects($this->once())->method('createMailShare')->with($share)->willReturn(42);
  152. $instance->expects($this->once())->method('createShareActivity')->with($share);
  153. $instance->expects($this->once())->method('getRawShare')->with(42)->willReturn(['rawShare']);
  154. $instance->expects($this->once())->method('createShareObject')->with(['rawShare'])->willReturn($expectedShare);
  155. $share->expects($this->any())->method('getNode')->willReturn($node);
  156. // As share api link password is not enforced, the password will not be generated.
  157. $this->shareManager->expects($this->once())->method('shareApiLinkEnforcePassword')->willReturn(false);
  158. $this->settingsManager->expects($this->never())->method('sendPasswordByMail');
  159. // Mail notification is triggered by the share manager.
  160. $instance->expects($this->never())->method('sendEmail');
  161. $instance->expects($this->never())->method('sendPassword');
  162. $this->assertSame($expectedShare, $instance->create($share));
  163. }
  164. public function testCreateSendPasswordByMailWithoutEnforcedPasswordProtection(): void {
  165. $expectedShare = $this->createMock(IShare::class);
  166. $node = $this->getMockBuilder(File::class)->getMock();
  167. $node->expects($this->any())->method('getName')->willReturn('filename');
  168. $share = $this->getMockBuilder(IShare::class)->getMock();
  169. $share->expects($this->any())->method('getSharedWith')->willReturn('receiver@examplelölöl.com');
  170. $share->expects($this->any())->method('getSendPasswordByTalk')->willReturn(false);
  171. $share->expects($this->any())->method('getSharedBy')->willReturn('owner');
  172. $share->expects($this->any())->method('getNode')->willReturn($node);
  173. $share->expects($this->any())->method('getId')->willReturn(42);
  174. $share->expects($this->any())->method('getNote')->willReturn('');
  175. $share->expects($this->any())->method('getToken')->willReturn('token');
  176. // Assume the mail address is valid.
  177. $this->mailer->expects($this->any())->method('validateMailAddress')->willReturn(true);
  178. $instance = $this->getInstance(['getSharedWith', 'createMailShare', 'getRawShare', 'createShareObject', 'createShareActivity', 'autoGeneratePassword', 'createPasswordSendActivity', 'sendEmail', 'sendPassword', 'sendPasswordToOwner']);
  179. $instance->expects($this->once())->method('getSharedWith')->willReturn([]);
  180. $instance->expects($this->once())->method('createMailShare')->with($share)->willReturn(42);
  181. $instance->expects($this->once())->method('createShareActivity')->with($share);
  182. $instance->expects($this->once())->method('getRawShare')->with(42)->willReturn(['rawShare']);
  183. $instance->expects($this->once())->method('createShareObject')->with(['rawShare'])->willReturn($expectedShare);
  184. $share->expects($this->any())->method('getNode')->willReturn($node);
  185. // The autogenerated password should not be mailed.
  186. $this->shareManager->expects($this->any())->method('shareApiLinkEnforcePassword')->willReturn(false);
  187. $this->settingsManager->expects($this->any())->method('sendPasswordByMail')->willReturn(true);
  188. $instance->expects($this->never())->method('autoGeneratePassword');
  189. // No password is set and no password sent via talk is requested
  190. $instance->expects($this->once())->method('sendEmail')->with($share, ['receiver@examplelölöl.com']);
  191. $instance->expects($this->never())->method('sendPassword');
  192. $instance->expects($this->never())->method('sendPasswordToOwner');
  193. // The manager sends the mail notification.
  194. // For the sake of testing simplicity, we will handle it ourselves.
  195. $this->assertSame($expectedShare, $instance->create($share));
  196. $instance->sendMailNotification($share);
  197. }
  198. public function testCreateSendPasswordByMailWithPasswordAndWithoutEnforcedPasswordProtectionWithPermanentPassword(): void {
  199. $expectedShare = $this->createMock(IShare::class);
  200. $node = $this->getMockBuilder(File::class)->getMock();
  201. $node->expects($this->any())->method('getName')->willReturn('filename');
  202. $share = $this->getMockBuilder(IShare::class)->getMock();
  203. $share->expects($this->any())->method('getSharedWith')->willReturn('receiver@example.com');
  204. $share->expects($this->any())->method('getSendPasswordByTalk')->willReturn(false);
  205. $share->expects($this->any())->method('getSharedBy')->willReturn('owner');
  206. $share->expects($this->any())->method('getNode')->willReturn($node);
  207. $share->expects($this->any())->method('getId')->willReturn(42);
  208. $share->expects($this->any())->method('getNote')->willReturn('');
  209. $share->expects($this->any())->method('getToken')->willReturn('token');
  210. // Assume the mail address is valid.
  211. $this->mailer->expects($this->any())->method('validateMailAddress')->willReturn(true);
  212. $instance = $this->getInstance(['getSharedWith', 'createMailShare', 'getRawShare', 'createShareObject', 'createShareActivity', 'autoGeneratePassword', 'createPasswordSendActivity', 'sendEmail', 'sendPassword', 'sendPasswordToOwner']);
  213. $instance->expects($this->once())->method('getSharedWith')->willReturn([]);
  214. $instance->expects($this->once())->method('createMailShare')->with($share)->willReturn(42);
  215. $instance->expects($this->once())->method('createShareActivity')->with($share);
  216. $instance->expects($this->once())->method('getRawShare')->with(42)->willReturn(['rawShare', 'password' => 'password']);
  217. $instance->expects($this->once())->method('createShareObject')->with(['rawShare', 'password' => 'password'])->willReturn($expectedShare);
  218. $share->expects($this->any())->method('getNode')->willReturn($node);
  219. $share->expects($this->any())->method('getPassword')->willReturn('password');
  220. $this->hasher->expects($this->once())->method('hash')->with('password')->willReturn('passwordHashed');
  221. $share->expects($this->once())->method('setPassword')->with('passwordHashed');
  222. // The given password (but not the autogenerated password) should not be
  223. // mailed to the receiver of the share because permanent passwords are not enforced.
  224. $this->shareManager->expects($this->any())->method('shareApiLinkEnforcePassword')->willReturn(false);
  225. $this->config->expects($this->once())->method('getSystemValue')->with('sharing.enable_mail_link_password_expiration')->willReturn(false);
  226. $instance->expects($this->never())->method('autoGeneratePassword');
  227. // A password is set but no password sent via talk has been requested
  228. $instance->expects($this->once())->method('sendEmail')->with($share, ['receiver@example.com']);
  229. $instance->expects($this->once())->method('sendPassword')->with($share, 'password');
  230. $instance->expects($this->never())->method('sendPasswordToOwner');
  231. $this->assertSame($expectedShare, $instance->create($share));
  232. $instance->sendMailNotification($share);
  233. }
  234. public function testCreateSendPasswordByMailWithPasswordAndWithoutEnforcedPasswordProtectionWithoutPermanentPassword(): void {
  235. $expectedShare = $this->createMock(IShare::class);
  236. $node = $this->getMockBuilder(File::class)->getMock();
  237. $node->expects($this->any())->method('getName')->willReturn('filename');
  238. $share = $this->getMockBuilder(IShare::class)->getMock();
  239. $share->expects($this->any())->method('getSharedWith')->willReturn('receiver@example.com');
  240. $share->expects($this->any())->method('getSendPasswordByTalk')->willReturn(false);
  241. $share->expects($this->any())->method('getSharedBy')->willReturn('owner');
  242. $share->expects($this->any())->method('getNode')->willReturn($node);
  243. $share->expects($this->any())->method('getId')->willReturn(42);
  244. $share->expects($this->any())->method('getNote')->willReturn('');
  245. $share->expects($this->any())->method('getToken')->willReturn('token');
  246. // Assume the mail address is valid.
  247. $this->mailer->expects($this->any())->method('validateMailAddress')->willReturn(true);
  248. $instance = $this->getInstance([
  249. 'getSharedWith', 'createMailShare', 'getRawShare', 'createShareObject',
  250. 'createShareActivity', 'autoGeneratePassword', 'createPasswordSendActivity',
  251. 'sendEmail', 'sendPassword', 'sendPasswordToOwner',
  252. ]);
  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(): void {
  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(PasswordContext::SHARING));
  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(): void {
  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(): void {
  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(): void {
  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(): void {
  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(): void {
  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(): void {
  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(): void {
  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): void {
  681. $node = $this->createMock(File::class);
  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(): void {
  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(): void {
  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(): void {
  745. $this->expectException(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(): void {
  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->createMock(Node::class);
  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(): void {
  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(): void {
  807. $this->expectException(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(): void {
  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(): void {
  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(): void {
  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(): void {
  900. $this->expectException(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(): void {
  936. $userManager = \OC::$server->getUserManager();
  937. $rootFolder = \OC::$server->getRootFolder();
  938. $this->shareManager->expects($this->any())
  939. ->method('newShare')
  940. ->willReturn(new 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(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(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(): void {
  973. $userManager = \OC::$server->getUserManager();
  974. $rootFolder = \OC::$server->getRootFolder();
  975. $this->shareManager->expects($this->any())
  976. ->method('newShare')
  977. ->willReturn(new 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(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(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(): void {
  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. 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(): void {
  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. 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(): void {
  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. 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(): void {
  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. 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(): void {
  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. 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(): void {
  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. 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. }