CallbackTest.php 20 KB

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