This repository has been archived by the owner on Nov 26, 2017. It is now read-only.
/
JGoogleDataPlusPeopleTest.php
240 lines (207 loc) · 5.51 KB
/
JGoogleDataPlusPeopleTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<?php
/**
* @package Joomla.UnitTest
*
* @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/
/**
* Test class for JGoogleDataPlusPeople.
*
* @package Joomla.UnitTest
* @subpackage Google
* @since 12.3
*/
class JGoogleDataPlusPeopleTest extends TestCase
{
/**
* @var JRegistry Options for the JOAuth2Client object.
*/
protected $options;
/**
* @var JHttp Mock client object.
*/
protected $http;
/**
* @var JInput The input object to use in retrieving GET/POST data.
*/
protected $input;
/**
* @var JOAuth2Client The OAuth client for sending requests to Google.
*/
protected $oauth;
/**
* @var JGoogleAuthOauth2 The Google OAuth client for sending requests.
*/
protected $auth;
/**
* @var JGoogleDataPlusPeople Object under test.
*/
protected $object;
/**
* @var string Sample JSON string.
* @since 12.3
*/
protected $sampleString = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
/**
* @var string Sample JSON error message.
* @since 12.3
*/
protected $errorString = '{"error": {"message": "Generic Error."}}';
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*
* @access protected
* @return void
*/
protected function setUp()
{
$_SERVER['HTTP_HOST'] = 'mydomain.com';
$_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0';
$_SERVER['REQUEST_URI'] = '/index.php';
$_SERVER['SCRIPT_NAME'] = '/index.php';
$this->options = new JRegistry;
$this->http = $this->getMock('JHttp', array('head', 'get', 'delete', 'trace', 'post', 'put', 'patch'), array($this->options));
$this->input = new JInput;
$this->oauth = new JOAuth2Client($this->options, $this->http, $this->input);
$this->auth = new JGoogleAuthOauth2($this->options, $this->oauth);
$this->object = new JGoogleDataPlusPeople($this->options, $this->auth);
$this->object->setOption('clientid', '01234567891011.apps.googleusercontent.com');
$this->object->setOption('clientsecret', 'jeDs8rKw_jDJW8MMf-ff8ejs');
$this->object->setOption('redirecturi', 'http://localhost/oauth');
$token['access_token'] = 'accessvalue';
$token['refresh_token'] = 'refreshvalue';
$token['created'] = time() - 1800;
$token['expires_in'] = 3600;
$this->oauth->setToken($token);
}
/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*
* @access protected
* @return void
*/
protected function tearDown()
{
}
/**
* Tests the auth method
*
* @group JGoogle
* @return void
*/
public function testAuth()
{
$this->assertEquals($this->auth->authenticate(), $this->object->authenticate());
}
/**
* Tests the isauth method
*
* @group JGoogle
* @return void
*/
public function testIsAuth()
{
$this->assertEquals($this->auth->isAuthenticated(), $this->object->isAuthenticated());
}
/**
* Tests the getPeople method
*
* @return void
*
* @since 12.3
*/
public function testGetPeople()
{
$id = '124346363456';
$fields = 'aboutMe,birthday';
$returnData = new stdClass;
$returnData->code = 200;
$returnData->body = $this->sampleString;
$url = 'people/' . $id . '?fields=' . $fields;
$this->http->expects($this->once())
->method('get')
->with($url)
->will($this->returnValue($returnData));
$this->assertThat(
$this->object->getPeople($id, $fields),
$this->equalTo(json_decode($this->sampleString, true))
);
// Test return false.
$this->oauth->setToken(null);
$this->assertThat(
$this->object->getPeople($id, $fields),
$this->equalTo(false)
);
}
/**
* Tests the search method
*
* @return void
*
* @since 12.3
*/
public function testSearch()
{
$query = 'test search';
$fields = 'aboutMe,birthday';
$language = 'en-GB';
$max = 5;
$token = 'EAoaAA';
$returnData = new stdClass;
$returnData->code = 200;
$returnData->body = $this->sampleString;
$url = 'people?query=' . urlencode($query) . '&fields=' . $fields . '&language=' . $language .
'&maxResults=' . $max . '&pageToken=' . $token;
$this->http->expects($this->once())
->method('get')
->with($url)
->will($this->returnValue($returnData));
$this->assertThat(
$this->object->search($query, $fields, $language, $max, $token),
$this->equalTo(json_decode($this->sampleString, true))
);
// Test return false.
$this->oauth->setToken(null);
$this->assertThat(
$this->object->search($query),
$this->equalTo(false)
);
}
/**
* Tests the listByActivity method
*
* @return void
*
* @since 12.3
*/
public function testListByActivity()
{
$activityId = 'z12ezrmamsvydrgsy221ypew2qrkt1ja404';
$collection = 'plusoners';
$fields = 'aboutMe,birthday';
$max = 5;
$token = 'EAoaAA';
$returnData = new stdClass;
$returnData->code = 200;
$returnData->body = $this->sampleString;
$url = 'activities/' . $activityId . '/people/' . $collection . '?fields=' . $fields .
'&maxResults=' . $max . '&pageToken=' . $token;
$this->http->expects($this->once())
->method('get')
->with($url)
->will($this->returnValue($returnData));
$this->assertThat(
$this->object->listByActivity($activityId, $collection, $fields, $max, $token),
$this->equalTo(json_decode($this->sampleString, true))
);
// Test return false.
$this->oauth->setToken(null);
$this->assertThat(
$this->object->listByActivity($activityId, $collection),
$this->equalTo(false)
);
}
}