From bb6776b9e6ca15d3bc811d3ca504aba80fecd171 Mon Sep 17 00:00:00 2001 From: Santeri Hurnanen Date: Wed, 20 Nov 2024 19:14:34 +0200 Subject: [PATCH 1/3] UHF-10990: Add HelfiConnector plugin --- ...ector.connector.helfi_connector.schema.yml | 2 ++ .../Connector/HelfiConnector.php | 33 +++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 config/schema/elasticsearch_connector.connector.helfi_connector.schema.yml create mode 100644 src/Plugin/ElasticSearch/Connector/HelfiConnector.php 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(); + } + +} From 4b420cc2eae351bd12abf5cba233d7735105cf60 Mon Sep 17 00:00:00 2001 From: Santeri Hurnanen Date: Wed, 20 Nov 2024 19:22:02 +0200 Subject: [PATCH 2/3] UHF-10990: Add test --- composer.json | 2 + .../Functional/ElasticsearchConnectorTest.php | 86 +++++++++++++++++++ 2 files changed, 88 insertions(+) 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/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'); + } + +} From 343f0ebbe84c483fc6891cb0456641e227ca2098 Mon Sep 17 00:00:00 2001 From: Santeri Hurnanen Date: Thu, 21 Nov 2024 12:42:21 +0200 Subject: [PATCH 3/3] UHF-10990: Kernel test --- .../ElasticsearchConnectorTest.php | 30 +++++-------------- 1 file changed, 8 insertions(+), 22 deletions(-) rename tests/src/{Functional => Kernel}/ElasticsearchConnectorTest.php (66%) diff --git a/tests/src/Functional/ElasticsearchConnectorTest.php b/tests/src/Kernel/ElasticsearchConnectorTest.php similarity index 66% rename from tests/src/Functional/ElasticsearchConnectorTest.php rename to tests/src/Kernel/ElasticsearchConnectorTest.php index 3a7f8da..221b3de 100644 --- a/tests/src/Functional/ElasticsearchConnectorTest.php +++ b/tests/src/Kernel/ElasticsearchConnectorTest.php @@ -2,17 +2,18 @@ declare(strict_types=1); -namespace Drupal\Tests\helfi_api_base\Functional; +namespace Drupal\Tests\helfi_api_base\Kernel; use Drupal\Core\Entity\EntityTypeManagerInterface; -use Drupal\Core\Url; +use Drupal\KernelTests\KernelTestBase; use Drupal\elasticsearch_connector\Plugin\search_api\backend\ElasticSearchBackend; use Drupal\helfi_api_base\Plugin\ElasticSearch\Connector\HelfiConnector; +use Elastic\Elasticsearch\Client; /** * Test for elasticsearch connector plugin. */ -class ElasticsearchConnectorTest extends BrowserTestBase { +class ElasticsearchConnectorTest extends KernelTestBase { /** * {@inheritdoc} @@ -23,23 +24,13 @@ class ElasticsearchConnectorTest extends BrowserTestBase { 'helfi_api_base', ]; - /** - * {@inheritdoc} - */ - protected $defaultTheme = 'stark'; - /** * {@inheritdoc} */ public function setUp(): void { parent::setUp(); - // Create an admin user. - $admin_user = $this->drupalCreateUser([ - 'access administration pages', - 'administer search_api', - ]); - $this->drupalLogin($admin_user); + $this->installEntitySchema('search_api_server'); } /** @@ -73,14 +64,9 @@ public function testSearchApiConnector() { $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'); + $connector = $backend->getConnector(); + $this->assertInstanceOf(HelfiConnector::class, $connector); + $this->assertInstanceOf(Client::class, $connector->getClient()); } }