|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Automattic\Jetpack\Connection; |
|
4
|
|
|
|
|
5
|
|
|
use phpmock\Mock; |
|
6
|
|
|
use phpmock\MockBuilder; |
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
|
8
|
|
|
|
|
9
|
|
|
use Automattic\Jetpack\Constants; |
|
10
|
|
|
|
|
11
|
|
|
class ManagerTest extends TestCase { |
|
12
|
|
|
public function setUp() { |
|
13
|
|
|
$this->manager = $this->getMockBuilder( 'Automattic\Jetpack\Connection\Manager' ) |
|
14
|
|
|
->setMethods( [ 'get_access_token' ] ) |
|
15
|
|
|
->getMock(); |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
public function tearDown() { |
|
19
|
|
|
unset( $this->manager ); |
|
20
|
|
|
Constants::clear_constants(); |
|
21
|
|
|
Mock::disableAll(); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @covers Automattic\Jetpack\Connection\Manager::is_active |
|
26
|
|
|
*/ |
|
27
|
|
|
public function test_is_active_when_connected() { |
|
28
|
|
|
$access_token = (object) [ |
|
29
|
|
|
'secret' => 'abcd1234', |
|
30
|
|
|
'external_user_id' => 1, |
|
31
|
|
|
]; |
|
32
|
|
|
$this->manager->expects( $this->once() ) |
|
33
|
|
|
->method( 'get_access_token' ) |
|
34
|
|
|
->will( $this->returnValue( $access_token ) ); |
|
35
|
|
|
|
|
36
|
|
|
$this->assertTrue( $this->manager->is_active() ); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @covers Automattic\Jetpack\Connection\Manager::is_active |
|
41
|
|
|
*/ |
|
42
|
|
|
public function test_is_active_when_not_connected() { |
|
43
|
|
|
$this->manager->expects( $this->once() ) |
|
44
|
|
|
->method( 'get_access_token' ) |
|
45
|
|
|
->will( $this->returnValue( false ) ); |
|
46
|
|
|
|
|
47
|
|
|
$this->assertFalse( $this->manager->is_active() ); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function test_api_url_defaults() { |
|
51
|
|
|
$this->assertEquals( |
|
52
|
|
|
'https://jetpack.wordpress.com/jetpack.something/1/', |
|
53
|
|
|
$this->manager->api_url( 'something' ) |
|
54
|
|
|
); |
|
55
|
|
|
$this->assertEquals( |
|
56
|
|
|
'https://jetpack.wordpress.com/jetpack.another_thing/1/', |
|
57
|
|
|
$this->manager->api_url( 'another_thing/' ) |
|
58
|
|
|
); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function test_api_url_uses_constants() { |
|
62
|
|
|
Constants::set_constant( 'JETPACK__API_BASE', 'https://example.com/api/base.' ); |
|
63
|
|
|
$this->assertEquals( |
|
64
|
|
|
'https://example.com/api/base.something/1/', |
|
65
|
|
|
$this->manager->api_url( 'something' ) |
|
66
|
|
|
); |
|
67
|
|
|
|
|
68
|
|
|
Constants::set_constant( 'JETPACK__API_BASE', 'https://example.com/api/another.' ); |
|
69
|
|
|
Constants::set_constant( 'JETPACK__API_VERSION', '99' ); |
|
70
|
|
|
$this->assertEquals( |
|
71
|
|
|
'https://example.com/api/another.something/99/', |
|
72
|
|
|
$this->manager->api_url( 'something' ) |
|
73
|
|
|
); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @covers Automattic\Jetpack\Connection\Manager::is_user_connected |
|
78
|
|
|
*/ |
|
79
|
|
|
public function test_is_user_connected_with_default_user_id_logged_out() { |
|
80
|
|
|
$this->mock_function( 'get_current_user_id', 0 ); |
|
81
|
|
|
|
|
82
|
|
|
$this->assertFalse( $this->manager->is_user_connected() ); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @covers Automattic\Jetpack\Connection\Manager::is_user_connected |
|
87
|
|
|
*/ |
|
88
|
|
|
public function test_is_user_connected_with_false_user_id_logged_out() { |
|
89
|
|
|
$this->mock_function( 'get_current_user_id', 0 ); |
|
90
|
|
|
|
|
91
|
|
|
$this->assertFalse( $this->manager->is_user_connected( false ) ); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @covers Automattic\Jetpack\Connection\Manager::is_user_connected |
|
96
|
|
|
*/ |
|
97
|
|
|
public function test_is_user_connected_with_user_id_logged_out_not_connected() { |
|
98
|
|
|
$this->mock_function( 'absint', 1 ); |
|
99
|
|
|
$this->manager->expects( $this->once() ) |
|
100
|
|
|
->method( 'get_access_token' ) |
|
101
|
|
|
->will( $this->returnValue( false ) ); |
|
102
|
|
|
|
|
103
|
|
|
$this->assertFalse( $this->manager->is_user_connected( 1 ) ); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @covers Automattic\Jetpack\Connection\Manager::is_user_connected |
|
108
|
|
|
*/ |
|
109
|
|
View Code Duplication |
public function test_is_user_connected_with_default_user_id_logged_in() { |
|
110
|
|
|
$this->mock_function( 'get_current_user_id', 1 ); |
|
111
|
|
|
$access_token = (object) [ |
|
112
|
|
|
'secret' => 'abcd1234', |
|
113
|
|
|
'external_user_id' => 1, |
|
114
|
|
|
]; |
|
115
|
|
|
$this->manager->expects( $this->once() ) |
|
116
|
|
|
->method( 'get_access_token' ) |
|
117
|
|
|
->will( $this->returnValue( $access_token ) ); |
|
118
|
|
|
|
|
119
|
|
|
$this->assertTrue( $this->manager->is_user_connected() ); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @covers Automattic\Jetpack\Connection\Manager::is_user_connected |
|
124
|
|
|
*/ |
|
125
|
|
View Code Duplication |
public function test_is_user_connected_with_user_id_logged_in() { |
|
126
|
|
|
$this->mock_function( 'absint', 1 ); |
|
127
|
|
|
$access_token = (object) [ |
|
128
|
|
|
'secret' => 'abcd1234', |
|
129
|
|
|
'external_user_id' => 1, |
|
130
|
|
|
]; |
|
131
|
|
|
$this->manager->expects( $this->once() ) |
|
132
|
|
|
->method( 'get_access_token' ) |
|
133
|
|
|
->will( $this->returnValue( $access_token ) ); |
|
134
|
|
|
|
|
135
|
|
|
$this->assertTrue( $this->manager->is_user_connected( 1 ) ); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Mock a global function and make it return a certain value. |
|
140
|
|
|
* |
|
141
|
|
|
* @param string $function_name Name of the function. |
|
142
|
|
|
* @param mixed $return_value Return value of the function. |
|
143
|
|
|
* @return phpmock\Mock The mock object. |
|
144
|
|
|
*/ |
|
145
|
|
View Code Duplication |
protected function mock_function( $function_name, $return_value = null ) { |
|
146
|
|
|
$builder = new MockBuilder(); |
|
147
|
|
|
$builder->setNamespace( __NAMESPACE__ ) |
|
148
|
|
|
->setName( $function_name ) |
|
149
|
|
|
->setFunction( function() use ( &$return_value ) { |
|
150
|
|
|
return $return_value; |
|
151
|
|
|
} ); |
|
152
|
|
|
return $builder->build()->enable(); |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|