public function SearchApiSolrTest::testDatasourceAdditionAndDeletion in Search API Solr 8.2
Same name and namespace in other branches
- 8 tests/src/Kernel/SearchApiSolrTest.php \Drupal\Tests\search_api_solr\Kernel\SearchApiSolrTest::testDatasourceAdditionAndDeletion()
Tests addition and deletion of a data source.
File
- tests/
src/ Kernel/ SearchApiSolrTest.php, line 639
Class
- SearchApiSolrTest
- Tests index and search capabilities using the Solr search backend.
Namespace
Drupal\Tests\search_api_solr\KernelCode
public function testDatasourceAdditionAndDeletion() {
$this
->insertExampleContent();
$this
->indexItems($this->indexId);
$results = $this
->buildSearch()
->execute();
$this
->assertEquals(5, $results
->getResultCount(), 'Number of indexed entities is correct.');
try {
$results = $this
->buildSearch()
->addCondition('uid', 0, '>')
->execute();
$this
->fail('Field uid must not yet exists in this index.');
} catch (\Exception $e) {
$this
->assertEquals('Filter term on unknown or unindexed field uid.', $e
->getMessage());
}
$index = $this
->getIndex();
$index
->set('datasource_settings', $index
->get('datasource_settings') + [
'entity:user' => [],
]);
$info = [
'label' => 'uid',
'type' => 'integer',
'datasource_id' => 'entity:user',
'property_path' => 'uid',
];
$index
->addField($this->fieldsHelper
->createField($index, 'uid', $info));
$index
->save();
User::create([
'uid' => 1,
'name' => 'root',
'langcode' => 'en',
])
->save();
$this
->indexItems($this->indexId);
$results = $this
->buildSearch()
->execute();
$this
->assertEquals(6, $results
->getResultCount(), 'Number of indexed entities in multi datasource index is correct.');
$results = $this
->buildSearch()
->addCondition('uid', 0, '>')
->execute();
$this
->assertEquals(1, $results
->getResultCount(), 'Search for users returned correct number of results.');
$index = $this
->getIndex();
$index
->removeDatasource('entity:user')
->save();
$this
->ensureCommit($index
->getServerInstance());
$results = $this
->buildSearch()
->execute();
$this
->assertEquals(5, $results
->getResultCount(), 'Number of indexed entities is correct.');
try {
$results = $this
->buildSearch()
->addCondition('uid', 0, '>')
->execute();
$this
->fail('Field uid must not yet exists in this index.');
} catch (\Exception $e) {
$this
->assertEquals('Filter term on unknown or unindexed field uid.', $e
->getMessage());
}
}