You are here

public function SearchApiSolrMultilingualTest::testLanguageLimitedByOptions in Search API Solr 8.2

Tests language limiting via options.

File

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

Class

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

Namespace

Drupal\Tests\search_api_solr\Kernel

Code

public function testLanguageLimitedByOptions() {
  $this
    ->insertMultilingualExampleContent();
  $this
    ->indexItems($this->indexId);
  $server = $this
    ->getIndex()
    ->getServerInstance();
  $config = $server
    ->getBackendConfig();
  $config['sasm_limit_search_page_to_content_language'] = FALSE;
  $server
    ->setBackendConfig($config)
    ->save();
  $this
    ->assertFalse($this
    ->getIndex()
    ->getServerInstance()
    ->getBackendConfig()['sasm_limit_search_page_to_content_language']);
  $config['sasm_search_page_include_language_independent'] = FALSE;
  $server
    ->setBackendConfig($config)
    ->save();
  $this
    ->assertFalse($this
    ->getIndex()
    ->getServerInstance()
    ->getBackendConfig()['sasm_search_page_include_language_independent']);

  // Stemming "en":
  // gene => gene
  // genes => gene
  //
  // Stemming "de":
  // Gen => gen
  // Gene => gen.
  $results = $this
    ->buildSearch('gene', [], [
    'body',
  ])
    ->execute();
  $this
    ->assertResults([
    1 => 'en',
    2 => 'en',
    3 => 'de',
    4 => 'de',
    5 => 'de-at',
    6 => 'de-at',
  ], $results, 'Search all languages for "gene".');
  $config['sasm_limit_search_page_to_content_language'] = TRUE;
  $server
    ->setBackendConfig($config)
    ->save();
  $this
    ->assertTrue($this
    ->getIndex()
    ->getServerInstance()
    ->getBackendConfig()['sasm_limit_search_page_to_content_language']);

  // Current content language is "en".
  $results = $this
    ->buildSearch('gene', [], [
    'body',
  ])
    ->execute();
  $this
    ->assertResults([
    1 => 'en',
    2 => 'en',
  ], $results, 'Search content language for "gene".');

  // A query created by Views must not be overruled.
  $results = $this
    ->buildSearch('gene', [], [
    'body',
  ])
    ->addTag('views')
    ->execute();
  $this
    ->assertResults([
    1 => 'en',
    2 => 'en',
    3 => 'de',
    4 => 'de',
    5 => 'de-at',
    6 => 'de-at',
  ], $results, 'Search all languages for "gene".');
  $config['sasm_search_page_include_language_independent'] = TRUE;
  $server
    ->setBackendConfig($config)
    ->save();
  $this
    ->assertTrue($this
    ->getIndex()
    ->getServerInstance()
    ->getBackendConfig()['sasm_search_page_include_language_independent']);
  $results = $this
    ->buildSearch('gene', [], [
    'body',
  ])
    ->execute();
  $this
    ->assertResults([
    1 => 'en',
    2 => 'en',
    7 => LanguageInterface::LANGCODE_NOT_SPECIFIED,
    8 => LanguageInterface::LANGCODE_NOT_APPLICABLE,
  ], $results, 'Search content and unspecified language for "gene".');
  $config['sasm_limit_search_page_to_content_language'] = FALSE;
  $server
    ->setBackendConfig($config)
    ->save();
  $this
    ->assertFalse($this
    ->getIndex()
    ->getServerInstance()
    ->getBackendConfig()['sasm_limit_search_page_to_content_language']);
  $results = $this
    ->buildSearch('gene', [], [
    'body',
  ])
    ->execute();
  $this
    ->assertResults([
    1 => 'en',
    2 => 'en',
    3 => 'de',
    4 => 'de',
    5 => 'de-at',
    6 => 'de-at',
    7 => LanguageInterface::LANGCODE_NOT_SPECIFIED,
    8 => LanguageInterface::LANGCODE_NOT_APPLICABLE,
  ], $results, 'Search all and unspecified languages for "gene".');
}