1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class OAuthTokenTest extends PHPUnit_Framework_TestCase |
|
|
|
|
4
|
|
|
{ |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
public function test__construct() |
8
|
|
|
{ |
9
|
|
|
|
10
|
|
|
//execute the contructor and check for the Object type and attributes |
11
|
|
|
$oauthToken = new OAuthToken(); |
|
|
|
|
12
|
|
|
|
13
|
|
|
$this->assertInstanceOf('OAuthToken',$oauthToken); |
14
|
|
|
$this->assertInstanceOf('SugarBean',$oauthToken); |
15
|
|
|
|
16
|
|
|
$this->assertAttributeEquals('OAuthTokens', 'module_dir', $oauthToken); |
17
|
|
|
$this->assertAttributeEquals('OAuthToken', 'object_name', $oauthToken); |
18
|
|
|
$this->assertAttributeEquals('oauth_tokens', 'table_name', $oauthToken); |
19
|
|
|
|
20
|
|
|
$this->assertAttributeEquals(true, 'disable_row_level_security', $oauthToken); |
21
|
|
|
|
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
public function testsetState() |
26
|
|
|
{ |
27
|
|
|
error_reporting(E_ERROR | E_PARSE); |
28
|
|
|
|
29
|
|
|
$oauthToken = new OAuthToken(); |
|
|
|
|
30
|
|
|
$oauthToken->setState(); |
31
|
|
|
|
32
|
|
|
$this->assertEquals( $oauthToken->REQUEST ,$oauthToken->tstate); |
33
|
|
|
|
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
public function testsetConsumer() |
38
|
|
|
{ |
39
|
|
|
$oauthToken = new OAuthToken(); |
|
|
|
|
40
|
|
|
|
41
|
|
|
$oauthKey = new OAuthKey(); |
42
|
|
|
$oauthKey->id = "1"; |
43
|
|
|
|
44
|
|
|
$oauthToken->setConsumer($oauthKey); |
45
|
|
|
|
46
|
|
|
$this->assertEquals( $oauthKey->id ,$oauthToken->consumer); |
47
|
|
|
$this->assertEquals( $oauthKey ,$oauthToken->consumer_obj); |
48
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
public function testsetCallbackURL() |
53
|
|
|
{ |
54
|
|
|
$oauthToken = new OAuthToken(); |
|
|
|
|
55
|
|
|
|
56
|
|
|
$url = "test url"; |
57
|
|
|
$oauthToken->setCallbackURL($url); |
58
|
|
|
|
59
|
|
|
$this->assertEquals( $url ,$oauthToken->callback_url ); |
60
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
public function testgenerate() |
65
|
|
|
{ |
66
|
|
|
|
67
|
|
|
$result = OAuthToken::generate(); |
68
|
|
|
|
69
|
|
|
$this->assertInstanceOf('OAuthToken',$result); |
70
|
|
|
$this->assertGreaterThan(0,strlen($result->token)); |
71
|
|
|
$this->assertGreaterThan(0,strlen($result->secret)); |
72
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
public function testSaveAndOthers() |
77
|
|
|
{ |
78
|
|
|
$oauthToken = OAuthToken::generate(); |
79
|
|
|
|
80
|
|
|
$oauthToken->save(); |
81
|
|
|
|
82
|
|
|
//test for record ID to verify that record is saved |
83
|
|
|
$this->assertTrue(isset($oauthToken->id)); |
84
|
|
|
$this->assertEquals(12, strlen($oauthToken->id)); |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
//test load method |
88
|
|
|
$this->load($oauthToken->id); |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
//test invalidate method |
92
|
|
|
$token = OAuthToken::load($oauthToken->id); |
93
|
|
|
$this->invalidate($token); |
94
|
|
|
|
95
|
|
|
|
96
|
|
|
//test authorize method |
97
|
|
|
$this->authorize($token); |
98
|
|
|
|
99
|
|
|
|
100
|
|
|
//test mark_deleted method |
101
|
|
|
$this->mark_deleted($oauthToken->id); |
102
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function load($id) |
106
|
|
|
{ |
107
|
|
|
$token = OAuthToken::load($id); |
108
|
|
|
|
109
|
|
|
$this->assertInstanceOf('OAuthToken',$token); |
110
|
|
|
$this->assertTrue(isset($token->id)); |
111
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function invalidate($token) |
115
|
|
|
{ |
116
|
|
|
|
117
|
|
|
$token->invalidate(); |
118
|
|
|
|
119
|
|
|
$this->assertEquals( $token::INVALID ,$token->tstate); |
120
|
|
|
$this->assertEquals( false ,$token->verify); |
121
|
|
|
|
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
|
125
|
|
|
public function authorize($token) |
126
|
|
|
{ |
127
|
|
|
|
128
|
|
|
$result = $token->authorize("test"); |
129
|
|
|
$this->assertEquals(false, $result); |
130
|
|
|
|
131
|
|
|
$token->tstate = $token::REQUEST ; |
132
|
|
|
$result = $token->authorize("test"); |
133
|
|
|
|
134
|
|
|
$this->assertEquals("test", $token->authdata); |
135
|
|
|
$this->assertGreaterThan(0, strlen($result)); |
136
|
|
|
$this->assertEquals($result, $token->verify); |
137
|
|
|
|
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
|
141
|
|
|
public function mark_deleted($id) |
142
|
|
|
{ |
143
|
|
|
|
144
|
|
|
$oauthToken = new OAuthToken(); |
|
|
|
|
145
|
|
|
|
146
|
|
|
//execute the method |
147
|
|
|
$oauthToken->mark_deleted($id); |
148
|
|
|
|
149
|
|
|
//verify that record can not be loaded anymore |
150
|
|
|
$token = OAuthToken::load($id); |
151
|
|
|
$this->assertEquals(null, $token->id); |
152
|
|
|
|
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
|
156
|
|
|
public function testcreateAuthorized() |
157
|
|
|
{ |
158
|
|
|
|
159
|
|
|
$oauthKey = new OAuthKey(); |
160
|
|
|
$oauthKey->id = "1"; |
161
|
|
|
|
162
|
|
|
$user = new User(); |
163
|
|
|
$user->retrieve("1"); |
164
|
|
|
|
165
|
|
|
$oauthToken = OAuthToken::createAuthorized($oauthKey, $user); |
166
|
|
|
|
167
|
|
|
$this->assertEquals( $oauthKey->id ,$oauthToken->consumer); |
168
|
|
|
$this->assertEquals( $oauthKey ,$oauthToken->consumer_obj); |
169
|
|
|
$this->assertEquals( $oauthToken::ACCESS ,$oauthToken->tstate); |
170
|
|
|
$this->assertEquals( $user->id ,$oauthToken->assigned_user_id); |
171
|
|
|
|
172
|
|
|
|
173
|
|
|
//execute copyAuthData method |
174
|
|
|
$oauthToken->authdata = "test"; |
175
|
|
|
$this->copyAuthData($oauthToken); |
176
|
|
|
|
177
|
|
|
//finally mark deleted for cleanup |
178
|
|
|
$oauthToken->mark_deleted($oauthToken->id); |
179
|
|
|
|
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
|
183
|
|
|
public function copyAuthData($token) |
184
|
|
|
{ |
185
|
|
|
$oauthToken = new OAuthToken(); |
|
|
|
|
186
|
|
|
|
187
|
|
|
$oauthToken->copyAuthData($token); |
188
|
|
|
$this->assertEquals( $token->authdata , $oauthToken->authdata); |
189
|
|
|
$this->assertEquals( $token->assigned_user_id , $oauthToken->assigned_user_id); |
190
|
|
|
|
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
|
194
|
|
|
public function testqueryString() |
195
|
|
|
{ |
196
|
|
|
$oauthToken = new OAuthToken(); |
|
|
|
|
197
|
|
|
|
198
|
|
|
$result = $oauthToken->queryString(); |
199
|
|
|
$this->assertEquals("oauth_token=&oauth_token_secret=", $result ); |
200
|
|
|
|
201
|
|
|
|
202
|
|
|
//test with attributes set |
203
|
|
|
$oauthToken->token = "toekn"; |
204
|
|
|
$oauthToken->secret = "secret"; |
205
|
|
|
$result = $oauthToken->queryString(); |
206
|
|
|
$this->assertEquals("oauth_token=toekn&oauth_token_secret=secret", $result ); |
207
|
|
|
|
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
public function testcleanup() |
211
|
|
|
{ |
212
|
|
|
|
213
|
|
|
//execute the method and test if it works and does not throws an exception. |
214
|
|
|
try { |
215
|
|
|
OAuthToken::cleanup(); |
216
|
|
|
$this->assertTrue(true); |
217
|
|
|
} |
218
|
|
|
catch (Exception $e) { |
219
|
|
|
$this->fail(); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
public function testcheckNonce() |
225
|
|
|
{ |
226
|
|
|
$result = OAuthToken::checkNonce("test", "test", 123); |
227
|
|
|
$this->assertEquals(0, $result ); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
|
231
|
|
|
public function testdeleteByConsumer() |
232
|
|
|
{ |
233
|
|
|
//execute the method and test if it works and does not throws an exception. |
234
|
|
|
try { |
235
|
|
|
OAuthToken::deleteByConsumer("1"); |
236
|
|
|
$this->assertTrue(true); |
237
|
|
|
} |
238
|
|
|
catch (Exception $e) { |
239
|
|
|
$this->fail(); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
|
245
|
|
|
public function testdeleteByUser() |
246
|
|
|
{ |
247
|
|
|
//execute the method and test if it works and does not throws an exception. |
248
|
|
|
try { |
249
|
|
|
OAuthToken::deleteByUser("1"); |
250
|
|
|
$this->assertTrue(true); |
251
|
|
|
} |
252
|
|
|
catch (Exception $e) { |
253
|
|
|
$this->fail(); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
|
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
public function testdisplayDateFromTs() |
260
|
|
|
{ |
261
|
|
|
//test with empty array |
262
|
|
|
$result = displayDateFromTs(array(''=>''), 'timestamp'); |
|
|
|
|
263
|
|
|
$this->assertEquals('', $result); |
264
|
|
|
|
265
|
|
|
|
266
|
|
|
//test with a valid array |
267
|
|
|
$result = displayDateFromTs(array('TIMESTAMP'=>'1272508903'), 'timestamp'); |
|
|
|
|
268
|
|
|
$this->assertEquals('04/29/2010 02:41', $result); |
269
|
|
|
|
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
|
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
|
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.