11<?php
2+
3+ declare (strict_types=1 );
4+
25/**
36 * @copyright 2017 Christoph Wurst <[email protected] > 47 * @copyright 2017 Lukas Reschke <[email protected] > 2831use OC \Contacts \ContactsMenu \ContactsStore ;
2932use OC \KnownUser \KnownUserService ;
3033use OC \Profile \ProfileManager ;
34+ use OCA \UserStatus \Db \UserStatus ;
35+ use OCA \UserStatus \Service \StatusService ;
3136use OCP \Contacts \IManager ;
3237use OCP \IConfig ;
3338use OCP \IGroupManager ;
4045
4146class ContactsStoreTest extends TestCase {
4247 private ContactsStore $ contactsStore ;
48+ private StatusService |MockObject $ statusService ;
4349 /** @var IManager|MockObject */
4450 private $ contactsManager ;
4551 /** @var ProfileManager */
@@ -61,6 +67,7 @@ protected function setUp(): void {
6167 parent ::setUp ();
6268
6369 $ this ->contactsManager = $ this ->createMock (IManager::class);
70+ $ this ->statusService = $ this ->createMock (StatusService::class);
6471 $ this ->userManager = $ this ->createMock (IUserManager::class);
6572 $ this ->profileManager = $ this ->createMock (ProfileManager::class);
6673 $ this ->urlGenerator = $ this ->createMock (IURLGenerator::class);
@@ -70,13 +77,14 @@ protected function setUp(): void {
7077 $ this ->l10nFactory = $ this ->createMock (IL10NFactory::class);
7178 $ this ->contactsStore = new ContactsStore (
7279 $ this ->contactsManager ,
80+ $ this ->statusService ,
7381 $ this ->config ,
7482 $ this ->profileManager ,
7583 $ this ->userManager ,
7684 $ this ->urlGenerator ,
7785 $ this ->groupManager ,
7886 $ this ->knownUserService ,
79- $ this ->l10nFactory
87+ $ this ->l10nFactory ,
8088 );
8189 }
8290
@@ -964,4 +972,118 @@ public function testFindOneNoMatches() {
964972
965973 $ this ->assertEquals (null , $ entry );
966974 }
975+
976+ public function testGetRecentStatusFirst (): void {
977+ $ user = $ this ->createMock (IUser::class);
978+ $ status1 = new UserStatus ();
979+ $ status1 ->setUserId ('user1 ' );
980+ $ status2 = new UserStatus ();
981+ $ status2 ->setUserId ('user2 ' );
982+ $ this ->statusService ->expects (self ::once ())
983+ ->method ('findAllRecentStatusChanges ' )
984+ ->willReturn ([
985+ $ status1 ,
986+ $ status2 ,
987+ ]);
988+ $ user1 = $ this ->createMock (IUser::class);
989+ $ user1 ->method ('getCloudId ' )->willReturn ('user1@localcloud ' );
990+ $ user2 = $ this ->createMock (IUser::class);
991+ $ user2 ->method ('getCloudId ' )->willReturn ('user2@localcloud ' );
992+ $ this ->userManager ->expects (self ::exactly (2 ))
993+ ->method ('get ' )
994+ ->willReturnCallback (function ($ uid ) use ($ user1 , $ user2 ) {
995+ return match ($ uid ) {
996+ 'user1 ' => $ user1 ,
997+ 'user2 ' => $ user2 ,
998+ };
999+ });
1000+ $ this ->contactsManager
1001+ ->expects (self ::exactly (3 ))
1002+ ->method ('search ' )
1003+ ->willReturnCallback (function ($ uid , $ searchProps , $ options ) {
1004+ return match ([$ uid , $ options ['limit ' ] ?? null ]) {
1005+ ['user1@localcloud ' , 1 ] => [
1006+ [
1007+ 'UID ' => 'user1 ' ,
1008+ 'URI ' => 'user1.vcf ' ,
1009+ ],
1010+ ],
1011+ ['user2@localcloud ' => [], 1 ], // Simulate not found
1012+ ['' , 4 ] => [
1013+ [
1014+ 'UID ' => 'contact1 ' ,
1015+ 'URI ' => 'contact1.vcf ' ,
1016+ ],
1017+ [
1018+ 'UID ' => 'contact2 ' ,
1019+ 'URI ' => 'contact2.vcf ' ,
1020+ ],
1021+ ],
1022+ default => [],
1023+ };
1024+ });
1025+
1026+ $ contacts = $ this ->contactsStore ->getContacts (
1027+ $ user ,
1028+ null ,
1029+ 5 ,
1030+ );
1031+
1032+ self ::assertCount (3 , $ contacts );
1033+ self ::assertEquals ('user1 ' , $ contacts [0 ]->getProperty ('UID ' ));
1034+ self ::assertEquals ('contact1 ' , $ contacts [1 ]->getProperty ('UID ' ));
1035+ self ::assertEquals ('contact2 ' , $ contacts [2 ]->getProperty ('UID ' ));
1036+ }
1037+
1038+ public function testPaginateRecentStatus (): void {
1039+ $ user = $ this ->createMock (IUser::class);
1040+ $ status1 = new UserStatus ();
1041+ $ status1 ->setUserId ('user1 ' );
1042+ $ status2 = new UserStatus ();
1043+ $ status2 ->setUserId ('user2 ' );
1044+ $ status3 = new UserStatus ();
1045+ $ status3 ->setUserId ('user3 ' );
1046+ $ this ->statusService ->expects (self ::never ())
1047+ ->method ('findAllRecentStatusChanges ' );
1048+ $ this ->contactsManager
1049+ ->expects (self ::exactly (2 ))
1050+ ->method ('search ' )
1051+ ->willReturnCallback (function ($ uid , $ searchProps , $ options ) {
1052+ return match ([$ uid , $ options ['limit ' ] ?? null , $ options ['offset ' ] ?? null ]) {
1053+ ['' , 2 , 0 ] => [
1054+ [
1055+ 'UID ' => 'contact1 ' ,
1056+ 'URI ' => 'contact1.vcf ' ,
1057+ ],
1058+ [
1059+ 'UID ' => 'contact2 ' ,
1060+ 'URI ' => 'contact2.vcf ' ,
1061+ ],
1062+ ],
1063+ ['' , 2 , 3 ] => [
1064+ [
1065+ 'UID ' => 'contact3 ' ,
1066+ 'URI ' => 'contact3.vcf ' ,
1067+ ],
1068+ ],
1069+ default => [],
1070+ };
1071+ });
1072+
1073+ $ page1 = $ this ->contactsStore ->getContacts (
1074+ $ user ,
1075+ null ,
1076+ 2 ,
1077+ 0 ,
1078+ );
1079+ $ page2 = $ this ->contactsStore ->getContacts (
1080+ $ user ,
1081+ null ,
1082+ 2 ,
1083+ 3 ,
1084+ );
1085+
1086+ self ::assertCount (2 , $ page1 );
1087+ self ::assertCount (1 , $ page2 );
1088+ }
9671089}
0 commit comments