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