This repository has been archived by the owner on Nov 26, 2017. It is now read-only.
/
JGoogleDataPicasaPhotoTest.php
457 lines (408 loc) · 10.4 KB
/
JGoogleDataPicasaPhotoTest.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
<?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 JGoogleDataPicasa.
*
* @package Joomla.UnitTest
* @subpackage Google
* @since 12.3
*/
class JGoogleDataPicasaPhotoTest 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 string The XML data for the album.
*/
protected $xml;
/**
* @var JGoogleDataPicasaPhoto Object under test.
*/
protected $object;
/**
* 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()
{
parent::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->xml = new SimpleXMLElement(JFile::read(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'photo.txt'));
$this->object = new JGoogleDataPicasaPhoto($this->xml, $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);
}
/**
* 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 delete method
*
* @group JGoogle
* @return void
*/
public function testDelete()
{
$this->http->expects($this->once())->method('delete')->will($this->returnCallback('emptyPicasaCallback'));
$result = $this->object->delete();
$this->assertTrue($result);
}
/**
* Tests the getLink method
*
* @group JGoogle
* @return void
*/
public function testGetLink()
{
$url = 'https://picasaweb.google.com/data/entry/api/user/12345678901234567890/albumid/0123456789012345678/photoid/12345678901234567890';
$link = $this->object->getLink();
$this->assertEquals($link, $url);
$link = $this->object->getLink('self');
$this->assertEquals($link, $url);
$link = $this->object->getLink('nothing');
$this->assertFalse($link);
}
/**
* Tests the getURL method
*
* @group JGoogle
* @return void
*/
public function testGetURL()
{
$url = $this->object->getURL();
$this->assertEquals($url, 'https://lh3.googleusercontent.com/-VQfLCrQyGuw/UAYBmwBJZ3I/AAAAAAAAF-k/8y_1iBPJcdQ/Photo2.jpg');
}
/**
* Tests the getThumbnails method
*
* @group JGoogle
* @return void
*/
public function testGetThumbnails()
{
$thumbs = $this->object->getThumbnails();
$valid[72] = array('url' => 'https://lh3.googleusercontent.com/sdgfdfgsdgf/werewr/aswertrt/vdfderer/s72/Photo2.jpg', 'w' => 72, 'h' => 54);
$valid[144] = array('url' => 'https://lh3.googleusercontent.com/sdgfdfgsdgf/werewr/aswertrt/vdfderer/s144/Photo2.jpg', 'w' => 144, 'h' => 108);
$valid[288] = array('url' => 'https://lh3.googleusercontent.com/sdgfdfgsdgf/werewr/aswertrt/vdfderer/s288/Photo2.jpg', 'w' => 288, 'h' => 216);
$this->assertEquals($thumbs, $valid);
}
/**
* Tests the getTitle method
*
* @group JGoogle
* @return void
*/
public function testGetTitle()
{
$title = $this->object->getTitle();
$this->assertEquals($title, 'Photo2.jpg');
}
/**
* Tests the getSummary method
*
* @group JGoogle
* @return void
*/
public function testGetSummary()
{
$summary = $this->object->getSummary();
$this->assertEquals($summary, 'Summary');
}
/**
* Tests the getAccess method
*
* @group JGoogle
* @return void
*/
public function testGetAccess()
{
$access = $this->object->getAccess();
$this->assertEquals($access, 'only_you');
}
/**
* Tests the getTime method
*
* @group JGoogle
* @return void
*/
public function testGetTime()
{
$time = $this->object->getTime();
$this->assertEquals($time, 1328140800);
}
/**
* Tests the getSize method
*
* @group JGoogle
* @return void
*/
public function testGetSize()
{
$size = $this->object->getSize();
$this->assertEquals($size, 648818);
}
/**
* Tests the getHeight method
*
* @group JGoogle
* @return void
*/
public function testGetHeight()
{
$height = $this->object->getHeight();
$this->assertEquals($height, 1536);
}
/**
* Tests the getTime method
*
* @group JGoogle
* @return void
*/
public function testGetWidth()
{
$width = $this->object->getWidth();
$this->assertEquals($width, 2048);
}
/**
* Tests the setTitle method
*
* @group JGoogle
* @return void
*/
public function testSetTitle()
{
$title = $this->object->setTitle('New Title')->getTitle();
$this->assertEquals($title, 'New Title');
}
/**
* Tests the setSummary method
*
* @group JGoogle
* @return void
*/
public function testSetSummary()
{
$summary = $this->object->setSummary('New Summary')->getSummary();
$this->assertEquals($summary, 'New Summary');
}
/**
* Tests the setAccess method
*
* @group JGoogle
* @return void
*/
public function testSetAccess()
{
$access = $this->object->setAccess('public')->getAccess();
$this->assertEquals($access, 'public');
}
/**
* Tests the getTime method
*
* @group JGoogle
* @return void
*/
public function testSetTime()
{
$time = $this->object->setTime('FIX')->getTime();
$this->assertEquals($time, 0);
}
/**
* Tests the save method
*
* @group JGoogle
* @return void
*/
public function testSave()
{
$this->http->expects($this->exactly(2))->method('put')->will($this->returnCallback('dataPicasaPhotoCallback'));
$this->object->setTitle('New Title');
$this->object->save();
$this->object->save(true);
}
/**
* Tests the refresh method
*
* @group JGoogle
* @return void
*/
public function testRefresh()
{
$this->http->expects($this->once())->method('get')->will($this->returnCallback('picasaPhotoCallback'));
$result = $this->object->refresh();
$this->assertEquals(get_class($result), 'JGoogleDataPicasaPhoto');
}
/**
* Tests the setOption method
*
* @group JGoogle
* @return void
*/
public function testSetOption()
{
$this->object->setOption('key', 'value');
$this->assertThat(
$this->options->get('key'),
$this->equalTo('value')
);
}
/**
* Tests the getOption method
*
* @group JGoogle
* @return void
*/
public function testGetOption()
{
$this->options->set('key', 'value');
$this->assertThat(
$this->object->getOption('key'),
$this->equalTo('value')
);
}
/**
* Tests that all functions properly return false
*
* @group JGoogle
* @return void
*/
public function testFalse()
{
$this->oauth->setToken(false);
$functions['delete'] = array();
$functions['save'] = array();
$functions['refresh'] = array();
foreach ($functions as $function => $params)
{
$this->assertFalse(call_user_func_array(array($this->object, $function), $params));
}
}
/**
* Tests that all functions properly return Exceptions
*
* @group JGoogle
* @return void
*/
public function testExceptions()
{
$this->http->expects($this->atLeastOnce())->method('get')->will($this->returnCallback('picasaExceptionCallback'));
$this->http->expects($this->atLeastOnce())->method('delete')->will($this->returnCallback('picasaExceptionCallback'));
$this->http->expects($this->atLeastOnce())->method('put')->will($this->returnCallback('picasaDataExceptionCallback'));
$functions['delete'] = array();
$functions['save'] = array();
$functions['refresh'] = array();
foreach ($functions as $function => $params)
{
$exception = false;
try
{
call_user_func_array(array($this->object, $function), $params);
}
catch (UnexpectedValueException $e)
{
$exception = true;
$this->assertEquals($e->getMessage(), 'Unexpected data received from Google: `BADDATA`.');
}
$this->assertTrue($exception);
}
}
}
/**
* Dummy method
*
* @param string $url Path to the resource.
* @param array $headers An array of name-value pairs to include in the header of the request.
* @param integer $timeout Read timeout in seconds.
*
* @return JHttpResponse
*
* @since 12.3
*/
function picasaPhotoCallback($url, array $headers = null, $timeout = null)
{
$response = new stdClass;
$response->code = 200;
$response->headers = array('Content-Type' => 'application/atom+xml');
$response->body = JFile::read(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'photo.txt');
return $response;
}
/**
* Dummy method
*
* @param string $url Path to the resource.
* @param mixed $data Either an associative array or a string to be sent with the request.
* @param array $headers An array of name-value pairs to include in the header of the request.
* @param integer $timeout Read timeout in seconds.
*
* @return JHttpResponse
*
* @since 12.3
*/
function dataPicasaPhotoCallback($url, $data, array $headers = null, $timeout = null)
{
PHPUnit_Framework_TestCase::assertContains('<title>New Title</title>', $data);
$response = new stdClass;
$response->code = 200;
$response->headers = array('Content-Type' => 'application/atom+xml');
$response->body = $data;
return $response;
}