diff --git a/includes/classes/HealthCheck/HealthCheckElasticsearch.php b/includes/classes/HealthCheck/HealthCheckElasticsearch.php index 7f2328711d..74aab0f6b5 100644 --- a/includes/classes/HealthCheck/HealthCheckElasticsearch.php +++ b/includes/classes/HealthCheck/HealthCheckElasticsearch.php @@ -13,7 +13,9 @@ use ElasticPress\Elasticsearch as Elasticsearch; if ( ! defined( 'ABSPATH' ) ) { + // @codeCoverageIgnoreStart exit; // Exit if accessed directly. + // @codeCoverageIgnoreEnd } /** diff --git a/tests/php/TestHealthCheckElasticsearch.php b/tests/php/TestHealthCheckElasticsearch.php new file mode 100644 index 0000000000..9750ba9b9c --- /dev/null +++ b/tests/php/TestHealthCheckElasticsearch.php @@ -0,0 +1,141 @@ +assertArrayHasKey( 'elasticpress-health-check-elasticsearch', $tests['async'] ); + } + + /** + * Test ajax output. + */ + public function testAjaxOutput() { + $admin_id = $this->factory->user->create( array( 'role' => 'administrator' ) ); + wp_set_current_user( $admin_id ); + + // Make the request. + try { + $this->_handleAjax( 'health-check-elasticpress-health-check-elasticsearch' ); + } catch ( WPAjaxDieContinueException $e ) { + unset( $e ); + } + + $response = json_decode( $this->_last_response, true ); + + $this->assertTrue( $response['success'] ); + $this->assertEquals( 'Your site can connect to Elasticsearch.', $response['data']['label'] ); + $this->assertEquals( 'good', $response['data']['status'] ); + $this->assertEquals( 'ElasticPress', $response['data']['badge']['label'] ); + $this->assertEquals( 'green', $response['data']['badge']['color'] ); + } + + /** + * Test ajax output when host is not set. + */ + public function testAjaxOutPutWhenHostIsNotSet() { + $admin_id = $this->factory->user->create( array( 'role' => 'administrator' ) ); + wp_set_current_user( $admin_id ); + + add_filter( 'ep_host', '__return_empty_string' ); + + // Make the request. + try { + $this->_handleAjax( 'health-check-elasticpress-health-check-elasticsearch' ); + } catch ( WPAjaxDieContinueException $e ) { + unset( $e ); + } + + $response = json_decode( $this->_last_response, true ); + + $this->assertTrue( $response['success'] ); + $this->assertEquals( 'Your site could not connect to Elasticsearch', $response['data']['label'] ); + $this->assertEquals( 'critical', $response['data']['status'] ); + $this->assertEquals( 'ElasticPress', $response['data']['badge']['label'] ); + $this->assertEquals( 'red', $response['data']['badge']['color'] ); + $this->assertEquals( 'The Elasticsearch host is not set.', $response['data']['description'] ); + } + + /** + * Test ajax output when host is not valid. + */ + public function testAjaxOutPutWhenHostIsNotValid() { + $admin_id = $this->factory->user->create( array( 'role' => 'administrator' ) ); + wp_set_current_user( $admin_id ); + + add_filter( 'ep_elasticsearch_version', '__return_false' ); + + // Make the request. + try { + $this->_handleAjax( 'health-check-elasticpress-health-check-elasticsearch' ); + } catch ( WPAjaxDieContinueException $e ) { + unset( $e ); + } + + $response = json_decode( $this->_last_response, true ); + + $this->assertTrue( $response['success'] ); + $this->assertEquals( 'Your site could not connect to Elasticsearch', $response['data']['label'] ); + $this->assertEquals( 'critical', $response['data']['status'] ); + $this->assertEquals( 'ElasticPress', $response['data']['badge']['label'] ); + $this->assertEquals( 'red', $response['data']['badge']['color'] ); + $this->assertEquals( 'Check if your Elasticsearch host URL is correct and you have the right access to the host.', $response['data']['description'] ); + } + + /** + * Test ajax output when elasticpress.io host is not valid. + */ + public function testAjaxOutPutWhenEpioHostIsNotValid() { + $admin_id = $this->factory->user->create( array( 'role' => 'administrator' ) ); + wp_set_current_user( $admin_id ); + + $ep_host = function () { + return 'elasticpress.io/random-string'; + }; + add_filter( 'ep_host', $ep_host ); + add_filter( 'ep_elasticsearch_version', '__return_false' ); + + // Make the request. + try { + $this->_handleAjax( 'health-check-elasticpress-health-check-elasticsearch' ); + } catch ( WPAjaxDieContinueException $e ) { + unset( $e ); + } + + $response = json_decode( $this->_last_response, true ); + + $this->assertTrue( $response['success'] ); + $this->assertEquals( 'Your site could not connect to Elasticsearch', $response['data']['label'] ); + $this->assertEquals( 'critical', $response['data']['status'] ); + $this->assertEquals( 'ElasticPress', $response['data']['badge']['label'] ); + $this->assertEquals( 'red', $response['data']['badge']['color'] ); + $this->assertEquals( 'Check if your credentials to ElasticPress.io host are correct.', $response['data']['description'] ); + + remove_filter( 'ep_host', $ep_host ); + remove_filter( 'ep_elasticsearch_version', '__return_false' ); + + // refetch the elasticsearch version. This is needed because this test has changed the value. + Elasticsearch::factory()->get_elasticsearch_version( true ); + } + +}