ShareByMailProviderTest.php 70 KB

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