CallbackTest.php 15 KB

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