diff --git a/src/PubSubHubbub/Subscriber.php b/src/PubSubHubbub/Subscriber.php index cdb2a30d..c6f8fc33 100644 --- a/src/PubSubHubbub/Subscriber.php +++ b/src/PubSubHubbub/Subscriber.php @@ -744,7 +744,7 @@ protected function _getRequestParameters($hubUrl, $mode) 'verify_token' => hash('sha256', $params['hub.verify_token']), 'secret' => null, 'expiration_time' => $expires, - 'subscription_state' => PubSubHubbub::SUBSCRIPTION_NOTVERIFIED, + 'subscription_state' => ($mode == 'unsubscribe')? PubSubHubbub::SUBSCRIPTION_TODELETE : PubSubHubbub::SUBSCRIPTION_NOTVERIFIED, ); $this->getStorage()->setSubscription($data); diff --git a/src/PubSubHubbub/Subscriber/Callback.php b/src/PubSubHubbub/Subscriber/Callback.php index 09e69656..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; /** @@ -93,19 +94,34 @@ public function handle(array $httpGetData = null, $sendResponseNow = false) * Handle any (un)subscribe confirmation requests */ } elseif ($this->isValidHubVerification($httpGetData)) { - $data = $this->currentSubscriptionData; $this->getHttpResponse()->setContent($httpGetData['hub_challenge']); - $data['subscription_state'] = PubSubHubbub\PubSubHubbub::SUBSCRIPTION_VERIFIED; - if (isset($httpGetData['hub_lease_seconds'])) { - $data['lease_seconds'] = $httpGetData['hub_lease_seconds']; + + 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'] + )); } - $this->getStorage()->setSubscription($data); /** * Hey, C'mon! We tried everything else! */ } else { $this->getHttpResponse()->setStatusCode(404); } + if ($sendResponseNow) { $this->sendResponse(); } diff --git a/test/PubSubHubbub/Subscriber/CallbackTest.php b/test/PubSubHubbub/Subscriber/CallbackTest.php index 99bdf8b9..77394180 100644 --- a/test/PubSubHubbub/Subscriber/CallbackTest.php +++ b/test/PubSubHubbub/Subscriber/CallbackTest.php @@ -280,17 +280,9 @@ 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'=> 'verified', - '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); diff --git a/test/PubSubHubbub/SubscriberHttpTest.php b/test/PubSubHubbub/SubscriberHttpTest.php index 37e1c6a2..92f919e7 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)