Skip to content

Commit

Permalink
unit tests for all providers
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
eleith committed Nov 4, 2020
1 parent da321c1 commit fd82342
Show file tree
Hide file tree
Showing 16 changed files with 1,209 additions and 113 deletions.
15 changes: 3 additions & 12 deletions lib/Service/Social/DiasporaProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,8 @@ public function __construct(IClientService $httpClient) {
* @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;
}

/**
Expand Down Expand Up @@ -150,7 +141,7 @@ protected function cleanupId(string $candidate):?string {
$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;
Expand Down
35 changes: 21 additions & 14 deletions lib/Service/Social/FacebookProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,8 @@ public function __construct(IClientService $httpClient) {
* @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;
}

/**
Expand Down Expand Up @@ -98,16 +89,32 @@ protected function cleanupId(string $candidate):string {
*
* @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;
}

Expand Down
36 changes: 10 additions & 26 deletions lib/Service/Social/GravatarProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
}

/**
Expand All @@ -53,29 +50,16 @@ public function supportsContact(array $contact):bool {
* @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'];
$emails = $contact['EMAIL'];
if (isset($emails)) {
return $emails;
}
return [];
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;
}
}
43 changes: 25 additions & 18 deletions lib/Service/Social/InstagramProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,8 @@ public function __construct(IClientService $httpClient) {
* @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;
}

/**
Expand Down Expand Up @@ -88,24 +79,40 @@ protected function cleanupId(string $candidate):string {
$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;
}

Expand Down
13 changes: 2 additions & 11 deletions lib/Service/Social/MastodonProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,8 @@ public function __construct(IClientService $httpClient) {
* @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;
}

/**
Expand Down
13 changes: 2 additions & 11 deletions lib/Service/Social/TumblrProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,8 @@ public function __construct() {
* @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;
}

/**
Expand Down
11 changes: 2 additions & 9 deletions lib/Service/Social/TwitterProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,8 @@ public function __construct(IClientService $httpClient) {
* @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;
}

/**
Expand Down
14 changes: 2 additions & 12 deletions lib/Service/Social/XingProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ class XingProvider implements ISocialProvider {

public function __construct(IClientService $httpClient) {
$this->httpClient = $httpClient->NewClient();
$this->looping = false;
}

/**
Expand All @@ -46,17 +45,8 @@ public function __construct(IClientService $httpClient) {
* @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;
}

/**
Expand Down
Loading

0 comments on commit fd82342

Please sign in to comment.