Issues (334)

unit-tests/Tests/Version3/system-status.php (4 issues)

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.5.0
13
 */
14
class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
0 ignored issues
show
The type WC_REST_Unit_Test_Case was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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
Bug Best Practice introduced by
The property endpoint does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
22
		$this->user     = $this->factory->user->create(
0 ignored issues
show
Bug Best Practice introduced by
The property user does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
23
			array(
24
				'role' => 'administrator',
25
			)
26
		);
27
28
		// Callback used by WP_HTTP_TestCase to decide whether to perform HTTP requests or to provide a mocked response.
29
		$this->http_responder = array( $this, 'mock_http_responses' );
0 ignored issues
show
Bug Best Practice introduced by
The property http_responder does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
30
	}
31
32
	/**
33
	 * Test route registration.
34
	 */
35
	public function test_register_routes() {
36
		$routes = $this->server->get_routes();
37
		$this->assertArrayHasKey( '/wc/v3/system_status', $routes );
38
		$this->assertArrayHasKey( '/wc/v3/system_status/tools', $routes );
39
		$this->assertArrayHasKey( '/wc/v3/system_status/tools/(?P<id>[\w-]+)', $routes );
40
	}
41
42
	/**
43
	 * Test to make sure system status cannot be accessed without valid creds
44
	 *
45
	 * @since 3.5.0
46
	 */
47
	public function test_get_system_status_info_without_permission() {
48
		wp_set_current_user( 0 );
49
		$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/system_status' ) );
50
		$this->assertEquals( 401, $response->get_status() );
51
	}
52
53
	/**
54
	 * Test to make sure root properties are present.
55
	 * (environment, theme, database, etc).
56
	 *
57
	 * @since 3.5.0
58
	 */
59
	public function test_get_system_status_info_returns_root_properties() {
60
		wp_set_current_user( $this->user );
61
		$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/system_status' ) );
62
		$data     = $response->get_data();
63
64
		$this->assertArrayHasKey( 'environment', $data );
65
		$this->assertArrayHasKey( 'database', $data );
66
		$this->assertArrayHasKey( 'active_plugins', $data );
67
		$this->assertArrayHasKey( 'theme', $data );
68
		$this->assertArrayHasKey( 'settings', $data );
69
		$this->assertArrayHasKey( 'security', $data );
70
		$this->assertArrayHasKey( 'pages', $data );
71
	}
72
73
	/**
74
	 * Test to make sure environment response is correct.
75
	 *
76
	 * @since 3.5.0
77
	 */
78
	public function test_get_system_status_info_environment() {
79
		wp_set_current_user( $this->user );
80
		$response    = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/system_status' ) );
81
		$data        = $response->get_data();
82
		$environment = (array) $data['environment'];
83
84
		// Make sure all expected data is present.
85
		$this->assertEquals( 32, count( $environment ) );
86
87
		// Test some responses to make sure they match up.
88
		$this->assertEquals( get_option( 'home' ), $environment['home_url'] );
89
		$this->assertEquals( get_option( 'siteurl' ), $environment['site_url'] );
90
		$this->assertEquals( WC()->version, $environment['version'] );
91
	}
92
93
	/**
94
	 * Test to make sure database response is correct.
95
	 *
96
	 * @since 3.5.0
97
	 */
98
	public function test_get_system_status_info_database() {
99
		global $wpdb;
100
		wp_set_current_user( $this->user );
101
		$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/system_status' ) );
102
		$data     = $response->get_data();
103
		$database = (array) $data['database'];
104
105
		$this->assertEquals( get_option( 'woocommerce_db_version' ), $database['wc_database_version'] );
106
		$this->assertEquals( $wpdb->prefix, $database['database_prefix'] );
107
		$this->assertEquals( WC_Geolocation::get_local_database_path(), $database['maxmind_geoip_database'] );
108
		$this->assertArrayHasKey( 'woocommerce', $database['database_tables'], wc_print_r( $database, true ) );
109
		$this->assertArrayHasKey( $wpdb->prefix . 'woocommerce_payment_tokens', $database['database_tables']['woocommerce'], wc_print_r( $database, true ) );
110
	}
111
112
	/**
113
	 * Test to make sure active plugins response is correct.
114
	 *
115
	 * @since 3.5.0
116
	 */
117
	public function test_get_system_status_info_active_plugins() {
118
		wp_set_current_user( $this->user );
119
120
		$actual_plugins = array( 'hello.php' );
121
		update_option( 'active_plugins', $actual_plugins );
122
		$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/system_status' ) );
123
		update_option( 'active_plugins', array() );
124
125
		$data    = $response->get_data();
126
		$plugins = (array) $data['active_plugins'];
127
128
		$this->assertEquals( 1, count( $plugins ) );
129
		$this->assertEquals( 'Hello Dolly', $plugins[0]['name'] );
130
	}
131
132
	/**
133
	 * Test to make sure theme response is correct.
134
	 *
135
	 * @since 3.5.0
136
	 */
137
	public function test_get_system_status_info_theme() {
138
		wp_set_current_user( $this->user );
139
		$active_theme = wp_get_theme();
140
141
		$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/system_status' ) );
142
		$data     = $response->get_data();
143
		$theme    = (array) $data['theme'];
144
145
		$this->assertEquals( 13, count( $theme ) );
146
		$this->assertEquals( $active_theme->Name, $theme['name'] ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.NotSnakeCaseMemberVar
147
	}
148
149
	/**
150
	 * Test to make sure settings response is correct.
151
	 *
152
	 * @since 3.5.0
153
	 */
154
	public function test_get_system_status_info_settings() {
155
		wp_set_current_user( $this->user );
156
157
		$term_response = array();
158
		$terms         = get_terms( 'product_type', array( 'hide_empty' => 0 ) );
159
		foreach ( $terms as $term ) {
160
			$term_response[ $term->slug ] = strtolower( $term->name );
161
		}
162
163
		$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/system_status' ) );
164
		$data     = $response->get_data();
165
		$settings = (array) $data['settings'];
166
167
		$this->assertEquals( 12, count( $settings ) );
168
		$this->assertEquals( ( 'yes' === get_option( 'woocommerce_api_enabled' ) ), $settings['api_enabled'] );
169
		$this->assertEquals( get_woocommerce_currency(), $settings['currency'] );
170
		$this->assertEquals( $term_response, $settings['taxonomies'] );
171
	}
172
173
	/**
174
	 * Test to make sure security response is correct.
175
	 *
176
	 * @since 3.5.0
177
	 */
178
	public function test_get_system_status_info_security() {
179
		wp_set_current_user( $this->user );
180
181
		$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/system_status' ) );
182
		$data     = $response->get_data();
183
		$settings = (array) $data['security'];
184
185
		$this->assertEquals( 2, count( $settings ) );
186
		$this->assertEquals( 'https' === substr( wc_get_page_permalink( 'shop' ), 0, 5 ), $settings['secure_connection'] );
187
		$this->assertEquals( ! ( defined( 'WP_DEBUG' ) && defined( 'WP_DEBUG_DISPLAY' ) && WP_DEBUG && WP_DEBUG_DISPLAY ) || 0 === intval( ini_get( 'display_errors' ) ), $settings['hide_errors'] );
188
	}
189
190
	/**
191
	 * Test to make sure pages response is correct.
192
	 *
193
	 * @since 3.5.0
194
	 */
195
	public function test_get_system_status_info_pages() {
196
		wp_set_current_user( $this->user );
197
		$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/system_status' ) );
198
		$data     = $response->get_data();
199
		$pages    = $data['pages'];
200
		$this->assertEquals( 5, count( $pages ) );
201
	}
202
203
	/**
204
	 * Test system status schema.
205
	 *
206
	 * @since 3.5.0
207
	 */
208
	public function test_system_status_schema() {
209
		$request    = new WP_REST_Request( 'OPTIONS', '/wc/v3/system_status' );
210
		$response   = $this->server->dispatch( $request );
211
		$data       = $response->get_data();
212
		$properties = $data['schema']['properties'];
213
		$this->assertEquals( 9, count( $properties ) );
214
		$this->assertArrayHasKey( 'environment', $properties );
215
		$this->assertArrayHasKey( 'database', $properties );
216
		$this->assertArrayHasKey( 'active_plugins', $properties );
217
		$this->assertArrayHasKey( 'theme', $properties );
218
		$this->assertArrayHasKey( 'settings', $properties );
219
		$this->assertArrayHasKey( 'security', $properties );
220
		$this->assertArrayHasKey( 'pages', $properties );
221
	}
222
223
	/**
224
	 * Test to make sure get_items (all tools) response is correct.
225
	 *
226
	 * @since 3.5.0
227
	 */
228
	public function test_get_system_tools() {
229
		wp_set_current_user( $this->user );
230
231
		$tools_controller = new WC_REST_System_Status_Tools_Controller();
232
		$raw_tools        = $tools_controller->get_tools();
233
234
		$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/system_status/tools' ) );
235
		$data     = $response->get_data();
236
237
		$this->assertEquals( 200, $response->get_status() );
238
		$this->assertEquals( count( $raw_tools ), count( $data ) );
239
		$this->assertContains(
240
			array(
241
				'id'          => 'regenerate_thumbnails',
242
				'name'        => 'Regenerate shop thumbnails',
243
				'action'      => 'Regenerate',
244
				'description' => 'This will regenerate all shop thumbnails to match your theme and/or image settings.',
245
				'_links'      => array(
246
					'item' => array(
247
						array(
248
							'href'       => rest_url( '/wc/v3/system_status/tools/regenerate_thumbnails' ),
249
							'embeddable' => 1,
250
						),
251
					),
252
				),
253
			),
254
			$data
255
		);
256
257
		$query_params = array(
258
			'_fields' => 'id,name,nonexisting',
259
		);
260
		$request      = new WP_REST_Request( 'GET', '/wc/v3/system_status/tools' );
261
		$request->set_query_params( $query_params );
262
		$response = $this->server->dispatch( $request );
263
		$data     = $response->get_data();
264
265
		$this->assertEquals( 200, $response->get_status() );
266
		$this->assertEquals( count( $raw_tools ), count( $data ) );
267
		$this->assertContains(
268
			array(
269
				'id'   => 'regenerate_thumbnails',
270
				'name' => 'Regenerate shop thumbnails',
271
			),
272
			$data
273
		);
274
		foreach ( $data as $item ) {
275
			// Fields that are not requested are not returned in response.
276
			$this->assertArrayNotHasKey( 'action', $item );
277
			$this->assertArrayNotHasKey( 'description', $item );
278
			// Links are part of data in collections, so excluded if not explicitly requested.
279
			$this->assertArrayNotHasKey( '_links', $item );
280
			// Non existing field is ignored.
281
			$this->assertArrayNotHasKey( 'nonexisting', $item );
282
		}
283
284
		// Links are part of data, not links in collections.
285
		$links = $response->get_links();
286
		$this->assertEquals( 0, count( $links ) );
287
	}
288
289
	/**
290
	 * Test to make sure system status tools cannot be accessed without valid creds
291
	 *
292
	 * @since 3.5.0
293
	 */
294
	public function test_get_system_status_tools_without_permission() {
295
		wp_set_current_user( 0 );
296
		$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/system_status/tools' ) );
297
		$this->assertEquals( 401, $response->get_status() );
298
	}
299
300
	/**
301
	 * Test to make sure we can load a single tool correctly.
302
	 *
303
	 * @since 3.5.0
304
	 */
305
	public function test_get_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( 'GET', '/wc/v3/system_status/tools/recount_terms' ) );
313
		$data     = $response->get_data();
314
315
		$this->assertEquals( 200, $response->get_status() );
316
317
		$this->assertEquals( 'recount_terms', $data['id'] );
318
		$this->assertEquals( 'Term counts', $data['name'] );
319
		$this->assertEquals( 'Recount terms', $data['action'] );
320
		$this->assertEquals( 'This tool will recount product terms - useful when changing your settings in a way which hides products from the catalog.', $data['description'] );
321
322
		// Test for _fields query parameter.
323
		$query_params = array(
324
			'_fields' => 'id,name,nonexisting',
325
		);
326
		$request      = new WP_REST_Request( 'GET', '/wc/v3/system_status/tools/recount_terms' );
327
		$request->set_query_params( $query_params );
328
		$response = $this->server->dispatch( $request );
329
		$data     = $response->get_data();
330
331
		$this->assertEquals( 200, $response->get_status() );
332
333
		$this->assertEquals( 'recount_terms', $data['id'] );
334
		$this->assertEquals( 'Term counts', $data['name'] );
335
		$this->assertArrayNotHasKey( 'action', $data );
336
		$this->assertArrayNotHasKey( 'description', $data );
337
		// Links are part of links, not data in single items.
338
		$this->assertArrayNotHasKey( '_links', $data );
339
340
		// Links are part of links, not data in single item response.
341
		$links = $response->get_links();
342
		$this->assertEquals( 1, count( $links ) );
343
	}
344
345
	/**
346
	 * Test to make sure a single system status toolscannot be accessed without valid creds.
347
	 *
348
	 * @since 3.5.0
349
	 */
350
	public function test_get_system_status_tool_without_permission() {
351
		wp_set_current_user( 0 );
352
		$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/system_status/tools/recount_terms' ) );
353
		$this->assertEquals( 401, $response->get_status() );
354
	}
355
356
	/**
357
	 * Test to make sure we can RUN a tool correctly.
358
	 *
359
	 * @since 3.5.0
360
	 */
361
	public function test_execute_system_tool() {
362
		wp_set_current_user( $this->user );
363
364
		$tools_controller = new WC_REST_System_Status_Tools_Controller();
365
		$raw_tools        = $tools_controller->get_tools();
366
		$raw_tool         = $raw_tools['recount_terms'];
367
368
		$response = $this->server->dispatch( new WP_REST_Request( 'POST', '/wc/v3/system_status/tools/recount_terms' ) );
369
		$data     = $response->get_data();
370
371
		$this->assertEquals( 'recount_terms', $data['id'] );
372
		$this->assertEquals( 'Term counts', $data['name'] );
373
		$this->assertEquals( 'Recount terms', $data['action'] );
374
		$this->assertEquals( 'This tool will recount product terms - useful when changing your settings in a way which hides products from the catalog.', $data['description'] );
375
		$this->assertTrue( $data['success'] );
376
		$this->assertEquals( 1, did_action( 'woocommerce_rest_insert_system_status_tool' ) );
377
378
		$response = $this->server->dispatch( new WP_REST_Request( 'POST', '/wc/v3/system_status/tools/not_a_real_tool' ) );
379
		$this->assertEquals( 404, $response->get_status() );
380
381
		// Test _fields for execute system tool request.
382
		$query_params = array(
383
			'_fields' => 'id,success,nonexisting',
384
		);
385
		$request      = new WP_REST_Request( 'PUT', '/wc/v3/system_status/tools/recount_terms' );
386
		$request->set_query_params( $query_params );
387
		$response = $this->server->dispatch( $request );
388
		$data     = $response->get_data();
389
390
		$this->assertEquals( 200, $response->get_status() );
391
		$this->assertEquals( 'recount_terms', $data['id'] );
392
		$this->assertTrue( $data['success'] );
393
394
		// Fields that are not requested are not returned in response.
395
		$this->assertArrayNotHasKey( 'action', $data );
396
		$this->assertArrayNotHasKey( 'name', $data );
397
		$this->assertArrayNotHasKey( 'description', $data );
398
		// Links are part of links, not data in single item response.
399
		$this->assertArrayNotHasKey( '_links', $data );
400
		// Non existing field is ignored.
401
		$this->assertArrayNotHasKey( 'nonexisting', $data );
402
403
		// Links are part of links, not data in single item response.
404
		$links = $response->get_links();
405
		$this->assertEquals( 1, count( $links ) );
406
	}
407
408
	/**
409
	 * Test to make sure a tool cannot be run without valid creds.
410
	 *
411
	 * @since 3.5.0
412
	 */
413
	public function test_execute_system_status_tool_without_permission() {
414
		wp_set_current_user( 0 );
415
		$response = $this->server->dispatch( new WP_REST_Request( 'POST', '/wc/v3/system_status/tools/recount_terms' ) );
416
		$this->assertEquals( 401, $response->get_status() );
417
	}
418
419
	/**
420
	 * Test system status schema.
421
	 *
422
	 * @since 3.5.0
423
	 */
424
	public function test_system_status_tool_schema() {
425
		$request    = new WP_REST_Request( 'OPTIONS', '/wc/v3/system_status/tools' );
426
		$response   = $this->server->dispatch( $request );
427
		$data       = $response->get_data();
428
		$properties = $data['schema']['properties'];
429
430
		$this->assertEquals( 6, count( $properties ) );
431
		$this->assertArrayHasKey( 'id', $properties );
432
		$this->assertArrayHasKey( 'name', $properties );
433
		$this->assertArrayHasKey( 'action', $properties );
434
		$this->assertArrayHasKey( 'description', $properties );
435
		$this->assertArrayHasKey( 'success', $properties );
436
		$this->assertArrayHasKey( 'message', $properties );
437
	}
438
439
	/**
440
	 * Provides a mocked response for external requests performed by WC_REST_System_Status_Controller.
441
	 * This way it is not necessary to perform a regular request to an external server which would
442
	 * significantly slow down the tests.
443
	 *
444
	 * This function is called by WP_HTTP_TestCase::http_request_listner().
445
	 *
446
	 * @param array  $request Request arguments.
447
	 * @param string $url URL of the request.
448
	 *
449
	 * @return array|false mocked response or false to let WP perform a regular request.
450
	 */
451
	protected function mock_http_responses( $request, $url ) {
452
		$mocked_response = false;
453
454
		if ( in_array( $url, array( 'https://www.paypal.com/cgi-bin/webscr', 'https://woocommerce.com/wc-api/product-key-api?request=ping&network=0' ), true ) ) {
455
			$mocked_response = array(
456
				'response' => array( 'code' => 200 ),
457
			);
458
		} elseif ( 'https://api.wordpress.org/themes/info/1.0/' === $url ) {
459
			$mocked_response = array(
460
				'body'     => 'O:8:"stdClass":12:{s:4:"name";s:7:"Default";s:4:"slug";s:7:"default";s:7:"version";s:5:"1.7.2";s:11:"preview_url";s:29:"https://wp-themes.com/default";s:6:"author";s:15:"wordpressdotorg";s:14:"screenshot_url";s:61:"//ts.w.org/wp-content/themes/default/screenshot.png?ver=1.7.2";s:6:"rating";d:100;s:11:"num_ratings";s:1:"3";s:10:"downloaded";i:296618;s:12:"last_updated";s:10:"2010-06-14";s:8:"homepage";s:37:"https://wordpress.org/themes/default/";s:13:"download_link";s:55:"https://downloads.wordpress.org/theme/default.1.7.2.zip";}',
461
				'response' => array( 'code' => 200 ),
462
			);
463
		}
464
465
		return $mocked_response;
466
	}
467
}
468