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>
This commit is contained in:
leith abdulla 2020-11-04 11:04:41 -08:00
parent da321c1451
commit ca1c40a8ae
16 changed files with 1218 additions and 112 deletions

View File

@ -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;

View File

@ -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;
}

View File

@ -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;
$emails = $contact['EMAIL'];
if (isset($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 $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;
}
return [];
}
}

View File

@ -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,6 +79,26 @@ class InstagramProvider implements ISocialProvider {
$candidate = preg_replace('/^' . preg_quote('x-apple:', '/') . '/', '', $candidate);
return basename($candidate);
}
/**
* Returns all possible profile urls for contact
*
* @param {array} contact information
*
* @return array of string profile urls
*/
protected function getProfiles($contact):array {
$socialprofiles = $contact['X-SOCIALPROFILE'];
$profiles = [];
if (isset($socialprofiles)) {
foreach ($socialprofiles as $profile) {
if (strtolower($profile['type']) == $this->name) {
$profiles[] = $profile['value'];
}
}
}
return $profiles;
}
/**
* Returns all possible profile ids for contact
@ -97,14 +108,10 @@ class InstagramProvider implements ISocialProvider {
* @return array of string profile ids
*/
protected function getProfileIds($contact):array {
$socialprofiles = $contact['X-SOCIALPROFILE'];
$socialprofiles = $this->getProfiles($contact);
$profileIds = [];
if (isset($socialprofiles)) {
foreach ($socialprofiles as $profile) {
if (strtolower($profile['type']) == $this->name) {
$profileIds[] = $this->cleanupId($profile['value']);
}
}
foreach ($socialprofiles as $profile) {
$profileIds[] = $this->cleanupId($profile);
}
return $profileIds;
}

View File

@ -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;
}
/**

View File

@ -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;
}
/**

View File

@ -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;
}
/**

View File

@ -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;
}
/**

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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;
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 InstagramProviderTest 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 InstagramProvider(
$this->clientService
);
}
public function dataProviderSupportsContact() {
$contactWithSocial = [
'X-SOCIALPROFILE' => [
["value" => "username1", "type" => "instagram"],
["value" => "username2", "type" => "instagram"]
]
];
$contactWithoutSocial = [
'X-SOCIALPROFILE' => [
["value" => "one", "type" => "social2"],
["value" => "two", "type" => "social1"]
]
];
return [
'contact with instagram fields' => [$contactWithSocial, true],
'contact without instagram 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" => "username1", "type" => "instagram"],
["value" => "username2", "type" => "instagram"]
]
];
$contactWithSocialUrls = [
"https://www.instagram.com/username1/?__a=1",
"https://www.instagram.com/username2/?__a=1",
];
$contactWithSocialJson = [
json_encode(
["graphql" => ["user" => ["profile_pic_url_hd" => "username1.jpg"]]]
),
json_encode(
["graphql" => ["user" => ["profile_pic_url_hd" => "username2.jpg"]]]
)
];
$contactWithSocialImgs = [
"username1.jpg",
"username2.jpg"
];
$contactWithoutSocial = [
'X-SOCIALPROFILE' => [
["value" => "one", "type" => "social2"],
["value" => "two", "type" => "social1"]
]
];
$contactWithoutSocialUrls = [];
$contactWithoutSocialJson = [];
$contactWithoutSocialImgs = [];
return [
'contact with instagram fields' => [
$contactWithSocial,
$contactWithSocialJson,
$contactWithSocialUrls,
$contactWithSocialImgs
],
'contact without instagram fields' => [
$contactWithoutSocial,
$contactWithoutSocialJson,
$contactWithoutSocialUrls,
$contactWithoutSocialImgs
]
];
}
/**
* @dataProvider dataProviderGetImageUrls
*/
public function testGetImageUrls($contact, $json, $urls, $imgs) {
if (count($urls)) {
$this->response->method("getBody")->willReturnOnConsecutiveCalls(...$json);
$this->client
->expects($this->exactly(count($urls)))
->method("get")
->withConsecutive(...array_map(function ($a) {
return [$a];
}, $urls))
->willReturn($this->response);
}
$result = $this->provider->getImageUrls($contact);
$this->assertEquals($imgs, $result);
}
}

View File

@ -0,0 +1,154 @@
<?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 MastodonProviderTest 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 MastodonProvider(
$this->clientService
);
}
public function dataProviderSupportsContact() {
$contactWithSocial = [
'X-SOCIALPROFILE' => [
["value" => "user1@cloud1", "type" => "mastodon"],
["value" => "user2@cloud2", "type" => "mastodon"]
]
];
$contactWithoutSocial = [
'X-SOCIALPROFILE' => [
["value" => "one", "type" => "social2"],
["value" => "two", "type" => "social1"]
]
];
return [
'contact with mastodon fields' => [$contactWithSocial, true],
'contact without mastodon 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" => "user1@cloud1", "type" => "mastodon"],
["value" => "user2@cloud2", "type" => "mastodon"]
]
];
$contactWithSocialUrls = [
"https://cloud1/@user1",
"https://cloud2/@user2",
];
$contactWithSocialHtml = [
'<html><profile id="profile_page_avatar" data-original="user1.jpg" /></html>',
'<html><profile id="profile_page_avatar" data-original="user2.jpg" /></html>'
];
$contactWithSocialImgs = [
"user1.jpg",
"user2.jpg"
];
$contactWithoutSocial = [
'X-SOCIALPROFILE' => [
["value" => "one", "type" => "social2"],
["value" => "two", "type" => "social1"]
]
];
$contactWithoutSocialUrls = [];
$contactWithoutSocialHtml = [];
$contactWithoutSocialImgs = [];
return [
'contact with mastodon fields' => [
$contactWithSocial,
$contactWithSocialHtml,
$contactWithSocialUrls,
$contactWithSocialImgs
],
'contact without mastodon fields' => [
$contactWithoutSocial,
$contactWithoutSocialHtml,
$contactWithoutSocialUrls,
$contactWithoutSocialImgs
]
];
}
/**
* @dataProvider dataProviderGetImageUrls
*/
public function testGetImageUrls($contact, $htmls, $urls, $imgs) {
if (count($urls)) {
$this->response->method("getBody")->willReturnOnConsecutiveCalls(...$htmls);
$this->client
->expects($this->exactly(count($urls)))
->method("get")
->withConsecutive(...array_map(function ($a) {
return [$a];
}, $urls))
->willReturn($this->response);
}
$result = $this->provider->getImageUrls($contact);
$this->assertEquals($imgs, $result);
}
}

View File

@ -0,0 +1,100 @@
<?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 TumblrProviderTest extends TestCase {
private $provider;
protected function setUp(): void {
parent::setUp();
$this->provider = new TumblrProvider(
);
}
public function dataProviderSupportsContact() {
$contactWithSocial = [
'X-SOCIALPROFILE' => [
["value" => "username1", "type" => "tumblr"],
["value" => "username2", "type" => "tumblr"]
]
];
$contactWithoutSocial = [
'X-SOCIALPROFILE' => [
["value" => "one", "type" => "social2"],
["value" => "two", "type" => "social1"]
]
];
return [
'contact with email' => [$contactWithSocial, true],
'contact without email' => [$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" => "username1", "type" => "tumblr"],
["value" => "username2", "type" => "tumblr"]
]
];
$contactWithoutSocial = [
'X-SOCIALPROFILE' => [
["value" => "one", "type" => "social2"],
["value" => "two", "type" => "social1"]
]
];
foreach ($contactWithSocial['X-SOCIALPROFILE'] as $profile) {
$urls[] = "https://api.tumblr.com/v2/blog/".$profile['value']."/avatar/512";
}
return [
'contact with email' => [$contactWithSocial, $urls],
'contact without email' => [$contactWithoutSocial, []]
];
}
/**
* @dataProvider dataProviderGetImageUrls
*/
public function testGetImageUrls($contact, $expected) {
$result = $this->provider->getImageUrls($contact);
$this->assertEquals($expected, $result);
}
}

View File

@ -0,0 +1,154 @@
<?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 TwitterProviderTest 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 TwitterProvider(
$this->clientService
);
}
public function dataProviderSupportsContact() {
$contactWithSocial = [
'X-SOCIALPROFILE' => [
["value" => "username1", "type" => "twitter"],
["value" => "username2", "type" => "twitter"]
]
];
$contactWithoutSocial = [
'X-SOCIALPROFILE' => [
["value" => "one", "type" => "social2"],
["value" => "two", "type" => "social1"]
]
];
return [
'contact with twitter fields' => [$contactWithSocial, true],
'contact without twitter 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" => "https://twitter.com/username1", "type" => "twitter"],
["value" => "https://twitter.com/@username2", "type" => "twitter"]
]
];
$contactWithSocialUrls = [
"https://mobile.twitter.com/username1",
"https://mobile.twitter.com/username2",
];
$contactWithSocialHtml = [
'<html><img src="username1_normal.jpg" /></html>',
'<html><img src="username2_normal.jpg" /></html>',
];
$contactWithSocialImgs = [
"username1_400x400.jpg",
"username2_400x400.jpg"
];
$contactWithoutSocial = [
'X-SOCIALPROFILE' => [
["value" => "one", "type" => "social2"],
["value" => "two", "type" => "social1"]
]
];
$contactWithoutSocialUrls = [];
$contactWithoutSocialHtml = [];
$contactWithoutSocialImgs = [];
return [
'contact with twitter fields' => [
$contactWithSocial,
$contactWithSocialHtml,
$contactWithSocialUrls,
$contactWithSocialImgs
],
'contact without twitter fields' => [
$contactWithoutSocial,
$contactWithoutSocialHtml,
$contactWithoutSocialUrls,
$contactWithoutSocialImgs
]
];
}
/**
* @dataProvider dataProviderGetImageUrls
*/
public function testGetImageUrls($contact, $htmls, $urls, $imgs) {
if (count($urls)) {
$this->response->method("getBody")->willReturnOnConsecutiveCalls(...$htmls);
$this->client
->expects($this->exactly(count($urls)))
->method("get")
->withConsecutive(...array_map(function ($a) {
return [$a];
}, $urls))
->willReturn($this->response);
}
$result = $this->provider->getImageUrls($contact);
$this->assertEquals($imgs, $result);
}
}

View File

@ -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 XingProviderTest 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 XingProvider(
$this->clientService
);
}
public function dataProviderSupportsContact() {
$contactWithSocial = [
'X-SOCIALPROFILE' => [
["value" => "username1", "type" => "xing"],
["value" => "username2", "type" => "xing"]
]
];
$contactWithoutSocial = [
'X-SOCIALPROFILE' => [
["value" => "one", "type" => "social2"],
["value" => "two", "type" => "social1"]
]
];
return [
'contact with xing fields' => [$contactWithSocial, true],
'contact without xing 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" => "username1", "type" => "xing"],
["value" => "username2", "type" => "xing"]
]
];
$contactWithSocialUrls = [
"https://www.xing.com/profile/username1",
"https://www.xing.com/profile/username2"
];
$contactWithSocialHtml = array_map(function ($profile) {
return '<img src="https://profile-images-abc'.$profile['value'].'.jpg" />';
}, $contactWithSocial['X-SOCIALPROFILE']);
$contactWithSocialImg = array_map(function ($profile) {
return 'https://profile-images-abc'.$profile['value'].".jpg";
}, $contactWithSocial['X-SOCIALPROFILE']);
$contactWithoutSocial = [
'X-SOCIALPROFILE' => [
["value" => "one", "type" => "social2"],
["value" => "two", "type" => "social1"]
]
];
$contactWithoutSocialUrls = [];
$contactWithoutSocialHtml = [];
$contactWithoutSocialImg = [];
return [
'contact with xing fields' => [
$contactWithSocial,
$contactWithSocialUrls,
$contactWithSocialHtml,
$contactWithSocialImg
],
'contact without xing 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);
}
}