diff --git a/lib/Controller/ContactController.php b/lib/Controller/ContactController.php index 874477f38..702258696 100644 --- a/lib/Controller/ContactController.php +++ b/lib/Controller/ContactController.php @@ -72,14 +72,7 @@ public function searchLocation(string $search): JSONResponse { $name = $this->contactsService->getNameFromContact($r); $photo = $this->contactsService->getPhotoUri($r); - - if (\is_string($r['ADR'])) { - $r['ADR'] = [$r['ADR']]; - } - $addresses = []; - foreach ($r['ADR'] as $address) { - $addresses[] = trim(preg_replace("/\n+/", "\n", str_replace(';', "\n", $address))); - } + $addresses = $this->contactsService->getAddress($r); $contacts[] = [ 'name' => $name, @@ -263,7 +256,7 @@ public function searchPhoto(string $search):JSONResponse { if (!$this->contactsService->hasEmail($r) || $this->contactsService->isSystemBook($r)) { continue; } - $email = $this->contactsService->getEmail($r['EMAIL']); + $email = $this->contactsService->getEmail($r); $match = false; foreach ($email as $e) { @@ -276,7 +269,7 @@ public function searchPhoto(string $search):JSONResponse { continue; } - $photo = $this->contactsService->getPhotoUri($r['PHOTO']); + $photo = $this->contactsService->getPhotoUri($r); if ($photo === null) { continue; } @@ -291,4 +284,5 @@ public function searchPhoto(string $search):JSONResponse { return new JSONResponse([], Http::STATUS_NOT_FOUND); } + } diff --git a/lib/Service/ContactsService.php b/lib/Service/ContactsService.php index b4df86740..2d35e3b54 100644 --- a/lib/Service/ContactsService.php +++ b/lib/Service/ContactsService.php @@ -126,4 +126,19 @@ public function filterGroupsWithCount(array $groups, string $search): array { } return array_count_values(array_merge(...$categories)); } + + /** + * @param array $contact + * @return array + */ + public function getAddress(array $contact): array { + if (\is_string($contact['ADR'])) { + $contact['ADR'] = [$contact['ADR']]; + } + $addresses = []; + foreach ($contact['ADR'] as $address) { + $addresses[] = trim(preg_replace("/\n+/", "\n", str_replace(';', "\n", $address))); + } + return $addresses; + } } diff --git a/tests/php/unit/Controller/ContactControllerTest.php b/tests/php/unit/Controller/ContactControllerTest.php index 7c80e97d6..afba3dfb7 100644 --- a/tests/php/unit/Controller/ContactControllerTest.php +++ b/tests/php/unit/Controller/ContactControllerTest.php @@ -70,56 +70,89 @@ public function testSearchLocationDisabled():void { } public function testSearchLocation():void { + $user1 = [ + 'FN' => 'Person 1', + 'ADR' => [ + '33 42nd Street;Random Town;Some State;;United States', + ';;5 Random Ave;12782 Some big city;Yet another state;United States', + ], + 'EMAIL' => [ + 'foo1@example.com', + 'foo2@example.com', + ], + 'LANG' => [ + 'de', + 'en' + ], + 'TZ' => [ + 'Europe/Berlin', + 'UTC' + ], + 'PHOTO' => 'VALUE=uri:http://foo.bar' + ]; + $user2 = [ + 'FN' => 'Person 2', + 'EMAIL' => 'foo3@example.com', + ]; + $user3 = [ + 'ADR' => [ + 'ABC Street 2;01337 Village;;Germany', + ], + 'LANG' => 'en_us', + 'TZ' => 'Australia/Adelaide', + 'PHOTO' => 'VALUE:BINARY:4242424242' + ]; + $user4 = [ + 'isLocalSystemBook' => true, + 'FN' => 'Person 3', + 'ADR' => [ + 'ABC Street 2;01337 Village;;Germany', + ], + 'LANG' => 'en_us', + 'TZ' => 'Australia/Adelaide', + 'PHOTO' => 'VALUE:BINARY:4242424242' + ]; + $this->manager->expects(self::once()) ->method('isEnabled') ->willReturn(true); - - $this->manager->expects(self::exactly(2)) + $this->service + ->method('isSystemBook') + ->willReturnMap([ + [$user1, false], + [$user2, false], + [$user3, false], + [$user4, true], + ]); + $this->service + ->method('getNameFromContact') + ->willReturnMap([ + [$user1, 'Person 1'], + [$user3, ''], + ]); + $this->service->method('getPhotoUri') + ->willReturnMap([ + [$user1, 'http://foo.bar'], + [$user3, null] + ]); + $this->service->method('getAddress') + ->willReturnMap([ + [$user1, [ + "33 42nd Street\nRandom Town\nSome State\nUnited States", + "5 Random Ave\n12782 Some big city\nYet another state\nUnited States", + ]], + [$user3, [ + "ABC Street 2\n01337 Village\nGermany", + ]], + ]); + $this->manager->expects(self::once()) ->method('search') ->with('search 123', ['FN', 'ADR']) ->willReturn([ - [ - 'FN' => 'Person 1', - 'ADR' => [ - '33 42nd Street;Random Town;Some State;;United States', - ';;5 Random Ave;12782 Some big city;Yet another state;United States', - ], - 'EMAIL' => [ - 'foo1@example.com', - 'foo2@example.com', - ], - 'LANG' => [ - 'de', - 'en' - ], - 'TZ' => [ - 'Europe/Berlin', - 'UTC' - ], - 'PHOTO' => 'VALUE=uri:http://foo.bar' - ], - [ - 'FN' => 'Person 2', - 'EMAIL' => 'foo3@example.com', - ], - [ - 'ADR' => [ - 'ABC Street 2;01337 Village;;Germany', - ], - 'LANG' => 'en_us', - 'TZ' => 'Australia/Adelaide', - 'PHOTO' => 'VALUE:BINARY:4242424242' - ], - [ - 'isLocalSystemBook' => true, - 'FN' => 'Person 3', - 'ADR' => [ - 'ABC Street 2;01337 Village;;Germany', - ], - 'LANG' => 'en_us', - 'TZ' => 'Australia/Adelaide', - 'PHOTO' => 'VALUE:BINARY:4242424242' - ], + $user1, + $user2, + $user3, + $user4, ]); $response = $this->controller->searchLocation('search 123'); @@ -144,7 +177,7 @@ public function testSearchLocation():void { $this->assertEquals(200, $response->getStatus()); } - public function testGetGroupMembers() { + public function testGetGroupMembersNoResults() { $this->manager->expects(self::once()) ->method('isEnabled') ->willReturn(true); @@ -152,36 +185,36 @@ public function testGetGroupMembers() { $groupname = 'groupname'; $this->manager->expects(self::once()) ->method('search') - ->with($groupname, ['CATEGORIES'], ['strict_search' => true]) + ->with($groupname, ['CATEGORIES']) ->willReturn([]); $this->controller->getContactGroupMembers($groupname); - - } - - public function testSearchAttendeeDisabled():void { - $this->manager->expects(self::once()) - ->method('isEnabled') - ->willReturn(false); - - $this->manager->expects(self::never()) - ->method('search'); - - $response = $this->controller->searchAttendee('search 123'); - - $this->assertInstanceOf(JSONResponse::class, $response); - $this->assertEquals([], $response->getData()); - $this->assertEquals(200, $response->getStatus()); } - public function testSearchAttendee():void { + public function testGetGroupMembers() { $this->manager->expects(self::once()) ->method('isEnabled') ->willReturn(true); - + $this->service->expects(self::once()) + ->method('hasEmail') + ->willReturn(true); + $this->service->expects(self::once()) + ->method('getNameFromContact') + ->willReturn('Person 1'); + $this->service->expects(self::once()) + ->method('getLanguageId') + ->willReturn('en_us'); + $this->service->expects(self::once()) + ->method('getTimezoneId') + ->willReturn('Australia/Adelaide'); + $this->service->expects(self::once()) + ->method('getEmail') + ->willReturn(['foo1@example.com']); + + $groupname = 'group'; $this->manager->expects(self::once()) ->method('search') - ->with('search 123', ['FN', 'EMAIL']) + ->with($groupname, ['CATEGORIES']) ->willReturn([ [ 'FN' => 'Person 1', @@ -201,11 +234,13 @@ public function testSearchAttendee():void { 'Europe/Berlin', 'UTC' ], - 'PHOTO' => 'VALUE=uri:http://foo.bar' + 'PHOTO' => 'VALUE=uri:http://foo.bar', + 'CATEGORIES' => 'groupname,group', ], [ 'FN' => 'Person 2', 'EMAIL' => 'foo3@example.com', + 'CATEGORIES' => 'groups,asecondgroup', ], [ 'ADR' => [ @@ -213,7 +248,8 @@ public function testSearchAttendee():void { ], 'LANG' => 'en_us', 'TZ' => 'Australia/Adelaide', - 'PHOTO' => 'VALUE:BINARY:4242424242' + 'PHOTO' => 'VALUE:BINARY:4242424242', + 'CATEGORIES' => 'agroupthatswrong,asecondgroup', ], [ 'isLocalSystemBook' => true, @@ -221,10 +257,139 @@ public function testSearchAttendee():void { 'ADR' => [ 'ABC Street 2;01337 Village;;Germany', ], + 'EMAIL' => 'foo4@example.com', 'LANG' => 'en_us', 'TZ' => 'Australia/Adelaide', - 'PHOTO' => 'VALUE:BINARY:4242424242' + 'PHOTO' => 'VALUE:BINARY:4242424242', + 'CATEGORIES' => 'groupppppp', + ], + ]); + + $groupmembers = $this->controller->getContactGroupMembers($groupname); + $this->assertCount(1, $groupmembers->getData()); + } + + public function testSearchAttendeeDisabled():void { + $this->manager->expects(self::once()) + ->method('isEnabled') + ->willReturn(false); + + $this->manager->expects(self::never()) + ->method('search'); + + $response = $this->controller->searchAttendee('search 123'); + + $this->assertInstanceOf(JSONResponse::class, $response); + $this->assertEquals([], $response->getData()); + $this->assertEquals(200, $response->getStatus()); + } + + public function testSearchAttendee():void { + $user1 = [ + 'FN' => 'Person 1', + 'ADR' => [ + '33 42nd Street;Random Town;Some State;;United States', + ';;5 Random Ave;12782 Some big city;Yet another state;United States', + ], + 'EMAIL' => [ + 'foo1@example.com', + 'foo2@example.com', + ], + 'LANG' => [ + 'de', + 'en' + ], + 'TZ' => [ + 'Europe/Berlin', + 'UTC' + ], + 'PHOTO' => 'VALUE=uri:http://foo.bar' + ]; + $user2 = [ + 'FN' => 'Person 2', + 'EMAIL' => 'foo3@example.com', + ]; + $user3 = [ + 'ADR' => [ + 'ABC Street 2;01337 Village;;Germany', + ], + 'LANG' => 'en_us', + 'TZ' => 'Australia/Adelaide', + 'PHOTO' => 'VALUE:BINARY:4242424242' + ]; + $user4 = [ + 'isLocalSystemBook' => true, + 'FN' => 'Person 3', + 'ADR' => [ + 'ABC Street 2;01337 Village;;Germany', + ], + 'LANG' => 'en_us', + 'TZ' => 'Australia/Adelaide', + 'PHOTO' => 'VALUE:BINARY:4242424242', + 'CATEGORIES' => 'search 123' + ]; + + $this->manager->expects(self::once()) + ->method('isEnabled') + ->willReturn(true); + $this->service + ->method('hasEmail') + ->willReturnMap([ + [$user1, true], + [$user2, true], + [$user3, false], + [$user4, true], + ]); + $this->service + ->method('isSystemBook') + ->willReturnMap([ + [$user1, false], + [$user2, false], + [$user3, false], + [$user4, true], + ]); + $this->service + ->method('getNameFromContact') + ->willReturnMap([ + [$user1, 'Person 1'], + [$user2, 'Person 2'], + [$user3, ''], + ]); + $this->service->expects(self::exactly(2)) + ->method('getLanguageId') + ->willReturnMap([ + [$user1, 'de'], + [$user3, 'en_us'], + ]); + $this->service->expects(self::exactly(2)) + ->method('getTimezoneId') + ->willReturnMap([ + [$user1, 'Europe/Berlin'], + [$user3, 'Australia/Adelaide'], + ]); + $this->service->expects(self::exactly(2)) + ->method('getEmail') + ->willReturnMap([ + [$user1, [ + 'foo1@example.com', + 'foo2@example.com', + ] ], + [$user2, ['foo3@example.com']], + [$user3, ['foo5@example.com']], + ]); + $this->service->method('getPhotoUri') + ->willReturnMap([ + [$user1, 'http://foo.bar'], + [$user2, null], + [$user3, null], + [$user4, null], + ]); + $this->manager->expects(self::exactly(2)) + ->method('search') + ->willReturnMap([ + ['search 123', ['FN', 'EMAIL'], [], [$user1, $user2, $user3, $user4]], + ['search 123', ['CATEGORIES'], [], [$user4]] ]); $response = $this->controller->searchAttendee('search 123'); @@ -240,6 +405,7 @@ public function testSearchAttendee():void { 'lang' => 'de', 'tzid' => 'Europe/Berlin', 'photo' => 'http://foo.bar', + 'type' => 'individual' ], [ 'name' => 'Person 2', 'emails' => [ @@ -248,6 +414,7 @@ public function testSearchAttendee():void { 'lang' => null, 'tzid' => null, 'photo' => null, + 'type' => 'individual' ] ], $response->getData()); $this->assertEquals(200, $response->getStatus()); @@ -268,57 +435,81 @@ public function testSearchPhotoDisabled():void { } public function testSearchPhoto():void { + $user1 = [ + 'FN' => 'Person 1', + 'ADR' => [ + '33 42nd Street;Random Town;Some State;;United States', + ';;5 Random Ave;12782 Some big city;Yet another state;United States', + ], + 'EMAIL' => [ + 'foo1@example.com', + 'foo2@example.com', + ], + 'LANG' => [ + 'de', + 'en' + ], + 'TZ' => [ + 'Europe/Berlin', + 'UTC' + ], + 'PHOTO' => 'VALUE=uri:http://foo123.bar' + ]; + $user2 = [ + 'FN' => 'Person 2', + 'EMAIL' => 'foo3@example.com', + 'PHOTO' => 'VALUE=uri:http://foo.bar' + ]; + $user3 = [ + 'ADR' => [ + 'ABC Street 2;01337 Village;;Germany', + ], + 'LANG' => 'en_us', + 'TZ' => 'Australia/Adelaide', + 'PHOTO' => 'VALUE:BINARY:4242424242' + ]; + $user4 = [ + 'isLocalSystemBook' => true, + 'FN' => 'Person 3', + 'ADR' => [ + 'ABC Street 2;01337 Village;;Germany', + ], + 'EMAIL' => 'foo5@example.com', + 'LANG' => 'en_us', + 'TZ' => 'Australia/Adelaide', + 'PHOTO' => 'VALUE=uri:http://foo456.bar' + ]; + $this->manager->expects(self::once()) ->method('isEnabled') ->willReturn(true); - + $this->service->method('hasEmail')->willReturnMap([ + [$user1, true], + [$user2, true], + [$user3, false], + [$user3, true], + ]); + $this->service->method('isSystemBook'); + $this->service->method('getEmail') + ->willReturnMap([ + [$user1, [ + 'foo1@example.com', + 'foo2@example.com', + ] + ], + [$user2, ['foo3@example.com']], + [$user3, ['foo5@example.com']], + ]); + $this->service->method('getNameFromContact')->willReturn('Person 2'); + $this->service->method('getPhotoUri')->willReturn('http://foo.bar'); $this->manager->expects(self::once()) ->method('search') ->with('foo3@example.com', ['EMAIL']) ->willReturn([ - [ - 'FN' => 'Person 1', - 'ADR' => [ - '33 42nd Street;Random Town;Some State;;United States', - ';;5 Random Ave;12782 Some big city;Yet another state;United States', - ], - 'EMAIL' => [ - 'foo1@example.com', - 'foo2@example.com', - ], - 'LANG' => [ - 'de', - 'en' - ], - 'TZ' => [ - 'Europe/Berlin', - 'UTC' - ], - 'PHOTO' => 'VALUE=uri:http://foo123.bar' - ], - [ - 'FN' => 'Person 2', - 'EMAIL' => 'foo3@example.com', - 'PHOTO' => 'VALUE=uri:http://foo.bar' - ], - [ - 'ADR' => [ - 'ABC Street 2;01337 Village;;Germany', - ], - 'LANG' => 'en_us', - 'TZ' => 'Australia/Adelaide', - 'PHOTO' => 'VALUE:BINARY:4242424242' - ], - [ - 'isLocalSystemBook' => true, - 'FN' => 'Person 3', - 'ADR' => [ - 'ABC Street 2;01337 Village;;Germany', - ], - 'LANG' => 'en_us', - 'TZ' => 'Australia/Adelaide', - 'PHOTO' => 'VALUE=uri:http://foo456.bar' - ], + $user1, + $user2, + $user3, + $user4, ]); $response = $this->controller->searchPhoto('foo3@example.com');