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