CallbackTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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->_callback->handle($this->_get);
  212. $this->assertTrue($this->_callback->getHttpResponse()->getHttpResponseCode() == 200);
  213. }
  214. public function testRespondsToValidConfirmationWithBodyContainingHubChallenge()
  215. {
  216. $this->_callback->handle($this->_get);
  217. $this->assertTrue($this->_callback->getHttpResponse()->getBody() == 'abc');
  218. }
  219. public function testRespondsToValidFeedUpdateRequestWith200Response()
  220. {
  221. $_SERVER['REQUEST_METHOD'] = 'POST';
  222. $_SERVER['REQUEST_URI'] = '/some/path/callback/verifytokenkey';
  223. $_SERVER['CONTENT_TYPE'] = 'application/atom+xml';
  224. $feedXml = file_get_contents(dirname(__FILE__) . '/_files/atom10.xml');
  225. $GLOBALS['HTTP_RAW_POST_DATA'] = $feedXml; // dirty alternative to php://input
  226. $this->_callback->handle(array());
  227. $this->assertTrue($this->_callback->getHttpResponse()->getHttpResponseCode() == 200);
  228. }
  229. public function testRespondsToInvalidFeedUpdateNotPostWith404Response()
  230. { // yes, this example makes no sense for GET - I know!!!
  231. $_SERVER['REQUEST_METHOD'] = 'GET';
  232. $_SERVER['REQUEST_URI'] = '/some/path/callback/verifytokenkey';
  233. $_SERVER['CONTENT_TYPE'] = 'application/atom+xml';
  234. $feedXml = file_get_contents(dirname(__FILE__) . '/_files/atom10.xml');
  235. $GLOBALS['HTTP_RAW_POST_DATA'] = $feedXml;
  236. $this->_callback->handle(array());
  237. $this->assertTrue($this->_callback->getHttpResponse()->getHttpResponseCode() == 404);
  238. }
  239. public function testRespondsToInvalidFeedUpdateWrongMimeWith404Response()
  240. {
  241. $_SERVER['REQUEST_METHOD'] = 'POST';
  242. $_SERVER['REQUEST_URI'] = '/some/path/callback/verifytokenkey';
  243. $_SERVER['CONTENT_TYPE'] = 'application/kml+xml';
  244. $feedXml = file_get_contents(dirname(__FILE__) . '/_files/atom10.xml');
  245. $GLOBALS['HTTP_RAW_POST_DATA'] = $feedXml;
  246. $this->_callback->handle(array());
  247. $this->assertTrue($this->_callback->getHttpResponse()->getHttpResponseCode() == 404);
  248. }
  249. /**
  250. * As a judgement call, we must respond to any successful request, regardless
  251. * of the wellformedness of any XML payload, by returning a 2xx response code.
  252. * The validation of feeds and their processing must occur outside the Hubbub
  253. * protocol.
  254. */
  255. public function testRespondsToInvalidFeedUpdateWrongFeedTypeForMimeWith200Response()
  256. {
  257. $_SERVER['REQUEST_METHOD'] = 'POST';
  258. $_SERVER['REQUEST_URI'] = '/some/path/callback/verifytokenkey';
  259. $_SERVER['CONTENT_TYPE'] = 'application/rss+xml';
  260. $feedXml = file_get_contents(dirname(__FILE__) . '/_files/atom10.xml');
  261. $GLOBALS['HTTP_RAW_POST_DATA'] = $feedXml;
  262. $this->_callback->handle(array());
  263. $this->assertTrue($this->_callback->getHttpResponse()->getHttpResponseCode() == 200);
  264. }
  265. public function testRespondsToValidFeedUpdateWithXHubOnBehalfOfHeader()
  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;
  272. $this->_callback->handle(array());
  273. $this->assertTrue($this->_callback->getHttpResponse()->getHeader('X-Hub-On-Behalf-Of') == 1);
  274. }
  275. protected function _getCleanMock($className) {
  276. $class = new ReflectionClass($className);
  277. $methods = $class->getMethods();
  278. $stubMethods = array();
  279. foreach ($methods as $method) {
  280. if ($method->isPublic() || ($method->isProtected()
  281. && $method->isAbstract())) {
  282. $stubMethods[] = $method->getName();
  283. }
  284. }
  285. $mocked = $this->getMock(
  286. $className,
  287. $stubMethods,
  288. array(),
  289. $className . '_PubsubSubscriberMock_' . uniqid(),
  290. false
  291. );
  292. return $mocked;
  293. }
  294. }
  295. /**
  296. * Stubs for storage access
  297. * DEPRECATED
  298. class Zend_Feed_Pubsubhubbub_Subscriber_CallbackTestStorageHas implements Zend_Feed_Pubsubhubbub_Storage_StorageInterface
  299. {
  300. public function setSubscription($key, array $data){}
  301. public function getSubscription($key){
  302. if ($key == 'verifytokenkey') {
  303. return array(
  304. 'id' => 'verifytokenkey',
  305. 'verify_token' => hash('sha256', 'cba')
  306. );
  307. }
  308. }
  309. public function hasSubscription($key){return true;}
  310. public function removeSubscription($key){}
  311. public function cleanup($type){}
  312. }
  313. class Zend_Feed_Pubsubhubbub_Subscriber_CallbackTestStorageHasNot implements Zend_Feed_Pubsubhubbub_Storage_StorageInterface
  314. {
  315. public function setSubscription($key, array $data){}
  316. public function getSubscription($key){}
  317. public function hasSubscription($key){return false;}
  318. public function removeSubscription($key){}
  319. public function cleanup($type){}
  320. }
  321. class Zend_Feed_Pubsubhubbub_Subscriber_CallbackTestStorageHasButWrong implements Zend_Feed_Pubsubhubbub_Storage_StorageInterface
  322. {
  323. public function setSubscription($key, array $data){}
  324. public function getSubscription($key){return 'wrong';}
  325. public function hasSubscription($key){return true;}
  326. public function removeSubscription($key){}
  327. public function cleanup($type){}
  328. }*/