Skip to content

Commit

Permalink
UHF-10990: Add HelfiConnector plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
hyrsky committed Nov 20, 2024
1 parent bfada1e commit 5324788
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 0 deletions.
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
plugin.plugin_configuration.elasticsearch_connector.helfi_connector:
type: plugin.plugin_configuration.elasticsearch_connector.basicauth
33 changes: 33 additions & 0 deletions src/Plugin/ElasticSearch/Connector/HelfiConnector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Drupal\helfi_api_base\Plugin\ElasticSearch\Connector;

use Drupal\elasticsearch_connector\Plugin\ElasticSearch\Connector\BasicAuthConnector;
use Elastic\Elasticsearch\Client;
use Elastic\Elasticsearch\ClientBuilder;

/**
* Provides an ElasticSearch connector that accepts self-signed certificates.
*
* @ElasticSearchConnector(
* id = "helfi_connector",
* label = @Translation("Helfi Connector"),
* description = @Translation("ElasticSearch connector with HTTP Basic Auth that accepts self signed certificates.")
* )
*/
class HelfiConnector extends BasicAuthConnector {

/**
* {@inheritdoc}
*/
public function getClient(): Client {
return ClientBuilder::create()
->setHosts([$this->configuration['url']])
->setBasicAuthentication($this->configuration['username'], $this->configuration['password'])
->setSSLVerification(FALSE)
->build();
}

}
83 changes: 83 additions & 0 deletions tests/src/Functional/ElasticsearchConnectorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

declare(strict_types=1);

namespace Drupal\Tests\helfi_api_base\Functional;

use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Url;
use Drupal\helfi_platform_config\Plugin\ElasticSearch\Connector\HelfiConnector;

/**
* Test for elasticsearch connector plugin.
*/
class ElasticsearchConnectorTest extends BrowserTestBase {

/**
* {@inheritdoc}
*/
protected static $modules = [
'search_api',
'elasticsearch_connector',
'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);
}

/**
* 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');
}

}

0 comments on commit 5324788

Please sign in to comment.