diff options
-rw-r--r-- | lib/Service/Social/CompositeSocialProvider.php | 5 | ||||
-rw-r--r-- | lib/Service/Social/DiasporaProvider.php | 84 | ||||
-rw-r--r-- | lib/Service/Social/FacebookProvider.php | 14 | ||||
-rw-r--r-- | lib/Service/Social/GravatarProvider.php | 6 | ||||
-rw-r--r-- | lib/Service/Social/InstagramProvider.php | 14 | ||||
-rw-r--r-- | lib/Service/Social/MastodonProvider.php | 18 | ||||
-rw-r--r-- | lib/Service/Social/TumblrProvider.php | 14 | ||||
-rw-r--r-- | lib/Service/Social/TwitterProvider.php | 14 | ||||
-rw-r--r-- | lib/Service/Social/XingProvider.php | 16 | ||||
-rw-r--r-- | lib/Service/SocialApiService.php | 12 | ||||
-rw-r--r-- | tests/unit/Service/SocialApiServiceTest.php | 184 |
11 files changed, 238 insertions, 143 deletions
diff --git a/lib/Service/Social/CompositeSocialProvider.php b/lib/Service/Social/CompositeSocialProvider.php index 8e9f3e89..751f5cc6 100644 --- a/lib/Service/Social/CompositeSocialProvider.php +++ b/lib/Service/Social/CompositeSocialProvider.php @@ -65,10 +65,9 @@ class CompositeSocialProvider { /** * generate download url for a social entry * - * @param array contact all social data from the contact * @param String network the choice which network to use * - * @returns ISocialProvider if provider of 'network' is found, otherwise null + * @return ISocialProvider if provider of 'network' is found, otherwise null */ public function getSocialConnector(string $network) : ?ISocialProvider { $connector = null; @@ -82,8 +81,6 @@ class CompositeSocialProvider { /** * generate download url for a social entry * - * @param array contact all social data from the contact - * * @return ISocialProvider[] all social providers */ public function getSocialConnectors() : array { diff --git a/lib/Service/Social/DiasporaProvider.php b/lib/Service/Social/DiasporaProvider.php index 44f46ae1..31269029 100644 --- a/lib/Service/Social/DiasporaProvider.php +++ b/lib/Service/Social/DiasporaProvider.php @@ -33,8 +33,8 @@ class DiasporaProvider implements ISocialProvider { /** @var bool */ private $looping; - /** @var string */ - public $name = "diaspora"; + /** @var string */ + public $name = "diaspora"; public function __construct(IClientService $httpClient) { $this->httpClient = $httpClient->NewClient(); @@ -49,17 +49,17 @@ class DiasporaProvider implements ISocialProvider { * @return bool */ public function supportsContact(array $contact):bool { - $socialprofiles = $contact['X-SOCIALPROFILE']; - $supports = false; - if(isset($socialprofiles)) { - foreach($socialprofiles as $profile) { - if ($profile['type'] == $this->name) { - $supports = true; - break; - } - } - } - return $supports; + $socialprofiles = $contact['X-SOCIALPROFILE']; + $supports = false; + if (isset($socialprofiles)) { + foreach ($socialprofiles as $profile) { + if ($profile['type'] == $this->name) { + $supports = true; + break; + } + } + } + return $supports; } /** @@ -67,23 +67,23 @@ class DiasporaProvider implements ISocialProvider { * * @param {array} contact information * - * @return array + * @return array */ public function getImageUrls(array $contact):array { - $profileIds = $this->getProfileIds($contact); - $urls = array(); + $profileIds = $this->getProfileIds($contact); + $urls = []; - foreach($profileIds as $profileId) { - $url = $this->getImageUrl($profileId); - if (isset($url)) { - $urls[] = $url; - } - } + foreach ($profileIds as $profileId) { + $url = $this->getImageUrl($profileId); + if (isset($url)) { + $urls[] = $url; + } + } - return $urls; + return $urls; } - /** + /** * Returns the profile-picture url * * @param {string} profileId the profile-id @@ -113,29 +113,29 @@ class DiasporaProvider implements ISocialProvider { } } - /** - * Returns all possible profile ids for contact + /** + * Returns all possible profile ids for contact * * @param {array} contact information * - * @return array + * @return array */ - protected function getProfileIds($contact):array { - $socialprofiles = $contact['X-SOCIALPROFILE']; - $profileIds = array(); + protected function getProfileIds($contact):array { + $socialprofiles = $contact['X-SOCIALPROFILE']; + $profileIds = []; - if(isset($socialprofiles)) { - foreach($socialprofiles as $profile) { - if (strtolower($profile['type']) == $this->name) { - $profileId = $this->cleanupId($profile['value']); - if (isset($profileId)) { - $profileIds[] = $profileId; - } - } - } - } - return $profileIds; - } + if (isset($socialprofiles)) { + foreach ($socialprofiles as $profile) { + if (strtolower($profile['type']) == $this->name) { + $profileId = $this->cleanupId($profile['value']); + if (isset($profileId)) { + $profileIds[] = $profileId; + } + } + } + } + return $profileIds; + } /** * Returns the profile-id diff --git a/lib/Service/Social/FacebookProvider.php b/lib/Service/Social/FacebookProvider.php index 2aa27f83..0202e6d0 100644 --- a/lib/Service/Social/FacebookProvider.php +++ b/lib/Service/Social/FacebookProvider.php @@ -47,8 +47,8 @@ class FacebookProvider implements ISocialProvider { public function supportsContact(array $contact):bool { $socialprofiles = $contact['X-SOCIALPROFILE']; $supports = false; - if(isset($socialprofiles)) { - foreach($socialprofiles as $profile) { + if (isset($socialprofiles)) { + foreach ($socialprofiles as $profile) { if (strtolower($profile['type']) == $this->name) { $supports = true; break; @@ -67,8 +67,8 @@ class FacebookProvider implements ISocialProvider { */ public function getImageUrls(array $contact):array { $profileIds = $this->getProfileIds($contact); - $urls = array(); - foreach($profileIds as $profileId) { + $urls = []; + foreach ($profileIds as $profileId) { $recipe = 'https://graph.facebook.com/{socialId}/picture?width=720'; $connector = str_replace("{socialId}", $profileId, $recipe); $urls[] = $connector; @@ -100,9 +100,9 @@ class FacebookProvider implements ISocialProvider { */ protected function getProfileIds($contact):array { $socialprofiles = $contact['X-SOCIALPROFILE']; - $profileIds = array(); - if(isset($socialprofiles)) { - foreach($socialprofiles as $profile) { + $profileIds = []; + if (isset($socialprofiles)) { + foreach ($socialprofiles as $profile) { if (strtolower($profile['type']) == $this->name) { $profileIds[] = $this->cleanupId($profile['value']); } diff --git a/lib/Service/Social/GravatarProvider.php b/lib/Service/Social/GravatarProvider.php index d735dd5b..07b0f83e 100644 --- a/lib/Service/Social/GravatarProvider.php +++ b/lib/Service/Social/GravatarProvider.php @@ -54,8 +54,8 @@ class GravatarProvider implements ISocialProvider { */ public function getImageUrls(array $contact):array { $emails = $this->getProfileIds($contact); - $urls = array(); - foreach($emails as $email) { + $urls = []; + foreach ($emails as $email) { $hash = md5(strtolower(trim($email['value']))); $recipe = 'https://www.gravatar.com/avatar/{hash}?s=720&d=404'; $connector = str_replace("{hash}", $hash, $recipe); @@ -76,6 +76,6 @@ class GravatarProvider implements ISocialProvider { if (isset($emails)) { return $emails; } - return array(); + return []; } } diff --git a/lib/Service/Social/InstagramProvider.php b/lib/Service/Social/InstagramProvider.php index 2cd3bf52..f0c593c7 100644 --- a/lib/Service/Social/InstagramProvider.php +++ b/lib/Service/Social/InstagramProvider.php @@ -47,8 +47,8 @@ class InstagramProvider implements ISocialProvider { public function supportsContact(array $contact):bool { $socialprofiles = $contact['X-SOCIALPROFILE']; $supports = false; - if(isset($socialprofiles)) { - foreach($socialprofiles as $profile) { + if (isset($socialprofiles)) { + foreach ($socialprofiles as $profile) { if (strtolower($profile['type']) == $this->name) { $supports = true; break; @@ -67,8 +67,8 @@ class InstagramProvider implements ISocialProvider { */ public function getImageUrls(array $contact):array { $profileIds = $this->getProfileIds($contact); - $urls = array(); - foreach($profileIds as $profileId) { + $urls = []; + foreach ($profileIds as $profileId) { $recipe = 'https://www.instagram.com/{socialId}/?__a=1'; $connector = str_replace("{socialId}", $profileId, $recipe); $connector = $this->getFromJson($connector, 'graphql->user->profile_pic_url_hd'); @@ -98,9 +98,9 @@ class InstagramProvider implements ISocialProvider { */ protected function getProfileIds($contact):array { $socialprofiles = $contact['X-SOCIALPROFILE']; - $profileIds = array(); - if(isset($socialprofiles)) { - foreach($socialprofiles as $profile) { + $profileIds = []; + if (isset($socialprofiles)) { + foreach ($socialprofiles as $profile) { if (strtolower($profile['type']) == $this->name) { $profileIds[] = $this->cleanupId($profile['value']); } diff --git a/lib/Service/Social/MastodonProvider.php b/lib/Service/Social/MastodonProvider.php index 2c6cbd19..b1bb5097 100644 --- a/lib/Service/Social/MastodonProvider.php +++ b/lib/Service/Social/MastodonProvider.php @@ -30,7 +30,7 @@ class MastodonProvider implements ISocialProvider { /** @var IClientService */ private $httpClient; - /** @var string */ + /** @var string */ public $name = "mastodon"; public function __construct(IClientService $httpClient) { @@ -47,8 +47,8 @@ class MastodonProvider implements ISocialProvider { public function supportsContact(array $contact):bool { $socialprofiles = $contact['X-SOCIALPROFILE']; $supports = false; - if(isset($socialprofiles)) { - foreach($socialprofiles as $profile) { + if (isset($socialprofiles)) { + foreach ($socialprofiles as $profile) { if (strtolower($profile['type']) == $this->name) { $supports = true; break; @@ -67,9 +67,9 @@ class MastodonProvider implements ISocialProvider { */ public function getImageUrls(array $contact):array { $profileIds = $this->getProfileIds($contact); - $urls = array(); + $urls = []; - foreach($profileIds as $profileId) { + foreach ($profileIds as $profileId) { $url = $this->getImageUrl($profileId); if (isset($url)) { $urls[] = $url; @@ -110,12 +110,12 @@ class MastodonProvider implements ISocialProvider { */ protected function getProfileIds($contact):array { $socialprofiles = $contact['X-SOCIALPROFILE']; - $profileIds = array(); - if(isset($socialprofiles)) { - foreach($socialprofiles as $profile) { + $profileIds = []; + if (isset($socialprofiles)) { + foreach ($socialprofiles as $profile) { if (strtolower($profile['type']) == $this->name) { $profileId = $this->cleanupId($profile['value']); - if(isset($profileId)) { + if (isset($profileId)) { $profileIds[] = $profileId; } } diff --git a/lib/Service/Social/TumblrProvider.php b/lib/Service/Social/TumblrProvider.php index e1c1d61e..c0d8ffca 100644 --- a/lib/Service/Social/TumblrProvider.php +++ b/lib/Service/Social/TumblrProvider.php @@ -40,8 +40,8 @@ class TumblrProvider implements ISocialProvider { public function supportsContact(array $contact):bool { $socialprofiles = $contact['X-SOCIALPROFILE']; $supports = false; - if(isset($socialprofiles)) { - foreach($socialprofiles as $profile) { + if (isset($socialprofiles)) { + foreach ($socialprofiles as $profile) { if (strtolower($profile['type']) == $this->name) { $supports = true; break; @@ -60,8 +60,8 @@ class TumblrProvider implements ISocialProvider { */ public function getImageUrls(array $contact):array { $profileIds = $this->getProfileIds($contact); - $urls = array(); - foreach($profileIds as $profileId) { + $urls = []; + foreach ($profileIds as $profileId) { $recipe = 'https://api.tumblr.com/v2/blog/{socialId}/avatar/512'; $connector = str_replace("{socialId}", $profileId, $recipe); $urls[] = $connector; @@ -94,9 +94,9 @@ class TumblrProvider implements ISocialProvider { */ protected function getProfileIds($contact):array { $socialprofiles = $contact['X-SOCIALPROFILE']; - $profileIds = array(); - if(isset($socialprofiles)) { - foreach($socialprofiles as $profile) { + $profileIds = []; + if (isset($socialprofiles)) { + foreach ($socialprofiles as $profile) { if (strtolower($profile['type']) == $this->name) { $profileIds[] = $this->cleanupId($profile['value']); } diff --git a/lib/Service/Social/TwitterProvider.php b/lib/Service/Social/TwitterProvider.php index ed249a0b..08aab855 100644 --- a/lib/Service/Social/TwitterProvider.php +++ b/lib/Service/Social/TwitterProvider.php @@ -45,8 +45,8 @@ class TwitterProvider implements ISocialProvider { */ public function supportsContact(array $contact):bool { $socialprofiles = $contact['X-SOCIALPROFILE']; - if(isset($socialprofiles)) { - foreach($socialprofiles as $profile) { + if (isset($socialprofiles)) { + foreach ($socialprofiles as $profile) { if (strtolower($profile['type']) == $this->name) { return true; } @@ -64,8 +64,8 @@ class TwitterProvider implements ISocialProvider { */ public function getImageUrls(array $contact):array { $profileIds = $this->getProfileIds($contact); - $urls = array(); - foreach($profileIds as $profileId) { + $urls = []; + foreach ($profileIds as $profileId) { $recipe = 'https://mobile.twitter.com/{socialId}'; $connector = str_replace("{socialId}", $profileId, $recipe); $connector = $this->getFromHtml($connector, '_normal'); @@ -98,9 +98,9 @@ class TwitterProvider implements ISocialProvider { */ protected function getProfileIds($contact):array { $socialprofiles = $contact['X-SOCIALPROFILE']; - $profileIds = array(); - if(isset($socialprofiles)) { - foreach($socialprofiles as $profile) { + $profileIds = []; + if (isset($socialprofiles)) { + foreach ($socialprofiles as $profile) { if (strtolower($profile['type']) == $this->name) { $profileIds[] = $this->cleanupId($profile['value']); } diff --git a/lib/Service/Social/XingProvider.php b/lib/Service/Social/XingProvider.php index ce695607..c0a816d9 100644 --- a/lib/Service/Social/XingProvider.php +++ b/lib/Service/Social/XingProvider.php @@ -48,8 +48,8 @@ class XingProvider implements ISocialProvider { public function supportsContact(array $contact):bool { $socialprofiles = $contact['X-SOCIALPROFILE']; $supports = false; - if(isset($socialprofiles)) { - foreach($socialprofiles as $profile) { + if (isset($socialprofiles)) { + foreach ($socialprofiles as $profile) { if (strtolower($profile['type']) == $this->name) { $supports = true; break; @@ -68,9 +68,9 @@ class XingProvider implements ISocialProvider { */ public function getImageUrls(array $contact):array { $profileIds = $this->getProfileIds($contact); - $urls = array(); + $urls = []; - foreach($profileIds as $profileId) { + foreach ($profileIds as $profileId) { $url = $this->getImageUrl($profileId); if (isset($url)) { $urls[] = $url; @@ -131,12 +131,12 @@ class XingProvider implements ISocialProvider { */ protected function getProfileIds($contact):array { $socialprofiles = $contact['X-SOCIALPROFILE']; - $profileIds = array(); - if(isset($socialprofiles)) { - foreach($socialprofiles as $profile) { + $profileIds = []; + if (isset($socialprofiles)) { + foreach ($socialprofiles as $profile) { if (strtolower($profile['type']) == $this->name) { $profileId = $this->cleanupId($profile['value']); - if(isset($profileId)) { + if (isset($profileId)) { $profileIds[] = $profileId; } } diff --git a/lib/Service/SocialApiService.php b/lib/Service/SocialApiService.php index af07c998..446a9ef6 100644 --- a/lib/Service/SocialApiService.php +++ b/lib/Service/SocialApiService.php @@ -167,10 +167,10 @@ class SocialApiService { * * @returns {JSONResponse} an empty JSONResponse with respective http status code */ - public function updateContact(string $addressbookId, string $contactId, string $network) : JSONResponse { + public function updateContact(string $addressbookId, string $contactId, ?string $network) : JSONResponse { $socialdata = null; $imageType = null; - $urls = array(); + $urls = []; $allConnectors = $this->socialProvider->getSocialConnectors(); try { @@ -191,7 +191,7 @@ class SocialApiService { $allConnectors = [$this->socialProvider->getSocialConnector($network)]; } - $connectors = array_filter($allConnectors, function($connector) use($contact) { + $connectors = array_filter($allConnectors, function ($connector) use ($contact) { return $connector->supportsContact($contact); }); @@ -199,7 +199,7 @@ class SocialApiService { return new JSONResponse([], Http::STATUS_PRECONDITION_FAILED); } - foreach($connectors as $connector) { + foreach ($connectors as $connector) { $urls = array_merge($connector->getImageUrls($contact), $urls); } @@ -207,7 +207,7 @@ class SocialApiService { return new JSONResponse([], Http::STATUS_BAD_REQUEST); } - foreach($urls as $url) { + foreach ($urls as $url) { try { $httpResult = $this->clientService->NewClient()->get($url); $socialdata = $httpResult->getBody(); @@ -215,7 +215,7 @@ class SocialApiService { if (isset($socialdata) && isset($imageType)) { break; } - } catch(\Exception $e) { + } catch (\Exception $e) { } } diff --git a/tests/unit/Service/SocialApiServiceTest.php b/tests/unit/Service/SocialApiServiceTest.php index 327bf161..e7f4c8e3 100644 --- a/tests/unit/Service/SocialApiServiceTest.php +++ b/tests/unit/Service/SocialApiServiceTest.php @@ -25,6 +25,7 @@ namespace OCA\Contacts\Service; use OCA\Contacts\Service\Social\CompositeSocialProvider; +use OCA\Contacts\Service\Social\ISocialProvider; use OCP\AppFramework\Http; use OCP\Http\Client\IClient; @@ -45,7 +46,7 @@ use ChristophWurst\Nextcloud\Testing\TestCase; class SocialApiServiceTest extends TestCase { private $service; - /** @var CompositeSocialProvider */ + /** @var CompositeSocialProvider|MockObject */ private $socialProvider; /** @var IManager|MockObject */ private $manager; @@ -53,22 +54,56 @@ class SocialApiServiceTest extends TestCase { private $config; /** @var IClientService|MockObject */ private $clientService; - /** @var IL10N|MockObject */ + /** @var IL10N|MockObject */ private $l10n; - /** @var IURLGenerator|MockObject */ + /** @var IURLGenerator|MockObject */ private $urlGen; /** @var CardDavBackend|MockObject */ private $davBackend; /** @var ITimeFactory|MockObject */ private $timeFactory; - public function socialProfileProvider() { + public function allSocialProfileProviders() { + $body = "the body"; + $imageType = "jpg"; + $contact = [ + 'URI' => '3225c0d5-1bd2-43e5-a08c-4e65eaa406b0', + 'VERSION' => '4.0' + ]; + $connector = $this->createMock(ISocialProvider::class); + $connector->method('supportsContact')->willReturn(true); + $connector->method('getImageUrls')->willReturn(["url1"]); + + $connectorNoSupport = $this->createMock(ISocialProvider::class); + $connectorNoSupport->method('supportsContact')->willReturn(false); + + $connectorNoUrl = $this->createMock(ISocialProvider::class); + $connectorNoUrl->method('supportsContact')->willReturn(true); + $connectorNoUrl->method('getImageUrls')->willReturn([]); + + $addressbookEmpty = $this->createMock(IAddressBook::class); + $addressbookEmpty + ->method('getUri') + ->willReturn('contacts'); + $addressbookEmpty + ->method('search') + ->willReturn(null); + + $addressbook = $this->createMock(IAddressBook::class); + $addressbook + ->method('getUri') + ->willReturn('contacts'); + $addressbook + ->method('search') + ->willReturn([$contact]); + return [ - 'no social profiles set' => [null, 'someConnector', 'someResult', Http::STATUS_PRECONDITION_FAILED], - 'valid social profile' => [[['type' => 'someNetwork', 'value' => 'someId']], 'someConnector', 'someResult', Http::STATUS_OK], - 'bad formatted profile id' => [[['type' => 'someNetwork', 'value' => 'someId']], null, 'someResult', Http::STATUS_BAD_REQUEST], - 'not existing profile id' => [[['type' => 'someNetwork', 'value' => 'someId']], 'someConnector', '', Http::STATUS_NOT_FOUND], - 'unchanged data' => [[['type' => 'someNetwork', 'value' => 'someId']], 'someConnector', 'thePhoto', Http::STATUS_NOT_MODIFIED], + 'no address book found' => [null, [], "", "", Http::STATUS_BAD_REQUEST], + 'no contact found' => [[$addressbookEmpty], [], "", "", Http::STATUS_PRECONDITION_FAILED], + 'no supporting contacts found' => [[$addressbook], [$connectorNoSupport], "", "", Http::STATUS_PRECONDITION_FAILED], + 'no url found' => [[$addressbook], [$connectorNoUrl], "", "", Http::STATUS_BAD_REQUEST], + 'no image found' => [[$addressbook], [$connector], "", "", Http::STATUS_NOT_FOUND], + 'image found' => [[$addressbook], [$connector], $body, $imageType, Http::STATUS_OK] ]; } @@ -114,15 +149,54 @@ class SocialApiServiceTest extends TestCase { } /** - * @dataProvider socialProfileProvider + * @dataProvider allSocialProfileProviders */ - public function testUpdateContact($social, $connector, $httpResult, $expected) { + public function testUpdateContactWithoutNetwork($addressbooks, $providers, $body, $imageType, $status) { + $this->manager + ->method('getUserAddressBooks') + ->willReturn($addressbooks); + + $this->socialProvider + ->method('getSocialConnectors') + ->willReturn($providers); + + $response = $this->createMock(IResponse::class); + $response + ->method('getBody') + ->willReturn($body); + $response + ->method('getHeader') + ->willReturn($imageType); + $client = $this->createMock(IClient::class); + $client + ->method('get') + ->willReturn($response); + $this->clientService + ->method('NewClient') + ->willReturn($client); + + $result = $this->service + ->updateContact( + 'contacts', + '3225c0d5-1bd2-43e5-a08c-4e65eaa406b0', + null); + $this->assertEquals($status, $result->getStatus()); + } + + public function testUpdateContactWithNetwork() { + $network = "mastodon"; + $body = "the body"; + $imageType = "jpg"; + $addressBookId = "contacts"; + $contactId = "3225c0d5-1bd2-43e5-a08c-4e65eaa406b0"; $contact = [ - 'URI' => '3225c0d5-1bd2-43e5-a08c-4e65eaa406b0', - 'VERSION' => '4.0', - 'PHOTO' => "data:" . $httpResult . ";base64," . base64_encode('thePhoto'), - 'X-SOCIALPROFILE' => $social, + 'URI' => $contactId, + 'VERSION' => '4.0' ]; + $provider = $this->createMock(ISocialProvider::class); + $provider->method('supportsContact')->willReturn(true); + $provider->method('getImageUrls')->willReturn(["url1"]); + $addressbook = $this->createMock(IAddressBook::class); $addressbook ->method('getUri') @@ -136,16 +210,20 @@ class SocialApiServiceTest extends TestCase { ->willReturn([$addressbook]); $this->socialProvider + ->method('getSocialConnectors') + ->willReturn([$provider]); + + $this->socialProvider ->method('getSocialConnector') - ->willReturn($connector); + ->willReturn($provider); $response = $this->createMock(IResponse::class); $response ->method('getBody') - ->willReturn($httpResult); + ->willReturn($body); $response ->method('getHeader') - ->willReturn($httpResult); + ->willReturn($imageType); $client = $this->createMock(IClient::class); $client ->method('get') @@ -154,9 +232,24 @@ class SocialApiServiceTest extends TestCase { ->method('NewClient') ->willReturn($client); - $result = $this->service->updateContact('contacts', '3225c0d5-1bd2-43e5-a08c-4e65eaa406b0', 'theSocialNetwork'); + $changes = [ + 'URI' => $contact['URI'], + 'VERSION' => $contact['VERSION'], + 'PHOTO' => "data:".$imageType.";base64," . base64_encode($body) + ]; + + $this->socialProvider + ->expects($this->once())->method("getSocialConnector")->with($network); + $provider->expects($this->once())->method("supportsContact")->with($contact); + $addressbook->expects($this->once())->method("createOrUpdate")->with($changes, $addressBookId); - $this->assertEquals($expected, $result->getStatus()); + $result = $this->service + ->updateContact( + $addressBookId, + $contactId, + $network); + + $this->assertEquals(Http::STATUS_OK, $result->getStatus()); } protected function setupAddressbooks() { @@ -219,15 +312,33 @@ class SocialApiServiceTest extends TestCase { ->method('getUserAddressBooks') ->willReturn([$addressbook1, $addressbook2]); - $socialConnectorMap = [ - [$validContact1['X-SOCIALPROFILE'], 'any', 'validConnector'], - [$validContact2['X-SOCIALPROFILE'], 'any', 'validConnector'], - [$invalidContact['X-SOCIALPROFILE'], 'any', 'invalidConnector'], - [$emptyContact['X-SOCIALPROFILE'], 'any', 'emptyConnector'], + $providerSupportsMap = [ + [$validContact1, true], + [$emptyContact, false], + [$invalidContact, false], + [$validContact2, true] ]; + + $providerUrlMap = [ + [$validContact1, ["url1"]], + [$emptyContact, []], + [$invalidContact, []], + [$validContact2, ["url1"]] + ]; + + $provider = $this->createMock(ISocialProvider::class); + $provider->method('getImageUrls') + ->will($this->returnValueMap($providerUrlMap)); + $provider->method('supportsContact') + ->will($this->returnValueMap($providerSupportsMap)); + + $this->socialProvider + ->method('getSocialConnectors') + ->willReturn([$provider]); + $this->socialProvider ->method('getSocialConnector') - ->will($this->returnValueMap($socialConnectorMap)); + ->willReturn($provider); $validResponse = $this->createMock(IResponse::class); $validResponse @@ -236,29 +347,16 @@ class SocialApiServiceTest extends TestCase { $validResponse ->method('getHeader') ->willReturn('someHeader'); - $invalidResponse = $this->createMock(IResponse::class); - $invalidResponse - ->method('getBody') - ->willReturn(''); - $invalidResponse - ->method('getHeader') - ->willReturn(''); - $clientResponseMap = [ - ['validConnector', [], $validResponse], - ['invalidConnector', [], $invalidResponse], - ['emptyConnector', [], $invalidResponse], - ]; $client = $this->createMock(IClient::class); $client ->method('get') - ->will($this->returnValueMap($clientResponseMap)); + ->willReturn($validResponse); $this->clientService ->method('NewClient') ->willReturn($client); } - /** * @dataProvider updateAddressbookProvider */ @@ -287,9 +385,9 @@ class SocialApiServiceTest extends TestCase { $this->assertArrayHasKey('checked', $report[0]); $this->assertContains('Valid Contact Two', $report[0]['checked']); $this->assertArrayHasKey('failed', $report[0]); - $this->assertArrayHasKey('404', $report[0]['failed']); - $this->assertContains('Invalid Contact', $report[0]['failed']['404']); - $this->assertNotContains('Empty Contact', $report[0]['failed']['404']); + $this->assertArrayHasKey('412', $report[0]['failed']); + $this->assertContains('Invalid Contact', $report[0]['failed']['412']); + $this->assertContains('Empty Contact', $report[0]['failed']['412']); } } |