CallbackTest.php 18 KB

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