2727use OCP \EventDispatcher \IEventDispatcher ;
2828use OCP \Files \IRootFolder ;
2929use OCP \Group \ISubAdmin ;
30+ use OCP \IAppConfig ;
3031use OCP \IConfig ;
3132use OCP \IGroup ;
3233use OCP \IL10N ;
@@ -67,6 +68,7 @@ class UsersControllerTest extends TestCase {
6768 private IRootFolder $ rootFolder ;
6869 private IPhoneNumberUtil $ phoneNumberUtil ;
6970 private IAppManager $ appManager ;
71+ private IAppConfig &MockObject $ appConfig ;
7072 private GroupDisplayNameCache &MockObject $ groupDisplayNameCache ;
7173
7274 protected function setUp (): void {
@@ -89,6 +91,7 @@ protected function setUp(): void {
8991 $ this ->eventDispatcher = $ this ->createMock (IEventDispatcher::class);
9092 $ this ->phoneNumberUtil = new PhoneNumberUtil ();
9193 $ this ->appManager = $ this ->createMock (IAppManager::class);
94+ $ this ->appConfig = $ this ->createMock (IAppConfig::class);
9295 $ this ->rootFolder = $ this ->createMock (IRootFolder::class);
9396 $ this ->groupDisplayNameCache = $ this ->createMock (GroupDisplayNameCache::class);
9497
@@ -117,6 +120,7 @@ protected function setUp(): void {
117120 $ this ->eventDispatcher ,
118121 $ this ->phoneNumberUtil ,
119122 $ this ->appManager ,
123+ $ this ->appConfig ,
120124 $ this ->groupDisplayNameCache ,
121125 ])
122126 ->onlyMethods (['fillStorageInfo ' ])
@@ -270,6 +274,7 @@ public function testGetUsersDetailsReturnsEmptyGroupsList(): void {
270274 $ this ->eventDispatcher ,
271275 $ this ->phoneNumberUtil ,
272276 $ this ->appManager ,
277+ $ this ->appConfig ,
273278 $ this ->groupDisplayNameCache ,
274279 ])
275280 ->onlyMethods (['getUserData ' ])
@@ -587,6 +592,7 @@ public function testAddUserSuccessfulWithDisplayName(): void {
587592 $ this ->eventDispatcher ,
588593 $ this ->phoneNumberUtil ,
589594 $ this ->appManager ,
595+ $ this ->appConfig ,
590596 $ this ->groupDisplayNameCache ,
591597 ])
592598 ->onlyMethods (['editUser ' ])
@@ -730,6 +736,67 @@ public function testAddUserSuccessfulGeneratePassword(): void {
730736 ));
731737 }
732738
739+ /**
740+ * `newUser.sendEmail` has to be read as a boolean. It is stored as an untyped
741+ * 'yes'/'no' string on instances created before Nextcloud 33 and as a typed
742+ * boolean once the account settings toggle has been used, so comparing it to
743+ * the string 'yes' silently skipped the mail on upgraded instances.
744+ */
745+ #[\PHPUnit \Framework \Attributes \DataProvider('dataAddUserWelcomeMail ' )]
746+ public function testAddUserSendsWelcomeMailWhenEnabled (bool $ enabled ): void {
747+ $ this ->appConfig
748+ ->expects ($ this ->atLeastOnce ())
749+ ->method ('getValueBool ' )
750+ ->with ('core ' , 'newUser.sendEmail ' , true )
751+ ->willReturn ($ enabled );
752+
753+ $ newUser = $ this ->createMock (IUser::class);
754+ $ newUser ->expects ($ this ->once ())
755+ ->method ('setSystemEMailAddress ' )
756+ ->with ('foo@bar.com ' );
757+ $ this ->userManager
758+ ->expects ($ this ->once ())
759+ ->method ('userExists ' )
760+ ->with ('NewUser ' )
761+ ->willReturn (false );
762+ $ this ->userManager
763+ ->expects ($ this ->once ())
764+ ->method ('createUser ' )
765+ ->willReturn ($ newUser );
766+ $ loggedInUser = $ this ->createMock (IUser::class);
767+ $ loggedInUser
768+ ->method ('getUID ' )
769+ ->willReturn ('adminUser ' );
770+ $ this ->userSession
771+ ->expects ($ this ->once ())
772+ ->method ('getUser ' )
773+ ->willReturn ($ loggedInUser );
774+ $ this ->groupManager
775+ ->expects ($ this ->once ())
776+ ->method ('isAdmin ' )
777+ ->with ('adminUser ' )
778+ ->willReturn (true );
779+
780+ $ emailTemplate = $ this ->createMock (IEMailTemplate::class);
781+ $ this ->newUserMailHelper
782+ ->expects ($ enabled ? $ this ->once () : $ this ->never ())
783+ ->method ('generateTemplate ' )
784+ ->willReturn ($ emailTemplate );
785+ $ this ->newUserMailHelper
786+ ->expects ($ enabled ? $ this ->once () : $ this ->never ())
787+ ->method ('sendMail ' )
788+ ->with ($ newUser , $ emailTemplate );
789+
790+ $ this ->api ->addUser ('NewUser ' , 'PasswordOfTheNewUser ' , '' , 'foo@bar.com ' );
791+ }
792+
793+ public static function dataAddUserWelcomeMail (): array {
794+ return [
795+ 'enabled ' => [true ],
796+ 'disabled ' => [false ],
797+ ];
798+ }
799+
733800 public function testAddUserSuccessfulLowercaseEmail (): void {
734801 $ this ->userManager
735802 ->expects ($ this ->once ())
@@ -3928,6 +3995,7 @@ public function testGetCurrentUserLoggedIn(): void {
39283995 $ this ->eventDispatcher ,
39293996 $ this ->phoneNumberUtil ,
39303997 $ this ->appManager ,
3998+ $ this ->appConfig ,
39313999 $ this ->groupDisplayNameCache ,
39324000 ])
39334001 ->onlyMethods (['getUserData ' ])
@@ -4023,6 +4091,7 @@ public function testGetUser(): void {
40234091 $ this ->eventDispatcher ,
40244092 $ this ->phoneNumberUtil ,
40254093 $ this ->appManager ,
4094+ $ this ->appConfig ,
40264095 $ this ->groupDisplayNameCache ,
40274096 ])
40284097 ->onlyMethods (['getUserData ' ])
0 commit comments