-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Production release v20240528.0 (#5613)
Production release v20240528.0
- Loading branch information
Showing
10 changed files
with
236 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule jetpack
updated
29 files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters