]> git.mxchange.org Git - friendica.git/blob - tests/src/Module/Api/Twitter/ContactEndpointTest.php
Merge branch 'friendica:develop' into doc_faq_client-clean-up
[friendica.git] / tests / src / Module / Api / Twitter / ContactEndpointTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Test\src\Module\Api\Twitter;
23
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;
30
31 class ContactEndpointTest extends FixtureTest
32 {
33         public function testGetUid()
34         {
35                 self::assertSame(42, ContactEndpointMock::getUid(42));
36                 self::assertSame(42, ContactEndpointMock::getUid(null, 'selfcontact'));
37                 self::assertSame(42, ContactEndpointMock::getUid(84, 'selfcontact'));
38         }
39
40         public function testGetUidContactIdNotFound()
41         {
42                 $this->expectException(NotFoundException::class);
43                 $this->expectExceptionMessage('Contact not found');
44
45                 ContactEndpointMock::getUid(84);
46         }
47
48         public function testGetUidScreenNameNotFound()
49         {
50                 $this->expectException(NotFoundException::class);
51                 $this->expectExceptionMessage('User not found');
52
53                 ContactEndpointMock::getUid(null, 'othercontact');
54         }
55
56         public function testGetUidContactIdScreenNameNotFound()
57         {
58                 $this->expectException(NotFoundException::class);
59                 $this->expectExceptionMessage('User not found');
60
61                 ContactEndpointMock::getUid(42, 'othercontact');
62         }
63
64         public function testIds()
65         {
66                 $expectedEmpty = [
67                         'ids' => [],
68                         'next_cursor' => -1,
69                         'next_cursor_str' => '-1',
70                         'previous_cursor' => 0,
71                         'previous_cursor_str' => '0',
72                         'total_count' => 0,
73                 ];
74
75                 self::assertSame($expectedEmpty, ContactEndpointMock::ids(Contact::FOLLOWER, 42));
76
77                 $expectedFriend = [
78                         'ids' => [47],
79                         'next_cursor' => 0,
80                         'next_cursor_str' => '0',
81                         'previous_cursor' => 0,
82                         'previous_cursor_str' => '0',
83                         'total_count' => 1,
84                 ];
85
86                 self::assertSame($expectedFriend, ContactEndpointMock::ids(Contact::FRIEND, 42));
87                 self::assertSame($expectedFriend, ContactEndpointMock::ids([Contact::FOLLOWER, Contact::FRIEND], 42));
88
89                 $result = ContactEndpointMock::ids(Contact::SHARING, 42);
90
91                 self::assertArrayHasKey('ids', $result);
92                 self::assertContainsOnly('int', $result['ids']);
93                 self::assertSame(45, $result['ids'][0]);
94
95                 $result = ContactEndpointMock::ids([Contact::SHARING, Contact::FRIEND], 42);
96
97                 self::assertArrayHasKey('ids', $result);
98                 self::assertContainsOnly('int', $result['ids']);
99                 self::assertSame(45, $result['ids'][0]);
100         }
101
102         /**
103          * @depends testIds
104          *
105          * @throws NotFoundException
106          */
107         public function testIdsStringify()
108         {
109                 $result = ContactEndpointMock::ids(Contact::SHARING, 42, -1, ContactEndpoint::DEFAULT_COUNT, true);
110
111                 self::assertArrayHasKey('ids', $result);
112                 self::assertContainsOnly('string', $result['ids']);
113                 self::assertSame('45', $result['ids'][0]);
114         }
115
116         public function testIdsPagination()
117         {
118                 $expectedDefaultPageResult = [
119                         'ids' => [45],
120                         'next_cursor' => 44,
121                         'next_cursor_str' => '44',
122                         'previous_cursor' => 0,
123                         'previous_cursor_str' => '0',
124                         'total_count' => 2,
125                 ];
126
127                 $result = ContactEndpointMock::ids([Contact::SHARING, Contact::FRIEND], 42, -1, 1);
128
129                 self::assertSame($expectedDefaultPageResult, $result);
130
131                 $nextPageCursor = $result['next_cursor'];
132
133                 $expectedSecondPageResult = [
134                         'ids' => [47],
135                         'next_cursor' => 46,
136                         'next_cursor_str' => '46',
137                         'previous_cursor' => -46,
138                         'previous_cursor_str' => '-46',
139                         'total_count' => 2,
140                 ];
141
142                 $result = ContactEndpointMock::ids([Contact::SHARING, Contact::FRIEND], 42, $nextPageCursor, 1);
143
144                 self::assertSame($expectedSecondPageResult, $result);
145
146                 $firstPageCursor = $result['previous_cursor'];
147                 $emptyNextPageCursor = $result['next_cursor'];
148
149                 $expectedFirstPageResult = [
150                         'ids' => [45],
151                         'next_cursor' => 44,
152                         'next_cursor_str' => '44',
153                         'previous_cursor' => -44,
154                         'previous_cursor_str' => '-44',
155                         'total_count' => 2,
156                 ];
157
158                 $result = ContactEndpointMock::ids([Contact::SHARING, Contact::FRIEND], 42, $firstPageCursor, 1);
159
160                 self::assertSame($expectedFirstPageResult, $result);
161
162                 $emptyPrevPageCursor = $result['previous_cursor'];
163
164                 $expectedEmptyPrevPageResult = [
165                         'ids' => [],
166                         'next_cursor' => -1,
167                         'next_cursor_str' => '-1',
168                         'previous_cursor' => 0,
169                         'previous_cursor_str' => '0',
170                         'total_count' => 2,
171                 ];
172
173                 $result = ContactEndpointMock::ids([Contact::SHARING, Contact::FRIEND], 42, $emptyPrevPageCursor, 1);
174
175                 self::assertSame($expectedEmptyPrevPageResult, $result);
176
177                 $expectedEmptyNextPageResult = [
178                         'ids' => [],
179                         'next_cursor' => 0,
180                         'next_cursor_str' => '0',
181                         'previous_cursor' => -46,
182                         'previous_cursor_str' => '-46',
183                         'total_count' => 2,
184                 ];
185
186                 $result = ContactEndpointMock::ids([Contact::SHARING, Contact::FRIEND], 42, $emptyNextPageCursor, 1);
187
188                 self::assertSame($expectedEmptyNextPageResult, $result);
189         }
190
191         /**
192          * @depends testIds
193          *
194          * @throws NotFoundException
195          * @throws InternalServerErrorException
196          * @throws \ImagickException
197          */
198         public function testList()
199         {
200                 $expectedEmpty = [
201                         'users' => [],
202                         'next_cursor' => -1,
203                         'next_cursor_str' => '-1',
204                         'previous_cursor' => 0,
205                         'previous_cursor_str' => '0',
206                         'total_count' => 0,
207                 ];
208
209                 self::assertSame($expectedEmpty, ContactEndpointMock::list(Contact::FOLLOWER, 42));
210
211                 $expectedFriendContactUser = [
212                         'id' => 45,
213                         'id_str' => '45',
214                         'name' => 'Friend contact',
215                         'screen_name' => 'friendcontact',
216                         'location' => 'DFRN',
217                         'derived' => [],
218                         'url' => 'http://localhost/profile/friendcontact',
219                         'entities' => [
220                                 'url' => [
221                                         'urls' => [],
222                                 ],
223                                 'description' => [
224                                         'urls' => [],
225                                 ],
226                         ],
227                         'description' => '',
228                         'protected' => false,
229                         'verified' => true,
230                         'followers_count' => 0,
231                         'friends_count' => 0,
232                         'listed_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',
243                         'utc_offset' => 0,
244                         'time_zone' => 'UTC',
245                         'geo_enabled' => false,
246                         'lang' => NULL,
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,
254                         'uid' => 42,
255                         'cid' => 44,
256                         'pid' => 45,
257                         'self' => false,
258                         'network' => 'dfrn',
259                         'statusnet_profile_url' => 'http://localhost/profile/friendcontact',
260                 ];
261
262                 $result = ContactEndpointMock::list(Contact::SHARING, 42);
263
264                 self::assertArrayHasKey('users', $result);
265                 self::assertContainsOnlyInstancesOf(User::class, $result['users']);
266                 self::assertSame($expectedFriendContactUser, $result['users'][0]->toArray());
267
268                 $result = ContactEndpointMock::list([Contact::SHARING, Contact::FRIEND], 42);
269
270                 self::assertArrayHasKey('users', $result);
271                 self::assertContainsOnlyInstancesOf(User::class, $result['users']);
272                 self::assertSame($expectedFriendContactUser, $result['users'][0]->toArray());
273         }
274 }