SystemStatus::test_get_system_status_info_pages()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Class WC_Tests_REST_System_Status file.
4
 *
5
 * @package Automattic/WooCommerce/Tests
6
 */
7
8
namespace Automattic\WooCommerce\RestApi\UnitTests\Tests\Version4;
9
10
defined( 'ABSPATH' ) || exit;
11
12
use \WP_REST_Request;
13
use \WC_REST_Unit_Test_Case;
0 ignored issues
show
Bug introduced by
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...
14
15
/**
16
 * System Status REST Tests.
17
 *
18
 * @package WooCommerce\Tests\API
19
 * @since 3.5.0
20
 */
21
class SystemStatus extends WC_REST_Unit_Test_Case {
22
23
	/**
24
	 * User variable.
25
	 *
26
	 * @var WP_User
0 ignored issues
show
Bug introduced by
The type Automattic\WooCommerce\R...\Tests\Version4\WP_User was not found. Did you mean WP_User? If so, make sure to prefix the type with \.
Loading history...
27
	 */
28
	protected static $user;
29
30
	/**
31
	 * Setup once before running tests.
32
	 *
33
	 * @param object $factory Factory object.
34
	 */
35
	public static function wpSetUpBeforeClass( $factory ) {
36
		self::$user = $factory->user->create(
37
			array(
38
				'role' => 'administrator',
39
			)
40
		);
41
	}
42
43
	/**
44
	 * Setup our test server.
45
	 */
46
	public function setUp() {
47
		parent::setUp();
48
		wp_set_current_user( self::$user );
0 ignored issues
show
Bug introduced by
self::user of type Automattic\WooCommerce\R...\Tests\Version4\WP_User is incompatible with the type integer expected by parameter $id of wp_set_current_user(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

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