diff --git a/lib/Service/ContactsService.php b/lib/Service/ContactsService.php index 75c2e2552..825092673 100644 --- a/lib/Service/ContactsService.php +++ b/lib/Service/ContactsService.php @@ -10,83 +10,108 @@ class ContactsService { - public function isSystemBook(array $r): bool { + /** + * @param array $contact + * @return bool + */ + public function isSystemBook(array $contact): bool { // Information about system users is fetched via DAV nowadays return (isset($contact['isLocalSystemBook']) && $contact['isLocalSystemBook'] === true); } - public function hasEmail(array $r): bool { - return !isset($r['EMAIL']); + /** + * @param array $contact + * @return bool + */ + public function hasEmail(array $contact): bool { + return isset($contact['EMAIL']); } /** * Extract name from an array containing a contact's information * - * @param array $r + * @param array $contact * @return string */ - public function getNameFromContact(array $r): string { - return $r['FN'] ?? ''; + public function getNameFromContact(array $contact): string { + return $contact['FN'] ?? ''; } /** * Get photo uri from contact * - * @param string $raw + * @param array $contact * @return string|null */ - public function getPhotoUri(array $r): ?string { - if (!isset($r['PHOTO'])) { + public function getPhotoUri(array $contact): ?string { + if (!isset($contact['PHOTO'])) { return null; } - $raw = $r['PHOTO']; + $raw = $contact['PHOTO']; $uriPrefix = 'VALUE=uri:'; if (str_starts_with($raw, $uriPrefix)) { - return substr($raw, strpos($raw, 'http')); + $strpos = strpos($raw, 'http'); + return $strpos !== false ? substr($raw, $strpos) : null; } return null; } - public function getEmail(array $r): array { - if (\is_string($r['EMAIL'])) { - return [$r['EMAIL']]; + /** + * @param array $contact + * @return string[] + */ + public function getEmail(array $contact): array { + if (\is_string($contact['EMAIL'])) { + return [$contact['EMAIL']]; } - return $r['EMAIL']; + return $contact['EMAIL']; } - public function getTimezoneId(array $r): ?string { - if (!isset($r['TZ'])) { + /** + * @param array $contact + * @return string|null + */ + public function getTimezoneId(array $contact): ?string { + if (!isset($contact['TZ'])) { return null; } - if (\is_array($r['TZ'])) { - return $r['TZ'][0]; + if (\is_array($contact['TZ'])) { + return $contact['TZ'][0]; } - return $r['TZ']; + return $contact['TZ']; } - public function getLanguageId(array $r): ?string { - if (!isset($r['LANG'])) { + /** + * @param array $contact + * @return string|null + */ + public function getLanguageId(array $contact): ?string { + if (!isset($contact['LANG'])) { return null; } - if (\is_array($r['LANG'])) { - return $r['LANG'][0]; + if (\is_array($contact['LANG'])) { + return $contact['LANG'][0]; } - return $r['LANG']; + return $contact['LANG']; } + /** + * @param array $groups + * @param string $search + * @return array + */ public function filterGroupsWithCount(array $groups, string $search): array { //filter to be unique - $categoryMem = []; + $categories = []; foreach ($groups as $group) { - $categoryNames = explode(',', $group['CATEGORIES']); - $categoryMem[] = array_filter($categoryNames, static function ($cat) use ($search) { + $categories[] = array_filter(explode(',', $group['CATEGORIES']), static function ($cat) use ($search) { return str_contains(strtolower($cat), $search); }); } - return array_count_values(array_merge(...$categoryMem)); + return array_count_values(array_merge(...$categories)); } } diff --git a/tests/php/unit/Service/ContactsServiceTest.php b/tests/php/unit/Service/ContactsServiceTest.php new file mode 100644 index 000000000..986573f92 --- /dev/null +++ b/tests/php/unit/Service/ContactsServiceTest.php @@ -0,0 +1,96 @@ +service = new ContactsService(); + } + + public function testGetEmail(): void { + $contact = ['EMAIL' => 'test@test.com']; + $this->assertEquals(['test@test.com'], $this->service->getEmail($contact)); + } + + public function testIsSystemBook(): void { + $contact = ['isLocalSystemBook' => true]; + $this->assertTrue($this->service->isSystemBook($contact)); + } + + public function testIsNotSystemBook(): void { + $contact = ['isLocalSystemBook' => false]; + $this->assertFalse($this->service->isSystemBook($contact)); + } + + public function testNotSetSystemBook(): void { + $this->assertFalse($this->service->isSystemBook([])); + } + + public function testHasEmail(): void { + $contact = ['EMAIL' => 'test@test.com']; + $this->assertTrue($this->service->hasEmail($contact)); + } + + public function testHasNoEmail(): void { + $this->assertFalse($this->service->hasEmail([])); + } + + public function testGetPhotoUri(): void { + $contact = ['PHOTO' => 'VALUE=uri:http://test']; + $this->assertEquals('http://test', $this->service->getPhotoUri($contact)); + } + + public function testGetPhotoInvalidUri(): void { + $contact = ['PHOTO' => 'VALUE=uri:thisisnotit']; + $this->assertNull($this->service->getPhotoUri($contact)); + } + + public function testGetPhotoUriNoPhoto(): void { + $this->assertNull($this->service->getPhotoUri([])); + } + + public function testFilterGroupsWithCount(): void { + $contact = [ + ['CATEGORIES' => 'The Proclaimers,I\'m gonna be,When I go out,I would walk 500 Miles,I would walk 500 more'], + ['CATEGORIES' => 'The Proclaimers,When I\'m lonely,I would walk 500 Miles,I would walk 500 more'], + ]; + + $searchterm = 'walk'; + + $expected = [ + 'I would walk 500 Miles' => 2, + 'I would walk 500 more' => 2, + ]; + + $this->assertEqualsCanonicalizing($expected, $this->service->filterGroupsWithCount($contact, $searchterm)); + } + + public function testGetTimezoneId(): void { + $contact = ['TZ' => ['UTC']]; + $this->assertEquals('UTC', $this->service->getTimezoneId($contact)); + } + + public function testGetLanguageId(): void { + $contact = ['LANG' => ['de_de']]; + $this->assertEquals('de_de', $this->service->getLanguageId($contact)); + } + + public function testGetNameFromContact(): void { + $contact = ['FN' => 'test']; + $this->assertEquals('test', $this->service->getNameFromContact($contact)); + } + + public function testGetNameFromContactNoName(): void { + $this->assertEquals('', $this->service->getNameFromContact([])); + } +}