Completed
Push — add/connection-disconnects ( f5811a...8775a6 )
by
unknown
35:38 queued 28:15
created

ManagerTest::test_disconnect_site_wpcom()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16

Duplication

Lines 16
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 16
loc 16
rs 9.7333
c 0
b 0
f 0
1
<?php // phpcs:ignore WordPress.Files.FileName.NotHyphenatedLowercase
2
/**
3
 * Connection Manager functionality testing.
4
 *
5
 * @package automattic/jetpack-connection
6
 */
7
8
namespace Automattic\Jetpack\Connection;
9
10
use Automattic\Jetpack\Connection\Test\Mock\Hooks;
11
use Automattic\Jetpack\Connection\Test\Mock\Options;
12
use Automattic\Jetpack\Constants;
13
use phpmock\Mock;
14
use phpmock\MockBuilder;
15
use phpmock\MockEnabledException;
16
use PHPUnit\Framework\TestCase;
17
18
/**
19
 * Connection Manager functionality testing.
20
 */
21
class ManagerTest extends TestCase {
22
23
	/**
24
	 * Temporary stack for `wp_redirect`.
25
	 *
26
	 * @var array
27
	 */
28
	protected $arguments_stack = array();
29
30
	/**
31
	 * Initialize the object before running the test method.
32
	 */
33
	public function setUp() {
34
		$this->manager = $this->getMockBuilder( 'Automattic\Jetpack\Connection\Manager' )
35
			->setMethods( array( 'get_access_token' ) )
36
			->getMock();
37
38
		$builder = new MockBuilder();
39
		$builder->setNamespace( __NAMESPACE__ )
40
				->setName( 'apply_filters' )
41
				->setFunction(
42
					function( $filter_name, $return_value ) {
43
						return $return_value;
44
					}
45
				);
46
47
		$this->apply_filters = $builder->build();
48
49
		$builder = new MockBuilder();
50
		$builder->setNamespace( __NAMESPACE__ )
51
				->setName( 'wp_redirect' )
52
				->setFunction(
53
					function( $url ) {
54
						$this->arguments_stack['wp_redirect'] [] = array( $url );
55
						return true;
56
					}
57
				);
58
59
		$this->wp_redirect = $builder->build();
60
61
		// Mock the apply_filters() call in Constants::get_constant().
62
		$builder = new MockBuilder();
63
		$builder->setNamespace( 'Automattic\Jetpack' )
64
				->setName( 'apply_filters' )
65
				->setFunction(
66
					function( $filter_name, $value, $name ) {
67
						return constant( __NAMESPACE__ . "\Utils::DEFAULT_$name" );
68
					}
69
				);
70
		$this->constants_apply_filters = $builder->build();
71
	}
72
73
	/**
74
	 * Clean up the testing environment.
75
	 */
76
	public function tearDown() {
77
		unset( $this->manager );
78
		Constants::clear_constants();
79
		Mock::disableAll();
80
	}
81
82
	/**
83
	 * Test the `is_active` functionality when connected.
84
	 *
85
	 * @covers Automattic\Jetpack\Connection\Manager::is_active
86
	 */
87
	public function test_is_active_when_connected() {
88
		$access_token = (object) array(
89
			'secret'           => 'abcd1234',
90
			'external_user_id' => 1,
91
		);
92
		$this->manager->expects( $this->once() )
93
			->method( 'get_access_token' )
94
			->will( $this->returnValue( $access_token ) );
95
96
		$this->assertTrue( $this->manager->is_active() );
97
	}
98
99
	/**
100
	 * Test the `is_active` functionality when not connected.
101
	 *
102
	 * @covers Automattic\Jetpack\Connection\Manager::is_active
103
	 */
104
	public function test_is_active_when_not_connected() {
105
		$this->manager->expects( $this->once() )
106
			->method( 'get_access_token' )
107
			->will( $this->returnValue( false ) );
108
109
		$this->assertFalse( $this->manager->is_active() );
110
	}
111
112
	/**
113
	 * Test the `api_url` generation.
114
	 *
115
	 * @covers Automattic\Jetpack\Connection\Manager::api_url
116
	 */
117
	public function test_api_url_defaults() {
118
		$this->apply_filters->enable();
119
		$this->constants_apply_filters->enable();
120
121
		$this->assertEquals(
122
			'https://jetpack.wordpress.com/jetpack.something/1/',
123
			$this->manager->api_url( 'something' )
124
		);
125
		$this->assertEquals(
126
			'https://jetpack.wordpress.com/jetpack.another_thing/1/',
127
			$this->manager->api_url( 'another_thing/' )
128
		);
129
	}
130
131
	/**
132
	 * Testing the ability of the api_url method to follow set constants and filters.
133
	 *
134
	 * @covers Automattic\Jetpack\Connection\Manager::api_url
135
	 */
136
	public function test_api_url_uses_constants_and_filters() {
137
		$this->apply_filters->enable();
138
		$this->constants_apply_filters->enable();
139
140
		Constants::set_constant( 'JETPACK__API_BASE', 'https://example.com/api/base.' );
141
		$this->assertEquals(
142
			'https://example.com/api/base.something/1/',
143
			$this->manager->api_url( 'something' )
144
		);
145
146
		Constants::set_constant( 'JETPACK__API_BASE', 'https://example.com/api/another.' );
147
		Constants::set_constant( 'JETPACK__API_VERSION', '99' );
148
		$this->assertEquals(
149
			'https://example.com/api/another.something/99/',
150
			$this->manager->api_url( 'something' )
151
		);
152
153
		$this->apply_filters->disable();
154
155
		// Getting a new special mock just for this occasion.
156
		$builder = new MockBuilder();
157
		$builder->setNamespace( __NAMESPACE__ )
158
				->setName( 'apply_filters' )
159
				->setFunction(
160
					// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
161
					function( $filter_name, $return_value ) {
162
						$this->arguments_stack[ $filter_name ] [] = func_get_args();
163
						return 'completely overwrite';
164
					}
165
				);
166
167
		$builder->build()->enable();
168
169
		$this->assertEquals(
170
			'completely overwrite',
171
			$this->manager->api_url( 'something' )
172
		);
173
174
		// The jetpack_api_url argument stack should not be empty, making sure the filter was
175
		// called with a proper name and arguments.
176
		$call_arguments = array_pop( $this->arguments_stack['jetpack_api_url'] );
177
		$this->assertEquals( 'something', $call_arguments[2] );
178
		$this->assertEquals(
179
			Constants::get_constant( 'JETPACK__API_BASE' ),
180
			$call_arguments[3]
181
		);
182
		$this->assertEquals(
183
			'/' . Constants::get_constant( 'JETPACK__API_VERSION' ) . '/',
184
			$call_arguments[4]
185
		);
186
	}
187
188
	/**
189
	 * Test the `is_user_connected` functionality.
190
	 *
191
	 * @covers Automattic\Jetpack\Connection\Manager::is_user_connected
192
	 */
193
	public function test_is_user_connected_with_default_user_id_logged_out() {
194
		$this->mock_function( 'get_current_user_id', 0 );
195
196
		$this->assertFalse( $this->manager->is_user_connected() );
197
	}
198
199
	/**
200
	 * Test the `is_user_connected` functionality.
201
	 *
202
	 * @covers Automattic\Jetpack\Connection\Manager::is_user_connected
203
	 */
204
	public function test_is_user_connected_with_false_user_id_logged_out() {
205
		$this->mock_function( 'get_current_user_id', 0 );
206
207
		$this->assertFalse( $this->manager->is_user_connected( false ) );
208
	}
209
210
	/**
211
	 * Test the `is_user_connected` functionality
212
	 *
213
	 * @covers Automattic\Jetpack\Connection\Manager::is_user_connected
214
	 */
215
	public function test_is_user_connected_with_user_id_logged_out_not_connected() {
216
		$this->mock_function( 'absint', 1 );
217
		$this->manager->expects( $this->once() )
218
			->method( 'get_access_token' )
219
			->will( $this->returnValue( false ) );
220
221
		$this->assertFalse( $this->manager->is_user_connected( 1 ) );
222
	}
223
224
225
	/**
226
	 * Test the `is_user_connected` functionality.
227
	 *
228
	 * @covers Automattic\Jetpack\Connection\Manager::is_user_connected
229
	 */
230 View Code Duplication
	public function test_is_user_connected_with_default_user_id_logged_in() {
231
		$this->mock_function( 'get_current_user_id', 1 );
232
		$access_token = (object) array(
233
			'secret'           => 'abcd1234',
234
			'external_user_id' => 1,
235
		);
236
		$this->manager->expects( $this->once() )
237
			->method( 'get_access_token' )
238
			->will( $this->returnValue( $access_token ) );
239
240
		$this->assertTrue( $this->manager->is_user_connected() );
241
	}
242
243
	/**
244
	 * Test the `is_user_connected` functionality.
245
	 *
246
	 * @covers Automattic\Jetpack\Connection\Manager::is_user_connected
247
	 */
248 View Code Duplication
	public function test_is_user_connected_with_user_id_logged_in() {
249
		$this->mock_function( 'absint', 1 );
250
		$access_token = (object) array(
251
			'secret'           => 'abcd1234',
252
			'external_user_id' => 1,
253
		);
254
		$this->manager->expects( $this->once() )
255
			->method( 'get_access_token' )
256
			->will( $this->returnValue( $access_token ) );
257
258
		$this->assertTrue( $this->manager->is_user_connected( 1 ) );
259
	}
260
261
	/**
262
	 * Mock a global function and make it return a certain value.
263
	 *
264
	 * @param string $function_name Name of the function.
265
	 * @param mixed  $return_value Return value of the function.
266
	 *
267
	 * @return Mock The mock object.
268
	 * @throws MockEnabledException PHPUnit wasn't able to enable mock functions.
269
	 */
270 View Code Duplication
	protected function mock_function( $function_name, $return_value = null ) {
271
		$builder = new MockBuilder();
272
		$builder->setNamespace( __NAMESPACE__ )
273
			->setName( $function_name )
274
			->setFunction(
275
				function() use ( &$return_value ) {
276
					return $return_value;
277
				}
278
			);
279
280
		$mock = $builder->build();
281
		$mock->enable();
282
283
		return $mock;
284
	}
285
286
}
287