3 * @copyright Copyright (C) 2010-2021, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Test\src\Module\Api\Twitter;
24 use Friendica\Model\Contact;
25 use Friendica\Module\Api\Twitter\ContactEndpoint;
26 use Friendica\Network\HTTPException\InternalServerErrorException;
27 use Friendica\Network\HTTPException\NotFoundException;
28 use Friendica\Object\Api\Twitter\User;
29 use Friendica\Test\FixtureTest;
31 class ContactEndpointTest extends FixtureTest
33 public function testGetUid()
35 self::assertSame(42, ContactEndpointMock::getUid(42));
36 self::assertSame(42, ContactEndpointMock::getUid(null, 'selfcontact'));
37 self::assertSame(42, ContactEndpointMock::getUid(84, 'selfcontact'));
40 public function testGetUidContactIdNotFound()
42 $this->expectException(NotFoundException::class);
43 $this->expectExceptionMessage('Contact not found');
45 ContactEndpointMock::getUid(84);
48 public function testGetUidScreenNameNotFound()
50 $this->expectException(NotFoundException::class);
51 $this->expectExceptionMessage('User not found');
53 ContactEndpointMock::getUid(null, 'othercontact');
56 public function testGetUidContactIdScreenNameNotFound()
58 $this->expectException(NotFoundException::class);
59 $this->expectExceptionMessage('User not found');
61 ContactEndpointMock::getUid(42, 'othercontact');
64 public function testIds()
69 'next_cursor_str' => '-1',
70 'previous_cursor' => 0,
71 'previous_cursor_str' => '0',
75 self::assertSame($expectedEmpty, ContactEndpointMock::ids(Contact::FOLLOWER, 42));
80 'next_cursor_str' => '0',
81 'previous_cursor' => 0,
82 'previous_cursor_str' => '0',
86 self::assertSame($expectedFriend, ContactEndpointMock::ids(Contact::FRIEND, 42));
87 self::assertSame($expectedFriend, ContactEndpointMock::ids([Contact::FOLLOWER, Contact::FRIEND], 42));
89 $result = ContactEndpointMock::ids(Contact::SHARING, 42);
91 self::assertArrayHasKey('ids', $result);
92 self::assertContainsOnly('int', $result['ids']);
93 self::assertSame(45, $result['ids'][0]);
95 $result = ContactEndpointMock::ids([Contact::SHARING, Contact::FRIEND], 42);
97 self::assertArrayHasKey('ids', $result);
98 self::assertContainsOnly('int', $result['ids']);
99 self::assertSame(45, $result['ids'][0]);
105 * @throws NotFoundException
107 public function testIdsStringify()
109 $result = ContactEndpointMock::ids(Contact::SHARING, 42, -1, ContactEndpoint::DEFAULT_COUNT, true);
111 self::assertArrayHasKey('ids', $result);
112 self::assertContainsOnly('string', $result['ids']);
113 self::assertSame('45', $result['ids'][0]);
116 public function testIdsPagination()
118 $expectedDefaultPageResult = [
121 'next_cursor_str' => '44',
122 'previous_cursor' => 0,
123 'previous_cursor_str' => '0',
127 $result = ContactEndpointMock::ids([Contact::SHARING, Contact::FRIEND], 42, -1, 1);
129 self::assertSame($expectedDefaultPageResult, $result);
131 $nextPageCursor = $result['next_cursor'];
133 $expectedSecondPageResult = [
136 'next_cursor_str' => '46',
137 'previous_cursor' => -46,
138 'previous_cursor_str' => '-46',
142 $result = ContactEndpointMock::ids([Contact::SHARING, Contact::FRIEND], 42, $nextPageCursor, 1);
144 self::assertSame($expectedSecondPageResult, $result);
146 $firstPageCursor = $result['previous_cursor'];
147 $emptyNextPageCursor = $result['next_cursor'];
149 $expectedFirstPageResult = [
152 'next_cursor_str' => '44',
153 'previous_cursor' => -44,
154 'previous_cursor_str' => '-44',
158 $result = ContactEndpointMock::ids([Contact::SHARING, Contact::FRIEND], 42, $firstPageCursor, 1);
160 self::assertSame($expectedFirstPageResult, $result);
162 $emptyPrevPageCursor = $result['previous_cursor'];
164 $expectedEmptyPrevPageResult = [
167 'next_cursor_str' => '-1',
168 'previous_cursor' => 0,
169 'previous_cursor_str' => '0',
173 $result = ContactEndpointMock::ids([Contact::SHARING, Contact::FRIEND], 42, $emptyPrevPageCursor, 1);
175 self::assertSame($expectedEmptyPrevPageResult, $result);
177 $expectedEmptyNextPageResult = [
180 'next_cursor_str' => '0',
181 'previous_cursor' => -46,
182 'previous_cursor_str' => '-46',
186 $result = ContactEndpointMock::ids([Contact::SHARING, Contact::FRIEND], 42, $emptyNextPageCursor, 1);
188 self::assertSame($expectedEmptyNextPageResult, $result);
194 * @throws NotFoundException
195 * @throws InternalServerErrorException
196 * @throws \ImagickException
198 public function testList()
203 'next_cursor_str' => '-1',
204 'previous_cursor' => 0,
205 'previous_cursor_str' => '0',
209 self::assertSame($expectedEmpty, ContactEndpointMock::list(Contact::FOLLOWER, 42));
211 $expectedFriendContactUser = [
214 'name' => 'Friend contact',
215 'screen_name' => 'friendcontact',
216 'location' => 'DFRN',
218 'url' => 'http://localhost/profile/friendcontact',
228 'protected' => false,
230 'followers_count' => 0,
231 'friends_count' => 0,
233 'favourites_count' => 0,
234 'statuses_count' => 0,
235 'created_at' => 'Fri Feb 02 00:00:00 +0000 0000',
236 'profile_banner_url' => 'http://localhost/photo/header/44?ts=-62135596800',
237 'profile_image_url_https' => 'http://localhost/photo/contact/48/44?ts=-62135596800',
238 'default_profile' => false,
239 'default_profile_image' => false,
240 'profile_image_url' => 'http://localhost/photo/contact/48/44?ts=-62135596800',
241 'profile_image_url_profile_size' => 'http://localhost/photo/contact/80/44?ts=-62135596800',
242 'profile_image_url_large' => 'http://localhost/photo/contact/1024/44?ts=-62135596800',
244 'time_zone' => 'UTC',
245 'geo_enabled' => false,
247 'contributors_enabled' => false,
248 'is_translator' => false,
249 'is_translation_enabled' => false,
250 'following' => false,
251 'follow_request_sent' => false,
252 'statusnet_blocking' => false,
253 'notifications' => false,
259 'statusnet_profile_url' => 'http://localhost/profile/friendcontact',
262 $result = ContactEndpointMock::list(Contact::SHARING, 42);
264 self::assertArrayHasKey('users', $result);
265 self::assertContainsOnlyInstancesOf(User::class, $result['users']);
266 self::assertSame($expectedFriendContactUser, $result['users'][0]->toArray());
268 $result = ContactEndpointMock::list([Contact::SHARING, Contact::FRIEND], 42);
270 self::assertArrayHasKey('users', $result);
271 self::assertContainsOnlyInstancesOf(User::class, $result['users']);
272 self::assertSame($expectedFriendContactUser, $result['users'][0]->toArray());