Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rebeccahum committed Nov 8, 2022
1 parent c7ad85b commit c82a8d4
Show file tree
Hide file tree
Showing 42 changed files with 5,466 additions and 1,475 deletions.
219 changes: 205 additions & 14 deletions tests/php/TestAdminNotices.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ class TestAdminNotices extends BaseTestCase {
*
* @since 2.2
*/
public function setUp() {
public function set_up() {
global $wpdb;
parent::setUp();
parent::set_up();
$wpdb->suppress_errors();

$admin_id = $this->factory->user->create( array( 'role' => 'administrator' ) );
Expand Down Expand Up @@ -61,8 +61,8 @@ function() {
*
* @since 2.2
*/
public function tearDown() {
parent::tearDown();
public function tear_down() {
parent::tear_down();

// Update since we are deleting to test notifications
update_site_option( 'ep_host', $this->current_host );
Expand Down Expand Up @@ -118,7 +118,19 @@ public function testNoNoticesOnInstall() {
* @since 3.0
*/
public function testNeedSetupNoticeInAdmin() {
// VIP: Remove test
delete_site_option( 'ep_host' );
delete_site_option( 'ep_last_sync' );
delete_site_option( 'ep_need_upgrade_sync', true );
delete_site_option( 'ep_feature_auto_activated_sync' );

ElasticPress\Screen::factory()->set_current_screen( null );

ElasticPress\AdminNotices::factory()->process_notices();

$notices = ElasticPress\AdminNotices::factory()->get_notices();

$this->assertEquals( 1, count( $notices ) );
$this->assertTrue( ! empty( $notices['need_setup'] ) );
}

/**
Expand Down Expand Up @@ -199,7 +211,23 @@ public function testNoSyncNoticeInInstall() {
* @since 3.0
*/
public function testHostErrorNoticeInAdmin() {
// VIP: Remove test
update_site_option( 'ep_host', 'badhost' );
update_site_option( 'ep_last_sync', time() );
delete_site_option( 'ep_need_upgrade_sync', true );
delete_site_option( 'ep_feature_auto_activated_sync' );

remove_all_filters( 'ep_elasticsearch_version' );

ElasticPress\Elasticsearch::factory()->get_elasticsearch_version( true );

ElasticPress\Screen::factory()->set_current_screen( null );

ElasticPress\AdminNotices::factory()->process_notices();

$notices = ElasticPress\AdminNotices::factory()->get_notices();

$this->assertEquals( 1, count( $notices ) );
$this->assertTrue( ! empty( $notices['host_error'] ) );
}

/**
Expand All @@ -219,7 +247,23 @@ public function testHostErrorNoticeInAdmin() {
* @since 3.0
*/
public function testHostErrorNoticeInInstall() {
// VIP: Remove test
update_site_option( 'ep_host', 'badhost' );
update_site_option( 'ep_last_sync', time() );
delete_site_option( 'ep_need_upgrade_sync', true );
delete_site_option( 'ep_feature_auto_activated_sync' );

ElasticPress\Elasticsearch::factory()->get_elasticsearch_version( true );

ElasticPress\Screen::factory()->set_current_screen( 'install' );

ElasticPress\AdminNotices::factory()->process_notices();

$notices = ElasticPress\AdminNotices::factory()->get_notices();

$this->assertEquals( 0, count( $notices ) );

update_site_option( 'ep_host', $this->current_host );
ElasticPress\Elasticsearch::factory()->get_elasticsearch_version( true );
}

/**
Expand All @@ -239,7 +283,26 @@ public function testHostErrorNoticeInInstall() {
* @since 3.0
*/
public function testEsAboveCompatNoticeInAdmin() {
// VIP: Remove test
update_site_option( 'ep_last_sync', time() );
delete_site_option( 'ep_need_upgrade_sync', true );
delete_site_option( 'ep_feature_auto_activated_sync' );

$es_version = function() {
return '100';
};

add_filter( 'ep_elasticsearch_version', $es_version );

ElasticPress\Screen::factory()->set_current_screen( null );

ElasticPress\AdminNotices::factory()->process_notices();

$notices = ElasticPress\AdminNotices::factory()->get_notices();

remove_filter( 'ep_elasticsearch_version', $es_version );

$this->assertEquals( 1, count( $notices ) );
$this->assertTrue( ! empty( $notices['es_above_compat'] ) );
}

/**
Expand All @@ -259,7 +322,26 @@ public function testEsAboveCompatNoticeInAdmin() {
* @since 3.0
*/
public function testEsBelowCompatNoticeInAdmin() {
// VIP: Remove test
update_site_option( 'ep_last_sync', time() );
delete_site_option( 'ep_need_upgrade_sync', true );
delete_site_option( 'ep_feature_auto_activated_sync' );

$es_version = function() {
return '1';
};

add_filter( 'ep_elasticsearch_version', $es_version );

ElasticPress\Screen::factory()->set_current_screen( null );

ElasticPress\AdminNotices::factory()->process_notices();

$notices = ElasticPress\AdminNotices::factory()->get_notices();

remove_filter( 'ep_elasticsearch_version', $es_version );

$this->assertEquals( 1, count( $notices ) );
$this->assertTrue( ! empty( $notices['es_below_compat'] ) );
}

/**
Expand All @@ -279,7 +361,18 @@ public function testEsBelowCompatNoticeInAdmin() {
* @since 3.0
*/
public function testUpgradeSyncNoticeInAdmin() {
// VIP: Remove test
update_site_option( 'ep_last_sync', time() );
update_site_option( 'ep_need_upgrade_sync', true );
delete_site_option( 'ep_feature_auto_activated_sync' );

ElasticPress\Screen::factory()->set_current_screen( null );

ElasticPress\AdminNotices::factory()->process_notices();

$notices = ElasticPress\AdminNotices::factory()->get_notices();

$this->assertEquals( 1, count( $notices ) );
$this->assertTrue( ! empty( $notices['upgrade_sync'] ) );
}

/**
Expand All @@ -297,7 +390,42 @@ public function testUpgradeSyncNoticeInAdmin() {
* @since 4.0.0
*/
public function testUpgradeSyncNoticeAndInstantResultsInAdmin() {
// VIP: Remove test
update_site_option( 'ep_last_sync', time() );
update_site_option( 'ep_need_upgrade_sync', true );
update_site_option( 'ep_version', '3.6.6' );
delete_site_option( 'ep_feature_auto_activated_sync' );

ElasticPress\Screen::factory()->set_current_screen( null );

// Instant Results not available.
$not_available_full_text = '<a href="https://elasticpress.zendesk.com/hc/en-us/articles/360050447492#instant-results">Instant Results</a> is now available in ElasticPress, but requires a re-sync before activation. If you would like to use Instant Results, since you are not using ElasticPress.io, you will also need to <a href="https://elasticpress.zendesk.com/hc/en-us/articles/4413938931853-Considerations-for-self-hosted-Elasticsearch-setups">install and configure a PHP proxy</a>.';
ElasticPress\AdminNotices::factory()->process_notices();
$notices = ElasticPress\AdminNotices::factory()->get_notices();
$this->assertTrue( ! empty( $notices['upgrade_sync'] ) );
$this->assertStringContainsString( $not_available_full_text, $notices['upgrade_sync']['html'] );

// Instant Results available.
if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) {
$features_url = admin_url( 'network/admin.php?page=elasticpress' );
} else {
$features_url = admin_url( 'admin.php?page=elasticpress' );
}
$available_full_text = '<a href="https://elasticpress.zendesk.com/hc/en-us/articles/360050447492#instant-results">Instant Results</a> is now available in ElasticPress, but requires a re-sync before activation. If you would like to use Instant Results, click <a href="' . $features_url . '">here</a> to activate the feature and start your sync.';

// Instant Results available via custom proxy.
add_filter( 'ep_instant_results_available', '__return_true' );
ElasticPress\AdminNotices::factory()->process_notices();
$notices = ElasticPress\AdminNotices::factory()->get_notices();
$this->assertTrue( ! empty( $notices['upgrade_sync'] ) );
$this->assertStringContainsString( $available_full_text, $notices['upgrade_sync']['html'] );
remove_filter( 'ep_instant_results_available', '__return_true' );

// Instant Results available via EP.io.
update_site_option( 'ep_host', 'https://prefix.elasticpress.io/' );
ElasticPress\AdminNotices::factory()->process_notices();
$notices = ElasticPress\AdminNotices::factory()->get_notices();
$this->assertTrue( ! empty( $notices['upgrade_sync'] ) );
$this->assertStringContainsString( $available_full_text, $notices['upgrade_sync']['html'] );
}

/**
Expand All @@ -317,7 +445,18 @@ public function testUpgradeSyncNoticeAndInstantResultsInAdmin() {
* @since 3.0
*/
public function testFeatureSyncNoticeInAdmin() {
// VIP: Remove test
update_site_option( 'ep_last_sync', time() );
delete_site_option( 'ep_need_upgrade_sync' );
update_site_option( 'ep_feature_auto_activated_sync', true );

ElasticPress\Screen::factory()->set_current_screen( null );

ElasticPress\AdminNotices::factory()->process_notices();

$notices = ElasticPress\AdminNotices::factory()->get_notices();

$this->assertEquals( 1, count( $notices ) );
$this->assertTrue( ! empty( $notices['auto_activate_sync'] ) );
}


Expand All @@ -339,7 +478,30 @@ public function testFeatureSyncNoticeInAdmin() {
* @since 3.6.2
*/
public function testValidMappingNoticeInAdmin() {
// VIP: Remove test
update_site_option( 'ep_last_sync', time() );
delete_site_option( 'ep_need_upgrade_sync' );
update_site_option( 'ep_feature_auto_activated_sync', false );

// We need to do a proper sync with real version to ensure the index is in place
// and we do not get a 404 when requesting the mapping version.
$es_version = $this->real_es_version;
add_filter(
'ep_elasticsearch_version',
function() use ( $es_version ) {
return $es_version;
}
);

ElasticPress\Elasticsearch::factory()->delete_all_indices();
ElasticPress\Indexables::factory()->get( 'post' )->put_mapping();
ElasticPress\Indexables::factory()->get( 'post' )->sync_manager->sync_queue = [];

ElasticPress\Screen::factory()->set_current_screen( null );

ElasticPress\AdminNotices::factory()->process_notices();

$notices = ElasticPress\AdminNotices::factory()->get_notices();
$this->assertCount( 0, $notices );
}

/**
Expand All @@ -360,7 +522,36 @@ public function testValidMappingNoticeInAdmin() {
* @since 3.6.2
*/
public function testInvalidMappingNoticeInAdmin() {
// VIP: Remove test
update_site_option( 'ep_last_sync', time() );
delete_site_option( 'ep_need_upgrade_sync' );
update_site_option( 'ep_feature_auto_activated_sync', false );

// We need to do a proper sync with real version to ensure the index is in place
// and we do not get a 404 when requesting the mapping version.
$es_version = $this->real_es_version;
add_filter(
'ep_elasticsearch_version',
function() use ( $es_version ) {
return $es_version;
}
);

ElasticPress\Elasticsearch::factory()->delete_all_indices();
ElasticPress\Indexables::factory()->get( 'post' )->put_mapping();
ElasticPress\Indexables::factory()->get( 'post' )->sync_manager->sync_queue = [];

$mapping = function() {
return 'idonotmatch';
};
add_filter( 'ep_post_mapping_version_determined', $mapping );

ElasticPress\Screen::factory()->set_current_screen( null );

ElasticPress\AdminNotices::factory()->process_notices();

$notices = ElasticPress\AdminNotices::factory()->get_notices();
$this->assertCount( 1, $notices );
$this->assertTrue( ! empty( $notices['maybe_wrong_mapping'] ) );
}

/**
Expand Down
6 changes: 3 additions & 3 deletions tests/php/TestElasticsearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function testGetClusterStatus() {

$status_unindexed = ElasticPress\Elasticsearch::factory()->get_cluster_status();

$this->setUp();
$this->set_up();

if ( is_array( $status_indexed ) ) {

Expand Down Expand Up @@ -61,8 +61,8 @@ public function testGetClusterStatus() {
public function testGetDocuments() {

$post_ids = array();
$post_ids[] = Functions\create_and_sync_post();
$post_ids[] = Functions\create_and_sync_post();
$post_ids[] = $this->ep_factory->post->create();
$post_ids[] = $this->ep_factory->post->create();

ElasticPress\Elasticsearch::factory()->refresh_indices();

Expand Down
Loading

0 comments on commit c82a8d4

Please sign in to comment.