PageRenderTime 21ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/suites/unit/joomla/google/JGoogleDataPicasaAlbumTest.php

https://github.com/Hackwar/joomla-platform
PHP | 526 lines | 236 code | 55 blank | 235 comment | 0 complexity | e5c6507a97c930fa504915f2ddf4d1ff MD5 | raw file
  1. <?php
  2. /**
  3. * @package Joomla.UnitTest
  4. *
  5. * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
  6. * @license GNU General Public License version 2 or later; see LICENSE
  7. */
  8. /**
  9. * Test class for JGoogleDataPicasa.
  10. *
  11. * @package Joomla.UnitTest
  12. * @subpackage Google
  13. * @since 12.3
  14. */
  15. class JGoogleDataPicasaAlbumTest extends TestCase
  16. {
  17. /**
  18. * @var JRegistry Options for the JOAuth2Client object.
  19. */
  20. protected $options;
  21. /**
  22. * @var JHttp Mock client object.
  23. */
  24. protected $http;
  25. /**
  26. * @var JInput The input object to use in retrieving GET/POST data.
  27. */
  28. protected $input;
  29. /**
  30. * @var JOAuth2Client The OAuth client for sending requests to Google.
  31. */
  32. protected $oauth;
  33. /**
  34. * @var JGoogleAuthOauth2 The Google OAuth client for sending requests.
  35. */
  36. protected $auth;
  37. /**
  38. * @var string The XML data for the album.
  39. */
  40. protected $xml;
  41. /**
  42. * @var JGoogleDataPicasaAlbum Object under test.
  43. */
  44. protected $object;
  45. /**
  46. * Sets up the fixture, for example, opens a network connection.
  47. * This method is called before a test is executed.
  48. *
  49. * @access protected
  50. * @return void
  51. */
  52. protected function setUp()
  53. {
  54. $_SERVER['HTTP_HOST'] = 'mydomain.com';
  55. $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0';
  56. $_SERVER['REQUEST_URI'] = '/index.php';
  57. $_SERVER['SCRIPT_NAME'] = '/index.php';
  58. $this->options = new JRegistry;
  59. $this->http = $this->getMock('JHttp', array('head', 'get', 'delete', 'trace', 'post', 'put', 'patch'), array($this->options));
  60. $this->input = new JInput;
  61. $this->oauth = new JOAuth2Client($this->options, $this->http, $this->input);
  62. $this->auth = new JGoogleAuthOauth2($this->options, $this->oauth);
  63. $this->xml = new SimpleXMLElement(JFile::read(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'album.txt'));
  64. $this->object = new JGoogleDataPicasaAlbum($this->xml, $this->options, $this->auth);
  65. $this->object->setOption('clientid', '01234567891011.apps.googleusercontent.com');
  66. $this->object->setOption('clientsecret', 'jeDs8rKw_jDJW8MMf-ff8ejs');
  67. $this->object->setOption('redirecturi', 'http://localhost/oauth');
  68. $token['access_token'] = 'accessvalue';
  69. $token['refresh_token'] = 'refreshvalue';
  70. $token['created'] = time() - 1800;
  71. $token['expires_in'] = 3600;
  72. $this->oauth->setToken($token);
  73. }
  74. /**
  75. * Tears down the fixture, for example, closes a network connection.
  76. * This method is called after a test is executed.
  77. *
  78. * @access protected
  79. * @return void
  80. */
  81. protected function tearDown()
  82. {
  83. }
  84. /**
  85. * Tests the auth method
  86. *
  87. * @group JGoogle
  88. * @return void
  89. */
  90. public function testAuth()
  91. {
  92. $this->assertEquals($this->auth->authenticate(), $this->object->authenticate());
  93. }
  94. /**
  95. * Tests the isauth method
  96. *
  97. * @group JGoogle
  98. * @return void
  99. */
  100. public function testIsAuth()
  101. {
  102. $this->assertEquals($this->auth->isAuthenticated(), $this->object->isAuthenticated());
  103. }
  104. /**
  105. * Tests the delete method
  106. *
  107. * @group JGoogle
  108. * @return void
  109. */
  110. public function testDelete()
  111. {
  112. $this->http->expects($this->once())->method('delete')->will($this->returnCallback('emptyPicasaCallback'));
  113. $result = $this->object->delete();
  114. $this->assertTrue($result);
  115. }
  116. /**
  117. * Tests the getLink method
  118. *
  119. * @group JGoogle
  120. * @return void
  121. */
  122. public function testGetLink()
  123. {
  124. $link = $this->object->getLink();
  125. $this->assertEquals($link, 'https://picasaweb.google.com/data/entry/api/user/12345678901234567890/albumid/0123456789012345678');
  126. $link = $this->object->getLink('self');
  127. $this->assertEquals($link, 'https://picasaweb.google.com/data/entry/api/user/12345678901234567890/albumid/0123456789012345678');
  128. $link = $this->object->getLink('nothing');
  129. $this->assertFalse($link);
  130. }
  131. /**
  132. * Tests the getTitle method
  133. *
  134. * @group JGoogle
  135. * @return void
  136. */
  137. public function testGetTitle()
  138. {
  139. $title = $this->object->getTitle();
  140. $this->assertEquals($title, 'Album 2');
  141. }
  142. /**
  143. * Tests the getSummary method
  144. *
  145. * @group JGoogle
  146. * @return void
  147. */
  148. public function testGetSummary()
  149. {
  150. $summary = $this->object->getSummary();
  151. $this->assertEquals($summary, 'Summary');
  152. }
  153. /**
  154. * Tests the getLocation method
  155. *
  156. * @group JGoogle
  157. * @return void
  158. */
  159. public function testGetLocation()
  160. {
  161. $location = $this->object->getLocation();
  162. $this->assertEquals($location, 'California');
  163. }
  164. /**
  165. * Tests the getAccess method
  166. *
  167. * @group JGoogle
  168. * @return void
  169. */
  170. public function testGetAccess()
  171. {
  172. $access = $this->object->getAccess();
  173. $this->assertEquals($access, 'protected');
  174. }
  175. /**
  176. * Tests the getTime method
  177. *
  178. * @group JGoogle
  179. * @return void
  180. */
  181. public function testGetTime()
  182. {
  183. $time = $this->object->getTime();
  184. $this->assertEquals($time, 1293843600);
  185. }
  186. /**
  187. * Tests the setTitle method
  188. *
  189. * @group JGoogle
  190. * @return void
  191. */
  192. public function testSetTitle()
  193. {
  194. $title = $this->object->setTitle('New Title')->getTitle();
  195. $this->assertEquals($title, 'New Title');
  196. }
  197. /**
  198. * Tests the setSummary method
  199. *
  200. * @group JGoogle
  201. * @return void
  202. */
  203. public function testSetSummary()
  204. {
  205. $summary = $this->object->setSummary('New Summary')->getSummary();
  206. $this->assertEquals($summary, 'New Summary');
  207. }
  208. /**
  209. * Tests the setLocation method
  210. *
  211. * @group JGoogle
  212. * @return void
  213. */
  214. public function testSetLocation()
  215. {
  216. $location = $this->object->setLocation('San Francisco')->getLocation();
  217. $this->assertEquals($location, 'San Francisco');
  218. }
  219. /**
  220. * Tests the setAccess method
  221. *
  222. * @group JGoogle
  223. * @return void
  224. */
  225. public function testSetAccess()
  226. {
  227. $access = $this->object->setAccess('public')->getAccess();
  228. $this->assertEquals($access, 'public');
  229. }
  230. /**
  231. * Tests the setTime method
  232. *
  233. * @group JGoogle
  234. * @return void
  235. */
  236. public function testSetTime()
  237. {
  238. $time = $this->object->setTime(1293843600.001)->getTime();
  239. $this->assertEquals($time, 1293843600.001);
  240. }
  241. /**
  242. * Tests the save method
  243. *
  244. * @group JGoogle
  245. * @return void
  246. */
  247. public function testSave()
  248. {
  249. $this->http->expects($this->exactly(2))->method('put')->will($this->returnCallback('dataPicasaAlbumCallback'));
  250. $this->object->setTitle('New Title');
  251. $this->object->save();
  252. $this->object->save(true);
  253. }
  254. /**
  255. * Tests the refresh method
  256. *
  257. * @group JGoogle
  258. * @return void
  259. */
  260. public function testRefresh()
  261. {
  262. $this->http->expects($this->once())->method('get')->will($this->returnCallback('picasaAlbumCallback'));
  263. $result = $this->object->refresh();
  264. $this->assertEquals(get_class($result), 'JGoogleDataPicasaAlbum');
  265. }
  266. /**
  267. * Tests the listPhotos method
  268. *
  269. * @group JGoogle
  270. * @return void
  271. */
  272. public function testListPhotos()
  273. {
  274. $this->http->expects($this->once())->method('get')->will($this->returnCallback('picasaPhotolistCallback'));
  275. $results = $this->object->listPhotos();
  276. $this->assertEquals(count($results), 2);
  277. $i = 1;
  278. foreach ($results as $result)
  279. {
  280. $this->assertEquals(get_class($result), 'JGoogleDataPicasaPhoto');
  281. $this->assertEquals($result->getTitle(), 'Photo' . $i . '.jpg');
  282. $i++;
  283. }
  284. }
  285. /**
  286. * Tests the listPhotos method with wrong XML
  287. *
  288. * @group JGoogle
  289. * @expectedException UnexpectedValueException
  290. * @return void
  291. */
  292. public function testListPhotosException()
  293. {
  294. $this->http->expects($this->once())->method('get')->will($this->returnCallback('picasaBadXmlCallback'));
  295. $this->object->listPhotos();
  296. }
  297. /**
  298. * Tests the upload method
  299. *
  300. * @group JGoogle
  301. * @return void
  302. */
  303. public function testUpload()
  304. {
  305. $this->http->expects($this->exactly(4))->method('post')->will($this->returnCallback('dataPicasaUploadCallback'));
  306. $result = $this->object->upload(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'logo.png');
  307. $this->assertEquals(get_class($result), 'JGoogleDataPicasaPhoto');
  308. $result = $this->object->upload(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'logo.gif');
  309. $this->assertEquals(get_class($result), 'JGoogleDataPicasaPhoto');
  310. $result = $this->object->upload(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'logo.jpg');
  311. $this->assertEquals(get_class($result), 'JGoogleDataPicasaPhoto');
  312. $result = $this->object->upload(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'logo.bmp');
  313. $this->assertEquals(get_class($result), 'JGoogleDataPicasaPhoto');
  314. }
  315. /**
  316. * Tests the upload method with an unknown file type
  317. *
  318. * @group JGoogle
  319. * @expectedException RuntimeException
  320. * @return void
  321. */
  322. public function testUploadUnknown()
  323. {
  324. $result = $this->object->upload(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'photo.txt');
  325. }
  326. /**
  327. * Tests the upload method with an invalid file
  328. *
  329. * @group JGoogle
  330. * @expectedException PHPUnit_Framework_Error_Warning
  331. * @return void
  332. */
  333. public function testUploadFake()
  334. {
  335. $result = $this->object->upload(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'fakephoto.png');
  336. }
  337. /**
  338. * Tests the setOption method
  339. *
  340. * @group JGoogle
  341. * @return void
  342. */
  343. public function testSetOption()
  344. {
  345. $this->object->setOption('key', 'value');
  346. $this->assertThat(
  347. $this->options->get('key'),
  348. $this->equalTo('value')
  349. );
  350. }
  351. /**
  352. * Tests the getOption method
  353. *
  354. * @group JGoogle
  355. * @return void
  356. */
  357. public function testGetOption()
  358. {
  359. $this->options->set('key', 'value');
  360. $this->assertThat(
  361. $this->object->getOption('key'),
  362. $this->equalTo('value')
  363. );
  364. }
  365. /**
  366. * Tests that all functions properly return false
  367. *
  368. * @group JGoogle
  369. * @return void
  370. */
  371. public function testFalse()
  372. {
  373. $this->oauth->setToken(false);
  374. $functions['delete'] = array();
  375. $functions['save'] = array();
  376. $functions['refresh'] = array();
  377. $functions['listPhotos'] = array();
  378. $functions['upload'] = array(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'logo.png');
  379. foreach ($functions as $function => $params)
  380. {
  381. $this->assertFalse(call_user_func_array(array($this->object, $function), $params));
  382. }
  383. }
  384. /**
  385. * Tests that all functions properly return Exceptions
  386. *
  387. * @group JGoogle
  388. * @return void
  389. */
  390. public function testExceptions()
  391. {
  392. $this->http->expects($this->atLeastOnce())->method('get')->will($this->returnCallback('picasaExceptionCallback'));
  393. $this->http->expects($this->atLeastOnce())->method('delete')->will($this->returnCallback('picasaExceptionCallback'));
  394. $this->http->expects($this->atLeastOnce())->method('post')->will($this->returnCallback('picasaDataExceptionCallback'));
  395. $this->http->expects($this->atLeastOnce())->method('put')->will($this->returnCallback('picasaDataExceptionCallback'));
  396. $functions['delete'] = array();
  397. $functions['save'] = array();
  398. $functions['refresh'] = array();
  399. $functions['listPhotos'] = array();
  400. $functions['upload'] = array(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'logo.png');
  401. foreach ($functions as $function => $params)
  402. {
  403. $exception = false;
  404. try
  405. {
  406. call_user_func_array(array($this->object, $function), $params);
  407. }
  408. catch (UnexpectedValueException $e)
  409. {
  410. $exception = true;
  411. $this->assertEquals($e->getMessage(), 'Unexpected data received from Google: `BADDATA`.');
  412. }
  413. $this->assertTrue($exception);
  414. }
  415. }
  416. }
  417. /**
  418. * Dummy method
  419. *
  420. * @param string $url Path to the resource.
  421. * @param array $headers An array of name-value pairs to include in the header of the request.
  422. * @param integer $timeout Read timeout in seconds.
  423. *
  424. * @return JHttpResponse
  425. *
  426. * @since 12.3
  427. */
  428. function emptyPicasaCallback($url, array $headers = null, $timeout = null)
  429. {
  430. $response->code = 200;
  431. $response->headers = array('Content-Type' => 'application/atom+xml');
  432. $response->body = '';
  433. return $response;
  434. }
  435. /**
  436. * Dummy method
  437. *
  438. * @param string $url Path to the resource.
  439. * @param array $headers An array of name-value pairs to include in the header of the request.
  440. * @param integer $timeout Read timeout in seconds.
  441. *
  442. * @return JHttpResponse
  443. *
  444. * @since 12.3
  445. */
  446. function picasaPhotolistCallback($url, array $headers = null, $timeout = null)
  447. {
  448. $response->code = 200;
  449. $response->headers = array('Content-Type' => 'application/atom+xml');
  450. $response->body = JFile::read(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'photolist.txt');
  451. return $response;
  452. }
  453. /**
  454. * Dummy method
  455. *
  456. * @param string $url Path to the resource.
  457. * @param mixed $data Either an associative array or a string to be sent with the request.
  458. * @param array $headers An array of name-value pairs to include in the header of the request.
  459. * @param integer $timeout Read timeout in seconds.
  460. *
  461. * @return JHttpResponse
  462. *
  463. * @since 12.3
  464. */
  465. function dataPicasaUploadCallback($url, $data, array $headers = null, $timeout = null)
  466. {
  467. $response->code = 200;
  468. $response->headers = array('Content-Type' => 'application/atom+xml');
  469. $response->body = JFile::read(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'photo.txt');
  470. return $response;
  471. }