Skip to content

Commit

Permalink
Production release v20240528.0 (#5613)
Browse files Browse the repository at this point in the history
Production release v20240528.0
  • Loading branch information
noahtallen authored May 28, 2024
2 parents 056e190 + 5dd61e6 commit 43076be
Show file tree
Hide file tree
Showing 10 changed files with 236 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .github/actions/run-wp-tests/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ runs:
"${PHPUNIT}" ${OPTIONS}
- name: Upload coverage report
uses: codecov/codecov-action@v4.3.1
uses: codecov/codecov-action@v4.4.1
with:
files: ${{ inputs.coverage-file }}
flags: ${{ inputs.coverage-flags }}
Expand Down
79 changes: 79 additions & 0 deletions integrations/enterprise-search.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
/**
* Integration: Enterprise Search.
*
* @package Automattic\VIP\Integrations
*/

namespace Automattic\VIP\Integrations;

/**
* Loads Enterprise Search VIP Integration.
*/
class EnterpriseSearchIntegration extends Integration {

/**
* The version of Enterprise Search to load.
*
* @var string
*/
protected string $version = '1.0';

/**
* Returns `true` if Enterprise Search is already available e.g. customer code. We will use
* this function to prevent loading of integration again from platform side.
*/
public function is_loaded(): bool {
return class_exists( \Automattic\VIP\Search\Search::class );
}

/**
* Loads the plugin.
*/
public function load(): void {
// Return if the integration is already loaded.
//
// In activate() method we do make sure to not activate the integration if its already loaded
// but still adding it here as a safety measure i.e. if load() is called directly.
if ( $this->is_loaded() ) {
return;
}

// Load the version of the plugin that should be set to the latest version, otherwise if it's not found, fallback to the one in MU.
$load_path = WPVIP_MU_PLUGIN_DIR . '/vip-integrations/vip-enterprise-search-' . $this->version . '/src/search.php';
$use_versions = false; // Remove this once we are ready to use the versioned plugin.
if ( $use_versions && file_exists( $load_path ) ) {
require_once $load_path;
} else {
require_once __DIR__ . '/../search/search.php';
}
}

/**
* Configure `Enterprise Search` for VIP Platform.
*/
public function configure(): void {
if ( $this->is_es_credentials_set() ) {
return;
}

add_action( 'vip_search_loaded', array( $this, 'vip_set_es_credentials' ) );
}

/**
* Set the Elasticsearch credentials.
*/
public function vip_set_es_credentials(): void {
$config = $this->get_config();
if ( isset( $config['username'] ) && isset( $config['password'] ) ) {
define( 'VIP_ELASTICSEARCH_USERNAME', $config['username'] );
define( 'VIP_ELASTICSEARCH_PASSWORD', $config['password'] );
}
}

private function is_es_credentials_set(): bool {
$username_defined = defined( 'VIP_ELASTICSEARCH_USERNAME' ) && constant( 'VIP_ELASTICSEARCH_USERNAME' );
$password_defined = defined( 'VIP_ELASTICSEARCH_PASSWORD' ) && constant( 'VIP_ELASTICSEARCH_PASSWORD' );
return $username_defined && $password_defined;
}
}
6 changes: 4 additions & 2 deletions integrations/integration-vip-config.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,14 @@ public function get_site_status() {
* Get site config.
*
* @return array
*
* @private
*/
public function get_site_config() {
if ( is_multisite() ) {
$config = $this->get_value_from_config( 'network_sites', 'config' );
// If network site config is not found then fallback to env config if it exists
if ( empty( $config ) && true === $this->get_value_from_config( 'env', 'cascade_config' ) ) {
$config = $this->get_value_from_config( 'env', 'config' );
}
} else {
$config = $this->get_value_from_config( 'env', 'config' );
}
Expand Down
2 changes: 1 addition & 1 deletion jetpack
2 changes: 1 addition & 1 deletion jetpack.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Plugin URI: https://jetpack.com
* Description: Security, performance, and marketing tools made by WordPress experts. Jetpack keeps your site protected so you can focus on more important things.
* Author: Automattic
* Version: 13.4.2
* Version: 13.4.3
* Author URI: https://jetpack.com
* License: GPL2+
* Text Domain: jetpack
Expand Down
104 changes: 104 additions & 0 deletions tests/integrations/test-enterprise-search.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php
/**
* Test: Enterprise Search Integration.
*
* @package Automattic\VIP\Integrations
*/

namespace Automattic\VIP\Integrations;

use PHPUnit\Framework\MockObject\MockObject;
use WP_UnitTestCase;
use Automattic\Test\Constant_Mocker;

use function Automattic\Test\Utils\get_class_property_as_public;

// phpcs:disable Squiz.Commenting.ClassComment.Missing, Squiz.Commenting.FunctionComment.Missing, Squiz.Commenting.VariableComment.Missing

class VIP_EnterpriseSearch_Integration_Test extends WP_UnitTestCase {
private string $slug = 'enterprise-search';

public function tearDown(): void {
parent::tearDown();

Constant_Mocker::clear();
}

public function test_is_loaded_returns_true_if_es_exist(): void {
require_once __DIR__ . '/../../search/search.php';

$es_integration = new EnterpriseSearchIntegration( $this->slug );
$this->assertTrue( $es_integration->is_loaded() );
}

public function test__load_call_returns_without_requiring_class_if_es_is_already_loaded(): void {
/**
* Integration mock.
*
* @var MockObject|EnterpriseSearchIntegration
*/
$es_integration_mock = $this->getMockBuilder( EnterpriseSearchIntegration::class )->setConstructorArgs( [ 'enterprise-search' ] )->onlyMethods( [ 'is_loaded' ] )->getMock();
$es_integration_mock->expects( $this->once() )->method( 'is_loaded' )->willReturn( true );
$preload_state = class_exists( '\Automattic\VIP\Search\Search' );

$es_integration_mock->load();

$this->assertEquals( $preload_state, class_exists( '\Automattic\VIP\Search\Search' ) );
}

public function test__load_call_if_class_not_exist(): void {
/**
* Integration mock.
*
* @var MockObject|EnterpriseSearchIntegration
*/
$es_integration_mock = $this->getMockBuilder( EnterpriseSearchIntegration::class )->setConstructorArgs( [ 'enterprise-search' ] )->onlyMethods( [ 'is_loaded' ] )->getMock();
$es_integration_mock->expects( $this->once() )->method( 'is_loaded' )->willReturn( false );
$existing_value = class_exists( '\Automattic\VIP\Search\Search' );

$es_integration_mock->load();

if ( ! $existing_value ) {
$this->assertTrue( class_exists( '\Automattic\VIP\Search\Search' ) );
}
}

public function test__configure_action(): void {
$credentials = [
'username' => 'test-username',
'password' => 'foo-bar',
];
$es_integration = new EnterpriseSearchIntegration( $this->slug );
$es_integration->configure();

get_class_property_as_public( Integration::class, 'options' )->setValue( $es_integration, [
'config' => $credentials,
] );

do_action( 'vip_search_loaded' );

$this->assertEquals( 10, has_action( 'vip_search_loaded', [ $es_integration, 'vip_set_es_credentials' ] ) );
$this->assertEquals( constant( 'VIP_ELASTICSEARCH_USERNAME' ), $credentials['username'] );
$this->assertEquals( constant( 'VIP_ELASTICSEARCH_PASSWORD' ), $credentials['password'] );
}

public function test__should_not_configure_if_es_constants_are_already_present(): void {
Constant_Mocker::define( 'VIP_ELASTICSEARCH_USERNAME', 'baz' );
Constant_Mocker::define( 'VIP_ELASTICSEARCH_PASSWORD', '123' );

$credentials = [
'username' => 'test-username',
'password' => 'foo-bar',
];
$es_integration = new EnterpriseSearchIntegration( $this->slug );
get_class_property_as_public( Integration::class, 'options' )->setValue( $es_integration, [
'config' => $credentials,
] );
$es_integration->configure();


$this->assertEquals( false, has_action( 'vip_search_loaded', [ $es_integration, 'vip_set_es_credentials' ] ) );
$this->assertEquals( Constant_Mocker::constant( 'VIP_ELASTICSEARCH_USERNAME' ), 'baz' );
$this->assertEquals( Constant_Mocker::constant( 'VIP_ELASTICSEARCH_PASSWORD' ), '123' );
}
}
22 changes: 22 additions & 0 deletions tests/integrations/test-integration-vip-config.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,28 @@ public function test__get_site_config_returns_value_from_network_site_config():
);
}

public function test__get_site_config_with_cascading_config(): void {
if ( ! is_multisite() ) {
$this->markTestSkipped( 'Only valid for multisite.' );
}

$this->do_test_get_site_config(
[
'env' => [
'status' => Env_Integration_Status::ENABLED,
'config' => array( 'env-config' ),
'cascade_config' => true,
],
'network_sites' => [
'1' => [
'status' => Env_Integration_Status::ENABLED,
],
],
],
array( 'env-config' ),
);
}

/**
* Helper function for testing `get_site_config`.
*
Expand Down
11 changes: 6 additions & 5 deletions tests/integrations/test-parsely.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use PHPUnit\Framework\MockObject\MockObject;
use WP_UnitTestCase;
use Automattic\Test\Constant_Mocker;

use function Automattic\Test\Utils\get_class_property_as_public;

Expand All @@ -32,11 +33,11 @@ public function test__load_call_returns_without_setting_constant_if_parsely_is_a
*/
$parsely_integration_mock = $this->getMockBuilder( ParselyIntegration::class )->setConstructorArgs( [ 'parsely' ] )->onlyMethods( [ 'is_loaded' ] )->getMock();
$parsely_integration_mock->expects( $this->once() )->method( 'is_loaded' )->willReturn( true );
$preload_state = defined( 'VIP_PARSELY_ENABLED' );
$preload_state = Constant_Mocker::defined( 'VIP_PARSELY_ENABLED' );

$parsely_integration_mock->load();

$this->assertEquals( $preload_state, defined( 'VIP_PARSELY_ENABLED' ) );
$this->assertEquals( $preload_state, Constant_Mocker::defined( 'VIP_PARSELY_ENABLED' ) );
}

public function test__load_call_is_setting_the_enabled_constant_if_no_constant_is_defined(): void {
Expand All @@ -47,14 +48,14 @@ public function test__load_call_is_setting_the_enabled_constant_if_no_constant_i
*/
$parsely_integration_mock = $this->getMockBuilder( ParselyIntegration::class )->setConstructorArgs( [ 'parsely' ] )->onlyMethods( [ 'is_loaded' ] )->getMock();
$parsely_integration_mock->expects( $this->once() )->method( 'is_loaded' )->willReturn( false );
$existing_value = defined( 'VIP_PARSELY_ENABLED' ) ? VIP_PARSELY_ENABLED : null;
$existing_value = Constant_Mocker::defined( 'VIP_PARSELY_ENABLED' ) ? Constant_Mocker::constant( 'VIP_PARSELY_ENABLED' ) : null;

$parsely_integration_mock->load();

if ( is_null( $existing_value ) || $existing_value ) {
$this->assertTrue( VIP_PARSELY_ENABLED );
$this->assertTrue( Constant_Mocker::constant( 'VIP_PARSELY_ENABLED' ) );
} else {
$this->assertFalse( defined( 'VIP_PARSELY_ENABLED' ) );
$this->assertFalse( Constant_Mocker::defined( 'VIP_PARSELY_ENABLED' ) );
}
}

Expand Down
16 changes: 16 additions & 0 deletions tests/mock-constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,19 @@ function constant( $constant ) {
return Constant_Mocker::constant( $constant );
}
}

namespace Automattic\VIP\Integrations {
use Automattic\Test\Constant_Mocker;

function define( $constant, $value ) {
return Constant_Mocker::define( $constant, $value );
}

function defined( $constant ) {
return Constant_Mocker::defined( $constant );
}

function constant( $constant ) {
return Constant_Mocker::constant( $constant );
}
}
2 changes: 2 additions & 0 deletions vip-integrations.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@
require_once __DIR__ . '/integrations/block-data-api.php';
require_once __DIR__ . '/integrations/parsely.php';
require_once __DIR__ . '/integrations/vip-governance.php';
require_once __DIR__ . '/integrations/enterprise-search.php';

// Register VIP integrations here.
IntegrationsSingleton::instance()->register( new BlockDataApiIntegration( 'block-data-api' ) );
IntegrationsSingleton::instance()->register( new ParselyIntegration( 'parsely' ) );
IntegrationsSingleton::instance()->register( new VipGovernanceIntegration( 'vip-governance' ) );
IntegrationsSingleton::instance()->register( new EnterpriseSearchIntegration( 'enterprise-search' ) );
// @codeCoverageIgnoreEnd

/**
Expand Down

0 comments on commit 43076be

Please sign in to comment.