diff --git a/composer.json b/composer.json index ac49bd6..62c933a 100644 --- a/composer.json +++ b/composer.json @@ -24,6 +24,8 @@ "dealerdirect/phpcodesniffer-composer-installer": "^0.7.0", "dg/bypass-finals": "^1.0", "drupal/coder": "^8.3", + "drupal/search_api": "^1.0", + "drupal/elasticsearch_connector": "^8.0@alpha", "donatj/mock-webserver": "dev-master" } } diff --git a/config/schema/elasticsearch_connector.connector.helfi_connector.schema.yml b/config/schema/elasticsearch_connector.connector.helfi_connector.schema.yml new file mode 100644 index 0000000..03b540f --- /dev/null +++ b/config/schema/elasticsearch_connector.connector.helfi_connector.schema.yml @@ -0,0 +1,2 @@ +plugin.plugin_configuration.elasticsearch_connector.helfi_connector: + type: plugin.plugin_configuration.elasticsearch_connector.basicauth diff --git a/src/Plugin/ElasticSearch/Connector/HelfiConnector.php b/src/Plugin/ElasticSearch/Connector/HelfiConnector.php new file mode 100644 index 0000000..e323968 --- /dev/null +++ b/src/Plugin/ElasticSearch/Connector/HelfiConnector.php @@ -0,0 +1,33 @@ +setHosts([$this->configuration['url']]) + ->setBasicAuthentication($this->configuration['username'], $this->configuration['password']) + ->setSSLVerification(FALSE) + ->build(); + } + +} diff --git a/tests/src/Kernel/ElasticsearchConnectorTest.php b/tests/src/Kernel/ElasticsearchConnectorTest.php new file mode 100644 index 0000000..221b3de --- /dev/null +++ b/tests/src/Kernel/ElasticsearchConnectorTest.php @@ -0,0 +1,72 @@ +installEntitySchema('search_api_server'); + } + + /** + * Tests custom elasticsearch connector. + */ + public function testSearchApiConnector() { + // Add elasticsearch server configuration. + $config = $this->config('search_api.server.default'); + $config->setData([ + 'status' => TRUE, + 'id' => 'default', + 'name' => 'elasticsearch_server', + 'description' => 'Test server', + 'backend' => 'elasticsearch', + 'backend_config' => [ + 'connector' => 'helfi_connector', + 'connector_config' => [ + 'url' => 'http://elasticsearch.example.com:9200', + 'username' => '123', + 'password' => '456', + ], + ], + ]); + $config->save(); + + /** @var \Drupal\search_api\ServerInterface $server */ + $server = $this->container + ->get(EntityTypeManagerInterface::class) + ->getStorage('search_api_server') + ->load('default'); + + $backend = $server->getBackend(); + assert($backend instanceof ElasticSearchBackend); + $connector = $backend->getConnector(); + $this->assertInstanceOf(HelfiConnector::class, $connector); + $this->assertInstanceOf(Client::class, $connector->getClient()); + } + +}