1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use \Mockery as Mockery; |
4
|
|
|
|
5
|
|
|
class InstagramAccountTest extends SapphireTest |
|
|
|
|
6
|
|
|
{ |
7
|
|
|
public function tearDown() |
8
|
|
|
{ |
9
|
|
|
Mockery::close(); |
10
|
|
|
parent::tearDown(); |
11
|
|
|
} |
12
|
|
|
|
13
|
|
|
protected function getNewInstagramAccountMock() |
14
|
|
|
{ |
15
|
|
|
return Mockery::mock('InstagramAccount'); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
public function testUpdateAccessToken() |
19
|
|
|
{ |
20
|
|
|
$validToken = |
21
|
|
|
'{"user":{"username":"somardesignstudios","bio":"","website":"","profile_picture":"",' . |
22
|
|
|
'"full_name":"","id":"12345678"},"access_token":"","resource_owner_id":""}'; |
23
|
|
|
|
24
|
|
|
$invalidToken = |
25
|
|
|
'{"user":{"username":"somardesignstudios","bio":"","website":"","profile_picture":"",' . |
26
|
|
|
'"full_name":"","id":"87654321"},"access_token":"","resource_owner_id":""}'; |
27
|
|
|
|
28
|
|
|
/* |
29
|
|
|
* Should throw if the session state is invalid. |
30
|
|
|
*/ |
31
|
|
|
$mock = $this->getNewInstagramAccountMock()->makePartial(); |
32
|
|
|
$mock |
33
|
|
|
->shouldReceive('getSessionOAuthState') |
34
|
|
|
->once() |
35
|
|
|
->withNoArgs() |
36
|
|
|
->andReturn('state'); |
37
|
|
|
|
38
|
|
|
try { |
39
|
|
|
$mock->updateAccessToken($validToken, 'invalidState'); |
40
|
|
|
} catch (Exception $e) { |
41
|
|
|
$this->assertEquals('Trying to set token on wrong InstagramAccount', $e->getMessage()); |
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/* |
45
|
|
|
* Should throw if the session state is valid and the token username doesn't match the title. |
46
|
|
|
*/ |
47
|
|
|
$mock = $this->getNewInstagramAccountMock()->makePartial(); |
48
|
|
|
$mock |
49
|
|
|
->shouldReceive('getSessionOAuthState') |
50
|
|
|
->once() |
51
|
|
|
->withNoArgs() |
52
|
|
|
->andReturn('state'); |
53
|
|
|
$mock |
54
|
|
|
->shouldReceive('getField') |
55
|
|
|
->once() |
56
|
|
|
->with('Title') |
57
|
|
|
->andReturn('otherusername'); |
58
|
|
|
|
59
|
|
|
try { |
60
|
|
|
$mock->updateAccessToken($validToken, 'state'); |
61
|
|
|
} catch (Exception $e) { |
62
|
|
|
$this->assertEquals('Trying to set token on wrong InstagramAccount', $e->getMessage()); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/* |
66
|
|
|
* Should set the token if session state is valid and there's no current token. |
67
|
|
|
*/ |
68
|
|
|
$mock = $this->getNewInstagramAccountMock()->makePartial(); |
69
|
|
|
$mock |
70
|
|
|
->shouldReceive('getSessionOAuthState') |
71
|
|
|
->once() |
72
|
|
|
->withNoArgs() |
73
|
|
|
->andReturn('state'); |
74
|
|
|
$mock |
75
|
|
|
->shouldReceive('getField') |
76
|
|
|
->with('Title') |
77
|
|
|
->andReturn('somardesignstudios'); |
78
|
|
|
$mock |
79
|
|
|
->shouldReceive('getField') |
80
|
|
|
->with('AccessToken') |
81
|
|
|
->andReturn(null); |
82
|
|
|
$mock |
83
|
|
|
->shouldReceive('setField') |
84
|
|
|
->once() |
85
|
|
|
->with('AccessToken', $validToken); |
86
|
|
|
|
87
|
|
|
$mock->updateAccessToken($validToken, 'state'); |
88
|
|
|
|
89
|
|
|
/* |
90
|
|
|
* Should throw if the session state is valid, the token username matches the title, |
91
|
|
|
* and the new token is for another account. |
92
|
|
|
*/ |
93
|
|
|
$mock = $this->getNewInstagramAccountMock()->makePartial(); |
94
|
|
|
$mock |
95
|
|
|
->shouldReceive('getSessionOAuthState') |
96
|
|
|
->once() |
97
|
|
|
->withNoArgs() |
98
|
|
|
->andReturn('state'); |
99
|
|
|
$mock |
100
|
|
|
->shouldReceive('getField') |
101
|
|
|
->with('Title') |
102
|
|
|
->andReturn('somardesignstudios'); |
103
|
|
|
$mock |
104
|
|
|
->shouldReceive('getField') |
105
|
|
|
->with('AccessToken') |
106
|
|
|
->andReturn($validToken); |
107
|
|
|
|
108
|
|
|
try { |
109
|
|
|
$mock->updateAccessToken($invalidToken, 'state'); |
110
|
|
|
} catch (Exception $e) { |
111
|
|
|
$this->assertEquals('Trying to set token on wrong InstagramAccount', $e->getMessage()); |
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/* |
115
|
|
|
* Should update the token if the session state is valid and the token IDs match. |
116
|
|
|
*/ |
117
|
|
|
$mock = $this->getNewInstagramAccountMock()->makePartial(); |
118
|
|
|
$mock |
119
|
|
|
->shouldReceive('getSessionOAuthState') |
120
|
|
|
->once() |
121
|
|
|
->withNoArgs() |
122
|
|
|
->andReturn('state'); |
123
|
|
|
$mock |
124
|
|
|
->shouldReceive('getField') |
125
|
|
|
->with('Title') |
126
|
|
|
->andReturn('somardesignstudios'); |
127
|
|
|
$mock |
128
|
|
|
->shouldReceive('getField') |
129
|
|
|
->with('AccessToken') |
130
|
|
|
->andReturn($validToken); |
131
|
|
|
$mock |
132
|
|
|
->shouldReceive('setField') |
133
|
|
|
->once() |
134
|
|
|
->with('AccessToken', $validToken); |
135
|
|
|
|
136
|
|
|
$mock->updateAccessToken($validToken, 'state'); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function testGetOAuthStateValueFromLoginURL() |
140
|
|
|
{ |
141
|
|
|
$instagramAccount = new InstagramAccount(); |
142
|
|
|
|
143
|
|
|
/* |
144
|
|
|
* Should return null if no URL is passed. |
145
|
|
|
*/ |
146
|
|
|
$this->assertEquals(null, $instagramAccount->getOAuthStateValueFromLoginURL()); |
|
|
|
|
147
|
|
|
|
148
|
|
|
/* |
149
|
|
|
* Should return null if there's no "state" query string. |
150
|
|
|
*/ |
151
|
|
|
$this->assertEquals( |
|
|
|
|
152
|
|
|
null, |
153
|
|
|
$instagramAccount->getOAuthStateValueFromLoginURL( |
154
|
|
|
'http://example.com/admin/admin/instagram/InstagramAccount/OAuth?code=123' |
155
|
|
|
) |
156
|
|
|
); |
157
|
|
|
|
158
|
|
|
/* |
159
|
|
|
* Should return the value of the "state" query string if it exists. |
160
|
|
|
*/ |
161
|
|
|
$this->assertEquals( |
|
|
|
|
162
|
|
|
'abc', |
163
|
|
|
$instagramAccount->getOAuthStateValueFromLoginURL( |
164
|
|
|
'http://example.com/admin/admin/instagram/InstagramAccount/OAuth?code=123&state=abc' |
165
|
|
|
) |
166
|
|
|
); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public function testIsValidMediaID() |
170
|
|
|
{ |
171
|
|
|
/* |
172
|
|
|
* Should return false if no media ID is passed. |
173
|
|
|
*/ |
174
|
|
|
$instagramAccount = new InstagramAccount(); |
175
|
|
|
|
176
|
|
|
$this->assertEquals(false, $instagramAccount->isValidMediaID()); |
|
|
|
|
177
|
|
|
|
178
|
|
|
/* |
179
|
|
|
* Should return false if the account ID is unavailable. |
180
|
|
|
*/ |
181
|
|
|
$mock = $this->getNewInstagramAccountMock()->makePartial(); |
182
|
|
|
$mock |
183
|
|
|
->shouldReceive('getInstagramID') |
184
|
|
|
->withNoArgs() |
185
|
|
|
->andReturn(null); |
186
|
|
|
|
187
|
|
|
$this->assertEquals( |
|
|
|
|
188
|
|
|
false, |
189
|
|
|
$mock->isValidMediaID('123456789012345678_123') |
190
|
|
|
); |
191
|
|
|
|
192
|
|
|
/* |
193
|
|
|
* Should return false if the media ID is less than 18 digits. |
194
|
|
|
*/ |
195
|
|
|
$mock = $this->getNewInstagramAccountMock()->makePartial(); |
196
|
|
|
$mock |
197
|
|
|
->shouldReceive('getInstagramID') |
198
|
|
|
->withNoArgs() |
199
|
|
|
->andReturn('123'); |
200
|
|
|
|
201
|
|
|
$this->assertEquals( |
|
|
|
|
202
|
|
|
false, |
203
|
|
|
$mock->isValidMediaID('12345678901234567_123') |
204
|
|
|
); |
205
|
|
|
|
206
|
|
|
/* |
207
|
|
|
* Should return false if the media ID is greater than 19 digits. |
208
|
|
|
*/ |
209
|
|
|
$this->assertEquals( |
|
|
|
|
210
|
|
|
false, |
211
|
|
|
$mock->isValidMediaID('12345678901234567890_123') |
212
|
|
|
); |
213
|
|
|
|
214
|
|
|
/* |
215
|
|
|
* Should return false if the media ID length is valid but contains non-digits. |
216
|
|
|
*/ |
217
|
|
|
$this->assertEquals( |
|
|
|
|
218
|
|
|
false, |
219
|
|
|
$mock->isValidMediaID('123456789Z12345678_123') |
220
|
|
|
); |
221
|
|
|
|
222
|
|
|
$this->assertEquals( |
|
|
|
|
223
|
|
|
false, |
224
|
|
|
$mock->isValidMediaID('123456789&12345678_123') |
225
|
|
|
); |
226
|
|
|
|
227
|
|
|
/* |
228
|
|
|
* Should return false if a valid media ID is not followed by an underscore. |
229
|
|
|
*/ |
230
|
|
|
$this->assertEquals( |
|
|
|
|
231
|
|
|
false, |
232
|
|
|
$mock->isValidMediaID('123456789012345678-123') |
233
|
|
|
); |
234
|
|
|
|
235
|
|
|
/* |
236
|
|
|
* Should return false if the media ID doesn't end with the account ID. |
237
|
|
|
*/ |
238
|
|
|
$this->assertEquals( |
|
|
|
|
239
|
|
|
false, |
240
|
|
|
$mock->isValidMediaID('123456789012345678_321') |
241
|
|
|
); |
242
|
|
|
|
243
|
|
|
/* |
244
|
|
|
* Should return true if the media ID is 18 or 19 characters followed by |
245
|
|
|
* an underscore and the account ID. |
246
|
|
|
*/ |
247
|
|
|
$this->assertEquals( |
|
|
|
|
248
|
|
|
true, |
249
|
|
|
$mock->isValidMediaID('123456789012345678_123') |
250
|
|
|
); |
251
|
|
|
|
252
|
|
|
$this->assertEquals( |
|
|
|
|
253
|
|
|
true, |
254
|
|
|
$mock->isValidMediaID('1234567890123456789_123') |
255
|
|
|
); |
256
|
|
|
} |
257
|
|
|
} |
258
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.