summaryrefslogtreecommitdiffstats
path: root/tests
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 /tests
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>
Diffstat (limited to 'tests')
-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
8 files changed, 1152 insertions, 0 deletions
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;
+
+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);
+ }
+}
diff --git a/tests/unit/Service/Social/MastodonProviderTest.php b/tests/unit/Service/Social/MastodonProviderTest.php
new file mode 100644
index 00000000..9611bf6b
--- /dev/null
+++ b/tests/unit/Service/Social/MastodonProviderTest.php
@@ -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);
+ }
+}
diff --git a/tests/unit/Service/Social/TumblrProviderTest.php b/tests/unit/Service/Social/TumblrProviderTest.php
new file mode 100644
index 00000000..79e1c931
--- /dev/null
+++ b/tests/unit/Service/Social/TumblrProviderTest.php
@@ -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);
+ }
+}
diff --git a/tests/unit/Service/Social/TwitterProviderTest.php b/tests/unit/Service/Social/TwitterProviderTest.php
new file mode 100644
index 00000000..55515b0f
--- /dev/null
+++ b/tests/unit/Service/Social/TwitterProviderTest.php
@@ -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
+ );