/twitter/vendor/jublonet/codebird-php/test/signing_tests.php

https://github.com/friendica/friendica-addons · PHP · 166 lines · 102 code · 13 blank · 51 comment · 0 complexity · daf143f5d3815b26471f0345f9cfe981 MD5 · raw file

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