Completed
Push — add/handling-connection-errors ( 06a037...ad1b5d )
by
unknown
42:28 queued 26:50
created

ManagerTest::test_disconnect_site_wpcom()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 17
Ratio 100 %

Importance

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