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 46KB

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