public function SearchApiSolrTest::testDatasourceAdditionAndDeletion in Search API Solr 8
Same name and namespace in other branches
- 8.2 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 627
Class
- SearchApiSolrTest
- Tests index and search capabilities using the Solr search backend.
Namespace
Drupal\Tests\search_api_solr\KernelCode
public function testDatasourceAdditionAndDeletion() {
// Only run the tests if we have a Solr core available.
if ($this->solrAvailable) {
$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.');
$this
->getIndex()
->removeDatasource('entity:user')
->save();
// Wait for the commitWithin 1 second to complete the deletion.
sleep(2);
$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());
}
}
else {
$this
->assertTrue(TRUE, 'Error: The Solr instance could not be found. Please enable a multi-core one on http://localhost:8983/solr/drupal');
}
}