CallbackTest.php 20 KB

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