View source
<?php
namespace Drupal\Tests\search_api\Kernel;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\KernelTests\KernelTestBase;
use Drupal\search_api\Entity\Index;
use Drupal\search_api\Entity\Server;
use Drupal\search_api\IndexInterface;
use Drupal\search_api\Query\QueryInterface;
use Drupal\search_api\Query\ResultSetInterface;
use Drupal\search_api\Utility\Utility;
use Drupal\Tests\search_api\Functional\ExampleContentTrait;
abstract class BackendTestBase extends KernelTestBase {
use ExampleContentTrait;
use StringTranslationTrait;
public static $modules = [
'field',
'search_api',
'user',
'system',
'entity_test',
'filter',
'text',
'search_api_test_example_content',
];
protected $serverId = 'search_server';
protected $indexId = 'search_index';
public function setUp() {
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_test_example_content');
$this
->installConfig('search_api');
if (!Utility::isRunningInCli()) {
\Drupal::state()
->set('search_api_use_tracking_batch', FALSE);
}
$this
->setUpExampleStructure();
}
public function testBackend() {
$this
->insertExampleContent();
$this
->checkDefaultServer();
$this
->checkServerBackend();
$this
->checkDefaultIndex();
$this
->updateIndex();
$this
->searchNoResults();
$this
->indexItems($this->indexId);
$this
->searchSuccess();
if ($this
->getServer()
->supportsFeature('search_api_facets')) {
$this
->checkFacets();
}
$this
->checkSecondServer();
$this
->regressionTests();
$this
->clearIndex();
$this
->indexItems($this->indexId);
$this
->backendSpecificRegressionTests();
$this
->checkBackendSpecificFeatures();
$this
->clearIndex();
$this
->enableHtmlFilter();
$this
->indexItems($this->indexId);
$this
->disableHtmlFilter();
$this
->clearIndex();
$this
->searchNoResults();
$this
->regressionTests2();
$this
->checkIndexWithoutFields();
$this
->checkModuleUninstall();
}
protected abstract function checkServerBackend();
protected abstract function updateIndex();
protected abstract function checkSecondServer();
protected abstract function checkModuleUninstall();
protected function checkBackendSpecificFeatures() {
}
protected function backendSpecificRegressionTests() {
}
protected function checkDefaultServer() {
$server = $this
->getServer();
$this
->assertInstanceOf(Server::class, $server, 'The server was successfully created.');
}
protected function checkDefaultIndex() {
$index = $this
->getIndex();
$this
->assertInstanceOf(Index::class, $index, 'The index was successfully created.');
$this
->assertEquals([
"entity:entity_test_mulrev_changed",
], $index
->getDatasourceIds(), 'Datasources are set correctly.');
$this
->assertEquals('default', $index
->getTrackerId(), 'Tracker is set correctly.');
$this
->assertEquals(5, $index
->getTrackerInstance()
->getTotalItemsCount(), 'Correct item count.');
$this
->assertEquals(0, $index
->getTrackerInstance()
->getIndexedItemsCount(), 'All items still need to be indexed.');
}
protected function enableHtmlFilter() {
$index = $this
->getIndex();
$processor = \Drupal::getContainer()
->get('search_api.plugin_helper')
->createProcessorPlugin($index, 'html_filter');
$index
->addProcessor($processor)
->save();
$this
->assertArrayHasKey('html_filter', $index
->getProcessors(), 'HTML filter processor is added.');
}
protected function disableHtmlFilter() {
$index = $this
->getIndex();
$index
->removeProcessor('html_filter');
$index
->save();
$this
->assertArrayNotHasKey('html_filter', $index
->getProcessors(), 'HTML filter processor is removed.');
}
protected function buildSearch($keys = NULL, array $conditions = [], array $fields = NULL, $place_id_sort = TRUE) {
static $i = 0;
$query = $this
->getIndex()
->query();
if ($keys) {
$query
->keys($keys);
if ($fields) {
$query
->setFulltextFields($fields);
}
}
foreach ($conditions as $condition) {
list($field, $value) = explode(',', $condition, 2);
$query
->addCondition($field, $value);
}
$query
->range(0, 10);
if ($place_id_sort) {
$query
->sort(++$i % 2 ? 'id' : 'search_api_id');
}
return $query;
}
protected function searchNoResults() {
$results = $this
->buildSearch('test')
->execute();
$this
->assertResults([], $results, 'Search before indexing');
}
protected function searchSuccess() {
$results = $this
->buildSearch('test')
->range(1, 2)
->execute();
$this
->assertEquals(4, $results
->getResultCount(), 'Search for »test« returned correct number of results.');
$this
->assertEquals($this
->getItemIds([
2,
3,
]), array_keys($results
->getResultItems()), 'Search for »test« returned correct result.');
$this
->assertEmpty($results
->getIgnoredSearchKeys());
$this
->assertEmpty($results
->getWarnings());
$id = $this
->getItemIds([
2,
])[0];
$this
->assertEquals($id, key($results
->getResultItems()));
$this
->assertEquals($id, $results
->getResultItems()[$id]
->getId());
$this
->assertEquals('entity:entity_test_mulrev_changed', $results
->getResultItems()[$id]
->getDatasourceId());
$results = $this
->buildSearch('test foo')
->execute();
$this
->assertResults([
1,
2,
4,
], $results, 'Search for »test foo«');
$results = $this
->buildSearch('foo', [
'type,item',
])
->execute();
$this
->assertResults([
1,
2,
], $results, 'Search for »foo«');
$keys = [
'#conjunction' => 'AND',
'test',
[
'#conjunction' => 'OR',
'baz',
'foobar',
],
[
'#conjunction' => 'OR',
'#negation' => TRUE,
'bar',
'fooblob',
],
];
$results = $this
->buildSearch($keys)
->execute();
$this
->assertResults([
4,
], $results, 'Complex search 1');
$query = $this
->buildSearch();
$conditions = $query
->createConditionGroup('OR');
$conditions
->addCondition('name', 'bar');
$conditions
->addCondition('body', 'bar');
$query
->addConditionGroup($conditions);
$results = $query
->execute();
$this
->assertResults([
1,
2,
3,
5,
], $results, 'Search with multi-field fulltext filter');
$results = $this
->buildSearch()
->addCondition('keywords', [
'grape',
'apple',
], 'IN')
->execute();
$this
->assertResults([
2,
4,
5,
], $results, 'Query with IN filter');
$results = $this
->buildSearch()
->addCondition('keywords', [
'grape',
'apple',
], 'NOT IN')
->execute();
$this
->assertResults([
1,
3,
], $results, 'Query with NOT IN filter');
$results = $this
->buildSearch()
->addCondition('width', [
'0.9',
'1.5',
], 'BETWEEN')
->execute();
$this
->assertResults([
4,
], $results, 'Query with BETWEEN filter');
$results = $this
->buildSearch()
->addCondition('width', [
'0.9',
'1.5',
], 'NOT BETWEEN')
->execute();
$this
->assertResults([
1,
2,
3,
5,
], $results, 'Query with NOT BETWEEN filter');
$results = $this
->buildSearch()
->setLanguages([
'und',
'en',
])
->addCondition('keywords', [
'grape',
'apple',
], 'IN')
->execute();
$this
->assertResults([
2,
4,
5,
], $results, 'Query with IN filter');
$results = $this
->buildSearch()
->setLanguages([
'und',
])
->execute();
$this
->assertResults([], $results, 'Query with languages');
$query = $this
->buildSearch();
$conditions = $query
->createConditionGroup('OR')
->addCondition('search_api_language', 'und')
->addCondition('width', [
'0.9',
'1.5',
], 'BETWEEN');
$query
->addConditionGroup($conditions);
$results = $query
->execute();
$this
->assertResults([
4,
], $results, 'Query with search_api_language filter');
$results = $this
->buildSearch()
->addCondition('search_api_language', 'und')
->addCondition('width', [
'0.9',
'1.5',
], 'BETWEEN')
->execute();
$this
->assertResults([], $results, 'Query with search_api_language filter');
$results = $this
->buildSearch()
->addCondition('search_api_language', [
'und',
'en',
], 'IN')
->addCondition('width', [
'0.9',
'1.5',
], 'BETWEEN')
->execute();
$this
->assertResults([
4,
], $results, 'Query with search_api_language filter');
$results = $this
->buildSearch()
->addCondition('search_api_language', [
'und',
'de',
], 'NOT IN')
->addCondition('width', [
'0.9',
'1.5',
], 'BETWEEN')
->execute();
$this
->assertResults([
4,
], $results, 'Query with search_api_language "NOT IN" filter');
$results = $this
->buildSearch()
->addCondition('search_api_id', $this
->getItemIds([
1,
])[0])
->execute();
$this
->assertResults([
1,
], $results, 'Query with search_api_id filter');
$results = $this
->buildSearch()
->addCondition('search_api_id', $this
->getItemIds([
2,
4,
]), 'NOT IN')
->execute();
$this
->assertResults([
1,
3,
5,
], $results, 'Query with search_api_id "NOT IN" filter');
$results = $this
->buildSearch()
->addCondition('search_api_id', $this
->getItemIds([
3,
])[0], '>')
->execute();
$this
->assertResults([
4,
5,
], $results, 'Query with search_api_id "greater than" filter');
$results = $this
->buildSearch()
->addCondition('search_api_datasource', 'foobar')
->execute();
$this
->assertResults([], $results, 'Query for a non-existing datasource');
$results = $this
->buildSearch()
->addCondition('search_api_datasource', [
'foobar',
'entity:entity_test_mulrev_changed',
], 'IN')
->execute();
$this
->assertResults([
1,
2,
3,
4,
5,
], $results, 'Query with search_api_id "IN" filter');
$results = $this
->buildSearch()
->addCondition('search_api_datasource', [
'foobar',
'entity:entity_test_mulrev_changed',
], 'NOT IN')
->execute();
$this
->assertResults([], $results, 'Query with search_api_id "NOT IN" filter');
$results = $this
->buildSearch(NULL, [], [], FALSE)
->sort('search_api_relevance')
->sort('search_api_datasource', QueryInterface::SORT_DESC)
->sort('search_api_language')
->sort('search_api_id', QueryInterface::SORT_DESC)
->execute();
$this
->assertResults([
5,
4,
3,
2,
1,
], $results, 'Query with magic sorts');
}
protected function checkFacets() {
$query = $this
->buildSearch();
$conditions = $query
->createConditionGroup('OR', [
'facet:' . 'category',
]);
$conditions
->addCondition('category', 'article_category');
$query
->addConditionGroup($conditions);
$facets['category'] = [
'field' => 'category',
'limit' => 0,
'min_count' => 1,
'missing' => TRUE,
'operator' => 'or',
];
$query
->setOption('search_api_facets', $facets);
$results = $query
->execute();
$this
->assertResults([
4,
5,
], $results, 'OR facets query');
$expected = [
[
'count' => 2,
'filter' => '"article_category"',
],
[
'count' => 2,
'filter' => '"item_category"',
],
[
'count' => 1,
'filter' => '!',
],
];
$category_facets = $results
->getExtraData('search_api_facets')['category'];
usort($category_facets, [
$this,
'facetCompare',
]);
$this
->assertEquals($expected, $category_facets, 'Correct OR facets were returned');
$query = $this
->buildSearch();
$conditions = $query
->createConditionGroup('OR', [
'facet:' . 'category',
]);
$conditions
->addCondition('category', 'article_category');
$query
->addConditionGroup($conditions);
$conditions = $query
->createConditionGroup('AND');
$conditions
->addCondition('category', NULL, '<>');
$query
->addConditionGroup($conditions);
$facets['category'] = [
'field' => 'category',
'limit' => 0,
'min_count' => 1,
'missing' => TRUE,
'operator' => 'or',
];
$query
->setOption('search_api_facets', $facets);
$results = $query
->execute();
$this
->assertResults([
4,
5,
], $results, 'OR facets query');
$expected = [
[
'count' => 2,
'filter' => '"article_category"',
],
[
'count' => 2,
'filter' => '"item_category"',
],
];
$category_facets = $results
->getExtraData('search_api_facets')['category'];
usort($category_facets, [
$this,
'facetCompare',
]);
$this
->assertEquals($expected, $category_facets, 'Correct OR facets were returned');
}
protected function regressionTests() {
$this
->regressionTest2007872();
$this
->regressionTest1863672();
$this
->regressionTest2040543();
$this
->regressionTest2111753();
$this
->regressionTest2127001();
$this
->regressionTest2136409();
$this
->regressionTest1658964();
$this
->regressionTest2469547();
$this
->regressionTest1403916();
$this
->regressionTest2783987();
$this
->regressionTest2809753();
$this
->regressionTest2767609();
$this
->regressionTest2745655();
}
protected function regressionTest2007872() {
$results = $this
->buildSearch('test', [], [], FALSE)
->sort('id')
->sort('type')
->execute();
$this
->assertResults([
1,
2,
3,
4,
], $results, 'Sorting on field with NULLs');
$query = $this
->buildSearch(NULL, [], [], FALSE);
$conditions = $query
->createConditionGroup('OR');
$conditions
->addCondition('id', 3);
$conditions
->addCondition('type', 'article');
$query
->addConditionGroup($conditions);
$query
->sort('search_api_id', QueryInterface::SORT_DESC);
$results = $query
->execute();
$this
->assertResults([
5,
4,
3,
], $results, 'OR filter on field with NULLs');
}
protected function regressionTest1863672() {
$query = $this
->buildSearch();
$conditions = $query
->createConditionGroup('OR');
$conditions
->addCondition('keywords', 'orange');
$conditions
->addCondition('keywords', 'apple');
$query
->addConditionGroup($conditions);
$results = $query
->execute();
$this
->assertResults([
1,
2,
4,
5,
], $results, 'OR filter on multi-valued field');
$query = $this
->buildSearch();
$conditions = $query
->createConditionGroup('OR');
$conditions
->addCondition('keywords', 'orange');
$conditions
->addCondition('keywords', 'strawberry');
$query
->addConditionGroup($conditions);
$conditions = $query
->createConditionGroup('OR');
$conditions
->addCondition('keywords', 'apple');
$conditions
->addCondition('keywords', 'grape');
$query
->addConditionGroup($conditions);
$results = $query
->execute();
$this
->assertResults([
2,
4,
5,
], $results, 'Multiple OR filters on multi-valued field');
$query = $this
->buildSearch();
$conditions1 = $query
->createConditionGroup('OR');
$conditions = $query
->createConditionGroup('AND');
$conditions
->addCondition('keywords', 'orange');
$conditions
->addCondition('keywords', 'apple');
$conditions1
->addConditionGroup($conditions);
$conditions = $query
->createConditionGroup('AND');
$conditions
->addCondition('keywords', 'strawberry');
$conditions
->addCondition('keywords', 'grape');
$conditions1
->addConditionGroup($conditions);
$query
->addConditionGroup($conditions1);
$results = $query
->execute();
$this
->assertResults([
2,
4,
5,
], $results, 'Complex nested filters on multi-valued field');
}
protected function regressionTest2040543() {
$query = $this
->buildSearch();
$facets['category'] = [
'field' => 'category',
'limit' => 0,
'min_count' => 1,
'missing' => TRUE,
];
$query
->setOption('search_api_facets', $facets);
$query
->range(0, 0);
$results = $query
->execute();
$expected = [
[
'count' => 2,
'filter' => '"article_category"',
],
[
'count' => 2,
'filter' => '"item_category"',
],
[
'count' => 1,
'filter' => '!',
],
];
$type_facets = $results
->getExtraData('search_api_facets')['category'];
usort($type_facets, [
$this,
'facetCompare',
]);
$this
->assertEquals($expected, $type_facets, 'Correct facets were returned');
$query = $this
->buildSearch();
$facets['category']['missing'] = FALSE;
$query
->setOption('search_api_facets', $facets);
$query
->range(0, 0);
$results = $query
->execute();
$expected = [
[
'count' => 2,
'filter' => '"article_category"',
],
[
'count' => 2,
'filter' => '"item_category"',
],
];
$type_facets = $results
->getExtraData('search_api_facets')['category'];
usort($type_facets, [
$this,
'facetCompare',
]);
$this
->assertEquals($expected, $type_facets, 'Correct facets were returned');
}
protected function regressionTest2111753() {
$keys = [
'#conjunction' => 'OR',
'foo',
'test',
];
$query = $this
->buildSearch($keys, [], [
'name',
]);
$results = $query
->execute();
$this
->assertResults([
1,
2,
4,
], $results, 'OR keywords');
$query = $this
->buildSearch($keys, [], [
'name',
'body',
]);
$query
->range(0, 0);
$results = $query
->execute();
$this
->assertEquals(5, $results
->getResultCount(), 'Multi-field OR keywords returned correct number of results.');
$this
->assertEmpty($results
->getResultItems(), 'Multi-field OR keywords returned correct result.');
$this
->assertEmpty($results
->getIgnoredSearchKeys());
$this
->assertEmpty($results
->getWarnings());
$keys = [
'#conjunction' => 'OR',
'foo',
'test',
[
'#conjunction' => 'AND',
'bar',
'baz',
],
];
$query = $this
->buildSearch($keys, [], [
'name',
]);
$results = $query
->execute();
$this
->assertResults([
1,
2,
4,
5,
], $results, 'Nested OR keywords');
$keys = [
'#conjunction' => 'OR',
[
'#conjunction' => 'AND',
'foo',
'test',
],
[
'#conjunction' => 'AND',
'bar',
'baz',
],
];
$query = $this
->buildSearch($keys, [], [
'name',
'body',
]);
$results = $query
->execute();
$this
->assertResults([
1,
2,
4,
5,
], $results, 'Nested multi-field OR keywords');
}
protected function regressionTest2127001() {
$keys = [
'#conjunction' => 'AND',
'#negation' => TRUE,
'foo',
'bar',
];
$results = $this
->buildSearch($keys)
->execute();
$this
->assertResults([
3,
4,
], $results, 'Negated AND fulltext search');
$keys = [
'#conjunction' => 'OR',
'#negation' => TRUE,
'foo',
'baz',
];
$results = $this
->buildSearch($keys)
->execute();
$this
->assertResults([
3,
], $results, 'Negated OR fulltext search');
$keys = [
'#conjunction' => 'AND',
'test',
[
'#conjunction' => 'AND',
'#negation' => TRUE,
'foo',
'bar',
],
];
$results = $this
->buildSearch($keys)
->execute();
$this
->assertResults([
3,
4,
], $results, 'Nested NOT AND fulltext search');
}
protected function regressionTest2136409() {
$query = $this
->buildSearch();
$query
->addCondition('category', NULL);
$results = $query
->execute();
$this
->assertResults([
3,
], $results, 'NULL filter');
$query = $this
->buildSearch();
$query
->addCondition('category', NULL, '<>');
$results = $query
->execute();
$this
->assertResults([
1,
2,
4,
5,
], $results, 'NOT NULL filter');
}
protected function regressionTest1658964() {
$query = $this
->buildSearch();
$facets['type'] = [
'field' => 'type',
'limit' => 0,
'min_count' => 0,
'missing' => TRUE,
];
$query
->setOption('search_api_facets', $facets);
$query
->addCondition('type', 'article');
$query
->range(0, 0);
$results = $query
->execute();
$expected = [
[
'count' => 2,
'filter' => '"article"',
],
[
'count' => 0,
'filter' => '!',
],
[
'count' => 0,
'filter' => '"item"',
],
];
$facets = $results
->getExtraData('search_api_facets', [])['type'];
usort($facets, [
$this,
'facetCompare',
]);
$this
->assertEquals($expected, $facets, 'Correct facets were returned');
}
protected function regressionTest2469547() {
$query = $this
->buildSearch();
$facets = [];
$facets['body'] = [
'field' => 'body',
'limit' => 0,
'min_count' => 1,
'missing' => FALSE,
];
$query
->setOption('search_api_facets', $facets);
$query
->addCondition('id', 5, '<>');
$query
->range(0, 0);
$results = $query
->execute();
$expected = [
[
'count' => 4,
'filter' => '"test"',
],
[
'count' => 2,
'filter' => '"Case"',
],
[
'count' => 2,
'filter' => '"casE"',
],
[
'count' => 1,
'filter' => '"bar"',
],
[
'count' => 1,
'filter' => '"case"',
],
[
'count' => 1,
'filter' => '"foobar"',
],
];
$facets = $results
->getExtraData('search_api_facets', [])['body'];
usort($facets, [
$this,
'facetCompare',
]);
$this
->assertEquals($expected, $facets, 'Correct facets were returned for a fulltext field.');
}
protected function regressionTest1403916() {
$query = $this
->buildSearch('test foo');
$facets = [];
$facets['type'] = [
'field' => 'type',
'limit' => 0,
'min_count' => 1,
'missing' => TRUE,
];
$query
->setOption('search_api_facets', $facets);
$query
->range(0, 0);
$results = $query
->execute();
$expected = [
[
'count' => 2,
'filter' => '"item"',
],
[
'count' => 1,
'filter' => '"article"',
],
];
$facets = $results
->getExtraData('search_api_facets', [])['type'];
usort($facets, [
$this,
'facetCompare',
]);
$this
->assertEquals($expected, $facets, 'Correct facets were returned');
}
protected function regressionTest2783987() {
$query = $this
->buildSearch('test foo');
$facets = [];
$facets['type'] = [
'field' => 'type',
'limit' => 0,
'min_count' => 2,
'missing' => TRUE,
];
$query
->setOption('search_api_facets', $facets);
$query
->range(0, 0);
$results = $query
->execute();
$expected = [
[
'count' => 2,
'filter' => '"item"',
],
];
$facets = $results
->getExtraData('search_api_facets', [])['type'];
$this
->assertEquals($expected, $facets, 'Correct facets were returned');
}
protected function regressionTest2809753() {
$query = $this
->buildSearch();
$condition_group = $query
->createConditionGroup('OR', [
'facet:type',
]);
$condition_group
->addCondition('type', 'article');
$query
->addConditionGroup($condition_group);
$facets['type'] = [
'field' => 'type',
'limit' => 0,
'min_count' => 1,
'missing' => FALSE,
'operator' => 'or',
];
$facets['category'] = [
'field' => 'category',
'limit' => 0,
'min_count' => 1,
'missing' => FALSE,
'operator' => 'or',
];
$query
->setOption('search_api_facets', $facets);
$results = $query
->execute();
$this
->assertResults([
4,
5,
], $results, 'Multi-facets query');
$expected = [
[
'count' => 3,
'filter' => '"item"',
],
[
'count' => 2,
'filter' => '"article"',
],
];
$type_facets = $results
->getExtraData('search_api_facets')['type'];
usort($type_facets, [
$this,
'facetCompare',
]);
$this
->assertEquals($expected, $type_facets, 'Correct facets were returned for first facet');
$expected = [
[
'count' => 2,
'filter' => '"article_category"',
],
];
$category_facets = $results
->getExtraData('search_api_facets')['category'];
$this
->assertEquals($expected, $category_facets, 'Correct facets were returned for second facet');
}
protected function regressionTest2767609() {
$results = $this
->buildSearch(NULL, [
'type,',
])
->execute();
$this
->assertResults([], $results, 'Search for empty-string type');
$results = $this
->buildSearch(NULL, [
'category,',
])
->execute();
$this
->assertResults([], $results, 'Search for empty-string category');
$results = $this
->buildSearch()
->addCondition('category', '', '<>')
->execute();
$this
->assertResults([
1,
2,
3,
4,
5,
], $results, 'Search for items with category not an empty string');
$results = $this
->buildSearch()
->addCondition('category', '', '<')
->addCondition('category', [
'',
'foo',
], 'BETWEEN')
->addCondition('category', [
'',
'a',
'b',
], 'NOT IN')
->execute();
$this
->assertResults([], $results, 'Search with various empty-string filters');
}
protected function regressionTest2745655() {
$name = $this->entities[3]->name[0]->value;
$this->entities[3]->name[0]->value = NULL;
$this->entities[3]
->save();
$this
->indexItems($this->indexId);
$results = $this
->buildSearch()
->addCondition('name', NULL)
->execute();
$this
->assertResults([
3,
], $results, 'Search for items without name');
$results = $this
->buildSearch()
->addCondition('name', NULL, '<>')
->execute();
$this
->assertResults([
1,
2,
4,
5,
], $results, 'Search for items with name');
$this->entities[3]
->set('name', [
$name,
]);
$this->entities[3]
->save();
$this
->indexItems($this->indexId);
}
protected function facetCompare(array $a, array $b) {
if ($a['count'] != $b['count']) {
return $b['count'] - $a['count'];
}
return strcmp($a['filter'], $b['filter']);
}
protected function clearIndex() {
$this
->getIndex()
->clear();
}
protected function regressionTests2() {
FieldStorageConfig::create([
'field_name' => 'prices',
'entity_type' => 'entity_test_mulrev_changed',
'type' => 'decimal',
'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
])
->save();
FieldConfig::create([
'field_name' => 'prices',
'entity_type' => 'entity_test_mulrev_changed',
'bundle' => 'item',
'label' => 'Prices',
])
->save();
$this
->regressionTest1916474();
$this
->regressionTest2284199();
$this
->regressionTest2471509();
$this
->regressionTest2616804();
}
protected function regressionTest1916474() {
$index = $this
->getIndex();
$this
->addField($index, 'prices', 'decimal');
$success = $index
->save();
$this
->assertNotEmpty($success, 'The index field settings were successfully changed.');
$this
->resetEntityCache('server');
$this
->resetEntityCache();
$this
->addTestEntity(6, [
'prices' => [
'3.5',
'3.25',
'3.75',
'3.5',
],
'type' => 'item',
]);
$this
->indexItems($this->indexId);
$query = $this
->buildSearch(NULL, [
'prices,3.25',
]);
$results = $query
->execute();
$this
->assertResults([
6,
], $results, 'Filter on decimal field');
$query = $this
->buildSearch(NULL, [
'prices,3.5',
]);
$results = $query
->execute();
$this
->assertResults([
6,
], $results, 'Filter on decimal field');
$query = $this
->buildSearch()
->addCondition('prices', [
3.6,
3.8,
], 'BETWEEN');
$results = $query
->execute();
$this
->assertResults([
6,
], $results, 'BETWEEN filter on multi-valued field');
$query = $this
->buildSearch()
->addCondition('prices', [
3.6,
3.8,
], 'NOT BETWEEN');
$results = $query
->execute();
$this
->assertResults([
1,
2,
3,
4,
5,
], $results, 'NOT BETWEEN filter on multi-valued field');
}
protected function regressionTest2284199() {
$this
->addTestEntity(7, [
'type' => 'item',
]);
$count = $this
->indexItems($this->indexId);
$this
->assertEquals(1, $count, 'Indexing an item with an empty value for a non string field worked.');
}
protected function regressionTest2471509() {
$this
->addTestEntity(8, [
'name' => 'Article with long body',
'type' => 'article',
'body' => 'astringlongerthanfiftycharactersthatcantbestoredbythedbbackend',
]);
$count = $this
->indexItems($this->indexId);
$this
->assertEquals(1, $count, 'Indexing an item with a word longer than 50 characters worked.');
$index = $this
->getIndex();
$index
->getField('body')
->setType('string');
$index
->save();
$count = $this
->indexItems($this->indexId);
$this
->assertEquals(count($this->entities), $count, 'Switching type from text to string worked.');
$query = $this
->buildSearch(NULL, [
'body,astringlongerthanfiftycharactersthatcantbestoredbythedbbackend',
]);
$results = $query
->execute();
$this
->assertResults([
8,
], $results, 'Filter on new string field');
$index
->getField('body')
->setType('text');
$index
->save();
$count = $this
->indexItems($this->indexId);
$this
->assertEquals(count($this->entities), $count, 'All items needed to be re-indexed after switching type from string to text.');
}
protected function regressionTest2616804() {
$mb_word = 'äöüßáŧæøðđŋħĸµäöüßáŧæøðđŋħĸµ';
$mb_body = implode(' ', array_fill(0, 8, $mb_word));
$this
->addTestEntity(9, [
'name' => 'Test item 9',
'type' => 'item',
'body' => $mb_body,
]);
$count = $this
->indexItems($this->indexId);
$this
->assertEquals(1, $count, 'Indexing an item with a word with 28 multi-byte characters worked.');
$query = $this
->buildSearch($mb_word);
$results = $query
->execute();
$this
->assertResults([
9,
], $results, 'Search for word with 28 multi-byte characters');
$query = $this
->buildSearch($mb_word . 'ä');
$results = $query
->execute();
$this
->assertResults([], $results, 'Search for unknown word with 29 multi-byte characters');
$index = $this
->getIndex();
$index
->getField('body')
->setType('string');
$index
->save();
$entity_count = count($this->entities);
$count = $this
->indexItems($this->indexId);
$this
->assertEquals($entity_count, $count, 'Switching type from text to string worked.');
$query = $this
->buildSearch(NULL, [
"body,{$mb_body}",
]);
$results = $query
->execute();
$this
->assertResults([
9,
], $results, 'Search for body with 231 multi-byte characters');
$query = $this
->buildSearch(NULL, [
"body,{$mb_body}ä",
]);
$results = $query
->execute();
$this
->assertResults([], $results, 'Search for unknown body with 232 multi-byte characters');
$index
->getField('body')
->setType('text');
$index
->save();
}
protected function checkIndexWithoutFields() {
$index = Index::create([
'id' => 'test_index_2',
'name' => 'Test index 2',
'status' => TRUE,
'server' => $this->serverId,
'datasource_settings' => [
'entity:entity_test_mulrev_changed' => [],
],
'tracker_settings' => [
'default' => [],
],
]);
$index
->save();
$indexed_count = $this
->indexItems($index
->id());
$this
->assertEquals(count($this->entities), $indexed_count);
$search_count = $index
->query()
->execute()
->getResultCount();
$this
->assertEquals(count($this->entities), $search_count);
return $index;
}
protected function assertResults(array $result_ids, ResultSetInterface $results, $search_label = 'Search', array $ignored = [], array $warnings = []) {
$this
->assertEquals(count($result_ids), $results
->getResultCount(), "{$search_label} returned correct number of results.");
if ($result_ids) {
$this
->assertEquals($this
->getItemIds($result_ids), array_keys($results
->getResultItems()), "{$search_label} returned correct results.");
}
$this
->assertEquals($ignored, $results
->getIgnoredSearchKeys());
$this
->assertEquals($warnings, $results
->getWarnings());
}
protected function getServer() {
return Server::load($this->serverId);
}
protected function getIndex() {
return Index::load($this->indexId);
}
protected function addField(IndexInterface $index, $property_name, $type = 'text') {
$field_info = [
'label' => $property_name,
'type' => $type,
'datasource_id' => 'entity:entity_test_mulrev_changed',
'property_path' => $property_name,
];
$field = \Drupal::getContainer()
->get('search_api.fields_helper')
->createField($index, $property_name, $field_info);
$index
->addField($field);
$index
->save();
}
protected function resetEntityCache($type = 'index') {
$entity_type_id = 'search_api_' . $type;
\Drupal::entityTypeManager()
->getStorage($entity_type_id)
->resetCache([
$this->{$type . 'Id'},
]);
}
}