Fix tests by casting to array

This commit is contained in:
Mike Jolley 2016-12-07 11:37:11 +00:00
parent d9420e01d2
commit 6a7ceab462
1 changed files with 262 additions and 262 deletions

View File

@ -6,156 +6,156 @@
*/ */
class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case { class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
/** /**
* Setup our test server. * Setup our test server.
*/ */
public function setUp() { public function setUp() {
parent::setUp(); parent::setUp();
$this->endpoint = new WC_REST_System_Status_Controller(); $this->endpoint = new WC_REST_System_Status_Controller();
$this->user = $this->factory->user->create( array( $this->user = $this->factory->user->create( array(
'role' => 'administrator', 'role' => 'administrator',
) ); ) );
} }
/** /**
* Test route registration. * Test route registration.
*/ */
public function test_register_routes() { public function test_register_routes() {
$routes = $this->server->get_routes(); $routes = $this->server->get_routes();
$this->assertArrayHasKey( '/wc/v1/system_status', $routes ); $this->assertArrayHasKey( '/wc/v1/system_status', $routes );
$this->assertArrayHasKey( '/wc/v1/system_status/tools', $routes ); $this->assertArrayHasKey( '/wc/v1/system_status/tools', $routes );
$this->assertArrayHasKey( '/wc/v1/system_status/tools/(?P<id>[\w-]+)', $routes ); $this->assertArrayHasKey( '/wc/v1/system_status/tools/(?P<id>[\w-]+)', $routes );
} }
/** /**
* Test to make sure system status cannot be accessed without valid creds * Test to make sure system status cannot be accessed without valid creds
* *
* @since 2.7.0 * @since 2.7.0
*/ */
public function test_get_system_status_info_without_permission() { public function test_get_system_status_info_without_permission() {
wp_set_current_user( 0 ); wp_set_current_user( 0 );
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/system_status' ) ); $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/system_status' ) );
$this->assertEquals( 401, $response->get_status() ); $this->assertEquals( 401, $response->get_status() );
} }
/** /**
* Test to make sure root properties are present. * Test to make sure root properties are present.
* (environment, theme, database, etc). * (environment, theme, database, etc).
* *
* @since 2.7.0 * @since 2.7.0
*/ */
public function test_get_system_status_info_returns_root_properties() { public function test_get_system_status_info_returns_root_properties() {
wp_set_current_user( $this->user ); wp_set_current_user( $this->user );
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/system_status' ) ); $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/system_status' ) );
$data = $response->get_data(); $data = $response->get_data();
$this->assertArrayHasKey( 'environment', $data ); $this->assertArrayHasKey( 'environment', $data );
$this->assertArrayHasKey( 'database', $data ); $this->assertArrayHasKey( 'database', $data );
$this->assertArrayHasKey( 'active_plugins', $data ); $this->assertArrayHasKey( 'active_plugins', $data );
$this->assertArrayHasKey( 'theme', $data ); $this->assertArrayHasKey( 'theme', $data );
$this->assertArrayHasKey( 'settings', $data ); $this->assertArrayHasKey( 'settings', $data );
$this->assertArrayHasKey( 'security', $data ); $this->assertArrayHasKey( 'security', $data );
$this->assertArrayHasKey( 'pages', $data ); $this->assertArrayHasKey( 'pages', $data );
} }
/** /**
* Test to make sure environment response is correct. * Test to make sure environment response is correct.
* *
* @since 2.7.0 * @since 2.7.0
*/ */
public function test_get_system_status_info_environment() { public function test_get_system_status_info_environment() {
wp_set_current_user( $this->user ); wp_set_current_user( $this->user );
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/system_status' ) ); $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/system_status' ) );
$data = $response->get_data(); $data = $response->get_data();
$environment = $data['environment']; $environment = (array) $data['environment'];
// Make sure all expected data is present // Make sure all expected data is present
$this->assertEquals( 30, count( $environment ) ); $this->assertEquals( 30, count( $environment ) );
// Test some responses to make sure they match up // Test some responses to make sure they match up
$this->assertEquals( get_option( 'home' ), $environment['home_url'] ); $this->assertEquals( get_option( 'home' ), $environment['home_url'] );
$this->assertEquals( get_option( 'siteurl' ), $environment['site_url'] ); $this->assertEquals( get_option( 'siteurl' ), $environment['site_url'] );
$this->assertEquals( WC()->version, $environment['version'] ); $this->assertEquals( WC()->version, $environment['version'] );
} }
/** /**
* Test to make sure database response is correct. * Test to make sure database response is correct.
* *
* @since 2.7.0 * @since 2.7.0
*/ */
public function test_get_system_status_info_database() { public function test_get_system_status_info_database() {
global $wpdb; global $wpdb;
wp_set_current_user( $this->user ); wp_set_current_user( $this->user );
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/system_status' ) ); $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/system_status' ) );
$data = $response->get_data(); $data = $response->get_data();
$database = $data['database']; $database = (array) $data['database'];
$this->assertEquals( get_option( 'woocommerce_db_version' ), $database['wc_database_version'] ); $this->assertEquals( get_option( 'woocommerce_db_version' ), $database['wc_database_version'] );
$this->assertEquals( $wpdb->prefix, $database['database_prefix'] ); $this->assertEquals( $wpdb->prefix, $database['database_prefix'] );
$this->assertEquals( WC_Geolocation::get_local_database_path(), $database['maxmind_geoip_database'] ); $this->assertEquals( WC_Geolocation::get_local_database_path(), $database['maxmind_geoip_database'] );
$this->assertArrayHasKey( 'woocommerce_payment_tokens', $database['database_tables'] ); $this->assertArrayHasKey( 'woocommerce_payment_tokens', $database['database_tables'] );
} }
/** /**
* Test to make sure active plugins response is correct. * Test to make sure active plugins response is correct.
* *
* @since 2.7.0 * @since 2.7.0
*/ */
public function test_get_system_status_info_active_plugins() { public function test_get_system_status_info_active_plugins() {
wp_set_current_user( $this->user ); wp_set_current_user( $this->user );
$actual_plugins = array( 'hello.php' ); $actual_plugins = array( 'hello.php' );
update_option( 'active_plugins', $actual_plugins ); update_option( 'active_plugins', $actual_plugins );
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/system_status' ) ); $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/system_status' ) );
update_option( 'active_plugins', array() ); update_option( 'active_plugins', array() );
$data = $response->get_data(); $data = $response->get_data();
$plugins = $data['active_plugins']; $plugins = (array) $data['active_plugins'];
$this->assertEquals( 1, count( $plugins ) ); $this->assertEquals( 1, count( $plugins ) );
$this->assertEquals( 'Hello Dolly', $plugins[0]['name'] ); $this->assertEquals( 'Hello Dolly', $plugins[0]['name'] );
} }
/** /**
* Test to make sure theme response is correct. * Test to make sure theme response is correct.
* *
* @since 2.7.0 * @since 2.7.0
*/ */
public function test_get_system_status_info_theme() { public function test_get_system_status_info_theme() {
wp_set_current_user( $this->user ); wp_set_current_user( $this->user );
$active_theme = wp_get_theme(); $active_theme = wp_get_theme();
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/system_status' ) ); $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/system_status' ) );
$data = $response->get_data(); $data = $response->get_data();
$theme = $data['theme']; $theme = (array) $data['theme'];
$this->assertEquals( 13, count( $theme ) ); $this->assertEquals( 13, count( $theme ) );
$this->assertEquals( $active_theme->Name, $theme['name'] ); $this->assertEquals( $active_theme->Name, $theme['name'] );
} }
/** /**
* Test to make sure settings response is correct. * Test to make sure settings response is correct.
* *
* @since 2.7.0 * @since 2.7.0
*/ */
public function test_get_system_status_info_settings() { public function test_get_system_status_info_settings() {
wp_set_current_user( $this->user ); wp_set_current_user( $this->user );
$term_response = array(); $term_response = array();
$terms = get_terms( 'product_type', array( 'hide_empty' => 0 ) ); $terms = get_terms( 'product_type', array( 'hide_empty' => 0 ) );
foreach ( $terms as $term ) { foreach ( $terms as $term ) {
$term_response[ $term->slug ] = strtolower( $term->name ); $term_response[ $term->slug ] = strtolower( $term->name );
} }
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/system_status' ) ); $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/system_status' ) );
$data = $response->get_data(); $data = $response->get_data();
$settings = $data['settings']; $settings = (array) $data['settings'];
$this->assertEquals( 10, count( $settings ) ); $this->assertEquals( 10, count( $settings ) );
$this->assertEquals( ( 'yes' === get_option( 'woocommerce_api_enabled' ) ), $settings['api_enabled'] ); $this->assertEquals( ( 'yes' === get_option( 'woocommerce_api_enabled' ) ), $settings['api_enabled'] );
$this->assertEquals( get_woocommerce_currency(), $settings['currency'] ); $this->assertEquals( get_woocommerce_currency(), $settings['currency'] );
$this->assertEquals( $term_response, $settings['taxonomies'] ); $this->assertEquals( $term_response, $settings['taxonomies'] );
} }
/** /**
* Test to make sure security response is correct. * Test to make sure security response is correct.
@ -167,62 +167,62 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/system_status' ) ); $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/system_status' ) );
$data = $response->get_data(); $data = $response->get_data();
$settings = $data['security']; $settings = (array) $data['security'];
$this->assertEquals( 2, count( $settings ) ); $this->assertEquals( 2, count( $settings ) );
$this->assertEquals( 'https' === substr( get_permalink( wc_get_page_id( 'shop' ) ), 0, 5 ), $settings['secure_connection'] ); $this->assertEquals( 'https' === substr( get_permalink( wc_get_page_id( 'shop' ) ), 0, 5 ), $settings['secure_connection'] );
$this->assertEquals( ! ( defined( 'WP_DEBUG' ) && defined( 'WP_DEBUG_DISPLAY' ) && WP_DEBUG && WP_DEBUG_DISPLAY ) || 0 === intval( ini_get( 'display_errors' ) ), $settings['hide_errors'] ); $this->assertEquals( ! ( defined( 'WP_DEBUG' ) && defined( 'WP_DEBUG_DISPLAY' ) && WP_DEBUG && WP_DEBUG_DISPLAY ) || 0 === intval( ini_get( 'display_errors' ) ), $settings['hide_errors'] );
} }
/** /**
* Test to make sure pages response is correct. * Test to make sure pages response is correct.
* *
* @since 2.7.0 * @since 2.7.0
*/ */
public function test_get_system_status_info_pages() { public function test_get_system_status_info_pages() {
wp_set_current_user( $this->user ); wp_set_current_user( $this->user );
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/system_status' ) ); $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/system_status' ) );
$data = $response->get_data(); $data = $response->get_data();
$pages = $data['pages']; $pages = $data['pages'];
$this->assertEquals( 4, count( $pages ) ); $this->assertEquals( 4, count( $pages ) );
} }
/** /**
* Test system status schema. * Test system status schema.
* *
* @since 2.7.0 * @since 2.7.0
*/ */
public function test_system_status_schema() { public function test_system_status_schema() {
$request = new WP_REST_Request( 'OPTIONS', '/wc/v1/system_status' ); $request = new WP_REST_Request( 'OPTIONS', '/wc/v1/system_status' );
$response = $this->server->dispatch( $request ); $response = $this->server->dispatch( $request );
$data = $response->get_data(); $data = $response->get_data();
$properties = $data['schema']['properties']; $properties = $data['schema']['properties'];
$this->assertEquals( 7, count( $properties ) ); $this->assertEquals( 7, count( $properties ) );
$this->assertArrayHasKey( 'environment', $properties ); $this->assertArrayHasKey( 'environment', $properties );
$this->assertArrayHasKey( 'database', $properties ); $this->assertArrayHasKey( 'database', $properties );
$this->assertArrayHasKey( 'active_plugins', $properties ); $this->assertArrayHasKey( 'active_plugins', $properties );
$this->assertArrayHasKey( 'theme', $properties ); $this->assertArrayHasKey( 'theme', $properties );
$this->assertArrayHasKey( 'settings', $properties ); $this->assertArrayHasKey( 'settings', $properties );
$this->assertArrayHasKey( 'security', $properties ); $this->assertArrayHasKey( 'security', $properties );
$this->assertArrayHasKey( 'pages', $properties ); $this->assertArrayHasKey( 'pages', $properties );
} }
/** /**
* Test to make sure get_items (all tools) response is correct. * Test to make sure get_items (all tools) response is correct.
* *
* @since 2.7.0 * @since 2.7.0
*/ */
public function test_get_system_tools() { public function test_get_system_tools() {
wp_set_current_user( $this->user ); wp_set_current_user( $this->user );
$tools_controller = new WC_REST_System_Status_Tools_Controller; $tools_controller = new WC_REST_System_Status_Tools_Controller;
$raw_tools = $tools_controller->get_tools(); $raw_tools = $tools_controller->get_tools();
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/system_status/tools' ) ); $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/system_status/tools' ) );
$data = $response->get_data(); $data = $response->get_data();
$this->assertEquals( 200, $response->get_status() ); $this->assertEquals( 200, $response->get_status() );
$this->assertEquals( count( $raw_tools ), count( $data ) ); $this->assertEquals( count( $raw_tools ), count( $data ) );
$this->assertContains( array( $this->assertContains( array(
'id' => 'reset_tracking', 'id' => 'reset_tracking',
@ -238,105 +238,105 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
), ),
), ),
), $data ); ), $data );
} }
/** /**
* Test to make sure system status tools cannot be accessed without valid creds * Test to make sure system status tools cannot be accessed without valid creds
* *
* @since 2.7.0 * @since 2.7.0
*/ */
public function test_get_system_status_tools_without_permission() { public function test_get_system_status_tools_without_permission() {
wp_set_current_user( 0 ); wp_set_current_user( 0 );
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/system_status/tools' ) ); $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/system_status/tools' ) );
$this->assertEquals( 401, $response->get_status() ); $this->assertEquals( 401, $response->get_status() );
} }
/** /**
* Test to make sure we can load a single tool correctly. * Test to make sure we can load a single tool correctly.
* *
* @since 2.7.0 * @since 2.7.0
*/ */
public function test_get_system_tool() { public function test_get_system_tool() {
wp_set_current_user( $this->user ); wp_set_current_user( $this->user );
$tools_controller = new WC_REST_System_Status_Tools_Controller; $tools_controller = new WC_REST_System_Status_Tools_Controller;
$raw_tools = $tools_controller->get_tools(); $raw_tools = $tools_controller->get_tools();
$raw_tool = $raw_tools['recount_terms']; $raw_tool = $raw_tools['recount_terms'];
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/system_status/tools/recount_terms' ) ); $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/system_status/tools/recount_terms' ) );
$data = $response->get_data(); $data = $response->get_data();
$this->assertEquals( 200, $response->get_status() ); $this->assertEquals( 200, $response->get_status() );
$this->assertEquals( 'recount_terms', $data['id'] ); $this->assertEquals( 'recount_terms', $data['id'] );
$this->assertEquals( 'Term counts', $data['name'] ); $this->assertEquals( 'Term counts', $data['name'] );
$this->assertEquals( 'Recount terms', $data['action'] ); $this->assertEquals( 'Recount terms', $data['action'] );
$this->assertEquals( 'This tool will recount product terms - useful when changing your settings in a way which hides products from the catalog.', $data['description'] ); $this->assertEquals( 'This tool will recount product terms - useful when changing your settings in a way which hides products from the catalog.', $data['description'] );
} }
/** /**
* Test to make sure a single system status toolscannot be accessed without valid creds. * Test to make sure a single system status toolscannot be accessed without valid creds.
* *
* @since 2.7.0 * @since 2.7.0
*/ */
public function test_get_system_status_tool_without_permission() { public function test_get_system_status_tool_without_permission() {
wp_set_current_user( 0 ); wp_set_current_user( 0 );
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/system_status/tools/recount_terms' ) ); $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/system_status/tools/recount_terms' ) );
$this->assertEquals( 401, $response->get_status() ); $this->assertEquals( 401, $response->get_status() );
} }
/** /**
* Test to make sure we can RUN a tool correctly. * Test to make sure we can RUN a tool correctly.
* *
* @since 2.7.0 * @since 2.7.0
*/ */
public function test_execute_system_tool() { public function test_execute_system_tool() {
wp_set_current_user( $this->user ); wp_set_current_user( $this->user );
$tools_controller = new WC_REST_System_Status_Tools_Controller; $tools_controller = new WC_REST_System_Status_Tools_Controller;
$raw_tools = $tools_controller->get_tools(); $raw_tools = $tools_controller->get_tools();
$raw_tool = $raw_tools['recount_terms']; $raw_tool = $raw_tools['recount_terms'];
$response = $this->server->dispatch( new WP_REST_Request( 'POST', '/wc/v1/system_status/tools/recount_terms' ) ); $response = $this->server->dispatch( new WP_REST_Request( 'POST', '/wc/v1/system_status/tools/recount_terms' ) );
$data = $response->get_data(); $data = $response->get_data();
$this->assertEquals( 'recount_terms', $data['id'] ); $this->assertEquals( 'recount_terms', $data['id'] );
$this->assertEquals( 'Term counts', $data['name'] ); $this->assertEquals( 'Term counts', $data['name'] );
$this->assertEquals( 'Recount terms', $data['action'] ); $this->assertEquals( 'Recount terms', $data['action'] );
$this->assertEquals( 'This tool will recount product terms - useful when changing your settings in a way which hides products from the catalog.', $data['description'] ); $this->assertEquals( 'This tool will recount product terms - useful when changing your settings in a way which hides products from the catalog.', $data['description'] );
$this->assertTrue( $data['success'] ); $this->assertTrue( $data['success'] );
$response = $this->server->dispatch( new WP_REST_Request( 'POST', '/wc/v1/system_status/tools/not_a_real_tool' ) ); $response = $this->server->dispatch( new WP_REST_Request( 'POST', '/wc/v1/system_status/tools/not_a_real_tool' ) );
$this->assertEquals( 404, $response->get_status() ); $this->assertEquals( 404, $response->get_status() );
} }
/** /**
* Test to make sure a tool cannot be run without valid creds. * Test to make sure a tool cannot be run without valid creds.
* *
* @since 2.7.0 * @since 2.7.0
*/ */
public function test_execute_system_status_tool_without_permission() { public function test_execute_system_status_tool_without_permission() {
wp_set_current_user( 0 ); wp_set_current_user( 0 );
$response = $this->server->dispatch( new WP_REST_Request( 'POST', '/wc/v1/system_status/tools/recount_terms' ) ); $response = $this->server->dispatch( new WP_REST_Request( 'POST', '/wc/v1/system_status/tools/recount_terms' ) );
$this->assertEquals( 401, $response->get_status() ); $this->assertEquals( 401, $response->get_status() );
} }
/** /**
* Test system status schema. * Test system status schema.
* *
* @since 2.7.0 * @since 2.7.0
*/ */
public function test_system_status_tool_schema() { public function test_system_status_tool_schema() {
$request = new WP_REST_Request( 'OPTIONS', '/wc/v1/system_status/tools' ); $request = new WP_REST_Request( 'OPTIONS', '/wc/v1/system_status/tools' );
$response = $this->server->dispatch( $request ); $response = $this->server->dispatch( $request );
$data = $response->get_data(); $data = $response->get_data();
$properties = $data['schema']['properties']; $properties = $data['schema']['properties'];
$this->assertEquals( 6, count( $properties ) ); $this->assertEquals( 6, count( $properties ) );
$this->assertArrayHasKey( 'id', $properties ); $this->assertArrayHasKey( 'id', $properties );
$this->assertArrayHasKey( 'name', $properties ); $this->assertArrayHasKey( 'name', $properties );
$this->assertArrayHasKey( 'action', $properties ); $this->assertArrayHasKey( 'action', $properties );
$this->assertArrayHasKey( 'description', $properties ); $this->assertArrayHasKey( 'description', $properties );
$this->assertArrayHasKey( 'success', $properties ); $this->assertArrayHasKey( 'success', $properties );
$this->assertArrayHasKey( 'message', $properties ); $this->assertArrayHasKey( 'message', $properties );
} }
} }