You are here

public function SearchApiSolrMultilingualTest::testLanguageFallback in Search API Solr 8.2

Tests language fallback.

File

tests/src/Kernel/SearchApiSolrMultilingualTest.php, line 106

Class

SearchApiSolrMultilingualTest
Tests index and search capabilities using the Solr search backend.

Namespace

Drupal\Tests\search_api_solr\Kernel

Code

public function testLanguageFallback() {
  $server = $this
    ->getIndex()
    ->getServerInstance();
  $config = $server
    ->getBackendConfig();
  $config['sasm_language_unspecific_fallback_on_schema_issues'] = FALSE;
  $server
    ->setBackendConfig($config)
    ->save();
  $this
    ->assertFalse($this
    ->getIndex()
    ->getServerInstance()
    ->getBackendConfig()['sasm_language_unspecific_fallback_on_schema_issues']);
  $this
    ->insertMultilingualExampleContent();
  $this
    ->indexItems($this->indexId);
  $this
    ->assertLogMessage(LOG_ERR, '%type while trying to index items on index %index: @message in %function (line %line of %file)');
  $this
    ->clearIndex();
  $config['sasm_language_unspecific_fallback_on_schema_issues'] = TRUE;
  $server
    ->setBackendConfig($config)
    ->save();
  $this
    ->assertTrue($this
    ->getIndex()
    ->getServerInstance()
    ->getBackendConfig()['sasm_language_unspecific_fallback_on_schema_issues']);
  $this
    ->indexItems($this->indexId);
  $results = $this
    ->buildSearch()
    ->execute();
  $this
    ->assertEquals(6, $results
    ->getResultCount(), 'Number of indexed entities is correct.');

  // Stemming "en":
  // gene => gene
  // genes => gene
  //
  // Stemming "de":
  // Gen => gen
  // Gene => gen.
  $query = $this
    ->buildSearch('Gen');
  $query
    ->setLanguages([
    'en',
    'de',
  ]);
  $results = $query
    ->execute();
  $this
    ->assertEquals(2, $results
    ->getResultCount(), 'Two results for "Gen" in German entities. No results for "Gen" in English entities.');
  $query = $this
    ->buildSearch('Gene');
  $query
    ->setLanguages([
    'en',
    'de',
  ]);
  $results = $query
    ->execute();
  $this
    ->assertEquals(4, $results
    ->getResultCount(), 'Two results for "Gene" in German entities. Two results for "Gene" in English entities.');

  // Stemming of "de-at" should fall back to "de".
  $query = $this
    ->buildSearch('Gen');
  $query
    ->setLanguages([
    'de-at',
  ]);
  $results = $query
    ->execute();
  $this
    ->assertEquals(2, $results
    ->getResultCount(), 'Two results for "Gen" in Austrian entities.');
  $query = $this
    ->buildSearch('Gene');
  $query
    ->setLanguages([
    'de-at',
  ]);
  $results = $query
    ->execute();
  $this
    ->assertEquals(2, $results
    ->getResultCount(), 'Two results for "Gene" in Austrian entities.');
}