1010use OC \Contacts \ContactsMenu \Manager ;
1111use OC \Core \Controller \ContactsMenuController ;
1212use OCP \Contacts \ContactsMenu \IEntry ;
13+ use OCP \ICache ;
14+ use OCP \ICacheFactory ;
1315use OCP \IRequest ;
1416use OCP \IUser ;
1517use OCP \IUserSession ;
@@ -21,6 +23,8 @@ class ContactsMenuControllerTest extends TestCase {
2123 private IUserSession &MockObject $ userSession ;
2224 private Manager &MockObject $ contactsManager ;
2325 private ITeamManager &MockObject $ teamManager ;
26+ private ICacheFactory &MockObject $ cacheFactory ;
27+ private ICache &MockObject $ cache ;
2428
2529 private ContactsMenuController $ controller ;
2630
@@ -32,12 +36,19 @@ protected function setUp(): void {
3236 $ this ->userSession = $ this ->createMock (IUserSession::class);
3337 $ this ->contactsManager = $ this ->createMock (Manager::class);
3438 $ this ->teamManager = $ this ->createMock (ITeamManager::class);
39+ $ this ->cacheFactory = $ this ->createMock (ICacheFactory::class);
40+ $ this ->cache = $ this ->createMock (ICache::class);
41+
42+ $ this ->cacheFactory ->method ('createDistributed ' )
43+ ->with ('contactsmenu-preview ' )
44+ ->willReturn ($ this ->cache );
3545
3646 $ this ->controller = new ContactsMenuController (
3747 $ request ,
3848 $ this ->userSession ,
3949 $ this ->contactsManager ,
4050 $ this ->teamManager ,
51+ $ this ->cacheFactory ,
4152 );
4253 }
4354
@@ -128,56 +139,82 @@ public function testFindOne404(): void {
128139
129140 public function testPreviewAvatarsWithoutTeam (): void {
130141 $ user = $ this ->createMock (IUser::class);
142+ $ user ->method ('getUID ' )->willReturn ('current-user ' );
143+
131144 $ contacts = [
132- $ this ->createMock (IEntry::class),
133- $ this ->createMock (IEntry::class),
134- $ this ->createMock (IEntry::class),
135- $ this ->createMock (IEntry::class),
145+ $ this ->createPreviewEntry ('alice ' , 'Alice ' ),
146+ $ this ->createPreviewEntry ('bob ' , 'Bob ' ),
147+ $ this ->createPreviewEntry ('carol ' , 'Carol ' ),
148+ $ this ->createPreviewEntry ('dave ' , 'Dave ' ),
149+ ];
150+ $ expected = [
151+ $ contacts [0 ]->jsonSerialize (),
152+ $ contacts [1 ]->jsonSerialize (),
153+ $ contacts [2 ]->jsonSerialize (),
136154 ];
137155
138156 $ this ->userSession ->expects ($ this ->once ())
139157 ->method ('getUser ' )
140158 ->willReturn ($ user );
159+ $ this ->cache ->expects ($ this ->once ())
160+ ->method ('get ' )
161+ ->with ('current-user ' )
162+ ->willReturn (null );
141163 $ this ->contactsManager ->expects ($ this ->once ())
142- ->method ('getEntries ' )
143- ->with ($ user , '' )
144- ->willReturn ([
145- ' contacts ' => $ contacts ,
146- ' contactsAppEnabled ' => true ,
147- ] );
164+ ->method ('getPreviewEntries ' )
165+ ->with ($ user , 3 )
166+ ->willReturn ([$ contacts [ 0 ], $ contacts [ 1 ], $ contacts [ 2 ]]);
167+ $ this -> cache -> expects ( $ this -> once ())
168+ -> method ( ' set ' )
169+ -> with ( ' current-user ' , $ expected , 300 );
148170 $ this ->teamManager ->expects ($ this ->never ())
149171 ->method ('getMembersOfTeam ' );
150172
151- $ this ->assertEquals ([
152- $ contacts [0 ],
153- $ contacts [1 ],
154- $ contacts [2 ],
155- ], $ this ->controller ->previewAvatars ());
173+ $ this ->assertEquals ($ expected , $ this ->controller ->previewAvatars ());
174+ }
175+
176+ public function testPreviewAvatarsUsesCache (): void {
177+ $ user = $ this ->createMock (IUser::class);
178+ $ user ->method ('getUID ' )->willReturn ('current-user ' );
179+ $ cached = [
180+ ['uid ' => 'alice ' , 'fullName ' => 'Alice ' , 'isUser ' => true ],
181+ ['uid ' => 'bob ' , 'fullName ' => 'Bob ' , 'isUser ' => true ],
182+ ];
183+
184+ $ this ->userSession ->expects ($ this ->once ())
185+ ->method ('getUser ' )
186+ ->willReturn ($ user );
187+ $ this ->cache ->expects ($ this ->once ())
188+ ->method ('get ' )
189+ ->with ('current-user ' )
190+ ->willReturn ($ cached );
191+ $ this ->contactsManager ->expects ($ this ->never ())
192+ ->method ('getPreviewEntries ' );
193+ $ this ->cache ->expects ($ this ->never ())
194+ ->method ('set ' );
195+
196+ $ this ->assertEquals ($ cached , $ this ->controller ->previewAvatars ());
156197 }
157198
158199 public function testPreviewAvatarsWithTeam (): void {
159200 $ user = $ this ->createMock (IUser::class);
160201 $ user ->method ('getUID ' )->willReturn ('current-user ' );
161202
162- $ alice = $ this ->createMock (IEntry::class);
163- $ alice ->method ('getProperty ' )->with ('UID ' )->willReturn ('alice ' );
164- $ external = $ this ->createMock (IEntry::class);
165- $ external ->method ('getProperty ' )->with ('UID ' )->willReturn ('contact-1 ' );
166- $ bob = $ this ->createMock (IEntry::class);
167- $ bob ->method ('getProperty ' )->with ('UID ' )->willReturn ('bob ' );
168- $ carol = $ this ->createMock (IEntry::class);
169- $ carol ->method ('getProperty ' )->with ('UID ' )->willReturn ('carol ' );
203+ $ cached = [
204+ ['uid ' => 'alice ' , 'fullName ' => 'Alice ' , 'isUser ' => true ],
205+ ['uid ' => 'contact-1 ' , 'fullName ' => 'External ' , 'isUser ' => false ],
206+ ['uid ' => 'bob ' , 'fullName ' => 'Bob ' , 'isUser ' => true ],
207+ ];
170208
171209 $ this ->userSession ->expects ($ this ->once ())
172210 ->method ('getUser ' )
173211 ->willReturn ($ user );
174- $ this ->contactsManager ->expects ($ this ->once ())
175- ->method ('getEntries ' )
176- ->with ($ user , '' )
177- ->willReturn ([
178- 'contacts ' => [$ alice , $ external , $ bob , $ carol ],
179- 'contactsAppEnabled ' => true ,
180- ]);
212+ $ this ->cache ->expects ($ this ->once ())
213+ ->method ('get ' )
214+ ->with ('current-user ' )
215+ ->willReturn ($ cached );
216+ $ this ->contactsManager ->expects ($ this ->never ())
217+ ->method ('getPreviewEntries ' );
181218 $ this ->teamManager ->expects ($ this ->once ())
182219 ->method ('getMembersOfTeam ' )
183220 ->with ('team-id ' , 'current-user ' )
@@ -187,16 +224,31 @@ public function testPreviewAvatarsWithTeam(): void {
187224 'carol ' => 'Carol ' ,
188225 ]);
189226
190- $ this ->assertEquals ([$ alice , $ bob , $ carol ], $ this ->controller ->previewAvatars ('team-id ' ));
227+ $ this ->assertEquals ([
228+ $ cached [0 ],
229+ $ cached [2 ],
230+ ], $ this ->controller ->previewAvatars ('team-id ' ));
191231 }
192232
193233 public function testPreviewAvatarsWithoutUser (): void {
194234 $ this ->userSession ->expects ($ this ->once ())
195235 ->method ('getUser ' )
196236 ->willReturn (null );
197237 $ this ->contactsManager ->expects ($ this ->never ())
198- ->method ('getEntries ' );
238+ ->method ('getPreviewEntries ' );
239+ $ this ->cache ->expects ($ this ->never ())
240+ ->method ('get ' );
199241
200242 $ this ->assertEquals ([], $ this ->controller ->previewAvatars ());
201243 }
244+
245+ private function createPreviewEntry (string $ uid , string $ fullName ): IEntry &MockObject {
246+ $ entry = $ this ->createMock (IEntry::class);
247+ $ entry ->method ('jsonSerialize ' )->willReturn ([
248+ 'uid ' => $ uid ,
249+ 'fullName ' => $ fullName ,
250+ 'isUser ' => true ,
251+ ]);
252+ return $ entry ;
253+ }
202254}
0 commit comments