summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorleith abdulla <online-nextcloud@eleith.com>2020-11-04 11:04:41 -0800
committerleith abdulla <online-nextcloud@eleith.com>2020-11-04 11:10:50 -0800
commitca1c40a8ae2b388f5e923e70c9ac2c3af176e71d (patch)
tree14706b573dbcb632d3333d2f19bfad7417a9ad1b
parentda321c14516862c1216f57060d1d609704f61c9d (diff)
unit tests for all providers
this commit adds unit tests for all providers while also reducing some redundancy in looking up social fields minor feedback was addressed as well as some minor bugs fixed Signed-off-by: leith abdulla <online-nextcloud@eleith.com>
-rw-r--r--lib/Service/Social/DiasporaProvider.php15
-rw-r--r--lib/Service/Social/FacebookProvider.php35
-rw-r--r--lib/Service/Social/GravatarProvider.php32
-rw-r--r--lib/Service/Social/InstagramProvider.php41
-rw-r--r--lib/Service/Social/MastodonProvider.php13
-rw-r--r--lib/Service/Social/TumblrProvider.php13
-rw-r--r--lib/Service/Social/TwitterProvider.php11
-rw-r--r--lib/Service/Social/XingProvider.php14
-rw-r--r--tests/unit/Service/Social/DiasporaProviderTest.php182
-rw-r--r--tests/unit/Service/Social/FacebookProviderTest.php156
-rw-r--r--tests/unit/Service/Social/GravatarProviderTest.php92
-rw-r--r--tests/unit/Service/Social/InstagramProviderTest.php158
-rw-r--r--tests/unit/Service/Social/MastodonProviderTest.php154
-rw-r--r--tests/unit/Service/Social/TumblrProviderTest.php100
-rw-r--r--tests/unit/Service/Social/TwitterProviderTest.php154
-rw-r--r--tests/unit/Service/Social/XingProviderTest.php156
16 files changed, 1216 insertions, 110 deletions
diff --git a/lib/Service/Social/DiasporaProvider.php b/lib/Service/Social/DiasporaProvider.php
index 31269029..c0af278f 100644
--- a/lib/Service/Social/DiasporaProvider.php
+++ b/lib/Service/Social/DiasporaProvider.php
@@ -49,17 +49,8 @@ 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 = $this->getProfileIds($contact);
+ return isset($socialprofiles) && count($socialprofiles) > 0;
}
/**
@@ -150,7 +141,7 @@ class DiasporaProvider implements ISocialProvider {
$user_server = explode('@', $candidate);
$candidate = 'https://' . array_pop($user_server) . '/public/' . array_pop($user_server) . '.atom';
}
- } catch (Exception $e) {
+ } catch (\Exception $e) {
$candidate = null;
}
return $candidate;
diff --git a/lib/Service/Social/FacebookProvider.php b/lib/Service/Social/FacebookProvider.php
index 0202e6d0..e5d34397 100644
--- a/lib/Service/Social/FacebookProvider.php
+++ b/lib/Service/Social/FacebookProvider.php
@@ -45,17 +45,8 @@ class FacebookProvider implements ISocialProvider {
* @return bool
*/
public function supportsContact(array $contact):bool {
- $socialprofiles = $contact['X-SOCIALPROFILE'];
- $supports = false;
- if (isset($socialprofiles)) {
- foreach ($socialprofiles as $profile) {
- if (strtolower($profile['type']) == $this->name) {
- $supports = true;
- break;
- }
- }
- }
- return $supports;
+ $socialprofiles = $this->getProfiles($contact);
+ return isset($socialprofiles) && count($socialprofiles) > 0;
}
/**
@@ -98,16 +89,32 @@ class FacebookProvider implements ISocialProvider {
*
* @return array of string profile ids
*/
- protected function getProfileIds($contact):array {
+ protected function getProfiles(array $contact):array {
$socialprofiles = $contact['X-SOCIALPROFILE'];
- $profileIds = [];
+ $profiles = [];
if (isset($socialprofiles)) {
foreach ($socialprofiles as $profile) {
if (strtolower($profile['type']) == $this->name) {
- $profileIds[] = $this->cleanupId($profile['value']);
+ $profiles[] = $profile['value'];
}
}
}
+ return $profiles;
+ }
+
+ /**
+ * Returns all possible profile ids for contact
+ *
+ * @param {array} contact information
+ *
+ * @return array of string profile ids
+ */
+ protected function getProfileIds(array $contact):array {
+ $profiles = $this->getProfiles($contact);
+ $profileIds = [];
+ foreach ($profiles as $profile) {
+ $profileIds[] = $this->cleanupId($profile);
+ }
return $profileIds;
}
diff --git a/lib/Service/Social/GravatarProvider.php b/lib/Service/Social/GravatarProvider.php
index 07b0f83e..4b9d9a84 100644
--- a/lib/Service/Social/GravatarProvider.php
+++ b/lib/Service/Social/GravatarProvider.php
@@ -23,14 +23,11 @@
namespace OCA\Contacts\Service\Social;
-use OCP\Http\Client\IClientService;
-
class GravatarProvider implements ISocialProvider {
/** @var string */
public $name = "gravatar";
- public function __construct(IClientService $httpClient) {
- $this->httpClient = $httpClient->NewClient();
+ public function __construct() {
}
/**
@@ -53,29 +50,16 @@ class GravatarProvider implements ISocialProvider {
* @return array
*/
public function getImageUrls(array $contact):array {
- $emails = $this->getProfileIds($contact);
$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);
- $urls[] = $connector;
- }
- return $urls;
- }
-
- /**
- * Returns all possible profile ids for contact
- *
- * @param {array} contact information
- *
- * @return array of string profile ids
- */
- protected function getProfileIds(array $contact):array {
$emails = $contact['EMAIL'];
if (isset($emails)) {
- return $emails;
+ 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);
+ $urls[] = $connector;
+ }
}
- return [];
+ return $urls;
}
}
diff --git a/lib/Service/Social/InstagramProvider.php b/lib/Service/Social/InstagramProvider.php
index f0c593c7..a43d12c3 100644
--- a/lib/Service/Social/InstagramProvider.php
+++ b/lib/Service/Social/InstagramProvider.php
@@ -45,17 +45,8 @@ class InstagramProvider implements ISocialProvider {
* @return bool
*/
public function supportsContact(array $contact):bool {
- $socialprofiles = $contact['X-SOCIALPROFILE'];
- $supports = false;
- if (isset($socialprofiles)) {
- foreach ($socialprofiles as $profile) {
- if (strtolower($profile['type']) == $this->name) {
- $supports = true;
- break;
- }
- }
- }
- return $supports;
+ $socialprofiles = $this->getProfiles($contact);
+ return isset($socialprofiles) && count($socialprofiles) > 0;
}
/**
@@ -88,24 +79,40 @@ class InstagramProvider implements ISocialProvider {
$candidate = preg_replace('/^' . preg_quote('x-apple:', '/') . '/', '', $candidate);
return basename($candidate);
}
-
+
/**
- * Returns all possible profile ids for contact
+ * Returns all possible profile urls for contact
*
* @param {array} contact information
*
- * @return array of string profile ids
+ * @return array of string profile urls
*/
- protected function getProfileIds($contact):array {
+ protected function getProfiles($contact):array {
$socialprofiles = $contact['X-SOCIALPROFILE'];
- $profileIds = [];
+ $profiles = [];
if (isset($socialprofiles)) {
foreach ($socialprofiles as $profile) {
if (strtolower($profile['type']) == $this->name) {
- $profileIds[] = $this->cleanupId($profile['value']);
+ $profiles[] = $profile['value'];
}
}
}
+ return $profiles;
+ }
+
+ /**
+ * Returns all possible profile ids for contact
+ *
+ * @param {array} contact information
+ *
+ * @return array of string profile ids
+ */
+ protected function getProfileIds($contact):array {
+ $socialprofiles = $this->getProfiles($contact);
+ $profileIds = [];
+ foreach ($socialprofiles as $profile) {
+ $profileIds[] = $this->cleanupId($profile);
+ }
return $profileIds;
}
diff --git a/lib/Service/Social/MastodonProvider.php b/lib/Service/Social/MastodonProvider.php
index b1bb5097..a212433c 100644
--- a/lib/Service/Social/MastodonProvider.php
+++ b/lib/Service/Social/MastodonProvider.php
@@ -45,17 +45,8 @@ class MastodonProvider implements ISocialProvider {
* @return bool
*/
public function supportsContact(array $contact):bool {
- $socialprofiles = $contact['X-SOCIALPROFILE'];
- $supports = false;
- if (isset($socialprofiles)) {
- foreach ($socialprofiles as $profile) {
- if (strtolower($profile['type']) == $this->name) {
- $supports = true;
- break;
- }
- }
- }
- return $supports;
+ $profiles = $this->getProfileIds($contact);
+ return isset($profiles) && count($profiles) > 0;
}
/**
diff --git a/lib/Service/Social/TumblrProvider.php b/lib/Service/Social/TumblrProvider.php
index c0d8ffca..cd3d577f 100644
--- a/lib/Service/Social/TumblrProvider.php
+++ b/lib/Service/Social/TumblrProvider.php
@@ -38,17 +38,8 @@ class TumblrProvider implements ISocialProvider {
* @return bool
*/
public function supportsContact(array $contact):bool {
- $socialprofiles = $contact['X-SOCIALPROFILE'];
- $supports = false;
- if (isset($socialprofiles)) {
- foreach ($socialprofiles as $profile) {
- if (strtolower($profile['type']) == $this->name) {
- $supports = true;
- break;
- }
- }
- }
- return $supports;
+ $socialprofiles = $this->getProfileIds($contact);
+ return isset($socialprofiles) && count($socialprofiles) > 0;
}
/**
diff --git a/lib/Service/Social/TwitterProvider.php b/lib/Service/Social/TwitterProvider.php
index 08aab855..18cd0d46 100644
--- a/lib/Service/Social/TwitterProvider.php
+++ b/lib/Service/Social/TwitterProvider.php
@@ -44,15 +44,8 @@ class TwitterProvider implements ISocialProvider {
* @return bool
*/
public function supportsContact(array $contact):bool {
- $socialprofiles = $contact['X-SOCIALPROFILE'];
- if (isset($socialprofiles)) {
- foreach ($socialprofiles as $profile) {
- if (strtolower($profile['type']) == $this->name) {
- return true;
- }
- }
- }
- return false;
+ $socialprofiles = $this->getProfileIds($contact);
+ return isset($socialprofiles) && count($socialprofiles) > 0;
}
/**
diff --git a/lib/Service/Social/XingProvider.php b/lib/Service/Social/XingProvider.php
index c0a816d9..a752d3c6 100644
--- a/lib/Service/Social/XingProvider.php
+++ b/lib/Service/Social/XingProvider.php
@@ -35,7 +35,6 @@ class XingProvider implements ISocialProvider {
public function __construct(IClientService $httpClient) {
$this->httpClient = $httpClient->NewClient();
- $this->looping = false;
}
/**
@@ -46,17 +45,8 @@ class XingProvider implements ISocialProvider {
* @return bool
*/
public function supportsContact(array $contact):bool {
- $socialprofiles = $contact['X-SOCIALPROFILE'];
- $supports = false;
- if (isset($socialprofiles)) {
- foreach ($socialprofiles as $profile) {
- if (strtolower($profile['type']) == $this->name) {
- $supports = true;
- break;
- }
- }
- }
- return $supports;
+ $socialprofiles = $this->getProfileIds($contact);
+ return isset($socialprofiles) && count($socialprofiles) > 0;
}
/**
diff --git a/tests/unit/Service/Social/DiasporaProviderTest.php b/tests/unit/Service/Social/DiasporaProviderTest.php
new file mode 100644
index 00000000..e4b066c1
--- /dev/null
+++ b/tests/unit/Service/Social/DiasporaProviderTest.php
@@ -0,0 +1,182 @@
+<?php
+/**
+ * @copyright Copyright (c) 2020 Matthias Heinisch <nextcloud@matthiasheinisch.de>
+ *
+ * @author Matthias Heinisch <nextcloud@matthiasheinisch.de>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+
+namespace OCA\Contacts\Service\Social;
+
+use OCP\Http\Client\IClient;
+use OCP\Http\Client\IResponse;
+use OCP\Http\Client\IClientService;
+use ChristophWurst\Nextcloud\Testing\TestCase;
+use PHPUnit\Framework\MockObject\MockObject;
+
+class DiasporaProviderTest extends TestCase {
+ private $provider;
+
+ /** @var IClientService|MockObject */
+ private $clientService;
+
+ /** @var IClient|MockObject */
+ private $client;
+
+ /** @var IResponse|MockObject */
+ private $response;
+
+ protected function setUp(): void {
+ parent::setUp();
+ $this->clientService = $this->createMock(IClientService::class);
+ $this->response = $this->createMock(IResponse::class);
+ $this->client = $this->createMock(IClient::class);
+
+ $this->clientService
+ ->method('NewClient')
+ ->willReturn($this->client);
+
+ $this->provider = new DiasporaProvider(
+ $this->clientService
+ );
+ }
+
+ public function dataProviderSupportsContact() {
+ $contactWithSocial = [
+ 'X-SOCIALPROFILE' => [
+ ["value" => "one", "type" => "diaspora"],
+ ["value" => "two", "type" => "diaspora"]
+ ]
+ ];
+
+ $contactWithoutSocial = [
+ 'X-SOCIALPROFILE' => [
+ ["value" => "one", "type" => "social2"],
+ ["value" => "two", "type" => "social1"]
+ ]
+ ];
+
+ return [
+ 'contact with diaspora fields' => [$contactWithSocial, true],
+ 'contact without diaspora fields' => [$contactWithoutSocial, false]
+ ];
+ }
+
+ /**
+ * @dataProvider dataProviderSupportsContact
+ */
+ public function testSupportsContact($contact, $expected) {
+ $result = $this->provider->supportsContact($contact);
+ $this->assertEquals($expected, $result);
+ }
+
+ public function dataProviderGetImageUrls() {
+ $contactWithSocial = [
+ 'X-SOCIALPROFILE' => [
+ ["value" => "one@two", "type" => "diaspora"],
+ ["value" => "two@three", "type" => "diaspora"]
+ ]
+ ];
+ $contactWithSocialUrls = [
+ "https://two/public/one.atom",
+ "https://three/public/two.atom"
+ ];
+ $contactWithSocialHtml = array_map(function ($url) {
+ return "<logo>".$url."-small-avatar.jpg</logo>";
+ }, $contactWithSocialUrls);
+ $contactWithSocialImg = array_map(function ($url) {
+ return $url."-large-avatar.jpg";
+ }, $contactWithSocialUrls);
+
+ $contactWithoutSocial = [
+ 'X-SOCIALPROFILE' => [
+ ["value" => "one", "type" => "social2"],
+ ["value" => "two", "type" => "social1"]
+ ]
+ ];
+ $contactWithoutSocialUrls = [];
+ $contactWithoutSocialHtml = [];
+ $contactWithoutSocialImg = [];
+
+ return [
+ 'contact with diaspora fields' => [
+ $contactWithSocial,
+ $contactWithSocialUrls,
+ $contactWithSocialHtml,
+ $contactWithSocialImg
+ ],
+ 'contact without diaspora fields' => [
+ $contactWithoutSocial,
+ $contactWithoutSocialUrls,
+ $contactWithoutSocialHtml,
+ $contactWithoutSocialImg
+ ]
+ ];
+ }
+
+ /**
+ * @dataProvider dataProviderGetImageUrls
+ */
+ public function testGetImageUrls($contact, $urls, $htmls, $imgs) {
+ if (count($urls)) {
+ $this->response
+ ->method('getBody')
+ ->willReturnOnConsecutiveCalls(...$htmls);
+
+ $urlArgs = array_map(function ($url) {
+ return [$url];
+ }, $urls);
+
+ $this->client
+ ->expects($this->exactly(count($urls)))
+ ->method('get')
+ ->withConsecutive(...$urlArgs)
+ ->willReturn($this->response);
+ }
+
+ $result = $this->provider->getImageUrls($contact);
+ $this->assertEquals($imgs, $result);
+ }
+
+ public function testGetImageUrlLoop() {
+ $contact = [
+ 'X-SOCIALPROFILE' => [
+ ["value" => "one@two", "type" => "diaspora"],
+ ]
+ ];
+ $url1 = "https://two/public/one.atom";
+ $url2 = "https://four/public/three.atom";
+ $html1 = '<link rel="alternate" href="'.$url2.'" />';
+ $html2 = "<logo>".$url2."-small-avatar.jpg</logo>";
+ $img = $url2."-large-avatar.jpg";
+
+ $this->response
+ ->method('getBody')
+ ->willReturnOnConsecutiveCalls($html1, $html2);
+
+ $this->client
+ ->expects($this->exactly(2))
+ ->method('get')
+ ->withConsecutive([$url1], [$url2])
+ ->willReturn($this->response);
+
+ $result = $this->provider->getImageUrls($contact);
+ $this->assertEquals([$img], $result);
+ }
+}
diff --git a/tests/unit/Service/Social/FacebookProviderTest.php b/tests/unit/Service/Social/FacebookProviderTest.php
new file mode 100644
index 00000000..b2e3d8e0
--- /dev/null
+++ b/tests/unit/Service/Social/FacebookProviderTest.php
@@ -0,0 +1,156 @@
+<?php
+/**
+ * @copyright Copyright (c) 2020 Matthias Heinisch <nextcloud@matthiasheinisch.de>
+ *
+ * @author Matthias Heinisch <nextcloud@matthiasheinisch.de>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+
+namespace OCA\Contacts\Service\Social;
+
+use OCP\Http\Client\IClient;
+use OCP\Http\Client\IResponse;
+use OCP\Http\Client\IClientService;
+use ChristophWurst\Nextcloud\Testing\TestCase;
+use PHPUnit\Framework\MockObject\MockObject;
+
+class FacebookProviderTest extends TestCase {
+ private $provider;
+
+ /** @var IClientService|MockObject */
+ private $clientService;
+
+ /** @var IClient|MockObject */
+ private $client;
+
+ /** @var IResponse|MockObject */
+ private $response;
+
+ protected function setUp(): void {
+ parent::setUp();
+ $this->clientService = $this->createMock(IClientService::class);
+ $this->response = $this->createMock(IResponse::class);
+ $this->client = $this->createMock(IClient::class);
+
+ $this->clientService
+ ->method('NewClient')
+ ->willReturn($this->client);
+
+ $this->provider = new FacebookProvider(
+ $this->clientService
+ );
+ }
+
+ public function dataProviderSupportsContact() {
+ $contactWithSocial = [
+ 'X-SOCIALPROFILE' => [
+ ["value" => "123124123", "type" => "facebook"],
+ ["value" => "23426523423", "type" => "facebook"]
+ ]
+ ];
+
+ $contactWithoutSocial = [
+ 'X-SOCIALPROFILE' => [
+ ["value" => "one", "type" => "social2"],
+ ["value" => "two", "type" => "social1"]
+ ]
+ ];
+
+ return [
+ 'contact with facebook fields' => [$contactWithSocial, true],
+ 'contact without facebook fields' => [$contactWithoutSocial, false]
+ ];
+ }
+
+ /**
+ * @dataProvider dataProviderSupportsContact
+ */
+ public function testSupportsContact($contact, $expected) {
+ $result = $this->provider->supportsContact($contact);
+ $this->assertEquals($expected, $result);
+ }
+
+ public function dataProviderGetImageUrls() {
+ $contactWithSocial = [
+ 'X-SOCIALPROFILE' => [
+ ["value" => "123456", "type" => "facebook"],
+ ["value" => "7891011", "type" => "facebook"]
+ ]
+ ];
+ $contactWithSocialUrls = [
+ "https://graph.facebook.com/123456/picture?width=720",
+ "https://graph.facebook.com/7891011/picture?width=720",
+ ];
+
+ $contactWithoutSocial = [
+ 'X-SOCIALPROFILE' => [
+ ["value" => "one", "type" => "social2"],
+ ["value" => "two", "type" => "social1"]
+ ]
+ ];
+ $contactWithoutSocialUrls = [];
+
+ return [
+ 'contact with facebook fields' => [
+ $contactWithSocial,
+ $contactWithSocialUrls
+ ],
+ 'contact without facebook fields' => [
+ $contactWithoutSocial,
+ $contactWithoutSocialUrls
+ ]
+ ];
+ }
+
+ /**
+ * @dataProvider dataProviderGetImageUrls
+ */
+ public function testGetImageUrls($contact, $urls) {
+ $result = $this->provider->getImageUrls($contact);
+ $this->assertEquals($urls, $result);
+ }
+
+ public function testGetImageUrlLookup() {
+ $contact = [
+ 'X-SOCIALPROFILE' => [
+ ["value" => "username1", "type" => "facebook"],
+ ]
+ ];
+ $url1 = "https://facebook.com/username1";
+ $url2 = "https://graph.facebook.com/1234567/picture?width=720";
+ $html1 = '"entity_id":"1234567"';
+
+ $this->response
+ ->method('getBody')
+ ->willReturn($html1);
+
+ $this->response
+ ->method('getStatusCode')
+ ->willReturn(200);
+
+ $this->client
+ ->expects($this->once())
+ ->method('get')
+ ->with($url1)
+ ->willReturn($this->response);
+
+ $result = $this->provider->getImageUrls($contact);
+ $this->assertEquals([$url2], $result);
+ }
+}
diff --git a/tests/unit/Service/Social/GravatarProviderTest.php b/tests/unit/Service/Social/GravatarProviderTest.php
new file mode 100644
index 00000000..8de4f565
--- /dev/null
+++ b/tests/unit/Service/Social/GravatarProviderTest.php
@@ -0,0 +1,92 @@
+<?php
+/**
+ * @copyright Copyright (c) 2020 Matthias Heinisch <nextcloud@matthiasheinisch.de>
+ *
+ * @author Matthias Heinisch <nextcloud@matthiasheinisch.de>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+
+namespace OCA\Contacts\Service\Social;
+
+use ChristophWurst\Nextcloud\Testing\TestCase;
+
+class GravatarProviderTest extends TestCase {
+ private $provider;
+
+ protected function setUp(): void {
+ parent::setUp();
+
+ $this->provider = new GravatarProvider(
+ );
+ }
+
+ public function dataProviderSupportsContact() {
+ $contactWithEmail = [
+ 'EMAIL' => [["value" => "one"], ["value" => "two"]]
+ ];
+
+ $contactWithoutEmail = [
+ 'PHONE' => [["value" => "one"], ["value" => "two"]]
+ ];
+
+ return [
+ 'contact with email' => [$contactWithEmail, true],
+ 'contact without email' => [$contactWithoutEmail, false]
+ ];
+ }
+
+ /**
+ * @dataProvider dataProviderSupportsContact
+ */
+ public function testSupportsContact($contact, $expected) {
+ $result = $this->provider->supportsContact($contact);
+ $this->assertEquals($expected, $result);
+ }
+
+ public function dataProviderGetImageUrls() {
+ $contactWithEmail = [
+ 'EMAIL' => [["value" => "one"], ["value" => "two"]]
+ ];
+
+ $contactWithoutEmail = [
+ 'PHONE' => [["value" => "one"], ["value" => "two"]]
+ ];
+
+ $urls = [];
+
+ foreach ($contactWithEmail['EMAIL'] as $email) {
+ $hash = md5(strtolower(trim($email['value'])));
+ $recipe = 'https://www.gravatar.com/avatar/{hash}?s=720&d=404';
+ $urls[] = str_replace("{hash}", $hash, $recipe);
+ }
+
+ return [
+ 'contact with email' => [$contactWithEmail, $urls],
+ 'contact without email' => [$contactWithoutEmail, []]
+ ];
+ }
+
+ /**
+ * @dataProvider dataProviderGetImageUrls
+ */
+ public function testGetImageUrls($contact, $expected) {
+ $result = $this->provider->getImageUrls($contact);
+ $this->assertEquals($expected, $result);
+ }
+}
diff --git a/tests/unit/Service/Social/InstagramProviderTest.php b/tests/unit/Service/Social/InstagramProviderTest.php
new file mode 100644
index 00000000..6fa0e866
--- /dev/null
+++ b/tests/unit/Service/Social/InstagramProviderTest.php
@@ -0,0 +1,158 @@
+<?php
+/**
+ * @copyright Copyright (c) 2020 Matthias Heinisch <nextcloud@matthiasheinisch.de>
+ *
+ * @author Matthias Heinisch <nextcloud@matthiasheinisch.de>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+
+namespace OCA\Contacts\Service\Social;