Skip to content

Commit

Permalink
Search: Fix tests not running (#2951)
Browse files Browse the repository at this point in the history
* Use constant() for testing purposes

* Account for ABSPATH in testing and add define() to implementation for Automattic\VIP\Search

* Add init_es() helper to reduce bloat and utilize the Constant_Mocker class in Search tests

* HealthJob_Test: Remove tests that are not applicable

* Add description to init_es()
  • Loading branch information
rebeccahum committed Feb 28, 2022
1 parent 129f6fd commit a25820c
Show file tree
Hide file tree
Showing 5 changed files with 284 additions and 391 deletions.
6 changes: 3 additions & 3 deletions search/includes/classes/class-healthjob.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,17 +132,17 @@ public function validate_contents() {
* @return bool True if job is enabled. Else, false
*/
public function is_enabled() {
if ( defined( 'DISABLE_VIP_SEARCH_HEALTHCHECKS' ) && true === DISABLE_VIP_SEARCH_HEALTHCHECKS ) {
if ( defined( 'DISABLE_VIP_SEARCH_HEALTHCHECKS' ) && true === constant( 'DISABLE_VIP_SEARCH_HEALTHCHECKS' ) ) {
return false;
}

if ( defined( 'VIP_GO_APP_ID' ) && in_array( VIP_GO_APP_ID, $this->health_check_disabled_sites, true ) ) {
if ( defined( 'VIP_GO_APP_ID' ) && in_array( constant( 'VIP_GO_APP_ID' ), $this->health_check_disabled_sites, true ) ) {
return false;
}

$enabled_environments = apply_filters( 'vip_search_healthchecks_enabled_environments', array( 'production' ) );

$enabled = in_array( VIP_GO_ENV, $enabled_environments, true );
$enabled = in_array( constant( 'VIP_GO_ENV' ), $enabled_environments, true );

/**
* Filter whether to enable VIP search healthcheck
Expand Down
40 changes: 21 additions & 19 deletions search/includes/classes/class-search.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,9 @@ public function init() {
* @return bool true if constants are defined, false otherwise
*/
public static function are_es_constants_defined() {
$endpoints_defined = defined( 'VIP_ELASTICSEARCH_ENDPOINTS' ) && is_array( VIP_ELASTICSEARCH_ENDPOINTS ) && ! empty( VIP_ELASTICSEARCH_ENDPOINTS );
$username_defined = defined( 'VIP_ELASTICSEARCH_USERNAME' ) && VIP_ELASTICSEARCH_USERNAME;
$password_defined = defined( 'VIP_ELASTICSEARCH_PASSWORD' ) && VIP_ELASTICSEARCH_PASSWORD;
$endpoints_defined = defined( 'VIP_ELASTICSEARCH_ENDPOINTS' ) && is_array( constant( 'VIP_ELASTICSEARCH_ENDPOINTS' ) ) && ! empty( constant( 'VIP_ELASTICSEARCH_ENDPOINTS' ) );
$username_defined = defined( 'VIP_ELASTICSEARCH_USERNAME' ) && constant( 'VIP_ELASTICSEARCH_USERNAME' );
$password_defined = defined( 'VIP_ELASTICSEARCH_PASSWORD' ) && constant( 'VIP_ELASTICSEARCH_PASSWORD' );
return $endpoints_defined && $username_defined && $password_defined;
}

Expand Down Expand Up @@ -440,15 +440,15 @@ protected function setup_constants() {
define( 'EP_SYNC_CHUNK_LIMIT', 500 );
}

if ( ! defined( 'EP_HOST' ) && defined( 'VIP_ELASTICSEARCH_ENDPOINTS' ) && is_array( VIP_ELASTICSEARCH_ENDPOINTS ) ) {
$host = $this->get_random_host( VIP_ELASTICSEARCH_ENDPOINTS );
$this->current_host_index = array_search( $host, VIP_ELASTICSEARCH_ENDPOINTS );
if ( ! defined( 'EP_HOST' ) && defined( 'VIP_ELASTICSEARCH_ENDPOINTS' ) && is_array( constant( 'VIP_ELASTICSEARCH_ENDPOINTS' ) ) ) {
$host = $this->get_random_host( constant( 'VIP_ELASTICSEARCH_ENDPOINTS' ) );
$this->current_host_index = array_search( $host, constant( 'VIP_ELASTICSEARCH_ENDPOINTS' ) );

define( 'EP_HOST', $host );
}

if ( ! defined( 'ES_SHIELD' ) && ( defined( 'VIP_ELASTICSEARCH_USERNAME' ) && defined( 'VIP_ELASTICSEARCH_PASSWORD' ) ) ) {
define( 'ES_SHIELD', sprintf( '%s:%s', VIP_ELASTICSEARCH_USERNAME, VIP_ELASTICSEARCH_PASSWORD ) );
define( 'ES_SHIELD', sprintf( '%s:%s', constant( 'VIP_ELASTICSEARCH_USERNAME' ), constant( 'VIP_ELASTICSEARCH_PASSWORD' ) ) );
}

// Do not allow sync via Dashboard (WP-CLI is preferred for indexing).
Expand Down Expand Up @@ -1148,9 +1148,9 @@ public static function is_query_integration_enabled() {
}

// Legacy constant name
$query_integration_enabled_legacy = defined( 'VIP_ENABLE_ELASTICSEARCH_QUERY_INTEGRATION' ) && true === VIP_ENABLE_ELASTICSEARCH_QUERY_INTEGRATION;
$query_integration_enabled_legacy = defined( 'VIP_ENABLE_ELASTICSEARCH_QUERY_INTEGRATION' ) && true === constant( 'VIP_ENABLE_ELASTICSEARCH_QUERY_INTEGRATION' );

$query_integration_enabled = defined( 'VIP_ENABLE_VIP_SEARCH_QUERY_INTEGRATION' ) && true === VIP_ENABLE_VIP_SEARCH_QUERY_INTEGRATION;
$query_integration_enabled = defined( 'VIP_ENABLE_VIP_SEARCH_QUERY_INTEGRATION' ) && true === constant( 'VIP_ENABLE_VIP_SEARCH_QUERY_INTEGRATION' );

$enabled_by_constant = ( $query_integration_enabled || $query_integration_enabled_legacy );

Expand All @@ -1175,7 +1175,7 @@ public static function is_query_integration_enabled() {
*/
public static function is_network_mode() {
// NOTE - Not using strict equality check here so that we match EP
return defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK;
return defined( 'EP_IS_NETWORK' ) && constant( 'EP_IS_NETWORK' );
}

public static function ep_skip_query_integration( $skip, $query = null ) {
Expand Down Expand Up @@ -1404,11 +1404,11 @@ public function filter__ep_pre_request_host( $host, $failures ) {
return $host;
}

if ( ! is_array( VIP_ELASTICSEARCH_ENDPOINTS ) ) {
if ( ! is_array( constant( 'VIP_ELASTICSEARCH_ENDPOINTS' ) ) ) {
return $host;
}

if ( 0 === count( VIP_ELASTICSEARCH_ENDPOINTS ) ) {
if ( 0 === count( constant( 'VIP_ELASTICSEARCH_ENDPOINTS' ) ) ) {
return $host;
}

Expand Down Expand Up @@ -1793,23 +1793,25 @@ public function filter__epwr_weight() {
public function get_current_host() {
if ( ! defined( 'VIP_ELASTICSEARCH_ENDPOINTS' ) ) {
if ( defined( 'EP_HOST' ) ) {
return EP_HOST;
return constant( 'EP_HOST' );
}

return new \WP_Error( 'vip-search-no-host-found', 'No Elasticsearch hosts found' );
}

if ( ! is_array( VIP_ELASTICSEARCH_ENDPOINTS ) ) {
return VIP_ELASTICSEARCH_ENDPOINTS;
if ( ! is_array( constant( 'VIP_ELASTICSEARCH_ENDPOINTS' ) ) ) {
return constant( 'VIP_ELASTICSEARCH_ENDPOINTS' );
}

if ( ! is_int( $this->current_host_index ) ) {
$this->current_host_index = 0;
}

$this->current_host_index = $this->current_host_index % count( VIP_ELASTICSEARCH_ENDPOINTS );
$this->current_host_index = $this->current_host_index % count( constant( 'VIP_ELASTICSEARCH_ENDPOINTS' ) );

return VIP_ELASTICSEARCH_ENDPOINTS[ $this->current_host_index ];
$endpoints = constant( 'VIP_ELASTICSEARCH_ENDPOINTS' );

return $endpoints[ $this->current_host_index ];
}

/**
Expand Down Expand Up @@ -1945,7 +1947,7 @@ public function filter__vip_search_post_meta_allow_list_defaults( $keys ) {
}

public function is_jetpack_migration() {
return defined( 'VIP_SEARCH_MIGRATION_SOURCE' ) && 'jetpack' === VIP_SEARCH_MIGRATION_SOURCE;
return defined( 'VIP_SEARCH_MIGRATION_SOURCE' ) && 'jetpack' === constant( 'VIP_SEARCH_MIGRATION_SOURCE' );
}

/**
Expand Down Expand Up @@ -2021,7 +2023,7 @@ public function filter__ep_indexable_mapping( $mapping ) {
}

public function get_index_routing_allocation_include_dc() {
$dc = defined( 'VIP_ORIGIN_DATACENTER' ) ? VIP_ORIGIN_DATACENTER : $this->get_origin_dc_from_es_endpoint( $this->get_current_host() );
$dc = defined( 'VIP_ORIGIN_DATACENTER' ) ? constant( 'VIP_ORIGIN_DATACENTER' ) : $this->get_origin_dc_from_es_endpoint( $this->get_current_host() );

$dc = strtolower( $dc );

Expand Down
12 changes: 10 additions & 2 deletions tests/mock-constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@
use InvalidArgumentException;

abstract class Constant_Mocker {
private static $constants = [];
private static $constants = [
'ABSPATH' => '/tmp/wordpress',
];

public static function clear(): void {
self::$constants = [];
self::$constants = [
'ABSPATH' => '/tmp/wordpress',
];
}

public static function define( string $constant, $value ): void {
Expand Down Expand Up @@ -114,4 +118,8 @@ function defined( $constant ) {
function constant( $constant ) {
return Constant_Mocker::constant( $constant );
}

function define( $constant, $value ) {
return Constant_Mocker::define( $constant, $value );
}
}
38 changes: 8 additions & 30 deletions tests/search/includes/classes/test-class-healthjob.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Automattic\VIP\Search;

use WP_UnitTestCase;
use Automattic\Test\Constant_Mocker;

// phpcs:disable WordPress.PHP.DevelopmentFunctions.error_log_var_export

Expand All @@ -18,6 +19,8 @@ public static function setUpBeforeClass(): void {
define( 'VIP_ELASTICSEARCH_ENDPOINTS', array( 'https://elasticsearch:9200' ) );
}

Constant_Mocker::define( 'VIP_GO_ENV', 'test' );

require_once __DIR__ . '/../../../../search/search.php';

\Automattic\VIP\Search\Search::instance();
Expand All @@ -34,37 +37,12 @@ public function setUp(): void {
require_once __DIR__ . '/../../../../search/includes/classes/class-healthjob.php';
}

public function test_vip_search_healthjob_is_not_enabled_when_indexing_is_occuring() {
add_filter( 'ep_is_indexing', '__return_true' );

$job = new \Automattic\VIP\Search\HealthJob( Search::instance() );

$enabled = $job->is_enabled();

$this->assertFalse( $enabled );

remove_filter( 'ep_is_indexing', '__return_true' );
}

public function test_vip_search_healthjob_is_not_enabled_before_first_index() {
add_filter( 'ep_last_sync', '__return_false' );

$job = new \Automattic\VIP\Search\HealthJob( Search::instance() );

$enabled = $job->is_enabled();

$this->assertFalse( $enabled );

remove_filter( 'ep_last_sync', '__return_false' );
}

public function test_vip_search_healthjob_is_enabled_when_expected() {
add_filter( 'ep_is_indexing', '__return_false' );
add_filter( 'ep_last_sync', '__return_true' );

// Have to filter the enabled envs to allow `false`, which is the VIP_GO_ENV in tests
$enabled_environments = function() {
return [ false ];
return [ 'test' ];
};

add_filter( 'vip_search_healthchecks_enabled_environments', $enabled_environments );
Expand All @@ -85,7 +63,7 @@ public function test_vip_search_healthjob_is_enabled_when_expected() {
* @preserveGlobalState disabled
*/
public function test_vip_search_healthjob_is_disabled_when_constant_is_set() {
define( 'DISABLE_VIP_SEARCH_HEALTHCHECKS', true );
Constant_Mocker::define( 'DISABLE_VIP_SEARCH_HEALTHCHECKS', true );

$job = new \Automattic\VIP\Search\HealthJob( Search::instance() );

Expand All @@ -94,15 +72,15 @@ public function test_vip_search_healthjob_is_disabled_when_constant_is_set() {
$this->assertFalse( $enabled );
}

/**
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function test_vip_search_healthjob_is_disabled_when_app_id_matches_disabled_list() {
define( 'VIP_GO_APP_ID', 2341 );
Constant_Mocker::define( 'VIP_GO_APP_ID', 2341 );

$job = new \Automattic\VIP\Search\HealthJob( Search::instance() );
$job->health_check_disabled_sites[] = VIP_GO_APP_ID;
$job->health_check_disabled_sites[] = Constant_Mocker::constant( 'VIP_GO_APP_ID' );

$enabled = $job->is_enabled();

Expand Down

0 comments on commit a25820c

Please sign in to comment.