From 5324788eab39705483f5b60228d5487ce2a1ddf5 Mon Sep 17 00:00:00 2001 From: Santeri Hurnanen Date: Wed, 20 Nov 2024 19:01:55 +0200 Subject: [PATCH] UHF-10990: Add HelfiConnector plugin --- composer.json | 2 + ...ector.connector.helfi_connector.schema.yml | 2 + .../Connector/HelfiConnector.php | 33 ++++++++ .../Functional/ElasticsearchConnectorTest.php | 83 +++++++++++++++++++ 4 files changed, 120 insertions(+) create mode 100644 config/schema/elasticsearch_connector.connector.helfi_connector.schema.yml create mode 100644 src/Plugin/ElasticSearch/Connector/HelfiConnector.php create mode 100644 tests/src/Functional/ElasticsearchConnectorTest.php 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..fdef1bb --- /dev/null +++ b/tests/src/Functional/ElasticsearchConnectorTest.php @@ -0,0 +1,83 @@ +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'); + + $this->assertInstanceOf(HelfiConnector::class, $server->getBackend()->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'); + } + +}