You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ShareByMailProviderTest.php 52KB

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