| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544 |
- <?php
- /**
- * Zend Framework
- *
- * LICENSE
- *
- * This source file is subject to the new BSD license that is bundled
- * with this package in the file LICENSE.txt.
- * It is also available through the world-wide-web at this URL:
- * http://framework.zend.com/license/new-bsd
- * If you did not receive a copy of the license and are unable to
- * obtain it through the world-wide-web, please send an email
- * to license@zend.com so we can send you a copy immediately.
- *
- * @category Zend
- * @package UnitTests
- * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * @version $Id$
- */
- require_once 'Zend/Feed/Pubsubhubbub/Subscriber/Callback.php';
- require_once 'Zend/Feed/Pubsubhubbub/Model/Subscription.php';
- require_once 'Zend/Db/Table/Rowset/Abstract.php';
- require_once 'Zend/Db/Table/Row.php';
- require_once 'Zend/Db/Adapter/Abstract.php';
- require_once 'Zend/Db/Table/Abstract.php';
- require_once 'Zend/Db/Table/Rowset/Abstract.php';
- /**
- * @category Zend
- * @package Zend_Feed
- * @subpackage UnitTests
- * @group Zend_Feed
- * @group Zend_Feed_Subsubhubbub
- * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- */
- class Zend_Feed_Pubsubhubbub_Subscriber_CallbackTest extends PHPUnit_Framework_TestCase
- {
- protected $_originalServer = null;
- public function setUp()
- {
- $this->_callback = new Zend_Feed_Pubsubhubbub_Subscriber_Callback;
- $this->_adapter = $this->_getCleanMock(
- 'Zend_Db_Adapter_Abstract'
- );
- $this->_tableGateway = $this->_getCleanMock(
- 'Zend_Db_Table_Abstract'
- );
- $this->_rowset = $this->_getCleanMock(
- 'Zend_Db_Table_Rowset_Abstract'
- );
- $this->_tableGateway->expects($this->any())->method('getAdapter')
- ->will($this->returnValue($this->_adapter));
- $storage = new Zend_Feed_Pubsubhubbub_Model_Subscription($this->_tableGateway);
- $this->_callback->setStorage($storage);
- $this->_get = array(
- 'hub_mode' => 'subscribe',
- 'hub_topic' => 'http://www.example.com/topic',
- 'hub_challenge' => 'abc',
- 'hub_verify_token' => 'cba',
- 'hub_mode' => 'subscribe',
- 'hub_lease_seconds' => '1234567'
- );
- $this->_originalServer = $_SERVER;
- $_SERVER['REQUEST_METHOD'] = 'get';
- $_SERVER['QUERY_STRING'] = 'xhub.subscription=verifytokenkey';
- }
- public function tearDown()
- {
- $_SERVER = $this->_originalServer;
- }
- public function testCanSetHttpResponseObject()
- {
- $this->_callback->setHttpResponse(new Zend_Feed_Pubsubhubbub_HttpResponse);
- $this->assertTrue($this->_callback->getHttpResponse() instanceof Zend_Feed_Pubsubhubbub_HttpResponse);
- }
- public function testCanUsesDefaultHttpResponseObject()
- {
- $this->assertTrue($this->_callback->getHttpResponse() instanceof Zend_Feed_Pubsubhubbub_HttpResponse);
- }
- public function testThrowsExceptionOnInvalidHttpResponseObjectSet()
- {
- try {
- $this->_callback->setHttpResponse(new stdClass);
- $this->fail('Should not fail as an Exception would be raised and caught');
- } catch (Zend_Feed_Pubsubhubbub_Exception $e) {}
- }
- public function testThrowsExceptionIfNonObjectSetAsHttpResponseObject()
- {
- try {
- $this->_callback->setHttpResponse('');
- $this->fail('Should not fail as an Exception would be raised and caught');
- } catch (Zend_Feed_Pubsubhubbub_Exception $e) {}
- }
- public function testCanSetSubscriberCount()
- {
- $this->_callback->setSubscriberCount('10000');
- $this->assertEquals(10000, $this->_callback->getSubscriberCount());
- }
- public function testDefaultSubscriberCountIsOne()
- {
- $this->assertEquals(1, $this->_callback->getSubscriberCount());
- }
- public function testThrowsExceptionOnSettingZeroAsSubscriberCount()
- {
- try {
- $this->_callback->setSubscriberCount(0);
- $this->fail('Should not fail as an Exception would be raised and caught');
- } catch (Zend_Feed_Pubsubhubbub_Exception $e) {}
- }
- public function testThrowsExceptionOnSettingLessThanZeroAsSubscriberCount()
- {
- try {
- $this->_callback->setSubscriberCount(-1);
- $this->fail('Should not fail as an Exception would be raised and caught');
- } catch (Zend_Feed_Pubsubhubbub_Exception $e) {}
- }
- public function testThrowsExceptionOnSettingAnyScalarTypeCastToAZeroOrLessIntegerAsSubscriberCount()
- {
- try {
- $this->_callback->setSubscriberCount('0aa');
- $this->fail('Should not fail as an Exception would be raised and caught');
- } catch (Zend_Feed_Pubsubhubbub_Exception $e) {}
- }
- public function testCanSetStorageImplementation()
- {
- $storage = new Zend_Feed_Pubsubhubbub_Model_Subscription($this->_tableGateway);
- $this->_callback->setStorage($storage);
- $this->assertThat($this->_callback->getStorage(), $this->identicalTo($storage));
- }
- public function testValidatesValidHttpGetData()
- {
- $mockReturnValue = $this->getMock('Result', array('toArray'));
- $mockReturnValue->expects($this->any())->method('toArray')->will($this->returnValue(array(
- 'verify_token' => hash('sha256', 'cba')
- )));
- $this->_tableGateway->expects($this->any())
- ->method('find')
- ->with($this->equalTo('verifytokenkey'))
- ->will($this->returnValue($this->_rowset));
- $this->_rowset->expects($this->any())
- ->method('current')
- ->will($this->returnValue($mockReturnValue));
- $this->_rowset->expects($this->any())
- ->method('count')
- ->will($this->returnValue(1));
- $this->assertTrue($this->_callback->isValidHubVerification($this->_get));
- }
- public function testReturnsFalseIfHubVerificationNotAGetRequest()
- {
- $_SERVER['REQUEST_METHOD'] = 'POST';
- $this->assertFalse($this->_callback->isValidHubVerification($this->_get));
- }
- public function testReturnsFalseIfModeMissingFromHttpGetData()
- {
- unset($this->_get['hub_mode']);
- $this->assertFalse($this->_callback->isValidHubVerification($this->_get));
- }
- public function testReturnsFalseIfTopicMissingFromHttpGetData()
- {
- unset($this->_get['hub_topic']);
- $this->assertFalse($this->_callback->isValidHubVerification($this->_get));
- }
- public function testReturnsFalseIfChallengeMissingFromHttpGetData()
- {
- unset($this->_get['hub_challenge']);
- $this->assertFalse($this->_callback->isValidHubVerification($this->_get));
- }
- public function testReturnsFalseIfVerifyTokenMissingFromHttpGetData()
- {
- unset($this->_get['hub_verify_token']);
- $this->assertFalse($this->_callback->isValidHubVerification($this->_get));
- }
- public function testReturnsTrueIfModeSetAsUnsubscribeFromHttpGetData()
- {
- $mockReturnValue = $this->getMock('Result', array('toArray'));
- $mockReturnValue->expects($this->any())->method('toArray')->will($this->returnValue(array(
- 'verify_token' => hash('sha256', 'cba')
- )));
- $this->_get['hub_mode'] = 'unsubscribe';
- $this->_tableGateway->expects($this->any())
- ->method('find')
- ->with($this->equalTo('verifytokenkey'))
- ->will($this->returnValue($this->_rowset));
- $this->_rowset->expects($this->any())
- ->method('current')
- ->will($this->returnValue($mockReturnValue));
- // require for the count call on the rowset in Model/Subscription
- $this->_rowset->expects($this->any())
- ->method('count')
- ->will($this->returnValue(1));
- $this->assertTrue($this->_callback->isValidHubVerification($this->_get));
- }
- public function testReturnsFalseIfModeNotRecognisedFromHttpGetData()
- {
- $this->_get['hub_mode'] = 'abc';
- $this->assertFalse($this->_callback->isValidHubVerification($this->_get));
- }
- public function testReturnsFalseIfLeaseSecondsMissedWhenModeIsSubscribeFromHttpGetData()
- {
- unset($this->_get['hub_lease_seconds']);
- $this->assertFalse($this->_callback->isValidHubVerification($this->_get));
- }
- public function testReturnsFalseIfHubTopicInvalidFromHttpGetData()
- {
- $this->_get['hub_topic'] = 'http://';
- $this->assertFalse($this->_callback->isValidHubVerification($this->_get));
- }
- public function testReturnsFalseIfVerifyTokenRecordDoesNotExistForConfirmRequest()
- {
- //$this->_callback->setStorage(new Zend_Feed_Pubsubhubbub_Subscriber_CallbackTestStorageHasNot);
- $this->assertFalse($this->_callback->isValidHubVerification($this->_get));
- }
- public function testReturnsFalseIfVerifyTokenRecordDoesNotAgreeWithConfirmRequest()
- {
- //$this->_callback->setStorage(new Zend_Feed_Pubsubhubbub_Subscriber_CallbackTestStorageHasButWrong);
- $this->assertFalse($this->_callback->isValidHubVerification($this->_get));
- }
- public function testRespondsToInvalidConfirmationWith404Response()
- {
- unset($this->_get['hub_mode']);
- $this->_callback->handle($this->_get);
- $this->assertTrue($this->_callback->getHttpResponse()->getHttpResponseCode() == 404);
- }
- public function testRespondsToValidConfirmationWith200Response()
- {
- if (getenv('TRAVIS')) {
- $this->markTestSkipped(
- 'Test randomly fail on Travis CI.'
- );
- }
- $this->_get['hub_mode'] = 'unsubscribe';
- $this->_tableGateway->expects($this->any())
- ->method('find')
- ->with($this->equalTo('verifytokenkey'))
- ->will($this->returnValue($this->_rowset));
- $t = new Zend_Date;
- $rowdata = array(
- 'id' => 'verifytokenkey',
- 'verify_token' => hash('sha256', 'cba'),
- 'created_time' => $t->get(Zend_Date::TIMESTAMP),
- 'lease_seconds' => 10000
- );
- $row = new Zend_Db_Table_Row(array('data' => $rowdata));
- $this->_rowset->expects($this->any())
- ->method('current')
- ->will($this->returnValue($row));
- // require for the count call on the rowset in Model/Subscription
- $this->_rowset->expects($this->any())
- ->method('count')
- ->will($this->returnValue(1));
- $this->_tableGateway->expects($this->once())
- ->method('update')
- ->with(
- $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'))),
- $this->equalTo('id = \'verifytokenkey\'')
- );
- $this->_adapter->expects($this->once())
- ->method('quoteInto')
- ->with($this->equalTo('id = ?'), $this->equalTo('verifytokenkey'))
- ->will($this->returnValue('id = \'verifytokenkey\''));
- $this->_callback->handle($this->_get);
- $this->assertTrue($this->_callback->getHttpResponse()->getHttpResponseCode() == 200);
- }
- public function testRespondsToValidConfirmationWithBodyContainingHubChallenge()
- {
- if (getenv('TRAVIS')) {
- $this->markTestSkipped(
- 'Test randomly fail on Travis CI.'
- );
- }
- $this->_tableGateway->expects($this->any())
- ->method('find')
- ->with($this->equalTo('verifytokenkey'))
- ->will($this->returnValue($this->_rowset));
- $t = new Zend_Date;
- $rowdata = array(
- 'id' => 'verifytokenkey',
- 'verify_token' => hash('sha256', 'cba'),
- 'created_time' => $t->get(Zend_Date::TIMESTAMP),
- 'lease_seconds' => 10000
- );
- $row = new Zend_Db_Table_Row(array('data' => $rowdata));
- $this->_rowset->expects($this->any())
- ->method('current')
- ->will($this->returnValue($row));
- // require for the count call on the rowset in Model/Subscription
- $this->_rowset->expects($this->any())
- ->method('count')
- ->will($this->returnValue(1));
- $this->_tableGateway->expects($this->once())
- ->method('update')
- ->with(
- $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'))),
- $this->equalTo('id = \'verifytokenkey\'')
- );
- $this->_adapter->expects($this->once())
- ->method('quoteInto')
- ->with($this->equalTo('id = ?'), $this->equalTo('verifytokenkey'))
- ->will($this->returnValue('id = \'verifytokenkey\''));
- $this->_callback->handle($this->_get);
- $this->assertTrue($this->_callback->getHttpResponse()->getBody() == 'abc');
- }
- public function testRespondsToValidFeedUpdateRequestWith200Response()
- {
- $_SERVER['REQUEST_METHOD'] = 'POST';
- $_SERVER['REQUEST_URI'] = '/some/path/callback/verifytokenkey';
- $_SERVER['CONTENT_TYPE'] = 'application/atom+xml';
- $feedXml = file_get_contents(dirname(__FILE__) . '/_files/atom10.xml');
- $GLOBALS['HTTP_RAW_POST_DATA'] = $feedXml; // dirty alternative to php://input
- $this->_tableGateway->expects($this->any())
- ->method('find')
- ->with($this->equalTo('verifytokenkey'))
- ->will($this->returnValue($this->_rowset));
- $t = new Zend_Date;
- $rowdata = array(
- 'id' => 'verifytokenkey',
- 'verify_token' => hash('sha256', 'cba'),
- 'created_time' => time()
- );
- $row = new Zend_Db_Table_Row(array('data' => $rowdata));
- $this->_rowset->expects($this->any())
- ->method('current')
- ->will($this->returnValue($row));
- // require for the count call on the rowset in Model/Subscription
- $this->_rowset->expects($this->any())
- ->method('count')
- ->will($this->returnValue(1));
- $this->_callback->handle(array());
- $this->assertTrue($this->_callback->getHttpResponse()->getHttpResponseCode() == 200);
- }
- public function testRespondsToInvalidFeedUpdateNotPostWith404Response()
- { // yes, this example makes no sense for GET - I know!!!
- $_SERVER['REQUEST_METHOD'] = 'GET';
- $_SERVER['REQUEST_URI'] = '/some/path/callback/verifytokenkey';
- $_SERVER['CONTENT_TYPE'] = 'application/atom+xml';
- $feedXml = file_get_contents(dirname(__FILE__) . '/_files/atom10.xml');
- $GLOBALS['HTTP_RAW_POST_DATA'] = $feedXml;
- $this->_callback->handle(array());
- $this->assertTrue($this->_callback->getHttpResponse()->getHttpResponseCode() == 404);
- }
- public function testRespondsToInvalidFeedUpdateWrongMimeWith404Response()
- {
- $_SERVER['REQUEST_METHOD'] = 'POST';
- $_SERVER['REQUEST_URI'] = '/some/path/callback/verifytokenkey';
- $_SERVER['CONTENT_TYPE'] = 'application/kml+xml';
- $feedXml = file_get_contents(dirname(__FILE__) . '/_files/atom10.xml');
- $GLOBALS['HTTP_RAW_POST_DATA'] = $feedXml;
- $this->_callback->handle(array());
- $this->assertTrue($this->_callback->getHttpResponse()->getHttpResponseCode() == 404);
- }
- /**
- * As a judgement call, we must respond to any successful request, regardless
- * of the wellformedness of any XML payload, by returning a 2xx response code.
- * The validation of feeds and their processing must occur outside the Hubbub
- * protocol.
- */
- public function testRespondsToInvalidFeedUpdateWrongFeedTypeForMimeWith200Response()
- {
- $_SERVER['REQUEST_METHOD'] = 'POST';
- $_SERVER['REQUEST_URI'] = '/some/path/callback/verifytokenkey';
- $_SERVER['CONTENT_TYPE'] = 'application/rss+xml';
- $feedXml = file_get_contents(dirname(__FILE__) . '/_files/atom10.xml');
- $GLOBALS['HTTP_RAW_POST_DATA'] = $feedXml;
- $this->_tableGateway->expects($this->any())
- ->method('find')
- ->with($this->equalTo('verifytokenkey'))
- ->will($this->returnValue($this->_rowset));
- $rowdata = array(
- 'id' => 'verifytokenkey',
- 'verify_token' => hash('sha256', 'cba'),
- 'created_time' => time(),
- 'lease_seconds' => 10000
- );
- $row = new Zend_Db_Table_Row(array('data' => $rowdata));
- $this->_rowset->expects($this->any())
- ->method('current')
- ->will($this->returnValue($row));
- // require for the count call on the rowset in Model/Subscription
- $this->_rowset->expects($this->any())
- ->method('count')
- ->will($this->returnValue(1));
- $this->_callback->handle(array());
- $this->assertTrue($this->_callback->getHttpResponse()->getHttpResponseCode() == 200);
- }
- public function testRespondsToValidFeedUpdateWithXHubOnBehalfOfHeader()
- {
- $_SERVER['REQUEST_METHOD'] = 'POST';
- $_SERVER['REQUEST_URI'] = '/some/path/callback/verifytokenkey';
- $_SERVER['CONTENT_TYPE'] = 'application/atom+xml';
- $feedXml = file_get_contents(dirname(__FILE__) . '/_files/atom10.xml');
- $GLOBALS['HTTP_RAW_POST_DATA'] = $feedXml;
- $this->_tableGateway->expects($this->any())
- ->method('find')
- ->with($this->equalTo('verifytokenkey'))
- ->will($this->returnValue($this->_rowset));
- $rowdata = array(
- 'id' => 'verifytokenkey',
- 'verify_token' => hash('sha256', 'cba'),
- 'created_time' => time(),
- 'lease_seconds' => 10000
- );
- $row = new Zend_Db_Table_Row(array('data' => $rowdata));
- $this->_rowset->expects($this->any())
- ->method('current')
- ->will($this->returnValue($row));
- // require for the count call on the rowset in Model/Subscription
- $this->_rowset->expects($this->any())
- ->method('count')
- ->will($this->returnValue(1));
- $this->_callback->handle(array());
- $this->assertTrue($this->_callback->getHttpResponse()->getHeader('X-Hub-On-Behalf-Of') == 1);
- }
- protected function _getCleanMock($className) {
- $class = new ReflectionClass($className);
- $methods = $class->getMethods();
- $stubMethods = array();
- foreach ($methods as $method) {
- if ($method->isPublic() || ($method->isProtected()
- && $method->isAbstract())) {
- $stubMethods[] = $method->getName();
- }
- }
- $mocked = $this->getMock(
- $className,
- $stubMethods,
- array(),
- $className . '_PubsubSubscriberMock_' . uniqid(),
- false
- );
- return $mocked;
- }
- }
- /**
- * Stubs for storage access
- * DEPRECATED
- class Zend_Feed_Pubsubhubbub_Subscriber_CallbackTestStorageHas implements Zend_Feed_Pubsubhubbub_Storage_StorageInterface
- {
- public function setSubscription($key, array $data){}
- public function getSubscription($key){
- if ($key == 'verifytokenkey') {
- return array(
- 'id' => 'verifytokenkey',
- 'verify_token' => hash('sha256', 'cba')
- );
- }
- }
- public function hasSubscription($key){return true;}
- public function removeSubscription($key){}
- public function cleanup($type){}
- }
- class Zend_Feed_Pubsubhubbub_Subscriber_CallbackTestStorageHasNot implements Zend_Feed_Pubsubhubbub_Storage_StorageInterface
- {
- public function setSubscription($key, array $data){}
- public function getSubscription($key){}
- public function hasSubscription($key){return false;}
- public function removeSubscription($key){}
- public function cleanup($type){}
- }
- class Zend_Feed_Pubsubhubbub_Subscriber_CallbackTestStorageHasButWrong implements Zend_Feed_Pubsubhubbub_Storage_StorageInterface
- {
- public function setSubscription($key, array $data){}
- public function getSubscription($key){return 'wrong';}
- public function hasSubscription($key){return true;}
- public function removeSubscription($key){}
- public function cleanup($type){}
- }*/
|