/tests/unit/suites/libraries/joomla/facebook/JFacebookNoteTest.php

https://github.com/dextercowley/joomla-cms · PHP · 467 lines · 244 code · 70 blank · 153 comment · 0 complexity · 3b756130c1b9e47c8570606d63d76b1d MD5 · raw file

  1. <?php
  2. /**
  3. * @package Joomla.UnitTest
  4. * @subpackage Facebook
  5. *
  6. * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. /**
  10. * Test class for JFacebookNote.
  11. *
  12. * @package Joomla.UnitTest
  13. * @subpackage Facebook
  14. * @since 13.1
  15. */
  16. class JFacebookNoteTest extends TestCase
  17. {
  18. /**
  19. * @var JRegistry Options for the Facebook object.
  20. * @since 13.1
  21. */
  22. protected $options;
  23. /**
  24. * @var JHttp Mock client object.
  25. * @since 13.1
  26. */
  27. protected $client;
  28. /**
  29. * @var JFacebookNote Object under test.
  30. * @since 13.1
  31. */
  32. protected $object;
  33. /**
  34. * @var JFacebookOauth Facebook OAuth 2 client
  35. * @since 13.1
  36. */
  37. protected $oauth;
  38. /**
  39. * @var string Sample JSON string.
  40. * @since 13.1
  41. */
  42. protected $sampleString = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
  43. /**
  44. * @var string Sample JSON error message.
  45. * @since 13.1
  46. */
  47. protected $errorString = '{"error": {"message": "Generic Error."}}';
  48. /**
  49. * Sets up the fixture, for example, opens a network connection.
  50. * This method is called before a test is executed.
  51. *
  52. * @return void
  53. *
  54. * @since 13.1
  55. */
  56. protected function setUp()
  57. {
  58. $_SERVER['HTTP_HOST'] = 'example.com';
  59. $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0';
  60. $_SERVER['REQUEST_URI'] = '/index.php';
  61. $_SERVER['SCRIPT_NAME'] = '/index.php';
  62. $app_id = "app_id";
  63. $app_secret = "app_secret";
  64. $my_url = "http://localhost/gsoc/joomla-platform/facebook_test.php";
  65. $access_token = array(
  66. 'access_token' => 'token',
  67. 'expires' => '51837673', 'created' => '2443672521');
  68. $this->options = new JRegistry;
  69. $this->client = $this->getMock('JHttp', array('get', 'post', 'delete', 'put'));
  70. $this->input = new JInput;
  71. $this->oauth = new JFacebookOauth($this->options, $this->client, $this->input);
  72. $this->oauth->setToken($access_token);
  73. $this->object = new JFacebookNote($this->options, $this->client, $this->oauth);
  74. $this->options->set('clientid', $app_id);
  75. $this->options->set('clientsecret', $app_secret);
  76. $this->options->set('redirecturi', $my_url);
  77. $this->options->set('sendheaders', true);
  78. $this->options->set('authmethod', 'get');
  79. parent::setUp();
  80. }
  81. /**
  82. * Tests the getNote method
  83. *
  84. * @return void
  85. *
  86. * @since 13.1
  87. */
  88. public function testGetNote()
  89. {
  90. $token = $this->oauth->getToken();
  91. $note = '124346363456';
  92. $returnData = new stdClass;
  93. $returnData->code = 200;
  94. $returnData->body = $this->sampleString;
  95. $this->client->expects($this->once())
  96. ->method('get')
  97. ->with($note . '?access_token=' . $token['access_token'])
  98. ->will($this->returnValue($returnData));
  99. $this->assertThat(
  100. $this->object->getNote($note),
  101. $this->equalTo(json_decode($this->sampleString))
  102. );
  103. }
  104. /**
  105. * Tests the getNote method - failure
  106. *
  107. * @return void
  108. *
  109. * @since 13.1
  110. * @expectedException RuntimeException
  111. */
  112. public function testGetNoteFailure()
  113. {
  114. $token = $this->oauth->getToken();
  115. $note = '124346363456';
  116. $returnData = new stdClass;
  117. $returnData->code = 401;
  118. $returnData->body = $this->errorString;
  119. $this->client->expects($this->once())
  120. ->method('get')
  121. ->with($note . '?access_token=' . $token['access_token'])
  122. ->will($this->returnValue($returnData));
  123. $this->object->getNote($note);
  124. }
  125. /**
  126. * Tests the getComments method
  127. *
  128. * @return void
  129. *
  130. * @since 13.1
  131. */
  132. public function testGetComments()
  133. {
  134. $token = $this->oauth->getToken();
  135. $note = '124346363456';
  136. $returnData = new stdClass;
  137. $returnData->code = 200;
  138. $returnData->body = $this->sampleString;
  139. $this->client->expects($this->once())
  140. ->method('get')
  141. ->with($note . '/comments?access_token=' . $token['access_token'])
  142. ->will($this->returnValue($returnData));
  143. $this->assertThat(
  144. $this->object->getComments($note),
  145. $this->equalTo(json_decode($this->sampleString))
  146. );
  147. }
  148. /**
  149. * Tests the getComments method - failure
  150. *
  151. * @return void
  152. *
  153. * @since 13.1
  154. * @expectedException RuntimeException
  155. */
  156. public function testGetCommentsFailure()
  157. {
  158. $token = $this->oauth->getToken();
  159. $note = '124346363456';
  160. $returnData = new stdClass;
  161. $returnData->code = 401;
  162. $returnData->body = $this->errorString;
  163. $this->client->expects($this->once())
  164. ->method('get')
  165. ->with($note . '/comments?access_token=' . $token['access_token'])
  166. ->will($this->returnValue($returnData));
  167. $this->object->getComments($note);
  168. }
  169. /**
  170. * Tests the createComment method.
  171. *
  172. * @return void
  173. *
  174. * @since 13.1
  175. */
  176. public function testCreateComment()
  177. {
  178. $token = $this->oauth->getToken();
  179. $note = '124346363456';
  180. $message = 'test message';
  181. // Set POST request parameters.
  182. $data = array();
  183. $data['message'] = $message;
  184. $returnData = new stdClass;
  185. $returnData->code = 200;
  186. $returnData->body = $this->sampleString;
  187. $this->client->expects($this->once())
  188. ->method('post')
  189. ->with($note . '/comments?access_token=' . $token['access_token'], $data)
  190. ->will($this->returnValue($returnData));
  191. $this->assertThat(
  192. $this->object->createComment($note, $message),
  193. $this->equalTo(json_decode($this->sampleString))
  194. );
  195. }
  196. /**
  197. * Tests the createComment method - failure.
  198. *
  199. * @return void
  200. *
  201. * @since 13.1
  202. * @expectedException RuntimeException
  203. */
  204. public function testCreateCommentFailure()
  205. {
  206. $token = $this->oauth->getToken();
  207. $note = '124346363456';
  208. $message = 'test message';
  209. // Set POST request parameters.
  210. $data = array();
  211. $data['message'] = $message;
  212. $returnData = new stdClass;
  213. $returnData->code = 401;
  214. $returnData->body = $this->errorString;
  215. $this->client->expects($this->once())
  216. ->method('post')
  217. ->with($note . '/comments?access_token=' . $token['access_token'], $data)
  218. ->will($this->returnValue($returnData));
  219. $this->object->createComment($note, $message);
  220. }
  221. /**
  222. * Tests the deleteComment method.
  223. *
  224. * @return void
  225. *
  226. * @since 13.1
  227. */
  228. public function testDeleteComment()
  229. {
  230. $token = $this->oauth->getToken();
  231. $comment = '5148941614_12343468';
  232. $returnData = new stdClass;
  233. $returnData->code = 200;
  234. $returnData->body = true;
  235. $this->client->expects($this->once())
  236. ->method('delete')
  237. ->with($comment . '?access_token=' . $token['access_token'])
  238. ->will($this->returnValue($returnData));
  239. $this->assertThat(
  240. $this->object->deleteComment($comment),
  241. $this->equalTo(true)
  242. );
  243. }
  244. /**
  245. * Tests the deleteComment method - failure.
  246. *
  247. * @return void
  248. *
  249. * @since 13.1
  250. * @expectedException RuntimeException
  251. */
  252. public function testDeleteCommentFailure()
  253. {
  254. $token = $this->oauth->getToken();
  255. $comment = '5148941614_12343468';
  256. $returnData = new stdClass;
  257. $returnData->code = 401;
  258. $returnData->body = $this->errorString;
  259. $this->client->expects($this->once())
  260. ->method('delete')
  261. ->with($comment . '?access_token=' . $token['access_token'])
  262. ->will($this->returnValue($returnData));
  263. $this->object->deleteComment($comment);
  264. }
  265. /**
  266. * Tests the getLikes method
  267. *
  268. * @return void
  269. *
  270. * @since 13.1
  271. */
  272. public function testGetLikes()
  273. {
  274. $token = $this->oauth->getToken();
  275. $note = '124346363456';
  276. $returnData = new stdClass;
  277. $returnData->code = 200;
  278. $returnData->body = $this->sampleString;
  279. $this->client->expects($this->once())
  280. ->method('get')
  281. ->with($note . '/likes?access_token=' . $token['access_token'])
  282. ->will($this->returnValue($returnData));
  283. $this->assertThat(
  284. $this->object->getLikes($note),
  285. $this->equalTo(json_decode($this->sampleString))
  286. );
  287. }
  288. /**
  289. * Tests the getLikes method - failure
  290. *
  291. * @return void
  292. *
  293. * @since 13.1
  294. * @expectedException RuntimeException
  295. */
  296. public function testGetLikesFailure()
  297. {
  298. $token = $this->oauth->getToken();
  299. $note = '124346363456';
  300. $returnData = new stdClass;
  301. $returnData->code = 401;
  302. $returnData->body = $this->errorString;
  303. $this->client->expects($this->once())
  304. ->method('get')
  305. ->with($note . '/likes?access_token=' . $token['access_token'])
  306. ->will($this->returnValue($returnData));
  307. $this->object->getLikes($note);
  308. }
  309. /**
  310. * Tests the createLike method.
  311. *
  312. * @return void
  313. *
  314. * @since 13.1
  315. */
  316. public function testCreateLike()
  317. {
  318. $token = $this->oauth->getToken();
  319. $note = '124346363456';
  320. $returnData = new stdClass;
  321. $returnData->code = 200;
  322. $returnData->body = $this->sampleString;
  323. $this->client->expects($this->once())
  324. ->method('post')
  325. ->with($note . '/likes?access_token=' . $token['access_token'], '')
  326. ->will($this->returnValue($returnData));
  327. $this->assertThat(
  328. $this->object->createLike($note),
  329. $this->equalTo(json_decode($this->sampleString))
  330. );
  331. }
  332. /**
  333. * Tests the createLike method - failure.
  334. *
  335. * @return void
  336. *
  337. * @since 13.1
  338. * @expectedException RuntimeException
  339. */
  340. public function testCreateLikeFailure()
  341. {
  342. $token = $this->oauth->getToken();
  343. $note = '124346363456';
  344. $returnData = new stdClass;
  345. $returnData->code = 401;
  346. $returnData->body = $this->errorString;
  347. $this->client->expects($this->once())
  348. ->method('post')
  349. ->with($note . '/likes?access_token=' . $token['access_token'], '')
  350. ->will($this->returnValue($returnData));
  351. $this->object->createLike($note);
  352. }
  353. /**
  354. * Tests the deleteLike method.
  355. *
  356. * @return void
  357. *
  358. * @since 13.1
  359. */
  360. public function testDeleteLike()
  361. {
  362. $token = $this->oauth->getToken();
  363. $note = '124346363456';
  364. $returnData = new stdClass;
  365. $returnData->code = 200;
  366. $returnData->body = true;
  367. $this->client->expects($this->once())
  368. ->method('delete')
  369. ->with($note . '/likes?access_token=' . $token['access_token'])
  370. ->will($this->returnValue($returnData));
  371. $this->assertThat(
  372. $this->object->deleteLike($note),
  373. $this->equalTo(true)
  374. );
  375. }
  376. /**
  377. * Tests the deleteLike method - failure.
  378. *
  379. * @return void
  380. *
  381. * @since 13.1
  382. * @expectedException RuntimeException
  383. */
  384. public function testDeleteLikeFailure()
  385. {
  386. $token = $this->oauth->getToken();
  387. $note = '124346363456';
  388. $returnData = new stdClass;
  389. $returnData->code = 401;
  390. $returnData->body = $this->errorString;
  391. $this->client->expects($this->once())
  392. ->method('delete')
  393. ->with($note . '/likes?access_token=' . $token['access_token'])
  394. ->will($this->returnValue($returnData));
  395. $this->object->deleteLike($note);
  396. }
  397. }