3838use OCA \User_LDAP \Mapping \GroupMapping ;
3939use OCA \User_LDAP \User \Manager ;
4040use OCA \User_LDAP \User \OfflineUser ;
41+ use OCA \User_LDAP \User \User ;
42+ use OCA \User_LDAP \User_Proxy ;
4143use OCP \GroupInterface ;
4244use OCP \IConfig ;
45+ use OCP \IUser ;
46+ use OCP \IUserManager ;
4347use PHPUnit \Framework \MockObject \MockObject ;
4448use Test \TestCase ;
4549
@@ -54,6 +58,7 @@ class Group_LDAPTest extends TestCase {
5458 private MockObject |Access $ access ;
5559 private MockObject |GroupPluginManager $ pluginManager ;
5660 private MockObject |IConfig $ config ;
61+ private MockObject |IUserManager $ ncUserManager ;
5762 private GroupLDAP $ groupBackend ;
5863
5964 public function setUp (): void {
@@ -62,10 +67,11 @@ public function setUp(): void {
6267 $ this ->access = $ this ->getAccessMock ();
6368 $ this ->pluginManager = $ this ->createMock (GroupPluginManager::class);
6469 $ this ->config = $ this ->createMock (IConfig::class);
70+ $ this ->ncUserManager = $ this ->createMock (IUserManager::class);
6571 }
6672
6773 public function initBackend (): void {
68- $ this ->groupBackend = new GroupLDAP ($ this ->access , $ this ->pluginManager , $ this ->config );
74+ $ this ->groupBackend = new GroupLDAP ($ this ->access , $ this ->pluginManager , $ this ->config , $ this -> ncUserManager );
6975 }
7076
7177 public function testCountEmptySearchString () {
@@ -786,12 +792,14 @@ public function testGetUserGroupsMemberOf() {
786792 $ this ->access ->connection ->hasPrimaryGroups = false ;
787793 $ this ->access ->connection ->hasGidNumber = false ;
788794
795+ $ expectedGroups = ['cn=groupA,dc=foobar ' , 'cn=groupB,dc=foobar ' ];
796+
789797 $ this ->access ->expects ($ this ->any ())
790798 ->method ('username2dn ' )
791799 ->willReturn ($ dn );
792800 $ this ->access ->expects ($ this ->exactly (5 ))
793801 ->method ('readAttribute ' )
794- ->will ($ this ->onConsecutiveCalls ([ ' cn=groupA,dc=foobar ' , ' cn=groupB,dc=foobar ' ] , [], [], [], []));
802+ ->will ($ this ->onConsecutiveCalls ($ expectedGroups , [], [], [], []));
795803 $ this ->access ->expects ($ this ->any ())
796804 ->method ('dn2groupname ' )
797805 ->willReturnArgument (0 );
@@ -802,6 +810,10 @@ public function testGetUserGroupsMemberOf() {
802810 ->method ('isDNPartOfBase ' )
803811 ->willReturn (true );
804812
813+ $ this ->config ->expects ($ this ->once ())
814+ ->method ('setUserValue ' )
815+ ->with ('userX ' , 'user_ldap ' , 'cached-group-memberships- ' , \json_encode ($ expectedGroups ));
816+
805817 $ this ->initBackend ();
806818 $ groups = $ this ->groupBackend ->getUserGroups ('userX ' );
807819
@@ -835,6 +847,34 @@ public function testGetUserGroupsMemberOfDisabled() {
835847 ->method ('nextcloudGroupNames ' )
836848 ->willReturn ([]);
837849
850+ // empty group result should not be oer
851+ $ this ->config ->expects ($ this ->once ())
852+ ->method ('setUserValue ' )
853+ ->with ('userX ' , 'user_ldap ' , 'cached-group-memberships- ' , '[] ' );
854+
855+ $ ldapUser = $ this ->createMock (User::class);
856+
857+ $ this ->access ->userManager ->expects ($ this ->any ())
858+ ->method ('get ' )
859+ ->with ('userX ' )
860+ ->willReturn ($ ldapUser );
861+
862+ $ userBackend = $ this ->createMock (User_Proxy::class);
863+ $ userBackend ->expects ($ this ->once ())
864+ ->method ('userExistsOnLDAP ' )
865+ ->with ('userX ' , true )
866+ ->willReturn (true );
867+
868+ $ ncUser = $ this ->createMock (IUser::class);
869+ $ ncUser ->expects ($ this ->any ())
870+ ->method ('getBackend ' )
871+ ->willReturn ($ userBackend );
872+
873+ $ this ->ncUserManager ->expects ($ this ->once ())
874+ ->method ('get ' )
875+ ->with ('userX ' )
876+ ->willReturn ($ ncUser );
877+
838878 $ this ->initBackend ();
839879 $ this ->groupBackend ->getUserGroups ('userX ' );
840880 }
@@ -861,6 +901,49 @@ public function testGetUserGroupsOfflineUser() {
861901 $ this ->assertTrue (in_array ('groupF ' , $ returnedGroups ));
862902 }
863903
904+ public function testGetUserGroupsUnrecognizedOfflineUser () {
905+ $ this ->enableGroups ();
906+ $ dn = 'cn=userX,dc=foobar ' ;
907+
908+ $ ldapUser = $ this ->createMock (User::class);
909+
910+ $ userBackend = $ this ->createMock (User_Proxy::class);
911+ $ userBackend ->expects ($ this ->once ())
912+ ->method ('userExistsOnLDAP ' )
913+ ->with ('userX ' , true )
914+ ->willReturn (false );
915+
916+ $ ncUser = $ this ->createMock (IUser::class);
917+ $ ncUser ->expects ($ this ->any ())
918+ ->method ('getBackend ' )
919+ ->willReturn ($ userBackend );
920+
921+ $ this ->config ->expects ($ this ->atLeastOnce ())
922+ ->method ('getUserValue ' )
923+ ->with ('userX ' , 'user_ldap ' , 'cached-group-memberships- ' , $ this ->anything ())
924+ ->willReturn (\json_encode (['groupB ' , 'groupF ' ]));
925+
926+ $ this ->access ->expects ($ this ->any ())
927+ ->method ('username2dn ' )
928+ ->willReturn ($ dn );
929+
930+ $ this ->access ->userManager ->expects ($ this ->any ())
931+ ->method ('get ' )
932+ ->with ('userX ' )
933+ ->willReturn ($ ldapUser );
934+
935+ $ this ->ncUserManager ->expects ($ this ->once ())
936+ ->method ('get ' )
937+ ->with ('userX ' )
938+ ->willReturn ($ ncUser );
939+
940+ $ this ->initBackend ();
941+ $ returnedGroups = $ this ->groupBackend ->getUserGroups ('userX ' );
942+ $ this ->assertCount (2 , $ returnedGroups );
943+ $ this ->assertTrue (in_array ('groupB ' , $ returnedGroups ));
944+ $ this ->assertTrue (in_array ('groupF ' , $ returnedGroups ));
945+ }
946+
864947 public function nestedGroupsProvider (): array {
865948 return [
866949 [true ],
0 commit comments