1515 */
1616class GroupBackendTest extends TestCase {
1717 private GroupBackend $ groupBackend ;
18+ private IDBConnection $ connection ;
1819 private array $ users = [
1920 [
2021 'uid ' => 'user_saml_integration_test_uid1 ' ,
22+ 'displayname ' => 'SAML Integration User One ' ,
23+ 'email ' => 'saml-integration-one@example.test ' ,
2124 'groups ' => [
2225 'user_saml_integration_test_gid1 ' ,
2326 'SAML_user_saml_integration_test_gid2 '
2427 ]
2528 ],
2629 [
2730 'uid ' => 'user_saml_integration_test_uid2 ' ,
31+ 'displayname ' => 'SAML Integration User Two ' ,
32+ 'email ' => 'saml-integration-two@example.test ' ,
2833 'groups ' => [
2934 'user_saml_integration_test_gid1 '
3035 ]
@@ -59,21 +64,25 @@ class GroupBackendTest extends TestCase {
5964 #[Override]
6065 public function setUp (): void {
6166 parent ::setUp ();
62- $ this ->groupBackend = new GroupBackend (\OCP \Server::get (IDBConnection::class), $ this ->createMock (LoggerInterface::class));
67+ $ this ->connection = \OCP \Server::get (IDBConnection::class);
68+ $ this ->resetAccountData ();
69+ $ this ->groupBackend = new GroupBackend ($ this ->connection , $ this ->createMock (LoggerInterface::class));
6370 foreach ($ this ->groups as $ group ) {
6471 $ this ->groupBackend ->createGroup ($ group ['gid ' ], $ group ['saml_gid ' ]);
6572 }
6673 foreach ($ this ->users as $ user ) {
6774 foreach ($ user ['groups ' ] as $ group ) {
6875 $ this ->groupBackend ->addToGroup ($ user ['uid ' ], $ group );
6976 }
77+ $ this ->setAccountData ($ user ['uid ' ], 'displayname ' , $ user ['displayname ' ]);
78+ $ this ->setAccountData ($ user ['uid ' ], 'email ' , $ user ['email ' ]);
7079 }
7180 }
7281
7382 #[Override]
7483 public function tearDown (): void {
7584 parent ::tearDown ();
76- $ this ->groupBackend = new GroupBackend (\ OCP \Server:: get (IDBConnection::class) , $ this ->createMock (LoggerInterface::class));
85+ $ this ->groupBackend = new GroupBackend ($ this -> connection , $ this ->createMock (LoggerInterface::class));
7786 foreach ($ this ->users as $ user ) {
7887 foreach ($ user ['groups ' ] as $ group ) {
7988 $ this ->groupBackend ->removeFromGroup ($ user ['uid ' ], $ group );
@@ -82,6 +91,7 @@ public function tearDown(): void {
8291 foreach ($ this ->groups as $ group ) {
8392 $ this ->groupBackend ->deleteGroup ($ group ['gid ' ]);
8493 }
94+ $ this ->resetAccountData ();
8595 }
8696
8797 public function testInGroup (): void {
@@ -130,4 +140,49 @@ public function testUsersInGroups(): void {
130140 }
131141 }
132142 }
143+
144+ public function testUsersInGroupMatchesDisplayNameAndEmail (): void {
145+ $ groupId = $ this ->groups [0 ]['gid ' ];
146+
147+ $ byDisplayName = $ this ->groupBackend ->usersInGroup ($ groupId , $ this ->users [0 ]['displayname ' ]);
148+ $ this ->assertContains ($ this ->users [0 ]['uid ' ], $ byDisplayName , 'Display name search should return the matching user ' );
149+
150+ $ byEmail = $ this ->groupBackend ->usersInGroup ($ groupId , $ this ->users [1 ]['email ' ]);
151+ $ this ->assertContains ($ this ->users [1 ]['uid ' ], $ byEmail , 'Email search should return the matching user ' );
152+
153+ $ byUid = $ this ->groupBackend ->usersInGroup ($ groupId , $ this ->users [0 ]['uid ' ]);
154+ $ this ->assertContains ($ this ->users [0 ]['uid ' ], $ byUid , 'UID search should still work ' );
155+ }
156+
157+ public function testCountUsersInGroupMatchesDisplayNameAndEmail (): void {
158+ $ groupId = $ this ->groups [0 ]['gid ' ];
159+
160+ $ this ->assertSame (1 , $ this ->groupBackend ->countUsersInGroup ($ groupId , $ this ->users [0 ]['displayname ' ]));
161+ $ this ->assertSame (1 , $ this ->groupBackend ->countUsersInGroup ($ groupId , $ this ->users [1 ]['email ' ]));
162+ $ this ->assertSame (1 , $ this ->groupBackend ->countUsersInGroup ($ groupId , $ this ->users [0 ]['uid ' ]));
163+ }
164+
165+ private function resetAccountData (): void {
166+ foreach ($ this ->users as $ user ) {
167+ $ qb = $ this ->connection ->getQueryBuilder ();
168+ $ qb ->delete ('accounts_data ' )
169+ ->where ($ qb ->expr ()->eq ('uid ' , $ qb ->createNamedParameter ($ user ['uid ' ])))
170+ ->executeStatement ();
171+ }
172+ }
173+
174+ private function setAccountData (string $ uid , string $ name , string $ value ): void {
175+ $ qb = $ this ->connection ->getQueryBuilder ();
176+ $ qb ->delete ('accounts_data ' )
177+ ->where ($ qb ->expr ()->eq ('uid ' , $ qb ->createNamedParameter ($ uid )))
178+ ->andWhere ($ qb ->expr ()->eq ('name ' , $ qb ->createNamedParameter ($ name )))
179+ ->executeStatement ();
180+
181+ $ qb = $ this ->connection ->getQueryBuilder ();
182+ $ qb ->insert ('accounts_data ' )
183+ ->setValue ('uid ' , $ qb ->createNamedParameter ($ uid ))
184+ ->setValue ('name ' , $ qb ->createNamedParameter ($ name ))
185+ ->setValue ('value ' , $ qb ->createNamedParameter ($ value ))
186+ ->executeStatement ();
187+ }
133188}
0 commit comments