CallbackTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package UnitTests
  17. * @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. require_once 'PHPUnit/Framework/TestCase.php';
  22. require_once 'Zend/Feed/Pubsubhubbub/Subscriber/Callback.php';
  23. require_once 'Zend/Feed/Pubsubhubbub/Model/Subscription.php';
  24. require_once 'Zend/Db/Table/Rowset/Abstract.php';
  25. /**
  26. * @category Zend
  27. * @package Zend_Feed
  28. * @subpackage UnitTests
  29. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  30. * @license http://framework.zend.com/license/new-bsd New BSD License
  31. */
  32. class Zend_Feed_Pubsubhubbub_Subscriber_CallbackTest extends PHPUnit_Framework_TestCase
  33. {
  34. protected $_originalServer = null;
  35. public function setUp()
  36. {
  37. $this->_callback = new Zend_Feed_Pubsubhubbub_Subscriber_Callback;
  38. $this->_adapter = $this->_getCleanMock(
  39. 'Zend_Db_Adapter_Abstract'
  40. );
  41. $this->_tableGateway = $this->_getCleanMock(
  42. 'Zend_Db_Table_Abstract'
  43. );
  44. $this->_rowset = $this->_getCleanMock(
  45. 'Zend_Db_Table_Rowset_Abstract'
  46. );
  47. $this->_tableGateway->expects($this->any())->method('getAdapter')
  48. ->will($this->returnValue($this->_adapter));
  49. $storage = new Zend_Feed_Pubsubhubbub_Model_Subscription($this->_tableGateway);
  50. $this->_callback->setStorage($storage);
  51. $this->_get = array(
  52. 'hub_mode' => 'subscribe',
  53. 'hub_topic' => 'http://www.example.com/topic',
  54. 'hub_challenge' => 'abc',
  55. 'hub_verify_token' => 'cba',
  56. 'hub_mode' => 'subscribe',
  57. 'hub_lease_seconds' => '1234567'
  58. );
  59. $this->_originalServer = $_SERVER;
  60. $_SERVER['REQUEST_METHOD'] = 'get';
  61. $_SERVER['QUERY_STRING'] = 'xhub.subscription=verifytokenkey';
  62. }
  63. public function tearDown()
  64. {
  65. $_SERVER = $this->_originalServer;
  66. }
  67. public function testCanSetHttpResponseObject()
  68. {
  69. $this->_callback->setHttpResponse(new Zend_Feed_Pubsubhubbub_HttpResponse);
  70. $this->assertTrue($this->_callback->getHttpResponse() instanceof Zend_Feed_Pubsubhubbub_HttpResponse);
  71. }
  72. public function testCanUsesDefaultHttpResponseObject()
  73. {
  74. $this->assertTrue($this->_callback->getHttpResponse() instanceof Zend_Feed_Pubsubhubbub_HttpResponse);
  75. }
  76. public function testThrowsExceptionOnInvalidHttpResponseObjectSet()
  77. {
  78. try {
  79. $this->_callback->setHttpResponse(new stdClass);
  80. $this->fail('Should not fail as an Exception would be raised and caught');
  81. } catch (Zend_Feed_Pubsubhubbub_Exception $e) {}
  82. }
  83. public function testThrowsExceptionIfNonObjectSetAsHttpResponseObject()
  84. {
  85. try {
  86. $this->_callback->setHttpResponse('');
  87. $this->fail('Should not fail as an Exception would be raised and caught');
  88. } catch (Zend_Feed_Pubsubhubbub_Exception $e) {}
  89. }
  90. public function testCanSetSubscriberCount()
  91. {
  92. $this->_callback->setSubscriberCount('10000');
  93. $this->assertEquals(10000, $this->_callback->getSubscriberCount());
  94. }
  95. public function testDefaultSubscriberCountIsOne()
  96. {
  97. $this->assertEquals(1, $this->_callback->getSubscriberCount());
  98. }
  99. public function testThrowsExceptionOnSettingZeroAsSubscriberCount()
  100. {
  101. try {
  102. $this->_callback->setSubscriberCount(0);
  103. $this->fail('Should not fail as an Exception would be raised and caught');
  104. } catch (Zend_Feed_Pubsubhubbub_Exception $e) {}
  105. }
  106. public function testThrowsExceptionOnSettingLessThanZeroAsSubscriberCount()
  107. {
  108. try {
  109. $this->_callback->setSubscriberCount(-1);
  110. $this->fail('Should not fail as an Exception would be raised and caught');
  111. } catch (Zend_Feed_Pubsubhubbub_Exception $e) {}
  112. }
  113. public function testThrowsExceptionOnSettingAnyScalarTypeCastToAZeroOrLessIntegerAsSubscriberCount()
  114. {
  115. try {
  116. $this->_callback->setSubscriberCount('0aa');
  117. $this->fail('Should not fail as an Exception would be raised and caught');
  118. } catch (Zend_Feed_Pubsubhubbub_Exception $e) {}
  119. }
  120. public function testCanSetStorageImplementation()
  121. {
  122. $storage = new Zend_Feed_Pubsubhubbub_Model_ModelAbstract($this->_tableGateway);
  123. $this->_callback->setStorage($storage);
  124. $this->assertThat($this->_callback->getStorage(), $this->identicalTo($storage));
  125. }
  126. public function testValidatesValidHttpGetData()
  127. {
  128. $this->_tableGateway->expects($this->any())
  129. ->method('find')
  130. ->with($this->equalTo('verifytokenkey'))
  131. ->will($this->returnValue($this->_rowset));
  132. $this->_rowset->expects($this->any())
  133. ->method('current')
  134. ->will($this->returnValue(array(
  135. 'verify_token' => hash('sha256', 'cba')
  136. )));
  137. $this->assertTrue($this->_callback->isValidHubVerification($this->_get));
  138. }
  139. public function testReturnsFalseIfHubVerificationNotAGetRequest()
  140. {
  141. $_SERVER['REQUEST_METHOD'] = 'POST';
  142. $this->assertFalse($this->_callback->isValidHubVerification($this->_get));
  143. }
  144. public function testReturnsFalseIfModeMissingFromHttpGetData()
  145. {
  146. unset($this->_get['hub_mode']);
  147. $this->assertFalse($this->_callback->isValidHubVerification($this->_get));
  148. }
  149. public function testReturnsFalseIfTopicMissingFromHttpGetData()
  150. {
  151. unset($this->_get['hub_topic']);
  152. $this->assertFalse($this->_callback->isValidHubVerification($this->_get));
  153. }
  154. public function testReturnsFalseIfChallengeMissingFromHttpGetData()
  155. {
  156. unset($this->_get['hub_challenge']);
  157. $this->assertFalse($this->_callback->isValidHubVerification($this->_get));
  158. }
  159. public function testReturnsFalseIfVerifyTokenMissingFromHttpGetData()
  160. {
  161. unset($this->_get['hub_verify_token']);
  162. $this->assertFalse($this->_callback->isValidHubVerification($this->_get));
  163. }
  164. public function testReturnsTrueIfModeSetAsUnsubscribeFromHttpGetData()
  165. {
  166. $this->_get['hub_mode'] = 'unsubscribe';
  167. $this->_tableGateway->expects($this->any())
  168. ->method('find')
  169. ->with($this->equalTo('verifytokenkey'))
  170. ->will($this->returnValue($this->_rowset));
  171. $this->_rowset->expects($this->any())
  172. ->method('current')
  173. ->will($this->returnValue(array(
  174. 'verify_token' => hash('sha256', 'cba')
  175. )));
  176. $this->assertTrue($this->_callback->isValidHubVerification($this->_get));
  177. }
  178. public function testReturnsFalseIfModeNotRecognisedFromHttpGetData()
  179. {
  180. $this->_get['hub_mode'] = 'abc';
  181. $this->assertFalse($this->_callback->isValidHubVerification($this->_get));
  182. }
  183. public function testReturnsFalseIfLeaseSecondsMissedWhenModeIsSubscribeFromHttpGetData()
  184. {
  185. unset($this->_get['hub_lease_seconds']);
  186. $this->assertFalse($this->_callback->isValidHubVerification($this->_get));
  187. }
  188. public function testReturnsFalseIfHubTopicInvalidFromHttpGetData()
  189. {
  190. $this->_get['hub_topic'] = 'http://';
  191. $this->assertFalse($this->_callback->isValidHubVerification($this->_get));
  192. }
  193. public function testReturnsFalseIfVerifyTokenRecordDoesNotExistForConfirmRequest()
  194. {
  195. //$this->_callback->setStorage(new Zend_Feed_Pubsubhubbub_Subscriber_CallbackTestStorageHasNot);
  196. $this->assertFalse($this->_callback->isValidHubVerification($this->_get));
  197. }
  198. public function testReturnsFalseIfVerifyTokenRecordDoesNotAgreeWithConfirmRequest()
  199. {
  200. //$this->_callback->setStorage(new Zend_Feed_Pubsubhubbub_Subscriber_CallbackTestStorageHasButWrong);
  201. $this->assertFalse($this->_callback->isValidHubVerification($this->_get));
  202. }
  203. public function testRespondsToInvalidConfirmationWith404Response()
  204. {
  205. unset($this->_get['hub_mode']);
  206. $this->_callback->handle($this->_get);
  207. $this->assertTrue($this->_callback->getHttpResponse()->getHttpResponseCode() == 404);
  208. }
  209. public function testRespondsToValidConfirmationWith200Response()
  210. {
  211. $this->_get['hub_mode'] = 'unsubscribe';
  212. $this->_tableGateway->expects($this->any())
  213. ->method('find')
  214. ->with($this->equalTo('verifytokenkey'))
  215. ->will($this->returnValue($this->_rowset));
  216. $rowdata = new stdClass;
  217. $rowdata->id = 'verifytokenkey';
  218. $rowdata->verify_token = hash('sha256', 'cba');
  219. $t = time();
  220. $rowdata->created_time = $t;
  221. $this->_rowset->expects($this->any())
  222. ->method('current')
  223. ->will($this->returnValue($rowdata));
  224. $this->_tableGateway->expects($this->once())
  225. ->method('update')
  226. ->with(
  227. $this->equalTo(array('id'=>'verifytokenkey','verify_token'=>hash('sha256', 'cba'),'created_time'=>$t,'verified'=>'1')),
  228. $this->equalTo('id = \'verifytokenkey\'')
  229. );
  230. $this->_adapter->expects($this->once())
  231. ->method('quoteInto')
  232. ->with($this->equalTo('id = ?'), $this->equalTo('verifytokenkey'))
  233. ->will($this->returnValue('id = \'verifytokenkey\''));
  234. $this->_callback->handle($this->_get);
  235. $this->assertTrue($this->_callback->getHttpResponse()->getHttpResponseCode() == 200);
  236. }
  237. public function testRespondsToValidConfirmationWithBodyContainingHubChallenge()
  238. {
  239. $this->_tableGateway->expects($this->any())
  240. ->method('find')
  241. ->with($this->equalTo('verifytokenkey'))
  242. ->will($this->returnValue($this->_rowset));
  243. $rowdata = new stdClass;
  244. $rowdata->id = 'verifytokenkey';
  245. $rowdata->verify_token = hash('sha256', 'cba');
  246. $t = time();
  247. $rowdata->created_time = $t;
  248. $this->_rowset->expects($this->any())
  249. ->method('current')
  250. ->will($this->returnValue($rowdata));
  251. $this->_tableGateway->expects($this->once())
  252. ->method('update')
  253. ->with(
  254. $this->equalTo(array('id'=>'verifytokenkey','verify_token'=>hash('sha256', 'cba'),'created_time'=>$t,'verified'=>'1')),
  255. $this->equalTo('id = \'verifytokenkey\'')
  256. );
  257. $this->_adapter->expects($this->once())
  258. ->method('quoteInto')
  259. ->with($this->equalTo('id = ?'), $this->equalTo('verifytokenkey'))
  260. ->will($this->returnValue('id = \'verifytokenkey\''));
  261. $this->_callback->handle($this->_get);
  262. $this->assertTrue($this->_callback->getHttpResponse()->getBody() == 'abc');
  263. }
  264. public function testRespondsToValidFeedUpdateRequestWith200Response()
  265. {
  266. $_SERVER['REQUEST_METHOD'] = 'POST';
  267. $_SERVER['REQUEST_URI'] = '/some/path/callback/verifytokenkey';
  268. $_SERVER['CONTENT_TYPE'] = 'application/atom+xml';
  269. $feedXml = file_get_contents(dirname(__FILE__) . '/_files/atom10.xml');
  270. $GLOBALS['HTTP_RAW_POST_DATA'] = $feedXml; // dirty alternative to php://input
  271. $this->_tableGateway->expects($this->any())
  272. ->method('find')
  273. ->with($this->equalTo('verifytokenkey'))
  274. ->will($this->returnValue($this->_rowset));
  275. $rowdata = new stdClass;
  276. $rowdata->id = 'verifytokenkey';
  277. $rowdata->verify_token = hash('sha256', 'cba');
  278. $t = time();
  279. $rowdata->created_time = $t;
  280. $this->_rowset->expects($this->any())
  281. ->method('current')
  282. ->will($this->returnValue($rowdata));
  283. $this->_callback->handle(array());
  284. $this->assertTrue($this->_callback->getHttpResponse()->getHttpResponseCode() == 200);
  285. }
  286. public function testRespondsToInvalidFeedUpdateNotPostWith404Response()
  287. { // yes, this example makes no sense for GET - I know!!!
  288. $_SERVER['REQUEST_METHOD'] = 'GET';
  289. $_SERVER['REQUEST_URI'] = '/some/path/callback/verifytokenkey';
  290. $_SERVER['CONTENT_TYPE'] = 'application/atom+xml';
  291. $feedXml = file_get_contents(dirname(__FILE__) . '/_files/atom10.xml');
  292. $GLOBALS['HTTP_RAW_POST_DATA'] = $feedXml;
  293. $this->_callback->handle(array());
  294. $this->assertTrue($this->_callback->getHttpResponse()->getHttpResponseCode() == 404);
  295. }
  296. public function testRespondsToInvalidFeedUpdateWrongMimeWith404Response()
  297. {
  298. $_SERVER['REQUEST_METHOD'] = 'POST';
  299. $_SERVER['REQUEST_URI'] = '/some/path/callback/verifytokenkey';
  300. $_SERVER['CONTENT_TYPE'] = 'application/kml+xml';
  301. $feedXml = file_get_contents(dirname(__FILE__) . '/_files/atom10.xml');
  302. $GLOBALS['HTTP_RAW_POST_DATA'] = $feedXml;
  303. $this->_callback->handle(array());
  304. $this->assertTrue($this->_callback->getHttpResponse()->getHttpResponseCode() == 404);
  305. }
  306. /**
  307. * As a judgement call, we must respond to any successful request, regardless
  308. * of the wellformedness of any XML payload, by returning a 2xx response code.
  309. * The validation of feeds and their processing must occur outside the Hubbub
  310. * protocol.
  311. */
  312. public function testRespondsToInvalidFeedUpdateWrongFeedTypeForMimeWith200Response()
  313. {
  314. $_SERVER['REQUEST_METHOD'] = 'POST';
  315. $_SERVER['REQUEST_URI'] = '/some/path/callback/verifytokenkey';
  316. $_SERVER['CONTENT_TYPE'] = 'application/rss+xml';
  317. $feedXml = file_get_contents(dirname(__FILE__) . '/_files/atom10.xml');
  318. $GLOBALS['HTTP_RAW_POST_DATA'] = $feedXml;
  319. $this->_tableGateway->expects($this->any())
  320. ->method('find')
  321. ->with($this->equalTo('verifytokenkey'))
  322. ->will($this->returnValue($this->_rowset));
  323. $rowdata = new stdClass;
  324. $rowdata->id = 'verifytokenkey';
  325. $rowdata->verify_token = hash('sha256', 'cba');
  326. $t = time();
  327. $rowdata->created_time = $t;
  328. $this->_rowset->expects($this->any())
  329. ->method('current')
  330. ->will($this->returnValue($rowdata));
  331. $this->_callback->handle(array());
  332. $this->assertTrue($this->_callback->getHttpResponse()->getHttpResponseCode() == 200);
  333. }
  334. public function testRespondsToValidFeedUpdateWithXHubOnBehalfOfHeader()
  335. {
  336. $_SERVER['REQUEST_METHOD'] = 'POST';
  337. $_SERVER['REQUEST_URI'] = '/some/path/callback/verifytokenkey';
  338. $_SERVER['CONTENT_TYPE'] = 'application/atom+xml';
  339. $feedXml = file_get_contents(dirname(__FILE__) . '/_files/atom10.xml');
  340. $GLOBALS['HTTP_RAW_POST_DATA'] = $feedXml;
  341. $this->_tableGateway->expects($this->any())
  342. ->method('find')
  343. ->with($this->equalTo('verifytokenkey'))
  344. ->will($this->returnValue($this->_rowset));
  345. $rowdata = new stdClass;
  346. $rowdata->id = 'verifytokenkey';
  347. $rowdata->verify_token = hash('sha256', 'cba');
  348. $t = time();
  349. $rowdata->created_time = $t;
  350. $this->_rowset->expects($this->any())
  351. ->method('current')
  352. ->will($this->returnValue($rowdata));
  353. $this->_callback->handle(array());
  354. $this->assertTrue($this->_callback->getHttpResponse()->getHeader('X-Hub-On-Behalf-Of') == 1);
  355. }
  356. protected function _getCleanMock($className) {
  357. $class = new ReflectionClass($className);
  358. $methods = $class->getMethods();
  359. $stubMethods = array();
  360. foreach ($methods as $method) {
  361. if ($method->isPublic() || ($method->isProtected()
  362. && $method->isAbstract())) {
  363. $stubMethods[] = $method->getName();
  364. }
  365. }
  366. $mocked = $this->getMock(
  367. $className,
  368. $stubMethods,
  369. array(),
  370. $className . '_PubsubSubscriberMock_' . uniqid(),
  371. false
  372. );
  373. return $mocked;
  374. }
  375. }
  376. /**
  377. * Stubs for storage access
  378. * DEPRECATED
  379. class Zend_Feed_Pubsubhubbub_Subscriber_CallbackTestStorageHas implements Zend_Feed_Pubsubhubbub_Storage_StorageInterface
  380. {
  381. public function setSubscription($key, array $data){}
  382. public function getSubscription($key){
  383. if ($key == 'verifytokenkey') {
  384. return array(
  385. 'id' => 'verifytokenkey',
  386. 'verify_token' => hash('sha256', 'cba')
  387. );
  388. }
  389. }
  390. public function hasSubscription($key){return true;}
  391. public function removeSubscription($key){}
  392. public function cleanup($type){}
  393. }
  394. class Zend_Feed_Pubsubhubbub_Subscriber_CallbackTestStorageHasNot implements Zend_Feed_Pubsubhubbub_Storage_StorageInterface
  395. {
  396. public function setSubscription($key, array $data){}
  397. public function getSubscription($key){}
  398. public function hasSubscription($key){return false;}
  399. public function removeSubscription($key){}
  400. public function cleanup($type){}
  401. }
  402. class Zend_Feed_Pubsubhubbub_Subscriber_CallbackTestStorageHasButWrong implements Zend_Feed_Pubsubhubbub_Storage_StorageInterface
  403. {
  404. public function setSubscription($key, array $data){}
  405. public function getSubscription($key){return 'wrong';}
  406. public function hasSubscription($key){return true;}
  407. public function removeSubscription($key){}
  408. public function cleanup($type){}
  409. }*/