ShareByMailProviderTest.php 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936
  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('Click the button below to open it.');
  1060. $template
  1061. ->expects($this->once())
  1062. ->method('addBodyButton')
  1063. ->with(
  1064. 'Open file.txt',
  1065. 'https://example.com/file.txt'
  1066. );
  1067. $message
  1068. ->expects($this->once())
  1069. ->method('setTo')
  1070. ->with(['john@doe.com']);
  1071. $this->defaults
  1072. ->expects($this->once())
  1073. ->method('getName')
  1074. ->willReturn('UnitTestCloud');
  1075. $message
  1076. ->expects($this->once())
  1077. ->method('setFrom')
  1078. ->with([
  1079. Util::getDefaultEmailAddress('UnitTestCloud') => 'Mrs. Owner User via UnitTestCloud'
  1080. ]);
  1081. $user
  1082. ->expects($this->once())
  1083. ->method('getEMailAddress')
  1084. ->willReturn('owner@example.com');
  1085. $message
  1086. ->expects($this->once())
  1087. ->method('setReplyTo')
  1088. ->with(['owner@example.com' => 'Mrs. Owner User']);
  1089. $this->defaults
  1090. ->expects($this->exactly(2))
  1091. ->method('getSlogan')
  1092. ->willReturn('Testing like 1990');
  1093. $template
  1094. ->expects($this->once())
  1095. ->method('addFooter')
  1096. ->with('UnitTestCloud - Testing like 1990');
  1097. $template
  1098. ->expects($this->once())
  1099. ->method('setSubject')
  1100. ->with('Mrs. Owner User shared file.txt with you');
  1101. $message
  1102. ->expects($this->once())
  1103. ->method('useTemplate')
  1104. ->with($template);
  1105. $this->mailer->expects($this->once())
  1106. ->method('validateMailAddress')
  1107. ->willReturn(true);
  1108. $this->mailer
  1109. ->expects($this->once())
  1110. ->method('send')
  1111. ->with($message);
  1112. $this->urlGenerator->expects($this->once())->method('linkToRouteAbsolute')
  1113. ->with('files_sharing.sharecontroller.showShare', ['token' => 'token'])
  1114. ->willReturn('https://example.com/file.txt');
  1115. $node = $this->getMockBuilder(File::class)->getMock();
  1116. $node->expects($this->any())->method('getName')->willReturn('file.txt');
  1117. $share = $this->getMockBuilder(IShare::class)->getMock();
  1118. $share->expects($this->any())->method('getSharedBy')->willReturn('OwnerUser');
  1119. $share->expects($this->any())->method('getSharedWith')->willReturn('john@doe.com');
  1120. $share->expects($this->any())->method('getNode')->willReturn($node);
  1121. $share->expects($this->any())->method('getId')->willReturn(42);
  1122. $share->expects($this->any())->method('getNote')->willReturn('');
  1123. $share->expects($this->any())->method('getToken')->willReturn('token');
  1124. self::invokePrivate(
  1125. $provider,
  1126. 'sendMailNotification',
  1127. [$share]
  1128. );
  1129. }
  1130. public function testSendMailNotificationWithSameUserAndUserEmailAndNote(): void {
  1131. $provider = $this->getInstance();
  1132. $user = $this->createMock(IUser::class);
  1133. $this->settingsManager->expects($this->any())->method('replyToInitiator')->willReturn(true);
  1134. $this->userManager
  1135. ->expects($this->once())
  1136. ->method('get')
  1137. ->with('OwnerUser')
  1138. ->willReturn($user);
  1139. $user
  1140. ->expects($this->once())
  1141. ->method('getDisplayName')
  1142. ->willReturn('Mrs. Owner User');
  1143. $message = $this->createMock(Message::class);
  1144. $this->mailer
  1145. ->expects($this->once())
  1146. ->method('createMessage')
  1147. ->willReturn($message);
  1148. $template = $this->createMock(IEMailTemplate::class);
  1149. $this->mailer
  1150. ->expects($this->once())
  1151. ->method('createEMailTemplate')
  1152. ->willReturn($template);
  1153. $template
  1154. ->expects($this->once())
  1155. ->method('addHeader');
  1156. $template
  1157. ->expects($this->once())
  1158. ->method('addHeading')
  1159. ->with('Mrs. Owner User shared file.txt with you');
  1160. $template
  1161. ->expects($this->once())
  1162. ->method('addBodyText')
  1163. ->with('Click the button below to open it.');
  1164. $this->urlGenerator->expects($this->once())->method('imagePath')
  1165. ->with('core', 'caldav/description.png')
  1166. ->willReturn('core/img/caldav/description.png');
  1167. $this->urlGenerator->expects($this->once())->method('getAbsoluteURL')
  1168. ->with('core/img/caldav/description.png')
  1169. ->willReturn('https://example.com/core/img/caldav/description.png');
  1170. $template
  1171. ->expects($this->once())
  1172. ->method('addBodyListItem')
  1173. ->with(
  1174. 'This is a note to the recipient',
  1175. 'Note:',
  1176. 'https://example.com/core/img/caldav/description.png',
  1177. 'This is a note to the recipient'
  1178. );
  1179. $template
  1180. ->expects($this->once())
  1181. ->method('addBodyButton')
  1182. ->with(
  1183. 'Open file.txt',
  1184. 'https://example.com/file.txt'
  1185. );
  1186. $message
  1187. ->expects($this->once())
  1188. ->method('setTo')
  1189. ->with(['john@doe.com']);
  1190. $this->defaults
  1191. ->expects($this->once())
  1192. ->method('getName')
  1193. ->willReturn('UnitTestCloud');
  1194. $message
  1195. ->expects($this->once())
  1196. ->method('setFrom')
  1197. ->with([
  1198. Util::getDefaultEmailAddress('UnitTestCloud') => 'Mrs. Owner User via UnitTestCloud'
  1199. ]);
  1200. $user
  1201. ->expects($this->once())
  1202. ->method('getEMailAddress')
  1203. ->willReturn('owner@example.com');
  1204. $message
  1205. ->expects($this->once())
  1206. ->method('setReplyTo')
  1207. ->with(['owner@example.com' => 'Mrs. Owner User']);
  1208. $this->defaults
  1209. ->expects($this->exactly(2))
  1210. ->method('getSlogan')
  1211. ->willReturn('Testing like 1990');
  1212. $template
  1213. ->expects($this->once())
  1214. ->method('addFooter')
  1215. ->with('UnitTestCloud - Testing like 1990');
  1216. $template
  1217. ->expects($this->once())
  1218. ->method('setSubject')
  1219. ->with('Mrs. Owner User shared file.txt with you');
  1220. $message
  1221. ->expects($this->once())
  1222. ->method('useTemplate')
  1223. ->with($template);
  1224. $this->mailer->expects($this->once())
  1225. ->method('validateMailAddress')
  1226. ->willReturn(true);
  1227. $this->mailer
  1228. ->expects($this->once())
  1229. ->method('send')
  1230. ->with($message);
  1231. $this->urlGenerator->expects($this->once())->method('linkToRouteAbsolute')
  1232. ->with('files_sharing.sharecontroller.showShare', ['token' => 'token'])
  1233. ->willReturn('https://example.com/file.txt');
  1234. $node = $this->getMockBuilder(File::class)->getMock();
  1235. $node->expects($this->any())->method('getName')->willReturn('file.txt');
  1236. $share = $this->getMockBuilder(IShare::class)->getMock();
  1237. $share->expects($this->any())->method('getSharedBy')->willReturn('OwnerUser');
  1238. $share->expects($this->any())->method('getSharedWith')->willReturn('john@doe.com');
  1239. $share->expects($this->any())->method('getNode')->willReturn($node);
  1240. $share->expects($this->any())->method('getId')->willReturn(42);
  1241. $share->expects($this->any())->method('getNote')->willReturn('This is a note to the recipient');
  1242. $share->expects($this->any())->method('getToken')->willReturn('token');
  1243. self::invokePrivate(
  1244. $provider,
  1245. 'sendMailNotification',
  1246. [$share]
  1247. );
  1248. }
  1249. public function testSendMailNotificationWithSameUserAndUserEmailAndExpiration(): void {
  1250. $provider = $this->getInstance();
  1251. $user = $this->createMock(IUser::class);
  1252. $this->settingsManager->expects($this->any())->method('replyToInitiator')->willReturn(true);
  1253. $this->userManager
  1254. ->expects($this->once())
  1255. ->method('get')
  1256. ->with('OwnerUser')
  1257. ->willReturn($user);
  1258. $user
  1259. ->expects($this->once())
  1260. ->method('getDisplayName')
  1261. ->willReturn('Mrs. Owner User');
  1262. $message = $this->createMock(Message::class);
  1263. $this->mailer
  1264. ->expects($this->once())
  1265. ->method('createMessage')
  1266. ->willReturn($message);
  1267. $template = $this->createMock(IEMailTemplate::class);
  1268. $this->mailer
  1269. ->expects($this->once())
  1270. ->method('createEMailTemplate')
  1271. ->willReturn($template);
  1272. $template
  1273. ->expects($this->once())
  1274. ->method('addHeader');
  1275. $template
  1276. ->expects($this->once())
  1277. ->method('addHeading')
  1278. ->with('Mrs. Owner User shared file.txt with you');
  1279. $template
  1280. ->expects($this->once())
  1281. ->method('addBodyText')
  1282. ->with('Click the button below to open it.');
  1283. $expiration = new DateTime('2001-01-01');
  1284. $this->l->expects($this->once())
  1285. ->method('l')
  1286. ->with('date', $expiration, ['width' => 'medium'])
  1287. ->willReturn('2001-01-01');
  1288. $this->urlGenerator->expects($this->once())->method('imagePath')
  1289. ->with('core', 'caldav/time.png')
  1290. ->willReturn('core/img/caldav/time.png');
  1291. $this->urlGenerator->expects($this->once())->method('getAbsoluteURL')
  1292. ->with('core/img/caldav/time.png')
  1293. ->willReturn('https://example.com/core/img/caldav/time.png');
  1294. $template
  1295. ->expects($this->once())
  1296. ->method('addBodyListItem')
  1297. ->with(
  1298. 'This share is valid until 2001-01-01 at midnight',
  1299. 'Expiration:',
  1300. 'https://example.com/core/img/caldav/time.png',
  1301. );
  1302. $template
  1303. ->expects($this->once())
  1304. ->method('addBodyButton')
  1305. ->with(
  1306. 'Open file.txt',
  1307. 'https://example.com/file.txt'
  1308. );
  1309. $message
  1310. ->expects($this->once())
  1311. ->method('setTo')
  1312. ->with(['john@doe.com']);
  1313. $this->defaults
  1314. ->expects($this->once())
  1315. ->method('getName')
  1316. ->willReturn('UnitTestCloud');
  1317. $message
  1318. ->expects($this->once())
  1319. ->method('setFrom')
  1320. ->with([
  1321. Util::getDefaultEmailAddress('UnitTestCloud') => 'Mrs. Owner User via UnitTestCloud'
  1322. ]);
  1323. $user
  1324. ->expects($this->once())
  1325. ->method('getEMailAddress')
  1326. ->willReturn('owner@example.com');
  1327. $message
  1328. ->expects($this->once())
  1329. ->method('setReplyTo')
  1330. ->with(['owner@example.com' => 'Mrs. Owner User']);
  1331. $this->defaults
  1332. ->expects($this->exactly(2))
  1333. ->method('getSlogan')
  1334. ->willReturn('Testing like 1990');
  1335. $template
  1336. ->expects($this->once())
  1337. ->method('addFooter')
  1338. ->with('UnitTestCloud - Testing like 1990');
  1339. $template
  1340. ->expects($this->once())
  1341. ->method('setSubject')
  1342. ->with('Mrs. Owner User shared file.txt with you');
  1343. $message
  1344. ->expects($this->once())
  1345. ->method('useTemplate')
  1346. ->with($template);
  1347. $this->mailer->expects($this->once())
  1348. ->method('validateMailAddress')
  1349. ->willReturn(true);
  1350. $this->mailer
  1351. ->expects($this->once())
  1352. ->method('send')
  1353. ->with($message);
  1354. $this->urlGenerator->expects($this->once())->method('linkToRouteAbsolute')
  1355. ->with('files_sharing.sharecontroller.showShare', ['token' => 'token'])
  1356. ->willReturn('https://example.com/file.txt');
  1357. $node = $this->getMockBuilder(File::class)->getMock();
  1358. $node->expects($this->any())->method('getName')->willReturn('file.txt');
  1359. $share = $this->getMockBuilder(IShare::class)->getMock();
  1360. $share->expects($this->any())->method('getSharedBy')->willReturn('OwnerUser');
  1361. $share->expects($this->any())->method('getSharedWith')->willReturn('john@doe.com');
  1362. $share->expects($this->any())->method('getNode')->willReturn($node);
  1363. $share->expects($this->any())->method('getId')->willReturn(42);
  1364. $share->expects($this->any())->method('getNote')->willReturn('');
  1365. $share->expects($this->any())->method('getExpirationDate')->willReturn($expiration);
  1366. $share->expects($this->any())->method('getToken')->willReturn('token');
  1367. self::invokePrivate(
  1368. $provider,
  1369. 'sendMailNotification',
  1370. [$share]
  1371. );
  1372. }
  1373. public function testSendMailNotificationWithDifferentUserAndNoUserEmail(): void {
  1374. $provider = $this->getInstance();
  1375. $initiatorUser = $this->createMock(IUser::class);
  1376. $this->settingsManager->expects($this->any())->method('replyToInitiator')->willReturn(true);
  1377. $this->userManager
  1378. ->expects($this->once())
  1379. ->method('get')
  1380. ->with('InitiatorUser')
  1381. ->willReturn($initiatorUser);
  1382. $initiatorUser
  1383. ->expects($this->once())
  1384. ->method('getDisplayName')
  1385. ->willReturn('Mr. Initiator User');
  1386. $message = $this->createMock(Message::class);
  1387. $this->mailer
  1388. ->expects($this->once())
  1389. ->method('createMessage')
  1390. ->willReturn($message);
  1391. $template = $this->createMock(IEMailTemplate::class);
  1392. $this->mailer
  1393. ->expects($this->once())
  1394. ->method('createEMailTemplate')
  1395. ->willReturn($template);
  1396. $template
  1397. ->expects($this->once())
  1398. ->method('addHeader');
  1399. $template
  1400. ->expects($this->once())
  1401. ->method('addHeading')
  1402. ->with('Mr. Initiator User shared file.txt with you');
  1403. $template
  1404. ->expects($this->once())
  1405. ->method('addBodyText')
  1406. ->with('Click the button below to open it.');
  1407. $template
  1408. ->expects($this->once())
  1409. ->method('addBodyButton')
  1410. ->with(
  1411. 'Open file.txt',
  1412. 'https://example.com/file.txt'
  1413. );
  1414. $message
  1415. ->expects($this->once())
  1416. ->method('setTo')
  1417. ->with(['john@doe.com']);
  1418. $this->defaults
  1419. ->expects($this->once())
  1420. ->method('getName')
  1421. ->willReturn('UnitTestCloud');
  1422. $message
  1423. ->expects($this->once())
  1424. ->method('setFrom')
  1425. ->with([
  1426. Util::getDefaultEmailAddress('UnitTestCloud') => 'Mr. Initiator User via UnitTestCloud'
  1427. ]);
  1428. $message
  1429. ->expects($this->never())
  1430. ->method('setReplyTo');
  1431. $template
  1432. ->expects($this->once())
  1433. ->method('addFooter')
  1434. ->with('');
  1435. $template
  1436. ->expects($this->once())
  1437. ->method('setSubject')
  1438. ->with('Mr. Initiator User shared file.txt with you');
  1439. $message
  1440. ->expects($this->once())
  1441. ->method('useTemplate')
  1442. ->with($template);
  1443. $this->mailer->expects($this->once())
  1444. ->method('validateMailAddress')
  1445. ->willReturn(true);
  1446. $this->mailer
  1447. ->expects($this->once())
  1448. ->method('send')
  1449. ->with($message);
  1450. $this->urlGenerator->expects($this->once())->method('linkToRouteAbsolute')
  1451. ->with('files_sharing.sharecontroller.showShare', ['token' => 'token'])
  1452. ->willReturn('https://example.com/file.txt');
  1453. $node = $this->getMockBuilder(File::class)->getMock();
  1454. $node->expects($this->any())->method('getName')->willReturn('file.txt');
  1455. $share = $this->getMockBuilder(IShare::class)->getMock();
  1456. $share->expects($this->any())->method('getSharedBy')->willReturn('InitiatorUser');
  1457. $share->expects($this->any())->method('getSharedWith')->willReturn('john@doe.com');
  1458. $share->expects($this->any())->method('getNode')->willReturn($node);
  1459. $share->expects($this->any())->method('getId')->willReturn(42);
  1460. $share->expects($this->any())->method('getNote')->willReturn('');
  1461. $share->expects($this->any())->method('getToken')->willReturn('token');
  1462. self::invokePrivate(
  1463. $provider,
  1464. 'sendMailNotification',
  1465. [$share]
  1466. );
  1467. }
  1468. public function testSendMailNotificationWithSameUserAndUserEmailAndReplyToDesactivate(): void {
  1469. $provider = $this->getInstance();
  1470. $user = $this->createMock(IUser::class);
  1471. $this->settingsManager->expects($this->any())->method('replyToInitiator')->willReturn(false);
  1472. $this->userManager
  1473. ->expects($this->once())
  1474. ->method('get')
  1475. ->with('OwnerUser')
  1476. ->willReturn($user);
  1477. $user
  1478. ->expects($this->once())
  1479. ->method('getDisplayName')
  1480. ->willReturn('Mrs. Owner User');
  1481. $message = $this->createMock(Message::class);
  1482. $this->mailer
  1483. ->expects($this->once())
  1484. ->method('createMessage')
  1485. ->willReturn($message);
  1486. $template = $this->createMock(IEMailTemplate::class);
  1487. $this->mailer
  1488. ->expects($this->once())
  1489. ->method('createEMailTemplate')
  1490. ->willReturn($template);
  1491. $template
  1492. ->expects($this->once())
  1493. ->method('addHeader');
  1494. $template
  1495. ->expects($this->once())
  1496. ->method('addHeading')
  1497. ->with('Mrs. Owner User shared file.txt with you');
  1498. $template
  1499. ->expects($this->once())
  1500. ->method('addBodyText')
  1501. ->with('Click the button below to open it.');
  1502. $template
  1503. ->expects($this->once())
  1504. ->method('addBodyButton')
  1505. ->with(
  1506. 'Open file.txt',
  1507. 'https://example.com/file.txt'
  1508. );
  1509. $message
  1510. ->expects($this->once())
  1511. ->method('setTo')
  1512. ->with(['john@doe.com']);
  1513. $this->defaults
  1514. ->expects($this->once())
  1515. ->method('getName')
  1516. ->willReturn('UnitTestCloud');
  1517. $message
  1518. ->expects($this->once())
  1519. ->method('setFrom')
  1520. ->with([
  1521. Util::getDefaultEmailAddress('UnitTestCloud') => 'UnitTestCloud'
  1522. ]);
  1523. // Since replyToInitiator is false, we never get the initiator email address
  1524. $user
  1525. ->expects($this->never())
  1526. ->method('getEMailAddress');
  1527. $message
  1528. ->expects($this->never())
  1529. ->method('setReplyTo');
  1530. $template
  1531. ->expects($this->once())
  1532. ->method('addFooter')
  1533. ->with('');
  1534. $template
  1535. ->expects($this->once())
  1536. ->method('setSubject')
  1537. ->with('Mrs. Owner User shared file.txt with you');
  1538. $message
  1539. ->expects($this->once())
  1540. ->method('useTemplate')
  1541. ->with($template);
  1542. $this->mailer->expects($this->once())
  1543. ->method('validateMailAddress')
  1544. ->willReturn(true);
  1545. $this->mailer
  1546. ->expects($this->once())
  1547. ->method('send')
  1548. ->with($message);
  1549. $this->urlGenerator->expects($this->once())->method('linkToRouteAbsolute')
  1550. ->with('files_sharing.sharecontroller.showShare', ['token' => 'token'])
  1551. ->willReturn('https://example.com/file.txt');
  1552. $node = $this->getMockBuilder(File::class)->getMock();
  1553. $node->expects($this->any())->method('getName')->willReturn('file.txt');
  1554. $share = $this->getMockBuilder(IShare::class)->getMock();
  1555. $share->expects($this->any())->method('getSharedBy')->willReturn('OwnerUser');
  1556. $share->expects($this->any())->method('getSharedWith')->willReturn('john@doe.com');
  1557. $share->expects($this->any())->method('getNode')->willReturn($node);
  1558. $share->expects($this->any())->method('getId')->willReturn(42);
  1559. $share->expects($this->any())->method('getNote')->willReturn('');
  1560. $share->expects($this->any())->method('getToken')->willReturn('token');
  1561. self::invokePrivate(
  1562. $provider,
  1563. 'sendMailNotification',
  1564. [$share]
  1565. );
  1566. }
  1567. public function testSendMailNotificationWithDifferentUserAndNoUserEmailAndReplyToDesactivate(): void {
  1568. $provider = $this->getInstance();
  1569. $initiatorUser = $this->createMock(IUser::class);
  1570. $this->settingsManager->expects($this->any())->method('replyToInitiator')->willReturn(false);
  1571. $this->userManager
  1572. ->expects($this->once())
  1573. ->method('get')
  1574. ->with('InitiatorUser')
  1575. ->willReturn($initiatorUser);
  1576. $initiatorUser
  1577. ->expects($this->once())
  1578. ->method('getDisplayName')
  1579. ->willReturn('Mr. Initiator User');
  1580. $message = $this->createMock(Message::class);
  1581. $this->mailer
  1582. ->expects($this->once())
  1583. ->method('createMessage')
  1584. ->willReturn($message);
  1585. $template = $this->createMock(IEMailTemplate::class);
  1586. $this->mailer
  1587. ->expects($this->once())
  1588. ->method('createEMailTemplate')
  1589. ->willReturn($template);
  1590. $template
  1591. ->expects($this->once())
  1592. ->method('addHeader');
  1593. $template
  1594. ->expects($this->once())
  1595. ->method('addHeading')
  1596. ->with('Mr. Initiator User shared file.txt with you');
  1597. $template
  1598. ->expects($this->once())
  1599. ->method('addBodyText')
  1600. ->with('Click the button below to open it.');
  1601. $template
  1602. ->expects($this->once())
  1603. ->method('addBodyButton')
  1604. ->with(
  1605. 'Open file.txt',
  1606. 'https://example.com/file.txt'
  1607. );
  1608. $message
  1609. ->expects($this->once())
  1610. ->method('setTo')
  1611. ->with(['john@doe.com']);
  1612. $this->defaults
  1613. ->expects($this->once())
  1614. ->method('getName')
  1615. ->willReturn('UnitTestCloud');
  1616. $message
  1617. ->expects($this->once())
  1618. ->method('setFrom')
  1619. ->with([
  1620. Util::getDefaultEmailAddress('UnitTestCloud') => 'UnitTestCloud'
  1621. ]);
  1622. $message
  1623. ->expects($this->never())
  1624. ->method('setReplyTo');
  1625. $template
  1626. ->expects($this->once())
  1627. ->method('addFooter')
  1628. ->with('');
  1629. $template
  1630. ->expects($this->once())
  1631. ->method('setSubject')
  1632. ->with('Mr. Initiator User shared file.txt with you');
  1633. $message
  1634. ->expects($this->once())
  1635. ->method('useTemplate')
  1636. ->with($template);
  1637. $this->mailer->expects($this->once())
  1638. ->method('validateMailAddress')
  1639. ->willReturn(true);
  1640. $this->mailer
  1641. ->expects($this->once())
  1642. ->method('send')
  1643. ->with($message);
  1644. $this->urlGenerator->expects($this->once())->method('linkToRouteAbsolute')
  1645. ->with('files_sharing.sharecontroller.showShare', ['token' => 'token'])
  1646. ->willReturn('https://example.com/file.txt');
  1647. $node = $this->getMockBuilder(File::class)->getMock();
  1648. $node->expects($this->any())->method('getName')->willReturn('file.txt');
  1649. $share = $this->getMockBuilder(IShare::class)->getMock();
  1650. $share->expects($this->any())->method('getSharedBy')->willReturn('InitiatorUser');
  1651. $share->expects($this->any())->method('getSharedWith')->willReturn('john@doe.com');
  1652. $share->expects($this->any())->method('getNode')->willReturn($node);
  1653. $share->expects($this->any())->method('getId')->willReturn(42);
  1654. $share->expects($this->any())->method('getNote')->willReturn('');
  1655. $share->expects($this->any())->method('getToken')->willReturn('token');
  1656. self::invokePrivate(
  1657. $provider,
  1658. 'sendMailNotification',
  1659. [$share]
  1660. );
  1661. }
  1662. }