|
| 1 | +<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 | + |
| 3 | +namespace Automattic\Jetpack\Stats; |
| 4 | + |
| 5 | +use Automattic\Jetpack\Connection\Rest_Authentication as Connection_Rest_Authentication; |
| 6 | +use PHPUnit\Framework\TestCase; |
| 7 | +use WP_REST_Request; |
| 8 | +use WP_REST_Server; |
| 9 | + |
| 10 | +/** |
| 11 | + * Unit tests for the REST API endpoints. |
| 12 | + * |
| 13 | + * @package automattic/jetpack-stats |
| 14 | + * @see \Automattic\Jetpack\Stats\REST_Provider |
| 15 | + */ |
| 16 | +class Test_REST_Provider extends TestCase { |
| 17 | + /** |
| 18 | + * REST Server object. |
| 19 | + * |
| 20 | + * @var WP_REST_Server |
| 21 | + */ |
| 22 | + private $server; |
| 23 | + |
| 24 | + const BLOG_TOKEN = 'new.blogtoken'; |
| 25 | + const BLOG_ID = 42; |
| 26 | + |
| 27 | + /** |
| 28 | + * Setting up the test. |
| 29 | + * |
| 30 | + * @before |
| 31 | + */ |
| 32 | + public function set_up() { |
| 33 | + global $wp_rest_server; |
| 34 | + |
| 35 | + $wp_rest_server = new WP_REST_Server(); |
| 36 | + $this->server = $wp_rest_server; |
| 37 | + |
| 38 | + REST_Provider::init( true ); |
| 39 | + do_action( 'rest_api_init' ); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Returning the environment into its initial state. |
| 44 | + * |
| 45 | + * @after |
| 46 | + */ |
| 47 | + public function tear_down() { |
| 48 | + unset( $_SERVER['REQUEST_METHOD'] ); |
| 49 | + $_GET = array(); |
| 50 | + |
| 51 | + Connection_Rest_Authentication::init()->reset_saved_auth_state(); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Testing the `remote_provision` endpoint without authentication. |
| 56 | + * Response: failed authorization. |
| 57 | + */ |
| 58 | + public function test_get_blog_unauthenticated() { |
| 59 | + wp_set_current_user( 0 ); |
| 60 | + $request = new WP_REST_Request( 'GET', '/jetpack/v4/stats/blog' ); |
| 61 | + $request->set_header( 'Content-Type', 'application/json' ); |
| 62 | + |
| 63 | + // Mock full connection established. |
| 64 | + add_filter( 'jetpack_options', array( $this, 'mock_jetpack_options' ), 10, 2 ); |
| 65 | + |
| 66 | + $response = $this->server->dispatch( $request ); |
| 67 | + $response_data = $response->get_data(); |
| 68 | + |
| 69 | + remove_filter( 'jetpack_options', array( $this, 'mock_jetpack_options' ), 10 ); |
| 70 | + |
| 71 | + static::assertEquals( 'invalid_permission_stats_get_blog', $response_data['code'] ); |
| 72 | + static::assertEquals( 401, $response_data['data']['status'] ); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Testing the `remote_provision` endpoint with proper authentication. |
| 77 | + * We intentionally provide an invalid user ID so the `Jetpack_XMLRPC_Server::remote_provision()` would trigger an error. |
| 78 | + * Response: `input_error`, meaning that the REST endpoint passed the data to the handler. |
| 79 | + */ |
| 80 | + public function test_get_blog_authenticated() { |
| 81 | + wp_set_current_user( 0 ); |
| 82 | + |
| 83 | + // Mock full connection established. |
| 84 | + add_filter( 'jetpack_options', array( $this, 'mock_jetpack_options' ), 10, 2 ); |
| 85 | + |
| 86 | + $_SERVER['REQUEST_METHOD'] = 'POST'; |
| 87 | + |
| 88 | + $_GET['_for'] = 'jetpack'; |
| 89 | + $_GET['token'] = 'new:1:0'; |
| 90 | + $_GET['timestamp'] = (string) time(); |
| 91 | + $_GET['nonce'] = 'testing123'; |
| 92 | + $_GET['body-hash'] = ''; |
| 93 | + // This is intentionally using base64_encode(). |
| 94 | + // phpcs:disable WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode |
| 95 | + // phpcs:disable WordPress.Security.NonceVerification.Recommended |
| 96 | + // phpcs:disable WordPress.Security.ValidatedSanitizedInput |
| 97 | + $_GET['signature'] = base64_encode( |
| 98 | + hash_hmac( |
| 99 | + 'sha1', |
| 100 | + implode( |
| 101 | + "\n", |
| 102 | + $data = array( |
| 103 | + $_GET['token'], |
| 104 | + $_GET['timestamp'], |
| 105 | + $_GET['nonce'], |
| 106 | + $_GET['body-hash'], |
| 107 | + 'POST', |
| 108 | + 'anything.example', |
| 109 | + '80', |
| 110 | + '', |
| 111 | + ) |
| 112 | + ) . "\n", |
| 113 | + 'blogtoken', |
| 114 | + true |
| 115 | + ) |
| 116 | + ); |
| 117 | + // phpcs:enable WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode |
| 118 | + // phpcs:enable WordPress.Security.NonceVerification.Recommended |
| 119 | + // phpcs:enable WordPress.Security.ValidatedSanitizedInput |
| 120 | + |
| 121 | + Connection_Rest_Authentication::init()->wp_rest_authenticate( false ); |
| 122 | + |
| 123 | + $request = new WP_REST_Request( 'GET', '/jetpack/v4/stats/blog' ); |
| 124 | + $request->set_header( 'Content-Type', 'application/json' ); |
| 125 | + |
| 126 | + $response = $this->server->dispatch( $request ); |
| 127 | + $response_data = $response->get_data(); |
| 128 | + |
| 129 | + remove_filter( 'jetpack_options', array( $this, 'mock_jetpack_options' ), 10 ); |
| 130 | + |
| 131 | + $expected_stats_blog = array( |
| 132 | + 'admin_bar' => true, |
| 133 | + 'count_roles' => array(), |
| 134 | + 'do_not_track' => true, |
| 135 | + 'version' => Main::STATS_VERSION, |
| 136 | + 'collapse_nudges' => false, |
| 137 | + 'enable_odyssey_stats' => true, |
| 138 | + 'odyssey_stats_changed_at' => 0, |
| 139 | + 'notices' => array(), |
| 140 | + 'views' => 0, |
| 141 | + 'host' => 'example.org', |
| 142 | + 'path' => '/', |
| 143 | + 'blogname' => false, |
| 144 | + 'blogdescription' => false, |
| 145 | + 'siteurl' => 'http://example.org', |
| 146 | + 'gmt_offset' => false, |
| 147 | + 'timezone_string' => false, |
| 148 | + 'stats_version' => Main::STATS_VERSION, |
| 149 | + 'stats_api' => 'jetpack', |
| 150 | + 'page_on_front' => false, |
| 151 | + 'permalink_structure' => false, |
| 152 | + 'category_base' => false, |
| 153 | + 'tag_base' => false, |
| 154 | + ); |
| 155 | + |
| 156 | + $this->assertSame( $expected_stats_blog, $response_data ); |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * Intercept the `Jetpack_Options` call and mock the values. |
| 161 | + * Full connection set-up. |
| 162 | + * |
| 163 | + * @param mixed $value The current option value. |
| 164 | + * @param string $name Option name. |
| 165 | + * |
| 166 | + * @return mixed |
| 167 | + */ |
| 168 | + public function mock_jetpack_options( $value, $name ) { |
| 169 | + switch ( $name ) { |
| 170 | + case 'blog_token': |
| 171 | + return self::BLOG_TOKEN; |
| 172 | + case 'id': |
| 173 | + return self::BLOG_ID; |
| 174 | + } |
| 175 | + |
| 176 | + return $value; |
| 177 | + } |
| 178 | +} |
0 commit comments