test-rest-api.php 3.44 KB
Newer Older
1
2
<?php
/**
3
 * Class REST_API_Tests
4
 *
5
 * @package Automattic_Cron_Control
6
7
 */

8
9
namespace Automattic\WP\Cron_Control\Tests;

10
11
12
/**
 * Sample test case.
 */
13
class REST_API_Tests extends \WP_UnitTestCase {
Erick Hitter's avatar
Erick Hitter committed
14
15
16
17
18
	/**
	 * Prepare for REST API tests
	 */
	public function setUp() {
		parent::setUp();
Erick Hitter's avatar
Erick Hitter committed
19

Erick Hitter's avatar
Erick Hitter committed
20
		global $wp_rest_server;
21
		$this->server = $wp_rest_server = new \WP_REST_Server;
Erick Hitter's avatar
Erick Hitter committed
22
23
		do_action( 'rest_api_init' );
	}
Erick Hitter's avatar
Erick Hitter committed
24

Erick Hitter's avatar
Erick Hitter committed
25
26
27
28
	/**
	 * Verify that GET requests to the endpoint fail
	 */
	public function test_invalid_request() {
29
		$request = new \WP_REST_Request( 'GET', '/' . \Automattic\WP\Cron_Control\REST_API::API_NAMESPACE . '/' . \Automattic\WP\Cron_Control\REST_API::ENDPOINT_LIST );
Erick Hitter's avatar
Erick Hitter committed
30
31
32
		$response = $this->server->dispatch( $request );
		$this->assertResponseStatus( 404, $response );
	}
33
34

	/**
Erick Hitter's avatar
Erick Hitter committed
35
	 * Test that list endpoint returns expected format
36
	 */
Erick Hitter's avatar
Erick Hitter committed
37
	public function test_get_items() {
38
		$ev = Utils::create_test_event();
39

40
41
42
43
44
45
46
47
48
49
50
		// Don't test internal events with this test
		$internal_events = array(
			'a8c_cron_control_force_publish_missed_schedules',
			'a8c_cron_control_confirm_scheduled_posts',
			'a8c_cron_control_delete_cron_option',
			'a8c_cron_control_purge_completed_events',
		);
		foreach ( $internal_events as $internal_event ) {
			wp_clear_scheduled_hook( $internal_event );
		}

51
		$request = new \WP_REST_Request( 'POST', '/' . \Automattic\WP\Cron_Control\REST_API::API_NAMESPACE . '/' . \Automattic\WP\Cron_Control\REST_API::ENDPOINT_LIST );
52
		$request->set_body( wp_json_encode( array( 'secret' => \WP_CRON_CONTROL_SECRET, ) ) );
Erick Hitter's avatar
Erick Hitter committed
53
		$request->set_header( 'content-type', 'application/json' );
Erick Hitter's avatar
Erick Hitter committed
54
55
56
57

		$response = $this->server->dispatch( $request );
		$data     = $response->get_data();

Erick Hitter's avatar
Erick Hitter committed
58
		$this->assertResponseStatus( 200, $response );
Erick Hitter's avatar
Erick Hitter committed
59
60
		$this->assertArrayHasKey( 'events', $data );
		$this->assertArrayHasKey( 'endpoint', $data );
61

Erick Hitter's avatar
Erick Hitter committed
62
		$this->assertResponseData( array(
63
64
65
66
			'events'   => array(
				array(
					'timestamp' => $ev['timestamp'],
					'action'    => md5( $ev['action'] ),
67
					'instance'  => md5( maybe_serialize( $ev['args'] ) ),
68
69
				),
			),
70
			'endpoint' => get_rest_url( null, \Automattic\WP\Cron_Control\REST_API::API_NAMESPACE . '/' . \Automattic\WP\Cron_Control\REST_API::ENDPOINT_RUN ),
Erick Hitter's avatar
Erick Hitter committed
71
		), $response );
72
	}
Erick Hitter's avatar
Erick Hitter committed
73

74
75
76
77
	/**
	 * Test that list endpoint returns expected format
	 */
	public function test_run_event() {
78
		$ev = Utils::create_test_event();
79
		$ev['action'] = md5( $ev['action'] );
80
		$ev['instance'] = md5( maybe_serialize( $ev['args'] ) );
81
		$ev['secret'] = \WP_CRON_CONTROL_SECRET;
82
83
		unset( $ev['args'] );

84
		$request = new \WP_REST_Request( 'PUT', '/' . \Automattic\WP\Cron_Control\REST_API::API_NAMESPACE . '/' . \Automattic\WP\Cron_Control\REST_API::ENDPOINT_RUN );
85
86
87
88
89
90
91
92
93
94
95
96
97
		$request->set_body( wp_json_encode( $ev ) );
		$request->set_header( 'content-type', 'application/json' );

		$response = $this->server->dispatch( $request );
		$data     = $response->get_data();

		$this->assertResponseStatus( 200, $response );
		$this->assertArrayHasKey( 'success', $data );
		$this->assertArrayHasKey( 'message', $data );


	}

Erick Hitter's avatar
Erick Hitter committed
98
	/**
Erick Hitter's avatar
Erick Hitter committed
99
	 * Check response code
Erick Hitter's avatar
Erick Hitter committed
100
101
102
103
	 */
	protected function assertResponseStatus( $status, $response ) {
		$this->assertEquals( $status, $response->get_status() );
	}
Erick Hitter's avatar
Erick Hitter committed
104

Erick Hitter's avatar
Erick Hitter committed
105
	/**
Erick Hitter's avatar
Erick Hitter committed
106
	 * Ensure response includes the expected data
Erick Hitter's avatar
Erick Hitter committed
107
108
	 */
	protected function assertResponseData( $data, $response ) {
109
		Utils::compare_arrays( $data, $response->get_data(), $this );
Erick Hitter's avatar
Erick Hitter committed
110
	}
Erick Hitter's avatar
Erick Hitter committed
111

Erick Hitter's avatar
Erick Hitter committed
112
	/**
Erick Hitter's avatar
Erick Hitter committed
113
	 * Clean up
Erick Hitter's avatar
Erick Hitter committed
114
115
116
	 */
	public function tearDown() {
		parent::tearDown();
Erick Hitter's avatar
Erick Hitter committed
117

Erick Hitter's avatar
Erick Hitter committed
118
119
120
		global $wp_rest_server;
		$wp_rest_server = null;
	}
Erick Hitter's avatar
Erick Hitter committed
121

Erick Hitter's avatar
Erick Hitter committed
122
}