Completed
Push — update/add_crm_toggle ( 0afa6c...d43f92 )
by
unknown
11:00 queued 02:49
created

Test_REST_Endpoints::test_remote_authorize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
1
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2
3
require_once ABSPATH . WPINC . '/class-IXR.php';
4
5
use Automattic\Jetpack\Config;
6
use Automattic\Jetpack\Connection\Plugin as Connection_Plugin;
7
use Automattic\Jetpack\Connection\Plugin_Storage as Connection_Plugin_Storage;
8
use phpmock\MockBuilder;
9
use PHPUnit\Framework\TestCase;
10
use Automattic\Jetpack\Connection\REST_Connector;
11
use Automattic\Jetpack\Connection\Manager;
12
13
/**
14
 * Unit tests for the REST API endpoints.
15
 *
16
 * @package automattic/jetpack-connection
17
 * @see \Automattic\Jetpack\Connection\REST_Connector
18
 */
19
class Test_REST_Endpoints extends TestCase {
20
21
	/**
22
	 * REST Server object.
23
	 *
24
	 * @var WP_REST_Server
25
	 */
26
	private $server;
27
28
	/**
29
	 * Setting up the test.
30
	 */
31
	public function setUp() {
32
		parent::setUp();
33
34
		global $wp_rest_server;
35
36
		$wp_rest_server = new WP_REST_Server();
37
		$this->server   = $wp_rest_server;
38
39
		do_action( 'rest_api_init' );
40
		new REST_Connector( new Manager() );
41
42
		add_action( 'jetpack_disabled_raw_options', array( $this, 'bypass_raw_options' ) );
43
	}
44
45
	/**
46
	 * Returning the environment into its initial state.
47
	 */
48
	public function tearDown() {
49
		parent::tearDown();
50
51
		remove_action( 'jetpack_disabled_raw_options', array( $this, 'bypass_raw_options' ) );
52
	}
53
54
	/**
55
	 * Testing the `/jetpack/v4/remote_authorize` endpoint.
56
	 */
57
	public function test_remote_authorize() {
58
		$this->request = new WP_REST_Request( 'POST', '/jetpack/v4/remote_authorize' );
59
		$this->request->set_header( 'Content-Type', 'application/json' );
60
		$this->request->set_body( '{ "state": 111, "secret": "12345", "redirect_uri": "https://example.org", "code": "54321" }' );
61
62
		$response = $this->server->dispatch( $this->request );
63
		$data     = $response->get_data();
64
65
		$this->assertEquals( 400, $data['code'] );
66
		$this->assertContains( '[verify_secrets_missing]', $data['message'] );
67
	}
68
69
	/**
70
	 * Testing the `/jetpack/v4/connection` endpoint.
71
	 */
72
	public function test_connection() {
73
		$builder = new MockBuilder();
74
		$builder->setNamespace( 'Automattic\Jetpack' )
75
				->setName( 'apply_filters' )
76
				->setFunction(
77
					function( $hook, $value ) {
78
						return 'jetpack_development_mode' === $hook ? true : $value;
79
					}
80
				);
81
82
		$mock = $builder->build();
83
		$mock->enable();
84
85
		$this->request = new WP_REST_Request( 'GET', '/jetpack/v4/connection' );
86
87
		$response = $this->server->dispatch( $this->request );
88
		$data     = $response->get_data();
89
90
		$this->assertFalse( $data['isActive'] );
91
		$this->assertFalse( $data['isRegistered'] );
92
		$this->assertTrue( $data['devMode']['isActive'] );
93
	}
94
95
	/**
96
	 * Testing the `/jetpack/v4/connection/plugins` endpoint.
97
	 */
98
	public function test_connection_plugins() {
99
		$user = wp_get_current_user();
100
		$user->add_cap( 'jetpack_admin_page' );
101
		$user->add_cap( 'activate_plugins' );
102
103
		$plugins = array(
104
			array(
105
				'name' => 'Plugin Name 1',
106
				'slug' => 'plugin-slug-1',
107
			),
108
			array(
109
				'name' => 'Plugin Name 2',
110
				'slug' => 'plugin-slug-2',
111
			),
112
		);
113
114
		array_walk(
115
			$plugins,
116
			function( $plugin ) {
117
				( new Connection_Plugin( $plugin['slug'] ) )->add( $plugin['name'] );
118
			}
119
		);
120
121
		Connection_Plugin_Storage::configure();
122
123
		$this->request = new WP_REST_Request( 'GET', '/jetpack/v4/connection/plugins' );
124
125
		$response = $this->server->dispatch( $this->request );
126
127
		$this->assertEquals( $plugins, $response->get_data() );
128
129
		$user->remove_cap( 'activate_plugins' );
130
		$user->remove_cap( 'jetpack_admin_page' );
131
	}
132
133
	/**
134
	 * This filter callback allow us to skip the database query by `Jetpack_Options` to retrieve the option.
135
	 *
136
	 * @param array $options List of options already skipping the database request.
137
	 *
138
	 * @return array
139
	 */
140
	public function bypass_raw_options( array $options ) {
141
		$options[ Manager::SECRETS_OPTION_NAME ] = true;
142
143
		return $options;
144
	}
145
146
}
147