From 889699f4cbcf1d4cc7f0ab4853e4d346d79c4ab5 Mon Sep 17 00:00:00 2001 From: Quoc Vu Date: Wed, 9 Jan 2013 20:32:11 +0000 Subject: [PATCH 1/2] unit test for PubSub subcription records removal --- test/PubSubHubbub/Subscriber/CallbackTest.php | 3 ++- test/PubSubHubbub/SubscriberHttpTest.php | 3 +++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/test/PubSubHubbub/Subscriber/CallbackTest.php b/test/PubSubHubbub/Subscriber/CallbackTest.php index 58906883..48563977 100644 --- a/test/PubSubHubbub/Subscriber/CallbackTest.php +++ b/test/PubSubHubbub/Subscriber/CallbackTest.php @@ -286,7 +286,7 @@ public function testRespondsToValidConfirmationWith200Response() 'verify_token' => hash('sha256', 'cba'), 'created_time' => $t->getTimestamp(), 'lease_seconds' => 1234567, - 'subscription_state'=> 'verified', + 'subscription_state'=> 'to_delete', 'expiration_time' => $t->add(new DateInterval('PT1234567S')) ->format('Y-m-d H:i:s'))), $this->equalTo(array('id' => 'verifytokenkey')) @@ -294,6 +294,7 @@ public function testRespondsToValidConfirmationWith200Response() $this->_callback->handle($this->_get); $this->assertTrue($this->_callback->getHttpResponse()->getStatusCode() == 200); + $this->assertTrue($this->_callback->getStorage()->hasSubscription('verifytokenkey') == false); } public function testRespondsToValidConfirmationWithBodyContainingHubChallenge() diff --git a/test/PubSubHubbub/SubscriberHttpTest.php b/test/PubSubHubbub/SubscriberHttpTest.php index 4504e1bc..6482926a 100644 --- a/test/PubSubHubbub/SubscriberHttpTest.php +++ b/test/PubSubHubbub/SubscriberHttpTest.php @@ -97,6 +97,9 @@ public function testUnsubscriptionRequestSendsExpectedPostData() .'%3A%2F%2Fwww.example.com%2Ftopic&hub.verify=sync&hub.verify=async' .'&hub.verify_token=abc', $this->client->getResponse()->getBody()); + + $subscriptionRecord = $this->subscriber->getStorage()->getSubscription(); + $this->assertEquals($subscriptionRecord['subscription_state'], PubSubHubbub::SUBSCRIPTION_TODELETE); } protected function _getCleanMock($className) From a7ef12f6664e1d4c30833e8628ebdd2e3c2ce1a6 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Mon, 14 Jan 2013 10:01:11 -0600 Subject: [PATCH 2/2] [zendframework/zf2#3295] Fixed tests and logic flow - Fixed the unit tests for the unsubscribe functionality. The table gateway was expecting the "delete" method now (not "update"), with only the "id" member in the array argument; additionally, we needed to test the return value, so I added a "will" clause. - Altered the workflow of Callback to use a switch() statement for the "hub_mode"; this will make adding additional cases in the future easier, and allows us to raise an exception on an invalid mode. --- src/PubSubHubbub/Subscriber/Callback.php | 30 ++++++++++++------- test/PubSubHubbub/Subscriber/CallbackTest.php | 15 ++-------- 2 files changed, 23 insertions(+), 22 deletions(-) diff --git a/src/PubSubHubbub/Subscriber/Callback.php b/src/PubSubHubbub/Subscriber/Callback.php index f4d0718b..e07ff563 100644 --- a/src/PubSubHubbub/Subscriber/Callback.php +++ b/src/PubSubHubbub/Subscriber/Callback.php @@ -11,6 +11,7 @@ namespace Zend\Feed\PubSubHubbub\Subscriber; use Zend\Feed\PubSubHubbub; +use Zend\Feed\PubSubHubbub\Exception; use Zend\Uri; /** @@ -95,16 +96,24 @@ public function handle(array $httpGetData = null, $sendResponseNow = false) } elseif ($this->isValidHubVerification($httpGetData)) { $this->getHttpResponse()->setContent($httpGetData['hub_challenge']); - if ($httpGetData['hub_mode'] == 'subscribe') { - $data = $this->currentSubscriptionData; - $data['subscription_state'] = PubSubHubbub\PubSubHubbub::SUBSCRIPTION_VERIFIED; - if (isset($httpGetData['hub_lease_seconds'])) { - $data['lease_seconds'] = $httpGetData['hub_lease_seconds']; - } - $this->getStorage()->setSubscription($data); - } else { - $verifyTokenKey = $this->_detectVerifyTokenKey($httpGetData); - $this->getStorage()->deleteSubscription($verifyTokenKey); + switch (strtolower($httpGetData['hub_mode'])) { + case 'subscribe': + $data = $this->currentSubscriptionData; + $data['subscription_state'] = PubSubHubbub\PubSubHubbub::SUBSCRIPTION_VERIFIED; + if (isset($httpGetData['hub_lease_seconds'])) { + $data['lease_seconds'] = $httpGetData['hub_lease_seconds']; + } + $this->getStorage()->setSubscription($data); + break; + case 'unsubscribe': + $verifyTokenKey = $this->_detectVerifyTokenKey($httpGetData); + $this->getStorage()->deleteSubscription($verifyTokenKey); + break; + default: + throw new Exception\RuntimeException(sprintf( + 'Invalid hub_mode ("%s") provided', + $httpGetData['hub_mode'] + )); } /** * Hey, C'mon! We tried everything else! @@ -112,6 +121,7 @@ public function handle(array $httpGetData = null, $sendResponseNow = false) } else { $this->getHttpResponse()->setStatusCode(404); } + if ($sendResponseNow) { $this->sendResponse(); } diff --git a/test/PubSubHubbub/Subscriber/CallbackTest.php b/test/PubSubHubbub/Subscriber/CallbackTest.php index 16c256b8..77394180 100644 --- a/test/PubSubHubbub/Subscriber/CallbackTest.php +++ b/test/PubSubHubbub/Subscriber/CallbackTest.php @@ -280,21 +280,12 @@ public function testRespondsToValidConfirmationWith200Response() ->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->getTimestamp(), - 'lease_seconds' => 1234567, - 'subscription_state'=> 'to_delete', - 'expiration_time' => $t->add(new DateInterval('PT1234567S')) - ->format('Y-m-d H:i:s'))), - $this->equalTo(array('id' => 'verifytokenkey')) - ); + ->method('delete') + ->with($this->equalTo(array('id' => 'verifytokenkey'))) + ->will($this->returnValue(true)); $this->_callback->handle($this->_get); $this->assertTrue($this->_callback->getHttpResponse()->getStatusCode() == 200); - $this->assertTrue($this->_callback->getStorage()->hasSubscription('verifytokenkey') == false); } public function testRespondsToValidConfirmationWithBodyContainingHubChallenge()