Codebird library added
[friendica-addons.git/.git] / twitter / vendor / jublonet / codebird-php / test / signing_tests.php
1 <?php
2
3 namespace Codebird;
4 require_once ('test/codebirdm.php');
5
6 /**
7  * A Twitter library in PHP.
8  *
9  * @package   codebird-test
10  * @author    Jublo Solutions <support@jublo.net>
11  * @copyright 2010-2016 Jublo Solutions <support@jublo.net>
12  * @license   https://opensource.org/licenses/GPL-3.0 GNU General Public License 3.0
13  * @link      https://github.com/jublonet/codebird-php
14  */
15
16 /**
17  * OAuth signing tests
18  *
19  * @package codebird-test
20  */
21 class Signing_Test extends \PHPUnit_Framework_TestCase
22 {
23   /**
24    * Initialise Codebird class
25    *
26    * @return \Codebird\Codebird The Codebird class
27    */
28   protected function getCB()
29   {
30     Codebird::setConsumerKey('123', '456');
31     $cb = new CodebirdM();
32
33     return $cb;
34   }
35
36   /**
37    * Tests _url
38    */
39   public function testUrl()
40   {
41     $cb = $this->getCB();
42     // string
43     $this->assertEquals(
44       'q%20%2B%20b%21%22%C2%A7%24%25%26%2F%28%29%3D%3F%23%2A13%2C3%C3%A4.',
45       $cb->call('_url', ['q + b!"§$%&/()=?#*13,3ä.'])
46     );
47     // array
48     $this->assertEquals(
49       [
50         'q%20%2B%20b%21%22%C2%A7%24%25%26%2F%28%29%3D%3F%23%2A13%2C3%C3%A4.',
51         'test123'
52       ],
53       $cb->call('_url', [[
54         'q + b!"§$%&/()=?#*13,3ä.',
55         'test123'
56       ]])
57     );
58   }
59
60   /**
61    * Tests _sha1
62    */
63   public function testSha1()
64   {
65     $cb = $this->getCB();
66     $this->assertEquals(
67       'ydAlpYjrGHU7psDQ9HPgHTcwEuw=',
68       $cb->call('_sha1', ['q + b!"§$%&/()=?#*13,3ä.'])
69     );
70
71     // with access token secret
72     $cb->setToken('678', '789');
73     $this->assertEquals(
74       'CtivZhAHiX49ZMUuHXtKabLAuo0=',
75       $cb->call('_sha1', ['q + b!"§$%&/()=?#*13,3ä.'])
76     );
77   }
78
79   /**
80    * Tests _nonce
81    */
82   public function testNonce1()
83   {
84     $cb = $this->getCB();
85     // default length
86     $this->assertEquals(
87       '4247c524',
88       $cb->call('_nonce', [])
89     );
90     // custom length
91     $this->assertEquals(
92       '4247c5248da',
93       $cb->call('_nonce', [11])
94     );
95   }
96
97   /**
98    * Tests _nonce
99    * @expectedException \Exception
100    * @expectedExceptionMessage Invalid nonce length.
101    */
102   public function testNonce2()
103   {
104     $cb = $this->getCB();
105     // invalid length
106     $cb->call('_nonce', [0]);
107   }
108
109   /**
110    * Tests _getSignature
111    */
112   public function testGetSignature()
113   {
114     $cb = $this->getCB();
115     $base_params = [
116       'oauth_consumer_key' => '123',
117       'oauth_nonce' => '12345678',
118       'oauth_signature_method' => 'HMAC-SHA1',
119       'oauth_timestamp' => '1400000000',
120       'oauth_token' => '567',
121       'oauth_version' => '1.0',
122       'q' => 'Test search.'
123     ];
124     $this->assertEquals(
125       'ZON/m9bHvciPdtyK9BlokjeiW4M=',
126       $cb->call('_getSignature', ['GET', 'search/tweets', $base_params])
127     );
128   }
129
130   /**
131    * Tests _sign
132    * @expectedException \Exception
133    * @expectedExceptionMessage To generate a signature, the consumer key must be set.
134    */
135   public function testSign1()
136   {
137     $cb = $this->getCB();
138     $cb->setConsumerKey(null, null);
139     $params = ['q' => 'Test search.'];
140     $cb->call('_sign', ['GET', 'search/tweets', $params]);
141   }
142
143   /**
144    * Tests _sign
145    */
146   public function testSign2()
147   {
148     $cb = $this->getCB();
149     $params = ['q' => 'Test search.'];
150     $this->assertEquals(
151       'OAuth oauth_consumer_key="123", oauth_nonce="4247c524", oauth_signature'
152       . '="lOLNd5l6cGB9kWACxWLNKJwSD%2FI%3D", oauth_signature_method="HMAC-SHA'
153       . '1", oauth_timestamp="1412345678", oauth_version="1.0"',
154       $cb->call('_sign', ['GET', 'search/tweets', $params])
155     );
156
157     // with oauth token
158     $cb->setToken('678', '789');
159     $this->assertEquals(
160       'OAuth oauth_consumer_key="123", oauth_nonce="4247c524", oauth_signature'
161       . '="XzegzFKEqs2PpUMym5T%2BwhEmTz4%3D", oauth_signature_method="HMAC-SHA'
162       . '1", oauth_timestamp="1412345678", oauth_token="678", oauth_version="1.0"',
163       $cb->call('_sign', ['GET', 'search/tweets', $params])
164     );
165   }
166 }