Completed
Push — update/idc-endpoint-migration ( 65623a...1966c1 )
by
unknown
111:59 queued 101:53
created

Test_REST_Endpoints::test_start_fresh_no_access()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2
3
namespace Automattic\Jetpack\IdentityCrisis;
4
5
use Automattic\Jetpack\Connection\Manager;
6
use Automattic\Jetpack\Connection\REST_Connector;
7
use Automattic\Jetpack\Constants;
8
use Automattic\Jetpack\Identity_Crisis;
9
use Jetpack_options;
10
use PHPUnit\Framework\TestCase;
11
use WorDBless\Options as WorDBless_Options;
12
use WP_REST_Request;
13
use WP_REST_Server;
14
15
/**
16
 * Unit tests for the REST API endpoints.
17
 *
18
 * @package automattic/jetpack-identity-crisis
19
 */
20
class Test_REST_Endpoints extends TestCase {
21
22
	/**
23
	 * REST Server object.
24
	 *
25
	 * @var WP_REST_Server
26
	 */
27
	private $server;
28
29
	/**
30
	 * The original hostname to restore after tests are finished.
31
	 *
32
	 * @var string
33
	 */
34
	private $api_host_original;
35
36
	/**
37
	 * Setting up the test.
38
	 *
39
	 * @before
40
	 */
41
	public function set_up() {
42
		global $wp_rest_server;
43
44
		$wp_rest_server = new WP_REST_Server();
45
		$this->server   = $wp_rest_server;
46
47
		Identity_Crisis::init();
48
49
		do_action( 'rest_api_init' );
50
		new REST_Connector( new Manager() );
51
52
		$this->api_host_original                                  = Constants::get_constant( 'JETPACK__WPCOM_JSON_API_BASE' );
53
		Constants::$set_constants['JETPACK__WPCOM_JSON_API_BASE'] = 'https://public-api.wordpress.com';
54
55
		Constants::$set_constants['JETPACK__API_BASE'] = 'https://jetpack.wordpress.com/jetpack.';
56
57
		set_transient( 'jetpack_assumed_site_creation_date', '2020-02-28 01:13:27' );
58
	}
59
60
	/**
61
	 * Returning the environment into its initial state.
62
	 *
63
	 * @after
64
	 */
65
	public function tear_down() {
66
67
		Constants::$set_constants['JETPACK__WPCOM_JSON_API_BASE'] = $this->api_host_original;
68
69
		delete_transient( 'jetpack_assumed_site_creation_date' );
70
71
		WorDBless_Options::init()->clear_options();
72
	}
73
74
	/**
75
	 * Testing the `/jetpack/v4/identity-crisis/confirm-safe-mode` endpoint.
76
	 */
77
	public function test_confirm_safe_mode() {
78
79
		Jetpack_Options::update_option( 'safe_mode_confirmed', false );
80
81
		$user = wp_get_current_user();
82
		$user->add_cap( 'jetpack_disconnect' );
83
84
		$request = new WP_REST_Request( 'POST', '/jetpack/v4/identity-crisis/confirm-safe-mode' );
85
		$request->set_header( 'Content-Type', 'application/json' );
86
87
		$response = $this->server->dispatch( $request );
88
		$data     = $response->get_data();
89
90
		$user->remove_cap( 'jetpack_disconnect' );
91
92
		$this->assertEquals( 200, $response->get_status() );
93
		$this->assertEquals( 'success', $data['code'] );
94
		$this->assertTrue( Jetpack_Options::get_option( 'safe_mode_confirmed' ) );
95
	}
96
97
	/**
98
	 * Testing the `/jetpack/v4/identity-crisis/confirm-safe-mode` endpoint returns an error when user does not have permissions.
99
	 */
100
	public function test_confirm_safe_mode_no_access() {
101
102
		Jetpack_Options::update_option( 'safe_mode_confirmed', false );
103
104
		$request = new WP_REST_Request( 'POST', '/jetpack/v4/identity-crisis/confirm-safe-mode' );
105
		$request->set_header( 'Content-Type', 'application/json' );
106
107
		$response = $this->server->dispatch( $request );
108
109
		$this->assertEquals( 401, $response->get_status() );
110
		$this->assertFalse( Jetpack_Options::get_option( 'safe_mode_confirmed' ) );
111
	}
112
113
	/**
114
	 * Testing the `/jetpack/v4/identity-crisis/migrate` endpoint.
115
	 */
116
	public function test_migrate_stats_and_subscribers() {
117
118
		Jetpack_Options::update_option( 'sync_error_idc', true );
119
		Jetpack_Options::update_option( 'migrate_for_idc', false );
120
121
		$user = wp_get_current_user();
122
		$user->add_cap( 'jetpack_disconnect' );
123
124
		$request = new WP_REST_Request( 'POST', '/jetpack/v4/identity-crisis/migrate' );
125
		$request->set_header( 'Content-Type', 'application/json' );
126
127
		$response = $this->server->dispatch( $request );
128
		$data     = $response->get_data();
129
130
		$user->remove_cap( 'jetpack_disconnect' );
131
132
		$this->assertEquals( 200, $response->get_status() );
133
		$this->assertEquals( 'success', $data['code'] );
134
		$this->assertFalse( Jetpack_Options::get_option( 'sync_error_idc' ) );
135
		$this->assertTrue( Jetpack_Options::get_option( 'migrate_for_idc' ) );
136
	}
137
138
	/**
139
	 * Testing the `/jetpack/v4/identity-crisis/migrate` endpoint returns an error when user does not have permissions.
140
	 */
141
	public function test_migrate_stats_and_subscribers_no_access() {
142
143
		Jetpack_Options::update_option( 'sync_error_idc', true );
144
		Jetpack_Options::update_option( 'migrate_for_idc', false );
145
146
		$request = new WP_REST_Request( 'POST', '/jetpack/v4/identity-crisis/migrate' );
147
		$request->set_header( 'Content-Type', 'application/json' );
148
149
		$response = $this->server->dispatch( $request );
150
151
		$this->assertEquals( 401, $response->get_status() );
152
		$this->assertFalse( Jetpack_Options::get_option( 'safe_mode_confirmed' ) );
153
		$this->assertTrue( Jetpack_Options::get_option( 'sync_error_idc' ) );
154
		$this->assertFalse( Jetpack_Options::get_option( 'migrate_for_idc' ) );
155
	}
156
157
	/**
158
	 * Testing the `/jetpack/v4/identity-crisis/start-fresh` endpoint.
159
	 */
160
	public function test_start_fresh() {
161
162
		$user = wp_get_current_user();
163
		$user->add_cap( 'jetpack_disconnect' );
164
165
		$request = new WP_REST_Request( 'POST', '/jetpack/v4/identity-crisis/start-fresh' );
166
		$request->set_header( 'Content-Type', 'application/json' );
167
168
		$response = $this->server->dispatch( $request );
169
		$data     = $response->get_data();
170
171
		$user->remove_cap( 'jetpack_disconnect' );
172
173
		$this->assertEquals( 200, $response->get_status() );
174
		$this->assertSame( '', $data );
175
	}
176
177
	/**
178
	 * Testing the `/jetpack/v4/identity-crisis/start-fresh` endpoint returns an error when user does not have permissions.
179
	 */
180
	public function test_start_fresh_no_access() {
181
		$request = new WP_REST_Request( 'POST', '/jetpack/v4/identity-crisis/start-fresh' );
182
		$request->set_header( 'Content-Type', 'application/json' );
183
184
		$response = $this->server->dispatch( $request );
185
186
		$this->assertEquals( 401, $response->get_status() );
187
	}
188
189
}
190