woocommerce /
woocommerce-rest-api
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Class WC_Tests_REST_System_Status file. |
||
| 4 | * |
||
| 5 | * @package Automattic/WooCommerce/Tests |
||
| 6 | */ |
||
| 7 | |||
| 8 | /** |
||
| 9 | * System Status REST Tests. |
||
| 10 | * |
||
| 11 | * @package WooCommerce\Tests\API |
||
| 12 | * @since 3.0 |
||
| 13 | */ |
||
| 14 | class WC_Tests_REST_System_Status_V2 extends WC_REST_Unit_Test_Case { |
||
|
0 ignored issues
–
show
|
|||
| 15 | |||
| 16 | /** |
||
| 17 | * Setup our test server. |
||
| 18 | */ |
||
| 19 | public function setUp() { |
||
| 20 | parent::setUp(); |
||
| 21 | $this->endpoint = new WC_REST_System_Status_Controller(); |
||
|
0 ignored issues
–
show
|
|||
| 22 | $this->user = $this->factory->user->create( |
||
|
0 ignored issues
–
show
|
|||
| 23 | array( |
||
| 24 | 'role' => 'administrator', |
||
| 25 | ) |
||
| 26 | ); |
||
| 27 | } |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Test route registration. |
||
| 31 | */ |
||
| 32 | public function test_register_routes() { |
||
| 33 | $routes = $this->server->get_routes(); |
||
| 34 | $this->assertArrayHasKey( '/wc/v2/system_status', $routes ); |
||
| 35 | $this->assertArrayHasKey( '/wc/v2/system_status/tools', $routes ); |
||
| 36 | $this->assertArrayHasKey( '/wc/v2/system_status/tools/(?P<id>[\w-]+)', $routes ); |
||
| 37 | } |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Test to make sure system status cannot be accessed without valid creds |
||
| 41 | * |
||
| 42 | * @since 3.0.0 |
||
| 43 | */ |
||
| 44 | public function test_get_system_status_info_without_permission() { |
||
| 45 | wp_set_current_user( 0 ); |
||
| 46 | $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status' ) ); |
||
| 47 | $this->assertEquals( 401, $response->get_status() ); |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Test to make sure root properties are present. |
||
| 52 | * (environment, theme, database, etc). |
||
| 53 | * |
||
| 54 | * @since 3.0.0 |
||
| 55 | */ |
||
| 56 | public function test_get_system_status_info_returns_root_properties() { |
||
| 57 | wp_set_current_user( $this->user ); |
||
| 58 | $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status' ) ); |
||
| 59 | $data = $response->get_data(); |
||
| 60 | |||
| 61 | $this->assertArrayHasKey( 'environment', $data ); |
||
| 62 | $this->assertArrayHasKey( 'database', $data ); |
||
| 63 | $this->assertArrayHasKey( 'active_plugins', $data ); |
||
| 64 | $this->assertArrayHasKey( 'theme', $data ); |
||
| 65 | $this->assertArrayHasKey( 'settings', $data ); |
||
| 66 | $this->assertArrayHasKey( 'security', $data ); |
||
| 67 | $this->assertArrayHasKey( 'pages', $data ); |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Test to make sure environment response is correct. |
||
| 72 | * |
||
| 73 | * @since 3.0.0 |
||
| 74 | */ |
||
| 75 | public function test_get_system_status_info_environment() { |
||
| 76 | wp_set_current_user( $this->user ); |
||
| 77 | $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status' ) ); |
||
| 78 | $data = $response->get_data(); |
||
| 79 | $environment = (array) $data['environment']; |
||
| 80 | |||
| 81 | // Make sure all expected data is present. |
||
| 82 | $this->assertEquals( 32, count( $environment ) ); |
||
| 83 | |||
| 84 | // Test some responses to make sure they match up. |
||
| 85 | $this->assertEquals( get_option( 'home' ), $environment['home_url'] ); |
||
| 86 | $this->assertEquals( get_option( 'siteurl' ), $environment['site_url'] ); |
||
| 87 | $this->assertEquals( WC()->version, $environment['version'] ); |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Test to make sure database response is correct. |
||
| 92 | * |
||
| 93 | * @since 3.0.0 |
||
| 94 | */ |
||
| 95 | public function test_get_system_status_info_database() { |
||
| 96 | global $wpdb; |
||
| 97 | wp_set_current_user( $this->user ); |
||
| 98 | $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status' ) ); |
||
| 99 | $data = $response->get_data(); |
||
| 100 | $database = (array) $data['database']; |
||
| 101 | |||
| 102 | $this->assertEquals( get_option( 'woocommerce_db_version' ), $database['wc_database_version'] ); |
||
| 103 | $this->assertEquals( $wpdb->prefix, $database['database_prefix'] ); |
||
| 104 | $this->assertEquals( WC_Geolocation::get_local_database_path(), $database['maxmind_geoip_database'] ); |
||
| 105 | $this->assertArrayHasKey( 'woocommerce', $database['database_tables'], print_r( $database, true ) ); |
||
| 106 | $this->assertArrayHasKey( $wpdb->prefix . 'woocommerce_payment_tokens', $database['database_tables']['woocommerce'], print_r( $database, true ) ); |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Test to make sure active plugins response is correct. |
||
| 111 | * |
||
| 112 | * @since 3.0.0 |
||
| 113 | */ |
||
| 114 | public function test_get_system_status_info_active_plugins() { |
||
| 115 | wp_set_current_user( $this->user ); |
||
| 116 | |||
| 117 | $actual_plugins = array( 'hello.php' ); |
||
| 118 | update_option( 'active_plugins', $actual_plugins ); |
||
| 119 | $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status' ) ); |
||
| 120 | update_option( 'active_plugins', array() ); |
||
| 121 | |||
| 122 | $data = $response->get_data(); |
||
| 123 | $plugins = (array) $data['active_plugins']; |
||
| 124 | |||
| 125 | $this->assertEquals( 1, count( $plugins ) ); |
||
| 126 | $this->assertEquals( 'Hello Dolly', $plugins[0]['name'] ); |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Test to make sure theme response is correct. |
||
| 131 | * |
||
| 132 | * @since 3.0.0 |
||
| 133 | */ |
||
| 134 | public function test_get_system_status_info_theme() { |
||
| 135 | wp_set_current_user( $this->user ); |
||
| 136 | $active_theme = wp_get_theme(); |
||
| 137 | |||
| 138 | $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status' ) ); |
||
| 139 | $data = $response->get_data(); |
||
| 140 | $theme = (array) $data['theme']; |
||
| 141 | |||
| 142 | $this->assertEquals( 13, count( $theme ) ); |
||
| 143 | $this->assertEquals( $active_theme->Name, $theme['name'] ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.NotSnakeCaseMemberVar |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Test to make sure settings response is correct. |
||
| 148 | * |
||
| 149 | * @since 3.0.0 |
||
| 150 | */ |
||
| 151 | public function test_get_system_status_info_settings() { |
||
| 152 | wp_set_current_user( $this->user ); |
||
| 153 | |||
| 154 | $term_response = array(); |
||
| 155 | $terms = get_terms( 'product_type', array( 'hide_empty' => 0 ) ); |
||
| 156 | foreach ( $terms as $term ) { |
||
| 157 | $term_response[ $term->slug ] = strtolower( $term->name ); |
||
| 158 | } |
||
| 159 | |||
| 160 | $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status' ) ); |
||
| 161 | $data = $response->get_data(); |
||
| 162 | $settings = (array) $data['settings']; |
||
| 163 | |||
| 164 | $this->assertEquals( 12, count( $settings ) ); |
||
| 165 | $this->assertEquals( ( 'yes' === get_option( 'woocommerce_api_enabled' ) ), $settings['api_enabled'] ); |
||
| 166 | $this->assertEquals( get_woocommerce_currency(), $settings['currency'] ); |
||
| 167 | $this->assertEquals( $term_response, $settings['taxonomies'] ); |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Test to make sure security response is correct. |
||
| 172 | * |
||
| 173 | * @since 3.0.0 |
||
| 174 | */ |
||
| 175 | public function test_get_system_status_info_security() { |
||
| 176 | wp_set_current_user( $this->user ); |
||
| 177 | |||
| 178 | $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status' ) ); |
||
| 179 | $data = $response->get_data(); |
||
| 180 | $settings = (array) $data['security']; |
||
| 181 | |||
| 182 | $this->assertEquals( 2, count( $settings ) ); |
||
| 183 | $this->assertEquals( 'https' === substr( wc_get_page_permalink( 'shop' ), 0, 5 ), $settings['secure_connection'] ); |
||
| 184 | $this->assertEquals( ! ( defined( 'WP_DEBUG' ) && defined( 'WP_DEBUG_DISPLAY' ) && WP_DEBUG && WP_DEBUG_DISPLAY ) || 0 === intval( ini_get( 'display_errors' ) ), $settings['hide_errors'] ); |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Test to make sure pages response is correct. |
||
| 189 | * |
||
| 190 | * @since 3.0.0 |
||
| 191 | */ |
||
| 192 | public function test_get_system_status_info_pages() { |
||
| 193 | wp_set_current_user( $this->user ); |
||
| 194 | $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status' ) ); |
||
| 195 | $data = $response->get_data(); |
||
| 196 | $pages = $data['pages']; |
||
| 197 | $this->assertEquals( 5, count( $pages ) ); |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Test system status schema. |
||
| 202 | * |
||
| 203 | * @since 3.0.0 |
||
| 204 | */ |
||
| 205 | public function test_system_status_schema() { |
||
| 206 | $request = new WP_REST_Request( 'OPTIONS', '/wc/v2/system_status' ); |
||
| 207 | $response = $this->server->dispatch( $request ); |
||
| 208 | $data = $response->get_data(); |
||
| 209 | $properties = $data['schema']['properties']; |
||
| 210 | $this->assertEquals( 9, count( $properties ) ); |
||
| 211 | $this->assertArrayHasKey( 'environment', $properties ); |
||
| 212 | $this->assertArrayHasKey( 'database', $properties ); |
||
| 213 | $this->assertArrayHasKey( 'active_plugins', $properties ); |
||
| 214 | $this->assertArrayHasKey( 'theme', $properties ); |
||
| 215 | $this->assertArrayHasKey( 'settings', $properties ); |
||
| 216 | $this->assertArrayHasKey( 'security', $properties ); |
||
| 217 | $this->assertArrayHasKey( 'pages', $properties ); |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Test to make sure get_items (all tools) response is correct. |
||
| 222 | * |
||
| 223 | * @since 3.0.0 |
||
| 224 | */ |
||
| 225 | public function test_get_system_tools() { |
||
| 226 | wp_set_current_user( $this->user ); |
||
| 227 | |||
| 228 | $tools_controller = new WC_REST_System_Status_Tools_Controller(); |
||
| 229 | $raw_tools = $tools_controller->get_tools(); |
||
| 230 | |||
| 231 | $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status/tools' ) ); |
||
| 232 | $data = $response->get_data(); |
||
| 233 | |||
| 234 | $this->assertEquals( 200, $response->get_status() ); |
||
| 235 | $this->assertEquals( count( $raw_tools ), count( $data ) ); |
||
| 236 | $this->assertContains( |
||
| 237 | array( |
||
| 238 | 'id' => 'regenerate_thumbnails', |
||
| 239 | 'name' => 'Regenerate shop thumbnails', |
||
| 240 | 'action' => 'Regenerate', |
||
| 241 | 'description' => 'This will regenerate all shop thumbnails to match your theme and/or image settings.', |
||
| 242 | '_links' => array( |
||
| 243 | 'item' => array( |
||
| 244 | array( |
||
| 245 | 'href' => rest_url( '/wc/v2/system_status/tools/regenerate_thumbnails' ), |
||
| 246 | 'embeddable' => true, |
||
| 247 | ), |
||
| 248 | ), |
||
| 249 | ), |
||
| 250 | ), |
||
| 251 | $data |
||
| 252 | ); |
||
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Test to make sure system status tools cannot be accessed without valid creds |
||
| 257 | * |
||
| 258 | * @since 3.0.0 |
||
| 259 | */ |
||
| 260 | public function test_get_system_status_tools_without_permission() { |
||
| 261 | wp_set_current_user( 0 ); |
||
| 262 | $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status/tools' ) ); |
||
| 263 | $this->assertEquals( 401, $response->get_status() ); |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Test to make sure we can load a single tool correctly. |
||
| 268 | * |
||
| 269 | * @since 3.0.0 |
||
| 270 | */ |
||
| 271 | public function test_get_system_tool() { |
||
| 272 | wp_set_current_user( $this->user ); |
||
| 273 | |||
| 274 | $tools_controller = new WC_REST_System_Status_Tools_Controller(); |
||
| 275 | $raw_tools = $tools_controller->get_tools(); |
||
| 276 | $raw_tool = $raw_tools['recount_terms']; |
||
| 277 | |||
| 278 | $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status/tools/recount_terms' ) ); |
||
| 279 | $data = $response->get_data(); |
||
| 280 | |||
| 281 | $this->assertEquals( 200, $response->get_status() ); |
||
| 282 | |||
| 283 | $this->assertEquals( 'recount_terms', $data['id'] ); |
||
| 284 | $this->assertEquals( 'Term counts', $data['name'] ); |
||
| 285 | $this->assertEquals( 'Recount terms', $data['action'] ); |
||
| 286 | $this->assertEquals( 'This tool will recount product terms - useful when changing your settings in a way which hides products from the catalog.', $data['description'] ); |
||
| 287 | } |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Test to make sure a single system status toolscannot be accessed without valid creds. |
||
| 291 | * |
||
| 292 | * @since 3.0.0 |
||
| 293 | */ |
||
| 294 | public function test_get_system_status_tool_without_permission() { |
||
| 295 | wp_set_current_user( 0 ); |
||
| 296 | $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status/tools/recount_terms' ) ); |
||
| 297 | $this->assertEquals( 401, $response->get_status() ); |
||
| 298 | } |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Test to make sure we can RUN a tool correctly. |
||
| 302 | * |
||
| 303 | * @since 3.0.0 |
||
| 304 | */ |
||
| 305 | public function test_execute_system_tool() { |
||
| 306 | wp_set_current_user( $this->user ); |
||
| 307 | |||
| 308 | $tools_controller = new WC_REST_System_Status_Tools_Controller(); |
||
| 309 | $raw_tools = $tools_controller->get_tools(); |
||
| 310 | $raw_tool = $raw_tools['recount_terms']; |
||
| 311 | |||
| 312 | $response = $this->server->dispatch( new WP_REST_Request( 'POST', '/wc/v2/system_status/tools/recount_terms' ) ); |
||
| 313 | $data = $response->get_data(); |
||
| 314 | |||
| 315 | $this->assertEquals( 'recount_terms', $data['id'] ); |
||
| 316 | $this->assertEquals( 'Term counts', $data['name'] ); |
||
| 317 | $this->assertEquals( 'Recount terms', $data['action'] ); |
||
| 318 | $this->assertEquals( 'This tool will recount product terms - useful when changing your settings in a way which hides products from the catalog.', $data['description'] ); |
||
| 319 | $this->assertTrue( $data['success'] ); |
||
| 320 | $this->assertEquals( 1, did_action( 'woocommerce_rest_insert_system_status_tool' ) ); |
||
| 321 | |||
| 322 | $response = $this->server->dispatch( new WP_REST_Request( 'POST', '/wc/v2/system_status/tools/not_a_real_tool' ) ); |
||
| 323 | $this->assertEquals( 404, $response->get_status() ); |
||
| 324 | } |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Test to make sure a tool cannot be run without valid creds. |
||
| 328 | * |
||
| 329 | * @since 3.0.0 |
||
| 330 | */ |
||
| 331 | public function test_execute_system_status_tool_without_permission() { |
||
| 332 | wp_set_current_user( 0 ); |
||
| 333 | $response = $this->server->dispatch( new WP_REST_Request( 'POST', '/wc/v2/system_status/tools/recount_terms' ) ); |
||
| 334 | $this->assertEquals( 401, $response->get_status() ); |
||
| 335 | } |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Test system status schema. |
||
| 339 | * |
||
| 340 | * @since 3.0.0 |
||
| 341 | */ |
||
| 342 | public function test_system_status_tool_schema() { |
||
| 343 | $request = new WP_REST_Request( 'OPTIONS', '/wc/v2/system_status/tools' ); |
||
| 344 | $response = $this->server->dispatch( $request ); |
||
| 345 | $data = $response->get_data(); |
||
| 346 | $properties = $data['schema']['properties']; |
||
| 347 | |||
| 348 | $this->assertEquals( 6, count( $properties ) ); |
||
| 349 | $this->assertArrayHasKey( 'id', $properties ); |
||
| 350 | $this->assertArrayHasKey( 'name', $properties ); |
||
| 351 | $this->assertArrayHasKey( 'action', $properties ); |
||
| 352 | $this->assertArrayHasKey( 'description', $properties ); |
||
| 353 | $this->assertArrayHasKey( 'success', $properties ); |
||
| 354 | $this->assertArrayHasKey( 'message', $properties ); |
||
| 355 | } |
||
| 356 | } |
||
| 357 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths