|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Class APITest |
|
4
|
|
|
* |
|
5
|
|
|
* @package WPSiteMonitor |
|
6
|
|
|
* @since 1.0.0 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
use Tests\Test_Case; |
|
10
|
|
|
use WPSiteMonitor\API; |
|
11
|
|
|
use WPSiteMonitor\WP_Site_Monitor; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* API test case. |
|
15
|
|
|
*/ |
|
16
|
|
|
class APITest extends Test_Case { |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var WP_REST_Server |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $server; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var API |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $api; |
|
27
|
|
|
|
|
28
|
|
|
public function setUp() { |
|
29
|
|
|
parent::setUp(); |
|
30
|
|
|
|
|
31
|
|
|
$this->api = new API(); |
|
32
|
|
|
|
|
33
|
|
|
// Reset REST server to ensure only our routes are registered |
|
34
|
|
|
$GLOBALS['wp_rest_server'] = null; |
|
35
|
|
|
add_filter( 'wp_rest_server_class', array( $this, 'filter_wp_rest_server_class' ) ); |
|
36
|
|
|
$this->server = rest_get_server(); |
|
37
|
|
|
remove_filter( 'wp_rest_server_class', array( $this, 'filter_wp_rest_server_class' ) ); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
function tearDown() { |
|
|
|
|
|
|
41
|
|
|
// Remove our temporary spy server |
|
42
|
|
|
$GLOBALS['wp_rest_server'] = null; |
|
43
|
|
|
unset( $_REQUEST['_wpnonce'] ); |
|
44
|
|
|
|
|
45
|
|
|
parent::tearDown(); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Assert that the register_routes method is registered to the rest_api_init hook when API is enabled. |
|
50
|
|
|
*/ |
|
51
|
|
|
public function test_register_routes_is_registered_to_rest_api_init_hook() { |
|
52
|
|
|
update_option( self::OPTION_NAME, 1 ); |
|
53
|
|
|
|
|
54
|
|
|
$plugin = new WP_Site_Monitor(); |
|
55
|
|
|
do_action( 'rest_api_init' ); |
|
56
|
|
|
|
|
57
|
|
|
$this->assertNotFalse( has_action( 'rest_api_init', array( $plugin->api, 'register_routes' ) ) ); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Assert that the register_routes method is not registered to the rest_api_init hook when API is disabled. |
|
62
|
|
|
*/ |
|
63
|
|
|
public function test_register_routes_is_not_registered_to_rest_api_init_hook_when_api_disabled() { |
|
64
|
|
|
update_option( self::OPTION_NAME, 0 ); |
|
65
|
|
|
|
|
66
|
|
|
$plugin = new WP_Site_Monitor(); |
|
67
|
|
|
do_action( 'rest_api_init' ); |
|
68
|
|
|
|
|
69
|
|
|
$this->assertFalse( has_action( 'rest_api_init', array( $plugin->api, 'register_routes' ) ) ); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Assert that REST API namespace for the plugin exists. |
|
74
|
|
|
*/ |
|
75
|
|
|
public function test_rest_api_plugin_namespace_exists() { |
|
76
|
|
|
$this->api->register_routes(); |
|
77
|
|
|
|
|
78
|
|
|
$namespaces = $this->server->get_namespaces(); |
|
79
|
|
|
|
|
80
|
|
|
$this->assertContains( self::API_NAMESPACE, $namespaces ); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Assert that WordPress version endpoint exists. |
|
85
|
|
|
*/ |
|
86
|
|
|
public function test_wp_version_endpoint_exists() { |
|
87
|
|
|
$this->api->register_routes(); |
|
88
|
|
|
|
|
89
|
|
|
$routes = $this->server->get_routes(); |
|
90
|
|
|
|
|
91
|
|
|
$this->assertArrayHasKey( '/' . self::API_NAMESPACE . '/wp-version', $routes ); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Assert that 'wp-version' endpoint returns the version number. |
|
96
|
|
|
*/ |
|
97
|
|
|
public function test_wp_version_is_returned() { |
|
98
|
|
|
global $wp_version; |
|
99
|
|
|
$returned_version = $this->api->get_wp_version(); |
|
100
|
|
|
|
|
101
|
|
|
$this->assertEquals( $wp_version, $returned_version ); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Assert that 'wp-version' endpoint requires authentication. |
|
106
|
|
|
*/ |
|
107
|
|
|
public function test_wp_version_requires_authentication() { |
|
108
|
|
|
$this->assertFalse( $this->api->check_permissions() ); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Mock REST Server class name. |
|
113
|
|
|
* |
|
114
|
|
|
* @return string |
|
115
|
|
|
*/ |
|
116
|
|
|
public function filter_wp_rest_server_class() { |
|
117
|
|
|
return 'Spy_REST_Server'; |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.