@@ -240,6 +240,51 @@ public function testCreateSendPasswordByMailWithoutEnforcedPasswordProtection()
240240 );
241241 }
242242
243+ public function testCreateSendPasswordByMailWithPasswordAndWithoutEnforcedPasswordProtection () {
244+ $ share = $ this ->getMockBuilder (IShare::class)->getMock ();
245+ $ share ->expects ($ this ->any ())->method ('getSharedWith ' )->willReturn ('receiver@example.com ' );
246+ $ share ->expects ($ this ->any ())->method ('getSendPasswordByTalk ' )->willReturn (false );
247+ $ share ->expects ($ this ->any ())->method ('getSharedBy ' )->willReturn ('owner ' );
248+
249+ $ node = $ this ->getMockBuilder (File::class)->getMock ();
250+ $ node ->expects ($ this ->any ())->method ('getName ' )->willReturn ('filename ' );
251+
252+ $ instance = $ this ->getInstance (['getSharedWith ' , 'createMailShare ' , 'getRawShare ' , 'createShareObject ' , 'createShareActivity ' , 'autoGeneratePassword ' , 'createPasswordSendActivity ' ]);
253+
254+ $ instance ->expects ($ this ->once ())->method ('getSharedWith ' )->willReturn ([]);
255+ $ instance ->expects ($ this ->once ())->method ('createMailShare ' )->with ($ share )->willReturn (42 );
256+ $ instance ->expects ($ this ->once ())->method ('createShareActivity ' )->with ($ share );
257+ $ instance ->expects ($ this ->once ())->method ('getRawShare ' )->with (42 )->willReturn ('rawShare ' );
258+ $ instance ->expects ($ this ->once ())->method ('createShareObject ' )->with ('rawShare ' )->willReturn ('shareObject ' );
259+ $ share ->expects ($ this ->any ())->method ('getNode ' )->willReturn ($ node );
260+
261+ $ share ->expects ($ this ->once ())->method ('getPassword ' )->willReturn ('password ' );
262+ $ this ->hasher ->expects ($ this ->once ())->method ('hash ' )->with ('password ' )->willReturn ('passwordHashed ' );
263+ $ share ->expects ($ this ->once ())->method ('setPassword ' )->with ('passwordHashed ' );
264+
265+ // The given password (but not the autogenerated password) should be
266+ // mailed to the receiver of the share.
267+ $ this ->settingsManager ->expects ($ this ->any ())->method ('enforcePasswordProtection ' )->willReturn (false );
268+ $ this ->settingsManager ->expects ($ this ->any ())->method ('sendPasswordByMail ' )->willReturn (true );
269+ $ instance ->expects ($ this ->never ())->method ('autoGeneratePassword ' );
270+
271+ $ message = $ this ->createMock (IMessage::class);
272+ $ message ->expects ($ this ->once ())->method ('setTo ' )->with (['receiver@example.com ' ]);
273+ $ this ->mailer ->expects ($ this ->once ())->method ('createMessage ' )->willReturn ($ message );
274+ $ this ->mailer ->expects ($ this ->once ())->method ('createEMailTemplate ' )->with ('sharebymail.RecipientPasswordNotification ' , [
275+ 'filename ' => 'filename ' ,
276+ 'password ' => 'password ' ,
277+ 'initiator ' => 'owner ' ,
278+ 'initiatorEmail ' => null ,
279+ 'shareWith ' => 'receiver@example.com ' ,
280+ ]);
281+ $ this ->mailer ->expects ($ this ->once ())->method ('send ' );
282+
283+ $ this ->assertSame ('shareObject ' ,
284+ $ instance ->create ($ share )
285+ );
286+ }
287+
243288 public function testCreateSendPasswordByMailWithEnforcedPasswordProtection () {
244289 $ share = $ this ->getMockBuilder (IShare::class)->getMock ();
245290 $ share ->expects ($ this ->any ())->method ('getSharedWith ' )->willReturn ('receiver@example.com ' );
@@ -258,14 +303,70 @@ public function testCreateSendPasswordByMailWithEnforcedPasswordProtection() {
258303 $ instance ->expects ($ this ->once ())->method ('createShareObject ' )->with ('rawShare ' )->willReturn ('shareObject ' );
259304 $ share ->expects ($ this ->any ())->method ('getNode ' )->willReturn ($ node );
260305
306+ $ share ->expects ($ this ->once ())->method ('getPassword ' )->willReturn (null );
307+ $ this ->hasher ->expects ($ this ->once ())->method ('hash ' )->with ('autogeneratedPassword ' )->willReturn ('autogeneratedPasswordHashed ' );
308+ $ share ->expects ($ this ->once ())->method ('setPassword ' )->with ('autogeneratedPasswordHashed ' );
309+
261310 // The autogenerated password should be mailed to the receiver of the share.
262311 $ this ->settingsManager ->expects ($ this ->any ())->method ('enforcePasswordProtection ' )->willReturn (true );
263312 $ this ->settingsManager ->expects ($ this ->any ())->method ('sendPasswordByMail ' )->willReturn (true );
264- $ instance ->expects ($ this ->once ())->method ('autoGeneratePassword ' )->with ($ share )->willReturn ('password ' );
313+ $ instance ->expects ($ this ->once ())->method ('autoGeneratePassword ' )->with ($ share )->willReturn ('autogeneratedPassword ' );
265314
266315 $ message = $ this ->createMock (IMessage::class);
267316 $ message ->expects ($ this ->once ())->method ('setTo ' )->with (['receiver@example.com ' ]);
268317 $ this ->mailer ->expects ($ this ->once ())->method ('createMessage ' )->willReturn ($ message );
318+ $ this ->mailer ->expects ($ this ->once ())->method ('createEMailTemplate ' )->with ('sharebymail.RecipientPasswordNotification ' , [
319+ 'filename ' => 'filename ' ,
320+ 'password ' => 'autogeneratedPassword ' ,
321+ 'initiator ' => 'owner ' ,
322+ 'initiatorEmail ' => null ,
323+ 'shareWith ' => 'receiver@example.com ' ,
324+ ]);
325+ $ this ->mailer ->expects ($ this ->once ())->method ('send ' );
326+
327+ $ this ->assertSame ('shareObject ' ,
328+ $ instance ->create ($ share )
329+ );
330+ }
331+
332+ public function testCreateSendPasswordByMailWithPasswordAndWithEnforcedPasswordProtection () {
333+ $ share = $ this ->getMockBuilder (IShare::class)->getMock ();
334+ $ share ->expects ($ this ->any ())->method ('getSharedWith ' )->willReturn ('receiver@example.com ' );
335+ $ share ->expects ($ this ->any ())->method ('getSendPasswordByTalk ' )->willReturn (false );
336+ $ share ->expects ($ this ->any ())->method ('getSharedBy ' )->willReturn ('owner ' );
337+
338+ $ node = $ this ->getMockBuilder (File::class)->getMock ();
339+ $ node ->expects ($ this ->any ())->method ('getName ' )->willReturn ('filename ' );
340+
341+ $ instance = $ this ->getInstance (['getSharedWith ' , 'createMailShare ' , 'getRawShare ' , 'createShareObject ' , 'createShareActivity ' , 'autoGeneratePassword ' , 'createPasswordSendActivity ' ]);
342+
343+ $ instance ->expects ($ this ->once ())->method ('getSharedWith ' )->willReturn ([]);
344+ $ instance ->expects ($ this ->once ())->method ('createMailShare ' )->with ($ share )->willReturn (42 );
345+ $ instance ->expects ($ this ->once ())->method ('createShareActivity ' )->with ($ share );
346+ $ instance ->expects ($ this ->once ())->method ('getRawShare ' )->with (42 )->willReturn ('rawShare ' );
347+ $ instance ->expects ($ this ->once ())->method ('createShareObject ' )->with ('rawShare ' )->willReturn ('shareObject ' );
348+ $ share ->expects ($ this ->any ())->method ('getNode ' )->willReturn ($ node );
349+
350+ $ share ->expects ($ this ->once ())->method ('getPassword ' )->willReturn ('password ' );
351+ $ this ->hasher ->expects ($ this ->once ())->method ('hash ' )->with ('password ' )->willReturn ('passwordHashed ' );
352+ $ share ->expects ($ this ->once ())->method ('setPassword ' )->with ('passwordHashed ' );
353+
354+ // The given password (but not the autogenerated password) should be
355+ // mailed to the receiver of the share.
356+ $ this ->settingsManager ->expects ($ this ->any ())->method ('enforcePasswordProtection ' )->willReturn (true );
357+ $ this ->settingsManager ->expects ($ this ->any ())->method ('sendPasswordByMail ' )->willReturn (true );
358+ $ instance ->expects ($ this ->never ())->method ('autoGeneratePassword ' );
359+
360+ $ message = $ this ->createMock (IMessage::class);
361+ $ message ->expects ($ this ->once ())->method ('setTo ' )->with (['receiver@example.com ' ]);
362+ $ this ->mailer ->expects ($ this ->once ())->method ('createMessage ' )->willReturn ($ message );
363+ $ this ->mailer ->expects ($ this ->once ())->method ('createEMailTemplate ' )->with ('sharebymail.RecipientPasswordNotification ' , [
364+ 'filename ' => 'filename ' ,
365+ 'password ' => 'password ' ,
366+ 'initiator ' => 'owner ' ,
367+ 'initiatorEmail ' => null ,
368+ 'shareWith ' => 'receiver@example.com ' ,
369+ ]);
269370 $ this ->mailer ->expects ($ this ->once ())->method ('send ' );
270371
271372 $ this ->assertSame ('shareObject ' ,
@@ -291,14 +392,25 @@ public function testCreateSendPasswordByTalkWithEnforcedPasswordProtection() {
291392 $ instance ->expects ($ this ->once ())->method ('createShareObject ' )->with ('rawShare ' )->willReturn ('shareObject ' );
292393 $ share ->expects ($ this ->any ())->method ('getNode ' )->willReturn ($ node );
293394
395+ $ share ->expects ($ this ->once ())->method ('getPassword ' )->willReturn (null );
396+ $ this ->hasher ->expects ($ this ->once ())->method ('hash ' )->with ('autogeneratedPassword ' )->willReturn ('autogeneratedPasswordHashed ' );
397+ $ share ->expects ($ this ->once ())->method ('setPassword ' )->with ('autogeneratedPasswordHashed ' );
398+
294399 // The autogenerated password should be mailed to the owner of the share.
295400 $ this ->settingsManager ->expects ($ this ->any ())->method ('enforcePasswordProtection ' )->willReturn (true );
296401 $ this ->settingsManager ->expects ($ this ->any ())->method ('sendPasswordByMail ' )->willReturn (true );
297- $ instance ->expects ($ this ->once ())->method ('autoGeneratePassword ' )->with ($ share )->willReturn ('password ' );
402+ $ instance ->expects ($ this ->once ())->method ('autoGeneratePassword ' )->with ($ share )->willReturn ('autogeneratedPassword ' );
298403
299404 $ message = $ this ->createMock (IMessage::class);
300405 $ message ->expects ($ this ->once ())->method ('setTo ' )->with (['owner@example.com ' => 'Owner display name ' ]);
301406 $ this ->mailer ->expects ($ this ->once ())->method ('createMessage ' )->willReturn ($ message );
407+ $ this ->mailer ->expects ($ this ->once ())->method ('createEMailTemplate ' )->with ('sharebymail.OwnerPasswordNotification ' , [
408+ 'filename ' => 'filename ' ,
409+ 'password ' => 'autogeneratedPassword ' ,
410+ 'initiator ' => 'Owner display name ' ,
411+ 'initiatorEmail ' => 'owner@example.com ' ,
412+ 'shareWith ' => 'receiver@example.com ' ,
413+ ]);
302414 $ this ->mailer ->expects ($ this ->once ())->method ('send ' );
303415
304416 $ user = $ this ->createMock (IUser::class);
@@ -527,6 +639,13 @@ public function testUpdateSendPassword($plainTextPassword, string $originalPassw
527639 $ share ->expects ($ this ->any ())->method ('getSendPasswordByTalk ' )->willReturn ($ newSendPasswordByTalk );
528640
529641 if ($ sendMail ) {
642+ $ this ->mailer ->expects ($ this ->once ())->method ('createEMailTemplate ' )->with ('sharebymail.RecipientPasswordNotification ' , [
643+ 'filename ' => 'filename ' ,
644+ 'password ' => $ plainTextPassword ,
645+ 'initiator ' => null ,
646+ 'initiatorEmail ' => null ,
647+ 'shareWith ' => 'receiver@example.com ' ,
648+ ]);
530649 $ this ->mailer ->expects ($ this ->once ())->method ('send ' );
531650 } else {
532651 $ this ->mailer ->expects ($ this ->never ())->method ('send ' );
0 commit comments