ShareByMailProviderTest.php 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270
  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@examplelölöl.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. $this->share->expects($this->any())->method('getSharedWith')->willReturn('valid@valid.com');
  380. $node = $this->getMockBuilder('OCP\Files\Node')->getMock();
  381. $node->expects($this->any())->method('getName')->willReturn('fileName');
  382. $this->share->expects($this->any())->method('getNode')->willReturn($node);
  383. $instance = $this->getInstance(['generateToken', 'addShareToDB', 'sendMailNotification']);
  384. $instance->expects($this->once())->method('generateToken')->willReturn('token');
  385. $instance->expects($this->once())->method('addShareToDB')->willReturn(42);
  386. $instance->expects($this->once())->method('sendMailNotification');
  387. $this->urlGenerator->expects($this->once())->method('linkToRouteAbsolute')
  388. ->with('files_sharing.sharecontroller.showShare', ['token' => 'token']);
  389. $instance->expects($this->once())->method('sendMailNotification');
  390. $this->assertSame(42,
  391. $this->invokePrivate($instance, 'createMailShare', [$this->share])
  392. );
  393. }
  394. public function testCreateMailShareFailed() {
  395. $this->expectException(\OC\HintException::class);
  396. $this->share->expects($this->any())->method('getToken')->willReturn('token');
  397. $this->share->expects($this->once())->method('setToken')->with('token');
  398. $this->share->expects($this->any())->method('getSharedWith')->willReturn('valid@valid.com');
  399. $node = $this->getMockBuilder('OCP\Files\Node')->getMock();
  400. $node->expects($this->any())->method('getName')->willReturn('fileName');
  401. $this->share->expects($this->any())->method('getNode')->willReturn($node);
  402. $instance = $this->getInstance(['generateToken', 'addShareToDB', 'sendMailNotification']);
  403. $instance->expects($this->once())->method('generateToken')->willReturn('token');
  404. $instance->expects($this->once())->method('addShareToDB')->willReturn(42);
  405. $instance->expects($this->once())->method('sendMailNotification');
  406. $this->urlGenerator->expects($this->once())->method('linkToRouteAbsolute')
  407. ->with('files_sharing.sharecontroller.showShare', ['token' => 'token']);
  408. $instance->expects($this->once())->method('sendMailNotification')
  409. ->willReturnCallback(
  410. function () {
  411. throw new \Exception('should be converted to a hint exception');
  412. }
  413. );
  414. $this->assertSame(42,
  415. $this->invokePrivate($instance, 'createMailShare', [$this->share])
  416. );
  417. }
  418. public function testGenerateToken() {
  419. $instance = $this->getInstance();
  420. $this->secureRandom->expects($this->once())->method('generate')->willReturn('token');
  421. $this->assertSame('token',
  422. $this->invokePrivate($instance, 'generateToken')
  423. );
  424. }
  425. public function testAddShareToDB() {
  426. $itemSource = 11;
  427. $itemType = 'file';
  428. $shareWith = 'user@server.com';
  429. $sharedBy = 'user1';
  430. $uidOwner = 'user2';
  431. $permissions = 1;
  432. $token = 'token';
  433. $password = 'password';
  434. $sendPasswordByTalk = true;
  435. $hideDownload = true;
  436. $expiration = new \DateTime();
  437. $instance = $this->getInstance();
  438. $id = $this->invokePrivate(
  439. $instance,
  440. 'addShareToDB',
  441. [
  442. $itemSource,
  443. $itemType,
  444. $shareWith,
  445. $sharedBy,
  446. $uidOwner,
  447. $permissions,
  448. $token,
  449. $password,
  450. $sendPasswordByTalk,
  451. $hideDownload,
  452. $expiration
  453. ]
  454. );
  455. $qb = $this->connection->getQueryBuilder();
  456. $qb->select('*')
  457. ->from('share')
  458. ->where($qb->expr()->eq('id', $qb->createNamedParameter($id)));
  459. $qResult = $qb->execute();
  460. $result = $qResult->fetchAll();
  461. $qResult->closeCursor();
  462. $this->assertSame(1, count($result));
  463. $this->assertSame($itemSource, (int)$result[0]['item_source']);
  464. $this->assertSame($itemType, $result[0]['item_type']);
  465. $this->assertSame($shareWith, $result[0]['share_with']);
  466. $this->assertSame($sharedBy, $result[0]['uid_initiator']);
  467. $this->assertSame($uidOwner, $result[0]['uid_owner']);
  468. $this->assertSame($permissions, (int)$result[0]['permissions']);
  469. $this->assertSame($token, $result[0]['token']);
  470. $this->assertSame($password, $result[0]['password']);
  471. $this->assertSame($sendPasswordByTalk, (bool)$result[0]['password_by_talk']);
  472. $this->assertSame($hideDownload, (bool)$result[0]['hide_download']);
  473. $this->assertSame($expiration->getTimestamp(), \DateTime::createFromFormat('Y-m-d H:i:s', $result[0]['expiration'])->getTimestamp());
  474. }
  475. public function testUpdate() {
  476. $itemSource = 11;
  477. $itemType = 'file';
  478. $shareWith = 'user@server.com';
  479. $sharedBy = 'user1';
  480. $uidOwner = 'user2';
  481. $permissions = 1;
  482. $token = 'token';
  483. $note = 'personal note';
  484. $instance = $this->getInstance();
  485. $id = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token, $note);
  486. $this->share->expects($this->once())->method('getPermissions')->willReturn($permissions + 1);
  487. $this->share->expects($this->once())->method('getShareOwner')->willReturn($uidOwner);
  488. $this->share->expects($this->once())->method('getSharedBy')->willReturn($sharedBy);
  489. $this->share->expects($this->any())->method('getNote')->willReturn($note);
  490. $this->share->expects($this->atLeastOnce())->method('getId')->willReturn($id);
  491. $this->assertSame($this->share,
  492. $instance->update($this->share)
  493. );
  494. $qb = $this->connection->getQueryBuilder();
  495. $qb->select('*')
  496. ->from('share')
  497. ->where($qb->expr()->eq('id', $qb->createNamedParameter($id)));
  498. $qResult = $qb->execute();
  499. $result = $qResult->fetchAll();
  500. $qResult->closeCursor();
  501. $this->assertSame(1, count($result));
  502. $this->assertSame($itemSource, (int)$result[0]['item_source']);
  503. $this->assertSame($itemType, $result[0]['item_type']);
  504. $this->assertSame($shareWith, $result[0]['share_with']);
  505. $this->assertSame($sharedBy, $result[0]['uid_initiator']);
  506. $this->assertSame($uidOwner, $result[0]['uid_owner']);
  507. $this->assertSame($permissions + 1, (int)$result[0]['permissions']);
  508. $this->assertSame($token, $result[0]['token']);
  509. $this->assertSame($note, $result[0]['note']);
  510. }
  511. public function dataUpdateSendPassword() {
  512. return [
  513. ['password', 'hashed', 'hashed new', false, false, true],
  514. ['', 'hashed', 'hashed new', false, false, false],
  515. [null, 'hashed', 'hashed new', false, false, false],
  516. ['password', 'hashed', 'hashed', false, false, false],
  517. ['password', 'hashed', 'hashed new', false, true, false],
  518. ['password', 'hashed', 'hashed new', true, false, true],
  519. ['password', 'hashed', 'hashed', true, false, true],
  520. ];
  521. }
  522. /**
  523. * @dataProvider dataUpdateSendPassword
  524. *
  525. * @param string|null plainTextPassword
  526. * @param string originalPassword
  527. * @param string newPassword
  528. * @param string originalSendPasswordByTalk
  529. * @param string newSendPasswordByTalk
  530. * @param bool sendMail
  531. */
  532. public function testUpdateSendPassword($plainTextPassword, string $originalPassword, string $newPassword, $originalSendPasswordByTalk, $newSendPasswordByTalk, bool $sendMail) {
  533. $node = $this->getMockBuilder(File::class)->getMock();
  534. $node->expects($this->any())->method('getName')->willReturn('filename');
  535. $this->settingsManager->method('sendPasswordByMail')->willReturn(true);
  536. $originalShare = $this->getMockBuilder(IShare::class)->getMock();
  537. $originalShare->expects($this->any())->method('getSharedWith')->willReturn('receiver@example.com');
  538. $originalShare->expects($this->any())->method('getNode')->willReturn($node);
  539. $originalShare->expects($this->any())->method('getId')->willReturn(42);
  540. $originalShare->expects($this->any())->method('getPassword')->willReturn($originalPassword);
  541. $originalShare->expects($this->any())->method('getSendPasswordByTalk')->willReturn($originalSendPasswordByTalk);
  542. $share = $this->getMockBuilder(IShare::class)->getMock();
  543. $share->expects($this->any())->method('getSharedWith')->willReturn('receiver@example.com');
  544. $share->expects($this->any())->method('getNode')->willReturn($node);
  545. $share->expects($this->any())->method('getId')->willReturn(42);
  546. $share->expects($this->any())->method('getPassword')->willReturn($newPassword);
  547. $share->expects($this->any())->method('getSendPasswordByTalk')->willReturn($newSendPasswordByTalk);
  548. if ($sendMail) {
  549. $this->mailer->expects($this->once())->method('createEMailTemplate')->with('sharebymail.RecipientPasswordNotification', [
  550. 'filename' => 'filename',
  551. 'password' => $plainTextPassword,
  552. 'initiator' => null,
  553. 'initiatorEmail' => null,
  554. 'shareWith' => 'receiver@example.com',
  555. ]);
  556. $this->mailer->expects($this->once())->method('send');
  557. } else {
  558. $this->mailer->expects($this->never())->method('send');
  559. }
  560. $instance = $this->getInstance(['getShareById', 'createPasswordSendActivity']);
  561. $instance->expects($this->once())->method('getShareById')->willReturn($originalShare);
  562. $this->assertSame($share,
  563. $instance->update($share, $plainTextPassword)
  564. );
  565. }
  566. public function testDelete() {
  567. $instance = $this->getInstance(['removeShareFromTable', 'createShareActivity']);
  568. $this->share->expects($this->once())->method('getId')->willReturn(42);
  569. $instance->expects($this->once())->method('removeShareFromTable')->with(42);
  570. $instance->expects($this->once())->method('createShareActivity')->with($this->share, 'unshare');
  571. $instance->delete($this->share);
  572. }
  573. public function testGetShareById() {
  574. $instance = $this->getInstance(['createShareObject']);
  575. $itemSource = 11;
  576. $itemType = 'file';
  577. $shareWith = 'user@server.com';
  578. $sharedBy = 'user1';
  579. $uidOwner = 'user2';
  580. $permissions = 1;
  581. $token = 'token';
  582. $this->createDummyShare($itemType, $itemSource, $shareWith, "user1wrong", "user2wrong", $permissions, $token);
  583. $id2 = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token);
  584. $instance->expects($this->once())->method('createShareObject')
  585. ->willReturnCallback(
  586. function ($data) use ($uidOwner, $sharedBy, $id2) {
  587. $this->assertSame($uidOwner, $data['uid_owner']);
  588. $this->assertSame($sharedBy, $data['uid_initiator']);
  589. $this->assertSame($id2, (int)$data['id']);
  590. return $this->share;
  591. }
  592. );
  593. $result = $instance->getShareById($id2);
  594. $this->assertInstanceOf('OCP\Share\IShare', $result);
  595. }
  596. public function testGetShareByIdFailed() {
  597. $this->expectException(\OCP\Share\Exceptions\ShareNotFound::class);
  598. $instance = $this->getInstance(['createShareObject']);
  599. $itemSource = 11;
  600. $itemType = 'file';
  601. $shareWith = 'user@server.com';
  602. $sharedBy = 'user1';
  603. $uidOwner = 'user2';
  604. $permissions = 1;
  605. $token = 'token';
  606. $id = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token);
  607. $instance->getShareById($id + 1);
  608. }
  609. public function testGetShareByPath() {
  610. $itemSource = 11;
  611. $itemType = 'file';
  612. $shareWith = 'user@server.com';
  613. $sharedBy = 'user1';
  614. $uidOwner = 'user2';
  615. $permissions = 1;
  616. $token = 'token';
  617. $node = $this->getMockBuilder('OCP\Files\Node')->getMock();
  618. $node->expects($this->once())->method('getId')->willReturn($itemSource);
  619. $instance = $this->getInstance(['createShareObject']);
  620. $this->createDummyShare($itemType, 111, $shareWith, $sharedBy, $uidOwner, $permissions, $token);
  621. $id = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token);
  622. $instance->expects($this->once())->method('createShareObject')
  623. ->willReturnCallback(
  624. function ($data) use ($uidOwner, $sharedBy, $id) {
  625. $this->assertSame($uidOwner, $data['uid_owner']);
  626. $this->assertSame($sharedBy, $data['uid_initiator']);
  627. $this->assertSame($id, (int)$data['id']);
  628. return $this->share;
  629. }
  630. );
  631. $result = $instance->getSharesByPath($node);
  632. $this->assertTrue(is_array($result));
  633. $this->assertSame(1, count($result));
  634. $this->assertInstanceOf('OCP\Share\IShare', $result[0]);
  635. }
  636. public function testGetShareByToken() {
  637. $itemSource = 11;
  638. $itemType = 'file';
  639. $shareWith = 'user@server.com';
  640. $sharedBy = 'user1';
  641. $uidOwner = 'user2';
  642. $permissions = 1;
  643. $token = 'token';
  644. $instance = $this->getInstance(['createShareObject']);
  645. $idMail = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token);
  646. $idPublic = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token, '', IShare::TYPE_LINK);
  647. $this->assertTrue($idMail !== $idPublic);
  648. $instance->expects($this->once())->method('createShareObject')
  649. ->willReturnCallback(
  650. function ($data) use ($idMail) {
  651. $this->assertSame($idMail, (int)$data['id']);
  652. return $this->share;
  653. }
  654. );
  655. $result = $instance->getShareByToken('token');
  656. $this->assertInstanceOf('OCP\Share\IShare', $result);
  657. }
  658. public function testGetShareByTokenFailed() {
  659. $this->expectException(\OCP\Share\Exceptions\ShareNotFound::class);
  660. $itemSource = 11;
  661. $itemType = 'file';
  662. $shareWith = 'user@server.com';
  663. $sharedBy = 'user1';
  664. $uidOwner = 'user2';
  665. $permissions = 1;
  666. $token = 'token';
  667. $instance = $this->getInstance(['createShareObject']);
  668. $idMail = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token);
  669. $idPublic = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, "token2", '', IShare::TYPE_LINK);
  670. $this->assertTrue($idMail !== $idPublic);
  671. $this->assertInstanceOf('OCP\Share\IShare',
  672. $instance->getShareByToken('token2')
  673. );
  674. }
  675. public function testRemoveShareFromTable() {
  676. $itemSource = 11;
  677. $itemType = 'file';
  678. $shareWith = 'user@server.com';
  679. $sharedBy = 'user1';
  680. $uidOwner = 'user2';
  681. $permissions = 1;
  682. $token = 'token';
  683. $instance = $this->getInstance();
  684. $id = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token);
  685. $query = $this->connection->getQueryBuilder();
  686. $query->select('*')->from('share')
  687. ->where($query->expr()->eq('id', $query->createNamedParameter($id)));
  688. $result = $query->execute();
  689. $before = $result->fetchAll();
  690. $result->closeCursor();
  691. $this->assertTrue(is_array($before));
  692. $this->assertSame(1, count($before));
  693. $this->invokePrivate($instance, 'removeShareFromTable', [$id]);
  694. $query = $this->connection->getQueryBuilder();
  695. $query->select('*')->from('share')
  696. ->where($query->expr()->eq('id', $query->createNamedParameter($id)));
  697. $result = $query->execute();
  698. $after = $result->fetchAll();
  699. $result->closeCursor();
  700. $this->assertTrue(is_array($after));
  701. $this->assertEmpty($after);
  702. }
  703. public function testUserDeleted() {
  704. $itemSource = 11;
  705. $itemType = 'file';
  706. $shareWith = 'user@server.com';
  707. $sharedBy = 'user1';
  708. $uidOwner = 'user2';
  709. $permissions = 1;
  710. $token = 'token';
  711. $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token);
  712. $id = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, 'user2Wrong', $permissions, $token);
  713. $query = $this->connection->getQueryBuilder();
  714. $query->select('*')->from('share');
  715. $result = $query->execute();
  716. $before = $result->fetchAll();
  717. $result->closeCursor();
  718. $this->assertTrue(is_array($before));
  719. $this->assertSame(2, count($before));
  720. $instance = $this->getInstance();
  721. $instance->userDeleted($uidOwner, IShare::TYPE_EMAIL);
  722. $query = $this->connection->getQueryBuilder();
  723. $query->select('*')->from('share');
  724. $result = $query->execute();
  725. $after = $result->fetchAll();
  726. $result->closeCursor();
  727. $this->assertTrue(is_array($after));
  728. $this->assertSame(1, count($after));
  729. $this->assertSame($id, (int)$after[0]['id']);
  730. }
  731. public function testGetRawShare() {
  732. $itemSource = 11;
  733. $itemType = 'file';
  734. $shareWith = 'user@server.com';
  735. $sharedBy = 'user1';
  736. $uidOwner = 'user2';
  737. $permissions = 1;
  738. $token = 'token';
  739. $instance = $this->getInstance();
  740. $id = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token);
  741. $result = $this->invokePrivate($instance, 'getRawShare', [$id]);
  742. $this->assertTrue(is_array($result));
  743. $this->assertSame($itemSource, (int)$result['item_source']);
  744. $this->assertSame($itemType, $result['item_type']);
  745. $this->assertSame($shareWith, $result['share_with']);
  746. $this->assertSame($sharedBy, $result['uid_initiator']);
  747. $this->assertSame($uidOwner, $result['uid_owner']);
  748. $this->assertSame($permissions, (int)$result['permissions']);
  749. $this->assertSame($token, $result['token']);
  750. }
  751. public function testGetRawShareFailed() {
  752. $this->expectException(\OCP\Share\Exceptions\ShareNotFound::class);
  753. $itemSource = 11;
  754. $itemType = 'file';
  755. $shareWith = 'user@server.com';
  756. $sharedBy = 'user1';
  757. $uidOwner = 'user2';
  758. $permissions = 1;
  759. $token = 'token';
  760. $instance = $this->getInstance();
  761. $id = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token);
  762. $this->invokePrivate($instance, 'getRawShare', [$id + 1]);
  763. }
  764. private function createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token, $note = '', $shareType = IShare::TYPE_EMAIL) {
  765. $qb = $this->connection->getQueryBuilder();
  766. $qb->insert('share')
  767. ->setValue('share_type', $qb->createNamedParameter($shareType))
  768. ->setValue('item_type', $qb->createNamedParameter($itemType))
  769. ->setValue('item_source', $qb->createNamedParameter($itemSource))
  770. ->setValue('file_source', $qb->createNamedParameter($itemSource))
  771. ->setValue('share_with', $qb->createNamedParameter($shareWith))
  772. ->setValue('uid_owner', $qb->createNamedParameter($uidOwner))
  773. ->setValue('uid_initiator', $qb->createNamedParameter($sharedBy))
  774. ->setValue('permissions', $qb->createNamedParameter($permissions))
  775. ->setValue('token', $qb->createNamedParameter($token))
  776. ->setValue('note', $qb->createNamedParameter($note))
  777. ->setValue('stime', $qb->createNamedParameter(time()));
  778. /*
  779. * Added to fix https://github.com/owncloud/core/issues/22215
  780. * Can be removed once we get rid of ajax/share.php
  781. */
  782. $qb->setValue('file_target', $qb->createNamedParameter(''));
  783. $qb->execute();
  784. $id = $qb->getLastInsertId();
  785. return (int)$id;
  786. }
  787. public function testGetSharesInFolder() {
  788. $userManager = \OC::$server->getUserManager();
  789. $rootFolder = \OC::$server->getRootFolder();
  790. $provider = $this->getInstance(['sendMailNotification', 'createShareActivity']);
  791. $this->mailer->expects($this->any())->method('validateMailAddress')->willReturn(true);
  792. $u1 = $userManager->createUser('testFed', md5(time()));
  793. $u2 = $userManager->createUser('testFed2', md5(time()));
  794. $folder1 = $rootFolder->getUserFolder($u1->getUID())->newFolder('foo');
  795. $file1 = $folder1->newFile('bar1');
  796. $file2 = $folder1->newFile('bar2');
  797. $share1 = $this->shareManager->newShare();
  798. $share1->setSharedWith('user@server.com')
  799. ->setSharedBy($u1->getUID())
  800. ->setShareOwner($u1->getUID())
  801. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  802. ->setNode($file1);
  803. $provider->create($share1);
  804. $share2 = $this->shareManager->newShare();
  805. $share2->setSharedWith('user@server.com')
  806. ->setSharedBy($u2->getUID())
  807. ->setShareOwner($u1->getUID())
  808. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  809. ->setNode($file2);
  810. $provider->create($share2);
  811. $result = $provider->getSharesInFolder($u1->getUID(), $folder1, false);
  812. $this->assertCount(1, $result);
  813. $this->assertCount(1, $result[$file1->getId()]);
  814. $result = $provider->getSharesInFolder($u1->getUID(), $folder1, true);
  815. $this->assertCount(2, $result);
  816. $this->assertCount(1, $result[$file1->getId()]);
  817. $this->assertCount(1, $result[$file2->getId()]);
  818. $u1->delete();
  819. $u2->delete();
  820. }
  821. public function testGetAccessList() {
  822. $userManager = \OC::$server->getUserManager();
  823. $rootFolder = \OC::$server->getRootFolder();
  824. $provider = $this->getInstance(['sendMailNotification', 'createShareActivity']);
  825. $this->mailer->expects($this->any())->method('validateMailAddress')->willReturn(true);
  826. $u1 = $userManager->createUser('testFed', md5(time()));
  827. $u2 = $userManager->createUser('testFed2', md5(time()));
  828. $folder = $rootFolder->getUserFolder($u1->getUID())->newFolder('foo');
  829. $accessList = $provider->getAccessList([$folder], true);
  830. $this->assertArrayHasKey('public', $accessList);
  831. $this->assertFalse($accessList['public']);
  832. $accessList = $provider->getAccessList([$folder], false);
  833. $this->assertArrayHasKey('public', $accessList);
  834. $this->assertFalse($accessList['public']);
  835. $share1 = $this->shareManager->newShare();
  836. $share1->setSharedWith('user@server.com')
  837. ->setSharedBy($u1->getUID())
  838. ->setShareOwner($u1->getUID())
  839. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  840. ->setNode($folder);
  841. $share1 = $provider->create($share1);
  842. $share2 = $this->shareManager->newShare();
  843. $share2->setSharedWith('user2@server.com')
  844. ->setSharedBy($u2->getUID())
  845. ->setShareOwner($u1->getUID())
  846. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  847. ->setNode($folder);
  848. $share2 = $provider->create($share2);
  849. $accessList = $provider->getAccessList([$folder], true);
  850. $this->assertArrayHasKey('public', $accessList);
  851. $this->assertTrue($accessList['public']);
  852. $accessList = $provider->getAccessList([$folder], false);
  853. $this->assertArrayHasKey('public', $accessList);
  854. $this->assertTrue($accessList['public']);
  855. $provider->delete($share2);
  856. $accessList = $provider->getAccessList([$folder], true);
  857. $this->assertArrayHasKey('public', $accessList);
  858. $this->assertTrue($accessList['public']);
  859. $accessList = $provider->getAccessList([$folder], false);
  860. $this->assertArrayHasKey('public', $accessList);
  861. $this->assertTrue($accessList['public']);
  862. $provider->delete($share1);
  863. $accessList = $provider->getAccessList([$folder], true);
  864. $this->assertArrayHasKey('public', $accessList);
  865. $this->assertFalse($accessList['public']);
  866. $accessList = $provider->getAccessList([$folder], false);
  867. $this->assertArrayHasKey('public', $accessList);
  868. $this->assertFalse($accessList['public']);
  869. $u1->delete();
  870. $u2->delete();
  871. }
  872. public function testSendMailNotificationWithSameUserAndUserEmail() {
  873. $provider = $this->getInstance();
  874. $user = $this->createMock(IUser::class);
  875. $this->userManager
  876. ->expects($this->once())
  877. ->method('get')
  878. ->with('OwnerUser')
  879. ->willReturn($user);
  880. $user
  881. ->expects($this->once())
  882. ->method('getDisplayName')
  883. ->willReturn('Mrs. Owner User');
  884. $message = $this->createMock(Message::class);
  885. $this->mailer
  886. ->expects($this->once())
  887. ->method('createMessage')
  888. ->willReturn($message);
  889. $template = $this->createMock(IEMailTemplate::class);
  890. $this->mailer
  891. ->expects($this->once())
  892. ->method('createEMailTemplate')
  893. ->willReturn($template);
  894. $template
  895. ->expects($this->once())
  896. ->method('addHeader');
  897. $template
  898. ->expects($this->once())
  899. ->method('addHeading')
  900. ->with('Mrs. Owner User shared »file.txt« with you');
  901. $template
  902. ->expects($this->once())
  903. ->method('addBodyText')
  904. ->with(
  905. 'Mrs. Owner User shared »file.txt« with you. Click the button below to open it.',
  906. 'Mrs. Owner User shared »file.txt« with you.'
  907. );
  908. $template
  909. ->expects($this->once())
  910. ->method('addBodyButton')
  911. ->with(
  912. 'Open »file.txt«',
  913. 'https://example.com/file.txt'
  914. );
  915. $message
  916. ->expects($this->once())
  917. ->method('setTo')
  918. ->with(['john@doe.com']);
  919. $this->defaults
  920. ->expects($this->once())
  921. ->method('getName')
  922. ->willReturn('UnitTestCloud');
  923. $message
  924. ->expects($this->once())
  925. ->method('setFrom')
  926. ->with([
  927. \OCP\Util::getDefaultEmailAddress('UnitTestCloud') => 'Mrs. Owner User via UnitTestCloud'
  928. ]);
  929. $user
  930. ->expects($this->once())
  931. ->method('getEMailAddress')
  932. ->willReturn('owner@example.com');
  933. $message
  934. ->expects($this->once())
  935. ->method('setReplyTo')
  936. ->with(['owner@example.com' => 'Mrs. Owner User']);
  937. $this->defaults
  938. ->expects($this->exactly(2))
  939. ->method('getSlogan')
  940. ->willReturn('Testing like 1990');
  941. $template
  942. ->expects($this->once())
  943. ->method('addFooter')
  944. ->with('UnitTestCloud - Testing like 1990');
  945. $template
  946. ->expects($this->once())
  947. ->method('setSubject')
  948. ->with('Mrs. Owner User shared »file.txt« with you');
  949. $message
  950. ->expects($this->once())
  951. ->method('useTemplate')
  952. ->with($template);
  953. $this->mailer
  954. ->expects($this->once())
  955. ->method('send')
  956. ->with($message);
  957. self::invokePrivate(
  958. $provider,
  959. 'sendMailNotification',
  960. [
  961. 'file.txt',
  962. 'https://example.com/file.txt',
  963. 'OwnerUser',
  964. 'john@doe.com',
  965. null,
  966. ]);
  967. }
  968. public function testSendMailNotificationWithDifferentUserAndNoUserEmail() {
  969. $provider = $this->getInstance();
  970. $initiatorUser = $this->createMock(IUser::class);
  971. $this->userManager
  972. ->expects($this->once())
  973. ->method('get')
  974. ->with('InitiatorUser')
  975. ->willReturn($initiatorUser);
  976. $initiatorUser
  977. ->expects($this->once())
  978. ->method('getDisplayName')
  979. ->willReturn('Mr. Initiator User');
  980. $message = $this->createMock(Message::class);
  981. $this->mailer
  982. ->expects($this->once())
  983. ->method('createMessage')
  984. ->willReturn($message);
  985. $template = $this->createMock(IEMailTemplate::class);
  986. $this->mailer
  987. ->expects($this->once())
  988. ->method('createEMailTemplate')
  989. ->willReturn($template);
  990. $template
  991. ->expects($this->once())
  992. ->method('addHeader');
  993. $template
  994. ->expects($this->once())
  995. ->method('addHeading')
  996. ->with('Mr. Initiator User shared »file.txt« with you');
  997. $template
  998. ->expects($this->once())
  999. ->method('addBodyText')
  1000. ->with(
  1001. 'Mr. Initiator User shared »file.txt« with you. Click the button below to open it.',
  1002. 'Mr. Initiator User shared »file.txt« with you.'
  1003. );
  1004. $template
  1005. ->expects($this->once())
  1006. ->method('addBodyButton')
  1007. ->with(
  1008. 'Open »file.txt«',
  1009. 'https://example.com/file.txt'
  1010. );
  1011. $message
  1012. ->expects($this->once())
  1013. ->method('setTo')
  1014. ->with(['john@doe.com']);
  1015. $this->defaults
  1016. ->expects($this->once())
  1017. ->method('getName')
  1018. ->willReturn('UnitTestCloud');
  1019. $message
  1020. ->expects($this->once())
  1021. ->method('setFrom')
  1022. ->with([
  1023. \OCP\Util::getDefaultEmailAddress('UnitTestCloud') => 'Mr. Initiator User via UnitTestCloud'
  1024. ]);
  1025. $message
  1026. ->expects($this->never())
  1027. ->method('setReplyTo');
  1028. $template
  1029. ->expects($this->once())
  1030. ->method('addFooter')
  1031. ->with('');
  1032. $template
  1033. ->expects($this->once())
  1034. ->method('setSubject')
  1035. ->with('Mr. Initiator User shared »file.txt« with you');
  1036. $message
  1037. ->expects($this->once())
  1038. ->method('useTemplate')
  1039. ->with($template);
  1040. $this->mailer
  1041. ->expects($this->once())
  1042. ->method('send')
  1043. ->with($message);
  1044. self::invokePrivate(
  1045. $provider,
  1046. 'sendMailNotification',
  1047. [
  1048. 'file.txt',
  1049. 'https://example.com/file.txt',
  1050. 'InitiatorUser',
  1051. 'john@doe.com',
  1052. null,
  1053. ]);
  1054. }
  1055. }