|
1
|
|
|
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
|
2
|
|
|
/** |
|
3
|
|
|
* Unit tests for the Connection Webhooks class. |
|
4
|
|
|
* |
|
5
|
|
|
* @package automattic/jetpack-connection |
|
6
|
|
|
* @see \Automattic\Jetpack\Connection\Webhooks |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Automattic\Jetpack\Connection; |
|
10
|
|
|
|
|
11
|
|
|
use Brain\Monkey; |
|
12
|
|
|
use PHPUnit\Framework\TestCase; |
|
13
|
|
|
use WP_Error; |
|
14
|
|
|
|
|
15
|
|
|
// phpcs:disable WordPress.Security.NonceVerification.Recommended |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Unit tests for the Connection Webhooks class. |
|
19
|
|
|
* |
|
20
|
|
|
* @see \Automattic\Jetpack\Connection\Webhooks |
|
21
|
|
|
*/ |
|
22
|
|
|
class Test_Webhooks extends TestCase { |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* The redirects captured by the `wp_safe_redirect()` mock function. |
|
26
|
|
|
* Stored in an array in case there are multiple redirects within one query, which is an error and needs to be caught. |
|
27
|
|
|
* |
|
28
|
|
|
* @var array |
|
29
|
|
|
*/ |
|
30
|
|
|
private $redirect_stack = array(); |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Setting up the testing environment. |
|
34
|
|
|
* |
|
35
|
|
|
* @throws \phpmock\MockEnabledException The mock exception. |
|
36
|
|
|
* |
|
37
|
|
|
* @before |
|
38
|
|
|
*/ |
|
39
|
|
|
public function set_up() { |
|
40
|
|
|
Monkey\Functions\when( 'check_admin_referer' )->justReturn( true ); |
|
41
|
|
|
Monkey\Functions\when( 'wp_safe_redirect' )->alias( |
|
42
|
|
|
function ( $redirect ) { |
|
43
|
|
|
$this->redirect_stack[] = $redirect; |
|
44
|
|
|
return true; |
|
45
|
|
|
} |
|
46
|
|
|
); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Reverting the testing environment to its original state. |
|
51
|
|
|
* |
|
52
|
|
|
* @after |
|
53
|
|
|
*/ |
|
54
|
|
|
public function tear_down() { |
|
55
|
|
|
Monkey\tearDown(); |
|
56
|
|
|
$this->redirect_stack = array(); |
|
57
|
|
|
unset( $_GET['handler'], $_GET['action'] ); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Unit test for the `Webhooks::handle_authorize()` method. |
|
62
|
|
|
* Capturing the authorization error. |
|
63
|
|
|
* |
|
64
|
|
|
* @covers \Automattic\Jetpack\Connection\Webhooks::handle_authorize |
|
65
|
|
|
*/ |
|
66
|
|
|
public function test_handle_authorize_fail() { |
|
67
|
|
|
$webhooks = new Webhooks( new Manager() ); |
|
68
|
|
|
|
|
69
|
|
|
$error_result = null; |
|
70
|
|
|
$error_handler = function ( $error ) use ( &$error_result ) { |
|
71
|
|
|
$error_result = $error; |
|
72
|
|
|
}; |
|
73
|
|
|
add_action( 'jetpack_client_authorize_error', $error_handler ); |
|
74
|
|
|
|
|
75
|
|
|
$processing_started = false; |
|
76
|
|
|
$processing_handler = function () use ( &$processing_started ) { |
|
77
|
|
|
$processing_started = true; |
|
78
|
|
|
}; |
|
79
|
|
|
add_action( 'jetpack_client_authorize_processing', $processing_handler ); |
|
80
|
|
|
|
|
81
|
|
|
$webhooks->handle_authorize(); |
|
82
|
|
|
|
|
83
|
|
|
remove_action( 'jetpack_client_authorize_error', $error_handler ); |
|
84
|
|
|
remove_action( 'jetpack_client_authorize_processing', $processing_handler ); |
|
85
|
|
|
|
|
86
|
|
|
static::assertInstanceOf( WP_Error::class, $error_result ); |
|
87
|
|
|
static::assertEquals( array( '/wp-admin/' ), $this->redirect_stack ); |
|
88
|
|
|
|
|
89
|
|
|
static::assertTrue( $processing_started, 'The `jetpack_client_authorize_processing` hook was not executed.' ); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Unit test for the `Webhooks::handle_authorize()` method. |
|
94
|
|
|
* Testing the successful authorization. |
|
95
|
|
|
* |
|
96
|
|
|
* @covers \Automattic\Jetpack\Connection\Webhooks::handle_authorize |
|
97
|
|
|
*/ |
|
98
|
|
|
public function test_handle_authorize_success() { |
|
99
|
|
|
$manager = $this->createMock( Manager::class ); |
|
100
|
|
|
$manager->method( 'authorize' ) |
|
101
|
|
|
->willReturn( 'authorized' ); |
|
102
|
|
|
|
|
103
|
|
|
$webhooks = new Webhooks( $manager ); |
|
104
|
|
|
|
|
105
|
|
|
$success_handler_called = false; |
|
106
|
|
|
$success_handler = function () use ( &$success_handler_called ) { |
|
107
|
|
|
$success_handler_called = true; |
|
108
|
|
|
}; |
|
109
|
|
|
add_action( 'jetpack_client_authorized', $success_handler ); |
|
110
|
|
|
|
|
111
|
|
|
$_GET['redirect'] = '/wp-admin/?something'; |
|
112
|
|
|
$webhooks->handle_authorize(); |
|
113
|
|
|
|
|
114
|
|
|
remove_action( 'jetpack_client_authorized', $success_handler ); |
|
115
|
|
|
unset( $_GET['redirect'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
116
|
|
|
|
|
117
|
|
|
static::assertTrue( $success_handler_called ); |
|
118
|
|
|
static::assertEquals( array( '/wp-admin/?something' ), $this->redirect_stack ); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Unit test for the `Webhooks::controller()` method. |
|
123
|
|
|
* |
|
124
|
|
|
* @covers \Automattic\Jetpack\Connection\Webhooks::controller |
|
125
|
|
|
*/ |
|
126
|
|
|
public function test_controller() { |
|
127
|
|
|
$webhooks = $this->getMockBuilder( Webhooks::class ) |
|
128
|
|
|
->setConstructorArgs( array( new Manager() ) ) |
|
129
|
|
|
->setMethods( array( 'do_exit', 'handle_authorize' ) ) |
|
130
|
|
|
->getMock(); |
|
131
|
|
|
|
|
132
|
|
|
$controller_skipped = $webhooks->controller(); |
|
133
|
|
|
|
|
134
|
|
|
$webhooks->expects( $this->exactly( 2 ) ) |
|
135
|
|
|
->method( 'do_exit' ); |
|
136
|
|
|
|
|
137
|
|
|
$webhooks->expects( $this->once() ) |
|
138
|
|
|
->method( 'handle_authorize' ); |
|
139
|
|
|
|
|
140
|
|
|
$_GET['handler'] = 'jetpack-connection-webhooks'; |
|
141
|
|
|
$_GET['action'] = 'invalid-action'; |
|
142
|
|
|
|
|
143
|
|
|
// `do_exit` gets called for the first time. |
|
144
|
|
|
$webhooks->controller(); |
|
145
|
|
|
|
|
146
|
|
|
$_GET['action'] = 'authorize'; |
|
147
|
|
|
|
|
148
|
|
|
// `do_exit` gets called for the second time, and `handle_authorize` - for the first and only time. |
|
149
|
|
|
$webhooks->controller(); |
|
150
|
|
|
|
|
151
|
|
|
static::assertNull( $controller_skipped ); |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|