瀏覽代碼

Added exception throw on unauthorised actions (ignored unless a username set)

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@22338 44c647ce-9c0f-0410-b52a-842ac1e357ba
padraic 15 年之前
父節點
當前提交
85f1f0d34b
共有 2 個文件被更改,包括 23 次插入2 次删除
  1. 12 1
      library/Zend/Service/Twitter.php
  2. 11 1
      tests/Zend/Service/Twitter/TwitterTest.php

+ 12 - 1
library/Zend/Service/Twitter.php

@@ -30,6 +30,9 @@ require_once 'Zend/Rest/Client.php';
  */
 require_once 'Zend/Rest/Client/Result.php';
 
+/**
+ * @see Zend_Oauth_Consumer
+ */
 require_once 'Zend/Oauth/Consumer.php';
 
 /**
@@ -207,7 +210,6 @@ class Zend_Service_Twitter extends Zend_Rest_Client
     public function setUsername($value)
     {
         $this->_username = $value;
-        $this->_authInitialized = false;
         return $this;
     }
 
@@ -271,6 +273,15 @@ class Zend_Service_Twitter extends Zend_Rest_Client
      */
     protected function _init()
     {
+        if (!$this->isAuthorised() && $this->getUsername() !== null) {
+            require_once 'Zend/Service/Twitter/Exception.php';
+            throw new Zend_Service_Twitter_Exception(
+                'Twitter session is unauthorised. You need to initialize '
+                . 'Zend_Service_Twitter with an OAuth Access Token or use '
+                . 'its OAuth functionality to obtain an Access Token before '
+                . 'attempting any API actions that require authorisation'
+            );
+        }
         $client = $this->_localHttpClient;
         $client->resetParameters();
         if (null == $this->_cookieJar) {

+ 11 - 1
tests/Zend/Service/Twitter/TwitterTest.php

@@ -86,7 +86,7 @@ class Zend_Service_Twitter_TwitterTest extends PHPUnit_Framework_TestCase
      */
     protected function _stubTwitter($path, $method, $responseFile = null, array $params = null)
     {
-        $client = $this->getMock('Zend_Http_Client');
+        $client = $this->getMock('Zend_Oauth_Client', array(), array(), '', false);
         $client->expects($this->any())->method('resetParameters')
             ->will($this->returnValue($client));
         $client->expects($this->once())->method('setUri')
@@ -160,12 +160,22 @@ class Zend_Service_Twitter_TwitterTest extends PHPUnit_Framework_TestCase
         $token = $this->getMock('Zend_Oauth_Token_Access', array(), array(), '', false);
         $token->expects($this->once())->method('getHttpClient')->will($this->returnValue($client));
         $oauth->expects($this->once())->method('getAccessToken')->will($this->returnValue($token));
+        $client->expects($this->once())->method('setHeaders')->with('Accept-Charset', 'ISO-8859-1,utf-8');
         $twitter = new Zend_Service_Twitter(array(), $oauth);
         $twitter->getAccessToken(array(), $this->getMock('Zend_Oauth_Token_Request'));
         $this->assertTrue($client === $twitter->getLocalHttpClient());
     }
     
     /**
+     * @expectedException Zend_Service_Twitter_Exception
+     */
+    public function testAuthorisationFailureWithUsernameAndNoAccessToken()
+    {
+        $twitter = new Zend_Service_Twitter(array('username'=>'me'));
+        $twitter->statusPublicTimeline();
+    }
+    
+    /**
      * @group ZF-8218
      */
     public function testUserNameNotRequired()