]> git.mxchange.org Git - friendica.git/blob - tests/src/Module/Api/Twitter/ContactEndpointTest.php
Merge remote-tracking branch 'upstream/develop' into item-view
[friendica.git] / tests / src / Module / Api / Twitter / ContactEndpointTest.php
1 <?php
2
3 namespace Friendica\Test\src\Module\Api\Twitter;
4
5 use Friendica\Model\Contact;
6 use Friendica\Module\Api\Twitter\ContactEndpoint;
7 use Friendica\Network\HTTPException\InternalServerErrorException;
8 use Friendica\Network\HTTPException\NotFoundException;
9 use Friendica\Object\Api\Twitter\User;
10 use Friendica\Test\FixtureTest;
11
12 class ContactEndpointTest extends FixtureTest
13 {
14         public function testGetUid()
15         {
16                 self::assertSame(42, ContactEndpointMock::getUid(42));
17                 self::assertSame(42, ContactEndpointMock::getUid(null, 'selfcontact'));
18                 self::assertSame(42, ContactEndpointMock::getUid(84, 'selfcontact'));
19         }
20
21         public function testGetUidContactIdNotFound()
22         {
23                 $this->expectException(NotFoundException::class);
24                 $this->expectExceptionMessage('Contact not found');
25
26                 ContactEndpointMock::getUid(84);
27         }
28
29         public function testGetUidScreenNameNotFound()
30         {
31                 $this->expectException(NotFoundException::class);
32                 $this->expectExceptionMessage('User not found');
33
34                 ContactEndpointMock::getUid(null, 'othercontact');
35         }
36
37         public function testGetUidContactIdScreenNameNotFound()
38         {
39                 $this->expectException(NotFoundException::class);
40                 $this->expectExceptionMessage('User not found');
41
42                 ContactEndpointMock::getUid(42, 'othercontact');
43         }
44
45         public function testIds()
46         {
47                 $expectedEmpty = [
48                         'ids' => [],
49                         'next_cursor' => -1,
50                         'next_cursor_str' => '-1',
51                         'previous_cursor' => 0,
52                         'previous_cursor_str' => '0',
53                         'total_count' => 0,
54                 ];
55
56                 self::assertSame($expectedEmpty, ContactEndpointMock::ids(Contact::FOLLOWER, 42));
57
58                 $expectedFriend = [
59                         'ids' => [47],
60                         'next_cursor' => 0,
61                         'next_cursor_str' => '0',
62                         'previous_cursor' => 0,
63                         'previous_cursor_str' => '0',
64                         'total_count' => 1,
65                 ];
66
67                 self::assertSame($expectedFriend, ContactEndpointMock::ids(Contact::FRIEND, 42));
68                 self::assertSame($expectedFriend, ContactEndpointMock::ids([Contact::FOLLOWER, Contact::FRIEND], 42));
69
70                 $result = ContactEndpointMock::ids(Contact::SHARING, 42);
71
72                 self::assertArrayHasKey('ids', $result);
73                 self::assertContainsOnly('int', $result['ids']);
74                 self::assertSame(45, $result['ids'][0]);
75
76                 $result = ContactEndpointMock::ids([Contact::SHARING, Contact::FRIEND], 42);
77
78                 self::assertArrayHasKey('ids', $result);
79                 self::assertContainsOnly('int', $result['ids']);
80                 self::assertSame(45, $result['ids'][0]);
81         }
82
83         /**
84          * @depends testIds
85          *
86          * @throws NotFoundException
87          */
88         public function testIdsStringify()
89         {
90                 $result = ContactEndpointMock::ids(Contact::SHARING, 42, -1, ContactEndpoint::DEFAULT_COUNT, true);
91
92                 self::assertArrayHasKey('ids', $result);
93                 self::assertContainsOnly('string', $result['ids']);
94                 self::assertSame('45', $result['ids'][0]);
95         }
96
97         public function testIdsPagination()
98         {
99                 $expectedDefaultPageResult = [
100                         'ids' => [45],
101                         'next_cursor' => 44,
102                         'next_cursor_str' => '44',
103                         'previous_cursor' => 0,
104                         'previous_cursor_str' => '0',
105                         'total_count' => 2,
106                 ];
107
108                 $result = ContactEndpointMock::ids([Contact::SHARING, Contact::FRIEND], 42, -1, 1);
109
110                 self::assertSame($expectedDefaultPageResult, $result);
111
112                 $nextPageCursor = $result['next_cursor'];
113
114                 $expectedSecondPageResult = [
115                         'ids' => [47],
116                         'next_cursor' => 46,
117                         'next_cursor_str' => '46',
118                         'previous_cursor' => -46,
119                         'previous_cursor_str' => '-46',
120                         'total_count' => 2,
121                 ];
122
123                 $result = ContactEndpointMock::ids([Contact::SHARING, Contact::FRIEND], 42, $nextPageCursor, 1);
124
125                 self::assertSame($expectedSecondPageResult, $result);
126
127                 $firstPageCursor = $result['previous_cursor'];
128                 $emptyNextPageCursor = $result['next_cursor'];
129
130                 $expectedFirstPageResult = [
131                         'ids' => [45],
132                         'next_cursor' => 44,
133                         'next_cursor_str' => '44',
134                         'previous_cursor' => -44,
135                         'previous_cursor_str' => '-44',
136                         'total_count' => 2,
137                 ];
138
139                 $result = ContactEndpointMock::ids([Contact::SHARING, Contact::FRIEND], 42, $firstPageCursor, 1);
140
141                 self::assertSame($expectedFirstPageResult, $result);
142
143                 $emptyPrevPageCursor = $result['previous_cursor'];
144
145                 $expectedEmptyPrevPageResult = [
146                         'ids' => [],
147                         'next_cursor' => -1,
148                         'next_cursor_str' => '-1',
149                         'previous_cursor' => 0,
150                         'previous_cursor_str' => '0',
151                         'total_count' => 2,
152                 ];
153
154                 $result = ContactEndpointMock::ids([Contact::SHARING, Contact::FRIEND], 42, $emptyPrevPageCursor, 1);
155
156                 self::assertSame($expectedEmptyPrevPageResult, $result);
157
158                 $expectedEmptyNextPageResult = [
159                         'ids' => [],
160                         'next_cursor' => 0,
161                         'next_cursor_str' => '0',
162                         'previous_cursor' => -46,
163                         'previous_cursor_str' => '-46',
164                         'total_count' => 2,
165                 ];
166
167                 $result = ContactEndpointMock::ids([Contact::SHARING, Contact::FRIEND], 42, $emptyNextPageCursor, 1);
168
169                 self::assertSame($expectedEmptyNextPageResult, $result);
170         }
171
172         /**
173          * @depends testIds
174          *
175          * @throws NotFoundException
176          * @throws InternalServerErrorException
177          * @throws \ImagickException
178          */
179         public function testList()
180         {
181                 $expectedEmpty = [
182                         'users' => [],
183                         'next_cursor' => -1,
184                         'next_cursor_str' => '-1',
185                         'previous_cursor' => 0,
186                         'previous_cursor_str' => '0',
187                         'total_count' => 0,
188                 ];
189
190                 self::assertSame($expectedEmpty, ContactEndpointMock::list(Contact::FOLLOWER, 42));
191
192                 $expectedFriendContactUser = [
193                         'id' => 45,
194                         'id_str' => '45',
195                         'name' => 'Friend contact',
196                         'screen_name' => 'friendcontact',
197                         'location' => 'DFRN',
198                         'derived' => [],
199                         'url' => 'http://localhost/profile/friendcontact',
200                         'entities' => [
201                                 'url' => [
202                                         'urls' => [],
203                                 ],
204                                 'description' => [
205                                         'urls' => [],
206                                 ],
207                         ],
208                         'description' => '',
209                         'protected' => false,
210                         'verified' => false,
211                         'followers_count' => 0,
212                         'friends_count' => 0,
213                         'listed_count' => 0,
214                         'favourites_count' => 0,
215                         'statuses_count' => 0,
216                         'created_at' => 'Fri Feb 02 00:00:00 +0000 0000',
217                         'profile_banner_url' => '',
218                         'profile_image_url_https' => '',
219                         'default_profile' => false,
220                         'default_profile_image' => false,
221                         'profile_image_url' => '',
222                         'profile_image_url_profile_size' => '',
223                         'profile_image_url_large' => '',
224                         'utc_offset' => 0,
225                         'time_zone' => 'UTC',
226                         'geo_enabled' => false,
227                         'lang' => NULL,
228                         'contributors_enabled' => false,
229                         'is_translator' => false,
230                         'is_translation_enabled' => false,
231                         'following' => false,
232                         'follow_request_sent' => false,
233                         'statusnet_blocking' => false,
234                         'notifications' => false,
235                         'uid' => 42,
236                         'cid' => 44,
237                         'pid' => 45,
238                         'self' => false,
239                         'network' => 'dfrn',
240                         'statusnet_profile_url' => 'http://localhost/profile/friendcontact',
241                 ];
242
243                 $result = ContactEndpointMock::list(Contact::SHARING, 42);
244
245                 self::assertArrayHasKey('users', $result);
246                 self::assertContainsOnlyInstancesOf(User::class, $result['users']);
247                 self::assertSame($expectedFriendContactUser, $result['users'][0]->toArray());
248
249                 $result = ContactEndpointMock::list([Contact::SHARING, Contact::FRIEND], 42);
250
251                 self::assertArrayHasKey('users', $result);
252                 self::assertContainsOnlyInstancesOf(User::class, $result['users']);
253                 self::assertSame($expectedFriendContactUser, $result['users'][0]->toArray());
254         }
255 }