-
Notifications
You must be signed in to change notification settings - Fork 314
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Health Check Elasticsearch tests #3213
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
<?php | ||
/** | ||
* Test health check elasticsearch functionality. | ||
* | ||
* @since 4.4.1 | ||
* @package elasticpress | ||
*/ | ||
|
||
namespace ElasticPressTest; | ||
|
||
use ElasticPress\Elasticsearch; | ||
use WP_Site_Health; | ||
use WP_Ajax_UnitTestCase; | ||
use WPAjaxDieContinueException; | ||
|
||
/** | ||
* Health check elasticsearch test class | ||
*/ | ||
class TestHealthCheckElasticsearch extends WP_Ajax_UnitTestCase { | ||
|
||
/** | ||
* Test if the test is registered | ||
*/ | ||
public function testIsRegistered() { | ||
$tests = WP_Site_Health::get_tests(); | ||
|
||
$this->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->assertEquals( true, $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->assertEquals( true, $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'] ); | ||
|
||
remove_filter( 'ep_host', '__return_empty_string' ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the test suite removes all filters in the end of each test execution (at least WP core does.) Do you mind testing if we really need to remove it @burhandodhy? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The tests are working fine without removing the filters. Most of the tests involve removing the filters at the end. Do you mind if I remove |
||
} | ||
|
||
/** | ||
* 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->assertEquals( true, $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'] ); | ||
|
||
remove_filter( 'ep_elasticsearch_version', '__return_false' ); | ||
} | ||
|
||
/** | ||
* 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->assertEquals( true, $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 ); | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we change this to
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done