Skip to content
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

Merged
merged 4 commits into from
Dec 19, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions includes/classes/HealthCheck/HealthCheckElasticsearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
use ElasticPress\Elasticsearch as Elasticsearch;

if ( ! defined( 'ABSPATH' ) ) {
// @codeCoverageIgnoreStart
exit; // Exit if accessed directly.
// @codeCoverageIgnoreEnd
}

/**
Expand Down
145 changes: 145 additions & 0 deletions tests/php/TestHealthCheckElasticsearch.php
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'] );
Copy link
Member

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

Suggested change
$this->assertEquals( true, $response['success'] );
$this->assertTrue( $response['success'] );

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

$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' );
Copy link
Member

Choose a reason for hiding this comment

The 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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 remove_filters from all tests in a separate PR?

}

/**
* 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 );
}

}