summaryrefslogtreecommitdiff
path: root/lib/codebird-php/test/oauth_tests.php
blob: 34b10d10680d2fb0b6e90866387915b134262bfb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php

namespace Codebird;
require_once ('test/codebirdm.php');

/**
 * A Twitter library in PHP.
 *
 * @package   codebird-test
 * @author    Jublo Solutions <support@jublo.net>
 * @copyright 2010-2016 Jublo Solutions <support@jublo.net>
 * @license   https://opensource.org/licenses/GPL-3.0 GNU General Public License 3.0
 * @link      https://github.com/jublonet/codebird-php
 */

/**
 * OAuth tests
 *
 * @package codebird-test
 */
class Oauth_Test extends \PHPUnit_Framework_TestCase
{
  /**
   * Initialise Codebird class
   *
   * @return \Codebird\Codebird The Codebird class
   */
  protected function getCB()
  {
    Codebird::setConsumerKey('123', '456');
    $cb = new CodebirdM();

    return $cb;
  }

  /**
   * Tests oauth_authenticate
   */
  public function testOauthAuthenticate()
  {
    $cb = $this->getCB();
    $cb->setToken('123', '456');
    $this->assertEquals(
      'https://api.twitter.com/oauth/authenticate?oauth_token=123',
      $cb->oauth_authenticate()
    );
    $this->assertEquals(
      'https://api.twitter.com/oauth/authenticate?oauth_token=123&force_login=1',
      $cb->oauth_authenticate($force_login = true)
    );
    $this->assertEquals(
      'https://api.twitter.com/oauth/authenticate?'
      . 'oauth_token=123&force_login=1&screen_name=TwitterAPI',
      $cb->oauth_authenticate($force_login = true, $screen_name = 'TwitterAPI')
    );
    $this->assertEquals(
      'https://api.twitter.com/oauth/authenticate?'
      . 'oauth_token=123&screen_name=TwitterAPI',
      $cb->oauth_authenticate($force_login = false, $screen_name = 'TwitterAPI')
    );
  }

  /**
   * Tests oauth_authorize
   */
  public function testOauthAuthorize()
  {
    $cb = $this->getCB();
    $cb->setToken('123', '456');
    $this->assertEquals(
      'https://api.twitter.com/oauth/authorize?oauth_token=123',
      $cb->oauth_authorize()
    );
    $this->assertEquals(
      'https://api.twitter.com/oauth/authorize?oauth_token=123&force_login=1',
      $cb->oauth_authorize($force_login = true)
    );
    $this->assertEquals(
      'https://api.twitter.com/oauth/authorize?'
      . 'oauth_token=123&force_login=1&screen_name=TwitterAPI',
      $cb->oauth_authorize($force_login = true, $screen_name = 'TwitterAPI')
    );
    $this->assertEquals(
      'https://api.twitter.com/oauth/authorize?'
      . 'oauth_token=123&screen_name=TwitterAPI',
      $cb->oauth_authorize($force_login = false, $screen_name = 'TwitterAPI')
    );
  }

  /**
   * Tests oauth2_token
   */
  public function testOauth2Token()
  {
    $cb = $this->getCB();
    $expected = new \stdClass;
    $expected->token_type = 'bearer';
    $expected->access_token = 'VqiO0n2HrKE';
    $expected->httpstatus = '200';
    $expected->rate = null;
    $this->assertEquals($expected, $cb->oauth2_token());
  }

  /**
   * Tests _getBearerAuthorization
    * @expectedException \Exception
    * @expectedExceptionMessage To make an app-only auth API request, consumer key or bearer token must be set.
   */
  public function testGetBearerAuthorization1()
  {
    $cb = $this->getCB();
    Codebird::setConsumerKey(null, null);
    $cb->setBearerToken(null);
    $cb->call('_getBearerAuthorization', []);
  }

  /**
   * Tests _getBearerAuthorization
   */
  public function testGetBearerAuthorization2()
  {
    $cb = $this->getCB();
    $cb->setBearerToken('12345678');
    $this->assertEquals('Bearer 12345678', $cb->call('_getBearerAuthorization', []));

    // automatic fetching
    $cb->setBearerToken(null);
    $this->assertEquals('Bearer VqiO0n2HrKE', $cb->call('_getBearerAuthorization', []));
  }
}