CallbackTest.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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-2015 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-2015 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. if (getenv('TRAVIS')) {
  228. $this->markTestSkipped(
  229. 'Test randomly fail on Travis CI.'
  230. );
  231. }
  232. $this->_get['hub_mode'] = 'unsubscribe';
  233. $this->_tableGateway->expects($this->any())
  234. ->method('find')
  235. ->with($this->equalTo('verifytokenkey'))
  236. ->will($this->returnValue($this->_rowset));
  237. $t = new Zend_Date;
  238. $rowdata = array(
  239. 'id' => 'verifytokenkey',
  240. 'verify_token' => hash('sha256', 'cba'),
  241. 'created_time' => $t->get(Zend_Date::TIMESTAMP),
  242. 'lease_seconds' => 10000
  243. );
  244. $row = new Zend_Db_Table_Row(array('data' => $rowdata));
  245. $this->_rowset->expects($this->any())
  246. ->method('current')
  247. ->will($this->returnValue($row));
  248. // require for the count call on the rowset in Model/Subscription
  249. $this->_rowset->expects($this->any())
  250. ->method('count')
  251. ->will($this->returnValue(1));
  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->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'))),
  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()->getHttpResponseCode() == 200);
  264. }
  265. public function testRespondsToValidConfirmationWithBodyContainingHubChallenge()
  266. {
  267. if (getenv('TRAVIS')) {
  268. $this->markTestSkipped(
  269. 'Test randomly fail on Travis CI.'
  270. );
  271. }
  272. $this->_tableGateway->expects($this->any())
  273. ->method('find')
  274. ->with($this->equalTo('verifytokenkey'))
  275. ->will($this->returnValue($this->_rowset));
  276. $t = new Zend_Date;
  277. $rowdata = array(
  278. 'id' => 'verifytokenkey',
  279. 'verify_token' => hash('sha256', 'cba'),
  280. 'created_time' => $t->get(Zend_Date::TIMESTAMP),
  281. 'lease_seconds' => 10000
  282. );
  283. $row = new Zend_Db_Table_Row(array('data' => $rowdata));
  284. $this->_rowset->expects($this->any())
  285. ->method('current')
  286. ->will($this->returnValue($row));
  287. // require for the count call on the rowset in Model/Subscription
  288. $this->_rowset->expects($this->any())
  289. ->method('count')
  290. ->will($this->returnValue(1));
  291. $this->_tableGateway->expects($this->once())
  292. ->method('update')
  293. ->with(
  294. $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'))),
  295. $this->equalTo('id = \'verifytokenkey\'')
  296. );
  297. $this->_adapter->expects($this->once())
  298. ->method('quoteInto')
  299. ->with($this->equalTo('id = ?'), $this->equalTo('verifytokenkey'))
  300. ->will($this->returnValue('id = \'verifytokenkey\''));
  301. $this->_callback->handle($this->_get);
  302. $this->assertTrue($this->_callback->getHttpResponse()->getBody() == 'abc');
  303. }
  304. public function testRespondsToValidFeedUpdateRequestWith200Response()
  305. {
  306. $_SERVER['REQUEST_METHOD'] = 'POST';
  307. $_SERVER['REQUEST_URI'] = '/some/path/callback/verifytokenkey';
  308. $_SERVER['CONTENT_TYPE'] = 'application/atom+xml';
  309. $feedXml = file_get_contents(dirname(__FILE__) . '/_files/atom10.xml');
  310. $GLOBALS['HTTP_RAW_POST_DATA'] = $feedXml; // dirty alternative to php://input
  311. $this->_tableGateway->expects($this->any())
  312. ->method('find')
  313. ->with($this->equalTo('verifytokenkey'))
  314. ->will($this->returnValue($this->_rowset));
  315. $t = new Zend_Date;
  316. $rowdata = array(
  317. 'id' => 'verifytokenkey',
  318. 'verify_token' => hash('sha256', 'cba'),
  319. 'created_time' => time()
  320. );
  321. $row = new Zend_Db_Table_Row(array('data' => $rowdata));
  322. $this->_rowset->expects($this->any())
  323. ->method('current')
  324. ->will($this->returnValue($row));
  325. // require for the count call on the rowset in Model/Subscription
  326. $this->_rowset->expects($this->any())
  327. ->method('count')
  328. ->will($this->returnValue(1));
  329. $this->_callback->handle(array());
  330. $this->assertTrue($this->_callback->getHttpResponse()->getHttpResponseCode() == 200);
  331. }
  332. public function testRespondsToInvalidFeedUpdateNotPostWith404Response()
  333. { // yes, this example makes no sense for GET - I know!!!
  334. $_SERVER['REQUEST_METHOD'] = 'GET';
  335. $_SERVER['REQUEST_URI'] = '/some/path/callback/verifytokenkey';
  336. $_SERVER['CONTENT_TYPE'] = 'application/atom+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. public function testRespondsToInvalidFeedUpdateWrongMimeWith404Response()
  343. {
  344. $_SERVER['REQUEST_METHOD'] = 'POST';
  345. $_SERVER['REQUEST_URI'] = '/some/path/callback/verifytokenkey';
  346. $_SERVER['CONTENT_TYPE'] = 'application/kml+xml';
  347. $feedXml = file_get_contents(dirname(__FILE__) . '/_files/atom10.xml');
  348. $GLOBALS['HTTP_RAW_POST_DATA'] = $feedXml;
  349. $this->_callback->handle(array());
  350. $this->assertTrue($this->_callback->getHttpResponse()->getHttpResponseCode() == 404);
  351. }
  352. /**
  353. * As a judgement call, we must respond to any successful request, regardless
  354. * of the wellformedness of any XML payload, by returning a 2xx response code.
  355. * The validation of feeds and their processing must occur outside the Hubbub
  356. * protocol.
  357. */
  358. public function testRespondsToInvalidFeedUpdateWrongFeedTypeForMimeWith200Response()
  359. {
  360. $_SERVER['REQUEST_METHOD'] = 'POST';
  361. $_SERVER['REQUEST_URI'] = '/some/path/callback/verifytokenkey';
  362. $_SERVER['CONTENT_TYPE'] = 'application/rss+xml';
  363. $feedXml = file_get_contents(dirname(__FILE__) . '/_files/atom10.xml');
  364. $GLOBALS['HTTP_RAW_POST_DATA'] = $feedXml;
  365. $this->_tableGateway->expects($this->any())
  366. ->method('find')
  367. ->with($this->equalTo('verifytokenkey'))
  368. ->will($this->returnValue($this->_rowset));
  369. $rowdata = array(
  370. 'id' => 'verifytokenkey',
  371. 'verify_token' => hash('sha256', 'cba'),
  372. 'created_time' => time(),
  373. 'lease_seconds' => 10000
  374. );
  375. $row = new Zend_Db_Table_Row(array('data' => $rowdata));
  376. $this->_rowset->expects($this->any())
  377. ->method('current')
  378. ->will($this->returnValue($row));
  379. // require for the count call on the rowset in Model/Subscription
  380. $this->_rowset->expects($this->any())
  381. ->method('count')
  382. ->will($this->returnValue(1));
  383. $this->_callback->handle(array());
  384. $this->assertTrue($this->_callback->getHttpResponse()->getHttpResponseCode() == 200);
  385. }
  386. public function testRespondsToValidFeedUpdateWithXHubOnBehalfOfHeader()
  387. {
  388. $_SERVER['REQUEST_METHOD'] = 'POST';
  389. $_SERVER['REQUEST_URI'] = '/some/path/callback/verifytokenkey';
  390. $_SERVER['CONTENT_TYPE'] = 'application/atom+xml';
  391. $feedXml = file_get_contents(dirname(__FILE__) . '/_files/atom10.xml');
  392. $GLOBALS['HTTP_RAW_POST_DATA'] = $feedXml;
  393. $this->_tableGateway->expects($this->any())
  394. ->method('find')
  395. ->with($this->equalTo('verifytokenkey'))
  396. ->will($this->returnValue($this->_rowset));
  397. $rowdata = array(
  398. 'id' => 'verifytokenkey',
  399. 'verify_token' => hash('sha256', 'cba'),
  400. 'created_time' => time(),
  401. 'lease_seconds' => 10000
  402. );
  403. $row = new Zend_Db_Table_Row(array('data' => $rowdata));
  404. $this->_rowset->expects($this->any())
  405. ->method('current')
  406. ->will($this->returnValue($row));
  407. // require for the count call on the rowset in Model/Subscription
  408. $this->_rowset->expects($this->any())
  409. ->method('count')
  410. ->will($this->returnValue(1));
  411. $this->_callback->handle(array());
  412. $this->assertTrue($this->_callback->getHttpResponse()->getHeader('X-Hub-On-Behalf-Of') == 1);
  413. }
  414. protected function _getCleanMock($className) {
  415. $class = new ReflectionClass($className);
  416. $methods = $class->getMethods();
  417. $stubMethods = array();
  418. foreach ($methods as $method) {
  419. if ($method->isPublic() || ($method->isProtected()
  420. && $method->isAbstract())) {
  421. $stubMethods[] = $method->getName();
  422. }
  423. }
  424. $mocked = $this->getMock(
  425. $className,
  426. $stubMethods,
  427. array(),
  428. $className . '_PubsubSubscriberMock_' . uniqid(),
  429. false
  430. );
  431. return $mocked;
  432. }
  433. }
  434. /**
  435. * Stubs for storage access
  436. * DEPRECATED
  437. class Zend_Feed_Pubsubhubbub_Subscriber_CallbackTestStorageHas implements Zend_Feed_Pubsubhubbub_Storage_StorageInterface
  438. {
  439. public function setSubscription($key, array $data){}
  440. public function getSubscription($key){
  441. if ($key == 'verifytokenkey') {
  442. return array(
  443. 'id' => 'verifytokenkey',
  444. 'verify_token' => hash('sha256', 'cba')
  445. );
  446. }
  447. }
  448. public function hasSubscription($key){return true;}
  449. public function removeSubscription($key){}
  450. public function cleanup($type){}
  451. }
  452. class Zend_Feed_Pubsubhubbub_Subscriber_CallbackTestStorageHasNot implements Zend_Feed_Pubsubhubbub_Storage_StorageInterface
  453. {
  454. public function setSubscription($key, array $data){}
  455. public function getSubscription($key){}
  456. public function hasSubscription($key){return false;}
  457. public function removeSubscription($key){}
  458. public function cleanup($type){}
  459. }
  460. class Zend_Feed_Pubsubhubbub_Subscriber_CallbackTestStorageHasButWrong implements Zend_Feed_Pubsubhubbub_Storage_StorageInterface
  461. {
  462. public function setSubscription($key, array $data){}
  463. public function getSubscription($key){return 'wrong';}
  464. public function hasSubscription($key){return true;}
  465. public function removeSubscription($key){}
  466. public function cleanup($type){}
  467. }*/