CallbackTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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 = time();
  221. $rowdata->created_time = $t;
  222. $this->_rowset->expects($this->any())
  223. ->method('current')
  224. ->will($this->returnValue($rowdata));
  225. $this->_tableGateway->expects($this->once())
  226. ->method('update')
  227. ->with(
  228. $this->equalTo(array('id'=>'verifytokenkey','verify_token'=>hash('sha256', 'cba'),'created_time'=>$t,'verified'=>'1')),
  229. $this->equalTo('id = \'verifytokenkey\'')
  230. );
  231. $this->_adapter->expects($this->once())
  232. ->method('quoteInto')
  233. ->with($this->equalTo('id = ?'), $this->equalTo('verifytokenkey'))
  234. ->will($this->returnValue('id = \'verifytokenkey\''));
  235. $this->_callback->handle($this->_get);
  236. $this->assertTrue($this->_callback->getHttpResponse()->getHttpResponseCode() == 200);
  237. }
  238. public function testRespondsToValidConfirmationWithBodyContainingHubChallenge()
  239. {
  240. $this->_tableGateway->expects($this->any())
  241. ->method('find')
  242. ->with($this->equalTo('verifytokenkey'))
  243. ->will($this->returnValue($this->_rowset));
  244. $rowdata = new stdClass;
  245. $rowdata->id = 'verifytokenkey';
  246. $rowdata->verify_token = hash('sha256', 'cba');
  247. $t = time();
  248. $rowdata->created_time = $t;
  249. $this->_rowset->expects($this->any())
  250. ->method('current')
  251. ->will($this->returnValue($rowdata));
  252. $this->_tableGateway->expects($this->once())
  253. ->method('update')
  254. ->with(
  255. $this->equalTo(array('id'=>'verifytokenkey','verify_token'=>hash('sha256', 'cba'),'created_time'=>$t,'verified'=>'1')),
  256. $this->equalTo('id = \'verifytokenkey\'')
  257. );
  258. $this->_adapter->expects($this->once())
  259. ->method('quoteInto')
  260. ->with($this->equalTo('id = ?'), $this->equalTo('verifytokenkey'))
  261. ->will($this->returnValue('id = \'verifytokenkey\''));
  262. $this->_callback->handle($this->_get);
  263. $this->assertTrue($this->_callback->getHttpResponse()->getBody() == 'abc');
  264. }
  265. public function testRespondsToValidFeedUpdateRequestWith200Response()
  266. {
  267. $_SERVER['REQUEST_METHOD'] = 'POST';
  268. $_SERVER['REQUEST_URI'] = '/some/path/callback/verifytokenkey';
  269. $_SERVER['CONTENT_TYPE'] = 'application/atom+xml';
  270. $feedXml = file_get_contents(dirname(__FILE__) . '/_files/atom10.xml');
  271. $GLOBALS['HTTP_RAW_POST_DATA'] = $feedXml; // dirty alternative to php://input
  272. $this->_tableGateway->expects($this->any())
  273. ->method('find')
  274. ->with($this->equalTo('verifytokenkey'))
  275. ->will($this->returnValue($this->_rowset));
  276. $rowdata = new stdClass;
  277. $rowdata->id = 'verifytokenkey';
  278. $rowdata->verify_token = hash('sha256', 'cba');
  279. $t = time();
  280. $rowdata->created_time = $t;
  281. $this->_rowset->expects($this->any())
  282. ->method('current')
  283. ->will($this->returnValue($rowdata));
  284. $this->_callback->handle(array());
  285. $this->assertTrue($this->_callback->getHttpResponse()->getHttpResponseCode() == 200);
  286. }
  287. public function testRespondsToInvalidFeedUpdateNotPostWith404Response()
  288. { // yes, this example makes no sense for GET - I know!!!
  289. $_SERVER['REQUEST_METHOD'] = 'GET';
  290. $_SERVER['REQUEST_URI'] = '/some/path/callback/verifytokenkey';
  291. $_SERVER['CONTENT_TYPE'] = 'application/atom+xml';
  292. $feedXml = file_get_contents(dirname(__FILE__) . '/_files/atom10.xml');
  293. $GLOBALS['HTTP_RAW_POST_DATA'] = $feedXml;
  294. $this->_callback->handle(array());
  295. $this->assertTrue($this->_callback->getHttpResponse()->getHttpResponseCode() == 404);
  296. }
  297. public function testRespondsToInvalidFeedUpdateWrongMimeWith404Response()
  298. {
  299. $_SERVER['REQUEST_METHOD'] = 'POST';
  300. $_SERVER['REQUEST_URI'] = '/some/path/callback/verifytokenkey';
  301. $_SERVER['CONTENT_TYPE'] = 'application/kml+xml';
  302. $feedXml = file_get_contents(dirname(__FILE__) . '/_files/atom10.xml');
  303. $GLOBALS['HTTP_RAW_POST_DATA'] = $feedXml;
  304. $this->_callback->handle(array());
  305. $this->assertTrue($this->_callback->getHttpResponse()->getHttpResponseCode() == 404);
  306. }
  307. /**
  308. * As a judgement call, we must respond to any successful request, regardless
  309. * of the wellformedness of any XML payload, by returning a 2xx response code.
  310. * The validation of feeds and their processing must occur outside the Hubbub
  311. * protocol.
  312. */
  313. public function testRespondsToInvalidFeedUpdateWrongFeedTypeForMimeWith200Response()
  314. {
  315. $_SERVER['REQUEST_METHOD'] = 'POST';
  316. $_SERVER['REQUEST_URI'] = '/some/path/callback/verifytokenkey';
  317. $_SERVER['CONTENT_TYPE'] = 'application/rss+xml';
  318. $feedXml = file_get_contents(dirname(__FILE__) . '/_files/atom10.xml');
  319. $GLOBALS['HTTP_RAW_POST_DATA'] = $feedXml;
  320. $this->_tableGateway->expects($this->any())
  321. ->method('find')
  322. ->with($this->equalTo('verifytokenkey'))
  323. ->will($this->returnValue($this->_rowset));
  324. $rowdata = new stdClass;
  325. $rowdata->id = 'verifytokenkey';
  326. $rowdata->verify_token = hash('sha256', 'cba');
  327. $t = time();
  328. $rowdata->created_time = $t;
  329. $this->_rowset->expects($this->any())
  330. ->method('current')
  331. ->will($this->returnValue($rowdata));
  332. $this->_callback->handle(array());
  333. $this->assertTrue($this->_callback->getHttpResponse()->getHttpResponseCode() == 200);
  334. }
  335. public function testRespondsToValidFeedUpdateWithXHubOnBehalfOfHeader()
  336. {
  337. $_SERVER['REQUEST_METHOD'] = 'POST';
  338. $_SERVER['REQUEST_URI'] = '/some/path/callback/verifytokenkey';
  339. $_SERVER['CONTENT_TYPE'] = 'application/atom+xml';
  340. $feedXml = file_get_contents(dirname(__FILE__) . '/_files/atom10.xml');
  341. $GLOBALS['HTTP_RAW_POST_DATA'] = $feedXml;
  342. $this->_tableGateway->expects($this->any())
  343. ->method('find')
  344. ->with($this->equalTo('verifytokenkey'))
  345. ->will($this->returnValue($this->_rowset));
  346. $rowdata = new stdClass;
  347. $rowdata->id = 'verifytokenkey';
  348. $rowdata->verify_token = hash('sha256', 'cba');
  349. $t = time();
  350. $rowdata->created_time = $t;
  351. $this->_rowset->expects($this->any())
  352. ->method('current')
  353. ->will($this->returnValue($rowdata));
  354. $this->_callback->handle(array());
  355. $this->assertTrue($this->_callback->getHttpResponse()->getHeader('X-Hub-On-Behalf-Of') == 1);
  356. }
  357. protected function _getCleanMock($className) {
  358. $class = new ReflectionClass($className);
  359. $methods = $class->getMethods();
  360. $stubMethods = array();
  361. foreach ($methods as $method) {
  362. if ($method->isPublic() || ($method->isProtected()
  363. && $method->isAbstract())) {
  364. $stubMethods[] = $method->getName();
  365. }
  366. }
  367. $mocked = $this->getMock(
  368. $className,
  369. $stubMethods,
  370. array(),
  371. $className . '_PubsubSubscriberMock_' . uniqid(),
  372. false
  373. );
  374. return $mocked;
  375. }
  376. }
  377. /**
  378. * Stubs for storage access
  379. * DEPRECATED
  380. class Zend_Feed_Pubsubhubbub_Subscriber_CallbackTestStorageHas implements Zend_Feed_Pubsubhubbub_Storage_StorageInterface
  381. {
  382. public function setSubscription($key, array $data){}
  383. public function getSubscription($key){
  384. if ($key == 'verifytokenkey') {
  385. return array(
  386. 'id' => 'verifytokenkey',
  387. 'verify_token' => hash('sha256', 'cba')
  388. );
  389. }
  390. }
  391. public function hasSubscription($key){return true;}
  392. public function removeSubscription($key){}
  393. public function cleanup($type){}
  394. }
  395. class Zend_Feed_Pubsubhubbub_Subscriber_CallbackTestStorageHasNot implements Zend_Feed_Pubsubhubbub_Storage_StorageInterface
  396. {
  397. public function setSubscription($key, array $data){}
  398. public function getSubscription($key){}
  399. public function hasSubscription($key){return false;}
  400. public function removeSubscription($key){}
  401. public function cleanup($type){}
  402. }
  403. class Zend_Feed_Pubsubhubbub_Subscriber_CallbackTestStorageHasButWrong implements Zend_Feed_Pubsubhubbub_Storage_StorageInterface
  404. {
  405. public function setSubscription($key, array $data){}
  406. public function getSubscription($key){return 'wrong';}
  407. public function hasSubscription($key){return true;}
  408. public function removeSubscription($key){}
  409. public function cleanup($type){}
  410. }*/