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/Functional/ElasticsearchConnectorTest.php b/tests/src/Functional/ElasticsearchConnectorTest.php new file mode 100644 index 0000000..3a7f8da --- /dev/null +++ b/tests/src/Functional/ElasticsearchConnectorTest.php @@ -0,0 +1,86 @@ +drupalCreateUser([ + 'access administration pages', + 'administer search_api', + ]); + $this->drupalLogin($admin_user); + } + + /** + * 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); + $this->assertInstanceOf(HelfiConnector::class, $backend->getConnector()); + + $assert_session = $this->assertSession(); + $this->drupalGet(Url::fromRoute('entity.search_api_server.edit_form', [ + 'search_api_server' => 'default', + ])); + $assert_session->statusCodeEquals(200); + $assert_session->pageTextContains('Helfi Connector'); + } + +}