Skip to content

Commit

Permalink
OEL-167: Add test structure.
Browse files Browse the repository at this point in the history
  • Loading branch information
drishu committed May 31, 2021
1 parent 32adb75 commit 712d697
Show file tree
Hide file tree
Showing 6 changed files with 201 additions and 0 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"composer/installers": "^1.11",
"drupal/drupal-extension": "~4.0",
"drupal/facets": "^1.7",
"drupal/http_request_mock": "^1.0",
"drush/drush": "^10.4",
"openeuropa/behat-transformation-context": "~0.1",
"openeuropa/code-review": "~1.0@beta",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
langcode: en
status: true
dependencies:
module:
- entity_test
- search_api
config:
- search_api.server.europa_search_server
id: europa_search_index
name: 'Europa search index'
description: 'Test index.'
read_only: false
field_settings:
id:
label: ID
type: integer
datasource_id: 'entity:entity_test_mulrev_changed'
property_path: id
name:
label: Name
type: text
datasource_id: 'entity:entity_test_mulrev_changed'
property_path: name
boost: 5.0
created:
label: Authored on
type: date
datasource_id: 'entity:entity_test_mulrev_changed'
property_path: created
body:
label: Body
type: text
datasource_id: 'entity:entity_test_mulrev_changed'
property_path: body
datasource_settings:
'entity:entity_test_mulrev_changed': { }
processor_settings:
add_url: { }
aggregated_field: { }
language_with_fallback: { }
rendered_item: { }
tracker_settings:
default:
indexing_order: fifo
options:
index_directly: true
cron_limit: 50
server: europa_search_server
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
langcode: en
status: true
dependencies:
module:
- oe_search
id: europa_search_server
name: 'Europa search server'
description: 'Test server.'
backend: search_api_europa_search
backend_config:
api_key: api-key
enable_ingestion: true
database: db
text_ingestion_api_endpoint: 'http://example.com/ingest/text'
file_ingestion_api_endpoint: 'http://example.com/ingest'
delete_api_endpoint: 'http://example.com/document'
token_api_endpoint: 'http://example.com/token'
search_api_endpoint: 'http://example.com/search'
facets_api_endpoint: 'http://example.com/search/facets'
info_api_endpoint: 'http://example.com/search/info'
9 changes: 9 additions & 0 deletions tests/modules/oe_search_test/oe_search_test.info.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name: 'OpenEuropa Search Test'
description: 'Test module for the OpenEuropa Search.'
package: Testing

type: module
core_version_requirement: ^8.9 || ^9

dependencies:
- oe_search:oe_search
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types = 1);

namespace Drupal\http_request_mock\Plugin\ServiceMock;

use Drupal\Core\Plugin\PluginBase;
use Drupal\http_request_mock\ServiceMockPluginInterface;
use GuzzleHttp\Psr7\Response;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

/**
* Intercepts any HTTP request made to example.com.
*
* @ServiceMock(
* id = "europa_search_server_response",
* label = @Translation("Europa Search mocked server responses for tesing."),
* weight = 0,
* )
*/
class EuropaSearchServerResponse extends PluginBase implements ServiceMockPluginInterface {

/**
* {@inheritdoc}
*/
public function applies(RequestInterface $request, array $options): bool {
return $request->getUri()->getHost() === 'example.com';
}

/**
* {@inheritdoc}
*/
public function getResponse(RequestInterface $request, array $options): ResponseInterface {
return new Response(200, [], 'Mocking example.com response');
}

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

declare(strict_types=1);

namespace Drupal\oe_search\Tests;

use Drupal\KernelTests\KernelTestBase;
use Drupal\search_api\Entity\Index;
use Drupal\search_api\Entity\Server;
use Drupal\search_api\Utility\Utility;
use Drupal\Tests\search_api\Functional\ExampleContentTrait;

/**
* Class for testing Europa Search drupal integration.
*/
class BackendTest extends KernelTestBase {
use ExampleContentTrait;

/**
* A Search API server ID.
*
* @var string
*/
protected $serverId = 'europa_search_server';

/**
* A Search API index ID.
*
* @var string
*/
protected $indexId = 'europa_search_index';

/**
* Modules to enable.
*
* @var array
*/
protected static $modules = [
'field',
'user',
'system',
'entity_test',
'search_api',
'oe_search',
'oe_search_test',
];

/**
* {@inheritdoc}
*/
public function setUp(): void {
parent::setUp();

$this->installSchema('search_api', ['search_api_item']);
$this->installSchema('user', ['users_data']);
$this->installEntitySchema('entity_test_mulrev_changed');
$this->installEntitySchema('search_api_task');
$this->installConfig([
'search_api',
'oe_search',
'oe_search_test',
]);

// Do not use a batch for tracking the initial items after creating an
// index when running the tests via the GUI. Otherwise, it seems Drupal's
// Batch API gets confused and the test fails.
if (!Utility::isRunningInCli()) {
\Drupal::state()->set('search_api_use_tracking_batch', FALSE);
}

entity_test_create_bundle('item', NULL, 'entity_test_mulrev_changed');
}

/**
* Test Ingestion.
*/
public function testIndexItems(): void {
$backend = Server::load($this->serverId)->getBackend();
$index = Index::load($this->indexId);
$items = [];
$backend->indexItems($index, $items);
$this->assertTrue(TRUE);
}

}

0 comments on commit 712d697

Please sign in to comment.