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