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 REST API namespace for the plugin exists. |
50
|
|
|
*/ |
51
|
|
|
public function test_rest_api_plugin_namespace_exists() { |
52
|
|
|
$this->reload_plugin_api_init(); |
53
|
|
|
|
54
|
|
|
$namespaces = $this->server->get_namespaces(); |
55
|
|
|
|
56
|
|
|
$this->assertContains( self::API_NAMESPACE, $namespaces ); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Assert that namespace does not exist if wp_site_monitor_enable setting is false. |
61
|
|
|
*/ |
62
|
|
|
public function test_namespace_does_not_exist_if_wp_site_monitor_enable_is_false() { |
63
|
|
|
$this->reload_plugin_api_init( false ); |
64
|
|
|
|
65
|
|
|
$namespaces = $this->server->get_namespaces(); |
66
|
|
|
|
67
|
|
|
$this->assertNotContains( self::API_NAMESPACE, $namespaces ); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Assert that WordPress version endpoint exists. |
72
|
|
|
*/ |
73
|
|
|
public function test_wp_version_endpoint_exists() { |
74
|
|
|
$this->reload_plugin_api_init(); |
75
|
|
|
|
76
|
|
|
$routes = $this->server->get_routes(); |
77
|
|
|
|
78
|
|
|
$this->assertArrayHasKey( '/' . self::API_NAMESPACE . '/wp-version', $routes ); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Assert that 'wp-version' endpoint returns the version number. |
83
|
|
|
*/ |
84
|
|
|
public function test_wp_version_is_returned() { |
85
|
|
|
global $wp_version; |
86
|
|
|
$returned_version = $this->api->get_wp_version(); |
87
|
|
|
|
88
|
|
|
$this->assertEquals( $wp_version, $returned_version ); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Assert that 'wp-version' endpoint requires authentication. |
93
|
|
|
*/ |
94
|
|
|
public function test_wp_version_requires_authentication() { |
95
|
|
|
$this->assertFalse( $this->api->check_permissions() ); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Mock REST Server class name. |
100
|
|
|
* |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
|
|
public function filter_wp_rest_server_class() { |
104
|
|
|
return 'Spy_REST_Server'; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Reload the plugin to make sure the correct wp_site_monitor_enable setting is applied. |
109
|
|
|
* |
110
|
|
|
* @param bool $enable |
111
|
|
|
*/ |
112
|
|
|
protected function reload_plugin_api_init( $enable = true ) { |
113
|
|
|
update_option( self::OPTION_NAME, $enable ); |
114
|
|
|
new WP_Site_Monitor(); |
115
|
|
|
do_action( 'rest_api_init' ); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
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.