You are here

public function SearchApiSolrTest::testLanguageFallbackAndLanguageLimitedByOptions in Search API Solr 8.3

Same name and namespace in other branches
  1. 4.x tests/src/Kernel/SearchApiSolrTest.php \Drupal\Tests\search_api_solr\Kernel\SearchApiSolrTest::testLanguageFallbackAndLanguageLimitedByOptions()

Tests language fallback and language limiting via options.

File

tests/src/Kernel/SearchApiSolrTest.php, line 1099

Class

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

Namespace

Drupal\Tests\search_api_solr\Kernel

Code

public function testLanguageFallbackAndLanguageLimitedByOptions() {
  $this->travisLogger
    ->debug('SearchApiSolrTest::testLanguageFallbackAndLanguageLimitedByOptions()');
  $this
    ->insertMultilingualExampleContent();
  $this
    ->indexItems($this->indexId);
  $index = $this
    ->getIndex();
  $connector = $index
    ->getServerInstance()
    ->getBackend()
    ->getSolrConnector();
  $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
    ->sort('name');
  $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.');
  $params = $connector
    ->getRequestParams();
  $this
    ->assertEquals('ss_search_api_language:("en" "de")', $params['fq'][1]);
  $this
    ->assertEquals('ss_search_api_id asc,sort_X3b_en_name asc', $params['sort'][0]);
  $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.');
  $params = $connector
    ->getRequestParams();
  $this
    ->assertEquals('ss_search_api_language:"de-at"', $params['fq'][1]);
  $settings = $index
    ->getThirdPartySettings('search_api_solr');
  $settings['multilingual']['limit_to_content_language'] = FALSE;
  $settings['multilingual']['include_language_independent'] = FALSE;
  $index
    ->setThirdPartySetting('search_api_solr', 'multilingual', $settings['multilingual']);
  $index
    ->save();
  $this
    ->assertFalse($this
    ->getIndex()
    ->getThirdPartySetting('search_api_solr', 'multilingual')['limit_to_content_language']);
  $this
    ->assertFalse($this
    ->getIndex()
    ->getThirdPartySetting('search_api_solr', 'multilingual')['include_language_independent']);

  // Stemming "en":
  // gene => gene
  // genes => gene
  //
  // Stemming "de":
  // Gen => gen
  // Gene => gen.
  $results = $this
    ->buildSearch('gene', [], [
    'body',
  ])
    ->execute();
  $expected_results = [
    1 => 'en',
    2 => 'en',
    3 => 'de',
    4 => 'de',
    5 => 'de-at',
    6 => 'de-at',
  ];
  $this
    ->assertResults($expected_results, $results, 'Search all languages for "gene".');
  $settings['multilingual']['limit_to_content_language'] = TRUE;
  $index
    ->setThirdPartySetting('search_api_solr', 'multilingual', $settings['multilingual']);
  $index
    ->save();
  $this
    ->assertTrue($this
    ->getIndex()
    ->getThirdPartySetting('search_api_solr', 'multilingual')['limit_to_content_language']);

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

  // A query created by Views must not be overruled.
  $results = $this
    ->buildSearch('gene', [], [
    'body',
  ])
    ->addTag('views')
    ->execute();
  $expected_results = [
    1 => 'en',
    2 => 'en',
    3 => 'de',
    4 => 'de',
    5 => 'de-at',
    6 => 'de-at',
  ];
  $this
    ->assertResults($expected_results, $results, 'Search all languages for "gene".');
  $settings['multilingual']['include_language_independent'] = TRUE;
  $index
    ->setThirdPartySetting('search_api_solr', 'multilingual', $settings['multilingual']);
  $index
    ->save();
  $this
    ->assertTrue($this
    ->getIndex()
    ->getThirdPartySetting('search_api_solr', 'multilingual')['include_language_independent']);
  $results = $this
    ->buildSearch('gene', [], [
    'body',
  ])
    ->execute();
  $expected_results = [
    1 => 'en',
    2 => 'en',
    7 => LanguageInterface::LANGCODE_NOT_SPECIFIED,
    8 => LanguageInterface::LANGCODE_NOT_APPLICABLE,
  ];
  $this
    ->assertResults($expected_results, $results, 'Search content and unspecified language for "gene".');
  $settings['multilingual']['limit_to_content_language'] = FALSE;
  $index
    ->setThirdPartySetting('search_api_solr', 'multilingual', $settings['multilingual']);
  $index
    ->save();
  $this
    ->assertFalse($this
    ->getIndex()
    ->getThirdPartySetting('search_api_solr', 'multilingual')['limit_to_content_language']);
  $results = $this
    ->buildSearch('gene', [], [
    'body',
  ])
    ->execute();
  $expected_results = [
    1 => 'en',
    2 => 'en',
    3 => 'de',
    4 => 'de',
    5 => 'de-at',
    6 => 'de-at',
    7 => LanguageInterface::LANGCODE_NOT_SPECIFIED,
    8 => LanguageInterface::LANGCODE_NOT_APPLICABLE,
  ];
  $this
    ->assertResults($expected_results, $results, 'Search all and unspecified languages for "gene".');
  $this
    ->assertFalse($this
    ->getIndex()
    ->isReindexing());
  ConfigurableLanguage::createFromLangcode('de-ch')
    ->save();
  $this
    ->assertTrue($this
    ->getIndex()
    ->isReindexing());
}