|
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
|
|
|
const DEFAULT_TEST_CAPS = array( 'default_test_caps' ); |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Initialize the object before running the test method. |
|
39
|
|
|
*/ |
|
40
|
|
|
public function setUp() { |
|
41
|
|
|
$this->manager = $this->getMockBuilder( 'Automattic\Jetpack\Connection\Manager' ) |
|
42
|
|
|
->setMethods( array( 'get_access_token' ) ) |
|
43
|
|
|
->getMock(); |
|
44
|
|
|
|
|
45
|
|
|
$builder = new MockBuilder(); |
|
46
|
|
|
$builder->setNamespace( __NAMESPACE__ ) |
|
47
|
|
|
->setName( 'apply_filters' ) |
|
48
|
|
|
->setFunction( |
|
49
|
|
|
function( $filter_name, $return_value ) { |
|
50
|
|
|
return $return_value; |
|
51
|
|
|
} |
|
52
|
|
|
); |
|
53
|
|
|
|
|
54
|
|
|
$this->apply_filters = $builder->build(); |
|
55
|
|
|
|
|
56
|
|
|
$builder = new MockBuilder(); |
|
57
|
|
|
$builder->setNamespace( __NAMESPACE__ ) |
|
58
|
|
|
->setName( 'wp_redirect' ) |
|
59
|
|
|
->setFunction( |
|
60
|
|
|
function( $url ) { |
|
61
|
|
|
$this->arguments_stack['wp_redirect'] [] = array( $url ); |
|
62
|
|
|
return true; |
|
63
|
|
|
} |
|
64
|
|
|
); |
|
65
|
|
|
|
|
66
|
|
|
$this->wp_redirect = $builder->build(); |
|
67
|
|
|
|
|
68
|
|
|
// Mock the apply_filters() call in Constants::get_constant(). |
|
69
|
|
|
$builder = new MockBuilder(); |
|
70
|
|
|
$builder->setNamespace( 'Automattic\Jetpack' ) |
|
71
|
|
|
->setName( 'apply_filters' ) |
|
72
|
|
|
->setFunction( |
|
73
|
|
|
function( $filter_name, $value, $name ) { |
|
74
|
|
|
return constant( __NAMESPACE__ . "\Utils::DEFAULT_$name" ); |
|
75
|
|
|
} |
|
76
|
|
|
); |
|
77
|
|
|
$this->constants_apply_filters = $builder->build(); |
|
78
|
|
|
|
|
79
|
|
|
$this->build_mock_options(); |
|
80
|
|
|
$this->build_mock_actions(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Clean up the testing environment. |
|
85
|
|
|
*/ |
|
86
|
|
|
public function tearDown() { |
|
87
|
|
|
unset( $this->manager ); |
|
88
|
|
|
Constants::clear_constants(); |
|
89
|
|
|
Mock::disableAll(); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Test the `is_active` functionality when connected. |
|
94
|
|
|
* |
|
95
|
|
|
* @covers Automattic\Jetpack\Connection\Manager::is_active |
|
96
|
|
|
*/ |
|
97
|
|
|
public function test_is_active_when_connected() { |
|
98
|
|
|
$access_token = (object) array( |
|
99
|
|
|
'secret' => 'abcd1234', |
|
100
|
|
|
'external_user_id' => 1, |
|
101
|
|
|
); |
|
102
|
|
|
$this->manager->expects( $this->once() ) |
|
103
|
|
|
->method( 'get_access_token' ) |
|
104
|
|
|
->will( $this->returnValue( $access_token ) ); |
|
105
|
|
|
|
|
106
|
|
|
$this->assertTrue( $this->manager->is_active() ); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Test the `is_active` functionality when not connected. |
|
111
|
|
|
* |
|
112
|
|
|
* @covers Automattic\Jetpack\Connection\Manager::is_active |
|
113
|
|
|
*/ |
|
114
|
|
|
public function test_is_active_when_not_connected() { |
|
115
|
|
|
$this->manager->expects( $this->once() ) |
|
116
|
|
|
->method( 'get_access_token' ) |
|
117
|
|
|
->will( $this->returnValue( false ) ); |
|
118
|
|
|
|
|
119
|
|
|
$this->assertFalse( $this->manager->is_active() ); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Test the `api_url` generation. |
|
124
|
|
|
* |
|
125
|
|
|
* @covers Automattic\Jetpack\Connection\Manager::api_url |
|
126
|
|
|
*/ |
|
127
|
|
|
public function test_api_url_defaults() { |
|
128
|
|
|
$this->apply_filters->enable(); |
|
129
|
|
|
$this->constants_apply_filters->enable(); |
|
130
|
|
|
|
|
131
|
|
|
$this->assertEquals( |
|
132
|
|
|
'https://jetpack.wordpress.com/jetpack.something/1/', |
|
133
|
|
|
$this->manager->api_url( 'something' ) |
|
134
|
|
|
); |
|
135
|
|
|
$this->assertEquals( |
|
136
|
|
|
'https://jetpack.wordpress.com/jetpack.another_thing/1/', |
|
137
|
|
|
$this->manager->api_url( 'another_thing/' ) |
|
138
|
|
|
); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Testing the ability of the api_url method to follow set constants and filters. |
|
143
|
|
|
* |
|
144
|
|
|
* @covers Automattic\Jetpack\Connection\Manager::api_url |
|
145
|
|
|
*/ |
|
146
|
|
|
public function test_api_url_uses_constants_and_filters() { |
|
147
|
|
|
$this->apply_filters->enable(); |
|
148
|
|
|
$this->constants_apply_filters->enable(); |
|
149
|
|
|
|
|
150
|
|
|
Constants::set_constant( 'JETPACK__API_BASE', 'https://example.com/api/base.' ); |
|
151
|
|
|
$this->assertEquals( |
|
152
|
|
|
'https://example.com/api/base.something/1/', |
|
153
|
|
|
$this->manager->api_url( 'something' ) |
|
154
|
|
|
); |
|
155
|
|
|
|
|
156
|
|
|
Constants::set_constant( 'JETPACK__API_BASE', 'https://example.com/api/another.' ); |
|
157
|
|
|
Constants::set_constant( 'JETPACK__API_VERSION', '99' ); |
|
158
|
|
|
$this->assertEquals( |
|
159
|
|
|
'https://example.com/api/another.something/99/', |
|
160
|
|
|
$this->manager->api_url( 'something' ) |
|
161
|
|
|
); |
|
162
|
|
|
|
|
163
|
|
|
$this->apply_filters->disable(); |
|
164
|
|
|
|
|
165
|
|
|
// Getting a new special mock just for this occasion. |
|
166
|
|
|
$builder = new MockBuilder(); |
|
167
|
|
|
$builder->setNamespace( __NAMESPACE__ ) |
|
168
|
|
|
->setName( 'apply_filters' ) |
|
169
|
|
|
->setFunction( |
|
170
|
|
|
// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
171
|
|
|
function( $filter_name, $return_value ) { |
|
172
|
|
|
$this->arguments_stack[ $filter_name ] [] = func_get_args(); |
|
173
|
|
|
return 'completely overwrite'; |
|
174
|
|
|
} |
|
175
|
|
|
); |
|
176
|
|
|
|
|
177
|
|
|
$builder->build()->enable(); |
|
178
|
|
|
|
|
179
|
|
|
$this->assertEquals( |
|
180
|
|
|
'completely overwrite', |
|
181
|
|
|
$this->manager->api_url( 'something' ) |
|
182
|
|
|
); |
|
183
|
|
|
|
|
184
|
|
|
// The jetpack_api_url argument stack should not be empty, making sure the filter was |
|
185
|
|
|
// called with a proper name and arguments. |
|
186
|
|
|
$call_arguments = array_pop( $this->arguments_stack['jetpack_api_url'] ); |
|
187
|
|
|
$this->assertEquals( 'something', $call_arguments[2] ); |
|
188
|
|
|
$this->assertEquals( |
|
189
|
|
|
Constants::get_constant( 'JETPACK__API_BASE' ), |
|
190
|
|
|
$call_arguments[3] |
|
191
|
|
|
); |
|
192
|
|
|
$this->assertEquals( |
|
193
|
|
|
'/' . Constants::get_constant( 'JETPACK__API_VERSION' ) . '/', |
|
194
|
|
|
$call_arguments[4] |
|
195
|
|
|
); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* Test the `is_user_connected` functionality. |
|
200
|
|
|
* |
|
201
|
|
|
* @covers Automattic\Jetpack\Connection\Manager::is_user_connected |
|
202
|
|
|
*/ |
|
203
|
|
|
public function test_is_user_connected_with_default_user_id_logged_out() { |
|
204
|
|
|
$this->mock_function( 'get_current_user_id', 0 ); |
|
205
|
|
|
|
|
206
|
|
|
$this->assertFalse( $this->manager->is_user_connected() ); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* Test the `is_user_connected` functionality. |
|
211
|
|
|
* |
|
212
|
|
|
* @covers Automattic\Jetpack\Connection\Manager::is_user_connected |
|
213
|
|
|
*/ |
|
214
|
|
|
public function test_is_user_connected_with_false_user_id_logged_out() { |
|
215
|
|
|
$this->mock_function( 'get_current_user_id', 0 ); |
|
216
|
|
|
|
|
217
|
|
|
$this->assertFalse( $this->manager->is_user_connected( false ) ); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* Test the `is_user_connected` functionality |
|
222
|
|
|
* |
|
223
|
|
|
* @covers Automattic\Jetpack\Connection\Manager::is_user_connected |
|
224
|
|
|
*/ |
|
225
|
|
|
public function test_is_user_connected_with_user_id_logged_out_not_connected() { |
|
226
|
|
|
$this->mock_function( 'absint', 1 ); |
|
227
|
|
|
$this->manager->expects( $this->once() ) |
|
228
|
|
|
->method( 'get_access_token' ) |
|
229
|
|
|
->will( $this->returnValue( false ) ); |
|
230
|
|
|
|
|
231
|
|
|
$this->assertFalse( $this->manager->is_user_connected( 1 ) ); |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* Test the `is_user_connected` functionality. |
|
237
|
|
|
* |
|
238
|
|
|
* @covers Automattic\Jetpack\Connection\Manager::is_user_connected |
|
239
|
|
|
*/ |
|
240
|
|
View Code Duplication |
public function test_is_user_connected_with_default_user_id_logged_in() { |
|
241
|
|
|
$this->mock_function( 'get_current_user_id', 1 ); |
|
242
|
|
|
$access_token = (object) array( |
|
243
|
|
|
'secret' => 'abcd1234', |
|
244
|
|
|
'external_user_id' => 1, |
|
245
|
|
|
); |
|
246
|
|
|
$this->manager->expects( $this->once() ) |
|
247
|
|
|
->method( 'get_access_token' ) |
|
248
|
|
|
->will( $this->returnValue( $access_token ) ); |
|
249
|
|
|
|
|
250
|
|
|
$this->assertTrue( $this->manager->is_user_connected() ); |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
/** |
|
254
|
|
|
* Test the `is_user_connected` functionality. |
|
255
|
|
|
* |
|
256
|
|
|
* @covers Automattic\Jetpack\Connection\Manager::is_user_connected |
|
257
|
|
|
*/ |
|
258
|
|
View Code Duplication |
public function test_is_user_connected_with_user_id_logged_in() { |
|
259
|
|
|
$this->mock_function( 'absint', 1 ); |
|
260
|
|
|
$access_token = (object) array( |
|
261
|
|
|
'secret' => 'abcd1234', |
|
262
|
|
|
'external_user_id' => 1, |
|
263
|
|
|
); |
|
264
|
|
|
$this->manager->expects( $this->once() ) |
|
265
|
|
|
->method( 'get_access_token' ) |
|
266
|
|
|
->will( $this->returnValue( $access_token ) ); |
|
267
|
|
|
|
|
268
|
|
|
$this->assertTrue( $this->manager->is_user_connected( 1 ) ); |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
/** |
|
272
|
|
|
* Unit test for the "Delete all tokens" functionality. |
|
273
|
|
|
* |
|
274
|
|
|
* @covers Automattic\Jetpack\Connection\Manager::delete_all_connection_tokens |
|
275
|
|
|
* @throws MockEnabledException PHPUnit wasn't able to enable mock functions. |
|
276
|
|
|
*/ |
|
277
|
|
View Code Duplication |
public function test_delete_all_connection_tokens() { |
|
278
|
|
|
$this->update_option->enable(); |
|
279
|
|
|
$this->get_option->enable(); |
|
280
|
|
|
$this->apply_filters->enable(); |
|
281
|
|
|
$this->do_action->enable(); |
|
282
|
|
|
|
|
283
|
|
|
( new Plugin( 'plugin-slug-1' ) )->add( 'Plugin Name 1' ); |
|
284
|
|
|
|
|
285
|
|
|
( new Plugin( 'plugin-slug-2' ) )->add( 'Plugin Name 2' ); |
|
286
|
|
|
|
|
287
|
|
|
$stub = $this->createMock( Plugin::class ); |
|
288
|
|
|
$stub->method( 'is_only' ) |
|
289
|
|
|
->willReturn( false ); |
|
290
|
|
|
$manager = ( new Manager() )->set_plugin_instance( $stub ); |
|
291
|
|
|
|
|
292
|
|
|
$this->assertFalse( $manager->delete_all_connection_tokens() ); |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
|
|
/** |
|
296
|
|
|
* Unit test for the "Disconnect from WP" functionality. |
|
297
|
|
|
* |
|
298
|
|
|
* @covers Automattic\Jetpack\Connection\Manager::disconnect_site_wpcom |
|
299
|
|
|
* @throws MockEnabledException PHPUnit wasn't able to enable mock functions. |
|
300
|
|
|
*/ |
|
301
|
|
View Code Duplication |
public function test_disconnect_site_wpcom() { |
|
302
|
|
|
$this->update_option->enable(); |
|
303
|
|
|
$this->get_option->enable(); |
|
304
|
|
|
$this->apply_filters->enable(); |
|
305
|
|
|
$this->do_action->enable(); |
|
306
|
|
|
|
|
307
|
|
|
( new Plugin( 'plugin-slug-1' ) )->add( 'Plugin Name 1' ); |
|
308
|
|
|
|
|
309
|
|
|
( new Plugin( 'plugin-slug-2' ) )->add( 'Plugin Name 2' ); |
|
310
|
|
|
|
|
311
|
|
|
$stub = $this->createMock( Plugin::class ); |
|
312
|
|
|
$stub->method( 'is_only' ) |
|
313
|
|
|
->willReturn( false ); |
|
314
|
|
|
$manager = ( new Manager() )->set_plugin_instance( $stub ); |
|
315
|
|
|
|
|
316
|
|
|
$this->assertFalse( $manager->disconnect_site_wpcom() ); |
|
317
|
|
|
} |
|
318
|
|
|
|
|
319
|
|
|
/** |
|
320
|
|
|
* Test the `jetpack_connection_custom_caps' method. |
|
321
|
|
|
* |
|
322
|
|
|
* @covers Automattic\Jetpack\Connection\Manager::jetpack_connection_custom_caps |
|
323
|
|
|
* @dataProvider jetpack_connection_custom_caps_data_provider |
|
324
|
|
|
* |
|
325
|
|
|
* @param bool $in_dev_mode Whether development mode is active. |
|
326
|
|
|
* @param string $custom_cap The custom capability that is being tested. |
|
327
|
|
|
* @param array $expected_caps The expected output. |
|
328
|
|
|
*/ |
|
329
|
|
|
public function test_jetpack_connection_custom_caps( $in_dev_mode, $custom_cap, $expected_caps ) { |
|
330
|
|
|
// Mock the site_url call in Status::is_development_mode. |
|
331
|
|
|
$this->mock_function( 'site_url', false, 'Automattic\Jetpack' ); |
|
332
|
|
|
|
|
333
|
|
|
// Mock the apply_filters( 'jetpack_development_mode', ) call in Status::is_development_mode. |
|
334
|
|
|
$this->mock_function( 'apply_filters', $in_dev_mode, 'Automattic\Jetpack' ); |
|
335
|
|
|
|
|
336
|
|
|
// Mock the apply_filters( 'jetpack_disconnect_cap', ) call in jetpack_connection_custom_caps. |
|
337
|
|
|
$this->mock_function( 'apply_filters', array( 'manage_options' ) ); |
|
338
|
|
|
|
|
339
|
|
|
$caps = $this->manager->jetpack_connection_custom_caps( self::DEFAULT_TEST_CAPS, $custom_cap, 1, array() ); |
|
340
|
|
|
$this->assertEquals( $expected_caps, $caps ); |
|
341
|
|
|
} |
|
342
|
|
|
|
|
343
|
|
|
/** |
|
344
|
|
|
* Data provider test_jetpack_connection_custom_caps. |
|
345
|
|
|
* |
|
346
|
|
|
* Structure of the test data arrays: |
|
347
|
|
|
* [0] => 'in_dev_mode' boolean Whether development mode is active. |
|
348
|
|
|
* [1] => 'custom_cap' string The custom capability that is being tested. |
|
349
|
|
|
* [2] => 'expected_caps' array The expected output of the call to jetpack_connection_custom_caps. |
|
350
|
|
|
*/ |
|
351
|
|
|
public function jetpack_connection_custom_caps_data_provider() { |
|
352
|
|
|
|
|
353
|
|
|
return array( |
|
354
|
|
|
'dev mode, jetpack_connect' => array( true, 'jetpack_connect', array( 'do_not_allow' ) ), |
|
355
|
|
|
'dev mode, jetpack_reconnect' => array( true, 'jetpack_reconnect', array( 'do_not_allow' ) ), |
|
356
|
|
|
'dev mode, jetpack_disconnect' => array( true, 'jetpack_disconnect', array( 'manage_options' ) ), |
|
357
|
|
|
'dev mode, jetpack_connect_user' => array( true, 'jetpack_connect_user', array( 'do_not_allow' ) ), |
|
358
|
|
|
'dev mode, unknown cap' => array( true, 'unknown_cap', self::DEFAULT_TEST_CAPS ), |
|
359
|
|
|
'not dev mode, jetpack_connect' => array( false, 'jetpack_connect', array( 'manage_options' ) ), |
|
360
|
|
|
'not dev mode, jetpack_reconnect' => array( false, 'jetpack_reconnect', array( 'manage_options' ) ), |
|
361
|
|
|
'not dev mode, jetpack_disconnect' => array( false, 'jetpack_disconnect', array( 'manage_options' ) ), |
|
362
|
|
|
'not dev mode, jetpack_connect_user' => array( false, 'jetpack_connect_user', array( 'read' ) ), |
|
363
|
|
|
'not dev mode, unknown cap' => array( false, 'unknown_cap', self::DEFAULT_TEST_CAPS ), |
|
364
|
|
|
); |
|
365
|
|
|
} |
|
366
|
|
|
|
|
367
|
|
|
/** |
|
368
|
|
|
* Mock a global function and make it return a certain value. |
|
369
|
|
|
* |
|
370
|
|
|
* @param string $function_name Name of the function. |
|
371
|
|
|
* @param mixed $return_value Return value of the function. |
|
372
|
|
|
* @param string $namespace The namespace of the function. |
|
373
|
|
|
* |
|
374
|
|
|
* @return Mock The mock object. |
|
375
|
|
|
* @throws MockEnabledException PHPUnit wasn't able to enable mock functions. |
|
376
|
|
|
*/ |
|
377
|
|
View Code Duplication |
protected function mock_function( $function_name, $return_value = null, $namespace = __NAMESPACE__ ) { |
|
378
|
|
|
$builder = new MockBuilder(); |
|
379
|
|
|
$builder->setNamespace( $namespace ) |
|
380
|
|
|
->setName( $function_name ) |
|
381
|
|
|
->setFunction( |
|
382
|
|
|
function() use ( &$return_value ) { |
|
383
|
|
|
return $return_value; |
|
384
|
|
|
} |
|
385
|
|
|
); |
|
386
|
|
|
|
|
387
|
|
|
$mock = $builder->build(); |
|
388
|
|
|
$mock->enable(); |
|
389
|
|
|
|
|
390
|
|
|
return $mock; |
|
391
|
|
|
} |
|
392
|
|
|
|
|
393
|
|
|
} |
|
394
|
|
|
|