|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Test: Enterprise Search Integration. |
| 4 | + * |
| 5 | + * @package Automattic\VIP\Integrations |
| 6 | + */ |
| 7 | + |
| 8 | +namespace Automattic\VIP\Integrations; |
| 9 | + |
| 10 | +use PHPUnit\Framework\MockObject\MockObject; |
| 11 | +use WP_UnitTestCase; |
| 12 | +use Automattic\Test\Constant_Mocker; |
| 13 | + |
| 14 | +use function Automattic\Test\Utils\get_class_property_as_public; |
| 15 | + |
| 16 | +// phpcs:disable Squiz.Commenting.ClassComment.Missing, Squiz.Commenting.FunctionComment.Missing, Squiz.Commenting.VariableComment.Missing |
| 17 | + |
| 18 | +class VIP_EnterpriseSearch_Integration_Test extends WP_UnitTestCase { |
| 19 | + private string $slug = 'enterprise-search'; |
| 20 | + |
| 21 | + public function tearDown(): void { |
| 22 | + parent::tearDown(); |
| 23 | + |
| 24 | + Constant_Mocker::clear(); |
| 25 | + } |
| 26 | + |
| 27 | + public function test_is_loaded_returns_true_if_es_exist(): void { |
| 28 | + require_once __DIR__ . '/../../search/search.php'; |
| 29 | + |
| 30 | + $es_integration = new EnterpriseSearchIntegration( $this->slug ); |
| 31 | + $this->assertTrue( $es_integration->is_loaded() ); |
| 32 | + } |
| 33 | + |
| 34 | + public function test__load_call_returns_without_requiring_class_if_es_is_already_loaded(): void { |
| 35 | + /** |
| 36 | + * Integration mock. |
| 37 | + * |
| 38 | + * @var MockObject|EnterpriseSearchIntegration |
| 39 | + */ |
| 40 | + $es_integration_mock = $this->getMockBuilder( EnterpriseSearchIntegration::class )->setConstructorArgs( [ 'enterprise-search' ] )->onlyMethods( [ 'is_loaded' ] )->getMock(); |
| 41 | + $es_integration_mock->expects( $this->once() )->method( 'is_loaded' )->willReturn( true ); |
| 42 | + $preload_state = class_exists( '\Automattic\VIP\Search\Search' ); |
| 43 | + |
| 44 | + $es_integration_mock->load(); |
| 45 | + |
| 46 | + $this->assertEquals( $preload_state, class_exists( '\Automattic\VIP\Search\Search' ) ); |
| 47 | + } |
| 48 | + |
| 49 | + public function test__load_call_if_class_not_exist(): void { |
| 50 | + /** |
| 51 | + * Integration mock. |
| 52 | + * |
| 53 | + * @var MockObject|EnterpriseSearchIntegration |
| 54 | + */ |
| 55 | + $es_integration_mock = $this->getMockBuilder( EnterpriseSearchIntegration::class )->setConstructorArgs( [ 'enterprise-search' ] )->onlyMethods( [ 'is_loaded' ] )->getMock(); |
| 56 | + $es_integration_mock->expects( $this->once() )->method( 'is_loaded' )->willReturn( false ); |
| 57 | + $existing_value = class_exists( '\Automattic\VIP\Search\Search' ); |
| 58 | + |
| 59 | + $es_integration_mock->load(); |
| 60 | + |
| 61 | + if ( ! $existing_value ) { |
| 62 | + $this->assertTrue( class_exists( '\Automattic\VIP\Search\Search' ) ); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + public function test__configure_action(): void { |
| 67 | + $credentials = [ |
| 68 | + 'username' => 'test-username', |
| 69 | + 'password' => 'foo-bar', |
| 70 | + ]; |
| 71 | + $es_integration = new EnterpriseSearchIntegration( $this->slug ); |
| 72 | + $es_integration->configure(); |
| 73 | + |
| 74 | + get_class_property_as_public( Integration::class, 'options' )->setValue( $es_integration, [ |
| 75 | + 'config' => $credentials, |
| 76 | + ] ); |
| 77 | + |
| 78 | + do_action( 'vip_search_loaded' ); |
| 79 | + |
| 80 | + $this->assertEquals( 10, has_action( 'vip_search_loaded', [ $es_integration, 'vip_set_es_credentials' ] ) ); |
| 81 | + $this->assertEquals( constant( 'VIP_ELASTICSEARCH_USERNAME' ), $credentials['username'] ); |
| 82 | + $this->assertEquals( constant( 'VIP_ELASTICSEARCH_PASSWORD' ), $credentials['password'] ); |
| 83 | + } |
| 84 | + |
| 85 | + public function test__should_not_configure_if_es_constants_are_already_present(): void { |
| 86 | + Constant_Mocker::define( 'VIP_ELASTICSEARCH_USERNAME', 'baz' ); |
| 87 | + Constant_Mocker::define( 'VIP_ELASTICSEARCH_PASSWORD', '123' ); |
| 88 | + |
| 89 | + $credentials = [ |
| 90 | + 'username' => 'test-username', |
| 91 | + 'password' => 'foo-bar', |
| 92 | + ]; |
| 93 | + $es_integration = new EnterpriseSearchIntegration( $this->slug ); |
| 94 | + get_class_property_as_public( Integration::class, 'options' )->setValue( $es_integration, [ |
| 95 | + 'config' => $credentials, |
| 96 | + ] ); |
| 97 | + $es_integration->configure(); |
| 98 | + |
| 99 | + |
| 100 | + $this->assertEquals( false, has_action( 'vip_search_loaded', [ $es_integration, 'vip_set_es_credentials' ] ) ); |
| 101 | + $this->assertEquals( Constant_Mocker::constant( 'VIP_ELASTICSEARCH_USERNAME' ), 'baz' ); |
| 102 | + $this->assertEquals( Constant_Mocker::constant( 'VIP_ELASTICSEARCH_PASSWORD' ), '123' ); |
| 103 | + } |
| 104 | +} |
0 commit comments